Modified SaveEdit, so that it saves the chosen search type

This commit is contained in:
Philip Ridout 2016-11-11 21:23:03 +00:00
parent 389fb391df
commit 80793f9412
6 changed files with 32 additions and 44 deletions

View File

@ -266,7 +266,7 @@ class MediaManagerItem(QtWidgets.QWidget, RegistryProperties):
self.search_text_layout.setObjectName('search_text_layout')
self.search_text_label = QtWidgets.QLabel(self.search_widget)
self.search_text_label.setObjectName('search_text_label')
self.search_text_edit = SearchEdit(self.search_widget)
self.search_text_edit = SearchEdit(self.search_widget, self.settings_section)
self.search_text_edit.setObjectName('search_text_edit')
self.search_text_label.setBuddy(self.search_text_edit)
self.search_text_layout.addRow(self.search_text_label, self.search_text_edit)

View File

@ -26,6 +26,7 @@ from PyQt5 import QtCore, QtWidgets
from openlp.core.lib import build_icon
from openlp.core.lib.ui import create_widget_action
from openlp.core.common import Settings
log = logging.getLogger(__name__)
@ -37,11 +38,12 @@ class SearchEdit(QtWidgets.QLineEdit):
searchTypeChanged = QtCore.pyqtSignal(QtCore.QVariant)
cleared = QtCore.pyqtSignal()
def __init__(self, parent):
def __init__(self, parent, settings_section):
"""
Constructor.
"""
super(SearchEdit, self).__init__(parent)
super().__init__(parent)
self.settings_section = settings_section
self._current_search_type = -1
self.clear_button = QtWidgets.QToolButton(self)
self.clear_button.setIcon(build_icon(':/system/clear_shortcut.png'))
@ -100,14 +102,10 @@ class SearchEdit(QtWidgets.QLineEdit):
menu = self.menu_button.menu()
for action in menu.actions():
if identifier == action.data():
# setPlaceholderText has been implemented in Qt 4.7 and in at least PyQt 4.9 (I am not sure, if it was
# implemented in PyQt 4.8).
try:
self.setPlaceholderText(action.placeholder_text)
except AttributeError:
pass
self.setPlaceholderText(action.placeholder_text)
self.menu_button.setDefaultAction(action)
self._current_search_type = identifier
Settings().setValue('{section}/last search type'.format(section=self.settings_section), identifier)
self.searchTypeChanged.emit(identifier)
return True
@ -130,14 +128,10 @@ class SearchEdit(QtWidgets.QLineEdit):
(2, ":/songs/authors.png", "Authors", "Search Authors...")
"""
menu = QtWidgets.QMenu(self)
first = None
for identifier, icon, title, placeholder in items:
action = create_widget_action(
menu, text=title, icon=icon, data=identifier, triggers=self._on_menu_action_triggered)
action.placeholder_text = placeholder
if first is None:
first = action
self._current_search_type = identifier
if not hasattr(self, 'menu_button'):
self.menu_button = QtWidgets.QToolButton(self)
self.menu_button.setIcon(build_icon(':/system/clear_shortcut.png'))
@ -146,7 +140,8 @@ class SearchEdit(QtWidgets.QLineEdit):
self.menu_button.setStyleSheet('QToolButton { border: none; padding: 0px 10px 0px 0px; }')
self.menu_button.resize(QtCore.QSize(28, 18))
self.menu_button.setMenu(menu)
self.menu_button.setDefaultAction(first)
self.set_current_search_type(
Settings().value('{section}/last search type'.format(section=self.settings_section)))
self.menu_button.show()
self._update_style_sheet()

View File

@ -199,7 +199,7 @@ class BibleMediaItem(MediaManagerItem):
self.quick_search_label = QtWidgets.QLabel(self.quickTab)
self.quick_search_label.setObjectName('quick_search_label')
self.quickLayout.addWidget(self.quick_search_label, 0, 0, QtCore.Qt.AlignRight)
self.quick_search_edit = SearchEdit(self.quickTab)
self.quick_search_edit = SearchEdit(self.quickTab, self.settings_section)
self.quick_search_edit.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Fixed)
self.quick_search_edit.setObjectName('quick_search_edit')
self.quick_search_label.setBuddy(self.quick_search_edit)
@ -333,8 +333,8 @@ class BibleMediaItem(MediaManagerItem):
translate('BiblesPlugin.MediaItem', 'Text Search'),
translate('BiblesPlugin.MediaItem', 'Search Text...'))
])
text = self.settings_section
self.quick_search_edit.set_current_search_type(Settings().value('{text}/last search type'.format(text=text)))
if Settings().value(self.settings_section + '/reset to combined quick search'):
self.quick_search_edit.set_current_search_type(BibleSearch.Combined)
self.config_update()
log.debug('bible manager initialise complete')
@ -444,15 +444,7 @@ class BibleMediaItem(MediaManagerItem):
only updated when we are doing reference or combined search, in text search the completion list is removed.
"""
log.debug('update_auto_completer')
# Save the current search type to the configuration. If setting for automatically resetting the search type to
# Combined is enabled, use that otherwise use the currently selected search type.
# Note: This setting requires a restart to take effect.
if Settings().value(self.settings_section + '/reset to combined quick search'):
Settings().setValue('{section}/last search type'.format(section=self.settings_section),
BibleSearch.Combined)
else:
Settings().setValue('{section}/last search type'.format(section=self.settings_section),
self.quick_search_edit.current_search_type())
# TODO: Move this else where?
# Save the current bible to the configuration.
Settings().setValue('{section}/quick bible'.format(section=self.settings_section),
self.quickVersionComboBox.currentText())

View File

@ -105,8 +105,6 @@ class CustomMediaItem(MediaManagerItem):
[(CustomSearch.Titles, ':/songs/song_search_title.png', translate('SongsPlugin.MediaItem', 'Titles'),
translate('SongsPlugin.MediaItem', 'Search Titles...')),
(CustomSearch.Themes, ':/slides/slide_theme.png', UiStrings().Themes, UiStrings().SearchThemes)])
text = '{section}/last search type'.format(section=self.settings_section)
self.search_text_edit.set_current_search_type(Settings().value(text))
self.load_list(self.plugin.db_manager.get_all_objects(CustomSlide, order_by_ref=CustomSlide.title))
self.config_update()
@ -250,9 +248,6 @@ class CustomMediaItem(MediaManagerItem):
"""
Search the plugin database
"""
# Save the current search type to the configuration.
Settings().setValue('{section}/last search type'.format(section=self.settings_section),
self.search_text_edit.current_search_type())
# Reload the list considering the new search type.
search_type = self.search_text_edit.current_search_type()
search_keywords = '%{search}%'.format(search=self.whitespace.sub(' ', self.search_text_edit.displayText()))

View File

@ -166,14 +166,9 @@ class SongMediaItem(MediaManagerItem):
translate('SongsPlugin.MediaItem', 'CCLI number'),
translate('SongsPlugin.MediaItem', 'Search CCLI number...'))
])
self.search_text_edit.set_current_search_type(
Settings().value('{section}/last search type'.format(section=self.settings_section)))
self.config_update()
def on_search_text_button_clicked(self):
# Save the current search type to the configuration.
Settings().setValue('{section}/last search type'.format(section=self.settings_section),
self.search_text_edit.current_search_type())
# Reload the list considering the new search type.
search_keywords = str(self.search_text_edit.displayText())
search_type = self.search_text_edit.current_search_type()

View File

@ -23,6 +23,7 @@
Module to test the EditCustomForm.
"""
from unittest import TestCase
from unittest.mock import MagicMock, call, patch
from PyQt5 import QtCore, QtGui, QtTest, QtWidgets
@ -58,7 +59,12 @@ class TestSearchEdit(TestCase, TestMixin):
self.main_window = QtWidgets.QMainWindow()
Registry().register('main_window', self.main_window)
self.search_edit = SearchEdit(self.main_window)
settings_patcher = patch(
'openlp.core.lib.searchedit.Settings', return_value=MagicMock(**{'value.return_value': SearchTypes.First}))
self.addCleanup(settings_patcher.stop)
self.mocked_settings = settings_patcher.start()
self.search_edit = SearchEdit(self.main_window,'settings_section')
# To complete set up we have to set the search types.
self.search_edit.set_search_types(SEARCH_TYPES)
@ -78,8 +84,11 @@ class TestSearchEdit(TestCase, TestMixin):
# WHEN:
# THEN: The first search type should be the first one in the list.
assert self.search_edit.current_search_type() == SearchTypes.First, "The first search type should be selected."
# THEN: The first search type should be the first one in the list. The selected type should be saved in the
# settings
self.assertEqual(self.search_edit.current_search_type(), SearchTypes.First,
"The first search type should be selected.")
self.mocked_settings().setValue.assert_called_once_with('settings_section/last search type', 0)
def test_set_current_search_type(self):
"""
@ -90,11 +99,13 @@ class TestSearchEdit(TestCase, TestMixin):
result = self.search_edit.set_current_search_type(SearchTypes.Second)
# THEN:
assert result, "The call should return success (True)."
assert self.search_edit.current_search_type() == SearchTypes.Second,\
"The search type should be SearchTypes.Second"
assert self.search_edit.placeholderText() == SECOND_PLACEHOLDER_TEXT,\
"The correct placeholder text should be 'Second Placeholder Text'."
self.assertTrue(result, "The call should return success (True).")
self.assertEqual(self.search_edit.current_search_type(), SearchTypes.Second,
"The search type should be SearchTypes.Second")
self.assertEqual(self.search_edit.placeholderText(), SECOND_PLACEHOLDER_TEXT,
"The correct placeholder text should be 'Second Placeholder Text'.")
self.mocked_settings().setValue.assert_has_calls(
[call('settings_section/last search type', 0), call('settings_section/last search type', 1)])
def test_clear_button_visibility(self):
"""