11import numpy as np
22import matplotlib
3+ import matplotlib .colors as colors
34import matplotlib .patches as patches
45import matplotlib .mathtext as mathtext
56import matplotlib .pyplot as plt
67import matplotlib .artist as artist
78import matplotlib .image as image
89
9- matplotlib .rc ('image' , origin = 'upper' )
10+
11+ class ItemProperties :
12+ def __init__ (self , fontsize = 14 , labelcolor = 'black' , bgcolor = 'yellow' , alpha = 1.0 ):
13+ self .fontsize = fontsize
14+ self .labelcolor = labelcolor
15+ self .bgcolor = bgcolor
16+ self .alpha = alpha
17+
18+ self .labelcolor_rgb = colors .colorConverter .to_rgb (labelcolor )
19+ self .bgcolor_rgb = colors .colorConverter .to_rgb (bgcolor )
1020
1121class MenuItem (artist .Artist ):
1222 parser = mathtext .MathTextParser ("Bitmap" )
1323 padx = 5
1424 pady = 5
15- def __init__ (self , fig , labelstr ):
25+ def __init__ (self , fig , labelstr , props = None , hoverprops = None , on_select = None ):
1626 artist .Artist .__init__ (self )
27+
1728 self .set_figure (fig )
29+ self .labelstr = labelstr
30+
31+ if props is None :
32+ props = ItemProperties ()
33+
34+ if hoverprops is None :
35+ hoverprops = ItemProperties ()
36+
37+ self .props = props
38+ self .hoverprops = hoverprops
39+
1840
41+ self .on_select = on_select
1942
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 )
43+ x , self .depth = self .parser .to_mask (
44+ labelstr , fontsize = props .fontsize , dpi = fig .dpi )
45+
46+ if props .fontsize != hoverprops .fontsize :
47+ raise NotImplementedError ('support for different font sizes not implemented' )
2448
2549
2650 self .labelwidth = x .shape [1 ]
2751 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. )
3452
53+ self .labelArray = np .zeros ((x .shape [0 ], x .shape [1 ], 4 ))
54+ self .labelArray [:,:,- 1 ] = x / 255.
3555
56+ self .label = image .FigureImage (fig , origin = 'upper' )
57+ self .label .set_array (self .labelArray )
3658
3759 # 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 )
60+ self .rect = patches .Rectangle ((0 ,0 ), 1 ,1 )
61+
62+ self .set_hover_props (False )
63+
64+ fig .canvas .mpl_connect ('button_release_event' , self .check_select )
4065
66+ def check_select (self , event ):
67+ over , junk = self .rect .contains (event )
68+ if not over :
69+ return
4170
71+ if self .on_select is not None :
72+ self .on_select (self )
4273
4374 def set_extent (self , x , y , w , h ):
4475 print x , y , w , h
@@ -47,65 +78,64 @@ def set_extent(self, x, y, w, h):
4778 self .rect .set_width (w )
4879 self .rect .set_height (h )
4980
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-
5581 self .label .ox = x + self .padx
5682 self .label .oy = y - self .depth + self .pady / 2.
5783
5884 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.
6285 self .hover = False
6386
64- self .activeRect = self .rect
65- self .activeLabel = self .label
66-
6787 def draw (self , renderer ):
68- self .activeRect .draw (renderer )
69- self .activeLabel .draw (renderer )
88+ self .rect .draw (renderer )
89+ self .label .draw (renderer )
90+
91+ def set_hover_props (self , b ):
92+ if b :
93+ props = self .hoverprops
94+ else :
95+ props = self .props
96+
97+ r , g , b = props .labelcolor_rgb
98+ self .labelArray [:,:,0 ] = r
99+ self .labelArray [:,:,1 ] = g
100+ self .labelArray [:,:,2 ] = b
101+ self .label .set_array (self .labelArray )
102+ self .rect .set (facecolor = props .bgcolor , alpha = props .alpha )
70103
71104 def set_hover (self , event ):
72105 'check the hover status of event and return true if status is changed'
73106 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
80107
81- h = self .hover
108+ changed = (b != self .hover )
109+
110+ if changed :
111+ self .set_hover_props (b )
112+
113+
82114 self .hover = b
83- return b != h
115+ return changed
84116
85117class Menu :
86118
87- def __init__ (self , fig , labels ):
119+ def __init__ (self , fig , menuitems ):
88120 self .figure = fig
89121 fig .suppressComposite = True
90- menuitems = []
91- self .numitems = len (labels )
92- for label in labels :
93- menuitems .append (MenuItem (fig , label ))
94122
95123 self .menuitems = menuitems
96-
124+ self . numitems = len ( menuitems )
97125
98126 maxw = max ([item .labelwidth for item in menuitems ])
99127 maxh = max ([item .labelheight for item in menuitems ])
100128
101129
102130 totalh = self .numitems * maxh + (self .numitems + 1 )* 2 * MenuItem .pady
103131
132+
104133 x0 = 100
105134 y0 = 400
106135
107136 width = maxw + 2 * MenuItem .padx
108137 height = maxh + MenuItem .pady
138+
109139 for item in menuitems :
110140 left = x0
111141 bottom = y0 - maxh - MenuItem .pady
@@ -122,17 +152,27 @@ def __init__(self, fig, labels):
122152 def on_move (self , event ):
123153 draw = False
124154 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 ()
155+ draw = item .set_hover (event )
156+ if draw :
157+ self .figure .canvas .draw ()
158+ break
131159
132160
133161fig = plt .figure ()
134- menu = Menu (fig , ('open' , 'close' , 'save' , 'save as' , 'quit' ))
135-
162+ fig .subplots_adjust (left = 0.3 )
163+ props = ItemProperties (labelcolor = 'black' , bgcolor = 'yellow' ,
164+ fontsize = 15 , alpha = 0.2 )
165+ hoverprops = ItemProperties (labelcolor = 'white' , bgcolor = 'blue' ,
166+ fontsize = 15 , alpha = 0.2 )
167+
168+ menuitems = []
169+ for label in ('open' , 'close' , 'save' , 'save as' , 'quit' ):
170+ def on_select (item ):
171+ print 'you selected' , item .labelstr
172+ item = MenuItem (fig , label , props = props , hoverprops = hoverprops , on_select = on_select )
173+ menuitems .append (item )
174+
175+ menu = Menu (fig , menuitems )
136176plt .show ()
137177
138178
0 commit comments