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
00039 from python_qt_binding.QtWidgets import QWidget
00040 
00041 from rqt_py_common.ini_helper import pack, unpack
00042 
00043 
00044 class ListFilterWidget(QWidget):
00045     """
00046     Generic List widget to be used when implementing filters that require
00047     limited dynamic selections
00048     """
00049     def __init__(self, parentfilter, rospack, item_provider):
00050         """
00051         :param parentfilter: The filter object, must implement set_list and
00052         contain _list ''QObject''
00053         :param item_provider: a function designed to provide a list or dict
00054         """
00055         super(ListFilterWidget, self).__init__()
00056         ui_file = os.path.join(rospack.get_path('rqt_console'), 'resource/filters', '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         # keep color for highlighted items even when not active
00062         active_color = self.palette().brush(QPalette.Active, QPalette.Highlight).color().name()
00063         self.setStyleSheet('QListWidget:item:selected:!active { background: %s; }' % active_color)
00064 
00065         self._list_populate_function = item_provider
00066         self.list_widget.itemSelectionChanged.connect(self.handle_item_changed)
00067         self._display_list = []
00068         self.repopulate()
00069 
00070     def select_item(self, text):
00071         """
00072         All items matching text will be selected in the list_widget
00073         :param item: a string to be matched against the list ''str''
00074         """
00075         items = self.list_widget.findItems(text, Qt.MatchExactly)
00076         for item in items:
00077             item.setSelected(True)
00078         self.handle_item_changed()
00079 
00080     def handle_item_changed(self):
00081         self._parentfilter.set_selected_items(self.list_widget.selectedItems())
00082 
00083     def repopulate(self):
00084         """
00085         Repopulates the display widgets based on the function arguments passed
00086         in during initialization
00087         """
00088         new_items = self._list_populate_function()
00089 
00090         new_set = set(new_items.values() if isinstance(new_items, dict) else new_items)
00091 
00092         if len(new_items) != len(self._display_list):
00093             if isinstance(new_items, dict):
00094                 # for dictionaries store the key as user role data
00095                 for key in sorted(new_items.keys()):
00096                     item = new_items[key]
00097                     if item not in self._display_list:
00098                         self.list_widget.addItem(item)
00099                         self.list_widget.item(self.list_widget.count() - 1).setData(Qt.UserRole, key)
00100             else:
00101                 for item in new_items:
00102                     if item not in self._display_list:
00103                         self._add_item(item)
00104         self._display_list = sorted(set(new_set) | set(self._display_list))
00105 
00106     def _add_item(self, item):
00107         """
00108         Insert item in alphabetical order.
00109         """
00110         for i in range(self.list_widget.count()):
00111             if item <= self.list_widget.item(i).text():
00112                 self.list_widget.insertItem(i, item)
00113                 return
00114         self.list_widget.addItem(item)
00115 
00116     def save_settings(self, settings):
00117         """
00118         Saves the settings for this filter.
00119         :param settings: used to write the settings to an ini file ''qt_gui.settings.Settings''
00120         """
00121         settings.set_value('itemlist', pack(self._parentfilter._selected_items))
00122 
00123     def restore_settings(self, settings):
00124         """
00125         Restores the settings for this filter from an ini file.
00126         :param settings: used to extract the settings from an ini file ''qt_gui.settings.Settings''
00127         """
00128         for index in range(self.list_widget.count()):
00129             self.list_widget.item(index).setSelected(False)
00130         item_list = unpack(settings.value('itemlist'))
00131         for item in item_list:
00132             if len(self.list_widget.findItems(item, Qt.MatchExactly)) == 0:
00133                 self.list_widget.addItem(item)
00134             self.select_item(item)


rqt_console
Author(s): Aaron Blasdel
autogenerated on Wed May 3 2017 02:48:27