diff options
author | hjk <hjk@qt.io> | 2016-12-20 11:19:22 +0100 |
---|---|---|
committer | hjk <hjk@qt.io> | 2017-11-16 10:28:49 +0000 |
commit | cdaa4aee1a5caa411b4f790ce1d6834da281eb54 (patch) | |
tree | dcc5aec6543511fbed236e66329aee7e9517c86d /src/plugins/coreplugin/icore.cpp | |
parent | f58a617ea9a593656cbf1ab82825c5c5026d9d77 (diff) |
Add some mechanism to help screenshot creation
If QTC_SCREENSHOTS_PATH points to a writable directory, each widget that
has been registered with ICore's setupScreenShooter(const QString &name,
QWidget *w) will dump a screen shot to this directory as soon as the
widget is shown.
Change-Id: I2dec12064f1bb3c510d2fd9d27c1b79f7b7d5f30
Reviewed-by: Jake Petroules <jake.petroules@qt.io>
Diffstat (limited to 'src/plugins/coreplugin/icore.cpp')
-rw-r--r-- | src/plugins/coreplugin/icore.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/plugins/coreplugin/icore.cpp b/src/plugins/coreplugin/icore.cpp index ab2a7f9770..550321396e 100644 --- a/src/plugins/coreplugin/icore.cpp +++ b/src/plugins/coreplugin/icore.cpp @@ -290,6 +290,7 @@ #include <QDebug> #include <QDir> #include <QStatusBar> +#include <QTimer> using namespace Core::Internal; using namespace ExtensionSystem; @@ -578,6 +579,56 @@ QString ICore::systemInformation() return result; } +static const QByteArray &screenShotsPath() +{ + static const QByteArray path = qgetenv("QTC_SCREENSHOTS_PATH"); + return path; +} + +class ScreenShooter : public QObject +{ +public: + ScreenShooter(QWidget *widget, const QString &name, const QRect &rc) + : m_widget(widget), m_name(name), m_rc(rc) + { + m_widget->installEventFilter(this); + } + + bool eventFilter(QObject *watched, QEvent *event) override + { + QTC_ASSERT(watched == m_widget, return false); + if (event->type() == QEvent::Show) + QTimer::singleShot(0, this, &ScreenShooter::helper); + return false; + } + + void helper() + { + if (m_widget) { + QRect rc = m_rc.isValid() ? m_rc : m_widget->rect(); + QPixmap pm = m_widget->grab(rc); + for (int i = 0; ; ++i) { + QString fileName = screenShotsPath() + '/' + m_name + QString("-%1.png").arg(i); + if (!QFileInfo::exists(fileName)) { + pm.save(fileName); + break; + } + } + } + deleteLater(); + } + + QPointer<QWidget> m_widget; + QString m_name; + QRect m_rc; +}; + +void ICore::setupScreenShooter(const QString &name, QWidget *w, const QRect &rc) +{ + if (!screenShotsPath().isEmpty()) + new ScreenShooter(w, name, rc); +} + void ICore::saveSettings() { emit m_instance->saveSettingsRequested(); |