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

Contents of /trunk/pygtk/gtkbrowser.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 803 - (show annotations) (download) (as text)
Fri Aug 4 05:51:31 2006 UTC (16 years, 3 months ago) by johnpye
File MIME type: text/x-python
File size: 32545 byte(s)
Added default solver preference in PyGTK GUI.
Removed some debug output from library.cpp
1 #!/usr/bin/env python
2
3 import sys
4 def print_loading_status(status,msg=None):
5 sys.stderr.write("\r \r")
6 if msg!=None:
7 sys.stderr.write(msg+"\n")
8 sys.stderr.write(status+"...\r")
9 sys.stderr.flush()
10
11 try:
12 print_loading_status("Loading PSYCO")
13
14 #try:
15 # import psyco
16 # psyco.full()
17 # print "Running with PSYCO optimisation..."
18 #except ImportError:
19 # pass
20
21
22 print_loading_status("Loading python standard libraries")
23
24 import re
25 import urlparse
26 import optparse
27 import platform
28 import sys
29 import os.path
30
31 if platform.system() != "Windows":
32 import dl
33 # This sets the flags for dlopen used by python so that the symbols in the
34 # ascend library are made available to libraries dlopened within ASCEND:
35 sys.setdlopenflags(dl.RTLD_GLOBAL|dl.RTLD_NOW)
36
37 print_loading_status("Loading LIBASCEND/ascpy")
38 import ascpy
39
40 print_loading_status("Loading PyGTK, glade, pango")
41
42 import pygtk
43 pygtk.require('2.0')
44 import gtk
45 import gtk.glade
46 import pango
47
48 print_loading_status("Loading python matplotlib")
49 try:
50 import matplotlib
51 matplotlib.use('GTKAgg')
52
53 try:
54 print_loading_status("Trying python numarray")
55 import numarray
56 matplotlib.rcParams['numerix'] = 'numarray'
57 print_loading_status("","Using python module numarray")
58 except ImportError:
59 try:
60 print_loading_status("Trying python numpy")
61 import numpy
62 matplotlib.rcParams['numerix'] = 'numpy'
63 print_loading_status("","Using python module numpy")
64 except ImportError:
65 try:
66 print_loading_status("Trying python Numeric")
67 import Numeric
68 matplotlib.rcParams['numerix'] = 'Numeric'
69 print_loading_status("","Using python module Numeric")
70 except ImportError:
71 print_loading_status("","FAILED TO LOAD A NUMERIC MODULE FOR PYTHON")
72
73 except ImportError,e:
74 print_loading_status("","FAILED TO LOAD MATPLOTLIB")
75 raise RuntimeError("Failed to load MATPLOTLIB (is it installed?). Details:"+str(e))
76
77 print_loading_status("Loading IPython")
78 import console;
79 if not console.have_ipython:
80 print_loading_status("","IPython couldn't be loaded")
81
82 print_loading_status("Loading ASCEND python modules")
83 from preferences import * # loading/saving of .ini options
84 from solverparameters import * # 'solver parameters' window
85 from help import * # viewing help files
86 from incidencematrix import * # incidence/sparsity matrix matplotlib window
87 from observer import * # observer tab support
88 from properties import * # solver_var properties dialog
89 from varentry import * # for inputting of variables with units
90 from diagnose import * # for diagnosing block non-convergence
91 from solverreporter import * # solver status reporting
92 from modelview import * # model browser
93 from integrator import * # integrator dialog
94 from infodialog import * # general-purpose textual information dialog
95 import config
96
97 except RuntimeError, e:
98 print "ASCEND had problems starting up. Please report the following"
99 print "error message at http://mantis.cruncher2.dyndns.org/."
100 print "\n\nFull error message:",str(e)
101 print "\n\nPress ENTER to close this window."
102 sys.stdout.flush()
103 sys.stdin.readline();
104 sys.exit();
105
106 except ImportError, e:
107 print "\n\n------------------ ERROR ---------------------"
108 print "ASCEND had problems importing required models."
109 print "\nPlease ensure you have all the runtime prerequisites installed."
110 print "Please then report a bug if you continue to have problems."
111 print "\nFull error message:",str(e)
112 if platform.system()=="Windows":
113 print "\nYou will also need to report the contents of any popup error"
114 print "messages from Windows if any were shown."
115 print "\n\nPress ENTER to close this window."
116 sys.stdout.flush()
117 sys.stdin.readline();
118 sys.exit();
119
120 print_loading_status("Starting GUI")
121
122 # This is my first ever GUI code so please be nice :)
123 # But I *have* at least read
124 # http://www.joelonsoftware.com/uibook/chapters/fog0000000057.html
125 # and leafed through
126 # http://developer.gnome.org/projects/gup/hig/
127
128 # The fancy tree-view gizmo is the GtkTreeView object. See the article
129 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/300304
130 # for the original source code on which my implementation was based.
131
132 ESCAPE_KEY = 65307
133
134 HELP_ROOT = None
135
136 #======================================
137 # Browser is the main ASCEND library/model browser window
138
139 class Browser:
140
141 # ---------------------------------
142 # SETUP
143
144 def __init__(self):
145 #--------
146 # load the file referenced in the command line, if any
147
148 print_loading_status("Parsing options")
149
150 parser = optparse.OptionParser(usage="%prog [[-m typename] file]", version="gtkbrowser $rev$" )
151 # add options here if we want
152
153 parser.add_option("-m", "--model"
154 ,action="store", type="string", dest="model"
155 ,help="specify the model to instantiate upon loading modules")
156
157 parser.add_option("--pygtk-assets"
158 ,action="store", type="string", dest="assets_dir"
159 ,help="override the configuration value for the location of assets"\
160 +" required by PyGTK for the ASCEND GUI, optional"
161 ,default=config.PYGTK_ASSETS
162 )
163
164 parser.add_option("--library"
165 ,action="store", type="string", dest="library_path"
166 ,help="override the configuration value for the library path"
167 ,default=None
168 )
169
170 parser.add_option("--no-auto-sim"
171 ,action="store_false", dest="auto_sim"
172 ,help="disable auto-instantiation of MODEL named as the file stem"
173 ,default=True
174 )
175
176 (self.options, args) = parser.parse_args()
177
178 #print "OPTIONS_______________:",self.options
179
180 self.assets_dir = self.options.assets_dir
181
182 self.observers = []
183 self.clip = None
184
185 #--------
186 # load up the preferences ini file
187
188 print_loading_status("Loading preferences")
189
190 self.prefs = Preferences()
191
192 _prefpath = self.prefs.getStringPref("Directories","librarypath",None)
193 _preffileopenpath = self.prefs.getStringPref("Directories","fileopenpath",None)
194
195 #--------
196 # set up library path and the path to use for File->Open dialogs
197
198 if self.options.library_path != None:
199 _path = os.path.abspath(self.options.library_path)
200 _pathsrc = "commandline"
201 # when a special path is specified, use that as the file-open location
202 self.fileopenpath = _path
203 else:
204 if _prefpath:
205 _path = _prefpath
206 _pathsrc = "user preferences"
207 else:
208 _path = config.LIBRARY_PATH
209 _pathsrc = "default (config.py)"
210
211 if _preffileopenpath:
212 self.fileopenpath = _preffileopenpath
213 else:
214 self.fileopenpath = _path
215
216 #--------
217 # Create the ASCXX 'Library' object
218
219 print_loading_status("Creating ASCEND 'Library' object","PATH = "+_path+" FROM "+_pathsrc)
220 self.library = ascpy.Library(_path)
221
222 self.sim = None
223
224 #-------------------
225 # Set up the window and main widget actions
226
227 print_loading_status("Setting up windows")
228
229 self.glade_file = self.assets_dir+config.GLADE_FILE
230 glade = gtk.glade.XML(self.glade_file,"browserwin")
231
232 self.window = glade.get_widget("browserwin")
233
234
235 if not self.window:
236 raise RuntimeError("Couldn't load window from glade file")
237
238 _display = self.window.get_screen().get_display().get_name()
239 _geom=self.prefs.getGeometrySizePosition(_display,"browserwin")
240 if _geom:
241 self.window.resize(_geom[0],_geom[1])
242 self.window.move(_geom[2],_geom[3])
243
244 self.window.connect("delete_event", self.delete_event)
245
246 self.browserpaned=glade.get_widget("browserpaned")
247 _geom2=self.prefs.getGeometryValue(_display,"browserpaned")
248 if _geom2:
249 self.browserpaned.set_position(_geom2)
250
251 self.openbutton=glade.get_widget("openbutton")
252 self.openbutton.connect("clicked",self.open_click)
253
254 self.reloadbutton=glade.get_widget("reloadbutton")
255 self.reloadbutton.connect("clicked",self.reload_click)
256
257 self.solvebutton=glade.get_widget("solvebutton")
258 self.solvebutton.connect("clicked",self.solve_click)
259
260 self.integratebutton=glade.get_widget("integratebutton")
261 self.integratebutton.connect("clicked",self.integrate_click)
262
263 self.checkbutton=glade.get_widget("checkbutton")
264 self.checkbutton.connect("clicked",self.check_click)
265
266 self.autotoggle=glade.get_widget("autotoggle")
267 self.automenu = glade.get_widget("automenu")
268 self.autotoggle.connect("toggled",self.auto_toggle)
269
270 self.methodrunbutton=glade.get_widget("methodrunbutton")
271 self.methodrunbutton.connect("clicked",self.methodrun_click)
272
273 self.methodsel=glade.get_widget("methodsel")
274
275 self.maintabs = glade.get_widget("maintabs")
276
277 self.statusbar = glade.get_widget("statusbar")
278
279 self.menu = glade.get_widget("browsermenu")
280
281 self.show_solving_popup=glade.get_widget("show_solving_popup")
282 self.show_solving_popup.set_active(self.prefs.getBoolPref("SolverReporter","show_popup",True))
283 self.close_on_converged=glade.get_widget("close_on_converged")
284 self.close_on_converged.set_active(self.prefs.getBoolPref("SolverReporter","close_on_converged",True))
285 self.close_on_nonconverged=glade.get_widget("close_on_nonconverged")
286 self.close_on_nonconverged.set_active(self.prefs.getBoolPref("SolverReporter","close_on_nonconverged",True))
287 self.solver_engine=glade.get_widget("solver_engine")
288
289 self.use_relation_sharing=glade.get_widget("use_relation_sharing")
290 self.use_relation_sharing.set_active(self.prefs.getBoolPref("Compiler","use_relation_sharing",True))
291
292 glade.signal_autoconnect(self)
293
294 #-------
295 # Status icons
296
297 self.fixedimg = gtk.Image()
298 self.fixedimg.set_from_file(self.options.assets_dir+'locked.png')
299
300 self.iconstatusunknown = None
301 self.iconfixed = self.fixedimg.get_pixbuf()
302 self.iconsolved = self.window.render_icon(gtk.STOCK_YES,gtk.ICON_SIZE_MENU)
303 self.iconactive = self.window.render_icon(gtk.STOCK_NO,gtk.ICON_SIZE_MENU)
304 self.iconunsolved = None
305
306 self.statusicons={
307 ascpy.ASCXX_VAR_STATUS_UNKNOWN: self.iconstatusunknown
308 ,ascpy.ASCXX_VAR_FIXED: self.iconfixed
309 ,ascpy.ASCXX_VAR_SOLVED: self.iconsolved
310 ,ascpy.ASCXX_VAR_ACTIVE: self.iconactive
311 ,ascpy.ASCXX_VAR_UNSOLVED: self.iconunsolved
312 }
313
314
315 self.statusmessages={
316 ascpy.ASCXX_VAR_STATUS_UNKNOWN: "Status unknown"
317 ,ascpy.ASCXX_VAR_FIXED: "Fixed"
318 ,ascpy.ASCXX_VAR_SOLVED: "Converged"
319 ,ascpy.ASCXX_VAR_ACTIVE: "Active (unconverged)"
320 ,ascpy.ASCXX_VAR_UNSOLVED: "Not yet visited"
321 }
322
323 #-------------------
324 # waitwin
325
326 self.waitwin = gtk.gdk.Window(self.window.window,
327 gtk.gdk.screen_width(),
328 gtk.gdk.screen_height(),
329 gtk.gdk.WINDOW_CHILD,
330 0,
331 gtk.gdk.INPUT_ONLY)
332
333 _cursor = gtk.gdk.Cursor(gtk.gdk.WATCH)
334 self.waitwin.set_cursor(_cursor)
335
336 #-------------------
337 # pixbufs to be used in the error listing
338
339 self.iconok = self.window.render_icon(gtk.STOCK_YES,gtk.ICON_SIZE_MENU)
340 self.iconinfo = self.window.render_icon(gtk.STOCK_DIALOG_INFO,gtk.ICON_SIZE_MENU)
341 self.iconwarning = self.window.render_icon(gtk.STOCK_DIALOG_WARNING,gtk.ICON_SIZE_MENU)
342 self.iconerror = self.window.render_icon(gtk.STOCK_DIALOG_ERROR,gtk.ICON_SIZE_MENU)
343
344 #--------------------
345 # pixbufs for solver_var status
346
347 #--------------------
348 # set up the error view
349
350 self.errorview = glade.get_widget("errorview")
351 errstorecolstypes = [gtk.gdk.Pixbuf,str,str,str,int]
352 self.errorstore = gtk.TreeStore(*errstorecolstypes)
353 errtitles = ["","Location","Message"];
354 self.errorview.set_model(self.errorstore)
355 self.errcols = [ gtk.TreeViewColumn() for _type in errstorecolstypes]
356
357 i = 0
358 for tvcolumn in self.errcols[:len(errtitles)]:
359 tvcolumn.set_title(errtitles[i])
360 self.errorview.append_column(tvcolumn)
361
362 if i>0:
363 _renderer = gtk.CellRendererText()
364 tvcolumn.pack_start(_renderer, True)
365 tvcolumn.add_attribute(_renderer, 'text', i)
366 if(i==2):
367 tvcolumn.add_attribute(_renderer, 'foreground', 3)
368 tvcolumn.add_attribute(_renderer, 'weight', 4)
369 else:
370 _renderer1 = gtk.CellRendererPixbuf()
371 tvcolumn.pack_start(_renderer1, False)
372 tvcolumn.add_attribute(_renderer1, 'pixbuf', int(0))
373
374 i = i + 1
375
376
377 #--------------------
378 # set up the error reporter callback
379 self.reporter = ascpy.getReporter()
380 self.reporter.setPythonErrorCallback(self.error_callback)
381
382
383 #-------
384 # Solver engine list
385
386 _slvlist = ascpy.getSolvers()
387 self.solver_engine_menu = gtk.Menu()
388 self.solver_engine_menu.show()
389 self.solver_engine.set_submenu(self.solver_engine_menu)
390 self.solver_engine_menu_dict = {}
391 _fmi = None
392 for _s in _slvlist:
393 _mi = gtk.RadioMenuItem(_fmi,_s.getName(),False)
394 if _fmi==None:
395 _fmi = _mi
396 _mi.show()
397 _mi.connect('toggled',self.on_select_solver_toggled,_s.getName())
398 self.solver_engine_menu.append(_mi)
399 self.solver_engine_menu_dict[_s.getName()]=_mi
400
401 _pref_solver = self.prefs.getStringPref("Solver","engine","QRSlv")
402 _mi = self.solver_engine_menu_dict.get(_pref_solver)
403 if _mi:
404 _mi.set_active(1)
405
406 #--------
407 # Assign an icon to the main window
408
409 self.icon = None
410 if config.ICON_EXTENSION:
411 _iconpath = ""
412 try:
413 _icon = gtk.Image()
414 _iconpath = self.assets_dir+'ascend'+config.ICON_EXTENSION
415 _icon.set_from_file(_iconpath)
416 _iconpbuf = _icon.get_pixbuf()
417 self.window.set_icon(_iconpbuf)
418 self.icon = _iconpbuf
419 except Exception, e:
420 self.reporter.reportError("FAILED to set application icon '%s': %s"
421 % (_iconpath,str(e))
422 )
423
424 #-------------------
425 # set up the module view
426
427 self.modtank = {}
428 self.moduleview = glade.get_widget("moduleview")
429 modulestorecoltypes = [str, str, int] # bool=can-be-instantiated
430 self.modulestore = gtk.TreeStore(*modulestorecoltypes)
431 moduleviewtitles = ["Module name", "Filename"]
432 self.moduleview.set_model(self.modulestore)
433 self.modcols = [ gtk.TreeViewColumn() for _type in modulestorecoltypes]
434 i = 0
435 for modcol in self.modcols[:len(moduleviewtitles)]:
436 modcol.set_title(moduleviewtitles[i])
437 self.moduleview.append_column(modcol)
438 _renderer = gtk.CellRendererText()
439 modcol.pack_start(_renderer, True)
440 modcol.add_attribute(_renderer, 'text', i)
441 modcol.add_attribute(_renderer,'weight',2)
442 i = i + 1
443 self.moduleview.connect("row-activated", self.module_activated )
444
445 #--------------------
446 # set up the methods combobox
447
448 self.methodstore = gtk.ListStore(str)
449 self.methodsel.set_model(self.methodstore)
450 _methodrenderer = gtk.CellRendererText()
451 self.methodsel.pack_start(_methodrenderer, True)
452 self.methodsel.add_attribute(_methodrenderer, 'text',0)
453
454 #--------
455 # set up the instance browser view
456
457 self.modelview = ModelView(self, glade)
458
459 #--------
460 # set the state of the 'auto' toggle
461
462 self.is_auto = self.prefs.getBoolPref("Browser","auto_solve",True)
463 self.autotoggle.set_active(self.is_auto)
464 self.automenu.set_active(self.is_auto)
465
466 #--------
467 # options
468
469 if(len(args)==1):
470 self.do_open(args[0])
471
472 print "Options: ",self.options
473
474 _model = None
475 if self.options.model:
476 _model = self.options.model
477 print "MODEL: '%s'" % _model
478 elif self.options.auto_sim:
479 _head, _tail = os.path.split(args[0])
480 if(_tail):
481 _model, _ext = os.path.splitext(_tail)
482
483 if _model:
484 try:
485 _t=self.library.findType(_model)
486 try:
487 self.do_sim(_t)
488 if not self.options.model:
489 self.reporter.reportNote("Instantiating self-titled model '%s'" %_model)
490 except RuntimeError, e:
491 self.reporter.reportError("Failed to create instance of '%s': %s"
492 %(_model, str(e))
493 );
494 except RuntimeError, e:
495 if self.options.model:
496 self.reporter.reportError("Unknown model type '%s': %s"
497 %(_model, str(e))
498 );
499
500 def run(self):
501 self.window.show()
502 print_loading_status("ASCEND is now running")
503 gtk.main()
504
505 # ------------------
506 # SOLVER LIST
507
508 def set_solver(self,solvername):
509 self.solver = ascpy.Solver(solvername)
510 self.prefs.setStringPref("Solver","engine",solvername)
511 self.reporter.reportNote("Set solver engine to '%s'" % solvername)
512
513 # --------------------------------------------
514 # MAJOR GUI COMMANDS
515
516 def on_fix_variable_activate(self,*args):
517 self.modelview.on_fix_variable_activate(*args)
518
519 def on_free_variable_activate(self,*args):
520 self.modelview.on_free_variable_activate(*args)
521
522 def on_select_solver_toggled(self,widget,solvername):
523 if widget.get_active():
524 self.set_solver(solvername)
525
526 def do_open(self,filename):
527 # TODO does the user want to lose their work?
528 # TODO do we need to chdir?
529
530 _context = self.statusbar.get_context_id("do_open")
531
532 self.errorstore.clear()
533
534 self.modelview.clear()
535
536 # self.library.clear()
537
538 self.statusbar.push(_context,"Loading '"+filename+"'")
539 self.library.load(filename)
540 self.statusbar.pop(_context)
541
542 self.filename = filename
543
544 # Load the current list of modules into self.modules
545 self.modtank = {}
546 self.modulestore.clear()
547 modules = self.library.getModules()
548 self.library.listModules()
549 try:
550 _lll=len(modules)
551 except:
552 _msg = "UNABLE TO ACCESS MODULES LIST. This is bad.\n"+\
553 "Check your SWIG configuration (check for warnings during build)."
554
555 self.reporter.reportError(_msg)
556 raise RuntimeError(_msg)
557
558 for m in reversed(modules):
559 _n = str( m.getName() )
560 _f = str( m.getFilename() )
561 #print "ADDING ROW name %s, file = %s" % (_n, _f)
562 _r = self.modulestore.append(None, [ _n, _f, pango.WEIGHT_NORMAL ])
563 for t in self.library.getModuleTypes(m):
564 _n = t.getName()
565 _hasparams = t.hasParameters()
566 if _hasparams:
567 _w = pango.WEIGHT_NORMAL
568 else:
569 _w = pango.WEIGHT_BOLD
570
571 #print "ADDING TYPE %s" % _n
572 _piter = self.modulestore.append(_r , [ _n, "", _w ])
573 _path = self.modulestore.get_path(_piter)
574 self.modtank[_path]=t
575
576 #print "DONE ADDING MODULES"
577
578 self.sim = None;
579 self.maintabs.set_current_page(0);
580
581 # See http://www.daa.com.au/pipermail/pygtk/2005-October/011303.html
582 # for details on how the 'wait cursor' is done.
583 def start_waiting(self, message):
584 self.waitcontext = self.statusbar.get_context_id("waiting")
585 self.statusbar.push(self.waitcontext,message)
586
587 if self.waitwin:
588 self.waitwin.show()
589
590 while gtk.events_pending():
591 gtk.main_iteration()
592
593 def stop_waiting(self):
594 if self.waitwin:
595 self.statusbar.pop(self.waitcontext)
596 self.waitwin.hide()
597
598 def do_sim(self, type_object):
599 self.sim = None;
600 # TODO: clear out old simulation first!
601
602 print "DO_SIM(%s)" % str(type_object.getName())
603 self.start_waiting("Compiling...")
604
605 try:
606 _v = self.prefs.getBoolPref("Compiler","use_relation_sharing",True)
607 ascpy.getCompiler().setUseRelationSharing(_v)
608
609 self.sim = type_object.getSimulation(str(type_object.getName())+"_sim")
610 except RuntimeError, e:
611 self.stop_waiting()
612 self.reporter.reportError(str(e))
613 return
614
615 print "...DONE 'getSimulation'"
616 self.stop_waiting()
617
618 self.start_waiting("Building simulation...")
619 print "BUILDING SIMULATION"
620
621 try:
622 self.sim.build()
623 except RuntimeError, e:
624 self.stop_waiting()
625 self.reporter.reportError(str(e))
626 return;
627
628 print "DONE BUILDING"
629 self.stop_waiting()
630
631 self.sim.setSolver(self.solver)
632
633 # methods
634 self.methodstore.clear()
635 _methods = self.sim.getType().getMethods()
636 _activemethod = None;
637 for _m in _methods:
638 _i = self.methodstore.append([_m.getName()])
639 if _m.getName()=="on_load":
640 self.methodsel.set_active_iter(_i)
641
642 self.modelview.setSimulation(self.sim)
643
644 def do_solve_if_auto(self):
645 if self.is_auto:
646 self.sim.checkInstance()
647 self.do_solve()
648 else:
649 self.sim.processVarStatus()
650 self.modelview.refreshtree()
651
652 self.sync_observers()
653
654 def do_solve(self):
655 if not self.sim:
656 self.reporter.reportError("No model selected yet")
657 return
658
659 self.start_waiting("Solving with %s..." % self.solver.getName())
660
661 if self.prefs.getBoolPref("SolverReporter","show_popup",True):
662 reporter = PopupSolverReporter(self,self.sim.getNumVars())
663 else:
664 reporter = SimpleSolverReporter(self)
665
666 self.sim.solve(self.solver,reporter)
667
668 self.stop_waiting()
669
670 self.sim.processVarStatus()
671 self.modelview.refreshtree()
672
673 def do_integrate(self):
674 if not self.sim:
675 self.reporter.reportError("No model selected yet")
676 return
677 integwin = IntegratorWindow(self,self.sim)
678 _integratorreporter = integwin.run()
679 if _integratorreporter!=None:
680 _integratorreporter.run()
681 self.sim.processVarStatus()
682 self.modelview.refreshtree()
683
684
685 def do_check(self):
686 if not self.sim:
687 self.reporter.reportError("No simulation yet")
688 return
689
690 self.start_waiting("Checking system...")
691
692 try:
693 self.sim.checkInstance()
694 self.reporter.reportWarning("System instance check run, check above for error (if any).")
695 # the above gives output but doesn't throw errors or return a status.
696 # ... this is a problem (at the C level)
697
698 status = self.sim.checkDoF()
699 if status==ascpy.ASCXX_DOF_UNDERSPECIFIED:
700 self.on_show_fixable_variables_activate(None)
701 elif status==ascpy.ASCXX_DOF_OVERSPECIFIED:
702 self.on_show_freeable_variables_activate(None)
703 elif status==ascpy.ASCXX_DOF_STRUCT_SINGULAR:
704 if not self.sim.checkStructuralSingularity():
705 sing = self.sim.getSingularityInfo()
706 title = "Structural singularity"
707 text = title
708 text += "\n\nThe singularity can be reduced by freeing the following variables:"
709 msgs = {
710 "The singularity can be reduced by freeing the following variables" : sing.freeablevars
711 ,"Relations involved in the structural singularity" : sing.rels
712 ,"Variables involved in the structural singularity" : sing.vars
713 }
714 for k,v in msgs.iteritems():
715 text+="\n\n%s:" % k
716 if len(v):
717 _l = [j.getName() for j in v]
718 _l.sort()
719 text+= "\n\t" + "\n\t".join(_l)
720 else:
721 text += "\nnone"
722
723 _dialog = InfoDialog(self,self.window,text,title)
724 _dialog.run()
725
726 self.reporter.reportNote("System DoF check OK")
727
728 except RuntimeError, e:
729 self.stop_waiting()
730 self.reporter.reportError(str(e))
731 return
732
733 self.stop_waiting()
734
735 self.modelview.refreshtree()
736
737 def do_method(self,method):
738 if not self.sim:
739 self.reporter.reportError("No model selected yet")
740
741 self.sim.run(method)
742 self.modelview.refreshtree()
743
744 def do_quit(self):
745 print_loading_status("Saving window location")
746 self.reporter.clearPythonErrorCallback()
747
748 _w,_h = self.window.get_size()
749 _t,_l = self.window.get_position()
750 _display = self.window.get_screen().get_display().get_name()
751 self.prefs.setGeometrySizePosition(_display,"browserwin",_w,_h,_t,_l );
752
753 _p = self.browserpaned.get_position()
754 self.prefs.setGeometryValue(_display,"browserpaned",_p);
755
756 print_loading_status("Saving current directory")
757 self.prefs.setStringPref("Directories","fileopenpath",self.fileopenpath)
758
759 self.prefs.setBoolPref("Browser","auto_solve",self.is_auto)
760
761 print_loading_status("Saving preferences")
762 # causes prefs to be saved unless they are still being used elsewher
763 del(self.prefs)
764
765 print_loading_status("Closing down GTK")
766 gtk.main_quit()
767
768 print_loading_status("Clearing error callback")
769 self.reporter.clearPythonErrorCallback()
770
771 print_loading_status("Quitting")
772 return False
773
774 def on_tools_sparsity_click(self,*args):
775
776 self.reporter.reportNote("Preparing incidence matrix...")
777 _im = self.sim.getIncidenceMatrix();
778
779 self.reporter.reportNote("Plotting incidence matrix...")
780
781 _sp = IncidenceMatrixWindow(_im);
782 _sp.run();
783
784 def on_diagnose_blocks_click(self,*args):
785 try:
786 _bl = self.sim.getActiveBlock()
787 except RuntimeError, e:
788 self.reporter.reportError(str(e))
789 return
790 _db = DiagnoseWindow(self,_bl)
791 _db.run();
792
793 def on_add_observer_click(self,*args):
794 if len(self.observers) > 0:
795 self.reporter.reportError("Not supported: multiple observers")
796 return
797 self.create_observer()
798
799 def on_keep_observed_click(self,*args):
800 if len(self.observers) > 1:
801 self.reporter.reportError("Not supported: multiple observers")
802 return
803 if len(self.observers) <= 0:
804 self.reporter.reportError("No observer defined!")
805 return
806 self.observers[0].do_add_row()
807
808 def on_copy_observer_matrix_click(self,*args):
809 if self.clip == None:
810 self.clip = gtk.Clipboard()
811
812 if len(self.observers) > 1:
813 self.reporter.reportError("Not supported: multiple observers")
814 return
815 if len(self.observers) <= 0:
816 self.reporter.reportError("No observer defined!")
817 return
818 self.observers[0].copy_to_clipboard(self.clip)
819
820 def on_use_relation_sharing_toggle(self,checkmenuitem,*args):
821 _v = checkmenuitem.get_active()
822 self.prefs.setBoolPref("Compiler","use_relation_sharing",_v)
823 self.reporter.reportNote("Relation sharing set to "+str(_v))
824
825 def on_show_solving_popup_toggle(self,checkmenuitem,*args):
826 _v = checkmenuitem.get_active()
827 self.prefs.setBoolPref("SolverReporter","show_popup",_v)
828 print "SET TO",_v
829
830 def on_close_on_converged_toggle(self,checkmenuitem,*args):
831 _v = checkmenuitem.get_active()
832 self.prefs.setBoolPref("SolverReporter","close_on_converged",_v)
833
834 def on_close_on_nonconverged_toggle(self,checkmenuitem,*args):
835 _v = checkmenuitem.get_active()
836 self.prefs.setBoolPref("SolverReporter","close_on_nonconverged",_v)
837
838 def on_show_variables_near_bounds_activate(self,*args):
839 _epsilon = 1e-4;
840 text = "Variables New Bounds"
841 title=text;
842 text += "\n"
843 _vars = self.sim.getVariablesNearBounds(_epsilon)
844 if len(_vars):
845 for _v in _vars:
846 text += "\n%s"%_v.getName()
847 else:
848 text +="\nnone"
849 _dialog = InfoDialog(self,self.window,text,title)
850 _dialog.run()
851
852 # --------------------------------------------
853 # MODULE LIST
854
855 def module_activated(self, treeview, path, column, *args):
856 modules = self.library.getModules()
857 print "PATH",path
858 if len(path)==1:
859 self.reporter.reportNote("Launching of external editor not yet implemented")
860 elif len(path)==2:
861 if(self.modtank.has_key(path)):
862 _type = self.modtank[path];
863 self.reporter.reportNote("Creating simulation for type %s" % str(_type.getName()) )
864 self.do_sim(_type)
865 else:
866 self.reporter.reportError("Didn't find type corresponding to row")
867
868 # ----------------------------------
869 # ERROR PANEL
870
871 def get_error_row_data(self,sev,filename,line,msg):
872 _sevicon = {
873 0: self.iconok
874 ,1: self.iconinfo
875 ,2: self.iconwarning
876 ,3: self.iconerror
877 ,4: self.iconinfo
878 ,5: self.iconwarning
879 ,6: self.iconerror
880 }[sev]
881
882 _fontweight = pango.WEIGHT_NORMAL
883 if sev==6:
884 _fontweight = pango.WEIGHT_BOLD
885
886 _fgcolor = "black"
887 if sev==4:
888 _fgcolor = "#888800"
889 elif sev==5:
890 _fgcolor = "#884400"
891 elif sev==6:
892 _fgcolor = "#880000"
893 elif sev==0:
894 _fgcolor = BROWSER_FIXED_COLOR
895
896 if not filename and not line:
897 _fileline = ""
898 else:
899 if(len(filename) > 25):
900 filename = "..."+filename[-22:]
901 _fileline = filename + ":" + str(line)
902
903 _res = [_sevicon,_fileline,msg.rstrip(),_fgcolor,_fontweight]
904 #print _res
905 return _res
906
907 def error_callback(self,sev,filename,line,msg):
908 pos = self.errorstore.append(None, self.get_error_row_data(sev, filename,line,msg))
909 path = self.errorstore.get_path(pos)
910 col = self.errorview.get_column(3)
911 self.errorview.scroll_to_cell(path,col)
912
913 return 0;
914
915 # --------------------------------
916 # BUTTON METHODS
917
918 def open_click(self,*args):
919 #print_loading_status("CURRENT FILEOPENPATH is",self.fileopenpath)
920 dialog = gtk.FileChooserDialog("Open ASCEND model...",
921 self.window,
922 gtk.FILE_CHOOSER_ACTION_OPEN,
923 (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)
924 )
925 dialog.set_current_folder(self.fileopenpath)
926 dialog.set_default_response(gtk.RESPONSE_OK)
927 dialog.set_transient_for(self.window)
928 dialog.set_modal(True)
929
930 filter = gtk.FileFilter()
931 filter.set_name("*.a4c, *.a4l")
932 filter.add_pattern("*.[Aa]4[Cc]")
933 filter.add_pattern("*.[Aa]4[Ll]")
934 dialog.add_filter(filter)
935
936 filter = gtk.FileFilter()
937 filter.set_name("All files")
938 filter.add_pattern("*")
939 dialog.add_filter(filter)
940
941 response = dialog.run()
942 _filename = dialog.get_filename()
943 print "\nFILENAME SELECTED:",_filename
944
945 _path = dialog.get_current_folder()
946 if _path:
947 self.fileopenpath = _path
948
949 dialog.hide()
950
951 if response == gtk.RESPONSE_OK:
952 self.reporter.reportNote("File %s selected." % dialog.get_filename() )
953 self.library.clear()
954 self.do_open( _filename)
955
956 def reload_click(self,*args):
957 _type = None
958 if(self.sim):
959 _type = self.sim.getType().getName().toString();
960
961 self.library.clear()
962 self.do_open(self.filename)
963
964 if _type:
965 _t = self.library.findType(_type)
966 self.do_sim(_t)
967
968 def props_activate(self,widget,*args):
969 return self.modelview.props_activate(self,widget,*args)
970
971 def observe_activate(self,widget,*args):
972 return self.modelview.observe_activate(self,widget,*args)
973
974 def solve_click(self,*args):
975 #self.reporter.reportError("Solving simulation '" + self.sim.getName().toString() +"'...")
976 self.do_solve()
977
978 def console_click(self,*args):
979 try:
980 console.start(self)
981 except RuntimeError,e:
982 self.reporter.reportError("Unable to start console: "+str(e));
983
984 def integrate_click(self,*args):
985 self.do_integrate()
986
987 def check_click(self,*args):
988 self.do_check()
989 #self.reporter.reportError("CHECK clicked")
990
991 def preferences_click(self,*args):
992 if not self.sim:
993 self.reporter.reportError("No simulation created yet!");
994
995 _paramswin = SolverParametersWindow(self)
996 _paramswin.show()
997
998 def methodrun_click(self,*args):
999 _sel = self.methodsel.get_active_text()
1000 if _sel:
1001 _method = None
1002 _methods = self.sim.getType().getMethods()
1003 for _m in _methods:
1004 if _m.getName()==_sel:
1005 _method = _m
1006 if not _method:
1007 self.reporter.reportError("Method is not valid")
1008 return
1009 self.do_method(_method)
1010 else:
1011 self.reporter.reportError("No method selected")
1012
1013 def auto_toggle(self,button,*args):
1014 self.is_auto = button.get_active()
1015 if hasattr(self,'automenu'):
1016 self.automenu.set_active(self.is_auto)
1017 else:
1018 raise RuntimeError("no automenu")
1019
1020 #if self.is_auto:
1021 # self.reporter.reportSuccess("Auto mode is now ON")
1022 #else:
1023 # self.reporter.reportSuccess("Auto mode is now OFF")
1024
1025 def on_file_quit_click(self,*args):
1026 self.do_quit()
1027
1028 def on_tools_auto_toggle(self,checkmenuitem,*args):
1029 self.is_auto = checkmenuitem.get_active()
1030 self.autotoggle.set_active(self.is_auto)
1031
1032 def on_help_about_click(self,*args):
1033 _xml = gtk.glade.XML(self.glade_file,"aboutdialog")
1034 _about = _xml.get_widget("aboutdialog")
1035 _about.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
1036 _about.set_transient_for(self.window);
1037 _about.set_version(config.VERSION)
1038 _about.run()
1039 _about.destroy()
1040
1041 def on_help_contents_click(self,*args):
1042 _help = Help(HELP_ROOT)
1043 _help.run()
1044
1045 def on_show_fixable_variables_activate(self,*args):
1046 v = self.sim.getFixableVariables()
1047 text = "Fixable Variables"
1048 title = text
1049 text += "\n"
1050 if len(v):
1051 for var in v:
1052 text += "\n%s"%var
1053 else:
1054 text += "\nnone"
1055 _dialog = InfoDialog(self,self.window,text,title)
1056 _dialog.run()
1057
1058 def on_show_freeable_variables_activate(self,*args):
1059 v = self.sim.getFreeableVariables()
1060
1061 text = "Freeable Variables"
1062 title = text
1063 text += "\n"
1064 if len(v):
1065 for var in v:
1066 text += "\n%s" % var
1067 else:
1068 text += "\nnone"
1069 _dialog = InfoDialog(self,self.window,text,title)
1070 _dialog.run()
1071
1072 def on_show_external_functions_activate(self,*args):
1073 v = self.library.getExtMethods()
1074 text = "External Functions"
1075 title = text
1076 text +="\nHere is the list of external functions currently present in"
1077 text +=" the Library:"
1078
1079 if len(v):
1080 for ext in v:
1081 text += "\n\n%s (%d inputs, %d outputs):" % \
1082 (ext.getName(), ext.getNumInputs(), ext.getNumOutputs())
1083 text += "\n%s" % ext.getHelp()
1084 else:
1085 text +="\n\nNone"
1086 _dialog = InfoDialog(self,self.window,text,title)
1087 _dialog.run()
1088
1089 def create_observer(self,name=None):
1090 if name==None:
1091 name="New Observer"
1092
1093 _xml = gtk.glade.XML(self.glade_file,"observervbox");
1094 _label = gtk.Label();
1095 _label.set_text(name)
1096 _tab = self.maintabs.append_page(_xml.get_widget("observervbox"),_label);
1097 self.observers.append(ObserverTab(_xml, name, self, _tab))
1098
1099 def sync_observers(self):
1100 for _o in self.observers:
1101 _o.sync()
1102
1103 def delete_event(self, widget, event):
1104 self.do_quit()
1105 return False
1106
1107 def observe(self,instance):
1108 if len(self.observers) > 1:
1109 self.reporter.reportError("Not implemented: multiple observers (currently %d observers)" %
1110 len(self.observers) )
1111 return
1112 if len(self.observers) ==0:
1113 self.create_observer()
1114 _observer = self.observers[0]
1115 _observer.add_instance(instance)
1116
1117 if __name__ == "__main__":
1118 b = Browser();
1119 b.run()

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