root/gaphas/branches/hw/demo_profile.py

Revision 1660, 5.4 kB (checked in by wrobe..@pld-linux.org, 1 year ago)

- always add 40 boxes in case of box grid

Line 
1 #!/usr/bin/env python
2
3 import pygtk
4 pygtk.require('2.0') 
5
6 import sys
7 import math
8 import gtk
9 from gaphas import Canvas, GtkView, View
10 from gaphas.examples import Box, Text, DefaultExampleTool
11 from gaphas.item import Line, NW, SE
12 from gaphas.tool import PlacementTool, HandleTool
13 from gaphas.painter import ItemPainter
14 from gaphas import state
15 from gaphas.util import text_extents
16
17 # Global undo list
18 undo_list = []
19
20 def undo_handler(event):
21     global undo_list
22     undo_list.append(event)
23
24
25 class MyBox(Box):
26     """Box with an example connection protocol.
27     """
28
29 class MyLine(Line):
30     """Line with experimental connection protocol.
31     """
32    
33     def draw_head(self, context):
34         cr = context.cairo
35         cr.move_to(0, 0)
36         cr.line_to(10, 10)
37         cr.stroke()
38         # Start point for the line to the next handle
39         cr.move_to(0, 0)
40
41     def draw_tail(self, context):
42         cr = context.cairo
43         cr.line_to(0, 0)
44         cr.line_to(10, 10)
45         cr.stroke()
46
47
48
49
50 class MyText(Text):
51     """
52     Text with experimental connection protocol.
53     """
54    
55     def draw(self, context):
56         Text.draw(self, context)
57         cr = context.cairo
58         w, h = text_extents(cr, self.text, multiline=self.multiline)
59         cr.rectangle(0, 0, w, h)
60         cr.set_source_rgba(.3, .3, 1., .6)
61         cr.stroke()
62
63
64 def create_window(canvas, title, zoom=1.0):
65     view = GtkView()
66     view.tool = DefaultExampleTool()
67
68     w = gtk.Window()
69     w.set_title(title)
70     h = gtk.HBox()
71     w.add(h)
72
73     # VBox contains buttons that can be used to manipulate the canvas:
74     v = gtk.VBox()
75     v.set_property('border-width', 3)
76     v.set_property('spacing', 2)
77     f = gtk.Frame()
78     f.set_property('border-width', 1)
79     f.add(v)
80     h.pack_start(f, expand=False)
81
82     b = gtk.Button('Move by (100, 50)')
83
84     def on_clicked(button):
85         global movable_item
86         item = movable_item
87         import time
88         t1 = time.time()
89         for i in range(20):
90             item.matrix.translate(1, 1)
91             item.request_update()
92             canvas.update_matrix(item)
93             # visualize each event:
94             while gtk.events_pending():
95                 gtk.main_iteration()
96         t2 = time.time()
97         print 'move time: %0.2f' % (t2 - t1,)
98
99     b.connect('clicked', on_clicked)
100     v.add(b)
101
102    
103 #    b = gtk.Button('Cursor')
104 #
105 #    def on_clicked(button, li):
106 #        c = li[0]
107 #        li[0] = (c+2) % 154
108 #        button.set_label('Cursor %d' % c)
109 #        button.window.set_cursor(gtk.gdk.Cursor(c))
110 #
111 #    b.connect('clicked', on_clicked, [0])
112 #    v.add(b)
113
114     # Add the actual View:
115
116     t = gtk.Table(2,2)
117     h.add(t)
118
119     w.connect('destroy', gtk.main_quit)
120
121     view.canvas = canvas
122     view.zoom(zoom)
123     view.set_size_request(150, 120)
124     hs = gtk.HScrollbar(view.hadjustment)
125     vs = gtk.VScrollbar(view.vadjustment)
126     t.attach(view, 0, 1, 0, 1)
127     t.attach(hs, 0, 1, 1, 2, xoptions=gtk.FILL, yoptions=gtk.FILL)
128     t.attach(vs, 1, 2, 0, 1, xoptions=gtk.FILL, yoptions=gtk.FILL)
129
130     w.show_all()
131    
132     def handle_changed(view, item, what):
133         print what, 'changed: ', item
134
135     view.connect('focus-changed', handle_changed, 'focus')
136     view.connect('hover-changed', handle_changed, 'hover')
137     view.connect('selection-changed', handle_changed, 'selection')
138
139 def main():
140     global movable_item
141     if len(sys.argv) == 2:
142         count = int(sys.argv[1])
143     else:
144         count = 1
145     c=Canvas()
146
147     create_window(c, 'View created before')
148
149     # Add stuff to the canvas:
150
151     movable_item=b=MyBox()
152     b.min_width = 20
153     b.min_height = 30
154     print 'box', b
155     b.matrix=(1.0, 0.0, 0.0, 1, 20,20)
156     b.width=b.height = 40
157     c.add(b)
158
159     bb=Box()
160     print 'box', bb
161     bb.matrix=(1.0, 0.0, 0.0, 1, 10,10)
162     c.add(bb, parent=b)
163     #v.selected_items = bb
164
165     # AJM: extra boxes:
166     bb=Box()
167     print 'box', bb
168     bb.matrix.rotate(math.pi/4.)
169     c.add(bb, parent=b)
170     for i in xrange(count):
171         bb=Box()
172         print 'box', bb
173         bb.matrix.rotate(math.pi/4.0 * i / 10.0)
174         c.add(bb, parent=b)
175
176     for i in range(40):
177         bb = MyBox()
178         bb.width = bb.height = 15
179         x = int(i % 4) * 20
180         y = int(i / 4) * 20
181         bb.matrix.translate(5 + x, 100 + y)
182         c.add(bb)
183
184     t=MyText('Single line')
185     t.matrix.translate(70,70)
186     c.add(t)
187
188     l=MyLine()
189     l.fyzzyness = 1
190     l.handles()[1].pos = (30, 30)
191     l.split_segment(0, 3)
192     l.matrix.translate(30, 60)
193     c.add(l)
194     l.orthogonal = True
195
196     off_y = 0
197     for align_x in (-1, 0, 1):
198         for align_y in (-1, 0, 1):
199             t=MyText('Aligned text %d/%d' % (align_x, align_y),
200                      align_x=align_x, align_y=align_y)
201             t.matrix.translate(120, 200 + off_y)
202             off_y += 30
203             c.add(t)
204
205     t=MyText('Multiple\nlines', multiline = True)
206     t.matrix.translate(70,100)
207     c.add(t)
208
209     gtk.main()
210
211 if __name__ == '__main__':
212     try:
213         import cProfile
214         import pstats
215         cProfile.run('main()', 'demo-gaphas.prof')
216         p = pstats.Stats('demo-gaphas.prof')
217         p.strip_dirs().sort_stats('time').print_stats(20)
218     except ImportError, ex:
219         import hotshot, hotshot.stats
220         import gc
221         prof = hotshot.Profile('demo-gaphas.prof')
222         prof.runcall(main)
223         prof.close()
224         stats = hotshot.stats.load('demo-gaphas.prof')
225         stats.strip_dirs()
226         stats.sort_stats('time', 'calls')
227         stats.print_stats(20)
228
229 # vim: sw=4:et:
Note: See TracBrowser for help on using the browser.