@@ -213,15 +213,6 @@ def __init__(self, figure=None):
213213 _create_qApp ()
214214 super ().__init__ (figure = figure )
215215
216- # We don't want to scale up the figure DPI more than once.
217- # Note, we don't handle a signal for changing DPI yet.
218- self .figure ._original_dpi = self .figure .dpi
219- self ._update_figure_dpi ()
220- # In cases with mixed resolution displays, we need to be careful if the
221- # dpi_ratio changes - in this case we need to resize the canvas
222- # accordingly.
223- self ._dpi_ratio_prev = self ._dpi_ratio
224-
225216 self ._draw_pending = False
226217 self ._is_drawing = False
227218 self ._draw_rect_callback = lambda painter : None
@@ -233,28 +224,13 @@ def __init__(self, figure=None):
233224 palette = QtGui .QPalette (QtCore .Qt .white )
234225 self .setPalette (palette )
235226
236- def _update_figure_dpi (self ):
237- dpi = self ._dpi_ratio * self .figure ._original_dpi
238- self .figure ._set_dpi (dpi , forward = False )
239-
240- @property
241- def _dpi_ratio (self ):
242- return _devicePixelRatioF (self )
243-
244227 def _update_pixel_ratio (self ):
245- # We need to be careful in cases with mixed resolution displays if
246- # dpi_ratio changes.
247- if self ._dpi_ratio != self ._dpi_ratio_prev :
248- # We need to update the figure DPI.
249- self ._update_figure_dpi ()
250- self ._dpi_ratio_prev = self ._dpi_ratio
228+ if self ._set_device_pixel_ratio (_devicePixelRatioF (self )):
251229 # The easiest way to resize the canvas is to emit a resizeEvent
252230 # since we implement all the logic for resizing the canvas for
253231 # that event.
254232 event = QtGui .QResizeEvent (self .size (), self .size ())
255233 self .resizeEvent (event )
256- # resizeEvent triggers a paintEvent itself, so we exit this one
257- # (after making sure that the event is immediately handled).
258234
259235 def _update_screen (self , screen ):
260236 # Handler for changes to a window's attached screen.
@@ -270,10 +246,6 @@ def showEvent(self, event):
270246 window .screenChanged .connect (self ._update_screen )
271247 self ._update_screen (window .screen ())
272248
273- def get_width_height (self ):
274- w , h = FigureCanvasBase .get_width_height (self )
275- return int (w / self ._dpi_ratio ), int (h / self ._dpi_ratio )
276-
277249 def enterEvent (self , event ):
278250 try :
279251 x , y = self .mouseEventCoords (event .pos ())
@@ -296,11 +268,10 @@ def mouseEventCoords(self, pos):
296268
297269 Also, the origin is different and needs to be corrected.
298270 """
299- dpi_ratio = self ._dpi_ratio
300271 x = pos .x ()
301272 # flip y so y=0 is bottom of canvas
302- y = self .figure .bbox .height / dpi_ratio - pos .y ()
303- return x * dpi_ratio , y * dpi_ratio
273+ y = self .figure .bbox .height / self . device_pixel_ratio - pos .y ()
274+ return x * self . device_pixel_ratio , y * self . device_pixel_ratio
304275
305276 def mousePressEvent (self , event ):
306277 x , y = self .mouseEventCoords (event .pos ())
@@ -361,8 +332,8 @@ def keyReleaseEvent(self, event):
361332 FigureCanvasBase .key_release_event (self , key , guiEvent = event )
362333
363334 def resizeEvent (self , event ):
364- w = event .size ().width () * self ._dpi_ratio
365- h = event .size ().height () * self ._dpi_ratio
335+ w = event .size ().width () * self .device_pixel_ratio
336+ h = event .size ().height () * self .device_pixel_ratio
366337 dpival = self .figure .dpi
367338 winch = w / dpival
368339 hinch = h / dpival
@@ -460,7 +431,7 @@ def blit(self, bbox=None):
460431 if bbox is None and self .figure :
461432 bbox = self .figure .bbox # Blit the entire canvas if bbox is None.
462433 # repaint uses logical pixels, not physical pixels like the renderer.
463- l , b , w , h = [int (pt / self ._dpi_ratio ) for pt in bbox .bounds ]
434+ l , b , w , h = [int (pt / self .device_pixel_ratio ) for pt in bbox .bounds ]
464435 t = b + h
465436 self .repaint (l , self .rect ().height () - t , w , h )
466437
@@ -481,11 +452,11 @@ def drawRectangle(self, rect):
481452 # Draw the zoom rectangle to the QPainter. _draw_rect_callback needs
482453 # to be called at the end of paintEvent.
483454 if rect is not None :
484- x0 , y0 , w , h = [int (pt / self ._dpi_ratio ) for pt in rect ]
455+ x0 , y0 , w , h = [int (pt / self .device_pixel_ratio ) for pt in rect ]
485456 x1 = x0 + w
486457 y1 = y0 + h
487458 def _draw_rect_callback (painter ):
488- pen = QtGui .QPen (QtCore .Qt .black , 1 / self ._dpi_ratio )
459+ pen = QtGui .QPen (QtCore .Qt .black , 1 / self .device_pixel_ratio )
489460 pen .setDashPattern ([3 , 3 ])
490461 for color , offset in [
491462 (QtCore .Qt .black , 0 ), (QtCore .Qt .white , 3 )]:
0 commit comments