From c3ac82a934ff1c09230f9cbaf4998d4a25b9571c Mon Sep 17 00:00:00 2001 From: Jon Tibble Date: Fri, 4 Feb 2011 18:17:28 +0000 Subject: [PATCH] UI library - add_widget_completer --- openlp/core/lib/ui.py | 8 ++++ openlp/plugins/bibles/lib/mediaitem.py | 7 ++-- openlp/plugins/songs/forms/editsongform.py | 45 ++++++++-------------- 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/openlp/core/lib/ui.py b/openlp/core/lib/ui.py index f388e03b4..983d278b0 100644 --- a/openlp/core/lib/ui.py +++ b/openlp/core/lib/ui.py @@ -182,3 +182,11 @@ def shortcut_action(parent, text, shortcuts, function): action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut) QtCore.QObject.connect(action, QtCore.SIGNAL(u'triggered()'), function) return action + +def add_widget_completer(cache, widget): + """ + Add a text autocompleter to a widget. + """ + completer = QtGui.QCompleter(cache) + completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive) + widget.setCompleter(completer) diff --git a/openlp/plugins/bibles/lib/mediaitem.py b/openlp/plugins/bibles/lib/mediaitem.py index 26f45b6b3..c162447b2 100644 --- a/openlp/plugins/bibles/lib/mediaitem.py +++ b/openlp/plugins/bibles/lib/mediaitem.py @@ -30,7 +30,8 @@ from PyQt4 import QtCore, QtGui from openlp.core.lib import MediaManagerItem, Receiver, BaseListWithDnD, \ ItemCapabilities, translate -from openlp.core.lib.ui import critical_error_message_box, media_item_combo_box +from openlp.core.lib.ui import add_widget_completer, media_item_combo_box, \ + critical_error_message_box from openlp.plugins.bibles.forms import BibleImportForm from openlp.plugins.bibles.lib import get_reference_match @@ -379,9 +380,7 @@ class BibleMediaItem(MediaManagerItem): book_data = bibles[bible].get_books() books = [book.name for book in book_data] books.sort() - completer = QtGui.QCompleter(books) - completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive) - self.quickSearchEdit.setCompleter(completer) + add_widget_completer(books, self.quickSearchEdit) def onAdvancedVersionComboBox(self): self.initialiseBible( diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index bf94503ff..57ad13f38 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -30,7 +30,7 @@ import re from PyQt4 import QtCore, QtGui from openlp.core.lib import Receiver, translate -from openlp.core.lib.ui import critical_error_message_box +from openlp.core.lib.ui import add_widget_completer, critical_error_message_box from openlp.plugins.songs.forms import EditVerseForm from openlp.plugins.songs.lib import SongXML, VerseType from openlp.plugins.songs.lib.db import Book, Song, Author, Topic @@ -129,37 +129,26 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): self.authorsComboBox.setItemData( row, QtCore.QVariant(author.id)) self.authors.append(author.display_name) - completer = QtGui.QCompleter(self.authors) - completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive) - self.authorsComboBox.setCompleter(completer) + add_widget_completer(self.authors, self.authorsComboBox) def loadTopics(self): - topics = self.manager.get_all_objects(Topic, order_by_ref=Topic.name) - self.topicsComboBox.clear() - self.topicsComboBox.addItem(u'') self.topics = [] - for topic in topics: - row = self.topicsComboBox.count() - self.topicsComboBox.addItem(topic.name) - self.topics.append(topic.name) - self.topicsComboBox.setItemData(row, QtCore.QVariant(topic.id)) - completer = QtGui.QCompleter(self.topics) - completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive) - self.topicsComboBox.setCompleter(completer) + self.__loadObjects(Topic, self.topicsComboBox, self.topics) def loadBooks(self): - books = self.manager.get_all_objects(Book, order_by_ref=Book.name) - self.songBookComboBox.clear() - self.songBookComboBox.addItem(u'') self.books = [] - for book in books: - row = self.songBookComboBox.count() - self.songBookComboBox.addItem(book.name) - self.books.append(book.name) - self.songBookComboBox.setItemData(row, QtCore.QVariant(book.id)) - completer = QtGui.QCompleter(self.books) - completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive) - self.songBookComboBox.setCompleter(completer) + self.__loadObjects(Book, self.songBookComboBox, self.books) + + def __loadObjects(self, class, combo, cache): + objects = self.manager.get_all_objects(class, order_by_ref=class.name) + combo.clear() + combo.addItem(u'') + for object in objects: + row = combo.count() + combo.addItem(object.name) + cache.append(object.name) + combo.setItemData(row, QtCore.QVariant(object.id)) + add_widget_completer(cache, combo) def loadThemes(self, theme_list): self.themeComboBox.clear() @@ -168,9 +157,7 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): for theme in theme_list: self.themeComboBox.addItem(theme) self.themes.append(theme) - completer = QtGui.QCompleter(self.themes) - completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive) - self.themeComboBox.setCompleter(completer) + add_widget_completer(self.themes, self.themeComboBox) def newSong(self): log.debug(u'New Song')