diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 32c512fe39..bd8e07fb58 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -791,6 +791,17 @@ class Toolbar(PreferencesPlugin): description = _('Customize the toolbars and context menus, changing which' ' actions are available in each') +class Search(PreferencesPlugin): + name = 'Search' + icon = I('search.png') + gui_name = _('Customize searching') + category = 'Interface' + gui_category = _('Interface') + category_order = 1 + name_order = 5 + config_widget = 'calibre.gui2.preferences.search' + description = _('Customize the way searching for books works in calibre') + class InputOptions(PreferencesPlugin): name = 'Input Options' icon = I('arrow-down.png') @@ -941,7 +952,7 @@ class Misc(PreferencesPlugin): config_widget = 'calibre.gui2.preferences.misc' description = _('Miscellaneous advanced configuration') -plugins += [LookAndFeel, Behavior, Columns, Toolbar, InputOptions, +plugins += [LookAndFeel, Behavior, Columns, Toolbar, Search, InputOptions, CommonOptions, OutputOptions, Adding, Saving, Sending, Plugboard, Email, Server, Plugins, Tweaks, Misc, TemplateFunctions] diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index 92a68fa840..b33166dd33 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -106,9 +106,13 @@ def _config(): 'clicked')) c.add_opt('asked_library_thing_password', default=False, help='Asked library thing password at least once.') - c.add_opt('search_as_you_type', default=True, - help='Start searching as you type. If this is disabled then search will ' - 'only take place when the Enter or Return key is pressed.') + c.add_opt('search_as_you_type', default=False, + help=_('Start searching as you type. If this is disabled then search will ' + 'only take place when the Enter or Return key is pressed.')) + c.add_opt('highlight_search_matches', default=False, + help=_('When searching, show all books with search results ' + 'highlighted instead of showing only the matches. You can use the ' + 'N or F3 keys to go to the next match.')) c.add_opt('save_to_disk_template_history', default=[], help='Previously used Save to Disk templates') c.add_opt('send_to_device_template_history', default=[], diff --git a/src/calibre/gui2/actions/next_match.py b/src/calibre/gui2/actions/next_match.py index 79de6a2d9b..1c74719674 100644 --- a/src/calibre/gui2/actions/next_match.py +++ b/src/calibre/gui2/actions/next_match.py @@ -28,21 +28,12 @@ class NextMatchAction(InterfaceAction): self.gui.addAction(self.p_action) self.p_action.triggered.connect(self.move_backward) - def gui_layout_complete(self): - self.gui.search_highlight_only.setVisible(True) - def location_selected(self, loc): self.can_move = loc == 'library' - try: - self.gui.search_highlight_only.setVisible(self.can_move) - except: - import traceback - traceback.print_exc() def move_forward(self): if self.can_move is None: self.can_move = self.gui.current_view() is self.gui.library_view - self.gui.search_highlight_only.setVisible(self.can_move) if self.can_move: self.gui.current_view().move_highlighted_row(forward=True) @@ -50,7 +41,6 @@ class NextMatchAction(InterfaceAction): def move_backward(self): if self.can_move is None: self.can_move = self.gui.current_view() is self.gui.library_view - self.gui.search_highlight_only.setVisible(self.can_move) if self.can_move: self.gui.current_view().move_highlighted_row(forward=False) diff --git a/src/calibre/gui2/actions/preferences.py b/src/calibre/gui2/actions/preferences.py index f1fe06c43d..ee52f06aac 100644 --- a/src/calibre/gui2/actions/preferences.py +++ b/src/calibre/gui2/actions/preferences.py @@ -33,7 +33,8 @@ class PreferencesAction(InterfaceAction): x.triggered.connect(self.do_config) - def do_config(self, checked=False, initial_plugin=None): + def do_config(self, checked=False, initial_plugin=None, + close_after_initial=False): if self.gui.job_manager.has_jobs(): d = error_dialog(self.gui, _('Cannot configure'), _('Cannot configure while there are running jobs.')) @@ -44,7 +45,8 @@ class PreferencesAction(InterfaceAction): _('Cannot configure before calibre is restarted.')) d.exec_() return - d = Preferences(self.gui, initial_plugin=initial_plugin) + d = Preferences(self.gui, initial_plugin=initial_plugin, + close_after_initial=close_after_initial) d.show() d.run_wizard_requested.connect(self.gui.run_wizard, type=Qt.QueuedConnection) diff --git a/src/calibre/gui2/init.py b/src/calibre/gui2/init.py index ebd670c8fa..0ca58582b6 100644 --- a/src/calibre/gui2/init.py +++ b/src/calibre/gui2/init.py @@ -64,6 +64,7 @@ class LibraryViewMixin(object): # {{{ view.verticalHeader().sectionDoubleClicked.connect(self.iactions['View'].view_specific_book) self.build_context_menus() + self.library_view.model().set_highlight_only(config['highlight_search_matches']) def build_context_menus(self): lm = QMenu(self) diff --git a/src/calibre/gui2/layout.py b/src/calibre/gui2/layout.py index c1d9498075..e8a4e79384 100644 --- a/src/calibre/gui2/layout.py +++ b/src/calibre/gui2/layout.py @@ -8,7 +8,7 @@ __docformat__ = 'restructuredtext en' from functools import partial from PyQt4.Qt import QIcon, Qt, QWidget, QToolBar, QSize, \ - pyqtSignal, QToolButton, QMenu, QCheckBox, \ + pyqtSignal, QToolButton, QMenu, \ QObject, QVBoxLayout, QSizePolicy, QLabel, QHBoxLayout, QActionGroup @@ -156,7 +156,8 @@ class SearchBar(QWidget): # {{{ x = ComboBoxWithHelp(self) x.setMaximumSize(QSize(150, 16777215)) x.setObjectName("search_restriction") - x.setToolTip(_("Books display will be restricted to those matching the selected saved search")) + x.setToolTip(_('Books display will be restricted to those matching the ' + 'selected saved search')) l.addWidget(x) parent.search_restriction = x @@ -175,7 +176,8 @@ class SearchBar(QWidget): # {{{ x = parent.search = SearchBox2(self) x.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum) x.setObjectName("search") - x.setToolTip(_("

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

Words separated by spaces are ANDed")) + 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 = QToolButton() @@ -194,13 +196,11 @@ class SearchBar(QWidget): # {{{ l.addWidget(x) x.setToolTip(_("Reset Quick Search")) - x = parent.search_highlight_only = QCheckBox() - x.setText(_('&Highlight')) - x.setToolTip('

'+_('When searching, highlight matched books, instead ' - 'of restricting the book list to the matches.

You can use the ' - 'N or F3 keys to go to the next match.')) + x = parent.search_options_button = QToolButton(self) + x.setIcon(QIcon(I('config.png'))) + x.setObjectName("search_option_button") l.addWidget(x) - x.setVisible(False) + x.setToolTip(_("Change the way searching for books works")) x = parent.saved_search = SavedSearchBox(self) x.setMaximumSize(QSize(150, 16777215)) @@ -227,7 +227,6 @@ class SearchBar(QWidget): # {{{ x.setToolTip(_("Delete current saved search")) - # }}} class Spacer(QWidget): # {{{ diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 2f8a747c39..48668d3376 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -238,8 +238,6 @@ class BooksModel(QAbstractTableModel): # {{{ def set_highlight_only(self, toWhat): self.highlight_only = toWhat - if self.last_search: - self.research() def get_current_highlighted_id(self): if len(self.ids_to_highlight) == 0 or self.current_highlighted_idx is None: diff --git a/src/calibre/gui2/preferences/look_feel.py b/src/calibre/gui2/preferences/look_feel.py index 37ed90cc61..196ef16b08 100644 --- a/src/calibre/gui2/preferences/look_feel.py +++ b/src/calibre/gui2/preferences/look_feel.py @@ -46,7 +46,6 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): r('disable_tray_notification', config) r('use_roman_numerals_for_series_number', config) r('separate_cover_flow', config, restart_required=True) - r('search_as_you_type', config) r('show_child_bar', gprefs) choices = [(_('Small'), 'small'), (_('Medium'), 'medium'), @@ -116,7 +115,6 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): def refresh_gui(self, gui): - gui.search.search_as_you_type(config['search_as_you_type']) self.update_font_display() gui.tags_view.reread_collapse_parameters() diff --git a/src/calibre/gui2/preferences/look_feel.ui b/src/calibre/gui2/preferences/look_feel.ui index 2223167068..3f2bb3e145 100644 --- a/src/calibre/gui2/preferences/look_feel.ui +++ b/src/calibre/gui2/preferences/look_feel.ui @@ -124,23 +124,13 @@ - + Show cover &browser in a separate window (needs restart) - - - - Search as you type - - - true - - - @@ -177,7 +167,7 @@ if you never want subcategories - If a Tag Browser category has more than this number of items, it is divided + If a Tag Browser category has more than this number of items, it is divided up into sub-categories. If the partition method is set to disable, this value is ignored. diff --git a/src/calibre/gui2/preferences/main.py b/src/calibre/gui2/preferences/main.py index f7d49427c8..f25cc85dce 100644 --- a/src/calibre/gui2/preferences/main.py +++ b/src/calibre/gui2/preferences/main.py @@ -157,11 +157,12 @@ class Preferences(QMainWindow): run_wizard_requested = pyqtSignal() - def __init__(self, gui, initial_plugin=None): + def __init__(self, gui, initial_plugin=None, close_after_initial=False): QMainWindow.__init__(self, gui) self.gui = gui self.must_restart = False self.committed = False + self.close_after_initial = close_after_initial self.resize(900, 720) nh, nw = min_available_height()-25, available_width()-10 @@ -306,7 +307,7 @@ class Preferences(QMainWindow): def esc(self, *args): if self.stack.currentIndex() == 1: - self.hide_plugin() + self.cancel() elif self.stack.currentIndex() == 0: self.close() @@ -331,12 +332,15 @@ class Preferences(QMainWindow): show_copy_button=False) self.showing_widget.refresh_gui(self.gui) self.hide_plugin() - if must_restart and rc: + if self.close_after_initial or (must_restart and rc): self.close() def cancel(self, *args): - self.hide_plugin() + if self.close_after_initial: + self.close() + else: + self.hide_plugin() def restore_defaults(self, *args): self.showing_widget.restore_defaults() diff --git a/src/calibre/gui2/preferences/search.py b/src/calibre/gui2/preferences/search.py new file mode 100644 index 0000000000..81bc603df4 --- /dev/null +++ b/src/calibre/gui2/preferences/search.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai + +__license__ = 'GPL v3' +__copyright__ = '2010, Kovid Goyal ' +__docformat__ = 'restructuredtext en' + +from PyQt4.Qt import QApplication + +from calibre.gui2.preferences import ConfigWidgetBase, test_widget, \ + CommaSeparatedList +from calibre.gui2.preferences.search_ui import Ui_Form +from calibre.gui2 import config +from calibre.utils.config import prefs + +class ConfigWidget(ConfigWidgetBase, Ui_Form): + + def genesis(self, gui): + self.gui = gui + + r = self.register + + r('search_as_you_type', config) + r('highlight_search_matches', config) + r('limit_search_columns', prefs) + r('limit_search_columns_to', prefs, setting=CommaSeparatedList) + fl = gui.library_view.model().db.field_metadata.get_search_terms() + self.opt_limit_search_columns_to.update_items_cache(fl) + + def refresh_gui(self, gui): + gui.search.search_as_you_type(config['search_as_you_type']) + gui.library_view.model().set_highlight_only(config['highlight_search_matches']) + gui.search.do_search() + +if __name__ == '__main__': + app = QApplication([]) + test_widget('Interface', 'Search') + diff --git a/src/calibre/gui2/preferences/search.ui b/src/calibre/gui2/preferences/search.ui new file mode 100644 index 0000000000..69426a3728 --- /dev/null +++ b/src/calibre/gui2/preferences/search.ui @@ -0,0 +1,130 @@ + + + Form + + + + 0 + 0 + 670 + 392 + + + + Form + + + + + + Search as you &type + + + + + + + &Highlight search results instead of restricting the book list to the results + + + + + + + What to search by default + + + + + + When you enter a search term without a prefix, by default calibre will search all metadata for matches. For example, entering, "asimov" will search not just authors but title/tags/series/comments/etc. Use these options if you would like to change this behavior. + + + true + + + + + + + &Limit the searched metadata + + + + + + + &Columns that non-prefixed searches are limited to: + + + opt_limit_search_columns_to + + + + + + + + + + Note that this option affects all searches, including saved searches and restrictions. Therefore, if you use this option, it is best to ensure that you always use prefixes in your saved searches. For example, use "series:Foundation" rather than just "Foundation" in a saved search + + + true + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + MultiCompleteLineEdit + QLineEdit +

calibre/gui2.complete.h
+ + + + + diff --git a/src/calibre/gui2/search_box.py b/src/calibre/gui2/search_box.py index e4073a01c9..900c882adc 100644 --- a/src/calibre/gui2/search_box.py +++ b/src/calibre/gui2/search_box.py @@ -16,7 +16,6 @@ from calibre.gui2 import config from calibre.gui2.dialogs.confirm_delete import confirm from calibre.gui2.dialogs.saved_search_editor import SavedSearchEditor from calibre.gui2.dialogs.search import SearchDialog -from calibre.utils.config import dynamic from calibre.utils.search_query_parser import saved_searches from calibre.utils.icu import sort_key @@ -271,7 +270,7 @@ class SavedSearchBox(QComboBox): # {{{ def initialize(self, _search_box, colorize=False, help_text=_('Search')): self.search_box = _search_box try: - self.line_edit.setPlaceholderText(help_text) + self.line_edit.setPlaceholderText(help_text) except: # Using Qt < 4.7 pass @@ -376,9 +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) - self.search_highlight_only.setChecked( - dynamic.get('search_highlight_only', False)) + self.search_options_button.clicked.connect(self.search_options_button_clicked) def focus_search_box(self, *args): self.search.setFocus(Qt.OtherFocusReason) @@ -402,14 +399,13 @@ class SearchBoxMixin(object): # {{{ self.search.do_search() self.focus_to_library() + def search_options_button_clicked(self): + self.iactions['Preferences'].do_config(initial_plugin=('Interface', + 'Search'), close_after_initial=True) + def focus_to_library(self): self.current_view().setFocus(Qt.OtherFocusReason) - def highlight_only_changed(self, toWhat): - dynamic.set('search_highlight_only', toWhat) - self.current_view().model().set_highlight_only(toWhat) - self.focus_to_library() - # }}} class SavedSearchBoxMixin(object): # {{{ diff --git a/src/calibre/gui2/tag_view.py b/src/calibre/gui2/tag_view.py index fd3530d333..79199c6881 100644 --- a/src/calibre/gui2/tag_view.py +++ b/src/calibre/gui2/tag_view.py @@ -1214,7 +1214,7 @@ class TagBrowserMixin(object): # {{{ db.field_metadata.remove_user_categories() for k in d.categories: db.field_metadata.add_user_category('@' + k, k) - db.data.sqp_change_locations(db.field_metadata.get_search_terms()) + db.data.change_search_locations(db.field_metadata.get_search_terms()) self.tags_view.set_new_model() self.tags_view.recount() diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index 907dd577b8..5ac7e6a45d 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -483,8 +483,10 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{ action.location_selected(location) if location == 'library': self.search_restriction.setEnabled(True) + self.search_options_button.setEnabled(True) else: self.search_restriction.setEnabled(False) + self.search_options_button.setEnabled(False) # Reset the view in case something changed while it was invisible self.current_view().reset() self.set_number_of_books_shown() diff --git a/src/calibre/library/caches.py b/src/calibre/library/caches.py index e818e6a3c0..1330d10e59 100644 --- a/src/calibre/library/caches.py +++ b/src/calibre/library/caches.py @@ -11,7 +11,7 @@ from itertools import repeat from datetime import timedelta from threading import Thread -from calibre.utils.config import tweaks +from calibre.utils.config import tweaks, prefs from calibre.utils.date import parse_date, now, UNDEFINED_DATE from calibre.utils.search_query_parser import SearchQueryParser from calibre.utils.pyparsing import ParseException @@ -182,15 +182,16 @@ class ResultCache(SearchQueryParser): # {{{ self.first_sort = True self.search_restriction = '' self.field_metadata = field_metadata - all_search_locations = field_metadata.get_search_terms() - SearchQueryParser.__init__(self, all_search_locations, optimize=True) + self.all_search_locations = field_metadata.get_search_terms() + SearchQueryParser.__init__(self, self.all_search_locations, optimize=True) self.build_date_relop_dict() self.build_numeric_relop_dict() def break_cycles(self): self._data = self.field_metadata = self.FIELD_MAP = \ self.numeric_search_relops = self.date_search_relops = \ - self.db_prefs = None + self.db_prefs = self.all_search_locations = None + self.sqp_change_locations([]) def __getitem__(self, row): @@ -218,6 +219,10 @@ class ResultCache(SearchQueryParser): # {{{ def universal_set(self): return set([i[0] for i in self._data if i is not None]) + def change_search_locations(self, locations): + self.sqp_change_locations(locations) + self.all_search_locations = locations + def build_date_relop_dict(self): ''' Because the database dates have time in them, we can't use direct @@ -432,6 +437,7 @@ class ResultCache(SearchQueryParser): # {{{ # get metadata key associated with the search term. Eliminates # dealing with plurals and other aliases location = self.field_metadata.search_term_to_field_key(icu_lower(location.strip())) + # grouped search terms if isinstance(location, list): if allow_recursion: for loc in location: @@ -440,6 +446,20 @@ class ResultCache(SearchQueryParser): # {{{ return matches raise ParseException(query, len(query), 'Recursive query group detected', self) + # apply the limit if appropriate + if location == 'all' and prefs['limit_search_columns'] and \ + prefs['limit_search_columns_to']: + terms = set([]) + for l in prefs['limit_search_columns_to']: + l = icu_lower(l.strip()) + if l and l != 'all' and l in self.all_search_locations: + terms.add(l) + if terms: + for l in terms: + matches |= self.get_matches(l, query, + candidates=candidates, allow_recursion=allow_recursion) + return matches + if location in self.field_metadata: fm = self.field_metadata[location] # take care of dates special case diff --git a/src/calibre/utils/config.py b/src/calibre/utils/config.py index 11c58f7769..88197d423d 100644 --- a/src/calibre/utils/config.py +++ b/src/calibre/utils/config.py @@ -728,6 +728,17 @@ def _prefs(): c.add_opt('user_categories', default={}, help=_('User-created tag browser categories')) c.add_opt('manage_device_metadata', default='manual', help=_('How and when calibre updates metadata on the device.')) + c.add_opt('limit_search_columns', default=False, + help=_('When searching for text without using lookup ' + 'prefixes, as for example, Red instead of title:Red, ' + 'limit the columns searched to those named below.')) + c.add_opt('limit_search_columns_to', + default=['title', 'authors', 'tags', 'series'], + help=_('Choose columns to be searched when not using prefixes, ' + 'as for example, when searching for Redd instead of ' + 'title:Red. Enter a list of search/lookup names ' + 'separated by commas. Only takes effect if you set the option ' + 'to limit search columns above.')) c.add_opt('migrated', default=False, help='For Internal use. Don\'t modify.') return c