Revert manifest to default one
[external/hplip.git] / align.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 # Author: Don Welch, Naga Samrat Chowdary Narla,
21 #
22
23 __version__ = '5.0'
24 __title__ = 'Printer Cartridge Alignment Utility'
25 __mod__ = 'hp-align'
26 __doc__ = "Cartridge alignment utility for HPLIP supported inkjet printers. (Note: Not all printers require the use of this utility)."
27
28 # Std Lib
29 import sys
30 import re
31 import getopt
32 import operator
33 import os
34
35 # Local
36 from base.g import *
37 from base import device, status, utils, maint, tui, module
38 from prnt import cups
39
40
41 def enterAlignmentNumber(letter, hortvert, colors, line_count, maximum):
42     ok, value = tui.enter_range("From the printed Alignment page, Enter the best aligned value for line %s (1-%d): " %
43                         (letter, maximum),
44                         1,
45                         maximum)
46     if not ok:
47         sys.exit(0)
48
49     return ok, value
50
51
52 def enterPaperEdge(maximum):
53     ok, value = tui.enter_range("Enter numbered arrow that is best aligned with the paper edge (1-%d): "
54                         % maximum,
55                         1,
56                         maximum)
57     if not ok:
58         sys.exit(0)
59
60     return ok, value
61
62
63 def colorAdj(line, maximum):
64     ok, value = tui.enter_range("Enter the numbered box on line %s that is best color matched to the background color (1-%d): " %
65                         (line, maximum),
66                         1,
67                         maximum)
68     if not ok:
69         sys.exit(0)
70
71     return ok, value
72
73
74 def bothPensRequired():
75     log.error("Cannot perform alignment with 0 or 1 cartridges installed.\nPlease install both cartridges and try again.")
76
77
78 def invalidPen():
79     log.error("Invalid cartridge(s) installed.\nPlease install valid cartridges and try again.")
80
81
82 def invalidPen2():
83     log.error("Invalid cartridge(s) installed. Cannot align with only the photo cartridge installed.\nPlease install other cartridges and try again.")
84
85
86 def aioUI1():
87     log.info("To perform alignment, you will need the alignment page that is automatically\nprinted after you install a print cartridge.")
88     log.info("\np\t\tPrint the alignment page and continue.")
89     log.info("n\t\tDo Not print the alignment page (you already have one) and continue.")
90     log.info("q\t\tQuit.\n")
91
92     ok, choice = tui.enter_choice("Choice (p=print page*, n=do not print page, q=quit) ? ", ['p', 'n', 'q'], 'p')
93
94     if choice == 'q':
95         sys.exit(0)
96
97     return choice == 'y'
98
99
100 def type10and11and14Align(pattern, align_type):
101     controls = maint.align10and11and14Controls(pattern, align_type)
102     values = []
103     s_controls = controls.keys()
104     s_controls.sort()
105
106     for line in s_controls:
107         if not controls[line][0]:
108             values.append(0)
109         else:
110             ok, value = tui.enter_range("Enter the numbered box on line %s where the inner lines best line up with the outer lines (1-%d): "
111                 % (line, controls[line][1]),  1, controls[line][1])
112             values.append(value)
113
114             if not ok:
115                 sys.exit(0)
116
117     return values
118
119
120 def aioUI2():
121     log.info("")
122     log.info(log.bold("Follow these steps to complete the alignment:"))
123     log.info("1. Place the alignment page, with the printed side facing down, ")
124     log.info("   in the scanner.")
125     log.info("2. Press the Enter or Scan button on the printer.")
126     log.info('3. "Alignment Complete" will be displayed when the process is finished (on some models).')
127
128
129
130
131 try:
132     mod = module.Module(__mod__, __title__, __version__, __doc__, None,
133                         (INTERACTIVE_MODE, GUI_MODE), (UI_TOOLKIT_QT4,))
134
135     mod.setUsage(module.USAGE_FLAG_DEVICE_ARGS,
136                  see_also_list=['hp-clean', 'hp-colorcal', 'hp-linefeedcal',
137                                 'hp-pqdiag'])
138
139     opts, device_uri, printer_name, mode, ui_toolkit, lang = \
140         mod.parseStdOpts()
141
142     device_uri = mod.getDeviceUri(device_uri, printer_name,
143          filter={'align-type': (operator.ne, ALIGN_TYPE_NONE)})
144
145     if mode == GUI_MODE:
146         if not utils.canEnterGUIMode4():
147             log.error("%s -u/--gui requires Qt4 GUI support. Entering interactive mode." % __mod__)
148             mode = INTERACTIVE_MODE
149
150     if mode == INTERACTIVE_MODE:
151         try:
152             d = device.Device(device_uri, printer_name)
153         except Error, e:
154             log.error("Unable to open device: %s" % e.msg)
155             sys.exit(0)
156
157         try:
158             try:
159                 d.open()
160             except Error:
161                 log.error("Device is busy or in an error state. Please check device and try again.")
162                 sys.exit(1)
163
164             if d.isIdleAndNoError():
165                 align_type = d.mq.get('align-type', ALIGN_TYPE_NONE)
166                 log.debug("Alignment type=%d" % align_type)
167                 d.close()
168
169                 if align_type == ALIGN_TYPE_UNSUPPORTED:
170                     log.error("Alignment through HPLIP not supported for this printer. Please use the printer's front panel to perform cartrdige alignment.")
171
172                 elif align_type == ALIGN_TYPE_AUTO:
173                     maint.AlignType1PML(d, tui.load_paper_prompt)
174
175                 elif align_type == ALIGN_TYPE_AIO:
176                     maint.AlignType13(d, tui.load_paper_prompt, tui.load_scanner_for_align_prompt)
177
178                 elif align_type == ALIGN_TYPE_8XX:
179                     maint.AlignType2(d, tui.load_paper_prompt, enterAlignmentNumber,
180                                       bothPensRequired)
181
182                 elif align_type in (ALIGN_TYPE_9XX,ALIGN_TYPE_9XX_NO_EDGE_ALIGN):
183                     maint.AlignType3(d, tui.load_paper_prompt, enterAlignmentNumber,
184                                       enterPaperEdge, update_spinner)
185
186                 elif align_type == ALIGN_TYPE_LIDIL_AIO:
187                     maint.AlignType6(d, aioUI1, aioUI2, tui.load_paper_prompt)
188
189                 elif align_type == ALIGN_TYPE_DESKJET_450:
190                     maint.AlignType8(d, tui.load_paper_prompt, enterAlignmentNumber)
191
192                 elif align_type in (ALIGN_TYPE_LIDIL_0_3_8, ALIGN_TYPE_LIDIL_0_4_3, ALIGN_TYPE_LIDIL_VIP):
193
194                     maint.AlignxBow(d, align_type, tui.load_paper_prompt, enterAlignmentNumber, enterPaperEdge,
195                                      invalidPen, colorAdj)
196
197                 elif align_type  == ALIGN_TYPE_LBOW:
198                     maint.AlignType10(d, tui.load_paper_prompt, type10and11and14Align)
199
200                 elif align_type == ALIGN_TYPE_LIDIL_0_5_4:
201                     maint.AlignType11(d, tui.load_paper_prompt, type10and11and14Align, invalidPen2)
202
203                 elif align_type == ALIGN_TYPE_OJ_PRO:
204                     maint.AlignType12(d, tui.load_paper_prompt)
205
206                 elif align_type == ALIGN_TYPE_LIDIL_DJ_D1600:
207                     maint.AlignType14(d, tui.load_paper_prompt, type10and11and14Align, invalidPen2)
208                 
209                 elif align_type == ALIGN_TYPE_LEDM:
210                     maint.AlignType15(d, tui.load_paper_prompt, aioUI2)
211
212                 elif align_type == ALIGN_TYPE_LEDM_MANUAL:
213                     maint.AlignType16(d, tui.load_paper_prompt, enterAlignmentNumber)
214
215                 else:
216                     log.error("Invalid alignment type.")
217
218             else:
219                 log.error("Device is busy or in an error state. Please check device and try again.")
220
221         finally:
222             d.close()
223
224     else: # GUI_MODE (qt4)
225         try:
226             from PyQt4.QtGui import QApplication
227             from ui4.aligndialog import AlignDialog
228         except ImportError:
229             log.error("Unable to load Qt4 support. Is it installed?")
230             sys.exit(1)
231
232
233         #try:
234         if 1:
235             app = QApplication(sys.argv)
236
237             dlg = AlignDialog(None, device_uri)
238             dlg.show()
239             try:
240                 log.debug("Starting GUI loop...")
241                 app.exec_()
242             except KeyboardInterrupt:
243                 sys.exit(0)
244
245         #finally:
246         if 1:
247             sys.exit(0)
248
249 except KeyboardInterrupt:
250     log.error("User exit")
251
252
253 log.info("")
254 log.info('Done.')