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