Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 import os
00034
00035 from python_qt_binding import loadUi
00036 from python_qt_binding.QtCore import Qt
00037 from python_qt_binding.QtGui import QWidget
00038
00039 from .filter_utils import pack, unpack
00040
00041
00042 class ListFilterWidget(QWidget):
00043 """
00044 Generic List widget to be used when implementing filters that require
00045 limited dynamic selections
00046 """
00047 def __init__(self, parentfilter, display_list_args):
00048 """
00049 :param parentfilter: The filter object, must implement set_list and
00050 contain _list ''QObject''
00051 :param display_list_args: list of arguments which must contain a
00052 function designed to populate a list 'display_list_args[0]' and may
00053 contain an optional variable to pass into that function 'display_list_args[1]'
00054 """
00055 super(ListFilterWidget, self).__init__()
00056 ui_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'list_filter_widget.ui')
00057 loadUi(ui_file, self)
00058 self.setObjectName('ListFilterWidget')
00059 self._parentfilter = parentfilter
00060
00061 self._list_populate_function = display_list_args[0]
00062 self._function_argument = False
00063 if len(display_list_args) > 1:
00064 self._function_argument = display_list_args[1]
00065 self.list_widget.itemSelectionChanged.connect(self.handle_item_changed)
00066 self._display_list = []
00067 self.repopulate()
00068
00069 def select_item(self, text):
00070 """
00071 All items matching text will be selected in the list_widget
00072 :param item: a string to be matched against the list ''str''
00073 """
00074 items = self.list_widget.findItems(text, Qt.MatchExactly)
00075 for item in items:
00076 item.setSelected(True)
00077 self.handle_item_changed()
00078
00079 def handle_item_changed(self):
00080 self._parentfilter.set_list(self.list_widget.selectedItems())
00081
00082 def repopulate(self):
00083 """
00084 Repopulates the display widgets based on the function arguments passed
00085 in during initialization
00086 """
00087 if not self._function_argument is False:
00088 newlist = self._list_populate_function(self._function_argument)
00089 else:
00090 newlist = self._list_populate_function()
00091
00092 if len(newlist) != len(self._display_list):
00093 for item in newlist:
00094 if item not in self._display_list:
00095 self.list_widget.addItem(item)
00096 self._display_list = list(set(newlist + self._display_list))
00097
00098 def save_settings(self, settings):
00099 """
00100 Saves the settings for this filter.
00101 :param settings: used to write the settings to an ini file ''qt_gui.settings.Settings''
00102 """
00103 settings.set_value('displist', pack(self._display_list))
00104 settings.set_value('itemlist', pack(self._parentfilter._list))
00105
00106 def restore_settings(self, settings):
00107 """
00108 Restores the settings for this filter from an ini file.
00109 :param settings: used to extract the settings from an ini file ''qt_gui.settings.Settings''
00110 """
00111 self._display_list = unpack(settings.value('displist'))
00112 for item in self._display_list:
00113 if len(self.list_widget.findItems(item, Qt.MatchExactly)) == 0:
00114 self.list_widget.addItem(item)
00115
00116 for index in range(self.list_widget.count()):
00117 self.list_widget.item(index).setSelected(False)
00118 item_list = unpack(settings.value('itemlist'))
00119 for item in item_list:
00120 self.select_item(item)