Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f779f0

Browse files
committed
Cleanup menu example.
1 parent 80eabbc commit 8f779f0

1 file changed

Lines changed: 21 additions & 57 deletions

File tree

examples/widgets/menu.py

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
66
"""
77
import numpy as np
8+
import matplotlib.artist as artist
89
import matplotlib.colors as colors
9-
import matplotlib.patches as patches
10+
import matplotlib.image as image
1011
import matplotlib.mathtext as mathtext
12+
import matplotlib.patches as patches
1113
import matplotlib.pyplot as plt
12-
import matplotlib.artist as artist
13-
import matplotlib.image as image
1414

1515

1616
class ItemProperties:
@@ -21,9 +21,6 @@ def __init__(self, fontsize=14, labelcolor='black', bgcolor='yellow',
2121
self.bgcolor = bgcolor
2222
self.alpha = alpha
2323

24-
self.labelcolor_rgb = colors.to_rgba(labelcolor)[:3]
25-
self.bgcolor_rgb = colors.to_rgba(bgcolor)[:3]
26-
2724

2825
class MenuItem(artist.Artist):
2926
parser = mathtext.MathTextParser("Bitmap")
@@ -37,99 +34,70 @@ def __init__(self, fig, labelstr, props=None, hoverprops=None,
3734
self.set_figure(fig)
3835
self.labelstr = labelstr
3936

40-
if props is None:
41-
props = ItemProperties()
42-
43-
if hoverprops is None:
44-
hoverprops = ItemProperties()
45-
46-
self.props = props
47-
self.hoverprops = hoverprops
37+
self.props = props if props is not None else ItemProperties()
38+
self.hoverprops = (
39+
hoverprops if hoverprops is not None else ItemProperties())
40+
if self.props.fontsize != self.hoverprops.fontsize:
41+
raise NotImplementedError(
42+
'support for different font sizes not implemented')
4843

4944
self.on_select = on_select
5045

5146
x, self.depth = self.parser.to_mask(
5247
labelstr, fontsize=props.fontsize, dpi=fig.dpi)
5348

54-
if props.fontsize != hoverprops.fontsize:
55-
raise NotImplementedError(
56-
'support for different font sizes not implemented')
57-
5849
self.labelwidth = x.shape[1]
5950
self.labelheight = x.shape[0]
6051

61-
self.labelArray = np.zeros((x.shape[0], x.shape[1], 4))
62-
self.labelArray[:, :, -1] = x/255.
52+
mask = np.zeros((*x.shape, 4))
53+
mask[:, :, -1] = x / 255
54+
self.label = image.FigureImage(fig, origin='upper', array=mask)
6355

64-
self.label = image.FigureImage(fig, origin='upper')
65-
self.label.set_array(self.labelArray)
66-
67-
# we'll update these later
68-
self.rect = patches.Rectangle((0, 0), 1, 1)
56+
self.rect = patches.Rectangle((0, 0), 1, 1) # Will be updated later.
6957

7058
self.set_hover_props(False)
7159

7260
fig.canvas.mpl_connect('button_release_event', self.check_select)
7361

7462
def check_select(self, event):
75-
over, junk = self.rect.contains(event)
63+
over, _ = self.rect.contains(event)
7664
if not over:
7765
return
78-
7966
if self.on_select is not None:
8067
self.on_select(self)
8168

8269
def set_extent(self, x, y, w, h):
83-
print(x, y, w, h)
84-
self.rect.set_x(x)
85-
self.rect.set_y(y)
86-
self.rect.set_width(w)
87-
self.rect.set_height(h)
88-
70+
self.rect.set(x=x, y=y, width=w, height=h)
8971
self.label.ox = x + self.padx
90-
self.label.oy = y - self.depth + self.pady/2.
91-
72+
self.label.oy = y - self.depth + self.pady/2
9273
self.hover = False
9374

9475
def draw(self, renderer):
9576
self.rect.draw(renderer)
9677
self.label.draw(renderer)
9778

9879
def set_hover_props(self, b):
99-
if b:
100-
props = self.hoverprops
101-
else:
102-
props = self.props
103-
104-
r, g, b = props.labelcolor_rgb
105-
self.labelArray[:, :, 0] = r
106-
self.labelArray[:, :, 1] = g
107-
self.labelArray[:, :, 2] = b
108-
self.label.set_array(self.labelArray)
80+
props = self.hoverprops if b else self.props
81+
self.label.get_array()[..., :3] = colors.to_rgb(props.labelcolor)
10982
self.rect.set(facecolor=props.bgcolor, alpha=props.alpha)
11083

11184
def set_hover(self, event):
11285
"""
11386
Update the hover status of event and return whether it was changed.
11487
"""
115-
b, junk = self.rect.contains(event)
116-
88+
b, _ = self.rect.contains(event)
11789
changed = (b != self.hover)
118-
11990
if changed:
12091
self.set_hover_props(b)
121-
12292
self.hover = b
12393
return changed
12494

12595

12696
class Menu:
12797
def __init__(self, fig, menuitems):
12898
self.figure = fig
129-
fig.suppressComposite = True
13099

131100
self.menuitems = menuitems
132-
self.numitems = len(menuitems)
133101

134102
maxw = max(item.labelwidth for item in menuitems)
135103
maxh = max(item.labelheight for item in menuitems)
@@ -152,12 +120,8 @@ def __init__(self, fig, menuitems):
152120
fig.canvas.mpl_connect('motion_notify_event', self.on_move)
153121

154122
def on_move(self, event):
155-
draw = False
156-
for item in self.menuitems:
157-
draw = item.set_hover(event)
158-
if draw:
159-
self.figure.canvas.draw()
160-
break
123+
if any(item.set_hover(event) for item in self.menuitems):
124+
self.figure.canvas.draw()
161125

162126

163127
fig = plt.figure()

0 commit comments

Comments
 (0)