Skip to content

Commit

Permalink
Edit Book: When re-arranging the toolbars inside an individual file e…
Browse files Browse the repository at this point in the history
…ditor, save the new toolbar layout so that it is re-used in the future. Also apply the changed layout to all open editors of the same type. See #1346913 (Edit Books - Toolbars)
  • Loading branch information
kovidgoyal committed Jul 22, 2014
1 parent d05e882 commit 7573a62
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/calibre/gui2/tweak_book/editor/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
QDialog, QSpinBox, QCheckBox, QDialogButtonBox, QToolButton, QMenu, QInputDialog)

from calibre.gui2 import error_dialog
from calibre.gui2.tweak_book import actions
from calibre.gui2.tweak_book import actions, tprefs, editors
from calibre.gui2.tweak_book.editor.canvas import Canvas

class ResizeDialog(QDialog): # {{{
Expand Down Expand Up @@ -164,6 +164,17 @@ def apply_settings(self, prefs=None, dictionaries_changed=False):
def go_to_line(self, *args, **kwargs):
pass

def save_state(self):
for bar in self.bars:
if bar.isFloating():
return
tprefs['image-editor-state'] = bytearray(self.saveState())

def restore_state(self):
state = tprefs.get('image-editor-state', None)
if state is not None:
self.restoreState(state)

def set_focus(self):
self.canvas.setFocus(Qt.OtherFocusReason)

Expand Down Expand Up @@ -234,6 +245,7 @@ def create_toolbars(self):
for x in ('undo', 'redo'):
b.addAction(getattr(self.canvas, '%s_action' % x))
self.edit_bar = b = self.addToolBar(_('Edit actions tool bar'))
b.setObjectName('edit-actions-bar')
for x in ('copy', 'paste'):
ac = actions['editor-%s' % x]
setattr(self, 'action_' + x, b.addAction(ac.icon(), x, getattr(self, x)))
Expand All @@ -254,11 +266,24 @@ def create_toolbars(self):
m.addAction(_('De-speckle image'), self.canvas.despeckle_image)

self.info_bar = b = self.addToolBar(_('Image information bar'))
b.setObjectName('image_info_bar')
self.fmt_label = QLabel('')
b.addWidget(self.fmt_label)
b.addSeparator()
self.size_label = QLabel('')
b.addWidget(self.size_label)
self.bars = [self.action_bar, self.edit_bar, self.info_bar]
for x in self.bars:
x.setFloatable(False)
x.topLevelChanged.connect(self.toolbar_floated)
self.restore_state()

def toolbar_floated(self, floating):
if not floating:
self.save_state()
for ed in editors.itervalues():
if ed is not self:
ed.restore_state()

def update_clipboard_actions(self, *args):
if self.canvas.has_selection:
Expand Down
26 changes: 25 additions & 1 deletion src/calibre/gui2/tweak_book/editor/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from calibre.constants import DEBUG
from calibre.ebooks.chardet import replace_encoding_declarations
from calibre.gui2 import error_dialog, open_url
from calibre.gui2.tweak_book import actions, current_container, tprefs, dictionaries, editor_toolbar_actions, editor_name
from calibre.gui2.tweak_book import actions, current_container, tprefs, dictionaries, editor_toolbar_actions, editor_name, editors
from calibre.gui2.tweak_book.editor import SPELL_PROPERTY, LINK_PROPERTY, TAG_NAME_PROPERTY, CSS_PROPERTY
from calibre.gui2.tweak_book.editor.help import help_url
from calibre.gui2.tweak_book.editor.text import TextEdit
Expand Down Expand Up @@ -259,11 +259,34 @@ def create_toolbars(self):
b.setObjectName('action_bar') # Needed for saveState
self.tools_bar = b = self.addToolBar(_('Editor tools'))
b.setObjectName('tools_bar')
self.bars = [self.action_bar, self.tools_bar]
if self.syntax == 'html':
self.format_bar = b = self.addToolBar(_('Format text'))
b.setObjectName('html_format_bar')
self.bars.append(self.format_bar)
self.insert_tag_menu = QMenu(self)
self.populate_toolbars()
for x in self.bars:
x.setFloatable(False)
x.topLevelChanged.connect(self.toolbar_floated)

def toolbar_floated(self, floating):
if not floating:
self.save_state()
for ed in editors.itervalues():
if ed is not self:
ed.restore_state()

def save_state(self):
for bar in self.bars:
if bar.isFloating():
return
tprefs['%s-editor-state' % self.syntax] = bytearray(self.saveState())

def restore_state(self):
state = tprefs.get('%s-editor-state' % self.syntax, None)
if state is not None:
self.restoreState(state)

def populate_toolbars(self):
self.action_bar.clear(), self.tools_bar.clear()
Expand Down Expand Up @@ -302,6 +325,7 @@ def add_action(name, bar):
self.format_bar.clear()
for name in tprefs['editor_format_toolbar']:
add_action(name, self.format_bar)
self.restore_state()

def break_cycles(self):
for x in ('modification_state_changed', 'word_ignored', 'link_clicked'):
Expand Down
6 changes: 5 additions & 1 deletion src/calibre/gui2/tweak_book/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from calibre.gui2.keyboard import Manager as KeyboardManager
from calibre.gui2.main_window import MainWindow
from calibre.gui2.throbber import ThrobbingButton, create_donate_widget
from calibre.gui2.tweak_book import current_container, tprefs, actions, capitalize, toolbar_actions
from calibre.gui2.tweak_book import current_container, tprefs, actions, capitalize, toolbar_actions, editors
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
Expand Down Expand Up @@ -146,6 +146,10 @@ def current_editor(self):
def save_state(self):
tprefs.set('search-panel-visible', self.search_panel.isVisible())
self.search_panel.save_state()
for ed in editors.itervalues():
ed.save_state()
if self.current_editor is not None:
self.current_editor.save_state() # Ensure the current editor saves it state last

def restore_state(self):
self.search_panel.setVisible(tprefs.get('search-panel-visible', False))
Expand Down

0 comments on commit 7573a62

Please sign in to comment.