checkbox_group.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2014, Andrew Wilson
4 # Copyright (c) 2012, Dorian Scholz
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 from python_qt_binding.QtWidgets import \
35  QButtonGroup, QCheckBox, QGroupBox, QLabel, QVBoxLayout
36 
37 
38 class CheckBoxGroup(QGroupBox):
39  """
40  Creates a button group of non-exclusive checkbox options.
41 
42  Options must be a dict with following keys: 'enabled','title','description','tooltip'
43  """
44 
45  def __init__(self, options, title='Checkboxes', selected_indexes=[], parent=None):
46  super(CheckBoxGroup, self).__init__()
47  self.setTitle(title)
48  self.setLayout(QVBoxLayout())
49  self._button_group = QButtonGroup()
50  self._button_group.setExclusive(False)
51  self._options = options
52  if parent is None:
53  parent = self
54 
55  for (button_id, option) in enumerate(self._options):
56 
57  checkbox = QCheckBox(option.get('title', 'option %d' % button_id))
58  checkbox.setEnabled(option.get('enabled', True))
59  checkbox.setChecked(button_id in selected_indexes)
60  checkbox.setToolTip(option.get('tooltip', ''))
61 
62  self._button_group.addButton(checkbox, button_id)
63  parent.layout().addWidget(checkbox)
64  if 'description' in option:
65  parent.layout().addWidget(QLabel(option['description']))
66 
67  def get_settings(self):
68  """Return dictionary with selected_indexes (array) and selected_options (array) keys."""
69  selected_indexes = []
70  selected_options = []
71  for button in self._button_group.buttons():
72  if button.isChecked():
73  selected_indexes.append(self._button_group.id(button))
74  selected_options.append(self._options[self._button_group.id(button)])
75  return {'selected_indexes': selected_indexes, 'selected_options': selected_options}
def __init__(self, options, title='Checkboxes', selected_indexes=[], parent=None)


qt_gui_py_common
Author(s): Dorian Scholz
autogenerated on Tue Apr 13 2021 03:03:15