Skip to content

Commit cd5a0b6

Browse files
Merge pull request #138 from CadQuery/adam-urbanczyk-release_prep
Prepare for 0.1 release
2 parents 97e1253 + 326bea9 commit cd5a0b6

File tree

8 files changed

+97
-29
lines changed

8 files changed

+97
-29
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ install:
3030
- cmd: activate cqgui
3131
- conda list
3232
- conda install -c conda-forge pytest=5.2.0 pluggy pytest-qt
33-
- pip install pytest-xvfb pytest-mock pytest-cov pytest-repeat codecov pyvirtualdisplay==0.2.1
33+
- pip install pytest-xvfb pytest-mock pytest-cov pytest-repeat codecov pyvirtualdisplay==0.2.1 EasyProcess==0.2.10
3434

3535
build: false
3636

cq_editor/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from ._version import __version__

cq_editor/_version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "0.1.0"

cq_editor/main_window.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import sys
22

3-
from PyQt5.QtWidgets import (QLabel, QMainWindow, QMessageBox, QToolBar,
4-
QDockWidget, QAction)
3+
from PyQt5.QtWidgets import (QLabel, QMainWindow, QToolBar, QDockWidget, QAction)
54

65
import cadquery as cq
76

@@ -14,7 +13,8 @@
1413
from .widgets.cq_object_inspector import CQObjectInspector
1514
from .widgets.log import LogViewer
1615

17-
from .utils import dock, add_actions, open_url, about_dialog, check_gtihub_for_updates
16+
from . import __version__
17+
from .utils import dock, add_actions, open_url, about_dialog, check_gtihub_for_updates, confirm
1818
from .mixins import MainMixin
1919
from .icons import icon
2020
from .preferences import PreferencesWidget
@@ -63,10 +63,9 @@ def closeEvent(self,event):
6363

6464
if self.components['editor'].document().isModified():
6565

66-
rv = QMessageBox.question(self, 'Confirm close',
67-
'Close without saving?',
68-
QMessageBox.Yes, QMessageBox.No)
69-
if rv == QMessageBox.Yes:
66+
rv = confirm(self, 'Confirm close', 'Close without saving?')
67+
68+
if rv:
7069
event.accept()
7170
super(MainWindow,self).closeEvent(event)
7271
else:
@@ -308,9 +307,11 @@ def edit_preferences(self):
308307

309308
def about(self):
310309

311-
about_dialog(self,
312-
'CadQuery GUI (PyQT)',
313-
'Experimental PyQt GUI for CadQuery')
310+
about_dialog(
311+
self,
312+
'CadQuery GUI (PyQT)',
313+
f'Experimental PyQt GUI for CadQuery.\nVersion: {__version__}.'
314+
)
314315

315316
def check_for_cq_updates(self):
316317

cq_editor/utils.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from PyQt5 import QtCore, QtWidgets
66
from PyQt5.QtGui import QDesktopServices
77
from PyQt5.QtCore import QUrl
8-
from PyQt5.QtWidgets import QFileDialog
8+
from PyQt5.QtWidgets import QFileDialog, QMessageBox
99

1010
DOCK_POSITIONS = {'right' : QtCore.Qt.RightDockWidgetArea,
1111
'left' : QtCore.Qt.LeftDockWidgetArea,
@@ -94,14 +94,14 @@ def about_dialog(parent,title,text):
9494
def get_save_filename(suffix):
9595

9696
rv,_ = QFileDialog.getSaveFileName(filter='*.{}'.format(suffix))
97-
if rv is not '' and not rv.endswith(suffix): rv += '.'+suffix
97+
if rv != '' and not rv.endswith(suffix): rv += '.'+suffix
9898

9999
return rv
100100

101101
def get_open_filename(suffix, curr_dir):
102102

103103
rv,_ = QFileDialog.getOpenFileName(directory=curr_dir, filter='*.{}'.format(suffix))
104-
if rv is not '' and not rv.endswith(suffix): rv += '.'+suffix
104+
if rv != '' and not rv.endswith(suffix): rv += '.'+suffix
105105

106106
return rv
107107

@@ -127,10 +127,8 @@ def check_gtihub_for_updates(parent,
127127

128128
QtWidgets.QMessageBox.about(parent,title,text)
129129

130+
def confirm(parent,title,msg):
130131

132+
rv = QMessageBox.question(parent, title, msg, QMessageBox.Yes, QMessageBox.No)
131133

132-
133-
134-
135-
136-
134+
return True if rv == QMessageBox.Yes else False

cq_editor/widgets/editor.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pyqtgraph.parametertree import Parameter
1010

1111
from ..mixins import ComponentMixin
12-
from ..utils import get_save_filename, get_open_filename
12+
from ..utils import get_save_filename, get_open_filename, confirm
1313

1414
from ..icons import icon
1515

@@ -112,17 +112,30 @@ def updatePreferences(self,*args):
112112
self.findChild(QAction, 'autoreload') \
113113
.setChecked(self.preferences['Autoreload'])
114114

115+
def confirm_discard(self):
116+
117+
if self.modified:
118+
rv = confirm(self,'Please confirm','Current document is not saved - do you want to continue?')
119+
else:
120+
rv = True
121+
122+
return rv
123+
115124
def new(self):
116125

126+
if not self.confirm_discard(): return
127+
117128
self.set_text('')
118129
self.filename = ''
119130
self.reset_modified()
120131

121132
def open(self):
133+
134+
if not self.confirm_discard(): return
122135

123136
curr_dir = Path(self.filename).abspath().dirname()
124137
fname = get_open_filename(self.EXTENSIONS, curr_dir)
125-
if fname is not '':
138+
if fname != '':
126139
self.load_from_file(fname)
127140

128141
def load_from_file(self,fname):
@@ -133,7 +146,7 @@ def load_from_file(self,fname):
133146

134147
def save(self):
135148

136-
if self._filename is not '':
149+
if self._filename != '':
137150

138151
if self.preferences['Autoreload']:
139152
self._file_watcher.removePath(self.filename)
@@ -150,10 +163,11 @@ def save(self):
150163

151164
else:
152165
self.save_as()
166+
153167
def save_as(self):
154168

155169
fname = get_save_filename(self.EXTENSIONS)
156-
if fname is not '':
170+
if fname != '':
157171
with open(fname,'w') as f:
158172
f.write(self.toPlainText())
159173
self.filename = fname
@@ -194,17 +208,22 @@ def autoreload(self, enabled):
194208
def reset_modified(self):
195209

196210
self.document().setModified(False)
211+
212+
@property
213+
def modified(self):
214+
215+
return self.document().isModified()
197216

198217
def saveComponentState(self,store):
199218

200-
if self.filename is not '':
219+
if self.filename != '':
201220
store.setValue(self.name+'/state',self.filename)
202221

203222
def restoreComponentState(self,store):
204223

205224
filename = store.value(self.name+'/state',self.filename)
206225

207-
if filename and filename is not '':
226+
if filename and filename != '':
208227
try:
209228
self.load_from_file(filename)
210229
except IOError:

setup.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1+
import codecs
2+
import os.path
3+
14
from setuptools import setup, find_packages
25

6+
def read(rel_path):
7+
here = os.path.abspath(os.path.dirname(__file__))
8+
with codecs.open(os.path.join(here, rel_path), 'r') as fp:
9+
return fp.read()
10+
11+
def get_version(rel_path):
12+
for line in read(rel_path).splitlines():
13+
if line.startswith('__version__'):
14+
delim = '"' if '"' in line else "'"
15+
return line.split(delim)[1]
16+
else:
17+
raise RuntimeError("Unable to find version string.")
18+
319
setup(name='CQ-editor',
4-
version='0.1.0dev',
20+
version=get_version('cq_editor/_version.py'),
521
packages=find_packages(),
622
entry_points={
723
'gui_scripts': [

tests/test_app.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,11 @@ def editor(qtbot):
493493

494494
return qtbot, win
495495

496-
def test_editor(monkeypatch,editor):
496+
def conv_line_ends(text):
497+
498+
return '\n'.join(text.splitlines())
497499

498-
def conv_line_ends(text):
499-
return '\n'.join(text.splitlines())
500+
def test_editor(monkeypatch,editor):
500501

501502
qtbot, editor = editor
502503

@@ -1027,3 +1028,34 @@ def get_rgba(ais):
10271028
# check if error occured
10281029
qtbot.wait(100)
10291030
assert('Unknown color format' in log.toPlainText().splitlines()[-1])
1031+
1032+
def test_confirm_new(monkeypatch,editor):
1033+
1034+
qtbot, editor = editor
1035+
1036+
#check that initial state is as expected
1037+
assert(editor.modified == False)
1038+
1039+
editor.document().setPlainText(code)
1040+
assert(editor.modified == True)
1041+
1042+
#monkeypatch the confirmation dialog and run both scenarios
1043+
def cancel(*args, **kwargs):
1044+
return QMessageBox.No
1045+
1046+
def ok(*args, **kwargs):
1047+
return QMessageBox.Yes
1048+
1049+
monkeypatch.setattr(QMessageBox, 'question',
1050+
staticmethod(cancel))
1051+
1052+
editor.new()
1053+
assert(editor.modified == True)
1054+
assert(conv_line_ends(editor.get_text_with_eol()) == code)
1055+
1056+
monkeypatch.setattr(QMessageBox, 'question',
1057+
staticmethod(ok))
1058+
1059+
editor.new()
1060+
assert(editor.modified == False)
1061+
assert(editor.get_text_with_eol() == '')

0 commit comments

Comments
 (0)