From 313a7e2fb426ad68665786738f3f067de906efd7 Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Tue, 3 Jul 2018 22:21:40 +0100 Subject: [PATCH] Added a new GlueApplication.screenshot method --- CHANGES.md | 3 +++ glue/app/qt/application.py | 11 +++++++++++ glue/app/qt/tests/test_application.py | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index a88425c9c..7f56775c3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,9 @@ v0.14.0 (unreleased) * Change 'Export Session' dialog to offer to save with relative paths to data by default instead of absolute paths. [#1803] +* Added a new method ``screenshot`` on ``GlueApplication`` to save a + screenshot of the current view. [#1808] + * Show the active subset in the toolbar. [#1797] * Refactored the viewer class base classes: diff --git a/glue/app/qt/application.py b/glue/app/qt/application.py index 863e9a9c6..a7eb0f66e 100644 --- a/glue/app/qt/application.py +++ b/glue/app/qt/application.py @@ -1353,6 +1353,17 @@ def _choose_merge(data, others, suggested_label): return None, None + def screenshot(self, filename): + """ + Save a screenshot of the current application window to a file. + """ + image = QtGui.QImage(self.size(), QtGui.QImage.Format_RGB32) + painter = QtGui.QPainter(image) + flags = self.IgnoreMask | self.DrawWindowBackground | self.DrawChildren + self.render(painter, QtCore.QPoint(), QtGui.QRegion(), flags) + image.save(filename) + painter.end() + def __gluestate__(self, context): state = super(GlueApplication, self).__gluestate__(context) state['tab_names'] = self.tab_names diff --git a/glue/app/qt/tests/test_application.py b/glue/app/qt/tests/test_application.py index 5303ea845..7b7676b71 100644 --- a/glue/app/qt/tests/test_application.py +++ b/glue/app/qt/tests/test_application.py @@ -2,6 +2,7 @@ from __future__ import absolute_import, division, print_function +import os import sys import pytest @@ -460,6 +461,12 @@ def test_deselect_tool_on_viewer_change(self): app.close() + def test_screenshot(self, tmpdir): + filename = tmpdir.join('screenshot.png').strpath + app = GlueApplication() + app.screenshot(filename) + assert os.path.exists(filename) + def test_logger_close():