Package conduit :: Package gtkui :: Module SimpleConfigurator
[hide private]

Source Code for Module conduit.gtkui.SimpleConfigurator

  1  import os.path 
  2  import gtk, gtk.glade 
  3  import logging 
  4  log = logging.getLogger("gtkui.Config") 
  5   
  6   
  7  import conduit 
  8   
9 -class SimpleConfigurator:
10 """ 11 Provides a simple modal configuration dialog for dataproviders. 12 13 Simply provide a list of dictionarys in the following format:: 14 15 maps = [ 16 { 17 "Name" : "Setting Name", 18 "Widget" : gtk.TextView, 19 "Callback" : function, 20 "InitialValue" : value 21 } 22 ] 23 """ 24 25 CONFIG_WINDOW_TITLE_TEXT = "Configure " 26
27 - def __init__(self, window, dp_name, config_mappings = []):
28 """ 29 @param window: Parent window (this dialog is modal) 30 @type window: C{gtk.Window} 31 @param dp_name: The dataprovider name to display in the dialog title 32 @type dp_name: C{string} 33 @param config_mappings: The list of dicts explained earlier 34 @type config_mappings: C{[{}]} 35 """ 36 self.mappings = config_mappings 37 #need to store ref to widget instances 38 self.widgetInstances = [] 39 self.dialogParent = window 40 #the child widget to contain the custom settings 41 self.customSettings = gtk.VBox(False, 5) 42 43 #The dialog is loaded from a glade file 44 gladeFile = os.path.join(conduit.SHARED_DATA_DIR, "conduit.glade") 45 widgets = gtk.glade.XML(gladeFile, "DataProviderConfigDialog") 46 callbacks = { 47 "on_okbutton_clicked" : self.on_ok_clicked, 48 "on_cancelbutton_clicked" : self.on_cancel_clicked, 49 "on_helpbutton_clicked" : self.on_help_clicked, 50 "on_dialog_close" : self.on_dialog_close 51 } 52 widgets.signal_autoconnect(callbacks) 53 self.dialog = widgets.get_widget("DataProviderConfigDialog") 54 self.dialog.set_transient_for(self.dialogParent) 55 self.dialog.set_title(SimpleConfigurator.CONFIG_WINDOW_TITLE_TEXT + dp_name) 56 57 #The contents of the dialog are built from the config mappings list 58 self.build_child() 59 vbox = widgets.get_widget("configVBox") 60 vbox.pack_start(self.customSettings) 61 self.customSettings.show_all()
62
63 - def on_ok_clicked(self, widget):
64 """ 65 on_ok_clicked 66 """ 67 log.debug("OK Clicked") 68 for w in self.widgetInstances: 69 #FIXME: This seems hackish 70 if isinstance(w["Widget"], gtk.Entry): 71 w["Callback"](w["Widget"].get_text()) 72 elif isinstance(w["Widget"], gtk.CheckButton): 73 w["Callback"](w["Widget"].get_active()) 74 else: 75 log.warn("Dont know how to retrieve value from a %s" % w["Widget"]) 76 77 self.dialog.destroy()
78
79 - def on_cancel_clicked(self, widget):
80 """ 81 on_cancel_clicked 82 """ 83 log.debug("Cancel Clicked") 84 self.dialog.destroy()
85
86 - def on_help_clicked(self, widget):
87 """ 88 on_help_clicked 89 """ 90 log.debug("Help Clicked")
91
92 - def on_dialog_close(self, widget):
93 """ 94 on_dialog_close 95 """ 96 log.debug("Dialog Closed") 97 self.dialog.destroy()
98
99 - def run(self):
100 """ 101 run 102 """ 103 resp = self.dialog.run()
104
105 - def build_child(self):
106 """ 107 For each item in the mappings list create the appropriate widget 108 """ 109 #For each item in the mappings list create the appropriate widget 110 for l in self.mappings: 111 #New instance of the widget 112 widget = l["Widget"]() 113 #all get packed into an HBox 114 hbox = gtk.HBox(False, 5) 115 116 #FIXME: I am ashamed about this ugly hackery and dupe code.... 117 if isinstance(widget, gtk.Entry): 118 #gtkEntry has its label beside it 119 label = gtk.Label(l["Name"]) 120 hbox.pack_start(label) 121 widget.set_text(str(l["InitialValue"])) 122 elif isinstance(widget, gtk.CheckButton): 123 #gtk.CheckButton has its label built in 124 widget = l["Widget"](l["Name"]) 125 widget.set_active(bool(l["InitialValue"])) 126 #FIXME: There must be a better way to do this but we need some way 127 #to identify the widget *instance* when we save the values from it 128 self.widgetInstances.append({ 129 "Widget" : widget, 130 "Callback" : l["Callback"] 131 }) 132 #pack them all together 133 hbox.pack_start(widget) 134 self.customSettings.pack_start(hbox)
135