23
23
#include " qgsrastertransparency.h"
24
24
#include " qgsprojectionselectiondialog.h"
25
25
#include " qgssettings.h"
26
-
26
+ #include " qgsrasterfilewriter.h"
27
+ #include " cpl_string.h"
27
28
#include < gdal.h>
28
29
29
30
#include < QFileDialog>
@@ -74,13 +75,7 @@ QgsRasterLayerSaveAsDialog::QgsRasterLayerSaveAsDialog( QgsRasterLayer *rasterLa
74
75
75
76
toggleResolutionSize ();
76
77
77
- // only one hardcoded format at the moment
78
- QStringList myFormats;
79
- myFormats << QStringLiteral ( " GTiff" );
80
- Q_FOREACH ( const QString &myFormat, myFormats )
81
- {
82
- mFormatComboBox ->addItem ( myFormat );
83
- }
78
+ insertAvailableOutputFormats ();
84
79
85
80
// fill reasonable default values depending on the provider
86
81
if ( mDataProvider )
@@ -104,7 +99,7 @@ QgsRasterLayerSaveAsDialog::QgsRasterLayerSaveAsDialog( QgsRasterLayer *rasterLa
104
99
mCreateOptionsWidget ->setProvider ( mDataProvider ->name () );
105
100
if ( mDataProvider ->name () == QLatin1String ( " gdal" ) )
106
101
{
107
- mCreateOptionsWidget ->setFormat ( myFormats[ 0 ] );
102
+ mCreateOptionsWidget ->setFormat ( mFormatComboBox -> currentData (). toString () );
108
103
}
109
104
mCreateOptionsWidget ->setRasterLayer ( mRasterLayer );
110
105
mCreateOptionsWidget ->update ();
@@ -163,6 +158,77 @@ QgsRasterLayerSaveAsDialog::QgsRasterLayerSaveAsDialog( QgsRasterLayer *rasterLa
163
158
connect ( mExtentGroupBox , &QgsExtentGroupBox::extentChanged, this , &QgsRasterLayerSaveAsDialog::extentChanged );
164
159
165
160
recalcResolutionSize ();
161
+
162
+ QgsSettings settings;
163
+ restoreGeometry ( settings.value ( QStringLiteral ( " Windows/RasterLayerSaveAs/geometry" ) ).toByteArray () );
164
+ }
165
+
166
+ QgsRasterLayerSaveAsDialog::~QgsRasterLayerSaveAsDialog ()
167
+ {
168
+ QgsSettings settings;
169
+ settings.setValue ( QStringLiteral ( " Windows/RasterLayerSaveAs/geometry" ), saveGeometry () );
170
+ }
171
+
172
+ void QgsRasterLayerSaveAsDialog::insertAvailableOutputFormats ()
173
+ {
174
+ GDALAllRegister ();
175
+
176
+ int nDrivers = GDALGetDriverCount ();
177
+ QMap< int , QPair< QString, QString > > topPriorityDrivers;
178
+ QMap< QString, QString > lowPriorityDrivers;
179
+
180
+ for ( int i = 0 ; i < nDrivers; ++i )
181
+ {
182
+ GDALDriverH driver = GDALGetDriver ( i );
183
+ if ( driver )
184
+ {
185
+ char **driverMetadata = GDALGetMetadata ( driver, nullptr );
186
+
187
+ if ( CSLFetchBoolean ( driverMetadata, GDAL_DCAP_CREATE, false ) && CSLFetchBoolean ( driverMetadata, GDAL_DCAP_RASTER, false ) )
188
+ {
189
+ QString driverShortName = GDALGetDriverShortName ( driver );
190
+ QString driverLongName = GDALGetDriverLongName ( driver );
191
+ if ( driverShortName == QLatin1String ( " MEM" ) )
192
+ {
193
+ // in memory rasters are not (yet) supported because the GDAL dataset handle
194
+ // would need to be passed directly to QgsRasterLayer (it is not possible to
195
+ // close it in raster calculator and reopen the dataset again in raster layer)
196
+ continue ;
197
+ }
198
+ else if ( driverShortName == QLatin1String ( " VRT" ) )
199
+ {
200
+ // skip GDAL vrt driver, since we handle that format manually
201
+ continue ;
202
+ }
203
+ else if ( driverShortName == QStringLiteral ( " GTiff" ) )
204
+ {
205
+ // always list geotiff first
206
+ topPriorityDrivers.insert ( 1 , qMakePair ( driverLongName, driverShortName ) );
207
+ }
208
+ else if ( driverShortName == QStringLiteral ( " GPKG" ) )
209
+ {
210
+ // and gpkg second
211
+ topPriorityDrivers.insert ( 2 , qMakePair ( driverLongName, driverShortName ) );
212
+ }
213
+ else
214
+ {
215
+ lowPriorityDrivers.insert ( driverLongName, driverShortName );
216
+ }
217
+ }
218
+ }
219
+ }
220
+
221
+ // will be sorted by priority, so that geotiff and geopackage are listed first
222
+ for ( auto priorityDriversIt = topPriorityDrivers.constBegin (); priorityDriversIt != topPriorityDrivers.constEnd (); ++priorityDriversIt )
223
+ {
224
+ mFormatComboBox ->addItem ( priorityDriversIt.value ().first , priorityDriversIt.value ().second );
225
+ }
226
+ // will be sorted by driver name
227
+ for ( auto lowPriorityDriversIt = lowPriorityDrivers.constBegin (); lowPriorityDriversIt != lowPriorityDrivers.constEnd (); ++lowPriorityDriversIt )
228
+ {
229
+ mFormatComboBox ->addItem ( lowPriorityDriversIt.key (), lowPriorityDriversIt.value () );
230
+ }
231
+
166
232
}
167
233
168
234
void QgsRasterLayerSaveAsDialog::setValidators ()
@@ -212,12 +278,26 @@ void QgsRasterLayerSaveAsDialog::mBrowseButton_clicked()
212
278
}
213
279
else
214
280
{
215
- fileName = QFileDialog::getSaveFileName ( this , tr ( " Select output file" ), dirName, tr ( " GeoTIFF" ) + " (*.tif *.tiff *.TIF *.TIFF)" );
281
+ QStringList extensions = QgsRasterFileWriter::extensionsForFormat ( outputFormat () );
282
+ QString filter;
283
+ QString defaultExt;
284
+ if ( extensions.empty () )
285
+ filter = tr ( " All files (*.*)" );
286
+ else
287
+ {
288
+ filter = QStringLiteral ( " %1 (*.%2);;%3" ).arg ( mFormatComboBox ->currentText (),
289
+ extensions.join ( QStringLiteral ( " *." ) ),
290
+ tr ( " All files (*.*)" ) );
291
+ defaultExt = extensions.at ( 0 );
292
+ }
293
+
294
+ fileName = QFileDialog::getSaveFileName ( this , tr ( " Select output file" ), dirName, filter );
216
295
217
296
// ensure the user never omits the extension from the file name
218
- if ( !fileName.isEmpty () && !fileName.endsWith ( QLatin1String ( " .tif" ), Qt::CaseInsensitive ) && !fileName.endsWith ( QLatin1String ( " .tiff" ), Qt::CaseInsensitive ) )
297
+ QFileInfo fi ( fileName );
298
+ if ( !fileName.isEmpty () && fi.suffix ().isEmpty () )
219
299
{
220
- fileName += QLatin1String ( " .tif " ) ;
300
+ fileName += ' . ' + defaultExt ;
221
301
}
222
302
}
223
303
@@ -239,12 +319,12 @@ void QgsRasterLayerSaveAsDialog::mSaveAsLineEdit_textChanged( const QString &tex
239
319
}
240
320
241
321
242
- void QgsRasterLayerSaveAsDialog::mFormatComboBox_currentIndexChanged ( const QString &text )
322
+ void QgsRasterLayerSaveAsDialog::mFormatComboBox_currentIndexChanged ( const QString & )
243
323
{
244
324
// gdal-specific
245
325
if ( mDataProvider && mDataProvider ->name () == QLatin1String ( " gdal" ) )
246
326
{
247
- mCreateOptionsWidget ->setFormat ( text );
327
+ mCreateOptionsWidget ->setFormat ( outputFormat () );
248
328
mCreateOptionsWidget ->update ();
249
329
}
250
330
}
@@ -296,7 +376,7 @@ QString QgsRasterLayerSaveAsDialog::outputFileName() const
296
376
297
377
QString QgsRasterLayerSaveAsDialog::outputFormat () const
298
378
{
299
- return mFormatComboBox ->currentText ();
379
+ return mFormatComboBox ->currentData (). toString ();
300
380
}
301
381
302
382
QStringList QgsRasterLayerSaveAsDialog::createOptions () const
0 commit comments