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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
"""
classes used in progress pages
from first run wizard
"""
try:
from collections import OrderedDict
except ImportError:
# We must be in 2.6
from leap.util.dicts import OrderedDict
import logging
from PyQt4 import QtCore
from PyQt4 import QtGui
from leap.gui.threads import FunThread
from leap.gui import mainwindow_rc
ICON_CHECKMARK = ":/images/Dialog-accept.png"
ICON_FAILED = ":/images/Dialog-error.png"
ICON_WAITING = ":/images/Emblem-question.png"
logger = logging.getLogger(__name__)
class ImgWidget(QtGui.QWidget):
# XXX move to widgets
def __init__(self, parent=None, img=None):
super(ImgWidget, self).__init__(parent)
self.pic = QtGui.QPixmap(img)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(0, 0, self.pic)
class ProgressStep(object):
"""
Data model for sequential steps
to be used in a progress page in
connection wizard
"""
NAME = 0
DONE = 1
def __init__(self, stepname, done, index=None):
"""
@param step: the name of the step
@type step: str
@param done: whether is completed or not
@type done: bool
"""
self.index = int(index) if index else 0
self.name = unicode(stepname)
self.done = bool(done)
@classmethod
def columns(self):
return ('name', 'done')
class ProgressStepContainer(object):
"""
a container for ProgressSteps objects
access data in the internal dict
"""
def __init__(self):
self.dirty = False
self.steps = {}
def step(self, identity):
return self.step.get(identity)
def addStep(self, step):
self.steps[step.index] = step
def removeStep(self, step):
del self.steps[step.index]
del step
self.dirty = True
def removeAllSteps(self):
for item in iter(self):
self.removeStep(item)
@property
def columns(self):
return ProgressStep.columns()
def __len__(self):
return len(self.steps)
def __iter__(self):
for step in self.steps.values():
yield step
class StepsTableWidget(QtGui.QTableWidget):
"""
initializes a TableWidget
suitable for our display purposes, like removing
header info and grid display
"""
def __init__(self, parent=None):
super(StepsTableWidget, self).__init__(parent)
# remove headers and all edit/select behavior
self.horizontalHeader().hide()
self.verticalHeader().hide()
self.setEditTriggers(
QtGui.QAbstractItemView.NoEditTriggers)
self.setSelectionMode(
QtGui.QAbstractItemView.NoSelection)
width = self.width()
# WTF? Here init width is 100...
# but on populating is 456... :(
# XXX do we need this initial?
logger.debug('init table. width=%s' % width)
self.horizontalHeader().resizeSection(0, width * 0.7)
# this disables the table grid.
# we should add alignment to the ImgWidget (it's top-left now)
self.setShowGrid(False)
self.setFocusPolicy(QtCore.Qt.NoFocus)
#self.setStyleSheet("QTableView{outline: 0;}")
# XXX change image for done to rc
# Note about the "done" status painting:
#
# XXX currently we are setting the CellWidget
# for the whole table on a per-row basis
# (on add_status_line method on ValidationPage).
# However, a more generic solution might be
# to implement a custom Delegate that overwrites
# the paint method (so it paints a checked tickmark if
# done is True and some other thing if checking or false).
# What we have now is quick and works because
# I'm supposing that on first fail we will
# go back to previous wizard page to signal the failure.
# A more generic solution could be used for
# some failing tests if they are not critical.
class WithStepsMixIn(object):
# worker threads for checks
def setupStepsProcessingQueue(self):
self.steps_queue = Queue.Queue()
self.stepscheck_timer = QtCore.QTimer()
self.stepscheck_timer.timeout.connect(self.processStepsQueue)
self.stepscheck_timer.start(100)
# we need to keep a reference to child threads
self.threads = []
def do_checks(self):
# yo dawg, I heard you like checks
# so I put a __do_checks in your do_checks
# for calling others' _do_checks
def __do_checks(fun=None, queue=None):
for checkcase in fun():
checkmsg, checkfun = checkcase
queue.put(checkmsg)
if checkfun() is False:
queue.put("failed")
break
t = FunThread(fun=partial(
__do_checks,
fun=self._do_checks,
queue=self.steps_queue))
t.finished.connect(self.on_checks_validation_ready)
t.begin()
self.threads.append(t)
def fail(self, err=None):
"""
return failed state
and send error notification as
a nice side effect
"""
wizard = self.wizard()
senderr = lambda err: wizard.set_validation_error(
self.current_page, err)
self.set_undone()
if err:
senderr(err)
return False
@QtCore.pyqtSlot()
def launch_checks(self):
self.do_checks()
# slot
#@QtCore.pyqtSlot(str, int)
def onStepStatusChanged(self, status, progress=None):
if status not in ("head_sentinel", "end_sentinel"):
self.add_status_line(status)
if status in ("end_sentinel"):
self.checks_finished = True
self.set_checked_icon()
if progress and hasattr(self, 'progress'):
self.progress.setValue(progress)
self.progress.update()
def processStepsQueue(self):
"""
consume steps queue
and pass messages
to the ui updater functions
"""
while self.steps_queue.qsize():
try:
status = self.steps_queue.get(0)
if status == "failed":
self.set_failed_icon()
else:
self.onStepStatusChanged(*status)
except Queue.Empty:
pass
def setupSteps(self):
self.steps = ProgressStepContainer()
# steps table widget
self.stepsTableWidget = StepsTableWidget(self)
zeros = (0, 0, 0, 0)
self.stepsTableWidget.setContentsMargins(*zeros)
self.errors = OrderedDict()
def set_error(self, name, error):
self.errors[name] = error
def pop_first_error(self):
return list(reversed(self.errors.items())).pop()
def clean_errors(self):
self.errors = OrderedDict()
def clean_wizard_errors(self, pagename=None):
if pagename is None:
pagename = getattr(self, 'prev_page', None)
if pagename is None:
return
logger.debug('cleaning wizard errors for %s' % pagename)
self.wizard().set_validation_error(pagename, None)
def populateStepsTable(self):
# from examples,
# but I guess it's not needed to re-populate
# the whole table.
table = self.stepsTableWidget
table.setRowCount(len(self.steps))
columns = self.steps.columns
table.setColumnCount(len(columns))
for row, step in enumerate(self.steps):
item = QtGui.QTableWidgetItem(step.name)
item.setData(QtCore.Qt.UserRole,
long(id(step)))
table.setItem(row, columns.index('name'), item)
table.setItem(row, columns.index('done'),
QtGui.QTableWidgetItem(step.done))
self.resizeTable()
self.update()
def clearTable(self):
# ??? -- not sure what's the difference
#self.stepsTableWidget.clear()
self.stepsTableWidget.clearContents()
def resizeTable(self):
# resize first column to ~80%
table = self.stepsTableWidget
FIRST_COLUMN_PERCENT = 0.70
width = table.width()
logger.debug('populate table. width=%s' % width)
table.horizontalHeader().resizeSection(0, width * FIRST_COLUMN_PERCENT)
def set_item_icon(self, img=ICON_CHECKMARK, current=True):
"""
mark the last item
as done
"""
# setting cell widget.
# see note on StepsTableWidget about plans to
# change this for a better solution.
index = len(self.steps)
table = self.stepsTableWidget
_index = index - 1 if current else index - 2
table.setCellWidget(
_index,
ProgressStep.DONE,
ImgWidget(img=img))
table.update()
def set_failed_icon(self):
self.set_item_icon(img=ICON_FAILED, current=True)
def set_checking_icon(self):
self.set_item_icon(img=ICON_WAITING, current=True)
def set_checked_icon(self, current=True):
self.set_item_icon(current=current)
def add_status_line(self, message):
"""
adds a new status line
and mark the next-to-last item
as done
"""
index = len(self.steps)
step = ProgressStep(message, False, index=index)
self.steps.addStep(step)
self.populateStepsTable()
self.set_checking_icon()
self.set_checked_icon(current=False)
# Sets/unsets done flag
# for isComplete checks
def set_done(self):
self.done = True
self.completeChanged.emit()
def set_undone(self):
self.done = False
self.completeChanged.emit()
def is_done(self):
return self.done
def go_back(self):
self.wizard().back()
def go_next(self):
self.wizard().next()
"""
We will use one base class for the intermediate pages
and another one for the in-page validations, both sharing the creation
of the tablewidgets.
The logic of this split comes from where I was trying to solve
the ui update using signals, but now that it's working well with
queues I could join them again.
"""
import Queue
from functools import partial
class InlineValidationPage(QtGui.QWizardPage, WithStepsMixIn):
def __init__(self, parent=None):
super(InlineValidationPage, self).__init__(parent)
self.setupStepsProcessingQueue()
self.done = False
# slot
@QtCore.pyqtSlot()
def showStepsFrame(self):
self.valFrame.show()
self.update()
# progress frame
def setupValidationFrame(self):
qframe = QtGui.QFrame
valFrame = qframe()
valFrame.setFrameStyle(qframe.NoFrame)
valframeLayout = QtGui.QVBoxLayout()
zeros = (0, 0, 0, 0)
valframeLayout.setContentsMargins(*zeros)
valframeLayout.addWidget(self.stepsTableWidget)
valFrame.setLayout(valframeLayout)
self.valFrame = valFrame
class ValidationPage(QtGui.QWizardPage, WithStepsMixIn):
"""
class to be used as an intermediate
between two pages in a wizard.
shows feedback to the user and goes back if errors,
goes forward if ok.
initializePage triggers a one shot timer
that calls do_checks.
Derived classes should implement
_do_checks and
_do_validation
"""
# signals
stepChanged = QtCore.pyqtSignal([str, int])
def __init__(self, parent=None):
super(ValidationPage, self).__init__(parent)
self.setupSteps()
#self.connect_step_status()
layout = QtGui.QVBoxLayout()
self.progress = QtGui.QProgressBar(self)
layout.addWidget(self.progress)
layout.addWidget(self.stepsTableWidget)
self.setLayout(layout)
self.layout = layout
self.timer = QtCore.QTimer()
self.done = False
self.setupStepsProcessingQueue()
def isComplete(self):
return self.is_done()
########################
def show_progress(self):
self.progress.show()
self.stepsTableWidget.show()
def hide_progress(self):
self.progress.hide()
self.stepsTableWidget.hide()
# pagewizard methods.
# if overriden, child classes should call super.
def initializePage(self):
self.clean_errors()
self.clean_wizard_errors()
self.steps.removeAllSteps()
self.clearTable()
self.resizeTable()
self.timer.singleShot(0, self.do_checks)
|