custom_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 CustomFilterWidget(QWidget):
45 
46  def __init__(self, parentfilter, rospack, item_providers):
47  super(CustomFilterWidget, self).__init__()
48  ui_file = os.path.join(
49  rospack.get_path('rqt_console'), 'resource/filters', 'custom_filter_widget.ui')
50  loadUi(ui_file, self)
51  self.setObjectName('CustomFilterWidget')
52  self._parentfilter = parentfilter # When data is changed it is stored in the parent filter
53 
54  # keep color for highlighted items even when not active
55  for list_widget in [self.severity_list, self.node_list, self.topic_list]:
56  active_color = list_widget.palette().brush(
57  QPalette.Active, QPalette.Highlight).color().name()
58  list_widget.setStyleSheet(
59  'QListWidget:item:selected:!active { background: %s; }' % active_color)
60 
61  # Text Filter Initialization
62  self.text_edit.textChanged.connect(self.handle_text_changed)
63  self.regex_check_box.clicked[bool].connect(self.handle_regex_clicked)
64  self.handle_text_changed()
65 
66  # Severity Filter Initialization
67  self.severity_list.itemSelectionChanged.connect(self.handle_severity_item_changed)
68  new_items = item_providers[0]()
69  for key in sorted(new_items.keys()):
70  item = new_items[key]
71  self.severity_list.addItem(item)
72  self.severity_list.item(self.severity_list.count() - 1).setData(Qt.UserRole, key)
73 
74  # Node Filter Initialization
75  self._node_list_populate_function = item_providers[1]
76  self.node_list.itemSelectionChanged.connect(self.handle_node_item_changed)
77 
78  # Topic Filter Initialization
79  self._topic_list_populate_function = item_providers[2]
80  self.topic_list.itemSelectionChanged.connect(self.handle_topic_item_changed)
81 
82  self.repopulate()
83 
85  self._parentfilter._node.set_selected_items(self.node_list.selectedItems())
86 
88  self._parentfilter._topic.set_selected_items(self.topic_list.selectedItems())
89 
91  self._parentfilter._severity.set_selected_items(self.severity_list.selectedItems())
92 
94  self._parentfilter._message.set_text(self.text_edit.text())
95 
96  def handle_regex_clicked(self, clicked):
97  self._parentfilter._message.set_regex(clicked)
98 
99  def repopulate(self):
100  """
101  Repopulates the display widgets based on the function arguments passed
102  in during initialization
103  """
104  newset = self._topic_list_populate_function()
105  for item in newset:
106  if len(self.topic_list.findItems(item, Qt.MatchExactly)) == 0:
107  self._add_item(self.topic_list, item)
108 
109  newset = self._node_list_populate_function()
110  for item in newset:
111  if len(self.node_list.findItems(item, Qt.MatchExactly)) == 0:
112  self._add_item(self.node_list, item)
113 
114  def _add_item(self, list_widget, item):
115  """
116  Insert item in alphabetical order.
117  """
118  for i in range(list_widget.count()):
119  if item <= list_widget.item(i).text():
120  list_widget.insertItem(i, item)
121  return
122  list_widget.addItem(item)
123 
124  def save_settings(self, settings):
125  """
126  Saves the settings for this filter to an ini file.
127  :param settings: used to write the settings to an ini file ''qt_gui.settings.Settings''
128  """
129  settings.set_value('text', self._parentfilter._message._text)
130  settings.set_value('regex', self._parentfilter._message._regex)
131 
132  settings.set_value('severityitemlist', pack(self._parentfilter._severity._selected_items))
133 
134  settings.set_value('topicitemlist', pack(self._parentfilter._topic._selected_items))
135 
136  settings.set_value('nodeitemlist', pack(self._parentfilter._node._selected_items))
137 
138  return
139 
140  def restore_settings(self, settings):
141  """
142  Restores the settings for this filter from an ini file.
143  :param settings: used to extract the settings from an ini file ''qt_gui.settings.Settings''
144  """
145  text = settings.value('text', '')
146  self.text_edit.setText(text)
147  self.handle_text_changed()
148 
149  regex = settings.value('regex') in [True, 'true']
150  self.regex_check_box.setChecked(regex)
151  self.handle_regex_clicked(regex)
152 
153  # Restore Severities
154  for index in range(self.severity_list.count()):
155  self.severity_list.item(index).setSelected(False)
156  severity_item_list = unpack(settings.value('severityitemlist'))
157  for item in severity_item_list:
158  items = self.severity_list.findItems(item, Qt.MatchExactly)
159  for item in items:
160  item.setSelected(True)
162 
163  # Restore Topics
164  for index in range(self.topic_list.count()):
165  self.topic_list.item(index).setSelected(False)
166  topic_item_list = unpack(settings.value('topicitemlist'))
167  for item in topic_item_list:
168  if len(self.topic_list.findItems(item, Qt.MatchExactly)) == 0:
169  self.topic_list.addItem(item)
170  items = self.topic_list.findItems(item, Qt.MatchExactly)
171  for item in items:
172  item.setSelected(True)
174 
175  # Restore Nodes
176  for index in range(self.node_list.count()):
177  self.node_list.item(index).setSelected(False)
178  node_item_list = unpack(settings.value('nodeitemlist'))
179  for item in node_item_list:
180  if len(self.node_list.findItems(item, Qt.MatchExactly)) == 0:
181  self.node_list.addItem(item)
182  items = self.node_list.findItems(item, Qt.MatchExactly)
183  for item in items:
184  item.setSelected(True)
def __init__(self, parentfilter, rospack, item_providers)


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