list_dialog.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2021, Matthijs van der Burgh
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 from python_qt_binding.QtCore import Qt
34 from python_qt_binding.QtGui import QColor
35 from python_qt_binding.QtWidgets import QDialog, QDialogButtonBox, QGroupBox, QHBoxLayout, QListWidget, QListWidgetItem, QPushButton, QVBoxLayout, QWidget
36 
37 
38 class ListDialog(QDialog):
39 
40  def __init__(self, title, items, parent):
41  super(ListDialog, self).__init__(parent)
42 
43  self._layout = QVBoxLayout()
44  self._view_box = QGroupBox("Optional topics")
45  self._view_box_layout = QVBoxLayout(self._view_box)
46  self._list_widget = QListWidget()
47  self._view_box_layout.addWidget(self._list_widget)
48  self._button_box = QDialogButtonBox(QDialogButtonBox.Save | QDialogButtonBox.Close)
49  self._button_box.accepted.connect(self.save)
50  self._button_box.rejected.connect(self.reject)
51 
52  self.setWindowTitle(title)
53 
54  self.create_list_widget(items)
55 
56  self._layout.addWidget(self._view_box)
57  self._layout.addWidget(self._button_box)
58  self.setLayout(self._layout)
59 
60  # createConnections
61  self._list_widget.itemChanged.connect(self.highlight_checked)
62 
63  self.selected_items = []
64 
65  def create_list_widget(self, items):
66  for item in items:
67  w_item = QListWidgetItem()
68  w_item.setText(item[0])
69  w_item.setFlags(w_item.flags() | Qt.ItemIsUserCheckable)
70  w_item.setCheckState(Qt.Checked if item[1] else Qt.Unchecked)
71  self._list_widget.addItem(w_item)
72  self._list_widget.setMinimumWidth(self._list_widget.sizeHintForColumn(0) + 5)
73 
74  @staticmethod
75  def highlight_checked(item):
76  if item.checkState() == Qt.Checked:
77  item.setBackground(QColor("#ffffb2"))
78  else:
79  item.setBackground(QColor("#ffffff"))
80 
81  def save(self):
82  self.selected_items = []
83  for i in range(len(self._list_widget)):
84  item = self._list_widget.item(i)
85  if item.checkState() == Qt.Checked:
86  self.selected_items.append(item.text())
87 
88  self.accept()
89 
90  def exec_(self):
91  error_code = super(ListDialog, self).exec_()
92  return self.selected_items, error_code
def __init__(self, title, items, parent)
Definition: list_dialog.py:40


rqt_nav_view
Author(s): Ze'ev Klapow
autogenerated on Wed Mar 24 2021 03:00:21