| 1 | | import demo |
|---|
| | 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 | handles = item.handles() |
|---|
| | 88 | import time |
|---|
| | 89 | t1 = time.time() |
|---|
| | 90 | for i in range(20): |
|---|
| | 91 | for h in handles: |
|---|
| | 92 | h.x += 1 |
|---|
| | 93 | h.y += 1 |
|---|
| | 94 | item.request_update() |
|---|
| | 95 | canvas.update_matrix(item) |
|---|
| | 96 | # visualize each event: |
|---|
| | 97 | while gtk.events_pending(): |
|---|
| | 98 | gtk.main_iteration() |
|---|
| | 99 | t2 = time.time() |
|---|
| | 100 | print 'move time: %0.2f' % (t2 - t1,) |
|---|
| | 101 | |
|---|
| | 102 | b.connect('clicked', on_clicked) |
|---|
| | 103 | v.add(b) |
|---|
| | 104 | |
|---|
| | 105 | |
|---|
| | 106 | # b = gtk.Button('Cursor') |
|---|
| | 107 | # |
|---|
| | 108 | # def on_clicked(button, li): |
|---|
| | 109 | # c = li[0] |
|---|
| | 110 | # li[0] = (c+2) % 154 |
|---|
| | 111 | # button.set_label('Cursor %d' % c) |
|---|
| | 112 | # button.window.set_cursor(gtk.gdk.Cursor(c)) |
|---|
| | 113 | # |
|---|
| | 114 | # b.connect('clicked', on_clicked, [0]) |
|---|
| | 115 | # v.add(b) |
|---|
| | 116 | |
|---|
| | 117 | # Add the actual View: |
|---|
| | 118 | |
|---|
| | 119 | t = gtk.Table(2,2) |
|---|
| | 120 | h.add(t) |
|---|
| | 121 | |
|---|
| | 122 | w.connect('destroy', gtk.main_quit) |
|---|
| | 123 | |
|---|
| | 124 | view.canvas = canvas |
|---|
| | 125 | view.zoom(zoom) |
|---|
| | 126 | view.set_size_request(150, 120) |
|---|
| | 127 | hs = gtk.HScrollbar(view.hadjustment) |
|---|
| | 128 | vs = gtk.VScrollbar(view.vadjustment) |
|---|
| | 129 | t.attach(view, 0, 1, 0, 1) |
|---|
| | 130 | t.attach(hs, 0, 1, 1, 2, xoptions=gtk.FILL, yoptions=gtk.FILL) |
|---|
| | 131 | t.attach(vs, 1, 2, 0, 1, xoptions=gtk.FILL, yoptions=gtk.FILL) |
|---|
| | 132 | |
|---|
| | 133 | w.show_all() |
|---|
| | 134 | |
|---|
| | 135 | def handle_changed(view, item, what): |
|---|
| | 136 | print what, 'changed: ', item |
|---|
| | 137 | |
|---|
| | 138 | view.connect('focus-changed', handle_changed, 'focus') |
|---|
| | 139 | view.connect('hover-changed', handle_changed, 'hover') |
|---|
| | 140 | view.connect('selection-changed', handle_changed, 'selection') |
|---|
| | 141 | |
|---|
| | 142 | def main(): |
|---|
| | 143 | global movable_item |
|---|
| | 144 | if len(sys.argv) == 2: |
|---|
| | 145 | count = int(sys.argv[1]) |
|---|
| | 146 | else: |
|---|
| | 147 | count = 1 |
|---|
| | 148 | c=Canvas() |
|---|
| | 149 | |
|---|
| | 150 | create_window(c, 'View created before') |
|---|
| | 151 | |
|---|
| | 152 | # Add stuff to the canvas: |
|---|
| | 153 | |
|---|
| | 154 | movable_item=b=MyBox() |
|---|
| | 155 | b.min_width = 20 |
|---|
| | 156 | b.min_height = 30 |
|---|
| | 157 | print 'box', b |
|---|
| | 158 | b.matrix=(1.0, 0.0, 0.0, 1, 20,20) |
|---|
| | 159 | b.width=b.height = 40 |
|---|
| | 160 | c.add(b) |
|---|
| | 161 | |
|---|
| | 162 | bb=Box() |
|---|
| | 163 | print 'box', bb |
|---|
| | 164 | bb.matrix=(1.0, 0.0, 0.0, 1, 10,10) |
|---|
| | 165 | c.add(bb, parent=b) |
|---|
| | 166 | #v.selected_items = bb |
|---|
| | 167 | |
|---|
| | 168 | # AJM: extra boxes: |
|---|
| | 169 | bb=Box() |
|---|
| | 170 | print 'box', bb |
|---|
| | 171 | bb.matrix.rotate(math.pi/4.) |
|---|
| | 172 | c.add(bb, parent=b) |
|---|
| | 173 | for i in xrange(count): |
|---|
| | 174 | bb=Box() |
|---|
| | 175 | print 'box', bb |
|---|
| | 176 | bb.matrix.rotate(math.pi/4.0 * i / 10.0) |
|---|
| | 177 | c.add(bb, parent=b) |
|---|
| | 178 | |
|---|
| | 179 | t=MyText('Single line') |
|---|
| | 180 | t.matrix.translate(70,70) |
|---|
| | 181 | c.add(t) |
|---|
| | 182 | |
|---|
| | 183 | l=MyLine() |
|---|
| | 184 | l.fyzzyness = 1 |
|---|
| | 185 | l.handles()[1].pos = (30, 30) |
|---|
| | 186 | l.split_segment(0, 3) |
|---|
| | 187 | l.matrix.translate(30, 60) |
|---|
| | 188 | c.add(l) |
|---|
| | 189 | l.orthogonal = True |
|---|
| | 190 | |
|---|
| | 191 | off_y = 0 |
|---|
| | 192 | for align_x in (-1, 0, 1): |
|---|
| | 193 | for align_y in (-1, 0, 1): |
|---|
| | 194 | t=MyText('Aligned text %d/%d' % (align_x, align_y), |
|---|
| | 195 | align_x=align_x, align_y=align_y) |
|---|
| | 196 | t.matrix.translate(120, 200 + off_y) |
|---|
| | 197 | off_y += 30 |
|---|
| | 198 | c.add(t) |
|---|
| | 199 | |
|---|
| | 200 | t=MyText('Multiple\nlines', multiline = True) |
|---|
| | 201 | t.matrix.translate(70,100) |
|---|
| | 202 | c.add(t) |
|---|
| | 203 | |
|---|
| | 204 | gtk.main() |
|---|