|
| 1 | + |
| 2 | + |
| 3 | +#include <QWindow> |
| 4 | +#include <QScreen> |
| 5 | +#include <QImageWriter> |
| 6 | + |
| 7 | +#include "qgsappscreenshots.h" |
| 8 | + |
| 9 | +#include "qgsvectorlayerproperties.h" |
| 10 | +#include "qgsvectorlayer.h" |
| 11 | +#include "qgsproject.h" |
| 12 | +#include "qgsmessagelog.h" |
| 13 | + |
| 14 | +QgsAppScreenShots::QgsAppScreenShots( const QString &saveDirectory ) |
| 15 | + : mSaveDirectory( saveDirectory ) |
| 16 | +{ |
| 17 | + QString layerDef = QStringLiteral( "Point?crs=epsg:4326&field=pk:integer&field=my_text:string&field=my_integer:integer&field=my_double:double&key=pk" ); |
| 18 | + mVectorLayer = new QgsVectorLayer( layerDef, QStringLiteral( "Layer" ), QStringLiteral( "memory" ) ); |
| 19 | + QgsProject::instance()->addMapLayer( mVectorLayer ); |
| 20 | +} |
| 21 | + |
| 22 | +void QgsAppScreenShots::saveScreenshot( const QString &name, QWidget *widget, GrabMode mode ) |
| 23 | +{ |
| 24 | + QPixmap pix; |
| 25 | + int x = 0; |
| 26 | + int y = 0; |
| 27 | + int w = -1; |
| 28 | + int h = -1; |
| 29 | + |
| 30 | + QScreen *screen = QGuiApplication::primaryScreen(); |
| 31 | + if ( widget ) |
| 32 | + { |
| 33 | + const QWindow *window = widget->windowHandle(); |
| 34 | + if ( window ) |
| 35 | + { |
| 36 | + screen = window->screen(); |
| 37 | + } |
| 38 | + widget->raise(); |
| 39 | + if ( mode == GrabWidget ) |
| 40 | + { |
| 41 | + pix = widget->grab(); |
| 42 | + } |
| 43 | + else if ( mode == GrabWidgetAndFrame ) |
| 44 | + { |
| 45 | + const QRect geom = widget->frameGeometry(); |
| 46 | + QPoint tl = geom.topLeft(); |
| 47 | + x = tl.x(); |
| 48 | + y = tl.y(); |
| 49 | + w = geom.width(); |
| 50 | + h = geom.height(); |
| 51 | + } |
| 52 | + } |
| 53 | + if ( !widget || mode != GrabWidget ) |
| 54 | + { |
| 55 | + pix = screen->grabWindow( 0, x, y, w, h ); |
| 56 | + } |
| 57 | + |
| 58 | + const QString &fileName = mSaveDirectory + "/" + name + ".png"; |
| 59 | + pix.save( fileName ); |
| 60 | + QMetaEnum metaEnum = QMetaEnum::fromType<GrabMode>(); |
| 61 | + QgsMessageLog::logMessage( QString( "Screenshot saved: %1 (%2)" ).arg( fileName, metaEnum.key( mode ) ) ); |
| 62 | +} |
| 63 | + |
| 64 | +void QgsAppScreenShots::takeScreenshots( Categories categories ) |
| 65 | +{ |
| 66 | + if ( !categories || categories.testFlag( VectorLayerProperties ) ) |
| 67 | + takeVectorLayerProperties(); |
| 68 | +} |
| 69 | + |
| 70 | +void QgsAppScreenShots::takeVectorLayerProperties() |
| 71 | +{ |
| 72 | + QgsVectorLayerProperties *dlg = new QgsVectorLayerProperties( mVectorLayer ); |
| 73 | + dlg->show(); |
| 74 | + // ---------------- |
| 75 | + // do all the pages |
| 76 | + for ( int row = 0; row < dlg->mOptionsListWidget->count(); ++row ) |
| 77 | + { |
| 78 | + dlg->mOptionsListWidget->setCurrentRow( row ); |
| 79 | + dlg->adjustSize(); |
| 80 | + QCoreApplication::processEvents(); |
| 81 | + QString name = dlg->mOptionsListWidget->item( row )[0].text().toLower(); |
| 82 | + name.replace( " ", "_" ); |
| 83 | + saveScreenshot( name, dlg ); |
| 84 | + } |
| 85 | + // ------------------ |
| 86 | + // style menu clicked |
| 87 | + dlg->mOptionsListWidget->setCurrentRow( 0 ); |
| 88 | + QCoreApplication::processEvents(); |
| 89 | + dlg->mBtnStyle->click(); |
| 90 | + saveScreenshot( "style", dlg ); |
| 91 | + |
| 92 | + // exit properly |
| 93 | + dlg->close(); |
| 94 | + dlg->deleteLater(); |
| 95 | +} |
| 96 | + |
0 commit comments