Debian Codegraph @

optionwidgets.py

raw
1 ## system-config-printer
2
3 ## Copyright (C) 2006, 2007, 2008, 2009, 2014 Red Hat, Inc.
4 ## Copyright (C) 2006 Florian Festi <[email protected]>
5 ## Copyright (C) 2007, 2008, 2009 Tim Waugh <[email protected]>
6
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
11
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ## GNU General Public License for more details.
16
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, write to the Free Software
19 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
21 import config
22 from gi.repository import Gtk
23 import cups
24 import gettext
25 gettext.install(domain=config.PACKAGE, localedir=config.localedir)
26 import ppdippstr
27
28 def OptionWidget(option, ppd, gui, tab_label=None):
29 """Factory function"""
30 ui = option.ui
31 if (ui == cups.PPD_UI_BOOLEAN and
32 len (option.choices) != 2):
33 # This option is advertised as a Boolean but in fact has more
34 # than two choices.
35 print("Treating Boolean option %s as PickOne" % option.keyword)
36 ui = cups.PPD_UI_PICKONE
37
38 if ui == cups.PPD_UI_BOOLEAN:
39 return OptionBool(option, ppd, gui, tab_label=tab_label)
40 elif ui == cups.PPD_UI_PICKONE:
41 return OptionPickOne(option, ppd, gui, tab_label=tab_label)
42 elif ui == cups.PPD_UI_PICKMANY:
43 return OptionPickMany(option, ppd, gui, tab_label=tab_label)
44
45 # ---------------------------------------------------------------------------
46
47 class Option:
48 def __init__(self, option, ppd, gui, tab_label=None):
49 self.option = option
50 self.ppd = ppd
51 self.gui = gui
52 self.enabled = True
53 self.tab_label = tab_label
54
55 vbox = Gtk.VBox()
56
57 self.btnConflict = Gtk.Button()
58 icon = Gtk.Image.new_from_icon_name(Gtk.STOCK_DIALOG_WARNING,
59 Gtk.IconSize.SMALL_TOOLBAR)
60 self.btnConflict.add(icon)
61 self.btnConflict.set_no_show_all(True) #avoid the button taking
62 # over control again
63 vbox.add(self.btnConflict) # vbox reserves space while button
64 #vbox.set_size_request(32, 28) # is hidden
65 self.conflictIcon = vbox
66
67 self.btnConflict.connect("clicked", self.on_btnConflict_clicked)
68 icon.show()
69
70 self.constraints = [c for c in ppd.constraints
71 if (c.option1 == option.keyword or
72 c.option2 == option.keyword)]
73 #for c in self.constraints:
74 # if not c.choice1 or not c.choice2:
75 # print c.option1, repr(c.choice1), c.option2, repr(c.choice2)
76 self.conflicts = set()
77 self.conflict_message = ""
78
79 def enable(self, enabled=True):
80 self.selector.set_sensitive (enabled)
81 self.enabled = enabled
82
83 def disable(self):
84 self.enable (False)
85
86 def is_enabled(self):
87 return self.enabled
88
89 def get_current_value(self):
90 raise NotImplemented
91
92 def is_changed(self):
93 return self.get_current_value()!= self.option.defchoice
94
95 def writeback(self):
96 #print repr(self.option.keyword), repr(self.get_current_value())
97 if self.enabled:
98 self.ppd.markOption(self.option.keyword, self.get_current_value())
99
100 def checkConflicts(self, update_others=True):
101 value = self.get_current_value()
102 for constraint in self.constraints:
103 if constraint.option1 == self.option.keyword:
104 option2 = self.gui.options.get(constraint.option2, None)
105 choice1 = constraint.choice1
106 choice2 = constraint.choice2
107 else:
108 option2 = self.gui.options.get(constraint.option1, None)
109 choice1 = constraint.choice2
110 choice2 = constraint.choice1
111
112 if option2 is None: continue
113
114 def matches (constraint_choice, value):
115 if constraint_choice != '':
116 return constraint_choice == value
117 return value not in ['None', 'False', 'Off']
118
119 if (matches (choice1, value) and
120 matches (choice2, option2.get_current_value())):
121 # conflict
122 self.conflicts.add(constraint)
123 if update_others:
124 option2.checkConflicts(update_others=False)
125 elif constraint in self.conflicts:
126 # remove conflict
127 self.conflicts.remove(constraint)
128 option2.checkConflicts(update_others=False)
129
130
131 tooltip = [_("Conflicts with:")]
132 conflicting_options = dict()
133 for c in self.conflicts:
134 if c.option1 == self.option.keyword:
135 option = self.gui.options.get(c.option2)
136 else:
137 option = self.gui.options.get(c.option1)
138
139 conflicting_options[option.option.keyword] = option
140
141 for option in conflicting_options.values ():
142 opt = option.option.text
143 val = option.get_current_value ()
144 for choice in option.option.choices:
145 if choice['choice'] == val:
146 val = ppdippstr.ppd.get (choice['text'])
147
148 tooltip.append ("%s: %s" % (opt, val))
149
150 tooltip = "\n".join(tooltip)
151
152 self.conflict_message = tooltip # XXX more verbose
153
154 if self.conflicts:
155 self.btnConflict.set_tooltip_text (tooltip)
156 self.btnConflict.show()
157 else:
158 self.btnConflict.hide()
159
160 self.gui.option_changed(self)
161 return self.conflicts
162
163 def on_change(self, widget):
164 self.checkConflicts()
165
166 def on_btnConflict_clicked(self, button):
167 parent = self.btnConflict
168 while parent is not None and not isinstance (parent, Gtk.Window):
169 parent = parent.get_parent ()
170 dialog = Gtk.MessageDialog (parent=parent,
171 modal=True, destroy_with_parent=True,
172 message_type=Gtk.MessageType.WARNING,
173 buttons=Gtk.ButtonsType.CLOSE,
174 text=self.conflict_message)
175 dialog.run()
176 dialog.destroy()
177
178 # ---------------------------------------------------------------------------
179
180 class OptionBool(Option):
181
182 def __init__(self, option, ppd, gui, tab_label=None):
183 self.selector = Gtk.CheckButton.new_with_label(
184 ppdippstr.ppd.get (option.text))
185 self.label = None
186 self.false = "False" # hack to allow "None" instead of "False"
187 self.true = "True"
188 for c in option.choices:
189 if c["choice"] in ("None", "False", "Off"):
190 self.false = c["choice"]
191 if c["choice"] in ("True", "On"):
192 self.true = c["choice"]
193 self.selector.set_active(option.defchoice == self.true)
194 self.selector.set_alignment(0.0, 0.5)
195 self.selector.connect("toggled", self.on_change)
196 Option.__init__(self, option, ppd, gui, tab_label=tab_label)
197
198 def get_current_value(self):
199 return (self.false, self.true)[self.selector.get_active()]
200
201 # ---------------------------------------------------------------------------
202
203 class OptionPickOne(Option):
204 widget_name = "OptionPickOne"
205
206 def __init__(self, option, ppd, gui, tab_label=None):
207 self.selector = Gtk.ComboBoxText()
208 #self.selector.set_alignment(0.0, 0.5)
209
210 label = ppdippstr.ppd.get (option.text)
211 if not label.endswith (':'):
212 label += ':'
213 self.label = Gtk.Label(label=label)
214 self.label.set_alignment(0.0, 0.5)
215
216 selected = None
217 for nr, choice in enumerate(option.choices):
218 self.selector.append_text(ppdippstr.ppd.get (choice['text']))
219 if option.defchoice == choice['choice']:
220 selected = nr
221 if selected is not None:
222 self.selector.set_active(selected)
223 else:
224 print(option.text, "unknown value:", option.defchoice)
225 self.selector.connect("changed", self.on_change)
226
227 Option.__init__(self, option, ppd, gui, tab_label=tab_label)
228
229 def get_current_value(self):
230 return self.option.choices[self.selector.get_active()]['choice']
231
232 # ---------------------------------------------------------------------------
233
234 class OptionPickMany(OptionPickOne):
235 widget_name = "OptionPickMany"
236
237 def __init__(self, option, ppd, gui, tab_label=None):
238 raise NotImplemented
239 Option.__init__(self, option, ppd, gui, tab_label=tab_label)
240