Mercurial > repos > rhope
annotate editgraph.py @ 138:1411de6050e1
First stab at transaction data structures, needs work
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 16 Nov 2010 21:53:18 -0500 |
parents | 0534bb8ee7ad |
children |
rev | line source |
---|---|
23
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 import Tkinter |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 from tkFont import Font |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 class Call(object): |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 def __init__(self, canvas, center, text = None, chars=None, padw = 20, padh = 4, fill="gray", outline="black",selectoutline="red"): |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 x,y = center |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 self.x = x |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 self.y = y |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 self.text = Tkinter.StringVar() |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 self.textlen = 0 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 if text != None: |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 self.text.set(text) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 self.textlen = len(text) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 if chars == None: |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 chars = len(text)+3 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 elif chars == None: |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 chars = 3 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 self.textwidget = Tkinter.Entry(canvas, width=chars, textvariable=self.text, font=Font(family="Courier New", size=10)) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 self.textwidget.bind('<Key>', self.textchanged, '+') |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 self.canvastext = canvas.create_window(x, y, window=self.textwidget) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 box = canvas.bbox(self.canvastext) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 self.width = box[2]-box[0]+padw |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 self.height = box[3]-box[1]+padh |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 hw = self.width / 2 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 hh = self.height / 2 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 ow = hw+10 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 self.poly = canvas.create_polygon(x-hw,y-hh, x+hw,y-hh, x+ow,y, x+hw,y+hh, x-hw,y+hh, x-ow,y, fill=fill, outline=outline) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 self.outline = outline |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 self.selectoutline = selectoutline |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 self.mousex = self.mousey = 0 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 def hittest(self, x, y): |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 ow = self.width / 2 + 10 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 hh = self.height / 2 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 return abs(x-self.x) <= ow and abs(y-self.y) <= hh |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 def onclick(self, x, y): |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 canvas.itemconfig(self.poly, outline=self.selectoutline) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 self.mousex = x |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 self.mousey = y |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 def lostfocus(self): |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 canvas.itemconfig(self.poly, outline=self.outline) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 def ondrag(self, x, y): |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 self.x += x-self.mousex |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 self.y += y-self.mousey |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 self.mousex = x |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 self.mousey = y |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 hw = self.width / 2 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 hh = self.height / 2 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 ow = hw+10 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 canvas.coords(self.poly, self.x-hw,self.y-hh, self.x+hw,self.y-hh, self.x+ow,self.y, self.x+hw,self.y+hh, self.x-hw,self.y+hh, self.x-ow,self.y) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 canvas.coords(self.canvastext, self.x, self.y) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 def textchanged(self,event): |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 text = self.text.get() |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 newlen = len(text) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 print text |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 if newlen != self.textlen: |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 print 'length changed' |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 self.textlen = newlen |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 grabbed = None |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 def onclick(event): |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 global grabbed |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 if grabbed != None: |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 grabbed.lostfocus() |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 grabbed = None |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 print "Click" |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 #TODO: Consider using quadtrees for performance |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 for i in range(len(nodes)-1,-1,-1): |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 node = nodes[i] |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 if node.hittest(event.x, event.y): |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 grabbed = node |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 print "Found hit!" |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 node.onclick(event.x, event.y) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 break |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
79 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 def ondrag(event): |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 global grabbed |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 if grabbed != None: |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 grabbed.ondrag(event.x, event.y) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 def ondoubleclick(event): |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 global nodes |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
87 nodes.append(Call(canvas, (event.x, event.y))) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 root = Tkinter.Tk() |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
90 canvas = Tkinter.Canvas(root, width=302,height=302) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
91 nodes = [] |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
92 nodes.append(Call(canvas, (50,40), "Foo")) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
93 canvas.bind('<Button-1>', onclick) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
94 canvas.bind('<B1-Motion>', ondrag) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
95 canvas.bind('<Double-Button-1>', ondoubleclick) |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
96 canvas.pack() |
0534bb8ee7ad
Added beginnings of graphical editor prototype
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
97 root.mainloop() |