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