Implement saving of main window state and quit action

This commit is contained in:
Kovid Goyal 2013-10-16 10:03:21 +05:30
parent bec396158a
commit c58e7eb2e8
3 changed files with 63 additions and 6 deletions

View File

@ -6,6 +6,8 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
from calibre.utils.config import JSONConfig
tprefs = JSONConfig('tweak_book_gui')
_current_container = None

View File

@ -8,13 +8,13 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import tempfile, shutil
from PyQt4.Qt import QObject
from PyQt4.Qt import QObject, QApplication
from calibre.gui2 import error_dialog, choose_files
from calibre.ptempfile import PersistentTemporaryDirectory
from calibre.ebooks.oeb.polish.main import SUPPORTED
from calibre.ebooks.oeb.polish.container import get_container, clone_container
from calibre.gui2.tweak_book import set_current_container, current_container
from calibre.gui2.tweak_book import set_current_container, current_container, tprefs
from calibre.gui2.tweak_book.undo import GlobalUndoHistory
class Boss(QObject):
@ -72,14 +72,15 @@ class Boss(QObject):
self.global_undo.open_book(container)
self.gui.update_window_title()
self.gui.file_list.build(container, preserve_state=False)
self.gui.action_save.setEnabled(False)
self.update_global_history_actions()
def update_global_history_actions(self):
gu = self.global_undo
for x, text in (('undo', _('&Undo')), ('redo', '&Redo')):
for x, text in (('undo', _('&Revert to before')), ('redo', '&Revert to after')):
ac = getattr(self.gui, 'action_global_%s' % x)
ac.setEnabled(getattr(gu, 'can_' + x))
ac.setText(text + ' ' + getattr(gu, x + '_msg'))
ac.setText(text + ' ' + (getattr(gu, x + '_msg') or '...'))
def add_savepoint(self, msg):
nc = clone_container(current_container(), self.mkdtemp())
@ -113,6 +114,25 @@ class Boss(QObject):
c.remove_from_spine(spine_items)
for name in other_items:
c.remove_item(name)
self.gui.action_save.setEnabled(True)
self.gui.file_list.delete_done(spine_items, other_items)
# TODO: Update other GUI elements
def save_book(self):
pass
def quit(self):
if not self.confirm_quit():
return
self.save_state()
QApplication.instance().quit()
def confirm_quit(self):
return True
def shutdown(self):
self.save_state()
def save_state(self):
with tprefs:
self.gui.save_state()

View File

@ -6,10 +6,10 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
from PyQt4.Qt import QDockWidget, Qt, QLabel, QIcon, QAction
from PyQt4.Qt import QDockWidget, Qt, QLabel, QIcon, QAction, QApplication
from calibre.gui2.main_window import MainWindow
from calibre.gui2.tweak_book import current_container
from calibre.gui2.tweak_book import current_container, tprefs
from calibre.gui2.tweak_book.file_list import FileListWidget
from calibre.gui2.tweak_book.job import BlockingJob
from calibre.gui2.tweak_book.boss import Boss
@ -18,6 +18,7 @@ from calibre.gui2.keyboard import Manager as KeyboardManager
class Main(MainWindow):
APP_NAME = _('Tweak Book')
STATE_VERSION = 0
def __init__(self, opts):
MainWindow.__init__(self, opts, disable_automatic_gc=True)
@ -41,6 +42,9 @@ class Main(MainWindow):
self.setCentralWidget(self.l)
self.boss(self)
g = QApplication.instance().desktop().availableGeometry(self)
self.resize(g.width()-50, g.height()-50)
self.restore_state()
self.keyboard.finalize()
@ -62,12 +66,17 @@ class Main(MainWindow):
_('Revert book to before the last action (Undo)'))
self.action_global_redo = reg('forward.png', _('&Revert to after'), self.boss.do_global_redo, 'global-redo', 'Ctrl+Right',
_('Revert book state to after the next action (Redo)'))
self.action_save = reg('save.png', _('&Save'), self.boss.save_book, 'save-book', 'Ctrl+S', _('Save book'))
self.action_save.setEnabled(False)
self.action_quit = reg('quit.png', _('&Quit'), self.boss.quit, 'quit', 'Ctrl+Q', _('Quit'))
def create_menubar(self):
b = self.menuBar()
f = b.addMenu(_('&File'))
f.addAction(self.action_open_book)
f.addAction(self.action_save)
f.addAction(self.action_quit)
e = b.addMenu(_('&Edit'))
e.addAction(self.action_global_undo)
@ -75,12 +84,15 @@ class Main(MainWindow):
def create_toolbar(self):
self.global_bar = b = self.addToolBar(_('Global'))
b.setObjectName('global_bar') # Needed for saveState
b.addAction(self.action_open_book)
b.addAction(self.action_global_undo)
b.addAction(self.action_global_redo)
b.addAction(self.action_save)
def create_docks(self):
self.file_list_dock = d = QDockWidget(_('&Files Browser'), self)
d.setObjectName('file_list_dock') # Needed for saveState
d.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
self.file_list = FileListWidget(d)
d.setWidget(self.file_list)
@ -92,3 +104,26 @@ class Main(MainWindow):
def update_window_title(self):
self.setWindowTitle(self.current_metadata.title + ' [%s] - %s' %(current_container().book_type.upper(), self.APP_NAME))
def closeEvent(self, e):
if not self.boss.confirm_quit():
e.ignore()
return
try:
self.boss.shutdown()
except:
import traceback
traceback.print_exc()
e.accept()
def save_state(self):
tprefs.set('main_window_geometry', bytearray(self.saveGeometry()))
tprefs.set('main_window_state', bytearray(self.saveState(self.STATE_VERSION)))
def restore_state(self):
geom = tprefs.get('main_window_geometry', None)
if geom is not None:
self.restoreGeometry(geom)
state = tprefs.get('main_window_state', None)
if state is not None:
self.restoreState(state, self.STATE_VERSION)