1 # -*- coding: utf-8 -*-
3 # (c) Copyright 2001-2009 Hewlett-Packard Development Company, L.P.
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.
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.
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
27 from ui_utils import *
28 from base import device
31 from PyQt4.QtCore import *
32 from PyQt4.QtGui import *
35 PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY = 0
36 PRINTERNAMECOMBOBOX_TYPE_FAX_ONLY = 1
37 PRINTERNAMECOMBOBOX_TYPE_PRINTER_AND_FAX = 2
40 class PrinterNameComboBox(QWidget):
41 def __init__(self, parent):
42 QWidget.__init__(self, parent)
43 self.printer_name = ''
45 self.printer_index = {}
46 self.initial_printer = None
48 self.typ = PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY
50 self.user_settings = UserSettings()
51 self.user_settings.load()
52 self.user_settings.debug()
58 #print "PrinterNameComboBox.initUi()"
59 HBoxLayout = QHBoxLayout(self)
60 HBoxLayout.setObjectName("HBoxLayout")
62 self.NameLabel = QLabel(self)
63 self.NameLabel.setObjectName("NameLabel")
64 HBoxLayout.addWidget(self.NameLabel)
66 SpacerItem = QSpacerItem(20, 20, QSizePolicy.Minimum, QSizePolicy.Minimum)
67 HBoxLayout.addItem(SpacerItem)
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)
78 self.NameLabel.setText(self.__tr("Printer:"))
80 #self.connect(self.ComboBox, SIGNAL("currentIndexChanged(int)"),
81 # self.ComboBox_currentIndexChanged)
83 self.connect(self.ComboBox, SIGNAL("currentIndexChanged(const QString &)"),
84 self.ComboBox_currentIndexChanged)
86 def setType(self, typ):
87 if typ in (PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY,
88 PRINTERNAMECOMBOBOX_TYPE_FAX_ONLY,
89 PRINTERNAMECOMBOBOX_TYPE_PRINTER_AND_FAX):
93 def setInitialPrinter(self, printer_name):
94 self.initial_printer = printer_name
98 #print "PrinterNameComboBox.updateUi()"
99 if self.typ == PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY:
100 self.NameLabel.setText(self.__tr("Printer Name:"))
103 elif self.typ == PRINTERNAMECOMBOBOX_TYPE_FAX_ONLY:
104 self.NameLabel.setText(self.__tr("Fax Name:"))
105 be_filter = ['hpfax']
107 else: # PRINTERNAMECOMBOBOX_TYPE_PRINTER_AND_FAX
108 self.NameLabel.setText(self.__tr("Printer/Fax Name:"))
109 be_filter = ['hp', 'hpfax']
111 self.printers = device.getSupportedCUPSPrinters(be_filter)
112 self.printer_index.clear() # = {}
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
122 for i, p in enumerate(self.printers):
123 self.printer_index[p.name] = p.device_uri
124 self.ComboBox.insertItem(i, p.name)
126 if self.initial_printer is not None and p.name == self.initial_printer:
127 self.initial_printer = None
130 self.ComboBox.setCurrentIndex(-1)
133 self.updating = False
135 self.ComboBox.setCurrentIndex(k)
137 self.emit(SIGNAL("PrinterNameComboBox_noPrinters"))
140 def ComboBox_currentIndexChanged(self, t):
141 self.printer_name = unicode(t)
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()
151 self.emit(SIGNAL("PrinterNameComboBox_currentChanged"), self.device_uri, self.printer_name)
154 def __tr(self,s,c = None):
155 return qApp.translate("PrinterNameComboBox",s,c)