Skip to content

Commit 4aa190a

Browse files
committed
start of a menu widget
svn path=/trunk/matplotlib/; revision=5621
1 parent 8d638e0 commit 4aa190a

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

examples/widgets/menu.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import numpy as np
2+
import matplotlib
3+
import matplotlib.patches as patches
4+
import matplotlib.mathtext as mathtext
5+
import matplotlib.pyplot as plt
6+
import matplotlib.artist as artist
7+
import matplotlib.image as image
8+
9+
matplotlib.rc('image', origin='upper')
10+
11+
class MenuItem(artist.Artist):
12+
parser = mathtext.MathTextParser("Bitmap")
13+
padx = 5
14+
pady =5
15+
def __init__(self, fig, labelstr):
16+
artist.Artist.__init__(self)
17+
self.set_figure(fig)
18+
19+
20+
x, self.depth = self.parser.to_rgba(
21+
labelstr, color='black', fontsize=14, dpi=100)
22+
xHover, depth = self.parser.to_rgba(
23+
labelstr, color='white', fontsize=14, dpi=100)
24+
25+
26+
self.labelwidth = x.shape[1]
27+
self.labelheight = x.shape[0]
28+
print 'h', self.labelheight
29+
self.label = image.FigureImage(fig)
30+
self.label.set_array(x.astype(float)/255.)
31+
32+
self.labelHover = image.FigureImage(fig)
33+
self.labelHover.set_array(xHover.astype(float)/255.)
34+
35+
36+
37+
# we'll update these later
38+
self.rect = patches.Rectangle((0,0), 1,1, facecolor='yellow', alpha=0.2)
39+
self.rectHover = patches.Rectangle((0,0), 1,1, facecolor='blue', alpha=0.2)
40+
41+
42+
43+
def set_extent(self, x, y, w, h):
44+
print x, y, w, h
45+
self.rect.set_x(x)
46+
self.rect.set_y(y)
47+
self.rect.set_width(w)
48+
self.rect.set_height(h)
49+
50+
self.rectHover.set_x(x)
51+
self.rectHover.set_y(y)
52+
self.rectHover.set_width(w)
53+
self.rectHover.set_height(h)
54+
55+
self.label.ox = x+self.padx
56+
self.label.oy = y-self.depth+self.pady/2.
57+
58+
self.rect._update_patch_transform()
59+
self.rectHover._update_patch_transform()
60+
self.labelHover.ox = x+self.padx
61+
self.labelHover.oy = y-self.depth+self.pady/2.
62+
self.hover = False
63+
64+
self.activeRect = self.rect
65+
self.activeLabel = self.label
66+
67+
def draw(self, renderer):
68+
self.activeRect.draw(renderer)
69+
self.activeLabel.draw(renderer)
70+
71+
def set_hover(self, event):
72+
'check the hover status of event and return true if status is changed'
73+
b,junk = self.rect.contains(event)
74+
if b:
75+
self.activeRect = self.rectHover
76+
self.activeLabel = self.labelHover
77+
else:
78+
self.activeRect = self.rect
79+
self.activeLabel = self.label
80+
81+
h = self.hover
82+
self.hover = b
83+
return b!=h
84+
85+
class Menu:
86+
87+
def __init__(self, fig, labels):
88+
self.figure = fig
89+
fig.suppressComposite = True
90+
menuitems = []
91+
self.numitems = len(labels)
92+
for label in labels:
93+
menuitems.append(MenuItem(fig, label))
94+
95+
self.menuitems = menuitems
96+
97+
98+
maxw = max([item.labelwidth for item in menuitems])
99+
maxh = max([item.labelheight for item in menuitems])
100+
101+
102+
totalh = self.numitems*maxh + (self.numitems+1)*2*MenuItem.pady
103+
104+
x0 = 100
105+
y0 = 400
106+
107+
width = maxw + 2*MenuItem.padx
108+
height = maxh+MenuItem.pady
109+
for item in menuitems:
110+
left = x0
111+
bottom = y0-maxh-MenuItem.pady
112+
113+
114+
item.set_extent(left, bottom, width, height)
115+
116+
fig.artists.append(item)
117+
y0 -= maxh + MenuItem.pady
118+
119+
120+
fig.canvas.mpl_connect('motion_notify_event', self.on_move)
121+
122+
def on_move(self, event):
123+
draw = False
124+
for item in self.menuitems:
125+
b = item.set_hover(event)
126+
draw = b
127+
128+
if draw:
129+
print 'draw'
130+
self.figure.canvas.draw()
131+
132+
133+
fig = plt.figure()
134+
menu = Menu(fig, ('open', 'close', 'save', 'save as', 'quit'))
135+
136+
plt.show()
137+
138+
139+
140+

0 commit comments

Comments
 (0)