@@ -80,11 +80,13 @@ QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode, QRect
80
80
81
81
if ( !crop.isNull () )
82
82
{
83
+ qreal dpr = scr->devicePixelRatio ();
83
84
if ( crop.height () == 0 )
84
- crop.setHeight ( pixmap.height () );
85
+ crop.setHeight ( static_cast < int >( pixmap.height () / dpr ) );
85
86
if ( crop.width () == 0 )
86
- crop.setWidth ( pixmap.width () );
87
+ crop.setWidth ( static_cast < int >( pixmap.width () / dpr ) );
87
88
}
89
+
88
90
if ( !crop.isEmpty () )
89
91
{
90
92
qreal dpr = scr->devicePixelRatio ();
@@ -95,7 +97,6 @@ QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode, QRect
95
97
pixmap = pixmap.copy ( crop );
96
98
}
97
99
98
-
99
100
if ( gradient )
100
101
{
101
102
QImage img = pixmap.toImage ();
@@ -113,17 +114,35 @@ QPixmap QgsAppScreenShots::takeScreenshot( QWidget *widget, GrabMode mode, QRect
113
114
return pixmap;
114
115
}
115
116
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 )
117
118
{
118
119
QPixmap pixmap = takeScreenshot ( widget, mode );
119
- saveScreenshot ( pixmap, name );
120
+ saveScreenshot ( pixmap, name, folder );
120
121
}
121
122
122
- void QgsAppScreenShots::saveScreenshot ( QPixmap &pixmap, const QString &name )
123
+ void QgsAppScreenShots::saveScreenshot ( QPixmap &pixmap, const QString &name, const QString &folder )
123
124
{
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 );
127
146
}
128
147
129
148
void QgsAppScreenShots::moveWidgetTo ( QWidget *widget, Qt::Corner corner, Reference reference )
@@ -187,6 +206,7 @@ void QgsAppScreenShots::setGradientSize( int size )
187
206
188
207
void QgsAppScreenShots::takeVectorLayerProperties ()
189
208
{
209
+ QString folder = QLatin1String ( " working_with_vector/img/auto_generated" );
190
210
QString rootName = QLatin1String ( " vectorlayerproperties_" );
191
211
QgsVectorLayerProperties *dlg = new QgsVectorLayerProperties ( mLineLayer , QgisApp::instance () );
192
212
dlg->show ();
@@ -199,7 +219,7 @@ void QgsAppScreenShots::takeVectorLayerProperties()
199
219
QCoreApplication::processEvents ();
200
220
QString name = dlg->mOptionsListWidget ->item ( row )[0 ].text ().toLower ();
201
221
name.replace ( " " , " _" );
202
- takeScreenshot ( rootName + name, dlg );
222
+ takeScreenshot ( rootName + name, folder, dlg );
203
223
}
204
224
// ------------------
205
225
// style menu clicked
@@ -209,7 +229,7 @@ void QgsAppScreenShots::takeVectorLayerProperties()
209
229
QCoreApplication::processEvents ();
210
230
dlg->mBtnStyle ->click ();
211
231
QCoreApplication::processEvents ();
212
- takeScreenshot ( rootName + " style_menu" , dlg );
232
+ takeScreenshot ( rootName + " style_menu" , folder, dlg );
213
233
QCoreApplication::processEvents ();
214
234
dlg->mBtnStyle ->menu ()->hide ();
215
235
QCoreApplication::processEvents ();
@@ -219,8 +239,11 @@ void QgsAppScreenShots::takeVectorLayerProperties()
219
239
dlg->deleteLater ();
220
240
}
221
241
242
+ // ---------------
243
+
222
244
void QgsAppScreenShots::take25dSymbol ()
223
245
{
246
+ QString folder = QLatin1String ( " working_with_vector/img/auto_generated" );
224
247
QString rootName = QLatin1String ( " vectorlayerproperties_" );
225
248
QgsVectorLayerProperties *dlg = new QgsVectorLayerProperties ( mPolygonLayer , QgisApp::instance () );
226
249
dlg->show ();
@@ -239,9 +262,9 @@ void QgsAppScreenShots::take25dSymbol()
239
262
QCoreApplication::processEvents ();
240
263
int cropHeight = w->mAdvancedConfigurationBox ->mapTo ( dlg, w->mAdvancedConfigurationBox ->frameGeometry ().bottomLeft () ).y ();
241
264
QPixmap pixmap = takeScreenshot ( dlg, GrabWidgetAndFrame, QRect ( 0 , 0 , 0 , cropHeight ), true );
242
- saveScreenshot ( pixmap, rootName + QLatin1String ( " 25dsymbol" ) );
265
+ saveScreenshot ( pixmap, rootName + QLatin1String ( " 25dsymbol" ), folder );
243
266
244
- // exit properly
267
+ // exit properly
245
268
dlg->close ();
246
269
dlg->deleteLater ();
247
270
}
0 commit comments