list_filter_widget.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 import os
34 import rospkg
35 
36 from python_qt_binding import loadUi
37 from python_qt_binding.QtCore import Qt
38 from python_qt_binding.QtGui import QPalette
39 from python_qt_binding.QtWidgets import QWidget
40 
41 from rqt_py_common.ini_helper import pack, unpack
42 
43 
44 class ListFilterWidget(QWidget):
45 
46  """
47  Generic List widget to be used when implementing filters that require
48  limited dynamic selections
49  """
50 
51  def __init__(self, parentfilter, rospack, item_provider):
52  """
53  :param parentfilter: The filter object, must implement set_list and
54  contain _list ''QObject''
55  :param item_provider: a function designed to provide a list or dict
56  """
57  super(ListFilterWidget, self).__init__()
58  ui_file = os.path.join(
59  rospack.get_path('rqt_console'), 'resource/filters', 'list_filter_widget.ui')
60  loadUi(ui_file, self)
61  self.setObjectName('ListFilterWidget')
62  # When data is changed we need to store it in the parent filter
63  self._parentfilter = parentfilter
64 
65  # keep color for highlighted items even when not active
66  active_color = self.palette().brush(QPalette.Active, QPalette.Highlight).color().name()
67  self.setStyleSheet('QListWidget:item:selected:!active { background: %s; }' % active_color)
68 
69  self._list_populate_function = item_provider
70  self.list_widget.itemSelectionChanged.connect(self.handle_item_changed)
71  self._display_list = []
72  self.repopulate()
73 
74  def select_item(self, text):
75  """
76  All items matching text will be selected in the list_widget
77  :param item: a string to be matched against the list ''str''
78  """
79  items = self.list_widget.findItems(text, Qt.MatchExactly)
80  for item in items:
81  item.setSelected(True)
82  self.handle_item_changed()
83 
85  self._parentfilter.set_selected_items(self.list_widget.selectedItems())
86 
87  def repopulate(self):
88  """
89  Repopulates the display widgets based on the function arguments passed
90  in during initialization
91  """
92  new_items = self._list_populate_function()
93 
94  new_set = set(new_items.values() if isinstance(new_items, dict) else new_items)
95 
96  if len(new_items) != len(self._display_list):
97  if isinstance(new_items, dict):
98  # for dictionaries store the key as user role data
99  for key in sorted(new_items.keys()):
100  item = new_items[key]
101  if item not in self._display_list:
102  self.list_widget.addItem(item)
103  self.list_widget.item(
104  self.list_widget.count() - 1).setData(Qt.UserRole, key)
105  else:
106  for item in new_items:
107  if item not in self._display_list:
108  self._add_item(item)
109  self._display_list = sorted(set(new_set) | set(self._display_list))
110 
111  def _add_item(self, item):
112  """
113  Insert item in alphabetical order.
114  """
115  for i in range(self.list_widget.count()):
116  if item <= self.list_widget.item(i).text():
117  self.list_widget.insertItem(i, item)
118  return
119  self.list_widget.addItem(item)
120 
121  def save_settings(self, settings):
122  """
123  Saves the settings for this filter.
124  :param settings: used to write the settings to an ini file ''qt_gui.settings.Settings''
125  """
126  settings.set_value('itemlist', pack(self._parentfilter._selected_items))
127 
128  def restore_settings(self, settings):
129  """
130  Restores the settings for this filter from an ini file.
131  :param settings: used to extract the settings from an ini file ''qt_gui.settings.Settings''
132  """
133  for index in range(self.list_widget.count()):
134  self.list_widget.item(index).setSelected(False)
135  item_list = unpack(settings.value('itemlist'))
136  for item in item_list:
137  if len(self.list_widget.findItems(item, Qt.MatchExactly)) == 0:
138  self.list_widget.addItem(item)
139  self.select_item(item)
def __init__(self, parentfilter, rospack, item_provider)


rqt_console
Author(s): Aaron Blasdel
autogenerated on Sun May 24 2020 03:23:49