8000 Prepare for 0.1 release by adam-urbanczyk · Pull Request #138 · CadQuery/CQ-editor · GitHub
[go: up one dir, main page]

Skip to content

Prepare for 0.1 release #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added confirm close/new if document is unsaved
Related to #82
  • Loading branch information
adam-urbanczyk committed May 5, 2020
commit c951310a4be11121a809bc883f6fa6dd8532aaf9
12 changes: 5 additions & 7 deletions cq_editor/main_window.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sys

from PyQt5.QtWidgets import (QLabel, QMainWindow, QMessageBox, QToolBar,
QDockWidget, QAction)
from PyQt5.QtWidgets import (QLabel, QMainWindow, QToolBar, QDockWidget, QAction)

import cadquery as cq

Expand All @@ -15,7 +14,7 @@
from .widgets.log import LogViewer

from . import __version__
from .utils import dock, add_actions, open_url, about_dialog, check_gtihub_for_updates
from .utils import dock, add_actions, open_url, about_dialog, check_gtihub_for_updates, confirm
from .mixins import MainMixin
from .icons import icon
from .preferences import PreferencesWidget
Expand Down Expand Up @@ -64,10 +63,9 @@ def closeEvent(self,event):

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

rv = QMessageBox.question(self, 'Confirm close',
'Close without saving?',
QMessageBox.Yes, QMessageBox.No)
if rv == QMessageBox.Yes:
rv = confirm(self, 'Confirm close', 'Close without saving?')

if rv:
event.accept()
super(MainWindow,self).closeEvent(event)
else:
Expand Down
14 changes: 6 additions & 8 deletions cq_editor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtWidgets import QFileDialog, QMessageBox

DOCK_POSITIONS = {'right' : QtCore.Qt.RightDockWidgetArea,
'left' : QtCore.Qt.LeftDockWidgetArea,
Expand Down Expand Up @@ -94,14 +94,14 @@ def about_dialog(parent,title,text):
def get_save_filename(suffix):

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

return rv

def get_open_filename(suffix, curr_dir):

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

return rv

Expand All @@ -127,10 +127,8 @@ def check_gtihub_for_updates(parent,

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

def confirm(parent,title,msg):

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






return True if rv == QMessageBox.Yes else False
31 changes: 25 additions & 6 deletions cq_editor/widgets/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pyqtgraph.parametertree import Parameter

from ..mixins import ComponentMixin
from ..utils import get_save_filename, get_open_filename
from ..utils import get_save_filename, get_open_filename, confirm

from ..icons import icon

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

def confirm_discard(self):

if self.modified:
rv = confirm(self,'Please confirm','Current document is not saved - do you want to continue?')
else:
rv = True

return rv

def new(self):

if not self.confirm_discard(): return

self.set_text('')
self.filename = ''
self.reset_modified()

def open(self):

if not self.confirm_discard(): return

curr_dir = Path(self.filename).abspath().dirname()
fname = get_open_filename(self.EXTENSIONS, curr_dir)
if fname is not '':
if fname != '':
self.load_from_file(fname)

def load_from_file(self,fname):
Expand All @@ -133,7 +146,7 @@ def load_from_file(self,fname):

def save(self):

if self._filename is not '':
if self._filename != '':

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

else:
self.save_as()

def save_as(self):

fname = get_save_filename(self.EXTENSIONS)
if fname is not '':
if fname != '':
with open(fname,'w') as f:
f.write(self.toPlainText())
self.filename = fname
Expand Down Expand Up @@ -194,17 +208,22 @@ def autoreload(self, enabled):
def reset_modified(self):

self.document().setModified(False)

@property
def modified(self):

return self.document().isModified()

def saveComponentState(self,store):

if self.filename is not '':
if self.filename != '':
store.setValue(self.name+'/state',self.filename)

def restoreComponentState(self,store):

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

if filename and filename is not '':
if filename and filename != '':
try:
self.load_from_file(filename)
except IOError:
Expand Down
0