1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 import re
34 from python_qt_binding import QtCore, QtGui
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
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
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
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
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
102 spacerItem = QtGui.QSpacerItem(515, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
103 hLayout.addItem(spacerItem)
104 self.verticalLayout.addWidget(options)
105
106
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
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
124
125
126
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
138 self.content.filter(self.filter_field.text())
139
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
155
156
158 self.setResult(QtGui.QDialog.Accepted)
159 self.hide()
160
162 self.setResult(QtGui.QDialog.Rejected)
163 self.hide()
164
167
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
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