Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaead584

Browse files
authored
Merge pull request#17641 from tacaswell/qt_backports
Qt backports
2 parentsdf4c5c1 +ff32cc7 commitaead584

File tree

6 files changed

+75
-25
lines changed

6 files changed

+75
-25
lines changed

‎lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2293,7 +2293,7 @@ def start_event_loop(self, timeout=0):
22932293
The event loop blocks until a callback function triggers
22942294
`stop_event_loop`, or *timeout* is reached.
22952295
2296-
If *timeout* is negative, never timeout.
2296+
If *timeout* is0 ornegative, never timeout.
22972297
22982298
Only interactive backends need to reimplement this method and it relies
22992299
on `flush_events` being properly implemented.

‎lib/matplotlib/backends/backend_qt5.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
importmatplotlib.backends.qt_editor.figureoptionsasfigureoptions
1616
frommatplotlib.backends.qt_editor.formsubplottoolimportUiSubplotTool
1717
frommatplotlib.backend_managersimportToolManager
18-
18+
from .importqt_compat
1919
from .qt_compatimport (
2020
QtCore,QtGui,QtWidgets,_isdeleted,_getSaveFileName,
21-
is_pyqt5,__version__,QT_API)
21+
is_pyqt5,__version__,QT_API,_setDevicePixelRatioF,
22+
_devicePixelRatioF)
23+
2224

2325
backend_version=__version__
2426

@@ -267,12 +269,7 @@ def _update_figure_dpi(self):
267269

268270
@property
269271
def_dpi_ratio(self):
270-
# Not available on Qt4 or some older Qt5.
271-
try:
272-
# self.devicePixelRatio() returns 0 in rare cases
273-
returnself.devicePixelRatio()or1
274-
exceptAttributeError:
275-
return1
272+
return_devicePixelRatioF(self)
276273

277274
def_update_dpi(self):
278275
# As described in __init__ above, we need to be careful in cases with
@@ -454,8 +451,9 @@ def start_event_loop(self, timeout=0):
454451
ifhasattr(self,"_event_loop")andself._event_loop.isRunning():
455452
raiseRuntimeError("Event loop already running")
456453
self._event_loop=event_loop=QtCore.QEventLoop()
457-
iftimeout:
458-
timer=QtCore.QTimer.singleShot(timeout*1000,event_loop.quit)
454+
iftimeout>0:
455+
timer=QtCore.QTimer.singleShot(int(timeout*1000),
456+
event_loop.quit)
459457
event_loop.exec_()
460458

461459
defstop_event_loop(self,event=None):
@@ -683,8 +681,7 @@ def _icon(self, name, color=None):
683681
ifis_pyqt5():
684682
name=name.replace('.png','_large.png')
685683
pm=QtGui.QPixmap(os.path.join(self.basedir,name))
686-
ifhasattr(pm,'setDevicePixelRatio'):
687-
pm.setDevicePixelRatio(self.canvas._dpi_ratio)
684+
_setDevicePixelRatioF(pm,_devicePixelRatioF(self))
688685
ifcolorisnotNone:
689686
mask=pm.createMaskFromColor(QtGui.QColor('black'),
690687
QtCore.Qt.MaskOutColor)

‎lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .backend_qt5import (
1212
QtCore,QtGui,QtWidgets,_BackendQT5,FigureCanvasQT,FigureManagerQT,
1313
NavigationToolbar2QT,backend_version)
14-
from .qt_compatimportQT_API
14+
from .qt_compatimportQT_API,_setDevicePixelRatioF
1515

1616

1717
classFigureCanvasQTAgg(FigureCanvasAgg,FigureCanvasQT):
@@ -64,9 +64,7 @@ def paintEvent(self, event):
6464

6565
qimage=QtGui.QImage(buf,buf.shape[1],buf.shape[0],
6666
QtGui.QImage.Format_ARGB32_Premultiplied)
67-
ifhasattr(qimage,'setDevicePixelRatio'):
68-
# Not available on Qt4 or some older Qt5.
69-
qimage.setDevicePixelRatio(self._dpi_ratio)
67+
_setDevicePixelRatioF(qimage,self._dpi_ratio)
7068
# set origin using original QT coordinates
7169
origin=QtCore.QPoint(rect.left(),rect.top())
7270
painter.drawImage(origin,qimage)
@@ -87,7 +85,7 @@ def blit(self, bbox=None):
8785
bbox=self.figure.bbox
8886

8987
# repaint uses logical pixels, not physical pixels like the renderer.
90-
l,b,w,h= [pt/self._dpi_ratioforptinbbox.bounds]
88+
l,b,w,h= [int(pt/self._dpi_ratio)forptinbbox.bounds]
9189
t=b+h
9290
self.repaint(l,self.renderer.height/self._dpi_ratio-t,w,h)
9391

‎lib/matplotlib/backends/backend_qt5cairo.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from .backend_cairoimportcairo,FigureCanvasCairo,RendererCairo
44
from .backend_qt5importQtCore,QtGui,_BackendQT5,FigureCanvasQT
5-
from .qt_compatimportQT_API
5+
from .qt_compatimportQT_API,_setDevicePixelRatioF
66

77

88
classFigureCanvasQTCairo(FigureCanvasQT,FigureCanvasCairo):
@@ -19,8 +19,8 @@ def draw(self):
1919
defpaintEvent(self,event):
2020
self._update_dpi()
2121
dpi_ratio=self._dpi_ratio
22-
width=dpi_ratio*self.width()
23-
height=dpi_ratio*self.height()
22+
width=int(dpi_ratio*self.width())
23+
height=int(dpi_ratio*self.height())
2424
if (width,height)!=self._renderer.get_canvas_width_height():
2525
surface=cairo.ImageSurface(cairo.FORMAT_ARGB32,width,height)
2626
self._renderer.set_ctx_from_surface(surface)
@@ -33,9 +33,7 @@ def paintEvent(self, event):
3333
# QImage under PySide on Python 3.
3434
ifQT_API=='PySide':
3535
ctypes.c_long.from_address(id(buf)).value=1
36-
ifhasattr(qimage,'setDevicePixelRatio'):
37-
# Not available on Qt4 or some older Qt5.
38-
qimage.setDevicePixelRatio(dpi_ratio)
36+
_setDevicePixelRatioF(qimage,dpi_ratio)
3937
painter=QtGui.QPainter(self)
4038
painter.eraseRect(event.rect())
4139
painter.drawImage(0,0,qimage)

‎lib/matplotlib/backends/qt_compat.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,38 @@ def is_pyqt5():
173173
# These globals are only defined for backcompatibility purposes.
174174
ETS=dict(pyqt=(QT_API_PYQTv2,4),pyside=(QT_API_PYSIDE,4),
175175
pyqt5=(QT_API_PYQT5,5),pyside2=(QT_API_PYSIDE2,5))
176-
QT_RC_MAJOR_VERSION=5ifis_pyqt5()else4
176+
177+
QT_RC_MAJOR_VERSION=int(QtCore.qVersion().split(".")[0])
178+
179+
180+
def_devicePixelRatioF(obj):
181+
"""
182+
Return obj.devicePixelRatioF() with graceful fallback for older Qt.
183+
184+
This can be replaced by the direct call when we require Qt>=5.6.
185+
"""
186+
try:
187+
# Not available on Qt<5.6
188+
returnobj.devicePixelRatioF()or1
189+
exceptAttributeError:
190+
pass
191+
try:
192+
# Not available on Qt4 or some older Qt5.
193+
# self.devicePixelRatio() returns 0 in rare cases
194+
returnobj.devicePixelRatio()or1
195+
exceptAttributeError:
196+
return1
197+
198+
199+
def_setDevicePixelRatioF(obj,val):
200+
"""
201+
Call obj.setDevicePixelRatioF(val) with graceful fallback for older Qt.
202+
203+
This can be replaced by the direct call when we require Qt>=5.6.
204+
"""
205+
ifhasattr(obj,'setDevicePixelRatioF'):
206+
# Not available on Qt<5.6
207+
obj.setDevicePixelRatioF(val)
208+
elifhasattr(obj,'setDevicePixelRatio'):
209+
# Not available on Qt4 or some older Qt5.
210+
obj.setDevicePixelRatio(val)

‎lib/matplotlib/tests/test_backend_qt.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,29 @@ def test_dpi_ratio_change():
253253
assertqt_canvas.get_width_height()== (600,240)
254254
assert (fig.get_size_inches()== (5,2)).all()
255255

256+
p.return_value=1.5
257+
258+
assertqt_canvas._dpi_ratio==1.5
259+
260+
qt_canvas.draw()
261+
qApp.processEvents()
262+
# this second processEvents is required to fully run the draw.
263+
# On `update` we notice the DPI has changed and trigger a
264+
# resize event to refresh, the second processEvents is
265+
# required to process that and fully update the window sizes.
266+
qApp.processEvents()
267+
268+
# The DPI and the renderer width/height change
269+
assertfig.dpi==180
270+
assertqt_canvas.renderer.width==900
271+
assertqt_canvas.renderer.height==360
272+
273+
# The actual widget size and figure physical size don't change
274+
assertsize.width()==600
275+
assertsize.height()==240
276+
assertqt_canvas.get_width_height()== (600,240)
277+
assert (fig.get_size_inches()== (5,2)).all()
278+
256279

257280
@pytest.mark.backend('Qt5Agg')
258281
deftest_subplottool():

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp