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