From b8b6e85abe1a0b373fe339a5e0aef324a31ae5ac Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 23 May 2018 19:09:59 +0530 Subject: [PATCH] Add an option to use two lines for the text under the toolbar button in Preferences->Look & feel --- src/calibre/gui2/__init__.py | 13 ++-- src/calibre/gui2/bars.py | 79 ++++++++++++++++++++--- src/calibre/gui2/preferences/look_feel.py | 14 ++-- src/calibre/gui2/preferences/look_feel.ui | 7 ++ 4 files changed, 90 insertions(+), 23 deletions(-) diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index 1c8c459853..1ee8b93355 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -167,6 +167,7 @@ def create_defs(): defs['qv_follows_column'] = False defs['book_details_narrow_comments_layout'] = 'float' defs['book_list_split'] = False + defs['wrap_toolbar_text'] = False create_defs() @@ -348,9 +349,9 @@ def extension(path): def warning_dialog(parent, title, msg, det_msg='', show=False, show_copy_button=True): from calibre.gui2.dialogs.message_box import MessageBox - d = MessageBox(MessageBox.WARNING, _('WARNING:')+ ' ' + - title, msg, det_msg, parent=parent, - show_copy_button=show_copy_button) + d = MessageBox(MessageBox.WARNING, _('WARNING:' + )+ ' ' + title, msg, det_msg, parent=parent, + show_copy_button=show_copy_button) if show: return d.exec_() return d @@ -359,9 +360,9 @@ def warning_dialog(parent, title, msg, det_msg='', show=False, def error_dialog(parent, title, msg, det_msg='', show=False, show_copy_button=True): from calibre.gui2.dialogs.message_box import MessageBox - d = MessageBox(MessageBox.ERROR, _('ERROR:')+ ' ' + - title, msg, det_msg, parent=parent, - show_copy_button=show_copy_button) + d = MessageBox(MessageBox.ERROR, _('ERROR:' + ) + ' ' + title, msg, det_msg, parent=parent, + show_copy_button=show_copy_button) if show: return d.exec_() return d diff --git a/src/calibre/gui2/bars.py b/src/calibre/gui2/bars.py index ba50235ec5..1587f679ba 100644 --- a/src/calibre/gui2/bars.py +++ b/src/calibre/gui2/bars.py @@ -7,6 +7,7 @@ __license__ = 'GPL v3' __copyright__ = '2011, Kovid Goyal ' __docformat__ = 'restructuredtext en' +from functools import partial import sip from PyQt5.Qt import ( Qt, QAction, QMenu, QObject, QToolBar, QToolButton, QSize, pyqtSignal, @@ -61,6 +62,60 @@ class RevealBar(QWidget): # {{{ # }}} +MAX_TEXT_LENGTH = 10 +connected_pairs = set() + + +def wrap_button_text(text, max_len=MAX_TEXT_LENGTH): + parts = text.split() + ans = '' + broken = False + for word in parts: + if broken: + ans += ' ' + word + else: + if len(ans) + len(word) < max_len: + if ans: + ans += ' ' + word + else: + ans = word + else: + if ans: + ans += '\n' + word + broken = True + else: + ans = word + if not broken: + if ' ' in ans: + ans = '\n'.join(ans.split(' ', 1)) + elif '/' in ans: + ans = '/\n'.join(ans.split('/', 1)) + else: + ans += '\n\xa0' + return ans + + +def rewrap_button(w): + if not sip.isdeleted(w): + w.setText(wrap_button_text(w.defaultAction().text())) + + +def wrap_all_button_texts(all_buttons): + if not all_buttons: + return + for w in all_buttons: + if hasattr(w, 'defaultAction'): + ac = w.defaultAction() + text = ac.text() + key = id(w), id(ac) + if key not in connected_pairs: + ac.changed.connect(partial(rewrap_button, w)) + connected_pairs.add(key) + else: + text = w.text() + w.setText(wrap_button_text(text)) + + def create_donate_button(action): ans = ThrobbingButton() ans.setAutoRaise(True) @@ -134,30 +189,32 @@ class ToolBar(QToolBar): # {{{ self.clear() self.added_actions = [] self.donate_button = None - - bar = self + self.all_widgets = [] for what in actions: if what is None: - bar.addSeparator() + self.addSeparator() elif what == 'Location Manager': for ac in self.location_manager.all_actions: - bar.addAction(ac) - bar.added_actions.append(ac) - bar.setup_tool_button(bar, ac, QToolButton.MenuButtonPopup) + self.addAction(ac) + self.added_actions.append(ac) + self.setup_tool_button(self, ac, QToolButton.MenuButtonPopup) ac.setVisible(False) elif what == 'Donate': self.donate_button = create_donate_button(self.donate_action) - bar.addWidget(self.donate_button) - self.donate_button.setIconSize(bar.iconSize()) + self.addWidget(self.donate_button) + self.donate_button.setIconSize(self.iconSize()) self.donate_button.setToolButtonStyle(self.toolButtonStyle()) self.showing_donate = True elif what in self.gui.iactions: action = self.gui.iactions[what] - bar.addAction(action.qaction) + self.addAction(action.qaction) self.added_actions.append(action.qaction) - self.setup_tool_button(bar, action.qaction, action.popup_type) + self.setup_tool_button(self, action.qaction, action.popup_type) self.preferred_width = self.sizeHint().width() + if gprefs['wrap_toolbar_text']: + wrap_all_button_texts(self.all_widgets) + self.all_widgets = [] def setup_tool_button(self, bar, ac, menu_mode=None): ch = bar.widgetForAction(ac) @@ -165,6 +222,8 @@ class ToolBar(QToolBar): # {{{ ch = self.child_bar.widgetForAction(ac) ch.setCursor(Qt.PointingHandCursor) ch.setAutoRaise(True) + if hasattr(ch, 'setText') and hasattr(ch, 'text'): + self.all_widgets.append(ch) m = ac.menu() if m is not None: if menu_mode is not None: diff --git a/src/calibre/gui2/preferences/look_feel.py b/src/calibre/gui2/preferences/look_feel.py index 7ff078c35a..7cfe96d1b0 100644 --- a/src/calibre/gui2/preferences/look_feel.py +++ b/src/calibre/gui2/preferences/look_feel.py @@ -394,6 +394,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): self.opt_hidpi.setVisible(False), self.label_hidpi.setVisible(False) r('ui_style', gprefs, restart_required=True, choices=[(_('System default'), 'system'), (_('calibre style'), 'calibre')]) r('book_list_tooltips', gprefs) + r('wrap_toolbar_text', gprefs, restart_required=True) r('show_layout_buttons', gprefs, restart_required=True) r('row_numbers_in_book_list', gprefs) r('tag_browser_old_look', gprefs) @@ -478,11 +479,11 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): r('tag_browser_dont_collapse', gprefs, setting=CommaSeparatedList) choices = set([k for k in db.field_metadata.all_field_keys() - if (db.field_metadata[k]['is_category'] and - (db.field_metadata[k]['datatype'] in ['text', 'series', 'enumeration']) and - not db.field_metadata[k]['display'].get('is_names', False)) or - (db.field_metadata[k]['datatype'] in ['composite'] and - db.field_metadata[k]['display'].get('make_category', False))]) + if (db.field_metadata[k]['is_category'] and ( + db.field_metadata[k]['datatype'] in ['text', 'series', 'enumeration' + ]) and not db.field_metadata[k]['display'].get('is_names', False)) or ( + db.field_metadata[k]['datatype'] in ['composite' + ] and db.field_metadata[k]['display'].get('make_category', False))]) choices -= set(['authors', 'publisher', 'formats', 'news', 'identifiers']) choices |= set(['search']) self.opt_categories_using_hierarchy.update_items_cache(choices) @@ -722,8 +723,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): name = unicode(fi.family()) self.font_display.setFont(font) - self.font_display.setText(name + - ' [%dpt]'%fi.pointSize()) + self.font_display.setText(name + ' [%dpt]'%fi.pointSize()) def change_font(self, *args): fd = QFontDialog(self.build_font_obj(), self) diff --git a/src/calibre/gui2/preferences/look_feel.ui b/src/calibre/gui2/preferences/look_feel.ui index 4c4a004f35..213a332f6d 100644 --- a/src/calibre/gui2/preferences/look_feel.ui +++ b/src/calibre/gui2/preferences/look_feel.ui @@ -90,6 +90,13 @@ + + + + Use t&wo lines for the text under the icons (needs restart) + + +