Debian Codegraph @

07p/python/auto/graphics/windowPlotter.py

raw symbols SCIP
1 #! /usr/bin/env python
2
3 try:
4 import Tkinter
5 except ImportError:
6 import tkinter as Tkinter # Python 3
7 from auto.graphics import Pmw
8 from auto import parseC
9 from auto import AUTOutil
10 from auto.graphics import plotter
11 import sys
12
13 # FIXME: No regression tester (except as part of interactiveBindings)
14 class WindowPlotter(Pmw.MegaToplevel):
15 def __init__(self,grapherClass,parent=None,disabled=False,**kw):
16 optiondefs = []
17 self.defineoptions(kw,optiondefs)
18 if kw.get('grapher_hide') or disabled:
19 if 'auto.graphics.grapher_mpl' in sys.modules or disabled:
20 kwnew = {}
21 for k in kw:
22 if k.startswith('grapher_'):
23 kwnew[k[8:]] = kw[k]
24 self.grapher = grapherClass(**kwnew)
25 self.savefig = self.grapher.savefig
26 return
27 # without matplotlib, retract the Tk() window
28 parent=Tkinter.Tk()
29 parent.withdraw()
30 print("\nCannot hide the on-screen plot without matplotlib.")
31 Pmw.MegaToplevel.__init__(self, parent)
32
33 interior = self.interior()
34 self.helpBalloon = self.createcomponent('helpBalloon',
35 (), None,
36 Pmw.Balloon, interior)
37
38 self.menuBar = self.createcomponent('menuBar',
39 (), None,
40 Pmw.MainMenuBar,interior,
41 hotkeys="true",
42 hull_relief = 'raised',
43 hull_borderwidth = 1,
44 balloon=self.helpBalloon)
45 interior.configure(menu=self.menuBar)
46 self.menuBar.addmenu("File","File operations")
47 self.menuBar.addmenu("Options","View and set options")
48 self.menuBar.addmenu("Help","View help on the plotting widget",name='help')
49
50 topbox = Tkinter.Frame(interior,relief="raised",borderwidth=2)
51 topbox.pack(side=Tkinter.BOTTOM)
52
53 self.grapher = self.createcomponent('grapher',
54 (), None,
55 grapherClass,interior)
56
57 self.menuBar.addmenuitem("File",
58 "command",
59 "Save the plot as postscript...",
60 label = "Save Postscript...",
61 command = self.grapher.generatePostscript
62 )
63 self.menuBar.addmenuitem("File",
64 "command",
65 "Destroy the plot",
66 label = "Quit",
67 command = self.destroywindow
68 )
69 self.menuBar.addmenuitem("Options",
70 "command",
71 label="Options...",
72 command=self._setOptionWindow
73 )
74
75
76 box = Tkinter.Frame(topbox)
77
78 self.grapher.pack(expand=1,fill=Tkinter.BOTH)
79 labelEntry = self.createcomponent('labelEntry',
80 (), None,
81 Pmw.RadioSelect,box,
82 labelpos="w",
83 label_text="Type")
84 labelEntry.add("bifurcation")
85 labelEntry.add("solution")
86 labelEntry.invoke(self.grapher.cget("type"))
87 labelEntry.configure(command = lambda entry:
88 self._modifyOption("type",entry))
89 labelEntry.grid(row=0,column=0)
90 self.labelEntry = labelEntry
91
92 typeEntry = self.createcomponent('typeEntry',
93 (), None,
94 Pmw.ComboBox,box,
95 labelpos="w",
96 label_text="Label")
97
98 typeEntry.grid(row=0,column=1)
99 typeEntry.configure(selectioncommand = lambda entry:self._modifyOption("label",entry))
100 self.typeEntry = typeEntry
101 box.grid(row=0)
102
103 box = Tkinter.Frame(topbox)
104 self._extraButtons(box)
105 box.grid(row=1)
106
107 # Let the appropriate things grow
108 topbox.rowconfigure(1,weight=1)
109 topbox.columnconfigure(0,weight=1)
110
111 self.initialiseoptions(WindowPlotter)
112 self.savefig = self.grapher.savefig
113
114 def __labelFunction(self,lst):
115 # The return value of a ScrolledListBox is a list of strings, so we change them
116 # to integers here
117 lst=map(int,lst)
118 # but the _modifyOption method expects a string, so we change the whole thing to a string here
119 self._modifyOption("label",str(lst))
120
121 def _setOptionWindow(self):
122 lst = sorted([key for key in self.grapher.configure()
123 if self.grapher._isInternalOption(key)])
124 self.optionSelctionDialog = Pmw.SelectionDialog(title = 'Options',
125 buttons = ('OK', 'Cancel'),
126 defaultbutton = 'OK',
127 scrolledlist_labelpos = 'n',
128 label_text = 'Setable options',
129 scrolledlist_items = lst,
130 command = self.optionSelectionDialogCommand)
131
132 def _extraButtons(self,box):
133 pass
134
135 def optionSelectionDialogCommand(self,button):
136 if button == "OK":
137 self.setOptionDialog(self.optionSelctionDialog.getcurselection()[0])
138 if button == "Cancel":
139 self.optionSelctionDialog.destroy()
140
141 def _shortstr(self,x):
142 if isinstance(x,(list,tuple)):
143 return ",".join(map(str,x))
144 return x
145
146 def setOptionDialog(self,key):
147 self.diag = Pmw.Dialog(self.interior(),
148 buttons=("Ok","Cancel"))
149 optionLabel = Pmw.EntryField(self.diag.interior(),
150 labelpos="w",
151 label_text="Option Name",
152 entry_state=Tkinter.DISABLED)
153 optionLabel.pack(side="top")
154
155 valueLabel = Pmw.EntryField(self.diag.interior(),
156 labelpos="w",
157 label_text="Old Value",
158 entry_state=Tkinter.DISABLED)
159 valueLabel.pack(side="top")
160
161 valueEntry = Pmw.EntryField(self.diag.interior(),
162 labelpos="w",
163 label_text="New Value")
164 valueEntry.pack(side="top")
165 valueEntry.configure(command = lambda: self.__dialogFunction("Ok",key,valueEntry.get()))
166
167 self.diag.configure(command = lambda button:self.__dialogFunction(button,key,valueEntry.get()))
168
169
170 optionLabel.setentry(key)
171 valueLabel.setentry(str(self.grapher.cget(key)))
172 valueEntry.clear()
173
174 def __dialogFunction(self,button,key,entry):
175 if button == "Ok":
176 self._modifyOption(key,entry)
177 self.diag.destroy()
178
179 def _modifyOption(self,key,entry):
180 if key[-2:] == "_z" and entry == " 2D Plot ":
181 self.grapher[key] = None
182 return
183 self.grapher[key] = self.grapher.parseoption(key, entry,
184 parseC.parseC())
185 if key == "type":
186 self.typeUpdateCallback()
187
188 def typeUpdateCallback(self):
189 pass
190
191 def checktype(self):
192 # force type change if a bifurcation diagram or solution don't exist
193 ty = self.grapher.cget("type")
194 bd = self.grapher.cget("bifurcation_diagram")
195 sol = self.grapher.cget("solution")
196 if ty == "bifurcation" and (len(bd)==0 or len(bd[0])==0) and len(sol):
197 self.grapher.config(type="solution")
198 self.labelEntry.invoke("solution")
199 elif ty == "solution" and len(sol)==0 and len(bd) and len(bd[0]):
200 self.grapher.config(type="bifurcation")
201 self.labelEntry.invoke("bifurcation")
202
203 def config(self,cnf=None,**kw):
204 """
205 Configure settings for a plot.
206
207 The values for resources are specified as keyword
208 arguments. See the manual and example autorc in $AUTO_DIR/autorc for
209 the list of allowed keyword arguments.
210 """
211 rval = self.grapher.config(cnf,**kw)
212 if isinstance(cnf, str) or (cnf is None and not kw):
213 return rval
214 self.checktype()
215 if not hasattr(self, "menuBar"):
216 #hidden (no on-screen) graph
217 return
218 dct = (cnf or {}).copy()
219 dct.update(kw)
220 for key,value in dct.items():
221 if key == "type":
222 self.labelEntry.invoke(value)
223 if key in ["type","label","label_defaults",
224 "bifurcation_diagram_filename",
225 "solution_filename",
226 "bifurcation_column_defaults",
227 "bifurcation_diagram",
228 "bifurcation_x","bifurcation_y","bifurcation_z",
229 "bifurcation_coordnames",
230 "solution_column_defaults",
231 "solution_indepvarname","solution_coordnames",
232 "solution","solution_x","solution_y","solution_z"]:
233 self.typeUpdateCallback()
234
235 configure = config
236
237 def cget(self,option):
238 return self.grapher.cget(option)
239
240 def __getitem__(self,key):
241 try:
242 return self.grapher[key]
243 except:
244 return Pmw.MegaToplevel.__getitem__(self,key)
245
246 def update(self):
247 self.grapher.update()
248 Pmw.MegaToplevel.update(self)
249
250 def destroy(self):
251 self.typeEntry.destroy()
252 Pmw.MegaToplevel.destroy(self)
253
254 def destroywindow(self):
255 self.destroy()
256
257 class WindowPlotter2D(WindowPlotter):
258 def __init__(self,parent=None,**kw):
259 WindowPlotter.__init__(self,plotter.plotter,parent,**kw)
260
261 def _extraButtons(self,box):
262 self.xEntry = self.createcomponent('xEntry',
263 (), None,
264 Pmw.ComboBox,box,
265 labelpos="w",
266 label_text="X")
267 self.xEntry.grid(row=0,column=0)
268
269 self.yEntry = self.createcomponent('yEntry',
270 (), None,
271 Pmw.ComboBox,box,
272 labelpos="w",
273 label_text="Y")
274 self.yEntry.grid(row=0,column=1)
275
276 if plotter.Axes3D is not None:
277 self.zEntry = self.createcomponent('zEntry',
278 (), None,
279 Pmw.ComboBox,box,
280 labelpos="w",
281 label_text="Z")
282 self.zEntry.grid(row=0,column=2)
283
284 self.checktype()
285 self.typeUpdateCallback()
286
287 def destroy(self):
288 self.xEntry.destroy()
289 self.yEntry.destroy()
290 if plotter.Axes3D is not None:
291 self.zEntry.destroy()
292 WindowPlotter.destroy(self)
293
294 def typeUpdateCallback(self):
295 if self.grapher.cget("type") == "bifurcation":
296 ox = "bifurcation_x"
297 oy = "bifurcation_y"
298 oz = "bifurcation_z"
299 ocd = "bifurcation_column_defaults"
300 o = "bifurcation_diagram"
301 else:
302 ox = "solution_x"
303 oy = "solution_y"
304 oz = "solution_z"
305 ocd = "solution_column_defaults"
306 o = "solution"
307 self.xEntry.configure(selectioncommand = lambda entry:
308 self._modifyOption(ox,entry))
309 self.yEntry.configure(selectioncommand = lambda entry:
310 self._modifyOption(oy,entry))
311 if plotter.Axes3D is not None:
312 self.zEntry.configure(selectioncommand = lambda entry:
313 self._modifyOption(oz,entry))
314
315 lst = []
316 if self.grapher.cget(ocd) is not None:
317 lst = list(map(str,self.grapher.cget(ocd)))
318 sol = self.grapher.cget(o)
319 if self.grapher.cget("type") == "solution":
320 indepvarname = None
321 for s in sol:
322 indepvarname = s.indepvarname
323 break
324 if self.grapher.cget(ox[:-1]+"indepvarname"):
325 indepvarname = self.grapher.cget(ox[:-1]+"indepvarname")
326 if indepvarname is not None:
327 lst.append(indepvarname)
328 coordnames = self.grapher._coordnames
329 lst.extend(coordnames)
330 xlist = self.grapher.cget(ox)
331 ylist = self.grapher.cget(oy)
332 zlist = self.grapher.cget(oz)
333 if type(xlist) == type((0,)):
334 xlist = list(xlist)
335 if type(ylist) == type((0,)):
336 ylist = list(ylist)
337 if type(zlist) == type((0,)):
338 zlist = list(zlist)
339 if len(sol) > 0:
340 for xylist in xlist,ylist,zlist:
341 if xylist is None:
342 break
343 for i in range(len(xylist)):
344 if type(xylist[i]) == type(1):
345 if xylist[i] == -1:
346 xylist[i] = indepvarname
347 elif coordnames != []:
348 xylist[i] = coordnames[xylist[i]]
349 else:
350 xylist[i] = str(i)
351 xentry = self._shortstr(xlist)
352 yentry = self._shortstr(ylist)
353 zentry = None
354 if plotter.Axes3D is not None:
355 if zlist is None:
356 zentry = " 2D Plot "
357 else:
358 zentry = self._shortstr(zlist)
359 for entry in xentry, yentry, zentry:
360 if entry is not None and entry != " 2D Plot " and entry not in lst:
361 lst.append(entry)
362 self.xEntry.setentry(xentry)
363 self.xEntry.setlist(lst)
364 self.yEntry.setentry(yentry)
365 self.yEntry.setlist(lst)
366 if plotter.Axes3D is not None:
367 self.zEntry.setentry(zentry)
368 self.zEntry.setlist([" 2D Plot "]+lst)
369 labels = []
370 if self.grapher.cget("label_defaults") is not None:
371 labels = list(map(str,self.grapher.cget("label_defaults")))
372 default_labels = self.grapher.cget("solution").getLabels()
373 labels.extend(["%d"%d for d in default_labels])
374 if len(default_labels) > 1 or len(labels) == 0:
375 labels.append(self._shortstr(default_labels))
376 if self.grapher.cget("label") == [0]:
377 entry = labels[0]
378 else:
379 entry = self._shortstr(self.grapher.cget("label"))
380 self.typeEntry.setentry(entry)
381 if entry not in labels:
382 labels.append(entry)
383 self.typeEntry.setlist(labels)
384
385
386