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


rqt_console
Author(s): Aaron Blasdel
autogenerated on Sat Jun 8 2019 20:58:08