Skip to content

Commit ffdf617

Browse files
committed
take screenshots in app for user documentation
1 parent 2a974e3 commit ffdf617

10 files changed

+175
-0
lines changed

python/gui/auto_generated/qgisinterface.sip.in

+7
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,13 @@ used for interacting and adding widgets and messages to the app's
598598
status bar (do not use the native Qt statusBar() method).
599599

600600
.. versionadded:: 3.0
601+
%End
602+
603+
virtual void takeAppScreenShots( const QString &saveDirectory, const int categories = 0 );
604+
%Docstring
605+
Take screenshots for the user documentation
606+
607+
.. versionadded:: 3.4
601608
%End
602609

603610
public slots: // TODO: do these functions really need to be slots?

src/app/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ SET(QGIS_APP_SRCS
1313
qgsappwindowmanager.cpp
1414
qgsaddattrdialog.cpp
1515
qgsaddtaborgroup.cpp
16+
qgsappscreenshots.cpp
1617
qgsjoindialog.cpp
1718
qgsannotationwidget.cpp
1819
qgsattributeactiondialog.cpp
@@ -236,6 +237,7 @@ SET (QGIS_APP_MOC_HDRS
236237
qgsaddattrdialog.h
237238
qgsalignrasterdialog.h
238239
qgsappbrowserproviders.h
240+
qgsappscreenshots.h
239241
qgsjoindialog.h
240242
qgsaddtaborgroup.h
241243
qgsannotationwidget.h

src/app/qgisapp.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();
149149
#include "qgsauthcertutils.h"
150150
#include "qgsauthsslerrorsdialog.h"
151151
#endif
152+
#include "qgsappscreenshots.h"
152153
#include "qgsbookmarks.h"
153154
#include "qgsbrowserdockwidget.h"
154155
#include "qgsadvanceddigitizingdockwidget.h"
@@ -13062,6 +13063,12 @@ void QgisApp::zoomToBookmarkIndex( const QModelIndex &index )
1306213063
mBookMarksDockWidget->zoomToBookmarkIndex( index );
1306313064
}
1306413065

13066+
void QgisApp::takeAppScreenShots( const QString &saveDirectory, const int categories )
13067+
{
13068+
QgsAppScreenShots ass( saveDirectory );
13069+
ass.takeScreenshots( QgsAppScreenShots::Categories( categories ) );
13070+
}
13071+
1306513072
// Slot that gets called when the project file was saved with an older
1306613073
// version of QGIS
1306713074

src/app/qgisapp.h

+3
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
696696
//! Returns pointer to the identify map tool - used by identify tool in 3D view
697697
QgsMapToolIdentifyAction *identifyMapTool() const { return mMapTools.mIdentify; }
698698

699+
//! Take screenshots for user documentation
700+
void takeAppScreenShots( const QString &saveDirectory, const int categories = 0 );
701+
699702
public slots:
700703
//! save current vector layer
701704
void saveAsFile( QgsMapLayer *layer = nullptr, bool onlySelected = false );

src/app/qgisappinterface.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -789,3 +789,8 @@ bool QgisAppInterface::askForDatumTransform( QgsCoordinateReferenceSystem source
789789
{
790790
return qgis->askUserForDatumTransform( sourceCrs, destinationCrs );
791791
}
792+
793+
void QgisAppInterface::takeAppScreenShots( const QString &saveDirectory, const int categories )
794+
{
795+
return qgis->takeAppScreenShots( saveDirectory, categories );
796+
}

src/app/qgisappinterface.h

+2
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,8 @@ class APP_EXPORT QgisAppInterface : public QgisInterface
551551

552552
bool askForDatumTransform( QgsCoordinateReferenceSystem sourceCrs, QgsCoordinateReferenceSystem destinationCrs ) override;
553553

554+
void takeAppScreenShots( const QString &saveDirectory, const int categories = 0 ) override;
555+
554556

555557
private slots:
556558

src/app/qgsappscreenshots.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+

src/app/qgsappscreenshots.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef QGSAPPSCREENSHOTS_H
2+
#define QGSAPPSCREENSHOTS_H
3+
4+
#include <QObject>
5+
6+
class QgsVectorLayer;
7+
8+
class QgsAppScreenShots
9+
{
10+
Q_GADGET
11+
public:
12+
enum GrabMode
13+
{
14+
GrabWidget,
15+
GrabWidgetAndFrame,
16+
GrabWholeWindow
17+
};
18+
Q_ENUM( GrabMode )
19+
20+
enum Category
21+
{
22+
VectorLayerProperties = 1,
23+
};
24+
Q_ENUM( Category )
25+
Q_DECLARE_FLAGS( Categories, Category )
26+
Q_FLAG( Categories )
27+
28+
QgsAppScreenShots( const QString &saveDirectory );
29+
30+
//! if categories is null, then takes all categories
31+
void takeScreenshots( Categories categories = nullptr );
32+
33+
private:
34+
void takeVectorLayerProperties();
35+
36+
37+
void saveScreenshot( const QString &name, QWidget *widget = nullptr, GrabMode mode = GrabWidgetAndFrame );
38+
39+
QString mSaveDirectory;
40+
QgsVectorLayer *mVectorLayer = nullptr;
41+
};
42+
43+
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsAppScreenShots::Categories )
44+
45+
#endif // QGSAPPSCREENSHOTS_H

src/app/qgsvectorlayerproperties.h

+2
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
251251

252252
private slots:
253253
void openPanel( QgsPanelWidget *panel );
254+
255+
friend class QgsAppScreenShots;
254256
};
255257

256258

src/gui/qgisinterface.h

+6
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,12 @@ class GUI_EXPORT QgisInterface : public QObject
528528
*/
529529
virtual QgsStatusBar *statusBarIface() = 0;
530530

531+
/**
532+
* Take screenshots for the user documentation
533+
* \since QGIS 3.4
534+
*/
535+
virtual void takeAppScreenShots( const QString &saveDirectory, const int categories = 0 ) {Q_UNUSED( saveDirectory ); Q_UNUSED( categories );}
536+
531537
public slots: // TODO: do these functions really need to be slots?
532538

533539
/* Exposed functions */

0 commit comments

Comments
 (0)