Tizen 2.1 base
[platform/upstream/hplip.git] / ui4 / printernamecombobox.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 # Author: Don Welch
20 #
21
22 # Std Lib
23 #import sys
24
25 # Local
26 from base.g import *
27 from ui_utils import *
28 from base import device
29
30 # Qt
31 from PyQt4.QtCore import *
32 from PyQt4.QtGui import *
33
34
35 PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY = 0
36 PRINTERNAMECOMBOBOX_TYPE_FAX_ONLY = 1
37 PRINTERNAMECOMBOBOX_TYPE_PRINTER_AND_FAX = 2
38
39
40 class PrinterNameComboBox(QWidget):
41     def __init__(self, parent):
42         QWidget.__init__(self, parent)
43         self.printer_name = ''
44         self.device_uri = ''
45         self.printer_index = {}
46         self.initial_printer = None
47         self.updating = False
48         self.typ = PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY
49
50         self.user_settings = UserSettings()
51         self.user_settings.load()
52         self.user_settings.debug()
53
54         self.initUi()
55
56
57     def initUi(self):
58         #print "PrinterNameComboBox.initUi()"
59         HBoxLayout = QHBoxLayout(self)
60         HBoxLayout.setObjectName("HBoxLayout")
61
62         self.NameLabel = QLabel(self)
63         self.NameLabel.setObjectName("NameLabel")
64         HBoxLayout.addWidget(self.NameLabel)
65
66         SpacerItem = QSpacerItem(20, 20, QSizePolicy.Minimum, QSizePolicy.Minimum)
67         HBoxLayout.addItem(SpacerItem)
68
69         self.ComboBox = QComboBox(self)
70         sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
71         sizePolicy.setHorizontalStretch(0)
72         sizePolicy.setVerticalStretch(0)
73         sizePolicy.setHeightForWidth(self.ComboBox.sizePolicy().hasHeightForWidth())
74         self.ComboBox.setSizePolicy(sizePolicy)
75         self.ComboBox.setObjectName("ComboBox")
76         HBoxLayout.addWidget(self.ComboBox)
77
78         self.NameLabel.setText(self.__tr("Printer:"))
79
80         #self.connect(self.ComboBox, SIGNAL("currentIndexChanged(int)"),
81         #    self.ComboBox_currentIndexChanged)
82
83         self.connect(self.ComboBox, SIGNAL("currentIndexChanged(const QString &)"),
84             self.ComboBox_currentIndexChanged)
85
86     def setType(self, typ):
87         if typ in (PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY,
88                    PRINTERNAMECOMBOBOX_TYPE_FAX_ONLY,
89                    PRINTERNAMECOMBOBOX_TYPE_PRINTER_AND_FAX):
90             self.typ = typ
91
92
93     def setInitialPrinter(self, printer_name):
94         self.initial_printer = printer_name
95
96
97     def updateUi(self):
98         #print "PrinterNameComboBox.updateUi()"
99         if self.typ == PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY:
100             self.NameLabel.setText(self.__tr("Printer Name:"))
101             be_filter = ['hp']
102
103         elif self.typ == PRINTERNAMECOMBOBOX_TYPE_FAX_ONLY:
104             self.NameLabel.setText(self.__tr("Fax Name:"))
105             be_filter = ['hpfax']
106
107         else: # PRINTERNAMECOMBOBOX_TYPE_PRINTER_AND_FAX
108             self.NameLabel.setText(self.__tr("Printer/Fax Name:"))
109             be_filter = ['hp', 'hpfax']
110
111         self.printers = device.getSupportedCUPSPrinters(be_filter)
112         self.printer_index.clear() # = {}
113
114         if self.printers:
115             if self.initial_printer is None:
116                 #user_conf.get('last_used', 'printer_name')
117                 self.initial_printer = self.user_settings.last_used_printer
118
119             self.updating = True
120             try:
121                 k = 0
122                 for i, p in enumerate(self.printers):
123                     self.printer_index[p.name] = p.device_uri
124                     self.ComboBox.insertItem(i, p.name)
125
126                     if self.initial_printer is not None and p.name == self.initial_printer:
127                         self.initial_printer = None
128                         k = i
129
130                 self.ComboBox.setCurrentIndex(-1)
131
132             finally:
133                 self.updating = False
134
135             self.ComboBox.setCurrentIndex(k)
136         else:
137             self.emit(SIGNAL("PrinterNameComboBox_noPrinters"))
138
139
140     def ComboBox_currentIndexChanged(self, t):
141         self.printer_name = unicode(t)
142
143         if self.updating:
144             return
145
146         self.device_uri = self.printer_index[self.printer_name]
147         #user_conf.set('last_used', 'printer_name', self.printer_name)
148         self.user_settings.last_used_printer = self.printer_name
149         self.user_settings.save()
150
151         self.emit(SIGNAL("PrinterNameComboBox_currentChanged"), self.device_uri, self.printer_name)
152
153
154     def __tr(self,s,c = None):
155         return qApp.translate("PrinterNameComboBox",s,c)