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 import rospkg
00035 
00036 from python_qt_binding import loadUi
00037 from python_qt_binding.QtCore import Qt
00038 from python_qt_binding.QtGui import QPalette, QWidget
00039 
00040 from rqt_py_common.ini_helper import pack, unpack
00041 
00042 
00043 class ListFilterWidget(QWidget):
00044     """
00045     Generic List widget to be used when implementing filters that require
00046     limited dynamic selections
00047     """
00048     def __init__(self, parentfilter, rospack, item_provider):
00049         """
00050         :param parentfilter: The filter object, must implement set_list and
00051         contain _list ''QObject''
00052         :param item_provider: a function designed to provide a list or dict
00053         """
00054         super(ListFilterWidget, self).__init__()
00055         ui_file = os.path.join(rospack.get_path('rqt_console'), 'resource/filters', 'list_filter_widget.ui')
00056         loadUi(ui_file, self)
00057         self.setObjectName('ListFilterWidget')
00058         self._parentfilter = parentfilter  # When data is changed we need to store it in the parent filter
00059 
00060         # keep color for highlighted items even when not active
00061         active_color = self.palette().brush(QPalette.Active, QPalette.Highlight).color().name()
00062         self.setStyleSheet('QListWidget:item:selected:!active { background: %s; }' % active_color)
00063 
00064         self._list_populate_function = item_provider
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_selected_items(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         new_items = self._list_populate_function()
00088 
00089         new_set = set(new_items.values() if isinstance(new_items, dict) else new_items)
00090 
00091         if len(new_items) != len(self._display_list):
00092             if isinstance(new_items, dict):
00093                 # for dictionaries store the key as user role data
00094                 for key in sorted(new_items.keys()):
00095                     item = new_items[key]
00096                     if item not in self._display_list:
00097                         self.list_widget.addItem(item)
00098                         self.list_widget.item(self.list_widget.count() - 1).setData(Qt.UserRole, key)
00099             else:
00100                 for item in new_items:
00101                     if item not in self._display_list:
00102                         self._add_item(item)
00103         self._display_list = sorted(set(new_set) | set(self._display_list))
00104 
00105     def _add_item(self, item):
00106         """
00107         Insert item in alphabetical order.
00108         """
00109         for i in range(self.list_widget.count()):
00110             if item <= self.list_widget.item(i).text():
00111                 self.list_widget.insertItem(i, item)
00112                 return
00113         self.list_widget.addItem(item)
00114 
00115     def save_settings(self, settings):
00116         """
00117         Saves the settings for this filter.
00118         :param settings: used to write the settings to an ini file ''qt_gui.settings.Settings''
00119         """
00120         settings.set_value('itemlist', pack(self._parentfilter._selected_items))
00121 
00122     def restore_settings(self, settings):
00123         """
00124         Restores the settings for this filter from an ini file.
00125         :param settings: used to extract the settings from an ini file ''qt_gui.settings.Settings''
00126         """
00127         for index in range(self.list_widget.count()):
00128             self.list_widget.item(index).setSelected(False)
00129         item_list = unpack(settings.value('itemlist'))
00130         for item in item_list:
00131             if len(self.list_widget.findItems(item, Qt.MatchExactly)) == 0:
00132                 self.list_widget.addItem(item)
00133             self.select_item(item)


rqt_console
Author(s): Aaron Blasdel
autogenerated on Mon Oct 6 2014 07:15:09