exclusive_options_group.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Dorian Scholz
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.QtWidgets import \
34  QButtonGroup, QGroupBox, QLabel, QRadioButton, QVBoxLayout
35 
36 
37 class ExclusiveOptionGroup(QGroupBox):
38  """
39  Creates a button group of exclusive radio options.
40 
41  Options must be a dict with following keys:
42  'enabled', 'selected', 'title', 'description', 'tooltip'
43  """
44 
45  def __init__(self, options, title='Exclusive Options', selected_index=None, parent=None):
46  super(ExclusiveOptionGroup, self).__init__()
47  self.setTitle(title)
48  self.setLayout(QVBoxLayout())
49  self._button_group = QButtonGroup()
50  self._button_group.setExclusive(True)
51  self._options = options
52  if parent is None:
53  parent = self
54 
55  for (button_id, option) in enumerate(self._options):
56 
57  radio_button = QRadioButton(option.get('title', 'option %d' % button_id))
58  radio_button.setEnabled(option.get('enabled', True))
59  radio_button.setChecked(option.get('selected', False) or button_id == selected_index)
60  radio_button.setToolTip(option.get('tooltip', ''))
61 
62  self._button_group.addButton(radio_button, button_id)
63  parent.layout().addWidget(radio_button)
64  if 'description' in option:
65  parent.layout().addWidget(QLabel(option['description']))
66 
67  def get_settings(self):
68  """Return dictionary with selected_index (int) and selected_option (dict) keys."""
69  selected_index = self._button_group.checkedId()
70  if selected_index >= 0:
71  return
72  {
73  'selected_index': selected_index,
74  'selected_option': self._options[selected_index]
75  }
76  return {'selected_index': None, 'selected_option': None}
def __init__(self, options, title='Exclusive Options', selected_index=None, parent=None)


qt_gui_py_common
Author(s): Dorian Scholz
autogenerated on Thu Jun 6 2019 19:54:33