@@ -232,7 +232,6 @@ def close(event):
232
232
import six
233
233
import os
234
234
import matplotlib
235
- import matplotlib .transforms as transforms
236
235
from matplotlib ._pylab_helpers import Gcf
237
236
from matplotlib .backend_bases import (
238
237
RendererBase ,
@@ -243,12 +242,15 @@ def close(event):
243
242
TimerBase ,
244
243
)
245
244
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 )
248
251
from matplotlib .backends .backend_agg import FigureCanvasAgg
249
252
from matplotlib .mathtext import MathTextParser
250
253
from matplotlib import rcParams
251
- from hashlib import md5
252
254
from matplotlib import _path
253
255
254
256
try :
@@ -260,7 +262,6 @@ def close(event):
260
262
from kivy .graphics .texture import Texture
261
263
from kivy .graphics import Rectangle
262
264
from kivy .uix .widget import Widget
263
- from kivy .uix .label import Label
264
265
from kivy .uix .floatlayout import FloatLayout
265
266
from kivy .uix .behaviors import FocusBehavior
266
267
from kivy .uix .actionbar import (
@@ -286,12 +287,8 @@ def close(event):
286
287
from kivy .resources import resource_find
287
288
from kivy .uix .stencilview import StencilView
288
289
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
292
290
from kivy .uix .popup import Popup
293
291
from kivy .properties import ObjectProperty
294
- from kivy .uix .textinput import TextInput
295
292
from kivy .lang import Builder
296
293
from kivy .clock import Clock
297
294
from distutils .version import LooseVersion
@@ -300,12 +297,9 @@ def close(event):
300
297
_mpl_ge_2_0 = LooseVersion (matplotlib .__version__ ) >= LooseVersion ("2.0.0" )
301
298
302
299
import numpy as np
303
- import io
304
300
import textwrap
305
301
import uuid
306
302
import numbers
307
- from functools import partial
308
- from math import cos , sin , pi
309
303
310
304
kivy .require ("1.9.1" )
311
305
@@ -340,7 +334,7 @@ def build(self):
340
334
341
335
342
336
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."""
344
338
if matplotlib .is_interactive ():
345
339
figManager = Gcf .get_active ()
346
340
if figManager :
@@ -989,13 +983,14 @@ def __init__(self, canvas, **kwargs):
989
983
)
990
984
991
985
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.
995
989
"""
996
990
basedir = os .path .join (rcParams ["datapath" ], "images" )
997
991
actionview = ActionView ()
998
- actionprevious = ActionPrevious (title = "Navigation" , with_previous = False )
992
+ actionprevious = ActionPrevious (title = "Navigation" ,
993
+ with_previous = False )
999
994
actionoverflow = ActionOverflow ()
1000
995
actionview .add_widget (actionprevious )
1001
996
actionview .add_widget (actionoverflow )
@@ -1217,31 +1212,21 @@ def on_touch_down(self, touch):
1217
1212
newcoord = self .to_widget (touch .x , touch .y , relative = True )
1218
1213
x = newcoord [0 ]
1219
1214
y = newcoord [1 ]
1220
-
1221
1215
if super (FigureCanvasKivy , self ).on_touch_down (touch ):
1222
1216
return True
1223
1217
if self .collide_point (* touch .pos ):
1224
- self .motion_notify_event (x , y , guiEvent = None )
1225
-
1218
+ self .motion_notify_event (x , y )
1226
1219
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 )
1232
1223
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 ))
1240
1225
if self .entered_figure :
1241
- self .enter_notify_event (guiEvent = None , xy = None )
1226
+ self .enter_notify_event ()
1242
1227
else :
1243
1228
if not self .entered_figure :
1244
- self .leave_notify_event (guiEvent = None )
1229
+ self .leave_notify_event ()
1245
1230
return False
1246
1231
1247
1232
def on_touch_move (self , touch ):
@@ -1253,12 +1238,12 @@ def on_touch_move(self, touch):
1253
1238
y = newcoord [1 ]
1254
1239
inside = self .collide_point (touch .x , touch .y )
1255
1240
if inside :
1256
- self .motion_notify_event (x , y , guiEvent = None )
1241
+ self .motion_notify_event (x , y )
1257
1242
if not inside and not self .entered_figure :
1258
- self .leave_notify_event (guiEvent = None )
1243
+ self .leave_notify_event ()
1259
1244
self .entered_figure = True
1260
1245
elif inside and self .entered_figure :
1261
- self .enter_notify_event (guiEvent = None , xy = ( x , y ) )
1246
+ self .enter_notify_event ()
1262
1247
self .entered_figure = False
1263
1248
return False
1264
1249
@@ -1284,31 +1269,27 @@ def on_touch_up(self, touch):
1284
1269
x = newcoord [0 ]
1285
1270
y = newcoord [1 ]
1286
1271
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 )
1292
1275
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 ))
1296
1277
touch .ungrab (self )
1297
1278
else :
1298
1279
return super (FigureCanvasKivy , self ).on_touch_up (touch )
1299
1280
return False
1300
1281
1301
1282
def keyboard_on_key_down (self , window , keycode , text , modifiers ):
1302
1283
"""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 ])
1304
1285
return super (FigureCanvasKivy , self ).keyboard_on_key_down (
1305
- window , keycode , text , modifiers
1306
- )
1286
+ window , keycode , text , modifiers )
1307
1287
1308
1288
def keyboard_on_key_up (self , window , keycode ):
1309
1289
"""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 )
1312
1293
1313
1294
def _on_mouse_pos (self , * args ):
1314
1295
"""Kivy Event to trigger the following matplotlib events:
@@ -1321,22 +1302,85 @@ def _on_mouse_pos(self, *args):
1321
1302
y = newcoord [1 ]
1322
1303
inside = self .collide_point (* pos )
1323
1304
if inside :
1324
- self .motion_notify_event (x , y , guiEvent = None )
1305
+ self .motion_notify_event (x , y )
1325
1306
if not inside and not self .entered_figure :
1326
- self .leave_notify_event (guiEvent = None )
1307
+ self .leave_notify_event ()
1327
1308
self .entered_figure = True
1328
1309
elif inside and self .entered_figure :
1329
- self .enter_notify_event (guiEvent = None , xy = ( pos [ 0 ], pos [ 1 ]) )
1310
+ self .enter_notify_event ()
1330
1311
self .entered_figure = False
1331
1312
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 )
1334
1315
self .callbacks .process ("figure_enter_event" , event )
1335
1316
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 )
1338
1319
self .callbacks .process ("figure_leave_event" , event )
1339
1320
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
+
1340
1384
def _on_pos_changed (self , * args ):
1341
1385
self .draw ()
1342
1386
@@ -1346,6 +1390,8 @@ def _on_size_changed(self, *args):
1346
1390
size.
1347
1391
"""
1348
1392
w , h = self .size
1393
+ if w <= 0 or h <= 0 :
1394
+ return
1349
1395
dpival = self .figure .dpi
1350
1396
winch = float (w ) / dpival
1351
1397
hinch = float (h ) / dpival
0 commit comments