1 import os.path
2 import gtk, gtk.glade
3 import logging
4 log = logging.getLogger("gtkui.Config")
5
6
7 import conduit
8
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
38 self.widgetInstances = []
39 self.dialogParent = window
40
41 self.customSettings = gtk.VBox(False, 5)
42
43
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
58 self.build_child()
59 vbox = widgets.get_widget("configVBox")
60 vbox.pack_start(self.customSettings)
61 self.customSettings.show_all()
62
64 """
65 on_ok_clicked
66 """
67 log.debug("OK Clicked")
68 for w in self.widgetInstances:
69
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
80 """
81 on_cancel_clicked
82 """
83 log.debug("Cancel Clicked")
84 self.dialog.destroy()
85
87 """
88 on_help_clicked
89 """
90 log.debug("Help Clicked")
91
93 """
94 on_dialog_close
95 """
96 log.debug("Dialog Closed")
97 self.dialog.destroy()
98
100 """
101 run
102 """
103 resp = self.dialog.run()
104
106 """
107 For each item in the mappings list create the appropriate widget
108 """
109
110 for l in self.mappings:
111
112 widget = l["Widget"]()
113
114 hbox = gtk.HBox(False, 5)
115
116
117 if isinstance(widget, gtk.Entry):
118
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
124 widget = l["Widget"](l["Name"])
125 widget.set_active(bool(l["InitialValue"]))
126
127
128 self.widgetInstances.append({
129 "Widget" : widget,
130 "Callback" : l["Callback"]
131 })
132
133 hbox.pack_start(widget)
134 self.customSettings.pack_start(hbox)
135