Skip to content

Commit 98e4d47

Browse files
authored
Refactor and fix resize_event for newer matplotlib versions (#3)
* initial commit * fix resize_event issue * renaming for pytest * refactoring * deprecation warning for matplotlib 3.8 * tests mouse behavior triggering event AttributeErrors * fix event AttributeErrors * redundant imports * increase matplotlib dependency version * copy paste fail * test and fix key events * readability * refactoring * argument renaming
1 parent 7864a8e commit 98e4d47

File tree

8 files changed

+192
-70
lines changed

8 files changed

+192
-70
lines changed

kivy_garden/__init__.py

Whitespace-only changes.

kivy_garden/matplotlib/backend_kivy.py

Lines changed: 103 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ def close(event):
232232
import six
233233
import os
234234
import matplotlib
235-
import matplotlib.transforms as transforms
236235
from matplotlib._pylab_helpers import Gcf
237236
from matplotlib.backend_bases import (
238237
RendererBase,
@@ -243,12 +242,15 @@ def close(event):
243242
TimerBase,
244243
)
245244
from matplotlib.figure import Figure
246-
from matplotlib.transforms import Bbox, Affine2D
247-
from matplotlib.backend_bases import ShowBase, Event
245+
from matplotlib.transforms import Affine2D
246+
from matplotlib.backend_bases import (ShowBase,
247+
Event,
248+
ResizeEvent,
249+
MouseEvent,
250+
KeyEvent)
248251
from matplotlib.backends.backend_agg import FigureCanvasAgg
249252
from matplotlib.mathtext import MathTextParser
250253
from matplotlib import rcParams
251-
from hashlib import md5
252254
from matplotlib import _path
253255

254256
try:
@@ -260,7 +262,6 @@ def close(event):
260262
from kivy.graphics.texture import Texture
261263
from kivy.graphics import Rectangle
262264
from kivy.uix.widget import Widget
263-
from kivy.uix.label import Label
264265
from kivy.uix.floatlayout import FloatLayout
265266
from kivy.uix.behaviors import FocusBehavior
266267
from kivy.uix.actionbar import (
@@ -286,12 +287,8 @@ def close(event):
286287
from kivy.resources import resource_find
287288
from kivy.uix.stencilview import StencilView
288289
from kivy.core.window import Window
289-
from kivy.uix.button import Button
290-
from kivy.uix.boxlayout import BoxLayout
291-
from kivy.uix.relativelayout import RelativeLayout
292290
from kivy.uix.popup import Popup
293291
from kivy.properties import ObjectProperty
294-
from kivy.uix.textinput import TextInput
295292
from kivy.lang import Builder
296293
from kivy.clock import Clock
297294
from distutils.version import LooseVersion
@@ -300,12 +297,9 @@ def close(event):
300297
_mpl_ge_2_0 = LooseVersion(matplotlib.__version__) >= LooseVersion("2.0.0")
301298

302299
import numpy as np
303-
import io
304300
import textwrap
305301
import uuid
306302
import numbers
307-
from functools import partial
308-
from math import cos, sin, pi
309303

310304
kivy.require("1.9.1")
311305

@@ -340,7 +334,7 @@ def build(self):
340334

341335

342336
def draw_if_interactive():
343-
"""Handle whether or not the backend is in interactive mode or not."""
337+
"""Handle whether the backend is in interactive mode or not."""
344338
if matplotlib.is_interactive():
345339
figManager = Gcf.get_active()
346340
if figManager:
@@ -989,13 +983,14 @@ def __init__(self, canvas, **kwargs):
989983
)
990984

991985
def _init_toolbar(self):
992-
"""A Toolbar is created with an ActionBar widget in which buttons are
993-
added with a specific behavior given by a callback. The buttons
994-
properties are given by matplotlib.
986+
"""A Toolbar is created with an ActionBar widget in which buttons
987+
are added with a specific behavior given by a callback.
988+
The buttons properties are given by matplotlib.
995989
"""
996990
basedir = os.path.join(rcParams["datapath"], "images")
997991
actionview = ActionView()
998-
actionprevious = ActionPrevious(title="Navigation", with_previous=False)
992+
actionprevious = ActionPrevious(title="Navigation",
993+
with_previous=False)
999994
actionoverflow = ActionOverflow()
1000995
actionview.add_widget(actionprevious)
1001996
actionview.add_widget(actionoverflow)
@@ -1217,31 +1212,21 @@ def on_touch_down(self, touch):
12171212
newcoord = self.to_widget(touch.x, touch.y, relative=True)
12181213
x = newcoord[0]
12191214
y = newcoord[1]
1220-
12211215
if super(FigureCanvasKivy, self).on_touch_down(touch):
12221216
return True
12231217
if self.collide_point(*touch.pos):
1224-
self.motion_notify_event(x, y, guiEvent=None)
1225-
1218+
self.motion_notify_event(x, y)
12261219
touch.grab(self)
1227-
if "button" in touch.profile and touch.button in (
1228-
"scrollup",
1229-
"scrolldown",
1230-
):
1231-
self.scroll_event(x, y, 5, guiEvent=None)
1220+
if 'button' in touch.profile and touch.button in ("scrollup",
1221+
"scrolldown"):
1222+
self.scroll_event(x, y, 5)
12321223
else:
1233-
self.button_press_event(
1234-
x,
1235-
y,
1236-
self.get_mouse_button(touch),
1237-
dblclick=False,
1238-
guiEvent=None,
1239-
)
1224+
self.button_press_event(x, y, self.get_mouse_button(touch))
12401225
if self.entered_figure:
1241-
self.enter_notify_event(guiEvent=None, xy=None)
1226+
self.enter_notify_event()
12421227
else:
12431228
if not self.entered_figure:
1244-
self.leave_notify_event(guiEvent=None)
1229+
self.leave_notify_event()
12451230
return False
12461231

12471232
def on_touch_move(self, touch):
@@ -1253,12 +1238,12 @@ def on_touch_move(self, touch):
12531238
y = newcoord[1]
12541239
inside = self.collide_point(touch.x, touch.y)
12551240
if inside:
1256-
self.motion_notify_event(x, y, guiEvent=None)
1241+
self.motion_notify_event(x, y)
12571242
if not inside and not self.entered_figure:
1258-
self.leave_notify_event(guiEvent=None)
1243+
self.leave_notify_event()
12591244
self.entered_figure = True
12601245
elif inside and self.entered_figure:
1261-
self.enter_notify_event(guiEvent=None, xy=(x, y))
1246+
self.enter_notify_event()
12621247
self.entered_figure = False
12631248
return False
12641249

@@ -1284,31 +1269,27 @@ def on_touch_up(self, touch):
12841269
x = newcoord[0]
12851270
y = newcoord[1]
12861271
if touch.grab_current is self:
1287-
if "button" in touch.profile and touch.button in (
1288-
"scrollup",
1289-
"scrolldown",
1290-
):
1291-
self.scroll_event(x, y, 5, guiEvent=None)
1272+
if 'button' in touch.profile and touch.button in ("scrollup",
1273+
"scrolldown"):
1274+
self.scroll_event(x, y, 5)
12921275
else:
1293-
self.button_release_event(
1294-
x, y, self.get_mouse_button(touch), guiEvent=None
1295-
)
1276+
self.button_release_event(x, y, self.get_mouse_button(touch))
12961277
touch.ungrab(self)
12971278
else:
12981279
return super(FigureCanvasKivy, self).on_touch_up(touch)
12991280
return False
13001281

13011282
def keyboard_on_key_down(self, window, keycode, text, modifiers):
13021283
"""Kivy event to trigger matplotlib `key_press_event`."""
1303-
self.key_press_event(keycode[1], guiEvent=None)
1284+
self.key_press_event(key=keycode[1])
13041285
return super(FigureCanvasKivy, self).keyboard_on_key_down(
1305-
window, keycode, text, modifiers
1306-
)
1286+
window, keycode, text, modifiers)
13071287

13081288
def keyboard_on_key_up(self, window, keycode):
13091289
"""Kivy event to trigger matplotlib `key_release_event`."""
1310-
self.key_release_event(keycode[1], guiEvent=None)
1311-
return super(FigureCanvasKivy, self).keyboard_on_key_up(window, keycode)
1290+
self.key_release_event(key=keycode[1])
1291+
return super(FigureCanvasKivy, self).keyboard_on_key_up(
1292+
window, keycode)
13121293

13131294
def _on_mouse_pos(self, *args):
13141295
"""Kivy Event to trigger the following matplotlib events:
@@ -1321,22 +1302,85 @@ def _on_mouse_pos(self, *args):
13211302
y = newcoord[1]
13221303
inside = self.collide_point(*pos)
13231304
if inside:
1324-
self.motion_notify_event(x, y, guiEvent=None)
1305+
self.motion_notify_event(x, y)
13251306
if not inside and not self.entered_figure:
1326-
self.leave_notify_event(guiEvent=None)
1307+
self.leave_notify_event()
13271308
self.entered_figure = True
13281309
elif inside and self.entered_figure:
1329-
self.enter_notify_event(guiEvent=None, xy=(pos[0], pos[1]))
1310+
self.enter_notify_event()
13301311
self.entered_figure = False
13311312

1332-
def enter_notify_event(self, guiEvent=None, xy=None):
1333-
event = Event("figure_enter_event", self, guiEvent)
1313+
def enter_notify_event(self, gui_event=None):
1314+
event = Event("figure_enter_event", self, gui_event)
13341315
self.callbacks.process("figure_enter_event", event)
13351316

1336-
def leave_notify_event(self, guiEvent=None):
1337-
event = Event("figure_leave_event", self, guiEvent)
1317+
def leave_notify_event(self, gui_event=None):
1318+
event = Event("figure_leave_event", self, gui_event)
13381319
self.callbacks.process("figure_leave_event", event)
13391320

1321+
def resize_event(self):
1322+
event = ResizeEvent('resize_event', self)
1323+
self.callbacks.process('resize_event', event)
1324+
1325+
def motion_notify_event(self, x, y, gui_event=None):
1326+
event = MouseEvent(
1327+
'motion_notify_event',
1328+
canvas=self,
1329+
x=x,
1330+
y=y,
1331+
guiEvent=gui_event)
1332+
self.callbacks.process('motion_notify_event', event)
1333+
1334+
def button_press_event(self, x, y, button,
1335+
dblclick=False, gui_event=None):
1336+
event = MouseEvent(
1337+
'button_press_event',
1338+
canvas=self,
1339+
x=x,
1340+
y=y,
1341+
button=button,
1342+
dblclick=dblclick,
1343+
guiEvent=gui_event)
1344+
self.callbacks.process('button_press_event', event)
1345+
1346+
def button_release_event(self, x, y, button,
1347+
dblclick=False, gui_event=None):
1348+
event = MouseEvent(
1349+
'button_release_event',
1350+
canvas=self,
1351+
x=x,
1352+
y=y,
1353+
button=button,
1354+
dblclick=dblclick,
1355+
guiEvent=gui_event)
1356+
self.callbacks.process('button_release_event', event)
1357+
1358+
def scroll_event(self, x, y, step, gui_event=None):
1359+
event = MouseEvent(
1360+
'scroll_event',
1361+
canvas=self,
1362+
x=x,
1363+
y=y,
1364+
step=step,
1365+
guiEvent=gui_event)
1366+
self.callbacks.process('scroll_event', event)
1367+
1368+
def key_press_event(self, key, gui_event=None):
1369+
event = KeyEvent(
1370+
'key_press_event',
1371+
canvas=self,
1372+
key=key,
1373+
guiEvent=gui_event)
1374+
self.callbacks.process('key_press_event', event)
1375+
1376+
def key_release_event(self, key, gui_event=None):
1377+
event = KeyEvent(
1378+
'key_release_event',
1379+
canvas=self,
1380+
key=key,
1381+
guiEvent=gui_event)
1382+
self.callbacks.process('key_release_event', event)
1383+
13401384
def _on_pos_changed(self, *args):
13411385
self.draw()
13421386

@@ -1346,6 +1390,8 @@ def _on_size_changed(self, *args):
13461390
size.
13471391
"""
13481392
w, h = self.size
1393+
if w <= 0 or h <= 0:
1394+
return
13491395
dpival = self.figure.dpi
13501396
winch = float(w) / dpival
13511397
hinch = float(h) / dpival

kivy_garden/matplotlib/backend_kivyagg.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,7 @@ def my_callback(event):
7373

7474
__all__ = "FigureCanvasKivyAgg"
7575

76-
import six
77-
78-
import matplotlib
79-
from matplotlib._pylab_helpers import Gcf
80-
from matplotlib.backend_bases import (
81-
RendererBase,
82-
GraphicsContextBase,
83-
FigureManagerBase,
84-
FigureCanvasBase,
85-
)
8676
from matplotlib.figure import Figure
87-
from matplotlib.transforms import Bbox
8877
from matplotlib.backends.backend_agg import FigureCanvasAgg
8978
from matplotlib.backend_bases import register_backend, ShowBase
9079

@@ -96,7 +85,6 @@ def my_callback(event):
9685
from kivy.app import App
9786
from kivy.graphics.texture import Texture
9887
from kivy.graphics import Rectangle, Color
99-
from kivy.uix.widget import Widget
10088
from kivy.properties import ObjectProperty
10189
from kivy.base import EventLoop
10290
from kivy.uix.floatlayout import FloatLayout

kivy_garden/matplotlib/tests/__init__.py

Whitespace-only changes.

kivy_garden/matplotlib/tests/test_backend_kivy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ def test_matplotlib_use_backend():
22
import matplotlib
33
import matplotlib.pyplot as plt
44

5+
plt.close('all')
56
matplotlib.use("module://kivy_garden.matplotlib.backend_kivy")
67

78
plt.plot([1, 2, 3, 4])

kivy_garden/matplotlib/tests/test_backend_kivyagg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ def test_matplotlib_use_backend():
22
import matplotlib
33
import matplotlib.pyplot as plt
44

5+
plt.close('all')
56
matplotlib.use("module://kivy_garden.matplotlib.backend_kivyagg")
67

78
plt.plot([1, 2, 3, 4])

0 commit comments

Comments
 (0)