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