From 45b22e5c4b6f2281079a9441e40195999471ad07 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 2 Nov 2013 18:04:29 +0530 Subject: [PATCH] Allow different shortcuts based on current editor --- src/calibre/gui2/tweak_book/boss.py | 2 +- .../gui2/tweak_book/editor/__init__.py | 2 +- src/calibre/gui2/tweak_book/editor/text.py | 10 +++++ src/calibre/gui2/tweak_book/editor/widget.py | 28 ++++++++++--- src/calibre/gui2/tweak_book/keyboard.py | 40 +++++++++++++++++++ src/calibre/gui2/tweak_book/ui.py | 9 +++-- 6 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 src/calibre/gui2/tweak_book/keyboard.py diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 5a4f9e74d9..a900a9a209 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -219,7 +219,7 @@ class Boss(QObject): editor = self.editors[name] = editor_from_syntax(syntax, self.gui.editor_tabs) self.gui.central.add_editor(name, editor) c = current_container() - editor.load_text(c.decode(c.open(name).read()), syntax=syntax) + editor.load_text(c.decode(c.open(name).read())) self.gui.central.show_editor(editor) def edit_file_requested(self, name, syntax, mime): diff --git a/src/calibre/gui2/tweak_book/editor/__init__.py b/src/calibre/gui2/tweak_book/editor/__init__.py index 0f0c94a549..caf01372b9 100644 --- a/src/calibre/gui2/tweak_book/editor/__init__.py +++ b/src/calibre/gui2/tweak_book/editor/__init__.py @@ -23,5 +23,5 @@ def editor_from_syntax(syntax, parent=None): if syntax not in {'text', 'html', 'css', 'xml'}: return None from calibre.gui2.tweak_book.editor.widget import Editor - return Editor(parent) + return Editor(syntax, parent=parent) diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index d8e1ecb8cd..741deed9c5 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -56,6 +56,16 @@ class TextEdit(QPlainTextEdit): self.updateRequest.connect(self.update_line_number_area) self.line_number_area = LineNumbers(self) + @dynamic_property + def is_modified(self): + ''' True if the document has been modified since it was loaded or since + the last time is_modified was set to False. ''' + def fget(self): + return self.document().isModified() + def fset(self, val): + self.document.setModified(bool(val)) + return property(fget=fget, fset=fset) + def sizeHint(self): return self.size_hint diff --git a/src/calibre/gui2/tweak_book/editor/widget.py b/src/calibre/gui2/tweak_book/editor/widget.py index 0e4c7b482c..36e02c755f 100644 --- a/src/calibre/gui2/tweak_book/editor/widget.py +++ b/src/calibre/gui2/tweak_book/editor/widget.py @@ -6,21 +6,39 @@ from __future__ import (unicode_literals, division, absolute_import, __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' -from PyQt4.Qt import QMainWindow, Qt, QApplication +from PyQt4.Qt import QMainWindow, Qt, QApplication, pyqtSignal from calibre.gui2.tweak_book.editor.text import TextEdit class Editor(QMainWindow): - def __init__(self, parent=None): + modification_state_changed = pyqtSignal(object) + + def __init__(self, syntax, parent=None): QMainWindow.__init__(self, parent) if parent is None: self.setWindowFlags(Qt.Widget) + self.syntax = syntax self.editor = TextEdit(self) self.setCentralWidget(self.editor) + self.editor.modificationChanged.connect(self.modification_state_changed.emit) + self.create_toolbars() + + def load_text(self, raw): + self.editor.load_text(raw, syntax=self.syntax) + + @dynamic_property + def is_modified(self): + def fget(self): + return self.editor.is_modified + def fset(self, val): + self.editor.is_modified = val + return property(fget=fget, fset=fset) + + def create_toolbars(self): + self.action_bar = b = self.addToolBar(_('Edit actions tool bar')) + b.setObjectName('action_bar') # Needed for saveState - def load_text(self, raw, syntax='html'): - self.editor.load_text(raw, syntax=syntax) def launch_editor(path_to_edit, path_is_raw=False, syntax='html'): if path_is_raw: @@ -34,7 +52,7 @@ def launch_editor(path_to_edit, path_is_raw=False, syntax='html'): elif ext in ('css',): syntax = 'css' app = QApplication([]) - t = Editor() + t = Editor(syntax) t.load_text(raw, syntax=syntax) t.show() app.exec_() diff --git a/src/calibre/gui2/tweak_book/keyboard.py b/src/calibre/gui2/tweak_book/keyboard.py new file mode 100644 index 0000000000..6f3a7ca2cb --- /dev/null +++ b/src/calibre/gui2/tweak_book/keyboard.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# vim:fileencoding=utf-8 +from __future__ import (unicode_literals, division, absolute_import, + print_function) + +__license__ = 'GPL v3' +__copyright__ = '2013, Kovid Goyal ' + +from calibre.gui2.keyboard import Manager + +class KeyboardManager(object): + + def __init__(self): + self.modes = {mode: Manager(config_name='shortcuts/tweak-book-%s' % mode) for mode in + ('html', 'css', 'xml', 'other')} + self.actions = {} + self.current_mode = None + + def register_shortcut(self, unique_name, name, default_keys=(), + description=None, action=None, group=None, modes=None): + if modes is None: + modes = tuple(self.modes) + for mode in modes: + self.modes[mode].register_shortcut( + unique_name, name, default_keys=default_keys, description=description, + action=None, group=group) + self.actions[unique_name] = action + + def finalize(self): + for km in self.modes.itervalues(): + km.finalize() + + def set_mode(self, name): + km = self.modes[name] + for un, action in self.actions.iteritems(): + keys = km.keys_map[un] + action.setShortcuts(list(keys)) + self.current_mode = name + + diff --git a/src/calibre/gui2/tweak_book/ui.py b/src/calibre/gui2/tweak_book/ui.py index 8a9e04670a..c170347bd1 100644 --- a/src/calibre/gui2/tweak_book/ui.py +++ b/src/calibre/gui2/tweak_book/ui.py @@ -16,7 +16,7 @@ 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 -from calibre.gui2.keyboard import Manager as KeyboardManager +from calibre.gui2.tweak_book.keyboard import KeyboardManager class Central(QStackedWidget): ' The central widget, hosts the editors ' @@ -65,7 +65,7 @@ class Main(MainWindow): self.container = None self.current_metadata = None self.blocking_job = BlockingJob(self) - self.keyboard = KeyboardManager(parent=self, config_name='shortcuts/tweak') + self.keyboard = KeyboardManager() self.create_actions() self.create_menubar() @@ -88,6 +88,7 @@ class Main(MainWindow): self.restore_state() self.keyboard.finalize() + self.keyboard.set_mode('other') @property def editor_tabs(self): @@ -112,7 +113,7 @@ 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 = reg('save.png', _('&Save'), self.boss.save_book, 'save-book', 'Ctrl+Shift+S', _('Save book')) self.action_save.setEnabled(False) self.action_quit = reg('quit.png', _('&Quit'), self.boss.quit, 'quit', 'Ctrl+Q', _('Quit')) @@ -129,7 +130,7 @@ class Main(MainWindow): e.addAction(self.action_global_redo) def create_toolbar(self): - self.global_bar = b = self.addToolBar(_('Global')) + self.global_bar = b = self.addToolBar(_('Global tool bar')) b.setObjectName('global_bar') # Needed for saveState b.addAction(self.action_open_book) b.addAction(self.action_global_undo)