From 700a7c6a2978935be21a2ac15e4f85c488f93d00 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Thu, 10 May 2018 20:08:27 +0200 Subject: [PATCH] fix #48 : auto-save on closing tab / app & opening folder when auto-save on --- src/mainwindow.py | 23 ++++++------------ src/sublimeless_zk.py | 54 ++++++++++++++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/mainwindow.py b/src/mainwindow.py index 6c8c657..70cfd07 100644 --- a/src/mainwindow.py +++ b/src/mainwindow.py @@ -4,7 +4,7 @@ from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.Qsci import * -from PyQt5.QtCore import Qt +from PyQt5.QtCore import Qt, pyqtSignal from zkscintilla import ZettelkastenScintilla from zkmdlexer import ZkMdLexer @@ -12,13 +12,14 @@ from settings import base_dir -class MainWindow(QMainWindow): - def __init__(self, theme): +class MainWindow(QMainWindow): + def __init__(self, theme, close_handler): super(MainWindow, self).__init__() # load theme self.theme = theme - + self.close_handler = close_handler + self.setGeometry(300, 200, 900, 600) self.setWindowTitle("Sublimeless Zettelkasten") self.setStyleSheet("QTabBar{font: 8px;}") @@ -256,15 +257,5 @@ def make_saved_searches_editor(self): return editor def closeEvent(self, event): - editor_list = [self.qtabs.widget(i) for i in range(self.qtabs.count())] - for editor in editor_list: - if editor.isModified(): - msg = "You have unsaved changes. Quit anyway?" - buttonReply = QMessageBox.question(self, 'Unsaved Changes', msg, QMessageBox.Yes | QMessageBox.No, - QMessageBox.No) - if buttonReply == QMessageBox.Yes: - pass - else: - event.ignore() - break - + if self.close_handler() == False: + event.ignore() diff --git a/src/sublimeless_zk.py b/src/sublimeless_zk.py index d0d0430..82e035e 100644 --- a/src/sublimeless_zk.py +++ b/src/sublimeless_zk.py @@ -503,7 +503,7 @@ def run(self): Theme.prepare_theme_folder() theme_f = os.path.basename(get_settings().get('theme', 'monokai.json')) theme = Theme(theme_f) - self.gui = MainWindow(theme) + self.gui = MainWindow(theme, self.mainwindow_close_handler) self.gui.setFocus() self.init_actions() self.initMenubar() @@ -652,13 +652,18 @@ def open_folder(self, folder=None): """ """ editor_list = [self.gui.qtabs.widget(i) for i in range(self.gui.qtabs.count())] - for editor in editor_list: - if editor.isModified(): - msg = "You have unsaved changes. Save them first?" - buttonReply = QMessageBox.question(self.gui, 'Save Changes', msg, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) - if buttonReply == QMessageBox.Yes: - self.save_all() - break + + # auto-save if auto-save is on, instead of nagging us + if self.autosave_interval > 0 and editor_list: + self.save_all() + else: + for editor in editor_list: + if editor.isModified(): + msg = "You have unsaved changes. Save them first?" + buttonReply = QMessageBox.question(self.gui, 'Save Changes', msg, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) + if buttonReply == QMessageBox.Yes: + self.save_all() + break if not folder: folder = str(QFileDialog.getExistingDirectory(self.gui, "Select Directory")) if folder: @@ -1287,12 +1292,17 @@ def tab_close_requested(self, index): if self.gui.qtabs.count() == 1: return editor = self.gui.qtabs.widget(index) - if editor.isModified(): - msg = f"You have unsaved changes in {os.path.basename(editor.file_name)} Close anyway?" - buttonReply = QMessageBox.question(editor, 'Unsaved Changes', msg, QMessageBox.Yes | QMessageBox.No, - QMessageBox.No) - if buttonReply == QMessageBox.No: - return # ignore + + # auto-save if auto-save is on, instead of nagging us + if self.autosave_interval > 0: + self.save_all() + else: + if editor.isModified(): + msg = f"You have unsaved changes in {os.path.basename(editor.file_name)} Close anyway?" + buttonReply = QMessageBox.question(editor, 'Unsaved Changes', msg, QMessageBox.Yes | QMessageBox.No, + QMessageBox.No) + if buttonReply == QMessageBox.No: + return # ignore self.gui.qtabs.removeTab(index) def about(self): @@ -1581,6 +1591,22 @@ def goto(self): editor.setCursorPosition(stop_index, 0) # ensure we're below the line editor.setCursorPosition(line_index, 0) + def mainwindow_close_handler(self): + if self.autosave_interval > 0: + self.save_all() + return True + editor_list = [self.gui.qtabs.widget(i) for i in range(self.gui.qtabs.count())] + for editor in editor_list: + if editor.isModified(): + msg = "You have unsaved changes. Quit anyway?" + buttonReply = QMessageBox.question(self.gui, 'Unsaved Changes', msg, QMessageBox.Yes | QMessageBox.No, + QMessageBox.No) + if buttonReply == QMessageBox.Yes: + return True + else: + return False + return True + if __name__ == '__main__': Sublimeless_Zk().run()