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  import re 
 34  from python_qt_binding import QtCore, QtGui 
35 36 37 -class SelectDialog(QtGui.QDialog):
38 ''' 39 This dialog creates an input mask for a string list and return selected entries. 40 ''' 41
42 - def __init__(self, input=list(), buttons=QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok, exclusive=False, preselect_all=False, title='', description='', icon='', parent=None):
43 ''' 44 Creates an input dialog. 45 @param input: a list with strings 46 @type input: C{list()} 47 ''' 48 QtGui.QDialog.__init__(self, parent=parent) 49 self.setObjectName(' - '.join(['SelectDialog', str(input)])) 50 51 self.verticalLayout = QtGui.QVBoxLayout(self) 52 self.verticalLayout.setObjectName("verticalLayout") 53 self.verticalLayout.setContentsMargins(1, 1, 1, 1) 54 55 # add filter row 56 self.filter_frame = QtGui.QFrame(self) 57 filterLayout = QtGui.QHBoxLayout(self.filter_frame) 58 filterLayout.setContentsMargins(1, 1, 1, 1) 59 label = QtGui.QLabel("Filter:", self.filter_frame) 60 self.filter_field = QtGui.QLineEdit(self.filter_frame) 61 filterLayout.addWidget(label) 62 filterLayout.addWidget(self.filter_field) 63 self.filter_field.textChanged.connect(self._on_filter_changed) 64 self.verticalLayout.addWidget(self.filter_frame) 65 66 if description: 67 self.description_frame = QtGui.QFrame(self) 68 descriptionLayout = QtGui.QHBoxLayout(self.description_frame) 69 # descriptionLayout.setContentsMargins(1, 1, 1, 1) 70 if icon: 71 self.icon_label = QtGui.QLabel(self.description_frame) 72 self.icon_label.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed) 73 self.icon_label.setPixmap(QtGui.QPixmap(icon).scaled(30, 30, QtCore.Qt.KeepAspectRatio)) 74 descriptionLayout.addWidget(self.icon_label) 75 self.description_label = QtGui.QLabel(self.description_frame) 76 self.description_label.setWordWrap(True) 77 self.description_label.setText(description) 78 descriptionLayout.addWidget(self.description_label) 79 self.verticalLayout.addWidget(self.description_frame) 80 81 # create area for the parameter 82 self.scrollArea = scrollArea = QtGui.QScrollArea(self); 83 self.scrollArea.setFocusPolicy(QtCore.Qt.NoFocus) 84 scrollArea.setObjectName("scrollArea") 85 scrollArea.setWidgetResizable(True) 86 self.content = MainBox(self) 87 scrollArea.setWidget(self.content) 88 self.verticalLayout.addWidget(scrollArea) 89 90 # add select all option 91 if not exclusive: 92 self._ignore_next_toggle = False 93 options = QtGui.QWidget(self) 94 hLayout = QtGui.QHBoxLayout(options) 95 hLayout.setContentsMargins(1, 1, 1, 1) 96 self.select_all_checkbox = QtGui.QCheckBox('all') 97 self.select_all_checkbox.setTristate(True) 98 self.select_all_checkbox.stateChanged.connect(self._on_select_all_checkbox_stateChanged) 99 hLayout.addWidget(self.select_all_checkbox) 100 self.content.toggled.connect(self._on_main_toggle) 101 # add spacer 102 spacerItem = QtGui.QSpacerItem(515, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) 103 hLayout.addItem(spacerItem) 104 self.verticalLayout.addWidget(options) 105 106 # create buttons 107 self.buttonBox = QtGui.QDialogButtonBox(self) 108 self.buttonBox.setObjectName("buttonBox") 109 self.buttonBox.setOrientation(QtCore.Qt.Horizontal) 110 self.buttonBox.setStandardButtons(buttons) 111 self.buttonBox.accepted.connect(self.accept) 112 self.buttonBox.rejected.connect(self.reject) 113 self.verticalLayout.addWidget(self.buttonBox) 114 115 # set the input fields 116 if input: 117 self.content.createFieldsFromValues(input, exclusive) 118 if len(input) == 1 or preselect_all: 119 self.select_all_checkbox.setCheckState(QtCore.Qt.Checked) 120 121 if not input or len(input) < 11: 122 self.filter_frame.setVisible(False)
123 # print '=============== create', self.objectName() 124 # 125 # def __del__(self): 126 # print "************ destroy", self.objectName() 127
128 - def _on_main_toggle(self, state):
129 self._ignore_next_toggle = state != self.select_all_checkbox.checkState() 130 self.select_all_checkbox.setCheckState(state)
131
133 if not self._ignore_next_toggle: 134 self.content.setState(state) 135 self._ignore_next_toggle = False
136
137 - def _on_filter_changed(self):
138 self.content.filter(self.filter_field.text())
139
140 - def getSelected(self):
141 return self.content.getSelected()
142 143 @staticmethod
144 - def getValue(title, description='', input=list(), exclusive=False, preselect_all=False, icon='', parent=None):
145 selectDia = SelectDialog(input, exclusive=exclusive, preselect_all=preselect_all, description=description, icon=icon, parent=parent) 146 selectDia.setWindowTitle(title) 147 selectDia.resize(480, 256) 148 if selectDia.exec_(): 149 return selectDia.getSelected(), True 150 return list(), False
151 152 153 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 154 #%%%%%%%%%%%%%%%%%% close handling %%%%%%%%%%%%%%%%%%%%% 155 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 156
157 - def accept(self):
158 self.setResult(QtGui.QDialog.Accepted) 159 self.hide()
160
161 - def reject(self):
162 self.setResult(QtGui.QDialog.Rejected) 163 self.hide()
164
165 - def hideEvent(self, event):
166 self.close()
167
168 - def closeEvent (self, event):
169 ''' 170 Test the open files for changes and save this if needed. 171 ''' 172 self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) 173 QtGui.QDialog.closeEvent(self, event)
174
175 176 -class MainBox(QtGui.QWidget):
177 ''' 178 A widget with entries. 179 ''' 180 181 toggled = QtCore.Signal(QtCore.Qt.CheckState) 182
183 - def __init__(self, parent=None):
184 QtGui.QWidget.__init__(self, parent) 185 self.setObjectName("MainBox") 186 self.__on_intern_change = False 187 boxLayout = QtGui.QFormLayout() 188 boxLayout.setVerticalSpacing(0) 189 self.setLayout(boxLayout)
190
191 - def createFieldsFromValues(self, values, exclusive=False):
192 self.setUpdatesEnabled(False) 193 try: 194 if isinstance(values, list): 195 for v in values: 196 checkbox = QtGui.QCheckBox(v) 197 checkbox.toggled.connect(self._on_checkbox_toggled) 198 checkbox.setObjectName(v) 199 checkbox.setAutoExclusive(exclusive) 200 self.layout().addRow(checkbox) 201 finally: 202 self.setUpdatesEnabled(True)
203
204 - def _on_checkbox_toggled(self):
205 if not self.__on_intern_change: 206 l = self.getSelected() 207 if len(l) == 0: 208 self.toggled.emit(QtCore.Qt.Unchecked) 209 elif len(l) == self.layout().count(): 210 self.toggled.emit(QtCore.Qt.Checked) 211 else: 212 self.toggled.emit(QtCore.Qt.PartiallyChecked)
213
214 - def filter(self, arg):
215 ''' 216 Hide the parameter input field, which label dosn't contains the C{arg}. 217 @param arg: the filter text 218 @type art: C{str} 219 ''' 220 result = False 221 for i in range(self.layout().count()): 222 item = self.layout().itemAt(i).widget() 223 if isinstance(item, QtGui.QCheckBox): 224 new_state = (not re.search(arg, item.objectName()) is None) 225 item.setVisible(new_state) 226 if new_state: 227 self._on_checkbox_toggled()
228
229 - def getSelected(self):
230 result = list() 231 for i in range(self.layout().count()): 232 item = self.layout().itemAt(i).widget() 233 if isinstance(item, QtGui.QCheckBox): 234 if item.isChecked(): 235 result.append(item.text()) 236 return result
237
238 - def setState(self, state):
239 self.__on_intern_change = True 240 for i in range(self.layout().count()): 241 item = self.layout().itemAt(i).widget() 242 if isinstance(item, QtGui.QCheckBox): 243 if state == QtCore.Qt.Checked: 244 item.setCheckState(QtCore.Qt.Checked) 245 elif state == QtCore.Qt.Unchecked: 246 item.setCheckState(QtCore.Qt.Unchecked) 247 elif state == QtCore.Qt.PartiallyChecked and item.isVisible(): 248 item.setCheckState(QtCore.Qt.Checked) 249 self.__on_intern_change = False 250 self._on_checkbox_toggled()
251