/[ascend]/trunk/pygtk/canvas/blockproperties.py
ViewVC logotype

Contents of /trunk/pygtk/canvas/blockproperties.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2200 - (show annotations) (download) (as text)
Thu May 20 05:17:35 2010 UTC (8 years, 6 months ago) by grivan
File MIME type: text/x-python
File size: 8894 byte(s)
Adding patch from Grivan, attempt to get code running with latest git master version of Gaphas. Submitted by John.
1 #!/usr/bin/env python
2
3 import gtk, gobject, os, pango, re
4 import blockinstance
5 import preferences
6 import gtksourceview2 as gtksourceview
7
8 UNITS_RE = re.compile("([-+]?(\d+(\.\d*)?|\d*\.d+)([eE][-+]?\d+)?)\s*(.*)");
9
10 class BlockProperties:
11 def __init__(self, parent, item, tab=None):
12 bi = item.blockinstance
13
14 icons = gtk.IconFactory()
15
16 _fixedicon = gtk.Image()
17 _fixedicon.set_from_file(os.path.join("../glade/locked.png"))
18 icons.add("ascend-fixed-icon",gtk.IconSet(_fixedicon.get_pixbuf()))
19
20 _freeicon = gtk.Image()
21 _freeicon.set_from_file(os.path.join("../glade/unlocked.png"))
22 icons.add("ascend-free-icon",gtk.IconSet(_freeicon.get_pixbuf()))
23
24 icons.add_default()
25
26 print "Opening Block Properties Dialog..."
27 self.bpdialog = gtk.Dialog()
28 self.bpdialog.set_size_request(450,400)
29 self.bpdialog.set_title('Properties for block "%s"' % bi.name)
30 #self.bpdialog.set_transient_for(parent)
31 windowicon = gtk.Image()
32 windowicon.set_from_file(os.path.join("../glade/ascend.svg"))
33 self.bpdialog.set_icon(windowicon.get_pixbuf())
34
35 # create the 'General' tab
36
37 table = gtk.Table(3, 6, False)
38 self.bpdialog.vbox.pack_start(table, True, True, 0)
39 self.notebook = gtk.Notebook()
40 self.notebook.set_tab_pos(gtk.POS_TOP)
41 table.attach(self.notebook, 0,6,0,1)
42 self.show_tabs = True
43 self.show_border = True
44
45 text = bi.name
46 text += " - Block Description : \n\n"
47 self.pnamelbl = gtk.Label("Name:")
48 self.pname = gtk.Entry()
49 self.pname.set_text(bi.name)
50 hb = gtk.HBox()
51
52 hb.pack_start(self.pnamelbl, True, False)
53 hb.pack_start(self.pname, True, False)
54 text2 = "\nType:\n\t\t%s\n" % bi.blocktype.type.getName()
55 text2 += "\nPorts:\n"
56 for k,v in bi.ports.iteritems():
57 text2 += "\t\t%s" % v.name
58
59 if v.type == blockinstance.PORT_IN:
60 text2 += " (IN)\n"
61 elif v.type == blockinstance.PORT_OUT:
62 text2 += " (OUT)\n"
63 elif v.type == blockinstance.PORT_INOUT:
64 text2 += " (IN/OUT)\n"
65
66 if hasattr(bi,"params"):
67 #text2 += "\nParameters:\n"
68
69 for k,v in bi.params.iteritems():
70 pass
71 #text2 += "\t\t%s = %s\n" % (v.name, v.value)
72 if bi.instance:
73 text2 += "\nInstance exists"
74 self.inst = True
75 else:
76 text2 += "\nNo instance exists"
77 self.inst = False
78
79 scrolled_window0 = gtk.ScrolledWindow()
80 scrolled_window0.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
81 #genf = gtk.Frame('General Block Info')
82 #genf.set_border_width(0)
83 lbl1 = gtk.Label(text)
84 lbl2 = gtk.Label(text2)
85 vbox = gtk.VBox()
86 vbox.pack_start(lbl1, True, True)
87 vbox.pack_start(hb, True, True)
88 vbox.pack_start(lbl2, True, True)
89
90 #if self.inst == True:
91 #import modeltree
92 #MT = modeltree.TreeView(bi.instance)
93 #vbox.pack_start(MT.treeview, True, True)
94
95 scrolled_window0.add_with_viewport(vbox)
96 label0 = gtk.Label('General')
97 self.notebook.append_page(scrolled_window0, label0)
98
99 # Create the 'Parameters' tab
100
101 paramsvbox = gtk.VBox(homogeneous=True, spacing=5)
102
103 self.button = {}
104 self.buttonflag = {}
105 self.textbox = {}
106 self.paramflag = 0
107 if hasattr(bi,"params"):
108 for k,v in bi.params.iteritems():
109 self.paramflag = 1
110 paramvbox = gtk.VBox()
111 lbl = gtk.Label(v.name)
112 hbox=gtk.HBox()
113 hbox.pack_start(lbl, True, False, 0)
114 self.button[v.name] = gtk.Button()
115 #self.buttonflag[v.name] = bi.pfix[v.name]
116 self.buttonflag[v.name] = v.fix
117 if self.buttonflag[v.name] == 0:
118 self.button[v.name].set_image(gtk.image_new_from_stock("ascend-free-icon",gtk.ICON_SIZE_BUTTON))
119 else:
120 self.button[v.name].set_image(gtk.image_new_from_stock("ascend-fixed-icon",gtk.ICON_SIZE_BUTTON))
121 self.button[v.name].connect("clicked", self.update_FIXed_button, v.name, item )
122 hbox.pack_start(self.button[v.name], False, False, 0)
123 self.textbox[v.name] = gtk.Entry()
124 self.textbox[v.name].set_text(str(v.value))
125 self.textbox[v.name].connect("key-press-event",self.FIX_button, v.name)
126 hbox.pack_start(self.textbox[v.name], True, False, 0)
127 paramvbox.pack_start(hbox, False, False, 0)
128 desclbl = gtk.Label(v.get_description())
129 paramvbox.pack_start(desclbl, False, False, 0)
130 paramsvbox.pack_start(paramvbox)
131
132 if self.paramflag:
133 scrolled_window1 = gtk.ScrolledWindow()
134 scrolled_window1.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
135 #scrolled_window1.set_border_width(10)
136 label = gtk.Label("Parameters")
137 scrolled_window1.add_with_viewport(paramsvbox)
138 self.notebook.append_page(scrolled_window1, label)
139 #self.paramflag = 0
140
141 #create the 'custom method' entry tab
142
143 methodvbox = gtk.VBox(homogeneous=True)
144 methodframe = gtk.Frame("Enter your custom METHODs for the block below")
145 methodentry = gtksourceview.View()
146 methodentry.set_show_line_numbers(True)
147 self.sb = gtksourceview.Buffer()
148 self.sb.set_highlight_matching_brackets(True)
149 self.sb.set_highlight_syntax(True)
150 self.sb.set_text(bi.usercode)
151 methodentry.set_buffer(self.sb)
152 scrolled_window2 = gtk.ScrolledWindow()
153 scrolled_window2.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
154 label = gtk.Label("Custom Method")
155 methodframe.add(methodentry)
156 scrolled_window2.add_with_viewport(methodframe)
157 self.notebook.append_page(scrolled_window2, label)
158
159 #the instance view tab
160 if self.inst == True:
161 scrolled_window0 = gtk.ScrolledWindow()
162 scrolled_window0.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
163 #genf = gtk.Frame('General Block Info')
164 #genf.set_border_width(0)
165 vbox = gtk.VBox()
166 #vbox.pack_start(lbl1, True, True)
167
168 import modeltree
169 MT = modeltree.TreeView(bi.instance)
170 vbox.pack_start(MT.treeview, True, True)
171
172 scrolled_window0.add_with_viewport(vbox)
173 label0 = gtk.Label('Instance')
174 self.notebook.append_page(scrolled_window0, label0)
175
176
177 self.notebook.show_all()
178 if tab == None:
179 self.notebook.set_current_page(item.blockinstance.tab)
180 else:
181 if self.paramflag :
182 self.notebook.set_current_page(tab)
183 else:
184 self.notebook.set_current_page(tab-1)
185 #action area buttons
186 #help = self.bpdialog.add_button(gtk.STOCK_HELP, gtk.RESPONSE_OK)
187 #appl = self.bpdialog.add_button(gtk.STOCK_APPLY, gtk.RESPONSE_OK)
188 cancel = self.bpdialog.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
189 ok = self.bpdialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
190 ok.connect("clicked", self.on_properties_ok_clicked, self.buttonflag, item,parent )
191 ok.grab_default()
192
193 self.bpdialog.show_all()
194
195 def run(self):
196 self.bpdialog.run()
197 print "Closing Block Properties..."
198 self.bpdialog.hide()
199
200 def update_FIXed_button(self, widget, vname, item):
201 if self.buttonflag[vname] == 0:
202 self.button[vname].set_image(gtk.image_new_from_stock("ascend-fixed-icon",gtk.ICON_SIZE_BUTTON))
203 self.buttonflag[vname] = 1
204 elif self.buttonflag[vname] == 1:
205 self.button[vname].set_image(gtk.image_new_from_stock("ascend-free-icon",gtk.ICON_SIZE_BUTTON))
206 self.buttonflag[vname] = 0
207
208 def FIX_button(self, widget,event, vname):
209 self.button[vname].set_image(gtk.image_new_from_stock("ascend-fixed-icon",gtk.ICON_SIZE_BUTTON))
210 self.buttonflag[vname] = 1
211
212 def on_properties_ok_clicked(self, widget, buttonflag, item,parent):
213 for k,v in item.blockinstance.params.iteritems():
214 try:
215 self.checkEntry(str(self.textbox[v.name].get_text()))
216 except:
217 m = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, "Please enter proper values.")
218 m.run()
219 m.destroy()
220 self.run()
221 return
222 if buttonflag[v.name] == 1:
223 v.fix = 1
224 print 'fixed ' + item.blockinstance.name+'.'+v.name
225 elif buttonflag[v.name] == 0:
226 v.fix = 0
227 print 'freed ' + item.blockinstance.name+'.'+v.name
228 item.blockinstance.name = self.pname.get_text()
229 v.value = str(self.textbox[v.name].get_text())
230
231 startiter = self.sb.get_start_iter()
232 enditer = self.sb.get_end_iter()
233 usertext = self.sb.get_text(startiter, enditer)
234 if usertext == "" :
235 pass
236 else:
237 item.blockinstance.usercode = usertext
238 item.blockinstance.tab = self.notebook.get_current_page()
239 parent.view.canvas.canvasmodelstate = 'Modified'
240 parent.status.push(0,"Canvasmodel state : Modified")
241 parent.view.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#FFF'))
242
243 def checkEntry(self, _param_value):
244 # match a float with option text afterwards, optionally separated by whitespace
245 # temporary solution provided here, more powerful typechecking needed to be supported
246 #try:
247 _match = re.match(UNITS_RE,_param_value)
248 #if not _match:
249 #print _param_val
250 #raise InputError("Not a valid value-and-optional-units")
251 #parent.reporter.reportError("Not a valid value-and-optional-units")
252 _val = _match.group(1) # the numerical part of the input
253 _units = _match.group(5) # the text entered for units
254 return str(_val)+'{'+str(_units)+'}'
255

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