From 4f5abd290fde266cef54cc6433ff91452cff31ba Mon Sep 17 00:00:00 2001 From: suutari-olli Date: Sun, 5 Jun 2016 14:03:28 +0300 Subject: [PATCH] - Created "def verse_search_while_typing" / This does not trigger web bibles error message and is used during search while typing. - Removed the new/old setting created for the old solution --- openlp/plugins/bibles/bibleplugin.py | 3 +- openlp/plugins/bibles/lib/manager.py | 52 ++++++++++++++++++++----- openlp/plugins/bibles/lib/mediaitem.py | 53 ++++++++++++++++++++++++-- 3 files changed, 93 insertions(+), 15 deletions(-) diff --git a/openlp/plugins/bibles/bibleplugin.py b/openlp/plugins/bibles/bibleplugin.py index 0a69068f8..3d27effe4 100644 --- a/openlp/plugins/bibles/bibleplugin.py +++ b/openlp/plugins/bibles/bibleplugin.py @@ -62,8 +62,7 @@ __default_settings__ = { 'bibles/end separator': '', 'bibles/last directory import': '', 'bibles/hide combined quick error': False, - 'bibles/is search while typing enabled': True, - 'bibles/hide web bible error if searching while typing': True + 'bibles/is search while typing enabled': True } diff --git a/openlp/plugins/bibles/lib/manager.py b/openlp/plugins/bibles/lib/manager.py index 11a6e48b3..facbb167b 100644 --- a/openlp/plugins/bibles/lib/manager.py +++ b/openlp/plugins/bibles/lib/manager.py @@ -324,15 +324,15 @@ class BibleManager(RegistryProperties): if web_bible or second_web_bible: # If either Bible is Web, cursor is reset to normal and message is given. self.application.set_normal_cursor() - # If we are performing "Search while typing", do not show this error. - if not Settings().value('bibles/hide web bible error if searching while typing'): - self.main_window.information_message( - translate('BiblesPlugin.BibleManager', 'Web Bible cannot be used'), - translate('BiblesPlugin.BibleManager', 'Text Search is not available with Web Bibles.\n' - 'Please use the Scripture Reference Search instead.\n\n' - 'This means that the currently used Bible\nor Second Bible ' - 'is installed as Web Bible.') - ) + self.main_window.information_message( + translate('BiblesPlugin.BibleManager', 'Web Bible cannot be used in Text Search'), + translate('BiblesPlugin.BibleManager', 'Text Search is not available with Web Bibles.\n' + 'Please use the Scripture Reference Search instead.\n\n' + 'This means that the currently used Bible\nor Second Bible ' + 'is installed as Web Bible.\n\n' + 'If you were trying to perform a Reference search\nin Combined ' + 'Search, your reference is invalid.') + ) return None # Shorter than 3 char searches break OpenLP with very long search times, thus they are blocked. if len(text) - text.count(' ') < 3: @@ -343,6 +343,40 @@ class BibleManager(RegistryProperties): else: return None + def verse_search_while_typing(self, bible, second_bible, text): + """ + Does a verse search for the given bible and text. + This is used during "Search while typing" + It's the same thing as the normal text search, but it does not show the web Bible error. + (It would result in the error popping every time a char is entered or removed) + It also does not have a minimum text len, this is set in mediaitem.py + + :param bible: The bible to search in (unicode). + :param second_bible: The second bible (unicode). We do not search in this bible. + :param text: The text to search for (unicode). + """ + log.debug('BibleManager.verse_search("%s", "%s")', bible, text) + # If no bibles are installed, message is given. + if not bible: + self.main_window.information_message( + UiStrings().BibleNoBiblesTitle, + UiStrings().BibleNoBibles) + return None + # Check if the bible or second_bible is a web bible. + web_bible = self.db_cache[bible].get_object(BibleMeta, 'download_source') + second_web_bible = '' + if second_bible: + second_web_bible = self.db_cache[second_bible].get_object(BibleMeta, 'download_source') + if web_bible or second_web_bible: + # If either Bible is Web, cursor is reset to normal and search ends w/o any message. + self.application.set_normal_cursor() + return None + # Fetch the results from db. If no results are found, return None, no message is given for this. + elif text: + return self.db_cache[bible].verse_search(text) + else: + return None + def save_meta_data(self, bible, version, copyright, permissions, book_name_language=None): """ Saves the bibles meta data. diff --git a/openlp/plugins/bibles/lib/mediaitem.py b/openlp/plugins/bibles/lib/mediaitem.py index 53780f873..a49753ce7 100644 --- a/openlp/plugins/bibles/lib/mediaitem.py +++ b/openlp/plugins/bibles/lib/mediaitem.py @@ -727,6 +727,53 @@ class BibleMediaItem(MediaManagerItem): self.search_results = new_search_results self.second_search_results = bibles[second_bible].get_verses(text) + def on_quick_text_search_while_typing(self): + """ + We are doing a 'Text Search' while typing + Call the verse_search_while_typing from manager.py + It does not show web bible errors while typing. + (It would result in the error popping every time a char is entered or removed) + """ + # Set Bibles to use the text input from Quick search field. + bible = self.quickVersionComboBox.currentText() + second_bible = self.quickSecondComboBox.currentText() + text = self.quick_search_edit.text() + self.application.set_busy_cursor() + # Get Bibles list + bibles = self.plugin.manager.get_bibles() + # Add results to "search_results" + self.search_results = self.plugin.manager.verse_search_while_typing(bible, second_bible, text) + if second_bible and self.search_results: + # new_search_results is needed to make sure 2nd bible contains all verses. (And counting them on error) + text = [] + new_search_results = [] + count = 0 + passage_not_found = False + # Search second bible for results of search_results to make sure everythigns there. + # Count all the unfound passages. + for verse in self.search_results: + db_book = bibles[second_bible].get_book_by_book_ref_id(verse.book.book_reference_id) + if not db_book: + log.debug('Passage ("{versebookname}","{versechapter}","{verseverse}") not found in Second Bible' + .format(versebookname=verse.book.name, versechapter='verse.chapter', + verseverse=verse.verse)) + passage_not_found = True + count += 1 + continue + new_search_results.append(verse) + text.append((verse.book.book_reference_id, verse.chapter, verse.verse, verse.verse)) + if passage_not_found: + # This is for the 2nd Bible. + self.main_window.information_message( + translate('BiblesPlugin.MediaItem', 'Information'), + translate('BiblesPlugin.MediaItem', 'The second Bible does not contain all the verses ' + 'that are in the main Bible.\nOnly verses found in both Bibles' + ' will be shown.\n\n {count} verses have not been included ' + 'in the results.').format(count=count)) + # Join the searches so only verses that are found on both Bibles are shown. + self.search_results = new_search_results + self.second_search_results = bibles[second_bible].get_verses(text) + def on_quick_search_button(self): """ This triggers the proper Quick search based on which search type is used. @@ -734,7 +781,6 @@ class BibleMediaItem(MediaManagerItem): """ log.debug('Quick Search Button clicked') # If we are performing "Search while typing", this setting is set to True, here it's reset to "False" - Settings().setValue('bibles/hide web bible error if searching while typing', False) self.quickSearchButton.setEnabled(False) self.application.process_events() bible = self.quickVersionComboBox.currentText() @@ -827,7 +873,7 @@ class BibleMediaItem(MediaManagerItem): if (char.isdigit() for char in text) and len(text) > 1: self.on_quick_reference_search() if not self.search_results and len(text) > 7: - self.on_quick_text_search() + self.on_quick_text_search_while_typing() elif self.quick_search_edit.current_search_type() == BibleSearch.Reference: self.on_quick_reference_search() elif self.quick_search_edit.current_search_type() == BibleSearch.Text: @@ -857,14 +903,13 @@ class BibleMediaItem(MediaManagerItem): """ If search automatically while typing is enabled, perform the search and list results when conditions are met. """ - text = self.quick_search_edit.text() """ If web bible is used, don't show the error while searching and typing. This would result in seeing the same message multiple times. This message is located in lib\manager.py, so the setting is required. """ if Settings().value('bibles/is search while typing enabled'): - Settings().setValue('bibles/hide web bible error if searching while typing', True) + text = self.quick_search_edit.text() # Regex for finding space + any non whitemark character. (Prevents search from starting on 1 word searches) space_and_any = re.compile(' \S') # Turn this into a format that may be used in if statement.