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