blacklist.py
Go to the documentation of this file.
00001 # Copyright 2014 Open Source Robotics Foundation, Inc.
00002 #
00003 # Licensed under the Apache License, Version 2.0 (the "License");
00004 # you may not use this file except in compliance with the License.
00005 # You may obtain a copy of the License at
00006 #
00007 #    http://www.apache.org/licenses/LICENSE-2.0
00008 #
00009 # Unless required by applicable law or agreed to in writing, software
00010 # distributed under the License is distributed on an "AS IS" BASIS,
00011 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 # See the License for the specific language governing permissions and
00013 # limitations under the License.
00014 
00015 """ Blacklist
00016 Provides a blacklist editing dialog.
00017 
00018 from python_qt_binding.QtGui import QApplication
00019 from blacklist import BlacklistDialog
00020 
00021 app = QApplication(sys.argv)
00022 current_blacklist = ['hare', 'hyrax', 'camel', 'pig']
00023 revised_blacklist = BlacklistDialog.get_blacklist(values=current_blacklist)
00024 """
00025 
00026 from python_qt_binding.QtGui import QDialog
00027 from python_qt_binding.QtGui import QHBoxLayout
00028 from python_qt_binding.QtGui import QListView
00029 from python_qt_binding.QtGui import QPushButton
00030 from python_qt_binding.QtGui import QStringListModel
00031 from python_qt_binding.QtGui import QVBoxLayout
00032 from python_qt_binding.QtGui import QIcon
00033 
00034 
00035 class BlacklistDialog(QDialog):
00036     """ BlacklistDialog """
00037     def __init__(self, parent=None, current_values=None):
00038         super(BlacklistDialog, self).__init__(parent)
00039         self.setWindowTitle("Blacklist")
00040         vbox = QVBoxLayout()
00041         self.setLayout(vbox)
00042 
00043         self._blacklist = Blacklist()
00044         if isinstance(current_values, list):
00045             for val in current_values:
00046                 self._blacklist.append(val)
00047         vbox.addWidget(self._blacklist)
00048 
00049         controls_layout = QHBoxLayout()
00050         add_button = QPushButton(icon=QIcon.fromTheme('list-add'))
00051         rem_button = QPushButton(icon=QIcon.fromTheme('list-remove'))
00052         ok_button = QPushButton("Ok")
00053         cancel_button = QPushButton("Cancel")
00054         add_button.clicked.connect(self._add_item)
00055         rem_button.clicked.connect(self._remove_item)
00056         ok_button.clicked.connect(self.accept)
00057         cancel_button.clicked.connect(self.reject)
00058 
00059         controls_layout.addWidget(add_button)
00060         controls_layout.addWidget(rem_button)
00061         controls_layout.addStretch(0)
00062         controls_layout.addWidget(ok_button)
00063         controls_layout.addWidget(cancel_button)
00064         vbox.addLayout(controls_layout)
00065 
00066     def _add_item(self):
00067         """ Adds a new default item to the list """
00068         self._blacklist.append("new item")
00069 
00070     def _remove_item(self):
00071         """ Removes any selected items from the list """
00072         self._blacklist.remove_selected()
00073 
00074     def get_values(self):
00075         """ returns the most recent values of the blacklist """
00076         return self._blacklist.get_values()
00077 
00078     @staticmethod
00079     def get_blacklist(parent=None, values=None):
00080         """ Get a modified blacklist
00081         :param list values: current blacklisted values to populate the dialog with
00082         :returns: revised list if Ok is pressed, else values
00083         """
00084         dialog = BlacklistDialog(parent=parent, current_values=values)
00085         result = dialog.exec_()
00086         return dialog.get_values() if result == QDialog.Accepted else values
00087 
00088 
00089 class Blacklist(QListView):
00090     """ an editable list of strings """
00091     def __init__(self, parent=None):
00092         super(Blacklist, self).__init__(parent)
00093         self._list = list()
00094         self._model = QStringListModel()
00095         self._model.setStringList(self._list)
00096         self.setModel(self._model)
00097         self.setDragEnabled(True)
00098 
00099         # Keep track of the selected value
00100         self._selected_index = None
00101         self.clicked.connect(self._selected)
00102 
00103         self._model.dataChanged.connect(self._datachanged)
00104 
00105     def _selected(self, index):
00106         """ registers when an item is selected """
00107         self._selected_index = index.row()
00108 
00109     def _datachanged(self, tl, br):
00110         """ Reorder list alphabetically when things change """
00111         self._list = self._model.stringList()
00112         self._list.sort()
00113         self._model.setStringList(self._list)
00114 
00115     def append(self, val):
00116         """ Adds a value to the list """
00117         self._list = self._model.stringList()
00118         self._list.append(val)
00119         self._model.setStringList(self._list)
00120         self._selected_index = None
00121 
00122     def remove_selected(self):
00123         """ removes any selected value from the list """
00124         if self._selected_index is not None:
00125             self._list = self._model.stringList()
00126             self._list.pop(self._selected_index)
00127             self._model.setStringList(self._list)
00128             self._selected_index = None
00129 
00130     def get_values(self):
00131         """ returns the values stored in the list """
00132         return [str(val) for val in self._model.stringList()]


rqt_graphprofiler
Author(s): Dan Brooks
autogenerated on Thu Jun 6 2019 20:29:31