/[ascend]/branches/mike/pygtk/canvas/blocklist.py
ViewVC logotype

Contents of /branches/mike/pygtk/canvas/blocklist.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3107 - (show annotations) (download) (as text)
Mon Jun 6 01:23:53 2016 UTC (6 years ago) by mike
File MIME type: text/x-python
File size: 30797 byte(s)
added undo button (finishing work from Adrian?) and redo (not yet working).
added check for gi version when importing.

1 #!/usr/bin/env python
2
3 if __name__ == '__main__':
4 print "ERROR: ASCEND Canvas should now be invoked using the file 'canvas.py' instead of 'blocklist.py'."
5 exit(1)
6
7 from gi.repository import Gtk, GdkPixbuf, Gdk, GObject
8 import os
9 import os.path
10 import glob
11 import cairo
12 import ascpy
13
14 class BlockIconView(Gtk.IconView):
15 """
16 IconView containing the palette of BlockTypes available for use in the
17 canvas. The list of blocks is supplied currently as an initialisation
18 parameter, but it is intended that this would be dynamic in a final system.
19
20 It should be possible drag icons from the palette into the canvas, but
21 that is not yet implemented.
22 """
23 def __init__(self,blocks=None,app=None):
24 self.model = Gtk.ListStore(str, GdkPixbuf.Pixbuf)
25 self.app = app
26 self.otank = {}
27 try:
28 for b in blocks:
29 pixbuf = b.get_icon(48,48)
30 #print pixbuf
31 iter = self.model.append([b.name, pixbuf])
32 path = self.model.get_path(iter)
33 self.otank[path.to_string()] = b
34 except Exception as e:
35 pass
36
37 Gtk.IconView.__init__(self)
38 self.set_model(self.model)
39 self.set_text_column(0)
40 self.set_pixbuf_column(1)
41 self.set_columns(-1)
42 self.set_size_request(180,100)
43 self.connect("item-activated", self.item_activated)
44 self.connect("selection-changed", self.selection_changed)
45
46 def refresh_view():
47 pass
48
49 def selection_changed(self,iconview):
50 s = self.get_selected_items()
51 if len(s)==1:
52 b = self.otank[s[0].to_string()]
53 self.app.set_placement_tool(b)
54
55 def item_activated(self,iconview, path):
56 self.app.set_placement_tool(self.otank[path])
57
58
59 from gaphas import GtkView, View
60 from gaphas.tool import HoverTool, PlacementTool, HandleTool, ToolChain
61 from gaphas.tool import ItemTool, RubberbandTool, PanTool, ZoomTool
62 from gaphas.painter import ItemPainter
63 from blockconnecttool import BlockConnectTool
64 from blockline import BlockLine
65 from blockitem import DefaultBlockItem, GraphicalBlockItem
66 from contextmenutool import ContextMenuTool
67 from connectortool import ConnectorTool
68 from blockcanvas import BlockCanvas
69 from blockinstance import BlockInstance
70 from solverreporterforcanvas import PopupSolverReporter
71 import canvasproperties
72 import blockproperties
73 import undo
74 from undo import UndoMonitorTool
75 import errorreporter
76 import pickle as pickle
77 # needed to allow cairo.Matrix to be pickled
78 import gaphas.picklers
79 import obrowser
80 import help
81 from preferences import Preferences
82
83 def BlockToolChain():
84 """
85 ToolChain for working with BlockCanvas, including several custom Tools.
86 """
87 chain = ToolChain()
88 chain.append(UndoMonitorTool())
89 chain.append(HoverTool())
90 chain.append(BlockConnectTool()) # for connect/disconnect of lines
91 chain.append(ConnectorTool()) # for creating new lines by drag from Port
92 chain.append(ContextMenuTool()) # right-click
93 # chain.append(LineSegmentTool()) # for moving line 'elbows'
94 chain.append(ItemTool())
95 chain.append(PanTool())
96 chain.append(ZoomTool())
97 chain.append(RubberbandTool())
98 return chain
99
100 class mainWindow(Gtk.Window):
101
102 menu_xml = '''<ui>
103 <menubar name='MenuBar'>
104 <menu action='File'>
105 <menuitem action='New' />
106 <menuitem action='Open' />
107 <menuitem action='Save' />
108 <menuitem action='SaveAs' />
109 <separator />
110 <menuitem action='Export' />
111 <separator />
112 <menuitem action='Library' />
113 <separator />
114 <menuitem action='Quit' />
115 </menu>
116 <menu action='Edit'>
117 <menuitem action='Undo' />
118 <menuitem action='Redo' />
119 <separator />
120 <menuitem action='BlockProperties' />
121 <menuitem action='Delete' />
122 </menu>
123 <menu action='View'>
124 <menuitem action='Fullscreen' />
125 <separator />
126 <menuitem action='ZoomIn' />
127 <menuitem action='ZoomOut' />
128 </menu>
129 <menu action='Tools'>
130 <menuitem action='Debug' />
131 <menuitem action='Run' />
132 <menuitem action='Preview' />
133 </menu>
134 <menu action='Help'>
135 <menuitem action='Development' />
136 <menuitem action='ReportBug' />
137 <menuitem action='About' />
138 </menu>
139 </menubar>
140 </ui>
141 '''
142
143 def __init__(self,library,options=None):
144 """
145 Initialise the application -- create all the widgets, and populate
146 the icon palette.
147 TODO: separate the icon palette into a separate method.
148 """
149 self.ascwrap= library
150 self.errorvars = []
151 self.errorblocks = []
152 self.status = Gtk.Statusbar()
153 self.temp = []
154 self.act = 1 #to be used for storing canvas zoom in/out related data.
155 # the Gaphas canvas
156 canvas = BlockCanvas()
157
158 # the main window
159 Gtk.Window.__init__(self)
160 self.iconok = self.render_icon(Gtk.STOCK_YES,Gtk.IconSize.MENU)
161 self.iconinfo = self.render_icon(Gtk.STOCK_DIALOG_INFO,Gtk.IconSize.MENU)
162 self.iconwarning = self.render_icon(Gtk.STOCK_DIALOG_WARNING,Gtk.IconSize.MENU)
163 self.iconerror = self.render_icon(Gtk.STOCK_DIALOG_ERROR,Gtk.IconSize.MENU)
164
165 self.set_title("ASCEND Canvas Modeller")
166 self.set_default_size(800,650)
167 self.connect("destroy", Gtk.main_quit)
168
169 windowicon = Gtk.Image()
170 windowicon.set_from_file(os.path.join('..','glade','ascend.svg'))
171 self.set_icon(windowicon.get_pixbuf())
172
173 vbox = Gtk.VBox()
174
175 ui_manager = Gtk.UIManager()
176 accelgroup = ui_manager.get_accel_group()
177 self.add_accel_group(accelgroup)
178
179 actiongroup = Gtk.ActionGroup('CanvasActionGroup')
180
181 self.prefs = Preferences()
182
183 actions = [('File', None, '_File')
184 ,('Quit', Gtk.STOCK_QUIT, '_Quit', None,'Quit the Program', self.quit)
185 ,('New', Gtk.STOCK_NEW,'_New',None,'Start a new Simulation', self.new)
186 ,('Open', Gtk.STOCK_OPEN,'_Open',None,'Open a saved Canvas file', self.fileopen)
187 ,('Save', Gtk.STOCK_SAVE,'_Save',None,'Open a saved Canvas file', self.save_canvas)
188 ,('SaveAs', Gtk.STOCK_SAVE_AS,'_Save As...',None,'Open a saved Canvas file', self.filesave)
189 ,('Export', Gtk.STOCK_PRINT, '_Export SVG', None,'Quit the Program', self.export_svg_as)
190 ,('Library', Gtk.STOCK_OPEN, '_Load Library...', '<Control>l','Load Library', self.load_library_dialog)
191 ,('Edit', None, '_Edit')
192 ,('Undo', Gtk.STOCK_UNDO, '_Undo', '<Control>z', 'Undo Previous Action', self.undo_canvas)
193 ,('Redo',Gtk.STOCK_REDO, '_Redo', '<Control>y', 'Redo Previous Undo', self.redo_canvas)
194 ,('BlockProperties',Gtk.STOCK_PROPERTIES, '_Block Properties', None, 'Edit Block Properties', self.bp)
195 ,('Delete', Gtk.STOCK_DELETE, '_Delete', 'Delete', 'Delete Selected Item', self.delblock)
196 ,('View', None, '_View')
197 ,('Fullscreen', Gtk.STOCK_FULLSCREEN, '_Full Screen', 'F11', 'Toggle Full Screen', self.fullscrn)
198 ,('ZoomIn', Gtk.STOCK_ZOOM_IN, '_Zoom In', None, 'Zoom In Canvas', self.zoom)
199 ,('ZoomOut', Gtk.STOCK_ZOOM_OUT, '_Zoom Out', None, 'Zoom Out Canvas', self.zoom)
200 #,('BestFit', Gtk.STOCK_ZOOM_FIT, '_Best Fit', None, 'Best Fit Canvas',self.zoom)
201 ,('Tools', None, '_Tools')
202 ,('Debug', None, '_Debug', None, 'View Instance Browser',self.debug_canvas)
203 ,('Run', Gtk.STOCK_EXECUTE, '_Run', None, 'Solve Canvas', self.run_canvas)
204 ,('Preview', Gtk.STOCK_PRINT_PREVIEW, '_Preview', None, 'Preview Generated Code', self.preview_canvas)
205 ,('Help', None, '_Help')
206 ,('Development', Gtk.STOCK_INFO, '_Development', None, 'Check Development', self.on_get_help_online_click)
207 ,('ReportBug', None, '_Report Bug', None, 'Report a Bug!', self.on_report_a_bug_click)
208 ,('About', Gtk.STOCK_ABOUT, '_About', None, 'About Us', self.about)
209 ]
210
211 actiongroup.add_actions(actions)
212
213 ui_manager.insert_action_group(actiongroup,0)
214 ui_manager.add_ui_from_string(self.menu_xml)
215
216 #Creating Menu Bar
217 menubar = ui_manager.get_widget('/MenuBar')
218 vbox.pack_start(menubar,False,False,0)
219
220 #Creating Tool Bar
221 '''The Toolbar Definations start here'''
222
223 tb = Gtk.Toolbar()
224 tb.set_style(Gtk.ToolbarStyle.BOTH)
225 #Load Button
226 loadbutton = Gtk.ToolButton(Gtk.STOCK_OPEN)
227 loadbutton.set_label("Load")
228 loadbutton.connect("clicked",self.fileopen)
229 tb.insert(loadbutton,0)
230
231 #Save Button
232 savebutton = Gtk.ToolButton(Gtk.STOCK_SAVE)
233 savebutton.set_label("Save")
234 savebutton.connect("clicked",self.save_canvas)
235 tb.insert(savebutton,1)
236
237 # Undo Button
238 undob = Gtk.ToolButton(Gtk.STOCK_UNDO)
239 undob.set_label("Undo")
240 undob.connect("clicked", self.undo_canvas)
241 tb.insert(undob, 2)
242
243 # Redo Button
244 redob = Gtk.ToolButton(Gtk.STOCK_REDO)
245 redob.set_label("Redo")
246 redob.connect("clicked", self.redo_canvas)
247 tb.insert(redob, 3)
248
249 #Debug Button
250 debugbutton = Gtk.ToolButton(Gtk.STOCK_PROPERTIES)
251 debugbutton.set_label("Debug")
252 debugbutton.connect("clicked",self.debug_canvas)
253 tb.insert(debugbutton,4)
254
255 #Preview Button
256 previewb = Gtk.ToolButton(Gtk.STOCK_PRINT_PREVIEW)
257 previewb.set_label("Preview")
258 previewb.connect("clicked",self.preview_canvas)
259 tb.insert(previewb,5)
260
261 #Export Button
262 exportbutton = Gtk.ToolButton(Gtk.STOCK_CONVERT)
263 exportbutton.set_label("Export SVG")
264 exportbutton.connect("clicked",self.export_svg_as)
265 tb.insert(exportbutton,6)
266
267 #Run Button
268 runb = Gtk.ToolButton(Gtk.STOCK_EXECUTE)
269 runb.set_label("Run")
270 runb.connect("clicked",self.run_canvas)
271 tb.insert(runb,7)
272
273 ##Custom Entry
274 #m_entry = Gtk.ToolButton(Gtk.STOCK_SAVE_AS)
275 #m_entry.set_label("Custom METHOD")
276 #m_entry.connect("clicked",self.custommethod)
277 #tb.insert(m_entry,5)
278
279 vbox.pack_start(tb, False, False,0)
280
281 # hbox occupies top part of vbox, with icons on left & canvas on right.
282 paned = Gtk.HPaned()
283
284 # the 'view' widget implemented by Gaphas
285 #gaphas.view.DEBUG_DRAW_BOUNDING_BOX = True
286 self.view = GtkView()
287 # workaround to provide proper adjustments
288 hadj = GObject.ParamSpecObject
289 hadj.name = "hadjustment"
290 self.view.do_set_property(hadj, None)
291
292 self.view.tool = BlockToolChain()
293
294 # table containing scrollbars and main canvas
295 t = Gtk.Table(2,2)
296 self.view.canvas = canvas
297 self.view.zoom(1)
298 self.view.set_size_request(450, 300)
299 hs = Gtk.HScrollbar(self.view.hadjustment)
300 vs = Gtk.VScrollbar(self.view.vadjustment)
301 t.attach(self.view, 0, 1, 0, 1)
302 t.attach(hs, 0, 1, 1, 2, xoptions=Gtk.AttachOptions.FILL, yoptions=Gtk.AttachOptions.FILL)
303 t.attach(vs, 1, 2, 0, 1, xoptions=Gtk.AttachOptions.FILL, yoptions=Gtk.AttachOptions.FILL)
304
305 # a scrolling window to contain the icon palette
306 self.scroll = Gtk.ScrolledWindow()
307 self.scroll.set_border_width(2)
308 self.scroll.set_shadow_type(Gtk.ShadowType.ETCHED_IN)
309
310 self.scroll.set_policy(Gtk.PolicyType.AUTOMATIC,Gtk.PolicyType.AUTOMATIC)
311
312 self.blockiconview = BlockIconView(blocks=self.ascwrap.canvas_blocks,app=self)
313 self.scroll.set_size_request(250, 300)
314 self.scroll.add(self.blockiconview)
315 self.reporter = self.ascwrap.reporter
316
317 paned.pack1(self.scroll, False, False)
318 paned.pack2(t)
319 vbox.pack_start(paned, True, True, 0)
320 vpane = Gtk.VPaned()
321 vpane.pack1(vbox, False, False)
322 lower_vbox = Gtk.VBox()
323
324 self.ET = errorreporter.ErrorReporter( self.reporter,self.iconok,self.iconinfo,self.iconwarning,self.iconerror)
325 self.notebook = Gtk.Notebook()
326 self.notebook.set_tab_pos(Gtk.PositionType.TOP)
327 label = Gtk.Label(label='Error / Status Reporter Console')
328 scrolledwindow = Gtk.ScrolledWindow()
329 scrolledwindow.set_policy(Gtk.PolicyType.AUTOMATIC,Gtk.PolicyType.AUTOMATIC)
330 scrolledwindow.add(self.ET.errorview)
331 self.notebook.append_page(scrolledwindow, label)
332 lower_vbox.pack_start(self.notebook,True, True, 0)
333 lower_vbox.pack_start(self.status, False, False, 0)
334 vpane.pack2(lower_vbox,False,False)
335
336 self.undo_manager = undo.undoManager(self)
337 self.undo_manager.start()
338
339 self.add(vpane)
340 self.show_all()
341
342 @undo.block_observed
343 def set_placement_tool(self,blocktype):
344 """
345 Prepare the canvas for drawing a block of the type selected in the
346 icon palette.
347 """
348 # TODO: add undo handler
349 graphic = blocktype.gr
350 def my_block_factory():
351 def wrapper():
352 b = BlockInstance(blocktype)
353 if len(graphic)==0:
354 bi = DefaultBlockItem(b)
355 else:
356 bi = GraphicalBlockItem(b)
357 self.view.canvas.add(bi)
358 return bi
359 return wrapper
360 self.view.unselect_all()
361 self.view.tool.grab(PlacementTool(self.view,my_block_factory(), HandleTool(), 2))
362 self.status.push(0,"Selected '%s'..." % blocktype.type.getName())
363
364 def set_connector_tool(self,foobar):
365 """
366 Prepare the canvas for drawing a connecting line (note that one can
367 alternatively just drag from a port to another port).
368 """
369
370 def my_line_factory():
371 def wrapper():
372 l = BlockLine()
373 self.view.canvas.add(l)
374 return l
375 return wrapper
376 self.view.tool.grab(PlacementTool(self.view,my_line_factory(), HandleTool(), 1))
377
378 @undo.block_observed
379 def delblock(self, widget = None):
380 '''Both individual and multiple selected items are deleted by the logic of following routine'''
381
382 if self.view.selected_items:
383 while(len(self.view.selected_items)!=0):
384 self.view.canvas.remove(self.view._selected_items.pop())
385 self.status.push(0,"Item deleted.")
386 self.view.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse('#FFF'))
387
388 '''if self.view.focused_item:
389 self.view.canvas.remove(self.view.focused_item)
390 self.status.push(0,"Item deleted.")
391 self.view.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse('#FFF'))'''
392
393
394
395 @undo.block_observed
396 def new(self, widget):
397 for item in self.view.canvas.get_all_items():
398 self.view.canvas.remove(item)
399 self.reporter.reportNote('Canvas cleared for new Model')
400 self.view.canvas.filestate = 0
401 self.view.canvas.filename = None
402 self.view.canvas.canvasmodelstate = 'Unsolved'
403 self.status.push(0,"Canvasmodel state : %s"% self.view.canvas.canvasmodelstate)
404 self.view.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse('#FFF'))
405
406 def debug_canvas(self,widget):
407 """
408 Display an 'object browser' to allow inspection of the objects
409 in the canvas.
410 """
411
412 b = obrowser.Browser("canvas",self.view.canvas, False)
413 self.status.push(0,"Canvasmodel state : %s"% self.view.canvas.canvasmodelstate)
414
415 def save_canvas(self,widget):
416 """
417 Save the canvas in 'pickle' format. Currently saving is jointly handled by both self.save_canvas and self.filesave methods
418 """
419 if not self.view.canvas.filestate:
420 self.filesave(widget)
421 return
422 else:
423 f = file(self.view.canvas.filename,"w")
424 try:
425 pickle.dump(self.view.canvas,f)
426 self.status.push(0,"Canvasmodel saved...")
427 except Exception,e:
428 print "ERROR:",str(e)
429 self.status.push(0,"Canvasmodel could not be saved : " + str(e))
430 b = obrowser.Browser("canvas",self.view.canvas)
431 d = Gtk.Dialog("Error",self,Gtk.DialogFlags.DESTROY_WITH_PARENT,(Gtk.STOCK_OK, Gtk.ResponseType.ACCEPT))
432 d.vbox.add(Gtk.Label(label=str(e)))
433 d.show_all()
434 d.run()
435 d.hide()
436 finally:
437 f.close()
438
439 def load_canvas(self,widget):
440 """
441 Restore a saved canvas in 'pickle' format. Currently not in use as loading now handled by self.fileopen().
442 """
443 f = file("./test.a4b","r")
444 try:
445 self.view.canvas = pickle.load(f)
446 if self.view.canvas.model_library is not None:
447 print "Loading Library...."
448 self.loadlib(self, self.view.canvas.model_library,0)
449 self.view.canvas.reattach_ascend(self.ascwrap.library,self.ascwrap.annodb)
450 self.view.canvas.update_now()
451 self.status.push(0,"Canvasmodel sucessfully loaded...")
452 except Exception,e:
453 self.status.push(0,"Canvasmodel could not be loaded : " + str(e))
454 self.reporter.ReportError("Canvasmodel could not be loaded : " + str(e))
455 self.reporter.ReportNote(" Error occured while attempting to load the file")
456 finally:
457 f.close()
458 self.load_presaved_canvas(None)
459 self.status.push(0,"Canvasmodel state : %s"% self.view.canvas.canvasmodelstate)
460
461
462 def temp_canvas(self,widget):
463 pass
464 """
465 NOTE: re-implementation needed
466 """
467
468 def undo_canvas(self,widget):
469 """
470 Undo
471
472 """
473 self.undo_manager.undo()
474 #pass
475
476 def redo_canvas(self,widget):
477 """
478 NOTE: re-implementation needed
479
480 """
481 self.undo_manager.redo()
482 #pass
483
484 def preview_canvas(self,widget):
485 """
486 Output an ASCEND representation of the canvas on the commandline.
487 Under development.
488 """
489 self.status.push(0,"Canvasmodel state : %s"% self.view.canvas.canvasmodelstate)
490 #info.Info(self.view.parent.parent.parent.parent.parent,str(self.view.canvas),"Canvas Preview").run()
491 canvasproperties.CanvasProperties(self).run()
492
493 def export_svg_as(self,widget):
494
495 f = None
496 dialog = Gtk.FileChooserDialog("Export Canvas As...", self,
497 Gtk.FileChooserAction.SAVE, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
498 dialog.set_default_response(Gtk.ResponseType.OK)
499 filter = Gtk.FileFilter()
500 filter.set_name("Image File")
501 filter.add_pattern("*.svg")
502 dialog.add_filter(filter)
503 dialog.show()
504 response = dialog.run()
505
506 svgview = View(self.view.canvas)
507 svgview.painter = ItemPainter()
508
509 # Update bounding boxes with a temporaly CairoContext
510 # (used for stuff like calculating font metrics)
511 tmpsurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0)
512 tmpcr = cairo.Context(tmpsurface)
513 svgview.update_bounding_box(tmpcr)
514 tmpcr.show_page()
515 tmpsurface.flush()
516
517 if response == Gtk.ResponseType.OK:
518 name = dialog.get_filename()
519 if '.svg' not in name:
520 name += '.svg'
521 if f == None and f != name:
522 f = open(name, 'w')
523 elif f != None and name == f:
524 dialog.set_do_overwrite_confirmation(True)
525 dialog.connect("confirm-overwrite", self.confirm_overwrite_callback)
526
527 fn = name
528 w, h = svgview.bounding_box.width, svgview.bounding_box.height
529 surface = cairo.SVGSurface(fn , w, h)
530 cr = cairo.Context(surface)
531 svgview.matrix.translate(-svgview.bounding_box.x, -svgview.bounding_box.y)
532 svgview.paint(cr)
533 cr.show_page()
534 surface.flush()
535 surface.finish()
536 self.reporter.reportNote(" File ' %s ' saved successfully." % name )
537 self.status.push(0,"Wrote SVG file '%s'." % fn)
538 dialog.destroy()
539
540
541 def export_svg(self,widget):
542 svgview = View(self.view.canvas)
543 svgview.painter = ItemPainter()
544
545 # Update bounding boxes with a temporaly CairoContext
546 # (used for stuff like calculating font metrics)
547 tmpsurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0)
548 tmpcr = cairo.Context(tmpsurface)
549 svgview.update_bounding_box(tmpcr)
550 tmpcr.show_page()
551 tmpsurface.flush()
552
553 fn = 'demo.svg'
554 w, h = svgview.bounding_box.width, svgview.bounding_box.height
555 surface = cairo.SVGSurface(fn , w, h)
556 cr = cairo.Context(surface)
557 svgview.matrix.translate(-svgview.bounding_box.x, -svgview.bounding_box.y)
558 svgview.paint(cr)
559 cr.show_page()
560 surface.flush()
561 surface.finish()
562
563 self.status.push(0,"Wrote SVG file '%s'." % fn)
564
565 #def run_presaved_canvas(self,widget):
566 ##TODO: Separate
567 #if self.view.canvas.saved_model is not None:
568 #model = self.view.canvas.saved_model
569 #self.ascwrap.library.loadString(model,"canvasmodel")
570
571 T = self.ascwrap.library.findType("canvasmodel")
572 M = T.getSimulation('canvassim',True)
573 M.setSolver(ascpy.Solver("QRSlv"))
574 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
575
576 #for item in self.view.canvas.get_all_items():
577 #if hasattr(item, 'blockinstance'):
578 #bi = item.blockinstance
579 #for i in self.ascwrap.library.modules.getChildren()[0].getChildren():
580 #if str(bi.name) == str(i.getName()):
581 #bi.instance = i
582
583 def load_presaved_canvas(self,widget):
584 #TODO: Separate
585 print self.view.canvas.saved_data
586 try:
587 if self.view.canvas.saved_data is not None:
588 model = str(self.view.canvas)
589 self.ascwrap.library.loadString(model,"canvasmodel")
590 T = self.ascwrap.library.findType("canvasmodel")
591 M = T.getSimulation("canvassim",True)
592
593 def assignval(sim_inst, name):
594 if sim_inst.isAtom():
595 try:
596 sim_inst.setRealValue(self.view.canvas.saved_data[name])
597 except Exception,e:
598 print e
599 elif sim_inst.isRelation():
600 pass
601 else:
602 for i in sim_inst.getChildren():
603 k = name+"."+str(i.getName())
604 assignval(i,k)
605
606 for i in M.getChildren()[0].getChildren():
607 assignval(i, str(i.getName()))
608
609 for item in self.view.canvas.get_all_items():
610 if hasattr(item, 'blockinstance'):
611 bi = item.blockinstance
612 for i in M.getChildren()[0].getChildren():
613 if str(bi.name) == str(i.getName()):
614 bi.instance = i
615 self.status.push(0,"Canvasmodel state : %s"% self.view.canvas.canvasmodelstate)
616 except AttributeError:
617 self.status.push(0,"Canvasmodel state : Unsolved")
618
619 def run_canvas(self,widget):
620 #TODO: Separate
621 """
622 Exports canvas to ASCEND solver and attempts to solve it. Also provides realtime feedback.
623 """
624 self.undo_manager.reset()
625 model = str(self.view.canvas)
626 #print model
627 self.view.canvas.saved_model = model
628 self.view.canvas.saved_data = {}
629
630 self.ascwrap.library.loadString(model,"canvasmodel")
631 T = self.ascwrap.library.findType("canvasmodel")
632
633
634 self.M = T.getSimulation("canvassim",True)
635 self.M.setSolver(ascpy.Solver("QRSlv"))
636 self.reporter = ascpy.getReporter()
637 reporter = PopupSolverReporter(self,self.M.getNumVars())
638
639 try:
640 self.M.build()
641 except RuntimeError,e:
642 print "Couldn't build system: %s" % str(e)
643 self.status.push(0,"Couldn't build system: %s" % str(e));
644 return
645 #try:
646 #met=T.getMethod('on_load')
647 #self.M.run(met)
648 #except Exception,e:
649 #print "Couldn't run on_load method: %s"% str(e)
650 #return
651
652 self.status.push(0,"Solving with 'QRSlv'. Please Wait...")
653
654 try:
655 self.M.solve(ascpy.Solver("QRSlv"),reporter)
656 self.reporterrorblock(self.M)
657 except RuntimeError:
658 self.reporterrorblock(self.M)
659
660 for item in self.view.canvas.get_all_items():
661 if hasattr(item, 'blockinstance'):
662 bi = item.blockinstance
663 for i in self.M.getChildren()[0].getChildren():
664 if str(bi.name) == str(i.getName()):
665 bi.instance = i
666
667 #Storing solved variables into a dictionary which will be pickled
668 for i in self.M.getallVariables():
669 self.view.canvas.saved_data[str(i.getName())] = i.getValue()
670
671 def about(self, widget):
672 about = Gtk.AboutDialog()
673 about.set_program_name("ASCEND CANVAS")
674 about.set_version("0.9.6x alpha")
675 about.set_copyright("Carnegie Mellon University")
676 about.set_comments("Canvas - Based GUI Modeller for Energy Systems")
677 about.set_website("http://www.ascend.cheme.cmu.edu")
678 windowicon = Gtk.Image()
679 windowicon.set_from_file(os.path.join("../glade/ascend.svg"))
680 about.set_icon(windowicon.get_pixbuf())
681 about.set_logo(GdkPixbuf.Pixbuf.new_from_file("../glade/ascend.png"))
682 about.set_transient_for(self)
683 about.run()
684 about.destroy()
685
686 def dummy(self, widget):
687 dum = Gtk.MessageDialog(self, Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, "Sorry ! This fuctionality is not implemented yet.")
688 dum.run()
689 dum.destroy()
690
691 def fileopen(self, widget):
692 dialog = Gtk.FileChooserDialog("Open..",self,Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
693 dialog.set_default_response(Gtk.ResponseType.OK)
694 filter = Gtk.FileFilter()
695 filter.set_name("Canvas Files")
696 filter.add_mime_type("Canvas Files/a4b")
697 filter.add_pattern("*.a4b")
698 dialog.add_filter(filter)
699 filter = Gtk.FileFilter()
700 filter.set_name("All files")
701 filter.add_pattern("*")
702 dialog.add_filter(filter)
703
704 res = dialog.run()
705 if res == Gtk.ResponseType.OK:
706 result = dialog.get_filename()
707 self.load_canvas_file(result)
708 dialog.destroy()
709
710 def load_canvas_file(self,filename):
711 #TODO: Separate
712 f = file(filename,"r")
713 try:
714 self.view.canvas = pickle.load(f)
715 if self.view.canvas.model_library is not None:
716 self.loadlib(self, self.view.canvas.model_library)
717 self.view.canvas.reattach_ascend(self.ascwrap.library,self.ascwrap.annodb)
718 self.view.canvas.update_now()
719 self.view.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse('#FFF'))
720 self.load_presaved_canvas(None)
721 self.reporter.reportSuccess(" File %s successfully loaded." % filename)
722 self.status.push(0,"File %s Loaded." % filename)
723 self.view.canvas.filename = filename
724 except Exception,e:
725 self.reporter.reportError(" Error occured while attempting to load the file. File could not be loaded properly.")
726 print e
727 finally:
728 f.close()
729
730 def filesave(self, widget):
731 f = None
732 dialog = Gtk.FileChooserDialog("Save..", self, Gtk.FileChooserAction.SAVE, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
733 dialog.set_default_response(Gtk.ResponseType.OK)
734 if self.view.canvas.filestate == 0:
735 dialog.set_filename("untitled")
736 filter = Gtk.FileFilter()
737 filter.set_name("Canvas File")
738 filter.add_pattern("*.a4b")
739 dialog.add_filter(filter)
740 dialog.show()
741 dialog.set_do_overwrite_confirmation(True)
742 #dialog.connect("confirm-overwrite", self.confirm_overwrite_callback)
743 response = dialog.run()
744 if response == Gtk.ResponseType.OK:
745 name = dialog.get_filename()
746 if '.a4b' not in name:
747 name += '.a4b'
748 if f == None and f != name:
749 f = open(name, 'w')
750 try:
751 pickle.dump(self.view.canvas,f)
752 self.reporter.reportNote(" File ' %s ' saved successfully." % name )
753 self.status.push(0,"CanvasModel Saved.")
754 self.view.canvas.filestate = 1
755 self.view.canvas.filename = name
756 except Exception,e:
757 print "ERROR:",str(e)
758 b = obrowser.Browser("canvas",self.view.canvas)
759 d = Gtk.Dialog("Error",self,Gtk.DialogFlags.DESTROY_WITH_PARENT,(Gtk.STOCK_OK, Gtk.ResponseType.ACCEPT))
760 d.vbox.add(Gtk.Label(label=str(e)))
761 d.show_all()
762 d.run()
763 d.hide()
764 finally:
765 f.close()
766 dialog.destroy()
767
768 def confirm_overwrite_callback(self, widget):
769
770 uri = widget.get_filename()
771 if is_uri_read_only(uri):
772 if user_wants_to_replace_read_only_file (uri):
773 return Gtk.FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME
774 else:
775 return Gtk.FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN
776 else:
777 return Gtk.FILE_CHOOSER_CONFIRMATION_CONFIRM
778 return
779
780
781 def bp(self, widget = None):
782 if self.view.focused_item:
783 blockproperties.BlockProperties(self, self.view.focused_item).run()
784 else:
785 m = Gtk.MessageDialog(self, Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, "No Block was selected! Please select a Block to view its properties.")
786 m.run()
787 m.destroy()
788
789 def reporterrorblock(self, sim):
790 #TODO: Separate
791 self.errortext = "Canvasmodel could NOT be solved \n Blocks which couldnot be solved:"
792
793 def checkifsolved(sim_inst, name):
794 if sim_inst.getType().isRefinedSolverVar():
795 try:
796 #if sim_inst.getStatus() == 2 :
797 # print name + " -> " + str(sim_inst.getStatus())
798 # self.errorvars.append(name)
799 if sim_inst.getStatus() == 3:
800 self.errorvars.append(name)
801
802 except Exception,e:
803 print e
804 elif sim_inst.isRelation():
805 pass
806 else:
807 for i in sim_inst.getChildren():
808 k = name+"."+str(i.getName())
809 checkifsolved(i,k)
810
811 self.errorvars = []
812 self.errorblocks = []
813 self.activevar = []
814 for i in sim.getChildren()[0].getChildren():
815 print i.getName()
816 checkifsolved(i, str(i.getName()))
817
818 print self.errorvars
819 for i in sim.getChildren()[0].getChildren():
820 for j in self.errorvars:
821 if str(i.getName()) in j:
822 if str(i.getName()) in self.errorblocks:
823 pass
824 else:
825 self.errortext += '\n'
826 self.errortext += str(i.getName()) #+' --> ' + ' couldnot be solved'
827 self.errorblocks.append(str(i.getName()))
828 print self.errorblocks
829 self.fill_blocks()
830
831
832 def fill_blocks(self):
833 cr = self.view.canvas._obtain_cairo_context()
834 for item in self.view.canvas.get_all_items():
835 if hasattr(item, 'blockinstance'):
836 bi = item.blockinstance
837 bi.color_r = 1
838 bi.color_g = 1
839 bi.color_b = 1
840 flag = 0
841 for item in self.view.canvas.get_all_items():
842 if hasattr(item, 'blockinstance'):
843 bi = item.blockinstance
844 for i in self.errorblocks:
845 if str(bi.name) == str(i):
846 bi.color_r = .5
847 bi.color_g = 0
848 bi.color_b = 0
849 flag = 1
850
851 print "updating canvas"
852 self.view.canvas.update_now()
853 if flag == 1:
854 self.view.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse('#F88'))
855 flag = 0
856 else:
857 self.view.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse('#AFA'))
858
859 def load_library_dialog(self,widget):
860 #TODO: separate
861 dialog = Gtk.FileChooserDialog('Load Library...',self, Gtk.FileChooserAction.OPEN,(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
862 dialog.set_default_response(Gtk.ResponseType.OK)
863 dialog.set_current_folder(os.path.join(self.ascwrap.defaultlibraryfolder))
864
865 filter = Gtk.FileFilter()
866 filter.set_name("Canvas Files")
867 filter.add_mime_type("Canvas Files/a4c")
868 filter.add_pattern("*.a4c")
869 dialog.add_filter(filter)
870
871 filter = Gtk.FileFilter()
872 filter.set_name("All files")
873 filter.add_pattern("*")
874 dialog.add_filter(filter)
875
876 res = dialog.run()
877 if res == Gtk.ResponseType.OK:
878 result = dialog.get_filename()
879 self.loadlib(lib_name = os.path.basename(result))
880 dialog.destroy()
881
882
883 def loadlib(self, widget = None, lib_name = None):
884 ##TODO: Separate
885 #if loadcondition == 1:
886 #if self.view.canvas.model_library == lib_name:
887 #m = Gtk.MessageDialog(self, Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.WARNING, Gtk.ButtonsType.CLOSE, "The selected Library is already loaded. ")
888 #m.run()
889 #m.destroy()
890 #return
891 #if self.view.canvas.get_all_items():
892 #m = Gtk.MessageDialog(self, Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, "Unable to switch Library ! The canvas contains models from present Model Library. ")
893 #m.run()
894 #m.destroy()
895 #return
896
897 #print lib_path
898 self.ascwrap.load_library(lib_name)
899
900 self.view.canvas.model_library = lib_name
901
902 self.scroll.remove(self.blockiconview)
903 self.blockiconview = BlockIconView(self.ascwrap.canvas_blocks, self)
904 self.scroll.add(self.blockiconview)
905 self.show_all()
906
907 #self.status.push(0, " Library '%s' loaded :: Found %d block types." %(lib_name, (len(blocks))))
908
909 def get_libraries_from_folder(self,path=None):
910 'Returns a dictionary of library names with their paths'
911 if path == None:
912 return
913 libs = {}
914 locs = glob.glob(os.path.join(path,'*.a4c'))
915 for loc in locs:
916 name = loc.strip(path)
917 libs[name] = loc
918 return libs
919
920 def zoom(self, widget):
921 zoom = {'ZoomIn':1.2,'ZoomOut':0.8,'BestFit':1.0}
922 x = zoom[widget.get_name()]
923 self.act = self.act*x
924 self.view.zoom(x)
925
926
927 def fullscrn(self, widget):
928 try:
929 if self.flag == 1:
930 self.unfullscreen()
931 self.flag = 0
932 else:
933 self.fullscreen()
934 self.flag = 1
935 except:
936 self.fullscreen()
937 self.flag = 1
938
939
940 def on_contents_click(self, widget):
941 _help = help.Help(url="http://ascendwiki.cheme.cmu.edu/Canvas-based_modeller_for_ASCEND ")
942 _help.run()
943
944 def on_get_help_online_click(self, widget):
945 _help = help.Help(url="http://ascendwiki.cheme.cmu.edu/Canvas_Development")
946 _help.run()
947
948 def on_report_a_bug_click(self, widget):
949 _help = help.Help(url="http://ascendbugs.cheme.cmu.edu/report/")
950 _help.run()
951
952 def quit(self,args):
953 del(self.prefs)
954 self.destroy()

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