Skip to content

Commit

Permalink
fix #48 : auto-save on closing tab / app & opening folder when auto-s…
Browse files Browse the repository at this point in the history
…ave on
  • Loading branch information
Rene Schallner committed May 10, 2018
1 parent 7bebcb6 commit 700a7c6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
23 changes: 7 additions & 16 deletions src/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
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
from themes import Theme
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;}")
Expand Down Expand Up @@ -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()
54 changes: 40 additions & 14 deletions src/sublimeless_zk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()

0 comments on commit 700a7c6

Please sign in to comment.