custom_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 CustomFilterWidget(QWidget):
00045 
00046     def __init__(self, parentfilter, rospack, item_providers):
00047         super(CustomFilterWidget, self).__init__()
00048         ui_file = os.path.join(
00049             rospack.get_path('rqt_console'), 'resource/filters', 'custom_filter_widget.ui')
00050         loadUi(ui_file, self)
00051         self.setObjectName('CustomFilterWidget')
00052         self._parentfilter = parentfilter  # When data is changed it is stored in the parent filter
00053 
00054         # keep color for highlighted items even when not active
00055         for list_widget in [self.severity_list, self.node_list, self.topic_list]:
00056             active_color = list_widget.palette().brush(
00057                 QPalette.Active, QPalette.Highlight).color().name()
00058             list_widget.setStyleSheet(
00059                 'QListWidget:item:selected:!active { background: %s; }' % active_color)
00060 
00061         # Text Filter Initialization
00062         self.text_edit.textChanged.connect(self.handle_text_changed)
00063         self.regex_check_box.clicked[bool].connect(self.handle_regex_clicked)
00064         self.handle_text_changed()
00065 
00066         # Severity Filter Initialization
00067         self.severity_list.itemSelectionChanged.connect(self.handle_severity_item_changed)
00068         new_items = item_providers[0]()
00069         for key in sorted(new_items.keys()):
00070             item = new_items[key]
00071             self.severity_list.addItem(item)
00072             self.severity_list.item(self.severity_list.count() - 1).setData(Qt.UserRole, key)
00073 
00074         # Node Filter Initialization
00075         self._node_list_populate_function = item_providers[1]
00076         self.node_list.itemSelectionChanged.connect(self.handle_node_item_changed)
00077 
00078         # Topic Filter Initialization
00079         self._topic_list_populate_function = item_providers[2]
00080         self.topic_list.itemSelectionChanged.connect(self.handle_topic_item_changed)
00081 
00082         self.repopulate()
00083 
00084     def handle_node_item_changed(self):
00085         self._parentfilter._node.set_selected_items(self.node_list.selectedItems())
00086 
00087     def handle_topic_item_changed(self):
00088         self._parentfilter._topic.set_selected_items(self.topic_list.selectedItems())
00089 
00090     def handle_severity_item_changed(self):
00091         self._parentfilter._severity.set_selected_items(self.severity_list.selectedItems())
00092 
00093     def handle_text_changed(self):
00094         self._parentfilter._message.set_text(self.text_edit.text())
00095 
00096     def handle_regex_clicked(self, clicked):
00097         self._parentfilter._message.set_regex(clicked)
00098 
00099     def repopulate(self):
00100         """
00101         Repopulates the display widgets based on the function arguments passed
00102         in during initialization
00103         """
00104         newset = self._topic_list_populate_function()
00105         for item in newset:
00106             if len(self.topic_list.findItems(item, Qt.MatchExactly)) == 0:
00107                 self._add_item(self.topic_list, item)
00108 
00109         newset = self._node_list_populate_function()
00110         for item in newset:
00111             if len(self.node_list.findItems(item, Qt.MatchExactly)) == 0:
00112                 self._add_item(self.node_list, item)
00113 
00114     def _add_item(self, list_widget, item):
00115         """
00116         Insert item in alphabetical order.
00117         """
00118         for i in range(list_widget.count()):
00119             if item <= list_widget.item(i).text():
00120                 list_widget.insertItem(i, item)
00121                 return
00122         list_widget.addItem(item)
00123 
00124     def save_settings(self, settings):
00125         """
00126         Saves the settings for this filter to an ini file.
00127         :param settings: used to write the settings to an ini file ''qt_gui.settings.Settings''
00128         """
00129         settings.set_value('text', self._parentfilter._message._text)
00130         settings.set_value('regex', self._parentfilter._message._regex)
00131 
00132         settings.set_value('severityitemlist', pack(self._parentfilter._severity._selected_items))
00133 
00134         settings.set_value('topicitemlist', pack(self._parentfilter._topic._selected_items))
00135 
00136         settings.set_value('nodeitemlist', pack(self._parentfilter._node._selected_items))
00137 
00138         return
00139 
00140     def restore_settings(self, settings):
00141         """
00142         Restores the settings for this filter from an ini file.
00143         :param settings: used to extract the settings from an ini file ''qt_gui.settings.Settings''
00144         """
00145         text = settings.value('text', '')
00146         self.text_edit.setText(text)
00147         self.handle_text_changed()
00148 
00149         regex = settings.value('regex') in [True, 'true']
00150         self.regex_check_box.setChecked(regex)
00151         self.handle_regex_clicked(regex)
00152 
00153         # Restore Severities
00154         for index in range(self.severity_list.count()):
00155             self.severity_list.item(index).setSelected(False)
00156         severity_item_list = unpack(settings.value('severityitemlist'))
00157         for item in severity_item_list:
00158             items = self.severity_list.findItems(item, Qt.MatchExactly)
00159             for item in items:
00160                 item.setSelected(True)
00161         self.handle_severity_item_changed()
00162 
00163         # Restore Topics
00164         for index in range(self.topic_list.count()):
00165             self.topic_list.item(index).setSelected(False)
00166         topic_item_list = unpack(settings.value('topicitemlist'))
00167         for item in topic_item_list:
00168             if len(self.topic_list.findItems(item, Qt.MatchExactly)) == 0:
00169                 self.topic_list.addItem(item)
00170             items = self.topic_list.findItems(item, Qt.MatchExactly)
00171             for item in items:
00172                 item.setSelected(True)
00173         self.handle_topic_item_changed()
00174 
00175         # Restore Nodes
00176         for index in range(self.node_list.count()):
00177             self.node_list.item(index).setSelected(False)
00178         node_item_list = unpack(settings.value('nodeitemlist'))
00179         for item in node_item_list:
00180             if len(self.node_list.findItems(item, Qt.MatchExactly)) == 0:
00181                 self.node_list.addItem(item)
00182             items = self.node_list.findItems(item, Qt.MatchExactly)
00183             for item in items:
00184                 item.setSelected(True)
00185         self.handle_node_item_changed()


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