Skip to content

Commit 18f6562

Browse files
committed
save screenshots to sub-folders
1 parent b0e1cf5 commit 18f6562

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

scripts/create_screenshots.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
FILE=$1
6+
7+
QGIS_SRC=~/opt/qgis/QGIS
8+
OUT_PATH=~/Documents/temp/quick
9+
CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

src/app/qgsappscreenshots.cpp

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode, QRect
8080

8181
if ( !crop.isNull() )
8282
{
83+
qreal dpr = scr->devicePixelRatio();
8384
if ( crop.height() == 0 )
84-
crop.setHeight( pixmap.height() );
85+
crop.setHeight( static_cast<int>( pixmap.height() / dpr ) );
8586
if ( crop.width() == 0 )
86-
crop.setWidth( pixmap.width() );
87+
crop.setWidth( static_cast<int>( pixmap.width() / dpr ) );
8788
}
89+
8890
if ( !crop.isEmpty() )
8991
{
9092
qreal dpr = scr->devicePixelRatio();
@@ -95,7 +97,6 @@ QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode, QRect
9597
pixmap = pixmap.copy( crop );
9698
}
9799

98-
99100
if ( gradient )
100101
{
101102
QImage img = pixmap.toImage();
@@ -113,17 +114,35 @@ QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode, QRect
113114
return pixmap;
114115
}
115116

116-
void QgsAppScreenShots::takeScreenshot( const QString &name, QWidget *widget, QgsAppScreenShots::GrabMode mode )
117+
void QgsAppScreenShots::takeScreenshot( const QString &name, const QString &folder, QWidget *widget, QgsAppScreenShots::GrabMode mode )
117118
{
118119
QPixmap pixmap = takeScreenshot( widget, mode );
119-
saveScreenshot( pixmap, name );
120+
saveScreenshot( pixmap, name, folder );
120121
}
121122

122-
void QgsAppScreenShots::saveScreenshot( QPixmap &pixmap, const QString &name )
123+
void QgsAppScreenShots::saveScreenshot( QPixmap &pixmap, const QString &name, const QString &folder )
123124
{
124-
const QString &fileName = mSaveDirectory + "/" + name + ".png";
125-
pixmap.save( fileName );
126-
QgsMessageLog::logMessage( QString( "Screenshot saved: %1" ).arg( fileName ) );
125+
const QDir topDirectory( mSaveDirectory );
126+
if ( !topDirectory.exists() )
127+
{
128+
QgsMessageLog::logMessage( QString( "Directory does not exist: %1" ).arg( mSaveDirectory ), QString(), Qgis::Critical );
129+
return;
130+
}
131+
132+
const QDir directory( topDirectory.absolutePath() + "/" + folder );
133+
const QString fileName = directory.absolutePath() + "/" + name + ".png";
134+
if ( !directory.exists() )
135+
{
136+
if ( !topDirectory.mkpath( folder ) )
137+
{
138+
QgsMessageLog::logMessage( QString( "Could not create directory %1 in %2" ).arg( folder, mSaveDirectory ), QString(), Qgis::Critical );
139+
return;
140+
}
141+
}
142+
if ( pixmap.save( fileName ) )
143+
QgsMessageLog::logMessage( QString( "Screenshot saved: %1" ).arg( fileName ) );
144+
else
145+
QgsMessageLog::logMessage( QString( "Failed to save screenshot: %1" ).arg( fileName ), QString(), Qgis::Critical );
127146
}
128147

129148
void QgsAppScreenShots::moveWidgetTo( QWidget *widget, Qt::Corner corner, Reference reference )
@@ -187,6 +206,7 @@ void QgsAppScreenShots::setGradientSize( int size )
187206

188207
void QgsAppScreenShots::takeVectorLayerProperties()
189208
{
209+
QString folder = QLatin1String( "working_with_vector/img/auto_generated" );
190210
QString rootName = QLatin1String( "vectorlayerproperties_" );
191211
QgsVectorLayerProperties *dlg = new QgsVectorLayerProperties( mLineLayer, QgisApp::instance() );
192212
dlg->show();
@@ -199,7 +219,7 @@ void QgsAppScreenShots::takeVectorLayerProperties()
199219
QCoreApplication::processEvents();
200220
QString name = dlg->mOptionsListWidget->item( row )[0].text().toLower();
201221
name.replace( " ", "_" );
202-
takeScreenshot( rootName + name, dlg );
222+
takeScreenshot( rootName + name, folder, dlg );
203223
}
204224
// ------------------
205225
// style menu clicked
@@ -209,7 +229,7 @@ void QgsAppScreenShots::takeVectorLayerProperties()
209229
QCoreApplication::processEvents();
210230
dlg->mBtnStyle->click();
211231
QCoreApplication::processEvents();
212-
takeScreenshot( rootName + "style_menu", dlg );
232+
takeScreenshot( rootName + "style_menu", folder, dlg );
213233
QCoreApplication::processEvents();
214234
dlg->mBtnStyle->menu()->hide();
215235
QCoreApplication::processEvents();
@@ -219,8 +239,11 @@ void QgsAppScreenShots::takeVectorLayerProperties()
219239
dlg->deleteLater();
220240
}
221241

242+
//---------------
243+
222244
void QgsAppScreenShots::take25dSymbol()
223245
{
246+
QString folder = QLatin1String( "working_with_vector/img/auto_generated" );
224247
QString rootName = QLatin1String( "vectorlayerproperties_" );
225248
QgsVectorLayerProperties *dlg = new QgsVectorLayerProperties( mPolygonLayer, QgisApp::instance() );
226249
dlg->show();
@@ -239,9 +262,9 @@ void QgsAppScreenShots::take25dSymbol()
239262
QCoreApplication::processEvents();
240263
int cropHeight = w->mAdvancedConfigurationBox->mapTo( dlg, w->mAdvancedConfigurationBox->frameGeometry().bottomLeft() ).y();
241264
QPixmap pixmap = takeScreenshot( dlg, GrabWidgetAndFrame, QRect( 0, 0, 0, cropHeight ), true );
242-
saveScreenshot( pixmap, rootName + QLatin1String( "25dsymbol" ) );
265+
saveScreenshot( pixmap, rootName + QLatin1String( "25dsymbol" ), folder );
243266

244-
// exit properly
267+
// exit properly
245268
dlg->close();
246269
dlg->deleteLater();
247270
}

src/app/qgsappscreenshots.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class QgsAppScreenShots
6565
QScreen *screen( QWidget *widget = nullptr );
6666
void moveWidgetTo( QWidget *widget, Qt::Corner corner, Reference reference = Screen );
6767
//! take and directly save screenshot
68-
void takeScreenshot( const QString &name, QWidget *widget = nullptr, GrabMode mode = GrabWidgetAndFrame );
68+
void takeScreenshot( const QString &name, const QString &folder, QWidget *widget = nullptr, GrabMode mode = GrabWidgetAndFrame );
6969
//! take screenshot and return pixmap
7070
QPixmap takeScreenshot( QWidget *widget = nullptr, GrabMode mode = GrabWidgetAndFrame, QRect crop = QRect(), bool gradient = false );
7171

@@ -76,7 +76,7 @@ class QgsAppScreenShots
7676
* @param crop the crop can have only one dimension (empty but not null rect)
7777
* @param gradient
7878
*/
79-
void saveScreenshot( QPixmap &pixmap, const QString &name );
79+
void saveScreenshot( QPixmap &pixmap, const QString &name, const QString &folder );
8080

8181
void takeVectorLayerProperties();
8282
void take25dSymbol();

0 commit comments

Comments
 (0)