1
1
import numpy as np
2
2
import matplotlib
3
+ import matplotlib .colors as colors
3
4
import matplotlib .patches as patches
4
5
import matplotlib .mathtext as mathtext
5
6
import matplotlib .pyplot as plt
6
7
import matplotlib .artist as artist
7
8
import matplotlib .image as image
8
9
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 )
10
20
11
21
class MenuItem (artist .Artist ):
12
22
parser = mathtext .MathTextParser ("Bitmap" )
13
23
padx = 5
14
24
pady = 5
15
- def __init__ (self , fig , labelstr ):
25
+ def __init__ (self , fig , labelstr , props = None , hoverprops = None , on_select = None ):
16
26
artist .Artist .__init__ (self )
27
+
17
28
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
+
18
40
41
+ self .on_select = on_select
19
42
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' )
24
48
25
49
26
50
self .labelwidth = x .shape [1 ]
27
51
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
52
53
+ self .labelArray = np .zeros ((x .shape [0 ], x .shape [1 ], 4 ))
54
+ self .labelArray [:,:,- 1 ] = x / 255.
35
55
56
+ self .label = image .FigureImage (fig , origin = 'upper' )
57
+ self .label .set_array (self .labelArray )
36
58
37
59
# 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 )
40
65
66
+ def check_select (self , event ):
67
+ over , junk = self .rect .contains (event )
68
+ if not over :
69
+ return
41
70
71
+ if self .on_select is not None :
72
+ self .on_select (self )
42
73
43
74
def set_extent (self , x , y , w , h ):
44
75
print x , y , w , h
@@ -47,65 +78,64 @@ def set_extent(self, x, y, w, h):
47
78
self .rect .set_width (w )
48
79
self .rect .set_height (h )
49
80
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
81
self .label .ox = x + self .padx
56
82
self .label .oy = y - self .depth + self .pady / 2.
57
83
58
84
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
85
self .hover = False
63
86
64
- self .activeRect = self .rect
65
- self .activeLabel = self .label
66
-
67
87
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 )
70
103
71
104
def set_hover (self , event ):
72
105
'check the hover status of event and return true if status is changed'
73
106
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
107
81
- h = self .hover
108
+ changed = (b != self .hover )
109
+
110
+ if changed :
111
+ self .set_hover_props (b )
112
+
113
+
82
114
self .hover = b
83
- return b != h
115
+ return changed
84
116
85
117
class Menu :
86
118
87
- def __init__ (self , fig , labels ):
119
+ def __init__ (self , fig , menuitems ):
88
120
self .figure = fig
89
121
fig .suppressComposite = True
90
- menuitems = []
91
- self .numitems = len (labels )
92
- for label in labels :
93
- menuitems .append (MenuItem (fig , label ))
94
122
95
123
self .menuitems = menuitems
96
-
124
+ self . numitems = len ( menuitems )
97
125
98
126
maxw = max ([item .labelwidth for item in menuitems ])
99
127
maxh = max ([item .labelheight for item in menuitems ])
100
128
101
129
102
130
totalh = self .numitems * maxh + (self .numitems + 1 )* 2 * MenuItem .pady
103
131
132
+
104
133
x0 = 100
105
134
y0 = 400
106
135
107
136
width = maxw + 2 * MenuItem .padx
108
137
height = maxh + MenuItem .pady
138
+
109
139
for item in menuitems :
110
140
left = x0
111
141
bottom = y0 - maxh - MenuItem .pady
@@ -122,17 +152,27 @@ def __init__(self, fig, labels):
122
152
def on_move (self , event ):
123
153
draw = False
124
154
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
131
159
132
160
133
161
fig = 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 )
136
176
plt .show ()
137
177
138
178
0 commit comments