Tizen 2.1 base
[platform/upstream/hplip.git] / ui4 / colorcaldialog.py
1 # -*- coding: utf-8 -*-
2 #
3 # (c) Copyright 2001-2008 Hewlett-Packard Development Company, L.P.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # Authors: Don Welch
20 #
21
22 # StdLib
23 import operator
24
25 # Local
26 from base.g import *
27 from base import device, utils, maint
28 from prnt import cups
29 from base.codes import *
30 from ui_utils import *
31
32 # Qt
33 from PyQt4.QtCore import *
34 from PyQt4.QtGui import *
35
36 # Ui
37 from colorcaldialog_base import Ui_Dialog
38
39
40 COLOR_CAL_TYPE_INITIAL = 1000
41 COLOR_CAL_TYPE_TEST = 1001
42
43 PAGE_START = 0
44 PAGE_LOAD_PAPER = 1
45 PAGE_DESKJET_450 = 2
46 PAGE_CRICK = 3
47 PAGE_LBOW = 4
48 PAGE_CONNERY = 5
49 PAGE_FRONT_PANEL = 6
50
51
52 BUTTON_CALIBRATE = 0
53 BUTTON_NEXT = 1
54 BUTTON_FINISH = 2
55
56
57 def true():
58     return True
59
60
61 class ColorCalDialog(QDialog, Ui_Dialog):
62     def __init__(self, parent, device_uri):
63         QDialog.__init__(self, parent)
64         self.setupUi(self)
65         self.device_uri = device_uri
66         self.color_cal_type = COLOR_CAL_TYPE_INITIAL
67         self.a, self.b = 0, 0
68         self.step = 1
69         self.abort = False
70         self.seq_index = 0
71         self.value = 0
72         self.values = []
73         self.step_max = 0
74
75         self.max_steps = {
76                     COLOR_CAL_TYPE_UNSUPPORTED : 1,
77                     COLOR_CAL_TYPE_DESKJET_450 : 2,
78                     COLOR_CAL_TYPE_MALIBU_CRICK : 0,
79                     COLOR_CAL_TYPE_STRINGRAY_LONGBOW_TORNADO : 0,
80                     COLOR_CAL_TYPE_CONNERY : 0,
81                     COLOR_CAL_TYPE_COUSTEAU : 0,
82                     COLOR_CAL_TYPE_CARRIER : 0,
83                     COLOR_CAL_TYPE_TYPHOON : 0,
84                    }
85
86         self.seq = { # (func|method, tuple of params|None)
87                     COLOR_CAL_TYPE_INITIAL: [ # (used when starting up and align-type isn't known)
88                                (self.showStartPage, None),
89                                (self.endStartPage, None), # switch to a valid align-type here
90                             ],
91
92                     COLOR_CAL_TYPE_UNSUPPORTED : [
93                                 (self.showFrontPanelPage, None),
94                                 (self.endFrontPanelPage, None),
95                                 (self.close, None),
96                                     ],
97
98                     COLOR_CAL_TYPE_DESKJET_450 : [ # 1
99                                 (self.colorCalType1PenCheck, None),
100                                 (self.showLoadPaperPage, None),
101                                 (self.endLoadPaperPage, None),
102                                 (maint.colorCalType1Phase1, (lambda: self.dev,)),
103                                 (self.setColorCalButton, (BUTTON_CALIBRATE,)),
104                                 (self.showDeskjet450Page, None),
105                                 (self.endDeskjet450Page, None),
106                                 (maint.colorCalType1Phase2, (lambda: self.dev, lambda: self.value)),
107                                 (self.close, None),
108                                     ],
109
110                     COLOR_CAL_TYPE_MALIBU_CRICK : [ # 2
111                                 (self.colorCalType2PenCheck, None),
112                                 (self.showLoadPaperPage, None),
113                                 (self.endLoadPaperPage, None),
114                                 (maint.colorCalType1Phase1, (lambda: self.dev,)),
115                                 (self.setColorCalButton, (BUTTON_CALIBRATE,)),
116                                 (self.showCrick, None),
117                                 (self.endCrick, None),
118                                 (maint.colorCalType2Phase2, (lambda: self.dev, lambda: self.value)),
119                                 (self.close, None),
120                                     ],
121
122                     COLOR_CAL_TYPE_STRINGRAY_LONGBOW_TORNADO : [ # 3
123                                 (self.colorCalType3PenCheck, None),
124                                 (self.showLoadPaperPage, None),
125                                 (self.endLoadPaperPage, None),
126                                 (maint.colorCalType3Phase1, (lambda: self.dev,)),
127                                 (self.showLBowPage, ('A', 21)),
128                                 (self.endLBowPage, ('A',)),
129                                 (self.setColorCalButton, (BUTTON_CALIBRATE,)),
130                                 (self.showLBowPage, ('B', 21)),
131                                 (self.endLBowPage, ('B',)),
132                                 (maint.colorCalType3Phase2, (lambda: self.dev, lambda: self.a,
133                                                              lambda: self.b)),
134                                 (self.close, None),
135                                     ],
136
137                     COLOR_CAL_TYPE_CONNERY : [ #4
138                                 (self.showLoadPaperPage, None),
139                                 (self.endLoadPaperPage, None),
140                                 (maint.colorCalType4Phase1, (lambda: self.dev,)),
141                                 (self.setColorCalButton, (BUTTON_CALIBRATE,)),
142                                 (self.showConneryPage, None),
143                                 (self.endConneryPage, None), # sets self.values (list)
144                                 (maint.colorCalType4Phase2, (lambda: self.dev, lambda: self.values)),
145                                 (self.showLoadPaperPage, None),
146                                 (self.endLoadPaperPage, None),
147                                 (maint.colorCalType4Phase3, (lambda: self.dev,)),
148                                 (self.close, None),
149                                     ],
150
151                     COLOR_CAL_TYPE_COUSTEAU : [ #5
152                                 (self.setColorCalButton, (BUTTON_CALIBRATE,)),
153                                 (self.showLoadPaperPage, None),
154                                 (self.endLoadPaperPage, None),
155                                 (maint.colorCalType5, (lambda: self.dev, lambda: true)),
156                                 (self.showConneryPage, None),
157                                 (self.endConneryPage, None),
158                                 (self.close, None),
159                                     ],
160
161                     COLOR_CAL_TYPE_CARRIER : [ #6
162                                 (self.setColorCalButton, (BUTTON_CALIBRATE,)),
163                                 (self.showLoadPaperPage, None),
164                                 (self.endLoadPaperPage, None),
165                                 (maint.colorCalType6, (lambda: self.dev, lambda: true)),
166                                 (self.close, None),
167                                     ],
168
169                     COLOR_CAL_TYPE_TYPHOON : [ #7
170                                 (self.setColorCalButton, (BUTTON_CALIBRATE,)),
171                                 (self.showLoadPaperPage, None),
172                                 (self.endLoadPaperPage, None),
173                                 (maint.colorCalType7, (lambda: self.dev, lambda: true)),
174                                 (self.close, None),
175                                     ],
176         }
177
178
179         self.initUi()
180
181         QTimer.singleShot(0, self.nextSequence)
182
183
184     def initUi(self):
185         # connect signals/slots
186         self.connect(self.CancelButton, SIGNAL("clicked()"), self.CancelButton_clicked)
187         self.connect(self.NextButton, SIGNAL("clicked()"), self.NextButton_clicked)
188         self.connect(self.DeviceComboBox, SIGNAL("DeviceUriComboBox_noDevices"), self.DeviceUriComboBox_noDevices)
189         self.connect(self.DeviceComboBox, SIGNAL("DeviceUriComboBox_currentChanged"), self.DeviceUriComboBox_currentChanged)
190         self.DeviceComboBox.setFilter({'color-cal-type': (operator.gt, 0)})
191
192         if self.device_uri:
193             self.DeviceComboBox.setInitialDevice(self.device_uri)
194
195         # Application icon
196         self.setWindowIcon(QIcon(load_pixmap('hp_logo', '128x128')))
197         self.updateStepText()
198
199
200
201
202     def DeviceUriComboBox_noDevices(self):
203         FailureUI(self, self.__tr("<b>No devices that support print cartridge alignment found.</b><p>Click <i>OK</i> to exit.</p>"))
204         self.close()
205
206
207     def DeviceUriComboBox_currentChanged(self, device_uri):
208         self.device_uri = device_uri
209
210
211     def CancelButton_clicked(self):
212         self.close()
213
214
215     def NextButton_clicked(self):
216         self.nextSequence()
217
218
219     def nextSequence(self):
220         while True:
221             try:
222                 seq, params = self.seq[self.color_cal_type][self.seq_index]
223             except IndexError:
224                 break
225
226             if seq is None:
227                 self.seq_index += 1
228                 continue
229
230             self.new_page = False
231
232             t = []
233             if params is not None:
234                 for p in params:
235                     try:
236                         t.append(p())
237                     except:
238                         t.append(p)
239
240             try:
241                 log.debug("%s(%s)" % (seq.func_name, ','.join([repr(x) for x in t])))
242             except AttributeError:
243                 pass
244
245             try:
246                 seq(*t)
247             except Error:
248                 CheckDeviceUI(self)
249                 break
250
251             self.seq_index += 1
252
253             if self.new_page:
254                 break
255
256             if self.abort:
257                 self.close()
258
259
260
261     def showStartPage(self):
262         self.BackButton.setEnabled(False)
263         num_devices = self.DeviceComboBox.setDevices()
264
265         if num_devices == 1:
266             self.skipPage()
267             return
268
269         self.DeviceComboBox.updateUi()
270         self.displayPage(PAGE_START)
271
272
273     def endStartPage(self):
274         self.mq = device.queryModelByURI(self.device_uri)
275         self.color_cal_type = self.mq.get('color-cal-type', COLOR_CAL_TYPE_NONE)
276         self.seq_index = -1
277
278         #self.color_cal_type = COLOR_CAL_TYPE_TEST # XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
279
280         log.debug("color-cal-type=%d" % self.color_cal_type)
281         self.step_max = self.max_steps[self.color_cal_type]
282
283         try:
284             self.dev = device.Device(self.device_uri)
285         except Error:
286             CheckDeviceUI(self)
287             return
288
289
290     def showLoadPaperPage(self):
291         self.LoadPaper.updateUi()
292         self.displayPage(PAGE_LOAD_PAPER)
293
294
295     def endLoadPaperPage(self):
296         pass
297
298
299     def showDeskjet450Page(self):
300         self.displayPage(PAGE_DESKJET_450)
301
302
303     def endDeskjet450Page(self):
304         self.value = int(unicode(self.Deskjet450ComboBox.currentText()))
305
306
307     def showCrick(self):
308         self.displayPage(PAGE_CRICK)
309
310
311     def endCrick(self):
312         self.value = self.CrickSpinBox.value()
313
314     def showLBowPage(self, line_id, count=21):
315         self.LBowComboBox.clear()
316         self.LBowIcon.setPixmap(load_pixmap('color_adj', 'other'))
317         self.LBowLabel.setText(self.__tr("Line %1:").arg(line_id))
318
319         for x in range(count):
320             self.LBowComboBox.addItem(QString("%1%2").arg(line_id).arg(x+1))
321
322         self.displayPage(PAGE_LBOW)
323
324
325     def endLBowPage(self, line_id):
326         v = int(str(self.LBowComboBox.currentText())[1:])
327
328         if line_id == 'A':
329             self.a = v
330             log.debug("A=%d" % v)
331
332         elif line_id == 'B':
333             self.b = v
334             log.debug("B=%d" % v)
335
336
337     def showConneryPage(self):
338         self.ConneryGrayPatchIcon.setPixmap(load_pixmap('type4_gray_patch', 'other'))
339         self.ConneryColorPatchIcon.setPixmap(load_pixmap('type4_color_patch', 'other'))
340
341         for x in 'ABCDEFGHIJKLMN':
342             self.ConneryGrayLetterComboBox.addItem(QString(x))
343
344         for x in range(13):
345             self.ConneryGrayNumberComboBox.addItem(QString("%1").arg(x+1))
346
347         for x in 'PQRSTUV':
348             self.ConneryColorLetterComboBox.addItem(QString(x))
349
350         for x in range(6):
351             self.ConneryColorNumberComboBox.addItem(QString("%1").arg(x+1))
352
353         self.displayPage(PAGE_CONNERY)
354
355
356     def endConneryPage(self):
357         if self.ConneryUseFactoryDefaultsCheckBox.checkState() == Qt.Checked:
358             log.debug("Using factory defaults")
359             self.values = [-1, -1, -1, -1]
360         else:
361             self.values = [
362                 (ord(str(self.ConneryGrayLetterComboBox.currentText())) - ord('A')),
363                 int(str(self.ConneryGrayNumberComboBox.currentText())),
364                 (ord(str(self.ConneryColorLetterComboBox.currentText())) - ord('P')),
365                 int(str(self.ConneryColorNumberComboBox.currentText()))
366             ]
367
368
369     def showFrontPanelPage(self):
370         self.BackButton.setEnabled(False)
371         self.setColorCalButton(BUTTON_FINISH)
372         self.displayPage(PAGE_FRONT_PANEL)
373
374
375     def endFrontPanelPage(self):
376         pass
377
378     #
379     # Color cal specific
380     #
381
382     def colorCalType1PenCheck(self):
383         if not maint.colorCalType1PenCheck(self.dev):
384             pass # TODO: Error message (photo pen must be inserted)
385
386
387     def colorCalType2PenCheck(self):
388         if not maint.colorCalType2PenCheck(self.dev):
389             pass # TODO: Error message (photo pen must be inserted)
390
391
392     def colorCalType3PenCheck(self):
393         if not maint.colorCalType3PenCheck(self.dev):
394             pass # TODO:
395
396     #
397     # Misc
398     #
399
400     def displayPage(self, page):
401         self.updateStepText(self.step)
402         self.step += 1
403         self.new_page = True
404         self.StackedWidget.setCurrentIndex(page)
405
406
407     def skipPage(self):
408         self.step += 1
409         self.new_page = False
410
411
412     def updateStepText(self, p=None):
413         if p is None or not self.step_max:
414             self.StepText.setText(QString(""))
415         else:
416             self.StepText.setText(self.__tr("Step %1 of %2").arg(p).arg(self.step_max))
417
418
419     def setColorCalButton(self, typ=BUTTON_CALIBRATE):
420         if typ == BUTTON_CALIBRATE:
421             self.NextButton.setText(self.__tr("Calibrate"))
422         elif typ == BUTTON_NEXT:
423             self.NextButton.setText(self.__tr("Next >"))
424         elif typ == BUTTON_FINISH:
425             self.NextButton.setText(self.__tr("Finish"))
426
427
428 #    def setPenConfig(self):
429 #        self.dev.pen_config = status.getPenConfiguration(dev.getStatusFromDeviceID())
430
431
432     def __tr(self,s,c = None):
433         return qApp.translate("ColorCalDialog",s,c)
434
435