Tizen 2.1 base
[platform/upstream/hplip.git] / ui / makecopiesform.py
1 # -*- coding: utf-8 -*-
2 #
3 # (c) Copyright 2001-2009 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 # Std Lib
22 import operator
23
24 # Local
25 from base.g import *
26 from prnt import cups
27 from base import device, utils, pml
28 from copier import copier
29 from ui_utils import load_pixmap
30
31 # Qt
32 from qt import *
33 from scrollcopy import ScrollCopyView
34
35 class MakeCopiesForm(QMainWindow):
36     def __init__(self, bus='cups', device_uri=None, printer_name=None,
37                 num_copies=None, contrast=None, quality=None,
38                 reduction=None, fit_to_page=None,
39                 parent=None, name=None, modal=0, fl=0):
40
41         QMainWindow.__init__(self,parent,name,fl)
42
43         self.setIcon(load_pixmap('hp_logo', '128x128'))
44
45         self.cur_device_uri = device_uri
46         self.printer_name = printer_name
47         self.init_failed = False
48         self.num_copies = num_copies
49         self.contrast = contrast
50         self.quality = quality
51         self.reduction = reduction
52         self.fit_to_page = fit_to_page
53
54         self.setCentralWidget(QWidget(self,"qt_central_widget"))
55         self.FormLayout = QGridLayout(self.centralWidget(),1,1,11,6,"FormLayout")
56         self.resize(QSize(600,480).expandedTo(self.minimumSizeHint()))
57         self.clearWState(Qt.WState_Polished)
58         self.languageChange()
59
60         if self.cur_device_uri and self.printer_name:
61             log.error("You may not specify both a printer (-p) and a device (-d).")
62             self.FailureUI(self.__tr("<p><b>You may not specify both a printer (-p) and a device (-d)."))
63             self.cur_device_uri, self.printer_name = None, None
64             self.init_failed = True
65
66         self.cups_printers = cups.getPrinters()
67         log.debug(self.cups_printers)
68
69         if not self.cur_device_uri and not self.printer_name:
70             t = device.probeDevices(bus=bus, filter={'copy-type': (operator.gt, 0)})
71             probed_devices = []
72
73             for d in t:
74                 if d.startswith('hp:'):
75                     probed_devices.append(d)
76
77             log.debug(probed_devices)
78
79             max_deviceid_size, x, devices = 0, 0, {}
80
81             for d in probed_devices:
82                 printers = []
83                 for p in self.cups_printers:
84                     if p.device_uri == d:
85                         printers.append(p.name)
86                 devices[x] = (d, printers)
87                 x += 1
88                 max_deviceid_size = max(len(d), max_deviceid_size)
89
90             if x == 0:
91                 from nodevicesform import NoDevicesForm
92                 self.FailureUI(self.__tr("<p><b>No devices found.</b><p>Please make sure your device is properly installed and try again."))
93                 self.init_failed = True
94
95             elif x == 1:
96                 log.info(log.bold("Using device: %s" % devices[0][0]))
97                 self.cur_device_uri = devices[0][0]
98
99
100             else:
101                 from choosedevicedlg import ChooseDeviceDlg
102                 dlg = ChooseDeviceDlg(devices) #, ['hp'])
103
104                 if dlg.exec_loop() == QDialog.Accepted:
105                     self.cur_device_uri = dlg.device_uri
106                 else:
107                     self.init_failed = True
108
109
110         self.CopyView = ScrollCopyView(None, num_copies=num_copies,
111                                         contrast=contrast, quality=quality,
112                                         reduction=reduction, fit_to_page=fit_to_page,
113                                         parent=self.centralWidget(), form=self)
114
115         self.FormLayout.addWidget(self.CopyView,0,0)
116
117         self.cur_device = self.cur_device_uri
118
119         if not self.init_failed:
120             try:
121                 self.cur_device = copier.PMLCopyDevice(device_uri=self.cur_device_uri,
122                                             printer_name=self.printer_name)
123             except Error:
124                 log.error("Invalid device URI or printer name.")
125                 self.FailureUI("<b>Invalid device URI or printer name.</b><p>Please check the parameters to hp-print and try again.")
126                 self.init_failed = True
127
128             else:
129
130                 if self.cur_device.copy_type == COPY_TYPE_NONE:
131                     self.FailureUI(self.__tr("<b>Sorry, make copies functionality is not implemented for this device.</b>"))
132                     self.close()
133                     return
134
135                 self.cur_device_uri = self.cur_device.device_uri
136                 user_conf.set('last_used', 'device_uri',  self.cur_device_uri)
137
138                 log.debug(self.cur_device_uri)
139
140                 self.statusBar().message(self.cur_device.device_uri)
141
142
143         QTimer.singleShot(0, self.InitialUpdate)
144
145     def InitialUpdate(self):
146         if self.init_failed:
147             self.close()
148             return
149
150         self.CopyView.onDeviceChange(self.cur_device)
151
152     def languageChange(self):
153         self.setCaption(self.__tr("HP Device Manager - Make Copies"))
154
155     def FailureUI(self, error_text):
156         QMessageBox.critical(self,
157                              self.caption(),
158                              error_text,
159                               QMessageBox.Ok,
160                               QMessageBox.NoButton,
161                               QMessageBox.NoButton)
162
163     def WarningUI(self, msg):
164         QMessageBox.warning(self,
165                              self.caption(),
166                              msg,
167                               QMessageBox.Ok,
168                               QMessageBox.NoButton,
169                               QMessageBox.NoButton)
170
171
172     def __tr(self,s,c = None):
173         return qApp.translate("MakeCopiesForm",s,c)