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>
This commit is contained in:
hjk
2016-12-20 11:19:22 +01:00
parent f58a617ea9
commit cdaa4aee1a
3 changed files with 54 additions and 0 deletions

View File

@@ -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();