summaryrefslogtreecommitdiff
path: root/ui4/printernamecombobox.py
blob: b3dc078741cb5d2258b5672070ea62f00df92107 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-
#
# (c) Copyright 2001-2015 HP Development Company, L.P.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Author: Don Welch
#

# Std Lib
#import sys

# Local
from base.g import *
from .ui_utils import *
from base import device
from base.sixext import to_unicode

# Qt
from PyQt4.QtCore import *
from PyQt4.QtGui import *


PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY = 0
PRINTERNAMECOMBOBOX_TYPE_FAX_ONLY = 1
PRINTERNAMECOMBOBOX_TYPE_PRINTER_AND_FAX = 2


class PrinterNameComboBox(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self, parent)
        self.printer_name = ''
        self.device_uri = ''
        self.printer_index = {}
        self.initial_printer = None
        self.updating = False
        self.typ = PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY

        self.user_settings = UserSettings()
        self.user_settings.load()
        self.user_settings.debug()

        self.initUi()


    def initUi(self):
        #print "PrinterNameComboBox.initUi()"
        HBoxLayout = QHBoxLayout(self)
        HBoxLayout.setObjectName("HBoxLayout")

        self.NameLabel = QLabel(self)
        self.NameLabel.setObjectName("NameLabel")
        HBoxLayout.addWidget(self.NameLabel)

        SpacerItem = QSpacerItem(20, 20, QSizePolicy.Minimum, QSizePolicy.Minimum)
        HBoxLayout.addItem(SpacerItem)

        self.ComboBox = QComboBox(self)
        sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.ComboBox.sizePolicy().hasHeightForWidth())
        self.ComboBox.setSizePolicy(sizePolicy)
        self.ComboBox.setObjectName("ComboBox")
        HBoxLayout.addWidget(self.ComboBox)

        self.NameLabel.setText(self.__tr("Printer:"))

        #self.connect(self.ComboBox, SIGNAL("currentIndexChanged(int)"),
        #    self.ComboBox_currentIndexChanged)

        self.connect(self.ComboBox, SIGNAL("currentIndexChanged(const QString &)"),
            self.ComboBox_currentIndexChanged)

    def setType(self, typ):
        if typ in (PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY,
                   PRINTERNAMECOMBOBOX_TYPE_FAX_ONLY,
                   PRINTERNAMECOMBOBOX_TYPE_PRINTER_AND_FAX):
            self.typ = typ


    def setInitialPrinter(self, printer_name):
        self.initial_printer = printer_name


    def updateUi(self):
        #print "PrinterNameComboBox.updateUi()"
        if self.typ == PRINTERNAMECOMBOBOX_TYPE_PRINTER_ONLY:
            self.NameLabel.setText(self.__tr("Printer Name:"))
            be_filter = ['hp']

        elif self.typ == PRINTERNAMECOMBOBOX_TYPE_FAX_ONLY:
            self.NameLabel.setText(self.__tr("Fax Name:"))
            be_filter = ['hpfax']

        else: # PRINTERNAMECOMBOBOX_TYPE_PRINTER_AND_FAX
            self.NameLabel.setText(self.__tr("Printer/Fax Name:"))
            be_filter = ['hp', 'hpfax']

        self.printers = device.getSupportedCUPSPrinters(be_filter)
        self.printer_index.clear() # = {}

        if self.printers:
            if self.initial_printer is None:
                #user_conf.get('last_used', 'printer_name')
                self.initial_printer = self.user_settings.last_used_printer

            self.updating = True
            try:
                k = 0
                for i, p in enumerate(self.printers):
                    self.printer_index[p.name] = p.device_uri
                    self.ComboBox.insertItem(i, p.name)

                    if self.initial_printer is not None and to_unicode(p.name).lower() == to_unicode(self.initial_printer).lower():
                        self.initial_printer = None
                        k = i

                self.ComboBox.setCurrentIndex(-1)

            finally:
                self.updating = False

            self.ComboBox.setCurrentIndex(k)
        else:
            self.emit(SIGNAL("PrinterNameComboBox_noPrinters"))


    def ComboBox_currentIndexChanged(self, t):
        self.printer_name = to_unicode(t)

        if self.updating:
            return

        self.device_uri = self.printer_index[self.printer_name]
        #user_conf.set('last_used', 'printer_name', self.printer_name)
        self.user_settings.last_used_printer = self.printer_name
        self.user_settings.save()

        self.emit(SIGNAL("PrinterNameComboBox_currentChanged"), self.device_uri, self.printer_name)


    def __tr(self,s,c = None):
        return qApp.translate("PrinterNameComboBox",s,c)