diff --git a/src/calibre/gui2/layout.py b/src/calibre/gui2/layout.py index aaaf1b0267..2edf19d0c4 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,12 @@ class SearchBar(QWidget): # {{{
l.addWidget(x)
x.setToolTip(_("Reset Quick Search"))
+ x = parent.search_highlight_only = QCheckBox()
+ x.setText(_('&Highlight'))
+ x.setToolTip(_('Highlight matched books in the book list, instead '
+ 'of restricting the book list to the matches.'))
+ 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/library/views.py b/src/calibre/gui2/library/views.py
index e1e9cf4456..357b48d1de 100644
--- a/src/calibre/gui2/library/views.py
+++ b/src/calibre/gui2/library/views.py
@@ -680,8 +680,12 @@ class BooksView(QTableView): # {{{
def set_editable(self, editable, supports_backloading):
self._model.set_editable(editable)
+ def search_proxy(self, txt):
+ self._model.search(txt)
+ self.setFocus(Qt.OtherFocusReason)
+
def connect_to_search_box(self, sb, search_done):
- sb.search.connect(self._model.search)
+ sb.search.connect(self.search_proxy)
self._search_done = search_done
self._model.searched.connect(self.search_done)
diff --git a/src/calibre/gui2/search_box.py b/src/calibre/gui2/search_box.py
index 9f74abfc86..5808a2dc46 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,10 @@ 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)
+ self.focus_to_library()
+
# }}}
class SavedSearchBoxMixin(object): # {{{