From dde241c5fed7d36d84e0fd73c8a3aa4f12fe6b6b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 30 May 2012 22:02:50 +0530 Subject: [PATCH] Make it a little harder to accidentally change the sorting of items in the Tag Browser. Also frees up more vertical space for the Tag Browser itself. --- src/calibre/gui2/tag_browser/ui.py | 81 ++++++++++++++++------------ src/calibre/gui2/tag_browser/view.py | 36 +++++++------ 2 files changed, 67 insertions(+), 50 deletions(-) diff --git a/src/calibre/gui2/tag_browser/ui.py b/src/calibre/gui2/tag_browser/ui.py index feb2e0536d..5a760639fb 100644 --- a/src/calibre/gui2/tag_browser/ui.py +++ b/src/calibre/gui2/tag_browser/ui.py @@ -10,8 +10,8 @@ __docformat__ = 'restructuredtext en' from functools import partial from PyQt4.Qt import (Qt, QIcon, QWidget, QHBoxLayout, QVBoxLayout, QShortcut, - QKeySequence, QToolButton, QString, QLabel, QFrame, QTimer, QComboBox, - QMenu, QPushButton) + QKeySequence, QToolButton, QString, QLabel, QFrame, QTimer, + QMenu, QPushButton, QActionGroup) from calibre.gui2 import error_dialog, question_dialog from calibre.gui2.widgets import HistoryLineEdit @@ -27,7 +27,7 @@ class TagBrowserMixin(object): # {{{ def __init__(self, db): self.library_view.model().count_changed_signal.connect(self.tags_view.recount) - self.tags_view.set_database(db, self.tag_match, self.sort_by) + self.tags_view.set_database(db, self.alter_tb) self.tags_view.tags_marked.connect(self.search.set_search_string) self.tags_view.tags_list_edit.connect(self.do_tags_list_edit) self.tags_view.edit_user_category.connect(self.do_edit_user_categories) @@ -59,9 +59,9 @@ class TagBrowserMixin(object): # {{{ (_('Manage Saved Searches'), self.do_saved_search_edit, (None,), 'search') ): - self.manage_items_button.menu().addAction( - QIcon(I(category_icon_map[cat_name])), - text, partial(func, *args)) + m = self.alter_tb.manage_menu + m.addAction( QIcon(I(category_icon_map[cat_name])), text, + partial(func, *args)) def do_restriction_error(self): error_dialog(self.tags_view, _('Invalid search restriction'), @@ -387,38 +387,51 @@ class TagBrowserWidget(QWidget): # {{{ self.not_found_label_timer.timeout.connect(self.not_found_label_timer_event, type=Qt.QueuedConnection) - parent.sort_by = QComboBox(parent) - # Must be in the same order as db2.CATEGORY_SORTS - for x in (_('Sort by name'), _('Sort by popularity'), - _('Sort by average rating')): - parent.sort_by.addItem(x) - parent.sort_by.setToolTip( - _('Set the sort order for entries in the Tag Browser')) - parent.sort_by.setStatusTip(parent.sort_by.toolTip()) - parent.sort_by.setCurrentIndex(0) - self._layout.addWidget(parent.sort_by) - - # Must be in the same order as db2.MATCH_TYPE - parent.tag_match = QComboBox(parent) - for x in (_('Match any'), _('Match all')): - parent.tag_match.addItem(x) - parent.tag_match.setCurrentIndex(0) - self._layout.addWidget(parent.tag_match) - parent.tag_match.setToolTip( - _('When selecting multiple entries in the Tag Browser ' - 'match any or all of them')) - parent.tag_match.setStatusTip(parent.tag_match.toolTip()) - - - l = parent.manage_items_button = QPushButton(self) - l.setStyleSheet('QPushButton {text-align: left; }') - l.setText(_('Manage authors, tags, etc')) - l.setToolTip(_('All of these category_managers are available by right-clicking ' - 'on items in the tag browser above')) + parent.alter_tb = l = QPushButton(parent) + l.setText(_('&Alter Tag Browser')) + l.setIcon(QIcon(I('tags.png'))) l.m = QMenu() l.setMenu(l.m) self._layout.addWidget(l) + sb = l.m.addAction(_('Sort by')) + sb.m = l.sort_menu = QMenu(l.m) + sb.setMenu(sb.m) + sb.bg = QActionGroup(sb) + + # Must be in the same order as db2.CATEGORY_SORTS + for i, x in enumerate((_('Sort by name'), _('Sort by popularity'), + _('Sort by average rating'))): + a = sb.m.addAction(x) + sb.bg.addAction(a) + a.setCheckable(True) + if i == 0: a.setChecked(True) + sb.setToolTip( + _('Set the sort order for entries in the Tag Browser')) + sb.setStatusTip(sb.toolTip()) + + ma = l.m.addAction(_('Match type')) + ma.m = l.match_menu = QMenu(l.m) + ma.setMenu(ma.m) + ma.ag = QActionGroup(ma) + + # Must be in the same order as db2.MATCH_TYPE + for i, x in enumerate((_('Match any'), _('Match all'))): + a = ma.m.addAction(x) + ma.ag.addAction(a) + a.setCheckable(True) + if i == 0: a.setChecked(True) + ma.setToolTip( + _('When selecting multiple entries in the Tag Browser ' + 'match any or all of them')) + ma.setStatusTip(ma.toolTip()) + + mt = l.m.addAction(_('Manage authors, tags, etc')) + mt.setToolTip(_('All of these category_managers are available by right-clicking ' + 'on items in the tag browser above')) + mt.m = l.manage_menu = QMenu(l.m) + mt.setMenu(mt.m) + # self.leak_test_timer = QTimer(self) # self.leak_test_timer.timeout.connect(self.test_for_leak) # self.leak_test_timer.start(5000) diff --git a/src/calibre/gui2/tag_browser/view.py b/src/calibre/gui2/tag_browser/view.py index 8ee9d44f8c..8edddf039f 100644 --- a/src/calibre/gui2/tag_browser/view.py +++ b/src/calibre/gui2/tag_browser/view.py @@ -75,7 +75,7 @@ class TagsView(QTreeView): # {{{ def __init__(self, parent=None): QTreeView.__init__(self, parent=None) - self.tag_match = None + self.alter_tb = None self.disable_recounting = False self.setUniformRowHeights(True) self.setCursor(Qt.PointingHandCursor) @@ -139,27 +139,25 @@ class TagsView(QTreeView): # {{{ def reread_collapse_parameters(self): self._model.reread_collapse_model(self.get_state()[1]) - def set_database(self, db, tag_match, sort_by): + def set_database(self, db, alter_tb): self._model.set_database(db) - + self.alter_tb = alter_tb self.pane_is_visible = True # because TagsModel.set_database did a recount - self.sort_by = sort_by - self.tag_match = tag_match self.setModel(self._model) self.setContextMenuPolicy(Qt.CustomContextMenu) - pop = config['sort_tags_by'] - self.sort_by.setCurrentIndex(self.db.CATEGORY_SORTS.index(pop)) + pop = self.db.CATEGORY_SORTS.index(config['sort_tags_by']) + self.alter_tb.sort_menu.actions()[pop].setChecked(True) try: match_pop = self.db.MATCH_TYPE.index(config['match_tags_type']) except ValueError: match_pop = 0 - self.tag_match.setCurrentIndex(match_pop) + self.alter_tb.match_menu.actions()[match_pop].setChecked(True) if not self.made_connections: self.clicked.connect(self.toggle) self.customContextMenuRequested.connect(self.show_context_menu) self.refresh_required.connect(self.recount, type=Qt.QueuedConnection) - self.sort_by.currentIndexChanged.connect(self.sort_changed) - self.tag_match.currentIndexChanged.connect(self.match_changed) + self.alter_tb.sort_menu.triggered.connect(self.sort_changed) + self.alter_tb.match_menu.triggered.connect(self.match_changed) self.made_connections = True self.refresh_signal_processed = True db.add_listener(self.database_changed) @@ -179,15 +177,21 @@ class TagsView(QTreeView): # {{{ @property def match_all(self): - return self.tag_match and self.tag_match.currentIndex() > 0 + return (self.alter_tb and + self.alter_tb.match_menu.actions()[1].isChecked()) - def sort_changed(self, pop): - config.set('sort_tags_by', self.db.CATEGORY_SORTS[pop]) - self.recount() + def sort_changed(self, action): + for i, ac in enumerate(self.alter_tb.sort_menu.actions()): + if ac is action: + config.set('sort_tags_by', self.db.CATEGORY_SORTS[i]) + self.recount() + break - def match_changed(self, pop): + def match_changed(self, action): try: - config.set('match_tags_type', self.db.MATCH_TYPE[pop]) + for i, ac in enumerate(self.alter_tb.match_menu.actions()): + if ac is action: + config.set('match_tags_type', self.db.MATCH_TYPE[i]) except: pass