Package node_manager_fkie :: Module select_dialog
[frames] | no frames]

Source Code for Module node_manager_fkie.select_dialog

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  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 Fraunhofer 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 PySide import QtCore, QtGui 
 34   
 35  import roslib 
 36  import rospy 
 37  from parameter_dialog import ScrollArea 
38 39 -class SelectDialog(QtGui.QDialog):
40 ''' 41 This dialog creates an input mask for a string list and return selected entries. 42 ''' 43
44 - def __init__(self, input=list(), buttons=QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok, exclusive=False, parent=None):
45 ''' 46 Creates an input dialog. 47 @param input: a list with strings 48 @type input: C{list()} 49 ''' 50 QtGui.QDialog.__init__(self, parent=parent) 51 self.setObjectName(' - '.join(['SelectDialog', str(input)])) 52 53 self.verticalLayout = QtGui.QVBoxLayout(self) 54 self.verticalLayout.setObjectName("verticalLayout") 55 self.verticalLayout.setContentsMargins(1, 1, 1, 1) 56 57 # create area for the parameter 58 self.scrollArea = scrollArea = QtGui.QScrollArea(self); 59 scrollArea.setObjectName("scrollArea") 60 scrollArea.setWidgetResizable(True) 61 self.content = MainBox(self) 62 scrollArea.setWidget(self.content) 63 self.verticalLayout.addWidget(scrollArea) 64 65 # add select all option 66 if not exclusive: 67 options = QtGui.QWidget(self) 68 hLayout = QtGui.QHBoxLayout(options) 69 hLayout.setContentsMargins(1, 1, 1, 1) 70 self.select_all_checkbox = QtGui.QCheckBox('all') 71 # self.select_all_checkbox.setTristate(True) 72 self.select_all_checkbox.setCheckState(QtCore.Qt.CheckState.Checked) 73 self.select_all_checkbox.toggled.connect(self._on_select_all_checkbox_toggled) 74 hLayout.addWidget(self.select_all_checkbox) 75 # add spacer 76 spacerItem = QtGui.QSpacerItem(515, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) 77 hLayout.addItem(spacerItem) 78 self.verticalLayout.addWidget(options) 79 80 # create buttons 81 self.buttonBox = QtGui.QDialogButtonBox(self) 82 self.buttonBox.setObjectName("buttonBox") 83 self.buttonBox.setOrientation(QtCore.Qt.Horizontal) 84 self.buttonBox.setStandardButtons(buttons) 85 self.buttonBox.accepted.connect(self.accept) 86 self.buttonBox.rejected.connect(self.reject) 87 self.verticalLayout.addWidget(self.buttonBox) 88 89 # set the input fields 90 if input: 91 self.content.createFieldsFromValues(input, exclusive)
92 93 # print '=============== create', self.objectName() 94 # 95 # def __del__(self): 96 # print "************ destroy", self.objectName() 97
98 - def _on_select_all_checkbox_toggled(self, state):
99 self.content.setState(state)
100
101 - def _on_filter_changed(self):
102 self.content.filter(self.filter_field.text())
103
104 - def getSelected(self):
105 return self.content.getSelected()
106 107 @staticmethod
108 - def getValue(title, input=list(), exclusive=False, parent=None):
109 selectDia = SelectDialog(input, exclusive=exclusive, parent=parent) 110 selectDia.setWindowTitle(title) 111 selectDia.resize(480, 256) 112 if selectDia.exec_(): 113 return selectDia.getSelected() 114 return list()
115 116 117 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 118 #%%%%%%%%%%%%%%%%%% close handling %%%%%%%%%%%%%%%%%%%%% 119 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 120
121 - def accept(self):
122 self.setResult(QtGui.QDialog.Accepted) 123 self.hide()
124
125 - def reject(self):
126 self.setResult(QtGui.QDialog.Rejected) 127 self.hide()
128
129 - def hideEvent(self, event):
130 self.close()
131
132 - def closeEvent (self, event):
133 ''' 134 Test the open files for changes and save this if needed. 135 ''' 136 self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) 137 QtGui.QDialog.closeEvent(self, event)
138
139 140 -class MainBox(QtGui.QWidget):
141 ''' 142 A widget with entries. 143 '''
144 - def __init__(self, parent=None):
145 QtGui.QWidget.__init__(self, parent) 146 self.setObjectName("MainBox") 147 boxLayout = QtGui.QFormLayout() 148 boxLayout.setVerticalSpacing(0) 149 self.box_group = QtGui.QButtonGroup(self) 150 self.setLayout(boxLayout)
151
152 - def createFieldsFromValues(self, values, exclusive=False):
153 self.setUpdatesEnabled(False) 154 self.box_group.setExclusive(exclusive) 155 try: 156 if isinstance(values, list): 157 for v in values: 158 checkbox = QtGui.QCheckBox(v) 159 checkbox.setObjectName(v) 160 self.box_group.addButton(checkbox) 161 self.layout().addRow(checkbox) 162 checkbox.setCheckState(QtCore.Qt.CheckState.Checked) 163 finally: 164 self.setUpdatesEnabled(True)
165
166 - def getSelected(self):
167 result = list() 168 for i in range(self.layout().count()): 169 item = self.layout().itemAt(i).widget() 170 if isinstance(item, QtGui.QCheckBox): 171 if item.isChecked(): 172 result.append(item.text()) 173 return result
174
175 - def setState(self, state):
176 for i in range(self.layout().count()): 177 item = self.layout().itemAt(i).widget() 178 if isinstance(item, QtGui.QCheckBox): 179 item.setCheckState(QtCore.Qt.CheckState.Checked if state else QtCore.Qt.CheckState.Unchecked)
180