From f6635f6f1f89cdfd9b586db0410c22e5f8206d37 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 7 Nov 2013 21:08:00 +0530 Subject: [PATCH] Basic editor actions --- src/calibre/gui2/tweak_book/boss.py | 23 +++++++++ src/calibre/gui2/tweak_book/editor/widget.py | 51 ++++++++++++++------ src/calibre/gui2/tweak_book/ui.py | 7 +++ 3 files changed, 66 insertions(+), 15 deletions(-) diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 9b7c983b9a..f7a87ba800 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -238,6 +238,7 @@ class Boss(QObject): editor = editors[name] = editor_from_syntax(syntax, self.gui.editor_tabs) editor.undo_redo_state_changed.connect(self.editor_undo_redo_state_changed) editor.data_changed.connect(self.editor_data_changed) + editor.copy_available_state_changed.connect(self.editor_copy_available_state_changed) c = current_container() with c.open(name) as f: editor.data = c.decode(f.read()) @@ -256,6 +257,7 @@ class Boss(QObject): _('Editing files of type %s is not supported' % mime), show=True) self.edit_file(name, syntax) + # Editor basic controls {{{ def do_editor_undo(self): ed = self.gui.central.current_editor if ed is not None: @@ -266,16 +268,35 @@ class Boss(QObject): if ed is not None: ed.redo() + def do_editor_copy(self): + ed = self.gui.central.current_editor + if ed is not None: + ed.copy() + + def do_editor_cut(self): + ed = self.gui.central.current_editor + if ed is not None: + ed.cut() + + def do_editor_paste(self): + ed = self.gui.central.current_editor + if ed is not None: + ed.paste() + def editor_data_changed(self, editor): self.gui.preview.refresh_timer.start(tprefs['preview_refresh_time'] * 1000) def editor_undo_redo_state_changed(self, *args): self.apply_current_editor_state(update_keymap=False) + def editor_copy_available_state_changed(self, *args): + self.apply_current_editor_state(update_keymap=False) + def editor_modification_state_changed(self, is_modified): self.apply_current_editor_state(update_keymap=False) if is_modified: actions['save-book'].setEnabled(True) + # }}} def apply_current_editor_state(self, update_keymap=True): ed = self.gui.central.current_editor @@ -283,6 +304,8 @@ class Boss(QObject): actions['editor-undo'].setEnabled(ed.undo_available) actions['editor-redo'].setEnabled(ed.redo_available) actions['editor-save'].setEnabled(ed.is_modified) + actions['editor-cut'].setEnabled(ed.copy_available) + actions['editor-copy'].setEnabled(ed.cut_available) self.gui.keyboard.set_mode(ed.syntax) name = None for n, x in editors.iteritems(): diff --git a/src/calibre/gui2/tweak_book/editor/widget.py b/src/calibre/gui2/tweak_book/editor/widget.py index 13393d72d8..f560aeb41e 100644 --- a/src/calibre/gui2/tweak_book/editor/widget.py +++ b/src/calibre/gui2/tweak_book/editor/widget.py @@ -16,6 +16,7 @@ class Editor(QMainWindow): modification_state_changed = pyqtSignal(object) undo_redo_state_changed = pyqtSignal(object, object) + copy_available_state_changed = pyqtSignal(object) data_changed = pyqtSignal(object) def __init__(self, syntax, parent=None): @@ -29,20 +30,11 @@ class Editor(QMainWindow): self.create_toolbars() self.undo_available = False self.redo_available = False + self.copy_available = self.cut_available = False self.editor.undoAvailable.connect(self._undo_available) self.editor.redoAvailable.connect(self._redo_available) self.editor.textChanged.connect(self._data_changed) - - def _data_changed(self): - self.data_changed.emit(self) - - def _undo_available(self, available): - self.undo_available = available - self.undo_redo_state_changed.emit(self.undo_available, self.redo_available) - - def _redo_available(self, available): - self.redo_available = available - self.undo_redo_state_changed.emit(self.undo_available, self.redo_available) + self.editor.copyAvailable.connect(self._copy_available) @dynamic_property def data(self): @@ -73,22 +65,51 @@ class Editor(QMainWindow): return property(fget=fget, fset=fset) def create_toolbars(self): - self.action_bar = b = self.addToolBar(_('Edit actions tool bar')) + self.action_bar = b = self.addToolBar(_('File actions tool bar')) b.setObjectName('action_bar') # Needed for saveState - b.addAction(actions['editor-save']) - b.addAction(actions['editor-undo']) - b.addAction(actions['editor-redo']) + for x in ('save', 'undo', 'redo'): + b.addAction(actions['editor-%s' % x]) + self.edit_bar = b = self.addToolBar(_('Edit actions tool bar')) + for x in ('cut', 'copy', 'paste'): + b.addAction(actions['editor-%s' % x]) def break_cycles(self): self.modification_state_changed.disconnect() self.undo_redo_state_changed.disconnect() + self.copy_available_state_changed.disconnect() self.data_changed.disconnect() self.editor.undoAvailable.disconnect() self.editor.redoAvailable.disconnect() self.editor.modificationChanged.disconnect() self.editor.textChanged.disconnect() + self.editor.copyAvailable.disconnect() self.editor.setPlainText('') + def _data_changed(self): + self.data_changed.emit(self) + + def _undo_available(self, available): + self.undo_available = available + self.undo_redo_state_changed.emit(self.undo_available, self.redo_available) + + def _redo_available(self, available): + self.redo_available = available + self.undo_redo_state_changed.emit(self.undo_available, self.redo_available) + + def _copy_available(self, available): + self.copy_available = self.cut_available = available + self.copy_available_state_changed.emit(available) + + def cut(self): + self.editor.cut() + + def copy(self): + self.editor.copy() + + def paste(self): + self.editor.paste() + + def launch_editor(path_to_edit, path_is_raw=False, syntax='html'): if path_is_raw: raw = path_to_edit diff --git a/src/calibre/gui2/tweak_book/ui.py b/src/calibre/gui2/tweak_book/ui.py index ee9da91970..9ec9608cdd 100644 --- a/src/calibre/gui2/tweak_book/ui.py +++ b/src/calibre/gui2/tweak_book/ui.py @@ -20,6 +20,7 @@ from calibre.gui2.tweak_book.keyboard import KeyboardManager from calibre.gui2.tweak_book.preview import Preview class Central(QStackedWidget): + ' The central widget, hosts the editors ' current_editor_changed = pyqtSignal() @@ -162,6 +163,12 @@ class Main(MainWindow): _('Redo typing')) self.action_editor_save = reg('save.png', _('&Save'), self.boss.do_editor_save, 'editor-save', 'Ctrl+S', _('Save changes to the current file')) + self.action_editor_cut = reg('edit-cut.png', _('C&ut text'), self.boss.do_editor_cut, 'editor-cut', 'Ctrl+X', + _('Cut text')) + self.action_editor_copy = reg('edit-copy.png', _('&Copy text'), self.boss.do_editor_copy, 'editor-copy', 'Ctrl+C', + _('Copy text')) + self.action_editor_paste = reg('edit-paste.png', _('&Paste text'), self.boss.do_editor_paste, 'editor-paste', 'Ctrl+V', + _('Paste text')) def create_menubar(self): b = self.menuBar()