From ca9b0c7910e999a774af38ae98f114bc32810468 Mon Sep 17 00:00:00 2001 From: Karl Georg Bedrich Date: Mon, 1 Jun 2020 20:24:18 +0200 Subject: [PATCH] new method 'getAxpectRatio' with code taken from 'setAspectLocked' (#392) Co-authored-by: Ogi Moore --- pyqtgraph/graphicsItems/ViewBox/ViewBox.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pyqtgraph/graphicsItems/ViewBox/ViewBox.py b/pyqtgraph/graphicsItems/ViewBox/ViewBox.py index 94aa6243..e665deef 100644 --- a/pyqtgraph/graphicsItems/ViewBox/ViewBox.py +++ b/pyqtgraph/graphicsItems/ViewBox/ViewBox.py @@ -233,6 +233,17 @@ class ViewBox(GraphicsWidget): if name is None: self.updateViewLists() + def getAspectRatio(self): + '''return the current aspect ratio''' + rect = self.rect() + vr = self.viewRect() + if rect.height() == 0 or vr.width() == 0 or vr.height() == 0: + currentRatio = 1.0 + else: + currentRatio = (rect.width()/float(rect.height())) / ( + vr.width()/vr.height()) + return currentRatio + def register(self, name): """ Add this ViewBox to the registered list of views. @@ -1134,12 +1145,7 @@ class ViewBox(GraphicsWidget): return self.state['aspectLocked'] = False else: - rect = self.rect() - vr = self.viewRect() - if rect.height() == 0 or vr.width() == 0 or vr.height() == 0: - currentRatio = 1.0 - else: - currentRatio = (rect.width()/float(rect.height())) / (vr.width()/vr.height()) + currentRatio = self.getAspectRatio() if ratio is None: ratio = currentRatio if self.state['aspectLocked'] == ratio: # nothing to change