Allow different shortcuts based on current editor

This commit is contained in:
Kovid Goyal 2013-11-02 18:04:29 +05:30
parent 1328d8bf94
commit 45b22e5c4b
6 changed files with 80 additions and 11 deletions

View File

@ -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):

View File

@ -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)

View File

@ -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

View File

@ -6,21 +6,39 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
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_()

View File

@ -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 <kovid at kovidgoyal.net>'
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

View File

@ -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)