From 34424068389e66c82156dca78b54175c114859cb Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Wed, 12 Jan 2011 15:32:00 +0000 Subject: [PATCH] Add ability to highlight when searching instead of restrict --- src/calibre/gui2/layout.py | 14 ++++++++++---- src/calibre/gui2/library/models.py | 22 ++++++++++++++++++++-- src/calibre/gui2/search_box.py | 4 ++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/calibre/gui2/layout.py b/src/calibre/gui2/layout.py index aaaf1b0267..4009f99201 100644 --- a/src/calibre/gui2/layout.py +++ b/src/calibre/gui2/layout.py @@ -8,9 +8,9 @@ __docformat__ = 'restructuredtext en' from functools import partial from PyQt4.Qt import QIcon, Qt, QWidget, QToolBar, QSize, \ - pyqtSignal, QToolButton, QPushButton, \ - QObject, QVBoxLayout, QSizePolicy, QLabel, QHBoxLayout, QActionGroup, \ - QMenu + pyqtSignal, QToolButton, QMenu, QCheckBox, \ + QObject, QVBoxLayout, QSizePolicy, QLabel, QHBoxLayout, QActionGroup + from calibre.constants import __appname__ from calibre.gui2.search_box import SearchBox2, SavedSearchBox @@ -178,7 +178,9 @@ class SearchBar(QWidget): # {{{ x.setToolTip(_("

Search the list of books by title, author, publisher, tags, comments, etc.

Words separated by spaces are ANDed")) l.addWidget(x) - self.search_button = QPushButton(_('&Go!')) + self.search_button = QToolButton() + self.search_button.setToolButtonStyle(Qt.ToolButtonTextOnly) + self.search_button.setText(_('&Go!')) l.addWidget(self.search_button) self.search_button.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) @@ -192,6 +194,10 @@ class SearchBar(QWidget): # {{{ l.addWidget(x) x.setToolTip(_("Reset Quick Search")) + x = parent.search_highlight_only = QCheckBox() + x.setText(_('Highlight')) + l.addWidget(x) + x = parent.saved_search = SavedSearchBox(self) x.setMaximumSize(QSize(150, 16777215)) x.setMinimumContentsLength(15) diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 49cb1ce182..98e61acf33 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -10,7 +10,7 @@ from contextlib import closing from operator import attrgetter from PyQt4.Qt import QAbstractTableModel, Qt, pyqtSignal, QIcon, QImage, \ - QModelIndex, QVariant, QDate + QModelIndex, QVariant, QDate, QColor from calibre.gui2 import NONE, config, UNDEFINED_QDATE from calibre.utils.pyparsing import ParseException @@ -93,6 +93,8 @@ class BooksModel(QAbstractTableModel): # {{{ self.bool_no_icon = QIcon(I('list_remove.png')) self.bool_blank_icon = QIcon(I('blank.png')) self.device_connected = False + self.rows_matching = set() + self.highlight_only = False self.read_config() def change_alignment(self, colname, alignment): @@ -229,9 +231,22 @@ class BooksModel(QAbstractTableModel): # {{{ self.endInsertRows() self.count_changed() + def set_highlight_only(self, toWhat): + self.highlight_only = toWhat + self.research() + def search(self, text, reset=True): try: - self.db.search(text) + if self.highlight_only: + self.db.search('') + if not text: + self.rows_matching = set() + else: + self.rows_matching = set(self.db.search(text, + return_matches=True)) + else: + self.rows_matching = set() + self.db.search(text) except ParseException as e: self.searched.emit(e.msg) return @@ -651,6 +666,9 @@ class BooksModel(QAbstractTableModel): # {{{ return NONE if role in (Qt.DisplayRole, Qt.EditRole): return self.column_to_dc_map[col](index.row()) + elif role == Qt.BackgroundColorRole: + if self.id(index) in self.rows_matching: + return QColor('lightgreen') elif role == Qt.DecorationRole: if self.column_to_dc_decorator_map[col] is not None: return self.column_to_dc_decorator_map[index.column()](index.row()) diff --git a/src/calibre/gui2/search_box.py b/src/calibre/gui2/search_box.py index 9f74abfc86..75d3c14ef1 100644 --- a/src/calibre/gui2/search_box.py +++ b/src/calibre/gui2/search_box.py @@ -375,6 +375,7 @@ class SearchBoxMixin(object): # {{{ unicode(self.search.toolTip()))) self.advanced_search_button.setStatusTip(self.advanced_search_button.toolTip()) self.clear_button.setStatusTip(self.clear_button.toolTip()) + self.search_highlight_only.stateChanged.connect(self.highlight_only_changed) def focus_search_box(self, *args): self.search.setFocus(Qt.OtherFocusReason) @@ -401,6 +402,9 @@ class SearchBoxMixin(object): # {{{ def focus_to_library(self): self.current_view().setFocus(Qt.OtherFocusReason) + def highlight_only_changed(self, toWhat): + self.current_view().model().set_highlight_only(toWhat) + # }}} class SavedSearchBoxMixin(object): # {{{