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