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

Annotation of /trunk/pygtk/gtkbrowser.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3334 - (hide annotations) (download) (as text)
Mon Jan 22 03:19:54 2018 UTC (19 months, 3 weeks ago) by jpye
File MIME type: text/x-python
File size: 51262 byte(s)
remove python debug output

1 johnpye 627 try:
2 jpye 1439 import loading
3     #loading.print_status("Loading PSYCO")
4 johnpye 627 #try:
5     # import psyco
6     # psyco.full()
7     # print "Running with PSYCO optimisation..."
8     #except ImportError:
9     # pass
10 johnpye 279
11 jpye 1439 loading.print_status("Loading python standard libraries")
12 johnpye 463
13 jpye 1427 import pygtk
14     pygtk.require('2.0')
15     import gtk
16 jpye 1738 import gtkexcepthook
17 jpye 1427
18 johnpye 627 import re
19     import urlparse
20     import optparse
21     import platform
22     import sys
23 jpye 2715 import os
24 johnpye 132
25 johnpye 627 if platform.system() != "Windows":
26 johnpye 1286 try:
27 johnpye 1287 import dl
28 johnpye 1286 _dlflags = dl.RTLD_GLOBAL|dl.RTLD_NOW
29     except:
30     # On platforms that unilaterally refuse to provide the 'dl' module
31     # we'll just set the value and see if it works.
32 jpye 1439 loading.print_status("Setting dlopen flags","Python 'dl' module not available on this system")
33 johnpye 1286 _dlflags = 258
34 johnpye 627 # This sets the flags for dlopen used by python so that the symbols in the
35     # ascend library are made available to libraries dlopened within ASCEND:
36 johnpye 1286 sys.setdlopenflags(_dlflags)
37 johnpye 499
38 jpye 1439 loading.print_status("Loading LIBASCEND/ascpy")
39 johnpye 627 import ascpy
40 johnpye 478
41 aakash 2494 loading.print_status("Loading PyGTK, pango")
42 johnpye 478
43 johnpye 627 import pango
44 johnpye 478
45 jpye 1439 loading.load_matplotlib()
46 johnpye 627
47 jpye 1439 loading.print_status("Loading ASCEND python modules")
48 johnpye 627 from preferences import * # loading/saving of .ini options
49     from solverparameters import * # 'solver parameters' window
50     from help import * # viewing help files
51     from incidencematrix import * # incidence/sparsity matrix matplotlib window
52 jpye 1685 from imagewindow import * # image viewer window
53 johnpye 627 from observer import * # observer tab support
54     from properties import * # solver_var properties dialog
55     from varentry import * # for inputting of variables with units
56     from diagnose import * # for diagnosing block non-convergence
57     from solverreporter import * # solver status reporting
58 jpye 1665 from moduleview import * # module browser
59 johnpye 627 from modelview import * # model browser
60 johnpye 732 from integrator import * # integrator dialog
61     from infodialog import * # general-purpose textual information dialog
62 johnpye 904 from versioncheck import * # version check (contacts ascend.cruncher2.dyndns.org)
63 jpye 1698 from unitsdialog import * # general-purpose textual information dialog
64 jpye 2183 from solverhooks import * # solver hooks for use from Python layer
65 johnpye 627 import config
66 johnpye 669
67 jpye 1740 #loading.complete();
68 jpye 1427
69 johnpye 627 except RuntimeError, e:
70     print "ASCEND had problems starting up. Please report the following"
71 jpye 2094 print "error message on ASCEND bug tracker."
72 johnpye 627 print "\n\nFull error message:",str(e)
73     print "\n\nPress ENTER to close this window."
74     sys.stdout.flush()
75     sys.stdin.readline();
76     sys.exit();
77 johnpye 311
78 johnpye 627 except ImportError, e:
79     print "\n\n------------------ ERROR ---------------------"
80 jpye 1761 print "ASCEND had problems importing required Python modules."
81 johnpye 627 print "\nPlease ensure you have all the runtime prerequisites installed."
82     print "Please then report a bug if you continue to have problems."
83     print "\nFull error message:",str(e)
84     if platform.system()=="Windows":
85     print "\nYou will also need to report the contents of any popup error"
86     print "messages from Windows if any were shown."
87     print "\n\nPress ENTER to close this window."
88     sys.stdout.flush()
89     sys.stdin.readline();
90     sys.exit();
91    
92 jpye 1439 loading.print_status("Starting GUI")
93 johnpye 478
94 johnpye 132 # This is my first ever GUI code so please be nice :)
95 johnpye 361 # But I *have* at least read
96     # http://www.joelonsoftware.com/uibook/chapters/fog0000000057.html
97     # and leafed through
98     # http://developer.gnome.org/projects/gup/hig/
99 johnpye 132
100     # The fancy tree-view gizmo is the GtkTreeView object. See the article
101     # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/300304
102     # for the original source code on which my implementation was based.
103    
104 johnpye 227 ESCAPE_KEY = 65307
105    
106 johnpye 230 HELP_ROOT = None
107    
108 johnpye 223 #======================================
109     # Browser is the main ASCEND library/model browser window
110    
111 johnpye 132 class Browser:
112    
113     # ---------------------------------
114     # SETUP
115    
116 johnpye 858 def __init__(self,librarypath=None,assetspath=None):
117    
118     if assetspath==None:
119 jpye 2715 if platform.system()=="Windows":
120     assetspath=os.path.normpath(os.path.join(os.path.dirname(__file__),"..","glade"))
121     else:
122     assetspath=config.PYGTK_ASSETS
123    
124 johnpye 132 #--------
125     # load the file referenced in the command line, if any
126    
127 jpye 1439 loading.print_status("Parsing options","CONFIG = %s"%config.VERSION)
128 jpye 2083
129     #print "Command-line options:",sys.argv
130 johnpye 478
131 johnpye 132 parser = optparse.OptionParser(usage="%prog [[-m typename] file]", version="gtkbrowser $rev$" )
132     # add options here if we want
133    
134 jpye 2083 #print "About to parse..."
135    
136     if platform.system() == "Darwin":
137     parser.add_option("-p", "--silly-mac-thing"
138     ,action="store", type="string", dest="process_number"
139     ,help="Launch Services for Mac passes in a -psn-NNNNN argument that we need to swallow.")
140    
141 johnpye 160 parser.add_option("-m", "--model"
142 johnpye 341 ,action="store", type="string", dest="model"
143 johnpye 160 ,help="specify the model to instantiate upon loading modules")
144 johnpye 436
145     parser.add_option("--pygtk-assets"
146 johnpye 455 ,action="store", type="string", dest="assets_dir"
147 johnpye 436 ,help="override the configuration value for the location of assets"\
148     +" required by PyGTK for the ASCEND GUI, optional"
149 johnpye 858 ,default=assetspath
150 johnpye 436 )
151    
152 johnpye 459 parser.add_option("--library"
153     ,action="store", type="string", dest="library_path"
154 johnpye 723 ,help="override the configuration value for the library path"
155 johnpye 858 ,default=librarypath
156 johnpye 459 )
157    
158 johnpye 723 parser.add_option("--no-auto-sim"
159     ,action="store_false", dest="auto_sim"
160     ,help="disable auto-instantiation of MODEL named as the file stem"
161     ,default=True
162     )
163    
164 sreenatha 2684 parser.add_option("-t", "--test"
165     ,action="store", type="string", dest="test"
166     ,help="load a model and run contained tests without GUI")
167    
168 johnpye 533 (self.options, args) = parser.parse_args()
169 johnpye 132
170 sreenatha 2684 if len(args)>=1:
171     if os.path.isfile(args[0])==False:
172     error = '\033[91mERROR : %s is not a file\033[0m'%args[0]
173     print error
174     sys.exit()
175    
176 johnpye 533 #print "OPTIONS_______________:",self.options
177 johnpye 482
178 johnpye 533 self.assets_dir = self.options.assets_dir
179 jpye 1740 #loading.create_window(self.assets_dir)
180 johnpye 132
181 johnpye 246 self.observers = []
182 aakash 2494 self.currentobservertab = None
183 johnpye 251 self.clip = None
184 johnpye 246
185 johnpye 132 #--------
186     # load up the preferences ini file
187    
188 jpye 1439 loading.print_status("Loading preferences")
189 johnpye 478
190 johnpye 479 self.prefs = Preferences()
191 johnpye 482 _prefpath = self.prefs.getStringPref("Directories","librarypath",None)
192 johnpye 500 _preffileopenpath = self.prefs.getStringPref("Directories","fileopenpath",None)
193 jpye 2175 self.filename = None
194    
195 johnpye 132 #--------
196 johnpye 482 # set up library path and the path to use for File->Open dialogs
197 johnpye 455
198 johnpye 533 if self.options.library_path != None:
199     _path = os.path.abspath(self.options.library_path)
200 johnpye 860 _pathsrc = "command line options"
201 johnpye 1005 # when a special path is specified, use the last path component as the file-open location
202     if platform.system()=="Windows":
203     self.fileopenpath = _path.split(":").pop()
204     else:
205     self.fileopenpath = _path.split(":").pop()
206 johnpye 356 else:
207 johnpye 482 if _prefpath:
208     _path = _prefpath
209     _pathsrc = "user preferences"
210     else:
211 jpye 1615 # default setting, but override with Windows registry if present
212 johnpye 482 _path = config.LIBRARY_PATH
213 johnpye 479 _pathsrc = "default (config.py)"
214 jpye 1615
215     if platform.system()=="Windows":
216     # use the registry
217     try:
218     import _winreg
219     x=_winreg.ConnectRegistry(None,_winreg.HKEY_LOCAL_MACHINE)
220     y= _winreg.OpenKey(x,r"SOFTWARE\ASCEND")
221     _regpath,t = _winreg.QueryValueEx(y,"ASCENDLIBRARY")
222     _winreg.CloseKey(y)
223     _winreg.CloseKey(x)
224     _path = _regpath
225     os.environ['ASCENDLIBRARY'] = _regpath
226     _pathsrc = "Windows registry"
227     except:
228     # otherwise keep using the default
229     pass
230 johnpye 482
231     if _preffileopenpath:
232     self.fileopenpath = _preffileopenpath
233 johnpye 479 else:
234 johnpye 482 self.fileopenpath = _path
235    
236     #--------
237     # Create the ASCXX 'Library' object
238    
239 jpye 1616 loading.print_status("Creating ASCEND 'Library' object","ASCENDLIBRARY = "+_path+" FROM "+_pathsrc)
240 jpye 1463 self.library = ascpy.Library(str(_path))
241 johnpye 479
242 johnpye 132 self.sim = None
243 sreenatha 2684
244     #--------
245     # report absence of solvers if nec.
246 johnpye 351
247 sreenatha 2684 if not len(ascpy.getSolvers()):
248     print "NO SOLVERS LOADED!"
249     self.reporter.reportError( "No solvers were loaded! ASCEND is probably not configured correctly." )
250    
251     #--test option
252     if self.options.test:
253     print '================================================================================'
254     print 'IN TEST'
255     self.test()
256     return
257    
258 johnpye 132 #-------------------
259     # Set up the window and main widget actions
260 johnpye 858 self.glade_file = os.path.join(self.assets_dir,config.GLADE_FILE)
261 johnpye 709
262 jpye 1439 loading.print_status("Setting up windows") #,"GLADE_FILE = %s" % self.glade_file)
263 johnpye 806
264 aakash 2494 builder = gtk.Builder()
265     #builder.add_from_file(self.glade_file)
266     builder.add_objects_from_file(self.glade_file,["integ_icon","browserwin","list_of_td"])
267     self.builder=builder
268     self.window=self.builder.get_object ("browserwin")
269 johnpye 132
270 aakash 2494 self.disable_menu()
271     self.disable_on_first_run()
272 johnpye 132 if not self.window:
273     raise RuntimeError("Couldn't load window from glade file")
274 johnpye 172
275 johnpye 294 _display = self.window.get_screen().get_display().get_name()
276 johnpye 173 _geom=self.prefs.getGeometrySizePosition(_display,"browserwin")
277 johnpye 172 if _geom:
278 johnpye 294 self.window.resize(_geom[0],_geom[1])
279     self.window.move(_geom[2],_geom[3])
280 johnpye 173
281 johnpye 132 self.window.connect("delete_event", self.delete_event)
282    
283 aakash 2494 self.browserpaned=self.builder.get_object ("browserpaned")
284 johnpye 294 _geom2=self.prefs.getGeometryValue(_display,"browserpaned")
285 johnpye 173 if _geom2:
286 johnpye 294 self.browserpaned.set_position(_geom2)
287 johnpye 173
288 jpye 1387 buttons = ["open","reload","solve","integrate","check","methodrun"]
289     for n in buttons:
290     name = "%sbutton"%n
291 aakash 2494 setattr(self,name,self.builder.get_object(name))
292 jpye 1387 getattr(self,name).connect("clicked",getattr(self,"%s_click"%n))
293 johnpye 132
294 aakash 2494 widgets = ["autotoggle","automenu","methodsel","maintabs","lowertabs","consolescroll","statusbar","browsermenu","reloadwarn"]
295 jpye 1387 for n in widgets:
296 aakash 2494 setattr(self,n,self.builder.get_object(n))
297 johnpye 132
298     self.autotoggle.connect("toggled",self.auto_toggle)
299    
300 aakash 2494 self.show_solving_popup=self.builder.get_object("show_solving_popup")
301 johnpye 321 self.show_solving_popup.set_active(self.prefs.getBoolPref("SolverReporter","show_popup",True))
302 aakash 2494 self.close_on_converged=self.builder.get_object("close_on_converged")
303 johnpye 321 self.close_on_converged.set_active(self.prefs.getBoolPref("SolverReporter","close_on_converged",True))
304 aakash 2494 self.close_on_nonconverged=self.builder.get_object("close_on_nonconverged")
305 johnpye 321 self.close_on_nonconverged.set_active(self.prefs.getBoolPref("SolverReporter","close_on_nonconverged",True))
306 aakash 2494 self.solver_engine=self.builder.get_object("solver_engine")
307 jpye 2580 self.recent_files=self.builder.get_object("recentfiles")
308 johnpye 164
309 aakash 2494 self.use_relation_sharing=self.builder.get_object("use_relation_sharing")
310 johnpye 770 self.use_relation_sharing.set_active(self.prefs.getBoolPref("Compiler","use_relation_sharing",True))
311    
312 aakash 2494 self.use_binary_compilation=self.builder.get_object("use_binary_compilation")
313 johnpye 1047 self.use_binary_compilation.set_active(self.prefs.getBoolPref("Compiler","use_binary_compilation",False))
314     self.use_binary_compilation.set_sensitive(self.use_relation_sharing.get_active())
315 aakash 2494
316     self.check_weekly=self.builder.get_object("check_weekly")
317     self.check_weekly.set_active(not(self.prefs.getBoolPref("Browser","disable_auto_check_for_updates",False)))
318 johnpye 1047
319 aakash 2494 self.builder.connect_signals(self)
320 johnpye 688
321 johnpye 533 #-------
322     # Status icons
323 johnpye 164
324 johnpye 268 self.fixedimg = gtk.Image()
325 jpye 2091 _fixedimgpath = os.path.join(self.options.assets_dir,'locked.png')
326 johnpye 268
327 jpye 2091 # this stuff catches some strange environment-variable related problems on Mac OSX.
328     try:
329     if not os.path.exists(_fixedimgpath):
330     raise RuntimeError("Image file '%s' could not be found" % _fixedimgpath)
331     _fixedpixbuf = gtk.gdk.pixbuf_new_from_file(_fixedimgpath)
332     self.fixedimg.set_from_pixbuf(_fixedpixbuf)
333     except Exception,e:
334     raise RuntimeError("Failed to load pixbuf '%s' (%s)" % (_fixedimgpath, str(e)))
335    
336 jpye 1362 self.inactiveimg = gtk.Image()
337     self.inactiveimg.set_from_file(os.path.join(self.options.assets_dir,'unattached.png'))
338    
339 johnpye 258 self.iconstatusunknown = None
340 johnpye 268 self.iconfixed = self.fixedimg.get_pixbuf()
341 johnpye 255 self.iconsolved = self.window.render_icon(gtk.STOCK_YES,gtk.ICON_SIZE_MENU)
342 johnpye 258 self.iconactive = self.window.render_icon(gtk.STOCK_NO,gtk.ICON_SIZE_MENU)
343 jpye 1362 self.iconinactive = self.inactiveimg.get_pixbuf()
344 johnpye 255 self.iconunsolved = None
345    
346     self.statusicons={
347 jpye 1362 ascpy.ASCXX_INST_STATUS_UNKNOWN: self.iconstatusunknown
348 johnpye 463 ,ascpy.ASCXX_VAR_FIXED: self.iconfixed
349     ,ascpy.ASCXX_VAR_SOLVED: self.iconsolved
350     ,ascpy.ASCXX_VAR_ACTIVE: self.iconactive
351     ,ascpy.ASCXX_VAR_UNSOLVED: self.iconunsolved
352 jpye 1362 ,ascpy.ASCXX_REL_INACTIVE: self.iconinactive
353 johnpye 255 }
354 johnpye 533
355    
356 johnpye 273 self.statusmessages={
357 jpye 1362 ascpy.ASCXX_INST_STATUS_UNKNOWN: "Status unknown"
358 johnpye 463 ,ascpy.ASCXX_VAR_FIXED: "Fixed"
359     ,ascpy.ASCXX_VAR_SOLVED: "Converged"
360     ,ascpy.ASCXX_VAR_ACTIVE: "Active (unconverged)"
361     ,ascpy.ASCXX_VAR_UNSOLVED: "Not yet visited"
362 jpye 1362 ,ascpy.ASCXX_REL_INACTIVE: "Inactive"
363 johnpye 533 }
364 johnpye 255
365 johnpye 533 #-------------------
366     # waitwin
367 johnpye 132
368 johnpye 533 self.waitwin = gtk.gdk.Window(self.window.window,
369     gtk.gdk.screen_width(),
370     gtk.gdk.screen_height(),
371     gtk.gdk.WINDOW_CHILD,
372     0,
373     gtk.gdk.INPUT_ONLY)
374 johnpye 226
375 johnpye 533 _cursor = gtk.gdk.Cursor(gtk.gdk.WATCH)
376     self.waitwin.set_cursor(_cursor)
377 johnpye 181
378 johnpye 533 #-------------------
379     # pixbufs to be used in the error listing
380 johnpye 181
381 johnpye 533 self.iconok = self.window.render_icon(gtk.STOCK_YES,gtk.ICON_SIZE_MENU)
382     self.iconinfo = self.window.render_icon(gtk.STOCK_DIALOG_INFO,gtk.ICON_SIZE_MENU)
383     self.iconwarning = self.window.render_icon(gtk.STOCK_DIALOG_WARNING,gtk.ICON_SIZE_MENU)
384     self.iconerror = self.window.render_icon(gtk.STOCK_DIALOG_ERROR,gtk.ICON_SIZE_MENU)
385 johnpye 181
386 johnpye 533 #--------------------
387     # pixbufs for solver_var status
388 johnpye 181
389 johnpye 132 #--------------------
390     # set up the error view
391    
392 aakash 2494 self.errorview = self.builder.get_object("errorview")
393 johnpye 132 errstorecolstypes = [gtk.gdk.Pixbuf,str,str,str,int]
394     self.errorstore = gtk.TreeStore(*errstorecolstypes)
395     errtitles = ["","Location","Message"];
396     self.errorview.set_model(self.errorstore)
397     self.errcols = [ gtk.TreeViewColumn() for _type in errstorecolstypes]
398    
399     i = 0
400     for tvcolumn in self.errcols[:len(errtitles)]:
401     tvcolumn.set_title(errtitles[i])
402     self.errorview.append_column(tvcolumn)
403    
404     if i>0:
405     _renderer = gtk.CellRendererText()
406     tvcolumn.pack_start(_renderer, True)
407     tvcolumn.add_attribute(_renderer, 'text', i)
408     if(i==2):
409     tvcolumn.add_attribute(_renderer, 'foreground', 3)
410     tvcolumn.add_attribute(_renderer, 'weight', 4)
411     else:
412     _renderer1 = gtk.CellRendererPixbuf()
413     tvcolumn.pack_start(_renderer1, False)
414     tvcolumn.add_attribute(_renderer1, 'pixbuf', int(0))
415    
416     i = i + 1
417    
418    
419     #--------------------
420     # set up the error reporter callback
421 johnpye 463 self.reporter = ascpy.getReporter()
422 johnpye 132 self.reporter.setPythonErrorCallback(self.error_callback)
423    
424 johnpye 785
425     #-------
426     # Solver engine list
427    
428 jpye 3308 self.update_solver_list()
429 johnpye 785
430 jpye 2580 #-------
431     # Recent file list
432    
433     self.recent_file_list = gtk.Menu()
434     self.recent_file_list.show()
435     self.recent_files.set_submenu(self.recent_file_list)
436    
437     _max_num = int(self.prefs.getStringPref("recentfiles","max","-1"))
438     if _max_num == -1:
439     self.prefs.setStringPref("recentfiles","max","10")
440    
441     _cur_num = int(self.prefs.getStringPref("recentfiles","cur","-1"))
442     if _cur_num >= 0:
443     for _i in range(_cur_num):
444     _fname = self.prefs.getStringPref("recentfiles","file%s"%(_cur_num -_i - 1),"no recent files")
445     _mi = gtk.MenuItem(_fname,False)
446     _mi.show()
447     _mi.connect("activate",self.on_recent_file_select)
448     self.recent_file_list.append(_mi)
449     else:
450     self.recent_file_list.set_state(gtk.STATE_INSENSITIVE)
451    
452 johnpye 803 _pref_solver = self.prefs.getStringPref("Solver","engine","QRSlv")
453     _mi = self.solver_engine_menu_dict.get(_pref_solver)
454     if _mi:
455     _mi.set_active(1)
456 johnpye 785
457 johnpye 709 #--------
458     # Assign an icon to the main window
459    
460     self.icon = None
461     if config.ICON_EXTENSION:
462     _iconpath = ""
463     try:
464     _icon = gtk.Image()
465 johnpye 858 _iconpath = os.path.join(self.assets_dir,'ascend'+config.ICON_EXTENSION)
466 johnpye 709 _icon.set_from_file(_iconpath)
467     _iconpbuf = _icon.get_pixbuf()
468     self.window.set_icon(_iconpbuf)
469     self.icon = _iconpbuf
470     except Exception, e:
471 johnpye 860 print "FAILED TO SET APPLICATION ICON PATH '%s': %s" % (_iconpath,str(e))
472 johnpye 709 self.reporter.reportError("FAILED to set application icon '%s': %s"
473     % (_iconpath,str(e))
474     )
475    
476 johnpye 132 #-------------------
477     # set up the module view
478    
479     self.modtank = {}
480 aakash 2494 self.moduleview = ModuleView(self, self.builder, self.library)
481 johnpye 132
482     #--------------------
483     # set up the methods combobox
484    
485     self.methodstore = gtk.ListStore(str)
486     self.methodsel.set_model(self.methodstore)
487     _methodrenderer = gtk.CellRendererText()
488     self.methodsel.pack_start(_methodrenderer, True)
489     self.methodsel.add_attribute(_methodrenderer, 'text',0)
490    
491     #--------
492     # set up the instance browser view
493    
494 aakash 2494 self.modelview = ModelView(self, self.builder)
495 johnpye 255
496 johnpye 533 #--------
497 johnpye 856 # set up the tabs
498     self.tabs = {}
499     self.activetab = None # most recent observer tab
500    
501     #--------
502 johnpye 688 # set the state of the 'auto' toggle
503    
504     self.is_auto = self.prefs.getBoolPref("Browser","auto_solve",True)
505     self.autotoggle.set_active(self.is_auto)
506     self.automenu.set_active(self.is_auto)
507 aakash 2494
508     #--------
509     # set the state of the 'warn on reload' toggle
510 johnpye 688
511 aakash 2494 self.reload_warn = self.prefs.getBoolPref("Browser","warn_on_reload",True)
512     self.reloadwarn.set_active(self.reload_warn)
513    
514 johnpye 688 #--------
515 johnpye 869 # tell libascend about this 'browser' object
516    
517 jpye 1749 #print dir(ascpy.Registry())
518 johnpye 869 ascpy.Registry().set("browser",self)
519    
520     #--------
521 jpye 2183 # Set up SolverHooks
522    
523     print "PYTHON: SETTING UP SOLVER HOOKS"
524     self.solverhooks = SolverHooksPythonBrowser(self)
525     ascpy.SolverHooksManager_Instance().setHooks(self.solverhooks)
526    
527     #--------
528 johnpye 533 # options
529 johnpye 132 if(len(args)==1):
530 johnpye 1043 try:
531     self.do_open(args[0])
532     except RuntimeError,e:
533     self.reporter.reportError(str(e))
534     return
535 johnpye 132
536 jpye 1749 #print "Options: ",self.options
537 johnpye 132
538 johnpye 723 _model = None
539 johnpye 533 if self.options.model:
540 johnpye 723 _model = self.options.model
541     print "MODEL: '%s'" % _model
542     elif self.options.auto_sim:
543 johnpye 728 _head, _tail = os.path.split(args[0])
544     if(_tail):
545     _model, _ext = os.path.splitext(_tail)
546 johnpye 723
547     if _model:
548 johnpye 132 try:
549 johnpye 723 _t=self.library.findType(_model)
550 jpye 1733 if not _t.isModel():
551     if self.options.auto_sim:
552     self.reporter.reportError("Won't auto-instantiate with type '%s': not a MODEL." % _model)
553     elif _t.hasParameters():
554     if self.options.auto_sim:
555     self.reporter.reportError("Won't auto-instantiate MODEL %s: model requires parameters." % _model)
556     else:
557     try:
558     self.do_sim(_t)
559     if not self.options.model:
560     self.reporter.reportNote("Instantiated self-titled model '%s'" %_model)
561     except RuntimeError, e:
562     self.reporter.reportError("Failed to create instance of '%s': %s"
563     %(_model, str(e))
564     );
565 johnpye 132 except RuntimeError, e:
566 johnpye 723 if self.options.model:
567     self.reporter.reportError("Unknown model type '%s': %s"
568     %(_model, str(e))
569     );
570 johnpye 223
571 jpye 2183
572 jpye 1600 #--------
573 jpye 1379 # IPython console, if available
574    
575     import console
576     console.create_widget(self)
577    
578 jpye 2446 import locale
579     if locale.localeconv()['decimal_point'] != '.':
580     self.reporter.reportError(
581     """Incompatible locale settings detected. Please set your system local to one which
582     uses '.' as the decimal point separator, such as en_US or en_AU. Note that failing
583     to make this change will cause input and output values to be truncated.
584     For details, see http://ascendbugs.cheme.cmu.edu/view.php?id=337"""
585     )
586    
587 johnpye 132 def run(self):
588 sreenatha 2684 if not self.options.test:
589     #self.window.show()
590     loading.print_status("ASCEND is now running")
591     loading.complete()
592     if self.prefs.getStringPref('Browser','first_run') == None:
593     self.prefs.setStringPref('Browser','first_run',time.time())
594     else:
595     time_now = time.time()
596     first_run_time = float(self.prefs.getStringPref('Browser','first_run'))
597     if ((time_now-first_run_time)/(3600*24)) >= 7:
598     self.auto_update_check()
599     gtk.main()
600 johnpye 132
601 sreenatha 2684 def test(self):
602     print sys.argv[1]
603     print sys.argv[3]
604     if len(sys.argv)==4:
605     ascpy.test_model(str(sys.argv[1]),str(sys.argv[3]))
606     #Call the function at the SWIG API level that runs all the tests and pass to it, the *args
607     #ascpy is accessible here
608    
609    
610 johnpye 785 # ------------------
611     # SOLVER LIST
612    
613 jpye 3308 def update_solver_list(self):
614     self.solver_engine_menu = gtk.Menu()
615     self.solver_engine_menu.show()
616     self.solver_engine.set_submenu(self.solver_engine_menu)
617     self.solver_engine_menu_dict = {}
618     _slvlist = ascpy.getSolvers()
619     _fmi = None
620     for _s in _slvlist:
621     _mi = gtk.RadioMenuItem(_fmi,_s.getName(),False)
622     if _fmi==None:
623     _fmi = _mi
624     _mi.show()
625     _mi.connect('toggled',self.on_select_solver_toggled,_s.getName())
626     self.solver_engine_menu.append(_mi)
627     self.solver_engine_menu_dict[_s.getName()]=_mi
628    
629 johnpye 785 def set_solver(self,solvername):
630 johnpye 900 """ this sets the active solver in the GUI, which is the default applied to newly instantiated models """
631 johnpye 785 self.solver = ascpy.Solver(solvername)
632 johnpye 803 self.prefs.setStringPref("Solver","engine",solvername)
633 johnpye 785 self.reporter.reportNote("Set solver engine to '%s'" % solvername)
634    
635 johnpye 132 # --------------------------------------------
636     # MAJOR GUI COMMANDS
637    
638 johnpye 533 def on_fix_variable_activate(self,*args):
639 jpye 2165 if not self.sim:
640     self.reporter.reportError("No model selected yet")
641     return
642 johnpye 533 self.modelview.on_fix_variable_activate(*args)
643 johnpye 132
644 johnpye 533 def on_free_variable_activate(self,*args):
645 jpye 2165 if not self.sim:
646     self.reporter.reportError("No model selected yet")
647     return
648 johnpye 533 self.modelview.on_free_variable_activate(*args)
649    
650 johnpye 785 def on_select_solver_toggled(self,widget,solvername):
651     if widget.get_active():
652     self.set_solver(solvername)
653    
654 jpye 2580 def on_recent_file_select(self,widget):
655     if widget:
656     # _msg = gtk.MessageDialog(buttons=gtk.BUTTONS_OK,message_format=filename,flags=gtk.DIALOG_MODAL)
657     # _msg.run()
658     self.do_open(widget.get_label())
659    
660     def update_recent_files(self,filename):
661     if filename:
662     _max_num = int(self.prefs.getStringPref("recentfiles","max","10"))
663     _cur_num = int(self.prefs.getStringPref("recentfiles","cur","0"))
664     _should_change = False
665     _temp = None
666     if _cur_num < _max_num:
667     for _i in range(_cur_num):
668     _temp = self.prefs.getStringPref("recentfiles","file%s"%(_i),None)
669     if _temp == filename: #already in the list before
670     _should_change = True
671     if _should_change and _i+1 < _cur_num:
672     _temp = self.prefs.getStringPref("recentfiles","file%s"%(_i+1),None)
673     self.prefs.setStringPref("recentfiles","file%s"%(_i),_temp)
674     if _should_change and _i == _cur_num - 1:
675     self.prefs.setStringPref("recentfiles","file%s"%(_i),filename)
676    
677     if _should_change == False:
678     self.prefs.setStringPref("recentfiles","file%s"%(_cur_num),filename)
679     self.prefs.setStringPref("recentfiles","cur",_cur_num+1)
680     else:
681     for _i in range(_max_num):
682     _temp = self.prefs.getStringPref("recentfiles","file%s"%(_i),None)
683     if _temp == filename: #already in the list before
684     _should_change = True
685     if _should_change and _i+1 < _max_num:
686     _temp = self.prefs.getStringPref("recentfiles","file%s"%(_i+1),None)
687     self.prefs.setStringPref("recentfiles","file%s"%(_i),_temp)
688     if _should_change and _i == _max_num - 1:
689     self.prefs.setStringPref("recentfiles","file%s"%(_i),filename)
690    
691     if _should_change == False: #this is a new file, then remove the oldest one
692     for _i in range(_max_num):
693     if _i+1 < _max_num:
694     _temp = self.prefs.getStringPref("recentfiles","file%s"%(_i+1),None)
695     self.prefs.setStringPref("recentfiles","file%s"%(_i),_temp)
696     else:
697     self.prefs.setStringPref("recentfiles","file%s"%(_i),filename)
698    
699    
700 johnpye 132 def do_open(self,filename):
701     # TODO does the user want to lose their work?
702     # TODO do we need to chdir?
703    
704 johnpye 164 _context = self.statusbar.get_context_id("do_open")
705    
706 johnpye 132 self.errorstore.clear()
707 johnpye 533 self.modelview.clear()
708 aakash 2494 self.currentobservertab = None
709     for _obs in self.observers:
710 sreenatha 2684 if _obs.alive == False:
711     _obs.reloaded = True
712 aakash 2494 _obs.set_dead()
713 johnpye 175 # self.library.clear()
714 jpye 1749 #print "Filename =",filename
715 johnpye 164 self.statusbar.push(_context,"Loading '"+filename+"'")
716 johnpye 1043 try:
717     self.filename = filename
718 aakash 2503 self.disable_menu()
719     self.enable_on_file_open()
720 jpye 1665 # call the low-level 'load' command...
721 johnpye 1043 self.library.load(filename)
722 jpye 2580 self.update_recent_files(filename)
723 johnpye 1043 except RuntimeError,e:
724     self.statusbar.pop(_context)
725     raise
726    
727 johnpye 875 try:
728     self.statusbar.pop(_context)
729     except TypeError,e:
730     print "For some reason, a type error (context=%s,filename=%s): %s" % (_context,filename,e)
731 johnpye 132
732     # Load the current list of modules into self.modules
733 jpye 1665 self.moduleview.refresh(self.library)
734 johnpye 132
735     self.sim = None;
736     self.maintabs.set_current_page(0);
737 johnpye 164
738 johnpye 168 # See http://www.daa.com.au/pipermail/pygtk/2005-October/011303.html
739     # for details on how the 'wait cursor' is done.
740 johnpye 164 def start_waiting(self, message):
741     self.waitcontext = self.statusbar.get_context_id("waiting")
742     self.statusbar.push(self.waitcontext,message)
743    
744     if self.waitwin:
745     self.waitwin.show()
746    
747     while gtk.events_pending():
748     gtk.main_iteration()
749 johnpye 132
750 johnpye 164 def stop_waiting(self):
751     if self.waitwin:
752     self.statusbar.pop(self.waitcontext)
753     self.waitwin.hide()
754    
755 johnpye 132 def do_sim(self, type_object):
756     self.sim = None;
757     # TODO: clear out old simulation first!
758    
759 jpye 1749 #print "DO_SIM(%s)" % str(type_object.getName())
760 johnpye 164 self.start_waiting("Compiling...")
761    
762 johnpye 277 try:
763 johnpye 770 _v = self.prefs.getBoolPref("Compiler","use_relation_sharing",True)
764     ascpy.getCompiler().setUseRelationSharing(_v)
765 jpye 2389 print "Relation sharing set to",_v
766 johnpye 770
767 jpye 2419 _v = self.prefs.getBoolPref("Compiler","use_binary_compilation",False)
768 jpye 2389 ascpy.getCompiler().setBinaryCompilation(_v)
769     print "Binary compilation set to",_v
770 johnpye 1046
771 johnpye 932 self.sim = type_object.getSimulation(str(type_object.getName())+"_sim",False)
772 johnpye 900
773 jpye 1600 #self.reporter.reportNote("SIMULATION ASSIGNED")
774 johnpye 277 except RuntimeError, e:
775     self.stop_waiting()
776     self.reporter.reportError(str(e))
777     return
778    
779 johnpye 164 self.stop_waiting()
780 johnpye 132
781 johnpye 902 # get method names and load them into the GUI
782     self.methodstore.clear()
783     _methods = self.sim.getType().getMethods()
784     _activemethod = None;
785     for _m in _methods:
786     _i = self.methodstore.append([_m.getName()])
787     if _m.getName()=="on_load":
788     self.methodsel.set_active_iter(_i)
789    
790     self.modelview.setSimulation(self.sim)
791    
792     # run the 'on_load' method
793 johnpye 900 self.start_waiting("Running default method...")
794     try:
795 jpye 2493 #self.reporter.reportNote("SIMULATION CREATED, RUNNING DEFAULT METHOD NOW...")
796 johnpye 900 self.sim.runDefaultMethod()
797     except RuntimeError, e:
798     self.stop_waiting()
799     self.reporter.reportError(str(e))
800     return
801     self.stop_waiting()
802 johnpye 903
803     self.modelview.refreshtree()
804 johnpye 249
805     def do_solve_if_auto(self):
806     if self.is_auto:
807 johnpye 775 self.sim.checkInstance()
808 johnpye 249 self.do_solve()
809     else:
810 jpye 2164 try:
811     self.sim.processVarStatus()
812     except RuntimeError,e:
813     self.reporter.reportError(str(e))
814 johnpye 533 self.modelview.refreshtree()
815 johnpye 249
816     self.sync_observers()
817 jpye 2327
818    
819     def no_built_system(self):
820     """ check that the system is 'built', ready for use by the solver. """
821    
822 johnpye 132 if not self.sim:
823     self.reporter.reportError("No model selected yet")
824 jpye 2327 return 1
825 johnpye 132
826 johnpye 919 try:
827     self.sim.build()
828 aakash 2494 self.enable_on_sim_build()
829 johnpye 919 except RuntimeError,e:
830 jpye 1797 self.reporter.reportError("Couldn't build system: %s" % str(e));
831 jpye 2327 return 1
832    
833     return 0;
834    
835     def do_solve(self):
836     if self.no_built_system():
837 johnpye 919 return
838    
839 jpye 1600 if not hasattr(self,'solver'):
840     self.reporter.reportError("No solver assigned!")
841     return
842    
843 johnpye 786 self.start_waiting("Solving with %s..." % self.solver.getName())
844 johnpye 164
845 johnpye 321 if self.prefs.getBoolPref("SolverReporter","show_popup",True):
846 sreenatha 2684 reporter = PopupSolverReporter(self,self.sim)
847 johnpye 321 else:
848     reporter = SimpleSolverReporter(self)
849    
850 johnpye 1035 try:
851     self.sim.solve(self.solver,reporter)
852     except RuntimeError,e:
853     self.reporter.reportError(str(e))
854 johnpye 164
855 johnpye 168 self.stop_waiting()
856 johnpye 255
857 johnpye 533 self.modelview.refreshtree()
858 johnpye 132
859 johnpye 669 def do_integrate(self):
860 jpye 2327 if self.no_built_system():
861 johnpye 669 return
862 johnpye 1069
863     try:
864     self.sim.build()
865     except RuntimeError,e:
866     self.reporter.reportError("Couldn't build system: %s",str(e))
867     return
868 sreenatha 2684 integrator = ascpy.Integrator(self.sim)
869     try:
870     integrator.findIndependentVar()
871     except RuntimeError,e:
872     self.reporter.reportNote(str(e))
873 johnpye 1069
874 johnpye 669 integwin = IntegratorWindow(self,self.sim)
875     _integratorreporter = integwin.run()
876     if _integratorreporter!=None:
877     _integratorreporter.run()
878     self.sim.processVarStatus()
879     self.modelview.refreshtree()
880    
881    
882 johnpye 132 def do_check(self):
883 jpye 2327 if self.no_built_system():
884 johnpye 669 return
885 johnpye 132
886 johnpye 171 self.start_waiting("Checking system...")
887    
888 johnpye 278 try:
889 johnpye 775 self.sim.checkInstance()
890     self.reporter.reportWarning("System instance check run, check above for error (if any).")
891     # the above gives output but doesn't throw errors or return a status.
892     # ... this is a problem (at the C level)
893 johnpye 772
894 johnpye 775 status = self.sim.checkDoF()
895     if status==ascpy.ASCXX_DOF_UNDERSPECIFIED:
896 johnpye 776 self.on_show_fixable_variables_activate(None)
897 johnpye 775 elif status==ascpy.ASCXX_DOF_OVERSPECIFIED:
898 johnpye 776 self.on_show_freeable_variables_activate(None)
899 johnpye 775 elif status==ascpy.ASCXX_DOF_STRUCT_SINGULAR:
900     if not self.sim.checkStructuralSingularity():
901     sing = self.sim.getSingularityInfo()
902     title = "Structural singularity"
903     text = title
904     msgs = {
905     "The singularity can be reduced by freeing the following variables" : sing.freeablevars
906     ,"Relations involved in the structural singularity" : sing.rels
907     ,"Variables involved in the structural singularity" : sing.vars
908     }
909     for k,v in msgs.iteritems():
910     text+="\n\n%s:" % k
911     if len(v):
912     _l = [j.getName() for j in v]
913     _l.sort()
914     text+= "\n\t" + "\n\t".join(_l)
915     else:
916     text += "\nnone"
917 johnpye 772
918 johnpye 775 _dialog = InfoDialog(self,self.window,text,title)
919     _dialog.run()
920 johnpye 1006 else:
921     self.reporter.reportNote("System DoF check OK")
922 johnpye 775
923 johnpye 278 except RuntimeError, e:
924     self.stop_waiting()
925     self.reporter.reportError(str(e))
926     return
927 johnpye 171
928     self.stop_waiting()
929 johnpye 533 self.modelview.refreshtree()
930 johnpye 132
931     def do_method(self,method):
932    
933 johnpye 1029 try:
934     self.sim.run(method)
935     except RuntimeError,e:
936 johnpye 1049 self.reporter.reportError(str(e))
937 johnpye 1029
938 johnpye 917 self.sim.processVarStatus()
939 johnpye 533 self.modelview.refreshtree()
940 johnpye 132
941 johnpye 230 def do_quit(self):
942 jpye 1439 loading.print_status("Saving window location")
943 johnpye 230 self.reporter.clearPythonErrorCallback()
944 johnpye 482
945 johnpye 230 _w,_h = self.window.get_size()
946     _t,_l = self.window.get_position()
947     _display = self.window.get_screen().get_display().get_name()
948     self.prefs.setGeometrySizePosition(_display,"browserwin",_w,_h,_t,_l );
949    
950     _p = self.browserpaned.get_position()
951     self.prefs.setGeometryValue(_display,"browserpaned",_p);
952    
953 jpye 1439 loading.print_status("Saving current directory")
954 johnpye 482 self.prefs.setStringPref("Directories","fileopenpath",self.fileopenpath)
955    
956 johnpye 682 self.prefs.setBoolPref("Browser","auto_solve",self.is_auto)
957    
958 jpye 1439 loading.print_status("Saving preferences")
959 johnpye 230 # causes prefs to be saved unless they are still being used elsewher
960     del(self.prefs)
961    
962 jpye 1439 loading.print_status("Clearing error callback")
963 johnpye 1006 self.reporter.clearPythonErrorCallback()
964    
965 jpye 1439 loading.print_status("Closing down GTK")
966 johnpye 230 gtk.main_quit()
967 johnpye 482
968 jpye 1439 loading.print_status("Clearing library")
969 johnpye 1006 self.library.clear()
970 johnpye 482
971 jpye 1439 loading.print_status("Quitting")
972 johnpye 1006
973 johnpye 230 return False
974    
975 johnpye 231 def on_tools_sparsity_click(self,*args):
976 johnpye 237
977     self.reporter.reportNote("Preparing incidence matrix...")
978 jpye 1911 try:
979     _im = self.sim.getIncidenceMatrix()
980     self.reporter.reportNote("Plotting incidence matrix...")
981     _sp = IncidenceMatrixWindow(_im)
982     _sp.run()
983     except RuntimeError,e:
984     self.reporter.reportError(str(e))
985 johnpye 231
986 jpye 1698 def on_units_click(self,*args):
987 jpye 1711 T = self.modelview.get_selected_type()
988     _un = UnitsDialog(self,T)
989 jpye 1698 _un.run()
990    
991 jpye 1680 def on_tools_incidencegraph_click(self,*args):
992     self.reporter.reportNote("Preparing incidence graph...")
993 jpye 2792 fname = os.tempnam()
994 jpye 1683 self.start_waiting("Creating incidence graph...")
995 jpye 1680 try:
996 jpye 2792 self.sim.write(fname,'dot') # create a PNG file in f
997 jpye 1680 except Exception,e:
998 jpye 1683 self.stop_waiting()
999 jpye 2429 self.reporter.reportError("Failed to create incidence graph: %s" % str(e))
1000 jpye 1765 return
1001 jpye 1683 self.stop_waiting()
1002 jpye 2169 _ig = ImageWindow(self, self.window, fname, title="Incidence Graph", delete=True)
1003 jpye 1680 _ig.run()
1004 jpye 2792 self.reporter.reportNote("Deleted temporary file")
1005 jpye 1680
1006 johnpye 912 def on_tools_repaint_tree_activate(self,*args):
1007     self.reporter.reportNote("Repainting model view...")
1008     self.modelview.refreshtree()
1009    
1010 sreenatha 2684 def on_diagnose_blocks_activate(self,*args):
1011 johnpye 285 try:
1012     _bl = self.sim.getActiveBlock()
1013 jpye 1911 _db = DiagnoseWindow(self,_bl)
1014     _db.run();
1015 johnpye 285 except RuntimeError, e:
1016     self.reporter.reportError(str(e))
1017     return
1018 johnpye 280
1019 johnpye 245 def on_add_observer_click(self,*args):
1020 johnpye 361 self.create_observer()
1021 johnpye 245
1022 johnpye 250 def on_keep_observed_click(self,*args):
1023 johnpye 856 print "KEEPING..."
1024 aakash 2494 if self.currentobservertab is None:
1025     self.reporter.reportError("No observers defined for this model!")
1026 johnpye 250 return
1027 johnpye 856 self.tabs[self.currentobservertab].do_add_row()
1028 johnpye 250
1029     def on_copy_observer_matrix_click(self,*args):
1030 johnpye 251 if self.clip == None:
1031     self.clip = gtk.Clipboard()
1032    
1033 johnpye 250 if len(self.observers) <= 0:
1034     self.reporter.reportError("No observer defined!")
1035     return
1036 aakash 2494 self.tabs[self.currentpage].copy_to_clipboard(self.clip)
1037 johnpye 270
1038 johnpye 770 def on_use_relation_sharing_toggle(self,checkmenuitem,*args):
1039     _v = checkmenuitem.get_active()
1040     self.prefs.setBoolPref("Compiler","use_relation_sharing",_v)
1041     self.reporter.reportNote("Relation sharing set to "+str(_v))
1042 johnpye 1047 self.use_binary_compilation.set_sensitive(_v);
1043 johnpye 770
1044 johnpye 1047 def on_use_binary_compilation_toggle(self,checkmenuitem,*args):
1045     _v = checkmenuitem.get_active()
1046     self.prefs.setBoolPref("Compiler","use_binary_compilation",_v)
1047     self.reporter.reportNote("Binary compilation set to "+str(_v))
1048    
1049 johnpye 321 def on_show_solving_popup_toggle(self,checkmenuitem,*args):
1050     _v = checkmenuitem.get_active()
1051     self.prefs.setBoolPref("SolverReporter","show_popup",_v)
1052    
1053     def on_close_on_converged_toggle(self,checkmenuitem,*args):
1054     _v = checkmenuitem.get_active()
1055     self.prefs.setBoolPref("SolverReporter","close_on_converged",_v)
1056    
1057     def on_close_on_nonconverged_toggle(self,checkmenuitem,*args):
1058     _v = checkmenuitem.get_active()
1059     self.prefs.setBoolPref("SolverReporter","close_on_nonconverged",_v)
1060    
1061 johnpye 328 def on_show_variables_near_bounds_activate(self,*args):
1062     _epsilon = 1e-4;
1063 jpye 1911 try:
1064     _vars = self.sim.getVariablesNearBounds(_epsilon)
1065     except RuntimeError,e:
1066     self.reporter.reportError("Unable to show variables near bounds:\n%s"%str(e))
1067     return
1068 johnpye 895 text = "Variables Near Bounds"
1069 johnpye 732 title=text;
1070     text += "\n"
1071     if len(_vars):
1072     for _v in _vars:
1073     text += "\n%s"%_v.getName()
1074     else:
1075     text +="\nnone"
1076     _dialog = InfoDialog(self,self.window,text,title)
1077     _dialog.run()
1078 johnpye 328
1079 johnpye 1145 def on_show_vars_far_from_nominals_activate(self,*args):
1080 johnpye 1147 _bignum = self.prefs.getRealPref("Browser","far_from_nominals",10);
1081 jpye 1911 try:
1082     _vars = self.sim.getVariablesFarFromNominals(_bignum)
1083     except RuntimeError,e:
1084     self.reporter.reportError("Unable to show variables far from nominals:\n%s"%str(e))
1085     return
1086 johnpye 1145 text = "Variables Far from Nominals"
1087     title=text;
1088     text += "\n"
1089     if len(_vars):
1090     for _v in _vars:
1091     text += "\n%s"%_v.getName()
1092     else:
1093     text +="\nnone"
1094    
1095     text+="\n\nAbove calculated using a relative error of %f" % float(_bignum)
1096 johnpye 1270 text+="\nModify this value in .ascend.ini, section '[Browser]', key 'far_from_nominals'."
1097 johnpye 1145 _dialog = InfoDialog(self,self.window,text,title)
1098     _dialog.run()
1099    
1100 jpye 2568 def on_show_config_activate(self,*args):
1101     text = "Configuration"
1102     title=text;
1103     text += "\n\nEnvironment variables:\n"
1104     if len(os.environ):
1105     for _k,_v in sorted(os.environ.items()):
1106     text += " %s=%s\n" % (_k,_v)
1107     else:
1108     text +="\nempty"
1109    
1110     _dialog = InfoDialog(self,self.window,text,title)
1111     _dialog.run()
1112    
1113    
1114 johnpye 132 # ----------------------------------
1115     # ERROR PANEL
1116    
1117 johnpye 255 def get_error_row_data(self,sev,filename,line,msg):
1118 johnpye 933 try:
1119     _sevicon = {
1120     0 : self.iconok
1121     ,1 : self.iconinfo
1122     ,2 : self.iconwarning
1123     ,4 : self.iconerror
1124     ,8 : self.iconinfo
1125     ,16 : self.iconwarning
1126     ,32 : self.iconerror
1127     ,64 : self.iconerror
1128     }[sev]
1129     except KeyError:
1130     _sevicon = self.iconerror
1131 johnpye 255
1132     _fontweight = pango.WEIGHT_NORMAL
1133 johnpye 933 if sev==32 or sev==64:
1134 johnpye 255 _fontweight = pango.WEIGHT_BOLD
1135    
1136     _fgcolor = "black"
1137 johnpye 933 if sev==8:
1138 johnpye 255 _fgcolor = "#888800"
1139 johnpye 933 elif sev==16:
1140 johnpye 255 _fgcolor = "#884400"
1141 johnpye 933 elif sev==32 or sev==64:
1142 johnpye 255 _fgcolor = "#880000"
1143     elif sev==0:
1144     _fgcolor = BROWSER_FIXED_COLOR
1145    
1146     if not filename and not line:
1147     _fileline = ""
1148     else:
1149     if(len(filename) > 25):
1150     filename = "..."+filename[-22:]
1151     _fileline = filename + ":" + str(line)
1152    
1153 jpye 3334 #print "Creating error row data with MSG = '%s'"%(msg.rstrip())
1154 jpye 3258
1155 johnpye 933 _res = (_sevicon,_fileline,msg.rstrip(),_fgcolor,_fontweight)
1156 johnpye 255 #print _res
1157     return _res
1158    
1159 johnpye 132 def error_callback(self,sev,filename,line,msg):
1160 johnpye 933 #print "SEV =",sev
1161     #print "FILENAME =",filename
1162     #print "LINE =",line
1163 jpye 3334 #print "PYTHON error_callback: MSG =",msg
1164 johnpye 933 pos = self.errorstore.append(None, self.get_error_row_data(sev, filename,line,msg))
1165     path = self.errorstore.get_path(pos)
1166     col = self.errorview.get_column(3)
1167 jpye 3258 self.errorview.scroll_to_cell(path,col)
1168 johnpye 132 return 0;
1169    
1170     # --------------------------------
1171     # BUTTON METHODS
1172    
1173     def open_click(self,*args):
1174 jpye 1439 #loading.print_status("CURRENT FILEOPENPATH is",self.fileopenpath)
1175 johnpye 482 dialog = gtk.FileChooserDialog("Open ASCEND model...",
1176     self.window,
1177     gtk.FILE_CHOOSER_ACTION_OPEN,
1178     (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)
1179     )
1180     dialog.set_current_folder(self.fileopenpath)
1181 johnpye 132 dialog.set_default_response(gtk.RESPONSE_OK)
1182 johnpye 329 dialog.set_transient_for(self.window)
1183     dialog.set_modal(True)
1184 johnpye 132
1185     filter = gtk.FileFilter()
1186     filter.set_name("*.a4c, *.a4l")
1187     filter.add_pattern("*.[Aa]4[Cc]")
1188     filter.add_pattern("*.[Aa]4[Ll]")
1189     dialog.add_filter(filter)
1190    
1191     filter = gtk.FileFilter()
1192     filter.set_name("All files")
1193     filter.add_pattern("*")
1194     dialog.add_filter(filter)
1195    
1196     response = dialog.run()
1197     _filename = dialog.get_filename()
1198 johnpye 669 print "\nFILENAME SELECTED:",_filename
1199 johnpye 482
1200     _path = dialog.get_current_folder()
1201     if _path:
1202     self.fileopenpath = _path
1203    
1204 johnpye 329 dialog.hide()
1205 johnpye 132
1206     if response == gtk.RESPONSE_OK:
1207     self.reporter.reportNote("File %s selected." % dialog.get_filename() )
1208 johnpye 185 self.library.clear()
1209 jpye 1911 try:
1210     self.do_open( _filename)
1211     except RuntimeError,e:
1212     self.reporter.reportError(str(e))
1213 sreenatha 2684
1214 aakash 2494 def on_reloadwarn_toggled(self,*args):
1215     self.prefs.setBoolPref("Browser","warn_on_reload",self.reloadwarn.get_active())
1216 johnpye 132
1217     def reload_click(self,*args):
1218     _type = None
1219 jpye 2175
1220     if not self.filename:
1221     self.reporter.reportError("No file loaded yet!")
1222 jpye 2160 return
1223 johnpye 132
1224 jpye 2175 if self.sim:
1225     _type = self.sim.getType().getName().toString();
1226 aakash 2494
1227     if self.reloadwarn.get_active() and self.currentobservertab is not None:
1228     _alertwin = ReloadDialog(self)
1229     _reload = _alertwin.run()
1230     if _reload is False:
1231     return
1232 johnpye 185 self.library.clear()
1233 johnpye 132
1234 johnpye 1043 try:
1235     self.do_open(self.filename)
1236     if _type:
1237     _t = self.library.findType(_type)
1238     self.do_sim(_t)
1239     except RuntimeError,e:
1240     self.reporter.reportError(str(e))
1241    
1242 johnpye 533 def props_activate(self,widget,*args):
1243     return self.modelview.props_activate(self,widget,*args)
1244    
1245     def observe_activate(self,widget,*args):
1246     return self.modelview.observe_activate(self,widget,*args)
1247    
1248 johnpye 132 def solve_click(self,*args):
1249     #self.reporter.reportError("Solving simulation '" + self.sim.getName().toString() +"'...")
1250     self.do_solve()
1251 johnpye 669
1252     def console_click(self,*args):
1253 jpye 1379 self.lowertabs.set_current_page(1)
1254 jpye 1381 self.consoletext.grab_focus()
1255 johnpye 669
1256     def integrate_click(self,*args):
1257     self.do_integrate()
1258 johnpye 132
1259     def check_click(self,*args):
1260     self.do_check()
1261     #self.reporter.reportError("CHECK clicked")
1262    
1263 johnpye 208 def preferences_click(self,*args):
1264     if not self.sim:
1265 johnpye 919 self.reporter.reportError("No simulation created yet!");
1266     self.sim.setSolver(self.solver)
1267 johnpye 1140 _params = self.sim.getParameters()
1268     _paramswin = SolverParametersWindow(
1269     browser=self
1270     ,params=_params
1271     ,name=self.solver.getName()
1272     )
1273     if _paramswin.run() == gtk.RESPONSE_OK:
1274     print "PARAMS UPDATED"
1275     self.sim.setParameters(_paramswin.params)
1276     else:
1277     print "PARAMS NOT UPDATED"
1278 johnpye 221
1279 johnpye 132 def methodrun_click(self,*args):
1280     _sel = self.methodsel.get_active_text()
1281     if _sel:
1282     _method = None
1283     _methods = self.sim.getType().getMethods()
1284     for _m in _methods:
1285     if _m.getName()==_sel:
1286     _method = _m
1287     if not _method:
1288     self.reporter.reportError("Method is not valid")
1289     return
1290     self.do_method(_method)
1291     else:
1292     self.reporter.reportError("No method selected")
1293    
1294     def auto_toggle(self,button,*args):
1295 johnpye 230 self.is_auto = button.get_active()
1296 johnpye 688 if hasattr(self,'automenu'):
1297     self.automenu.set_active(self.is_auto)
1298 johnpye 132 else:
1299 johnpye 688 raise RuntimeError("no automenu")
1300 johnpye 132
1301 johnpye 688 #if self.is_auto:
1302     # self.reporter.reportSuccess("Auto mode is now ON")
1303     #else:
1304     # self.reporter.reportSuccess("Auto mode is now OFF")
1305    
1306 johnpye 230 def on_file_quit_click(self,*args):
1307 sreenatha 2684 #self.exit_popup()
1308 johnpye 230 self.do_quit()
1309    
1310     def on_tools_auto_toggle(self,checkmenuitem,*args):
1311     self.is_auto = checkmenuitem.get_active()
1312     self.autotoggle.set_active(self.is_auto)
1313    
1314     def on_help_about_click(self,*args):
1315 aakash 2494
1316     self.builder.add_objects_from_file(self.glade_file,["aboutdialog"])
1317     _about = self.builder.get_object("aboutdialog")
1318 johnpye 328 _about.set_transient_for(self.window);
1319 johnpye 478 _about.set_version(config.VERSION)
1320 johnpye 307 _about.run()
1321     _about.destroy()
1322 johnpye 230
1323     def on_help_contents_click(self,*args):
1324     _help = Help(HELP_ROOT)
1325     _help.run()
1326    
1327 jpye 1429 def on_report_a_bug_click(self,*args):
1328     import urllib
1329     import platform
1330     _plat = str(platform.system())
1331     _version = config.VERSION
1332     _help = Help(
1333 aakash 2489 url="http://bugs.ascend4.org/bug_report_page.php?project_id=ascend&platform=%s&product_version=%s"
1334 jpye 1429 % (_plat,_version)
1335     )
1336     _help.run()
1337    
1338 johnpye 904 def on_help_check_for_updates_click(self,*args):
1339     v = VersionCheck()
1340     title = "Check for updates"
1341     text = "Your version is %s\n" % config.VERSION
1342 aakash 2494 self.prefs.setStringPref("Browser","last_update_check","%s" %time.time())
1343    
1344 johnpye 905 try:
1345     v.check()
1346 johnpye 965 if config.VERSION==v.latest:
1347     text += "You are running the latest released version"
1348     else:
1349     text += "Latest version is %s\n" % v.latest
1350     if v.info:
1351     text += "Get more info at %s\n" % v.info
1352     if v.download:
1353     text += "Download from %s\n" % v.download
1354 johnpye 905 except Exception, e:
1355     text += "\nUnable to check version\n"
1356     text += str(e)
1357 johnpye 904
1358     _dialog = InfoDialog(self,self.window,text,title)
1359     _dialog.run()
1360    
1361 johnpye 775 def on_show_fixable_variables_activate(self,*args):
1362 johnpye 1117 try:
1363     v = self.sim.getFixableVariables()
1364 jpye 1911 except Exception,e:
1365     self.reporter.reportError("Unable to show fixable variables: %s"%str(e))
1366     return
1367 johnpye 732 text = "Fixable Variables"
1368 johnpye 750 title = text
1369 johnpye 732 text += "\n"
1370     if len(v):
1371     for var in v:
1372     text += "\n%s"%var
1373     else:
1374     text += "\nnone"
1375     _dialog = InfoDialog(self,self.window,text,title)
1376     _dialog.run()
1377 johnpye 290
1378 johnpye 1117 def on_show_fixed_vars_activate(self,*args):
1379     try:
1380     v = self.sim.getFixedVariables()
1381     except RuntimeError,e:
1382 jpye 1911 self.reporter.reportError("Unable to show fixed variables: %s"%str(e))
1383     return
1384 jpye 2047 text = "%d fixed variables:" % len(v)
1385     title = "Fixed Variables"
1386 jpye 1911 text += "\n"
1387     if len(v):
1388     for var in v:
1389 jpye 2004 text += "\n%s\t= %f"%(str(var),var.getValue())
1390 jpye 1911 else:
1391     text += "\nnone"
1392 jpye 2004 _dialog = InfoDialog(self,self.window,text,title,tabs=[100,200])
1393 jpye 1911 _dialog.run()
1394 johnpye 1117
1395 johnpye 775 def on_show_freeable_variables_activate(self,*args):
1396 johnpye 1117 try:
1397     v = self.sim.getFreeableVariables()
1398     except RuntimeError,e:
1399 jpye 1911 self.reporter.reportError("Unable to show freeable variables: %s"%str(e))
1400     return
1401 johnpye 775 text = "Freeable Variables"
1402     title = text
1403     text += "\n"
1404     if len(v):
1405     for var in v:
1406     text += "\n%s" % var
1407     else:
1408     text += "\nnone"
1409     _dialog = InfoDialog(self,self.window,text,title)
1410     _dialog.run()
1411    
1412 johnpye 750 def on_show_external_functions_activate(self,*args):
1413     v = self.library.getExtMethods()
1414     text = "External Functions"
1415     title = text
1416     text +="\nHere is the list of external functions currently present in"
1417     text +=" the Library:"
1418    
1419     if len(v):
1420     for ext in v:
1421     text += "\n\n%s (%d inputs, %d outputs):" % \
1422     (ext.getName(), ext.getNumInputs(), ext.getNumOutputs())
1423     text += "\n%s" % ext.getHelp()
1424     else:
1425     text +="\n\nNone"
1426     _dialog = InfoDialog(self,self.window,text,title)
1427     _dialog.run()
1428    
1429 jpye 1927 def on_notes_view_activate(self,*args):
1430     t = None
1431     try:
1432     D = self.library.getAnnotationDatabase()
1433     i = self.modelview.get_selected_instance()
1434 jpye 2054 if i and i.isModel():
1435 jpye 1927 t = i.getType()
1436     else:
1437     self.reporter.reportError("First select a MODEL instance");
1438     return
1439     v = D.getNotes(t)
1440     except RuntimeError,e:
1441     self.reporter.reportError("Unable to show notes: %s"%str(e))
1442     return
1443     text = "Notes for '%s'" % t.getName()
1444     title = text
1445     text += "\nHere are all notes defined within this MODEL:"
1446     nn = {}
1447     if len(v):
1448     for n in v:
1449     text += "\n\n%s (%s):" % (n.getId(), n.getLanguage())
1450     text += "\n\t%s" % n.getText()
1451     else:
1452     text += "\n\nThere are no noted defined locally within in this model"
1453    
1454     _dialog = InfoDialog(self,self.window,text,title)
1455     _dialog.run()
1456    
1457 johnpye 856 def on_maintabs_switch_page(self,notebook,page,pagenum):
1458 jpye 1749 #print("Page switched to %d" % pagenum)
1459 aakash 2494 if (pagenum in self.tabs.keys()) and self.tabs[pagenum].alive:
1460 johnpye 856 self.currentobservertab = pagenum
1461 aakash 2494 self.currentpage = pagenum
1462     else:
1463     self.currentpage = pagenum
1464     if pagenum == 1:
1465     self.enable_on_enter_sim_tab()
1466     self.modelview.modelview.grab_focus()
1467     else:
1468     self.disable_on_leave_sim_tab()
1469 johnpye 856
1470 johnpye 245 def create_observer(self,name=None):
1471 aakash 2494 _imagelist = []
1472     for i in range(5):
1473     _imagelist.append("image%s" % (i+7))
1474     self.builder.add_objects_from_file(self.glade_file, _imagelist)
1475    
1476     self.builder.add_objects_from_file(self.glade_file,["observervbox","observercontext"])
1477    
1478 johnpye 245 _label = gtk.Label();
1479 aakash 2494 _tab = self.maintabs.append_page(self.builder.get_object("observervbox"),_label);
1480     _obs = ObserverTab(name=name, browser=self, tab=_tab)
1481 johnpye 849 _label.set_text(_obs.name)
1482     self.observers.append(_obs)
1483 johnpye 856 self.tabs[_tab] = _obs
1484     self.currentobservertab = _tab
1485 aakash 2494
1486     self.builder.get_object("copy_observer_matrix").set_sensitive(True)
1487     self.builder.get_object("keep_observed").set_sensitive(True)
1488 johnpye 849 return _obs
1489 johnpye 249
1490     def sync_observers(self):
1491     for _o in self.observers:
1492     _o.sync()
1493 johnpye 533
1494     def delete_event(self, widget, event):
1495 sreenatha 2684 self.do_quit()
1496 johnpye 533 return False
1497 sreenatha 2684 #return self.exit_popup()
1498 johnpye 249
1499 sreenatha 2684 def exit_popup(self):
1500     dialog = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO,
1501     gtk.BUTTONS_YES_NO, "Are you sure you want to Quit?")
1502     dialog.set_title("Exit popup")
1503     #dialog.set_default_size(600,300)
1504     response = dialog.run()
1505     dialog.destroy()
1506     if response == gtk.RESPONSE_YES:
1507     self.do_quit()
1508     return False
1509     else:
1510     return True
1511    
1512 johnpye 533 def observe(self,instance):
1513 aakash 2494 if self.currentobservertab is None:
1514 johnpye 246 self.create_observer()
1515 johnpye 856 _observer = self.tabs[self.currentobservertab]
1516 johnpye 533 _observer.add_instance(instance)
1517 aakash 2494
1518     def auto_update_check(self):
1519     _p = self.prefs
1520     _no_auto_check = _p.getBoolPref("Browser", "disable_auto_check_for_updates", False)
1521    
1522     if _no_auto_check is True:
1523     return
1524    
1525     _time_now = time.time()
1526     _last_update = float(self.prefs.getStringPref("Browser","last_update_check","0"))
1527     print "Time since last update check : %f days" %((_time_now-_last_update)/(3600*24))
1528    
1529     if ((_time_now-_last_update)/(3600*24)) < 7:
1530     return
1531    
1532     _win = AutoUpdateDialog(self)
1533     _check_now = _win.run()
1534    
1535     if _check_now is False:
1536     return
1537    
1538     self.on_help_check_for_updates_click()
1539    
1540     def on_check_weekly_toggled(self, widget):
1541     _p = self.prefs
1542     _p.setBoolPref("Browser", "disable_auto_check_for_updates", not(widget.get_active()))
1543    
1544     def disable_menu(self):
1545     list=["free_variable","fix_variable","sparsity","propsmenuitem","copy_observer_matrix",
1546     "incidencegraph","diagnose_blocks","show_fixed_vars","show_freeable_vars",
1547     "show_fixable_variables","show_variables_near_bounds","show_vars_far_from_nominals1",
1548     "repaint_tree","checkbutton","solvebutton","integratebutton","methodrunbutton",
1549 aakash 2503 "check1","solve1","integrate1","units","add_observer","keep_observed","preferences","notes_view"]
1550 aakash 2494 for button in list:
1551     self.builder.get_object(button).set_sensitive(False)
1552    
1553     def disable_on_leave_sim_tab(self):
1554     list =["free_variable","fix_variable","propsmenuitem","units"]
1555     for button in list:
1556     self.builder.get_object(button).set_sensitive(False)
1557    
1558     def enable_on_enter_sim_tab(self):
1559     list =["free_variable","fix_variable","propsmenuitem","units"]
1560     for button in list:
1561     self.builder.get_object(button).set_sensitive(False)
1562     if hasattr(self.modelview,'sim'):
1563     _path, _col = self.modelview.modelview.get_cursor()
1564     _instance = None
1565     if _path:
1566     _name,_instance = self.modelview.otank[_path]
1567     if _instance is None:
1568     return
1569     if _instance.isReal():
1570     self.builder.get_object("units").set_sensitive(True)
1571     if _instance.getType().isRefinedSolverVar():
1572     self.builder.get_object("propsmenuitem").set_sensitive(True)
1573     if _instance.isFixed():
1574     self.builder.get_object("free_variable").set_sensitive(True)
1575     else:
1576 aakash 2502 self.builder.get_object("fix_variable").set_sensitive(True)
1577     elif _instance.isRelation():
1578     self.builder.get_object("propsmenuitem").set_sensitive(True)
1579    
1580 aakash 2494 def enable_on_sim_build(self):
1581     list=["sparsity","incidencegraph","diagnose_blocks","show_fixed_vars","show_freeable_vars",
1582 aakash 2503 "show_fixable_variables","show_variables_near_bounds","show_vars_far_from_nominals1","notes_view"]
1583 aakash 2494 for button in list:
1584     self.builder.get_object(button).set_sensitive(True)
1585     def disable_on_first_run(self):
1586     list=["reloadbutton","reload","show_external_functions","notes_view"]
1587     for button in list:
1588     self.builder.get_object(button).set_sensitive(False)
1589     def enable_on_file_open(self):
1590 aakash 2503 list=["reloadbutton","reload","show_external_functions"]
1591 aakash 2494 for button in list:
1592     self.builder.get_object(button).set_sensitive(True)
1593     def enable_on_model_tree_build(self):
1594     list=["repaint_tree","checkbutton","solvebutton","integratebutton","methodrunbutton",
1595 aakash 2503 "check1","solve1","integrate1","units","add_observer","preferences","notes_view"]
1596 aakash 2494 for button in list:
1597     self.builder.get_object(button).set_sensitive(True)
1598 johnpye 478 if __name__ == "__main__":
1599 johnpye 132 b = Browser();
1600     b.run()
1601 aakash 2494
1602     class ReloadDialog:
1603    
1604     # Just a dialog to confirm that the user REALLY
1605     # wants to reload the model
1606    
1607     def __init__(self, browser):
1608     browser.builder.add_objects_from_file(browser.glade_file, ["reloaddialog"])
1609     self.alertwin = browser.builder.get_object("reloaddialog")
1610     browser.builder.connect_signals(self)
1611    
1612     def on_reloaddialog_close(self,*args):
1613     self.alertwin.response(gtk.RESPONSE_CLOSE)
1614    
1615     def run(self):
1616     _continue = True
1617     while _continue:
1618     _res = self.alertwin.run()
1619     if _res == gtk.RESPONSE_YES:
1620     self.alertwin.destroy()
1621     return True
1622     else:
1623     self.alertwin.destroy()
1624     return False
1625    
1626     class AutoUpdateDialog:
1627    
1628     # A dialog to automatically check for updates
1629    
1630     def __init__(self, browser):
1631     self.browser = browser
1632     browser.builder.add_objects_from_file(browser.glade_file, ["autoupdatedialog"])
1633     self.win = browser.builder.get_object("autoupdatedialog")
1634     self.checkbutton = browser.builder.get_object("autoupdate")
1635    
1636     _p = self.browser.prefs
1637     self.checkbutton.set_active(_p.getBoolPref("Browser", "disable_auto_check_for_updates", False))
1638    
1639     browser.builder.connect_signals(self)
1640    
1641     def on_autoupdate_toggled(self, widget):
1642     _p = self.browser.prefs
1643     _p.setBoolPref("Browser", "disable_auto_check_for_updates", widget.get_active())
1644 jpye 2521 self.browser.check_weekly.set_active(not(widget.get_active()))
1645 aakash 2494
1646     def run(self):
1647     _continue = True
1648     while _continue:
1649     _res = self.win.run()
1650     if _res == gtk.RESPONSE_YES:
1651     self.win.destroy()
1652     return True
1653     elif _res == gtk.RESPONSE_NO or _res == gtk.RESPONSE_DELETE_EVENT or _res == gtk.RESPONSE_CLOSE:
1654     self.win.destroy()
1655     return False
1656 sreenatha 2684

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