home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-config-printer / optionwidgets.py < prev    next >
Encoding:
Python Source  |  2009-05-05  |  8.2 KB  |  225 lines

  1. ## system-config-printer
  2.  
  3. ## Copyright (C) 2006, 2007, 2008, 2009 Red Hat, Inc.
  4. ## Copyright (C) 2006 Florian Festi <ffesti@redhat.com>
  5. ## Copyright (C) 2007, 2008, 2009 Tim Waugh <twaugh@redhat.com>
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. import gtk.glade, cups
  22. from gettext import gettext as _
  23. import ppdippstr
  24.  
  25. def OptionWidget(option, ppd, gui, tab_label=None):
  26.     """Factory function"""
  27.     ui = option.ui
  28.     if (ui == cups.PPD_UI_BOOLEAN and
  29.         len (option.choices) != 2):
  30.         # This option is advertised as a Boolean but in fact has more
  31.         # than two choices.
  32.         print "Treating Boolean option %s as PickOne" % option.keyword
  33.         ui = cups.PPD_UI_PICKONE
  34.  
  35.     if ui == cups.PPD_UI_BOOLEAN:
  36.         return OptionBool(option, ppd, gui, tab_label=tab_label)
  37.     elif ui == cups.PPD_UI_PICKONE:
  38.         return OptionPickOne(option, ppd, gui, tab_label=tab_label)
  39.     elif ui == cups.PPD_UI_PICKMANY:
  40.         return OptionPickMany(option, ppd, gui, tab_label=tab_label)
  41.  
  42. # ---------------------------------------------------------------------------
  43.  
  44. class Option:
  45.     def __init__(self, option, ppd, gui, tab_label=None):
  46.         self.option = option
  47.         self.ppd = ppd
  48.         self.gui = gui
  49.         self.enabled = True
  50.         self.tab_label = tab_label
  51.  
  52.         vbox = gtk.VBox()
  53.         
  54.         self.btnConflict = gtk.Button()
  55.         icon = gtk.image_new_from_stock(gtk.STOCK_DIALOG_WARNING,
  56.                                         gtk.ICON_SIZE_SMALL_TOOLBAR)
  57.         self.btnConflict.add(icon)
  58.         self.btnConflict.set_no_show_all(True) #avoid the button taking
  59.                                                # over control again
  60.         vbox.add(self.btnConflict)    # vbox reserves space while button
  61.         #vbox.set_size_request(32, 28) # is hidden
  62.         self.conflictIcon = vbox
  63.  
  64.         self.btnConflict.connect("clicked", self.on_btnConflict_clicked)
  65.         icon.show()
  66.  
  67.         self.constraints = [c for c in ppd.constraints
  68.                             if (c.option1 == option.keyword or
  69.                                 c.option2 == option.keyword)]
  70.         #for c in self.constraints:
  71.         #    if not c.choice1 or not c.choice2:
  72.         #        print c.option1, repr(c.choice1), c.option2, repr(c.choice2)
  73.         self.conflicts = set()
  74.         self.conflict_message = ""
  75.  
  76.     def enable(self, enabled=True):
  77.         self.selector.set_sensitive (enabled)
  78.         self.enabled = enabled
  79.  
  80.     def disable(self):
  81.         self.enable (False)
  82.  
  83.     def is_enabled(self):
  84.         return self.enabled
  85.  
  86.     def get_current_value(self):
  87.         raise NotImplemented
  88.  
  89.     def is_changed(self):
  90.         return self.get_current_value()!= self.option.defchoice
  91.     
  92.     def writeback(self):
  93.         #print repr(self.option.keyword), repr(self.get_current_value())
  94.         if self.enabled:
  95.             self.ppd.markOption(self.option.keyword, self.get_current_value())
  96.  
  97.     def checkConflicts(self, update_others=True):
  98.         value = self.get_current_value()
  99.         for constraint in self.constraints:
  100.             if constraint.option1 == self.option.keyword:
  101.                 option2 = self.gui.options.get(constraint.option2, None)
  102.                 choice1 = constraint.choice1
  103.                 choice2 = constraint.choice2
  104.             else:
  105.                 option2 = self.gui.options.get(constraint.option1, None)
  106.                 choice1 = constraint.choice2
  107.                 choice2 = constraint.choice1
  108.  
  109.             if option2 is None: continue
  110.  
  111.             if (choice1==value and
  112.                 option2.get_current_value() == choice2):
  113.                 # conflict
  114.                 self.conflicts.add(constraint)
  115.                 if update_others:
  116.                     option2.checkConflicts(update_others=False)
  117.             elif constraint in self.conflicts:
  118.                 # remove conflict
  119.                 self.conflicts.remove(constraint)
  120.                 option2.checkConflicts(update_others=False)
  121.  
  122.  
  123.         tooltip = [_("Conflicts with:")]
  124.         for c in self.conflicts:
  125.             if c.option1 == self.option.keyword:
  126.                 option = self.gui.options.get(c.option2)
  127.             else:
  128.                 option = self.gui.options.get(c.option1)
  129.  
  130.             tooltip.append(option.option.text)
  131.             
  132.         tooltip = "\n".join(tooltip)
  133.  
  134.         self.conflict_message = tooltip # XXX more verbose
  135.             
  136.         if self.conflicts:
  137.             self.gui.tooltips.set_tip(self.btnConflict, tooltip,
  138.                                       "OPTION-" + self.option.keyword)
  139.             self.btnConflict.show()
  140.         else:
  141.             self.btnConflict.hide()
  142.  
  143.         self.gui.option_changed(self)
  144.         return self.conflicts
  145.             
  146.     def on_change(self, widget):
  147.         self.checkConflicts()
  148.  
  149.     def on_btnConflict_clicked(self, button):
  150.         parent = self.btnConflict
  151.         while parent != None and not isinstance (parent, gtk.Window):
  152.             parent = parent.get_parent ()
  153.  
  154.         dialog = gtk.MessageDialog (parent,
  155.                                     gtk.DIALOG_DESTROY_WITH_PARENT |
  156.                                     gtk.DIALOG_MODAL,
  157.                                     gtk.MESSAGE_WARNING,
  158.                                     gtk.BUTTONS_CLOSE,
  159.                                     self.conflict_message)
  160.         dialog.run()
  161.         dialog.destroy()
  162.         
  163. # ---------------------------------------------------------------------------
  164.  
  165. class OptionBool(Option):
  166.  
  167.     def __init__(self, option, ppd, gui, tab_label=None):
  168.         self.selector = gtk.CheckButton(ppdippstr.ppd.get (option.text))
  169.         self.label = None
  170.         self.false = u"False" # hack to allow "None" instead of "False"
  171.         self.true = u"True"
  172.         for c in option.choices:
  173.             if c["choice"] in ("None", "False", "Off"):
  174.                 self.false = c["choice"]
  175.             if c["choice"] in ("True", "On"):
  176.                 self.true = c["choice"]
  177.         self.selector.set_active(option.defchoice == self.true)
  178.         self.selector.set_alignment(0.0, 0.5)
  179.         self.selector.connect("toggled", self.on_change)
  180.         Option.__init__(self, option, ppd, gui, tab_label=tab_label)
  181.  
  182.     def get_current_value(self):
  183.         return (self.false, self.true)[self.selector.get_active()]
  184.  
  185. # ---------------------------------------------------------------------------
  186.  
  187. class OptionPickOne(Option):
  188.     widget_name = "OptionPickOne"
  189.  
  190.     def __init__(self, option, ppd, gui, tab_label=None):
  191.         self.selector = gtk.combo_box_new_text()
  192.         #self.selector.set_alignment(0.0, 0.5)
  193.  
  194.         label = ppdippstr.ppd.get (option.text)
  195.         if not label.endswith (':'):
  196.             label += ':'
  197.         self.label = gtk.Label(label)
  198.         self.label.set_alignment(0.0, 0.5)
  199.         
  200.         selected = None
  201.         for nr, choice in enumerate(option.choices):
  202.             self.selector.append_text(ppdippstr.ppd.get (choice['text']))
  203.             if option.defchoice == choice['choice']:
  204.                 selected = nr
  205.         if selected is not None:
  206.             self.selector.set_active(selected)
  207.         else:
  208.             print option.text, "unknown value:", option.defchoice
  209.         self.selector.connect("changed", self.on_change)
  210.  
  211.         Option.__init__(self, option, ppd, gui, tab_label=tab_label)
  212.  
  213.     def get_current_value(self):
  214.         return self.option.choices[self.selector.get_active()]['choice']
  215.         
  216. # ---------------------------------------------------------------------------
  217.  
  218. class OptionPickMany(OptionPickOne):
  219.     widget_name = "OptionPickMany"
  220.  
  221.     def __init__(self, option, ppd, gui, tab_label=None):
  222.         raise NotImplemented
  223.         Option.__init__(self, option, ppd, gui, tab_label=tab_label)
  224.         
  225.