list_filter_widget.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
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  # When data is changed we need to store it in the parent filter
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)


rqt_console
Author(s): Aaron Blasdel
autogenerated on Fri Jan 3 2014 11:54:30