From afb8c371cf0b0361b94688a7578aa8035e6cb0ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20H=C3=B6reth?= Date: Sun, 10 Sep 2017 19:41:33 +0200 Subject: [PATCH] Re-render graphics on window resize. --- MPLAnimator.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/MPLAnimator.py b/MPLAnimator.py index e74cbb5..bb41e85 100644 --- a/MPLAnimator.py +++ b/MPLAnimator.py @@ -55,6 +55,7 @@ def initUI(self): self.w = QtWidgets.QWidget() self.layout = QtWidgets.QVBoxLayout() self.w.setLayout(self.layout) + self.w.resizeEvent = self._onResize # using a stacked layout for the figure # allows for quick exchange between pre-rendered image-view vs matplotlib figure @@ -134,11 +135,7 @@ def visualize(self, i=None): self.prerender() if self.stack.currentWidget() != self.label: self.stack.setCurrentWidget(self.label) - pm = QtGui.QPixmap('{}/{}.png'.format(self.dir, i)) - pm = pm.scaled(self.label.width(), self.label.height(), - Qt.KeepAspectRatio, Qt.SmoothTransformation) - self.label.setPixmap(pm) - self.label.setMinimumSize(1, 1) # Allow downsizing the window. + self._renderFrameFromFile(i) else: if self.stack.currentWidget() != self.canvas: self.stack.setCurrentWidget(self.canvas) @@ -179,3 +176,18 @@ def run(self, clear=False, prerendered=False, initialFrame=0): self.visualize(initialFrame) self.slider.setValue(initialFrame) self.qApp.exec() + + def _renderFrameFromFile(self, i=None): + """Render graphic from file given a frame number.""" + if i == None: + i = self.slider.value() + pm = QtGui.QPixmap('{}/{}.png'.format(self.dir, i)) + pm = pm.scaled(self.label.width(), self.label.height(), + Qt.KeepAspectRatio, Qt.SmoothTransformation) + self.label.setPixmap(pm) + self.label.setMinimumSize(1, 1) # Allow downsizing the window. + + def _onResize(self, event): # pylint: disable=unused-argument + """Re-render prerendered graphics on window resize.""" + if self.prerender_checkbox.isChecked(): + self._renderFrameFromFile()