/[ascend]/trunk/pygtk/unitsdialog.py
ViewVC logotype

Contents of /trunk/pygtk/unitsdialog.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1704 - (show annotations) (download) (as text)
Sun Jan 6 04:54:18 2008 UTC (12 years, 3 months ago) by jpye
File MIME type: text/x-python
File size: 4528 byte(s)
Added context menu for 'select units'.

1 import gtk, gtk.glade, pango, gobject, re
2
3 class UnitsDialog:
4
5 def __init__(self,browser,parent=None,typename=None):
6 self.browser = browser;
7
8 # GUI config
9 _xml = gtk.glade.XML(browser.glade_file,"unitsdialog")
10 self.window = _xml.get_widget("unitsdialog")
11 self.typecombo = _xml.get_widget("typecombo")
12 self.dimensionlabel = _xml.get_widget("dimensionlabel")
13 self.unitsview = _xml.get_widget("unitsview")
14 self.applybutton = _xml.get_widget("applybutton")
15
16 self.applybutton.set_sensitive(False)
17
18 self.parent = parent
19 if parent:
20 self.window.set_transient_for(self.parent)
21
22 _xml.signal_autoconnect(self)
23
24 self.units = self.browser.library.getUnits()
25 self.realtypes = self.browser.library.getRealAtomTypes()
26
27 if not len(self.realtypes):
28 self.browser.reporter.reportError("No dimensioned atom types available yet (have you loaded a model yet?)")
29 raise RuntimeError("no units available")
30
31 # set up columns in the units view:
32 _renderer0 = gtk.CellRendererToggle()
33 _renderer0.set_radio(True)
34 _renderer0.connect("toggled",self.unitsview_row_toggled)
35 _col0 = gtk.TreeViewColumn("",_renderer0,active=0)
36 self.unitsview.append_column(_col0)
37
38 _renderer1 = gtk.CellRendererText()
39 _col1 = gtk.TreeViewColumn("Units", _renderer1, text=1, weight=3)
40 self.unitsview.append_column(_col1)
41
42 # value column: 'editable' set by column 3 of the model data.
43 _renderer2 = gtk.CellRendererText()
44 _col2 = gtk.TreeViewColumn("Conversion", _renderer2, text=2)
45 self.unitsview.append_column(_col2)
46
47 if typename is not None:
48 self.typecombo.child.set_text(typename)
49 else:
50 self.update_typecombo()
51 self.changed = {}
52
53 def unitsview_row_toggled(self,widget,path,*args):
54 i = self.unitsview.get_model().get_iter_from_string(path)
55 n = self.unitsview.get_model().get_value(i,1)
56 j = self.unitsview.get_model().get_iter_first()
57 while j is not None:
58 self.unitsview.get_model().set_value(j,0,False)
59 j = self.unitsview.get_model().iter_next(j)
60 self.unitsview.get_model().set_value(i,0,True)
61 self.changed[self.typecombo.get_active_text()]=n
62 #self.browser.reporter.reportNote("Units for '%s' set to '%s'" % (self.typecombo.get_active_text(),n))
63 self.update_applybutton()
64
65 def update_applybutton(self):
66 can_apply = False
67 #print "changed = ",self.changed
68 for k,v in self.changed.iteritems():
69 T = self.browser.library.findType(k)
70 u = T.getPreferredUnits()
71 if u is None or str(T.getPreferredUnits().getName()) != v:
72 #print "CAN APPLY: for type '%s', pref units currently '%s', now selected '%s'" % (T.getName(),T.getPreferredUnits().getName(), v)
73 can_apply = True
74 break
75 self.applybutton.set_sensitive(can_apply)
76
77 def update_typecombo(self,text = None):
78 m = gtk.ListStore(str)
79 for t in self.realtypes:
80 if not text or re.compile("^%s"%re.escape(text)).match(str(t.getName())):
81 m.append([t.getName()])
82 self.typecombo.set_model(m)
83 if text and m.iter_n_children(None):
84 self.typecombo.popup()
85 self.typecombo.child.grab_focus()
86 self.typecombo.set_text_column(0)
87
88 def update_unitsview(self,T):
89 m = gtk.ListStore(bool,str,str,int)
90 if T is not None:
91 d = T.getDimensions()
92 up = T.getPreferredUnits()
93 if up is None:
94 print "no preferred units"
95 else:
96 print "preferred units =",up.getName()
97 else:
98 up = None
99 for u in self.units:
100 if T is None or u.getDimensions()==d:
101 if up is None:
102 selected = False
103 else:
104 selected = (u==up)
105 weight = pango.WEIGHT_NORMAL
106 if selected:
107 weight = pango.WEIGHT_BOLD
108 du = u.getDimensions().getDefaultUnits().getName()
109 m.append([selected,u.getName(),"%g %s" %(u.getConversion(),du),weight])
110 self.unitsview.set_model(m)
111
112 def on_typecombo_changed(self,widget,*args):
113 s = widget.get_active_text()
114 self.update_typecombo(s)
115 #self.browser.reporter.reportNote("value changed to '%s'" % s)
116 try:
117 T = self.browser.library.findType(widget.get_active_text())
118 self.dimensionlabel.set_text(str(T.getDimensions()))
119 except:
120 T = None
121 self.dimensionlabel.set_text("")
122
123 self.update_unitsview(T)
124
125 def run(self):
126 _res = gtk.RESPONSE_APPLY
127 while _res == gtk.RESPONSE_APPLY:
128 _res = self.window.run()
129 if _res == gtk.RESPONSE_APPLY or _res == gtk.RESPONSE_CLOSE:
130 for k,v in self.changed.iteritems():
131 self.browser.prefs.setPreferredUnits(k,v)
132 self.changed = {}
133 self.update_unitsview(self.browser.library.findType(self.typecombo.get_active_text()))
134 self.browser.modelview.refreshtree()
135 self.window.hide()
136

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22