27#include <QContextMenuEvent>
29#include <QInputDialog>
34#include "moc_qgsrasterformatsaveoptionswidget.cpp"
36QMap<QString, QStringList> QgsRasterFormatSaveOptionsWidget::sBuiltinProfiles;
38static const QString PYRAMID_JPEG_YCBCR_COMPRESSION( QStringLiteral(
"JPEG_QUALITY_OVERVIEW=75 COMPRESS_OVERVIEW=JPEG PHOTOMETRIC_OVERVIEW=YCBCR INTERLEAVE_OVERVIEW=PIXEL" ) );
39static const QString PYRAMID_JPEG_COMPRESSION( QStringLiteral(
"JPEG_QUALITY_OVERVIEW=75 COMPRESS_OVERVIEW=JPEG INTERLEAVE_OVERVIEW=PIXEL" ) );
44 , mProvider( provider )
49 mOptionsTable->setMinimumSize( 200, mOptionsTable->verticalHeader()->defaultSectionSize() * 4 + mOptionsTable->horizontalHeader()->height() + 2 );
51 connect( mProfileNewButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mProfileNewButton_clicked );
52 connect( mProfileDeleteButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mProfileDeleteButton_clicked );
53 connect( mProfileResetButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mProfileResetButton_clicked );
54 connect( mOptionsAddButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mOptionsAddButton_clicked );
55 connect( mOptionsDeleteButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mOptionsDeleteButton_clicked );
56 connect( mOptionsLineEdit, &QLineEdit::editingFinished,
this, &QgsRasterFormatSaveOptionsWidget::mOptionsLineEdit_editingFinished );
60 if ( sBuiltinProfiles.isEmpty() )
63 sBuiltinProfiles[QStringLiteral(
"z_adefault" )] = ( QStringList() << QString() << tr(
"Default" ) << QString() );
68 sBuiltinProfiles[QStringLiteral(
"z_gtiff_1big" )] = ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"No Compression" ) << QStringLiteral(
"COMPRESS=NONE BIGTIFF=IF_NEEDED" ) );
69 sBuiltinProfiles[QStringLiteral(
"z_gtiff_2medium" )] = ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"Low Compression" ) << QStringLiteral(
"COMPRESS=PACKBITS" ) );
70 sBuiltinProfiles[QStringLiteral(
"z_gtiff_3small" )] = ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"High Compression" ) << QStringLiteral(
"COMPRESS=DEFLATE PREDICTOR=2 ZLEVEL=9" ) );
71 sBuiltinProfiles[QStringLiteral(
"z_gtiff_4jpeg" )] = ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"JPEG Compression" ) << QStringLiteral(
"COMPRESS=JPEG JPEG_QUALITY=75" ) );
76 sBuiltinProfiles[QStringLiteral(
"z__pyramids_gtiff_1big" )] = ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"No Compression" ) << QStringLiteral(
"COMPRESS_OVERVIEW=NONE BIGTIFF_OVERVIEW=IF_NEEDED" ) );
77 sBuiltinProfiles[QStringLiteral(
"z__pyramids_gtiff_2medium" )] = ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"Low Compression" ) << QStringLiteral(
"COMPRESS_OVERVIEW=PACKBITS" ) );
78 sBuiltinProfiles[QStringLiteral(
"z__pyramids_gtiff_3small" )] = ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"High Compression" ) << QStringLiteral(
"COMPRESS_OVERVIEW=DEFLATE PREDICTOR_OVERVIEW=2 ZLEVEL=9" ) );
79 sBuiltinProfiles[QStringLiteral(
"z__pyramids_gtiff_4jpeg" )] = ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"JPEG Compression" ) << PYRAMID_JPEG_YCBCR_COMPRESSION );
82 connect( mProfileComboBox, &QComboBox::currentTextChanged,
this, &QgsRasterFormatSaveOptionsWidget::updateOptions );
83 connect( mOptionsTable, &QTableWidget::cellChanged,
this, &QgsRasterFormatSaveOptionsWidget::optionsTableChanged );
85 connect( mOptionsValidateButton, &QAbstractButton::clicked,
this, [
this] {
validateOptions(); } );
88 mOptionsLineEdit->installEventFilter(
this );
91 setContextMenuPolicy( Qt::CustomContextMenu );
92 connect(
this, &QWidget::customContextMenuRequested,
this, [
this]( QPoint pos ) {
95 if ( mTableWidget->isVisible() )
96 text = tr(
"Use Simple Interface" );
98 text = tr(
"Use Table Interface" );
99 QAction *swapAction = menu.addAction( text );
100 connect( swapAction, &QAction::triggered,
this, [
this]() { swapOptionsUI( -1 ); } );
101 menu.exec( this->mapToGlobal( pos ) );
120 mProvider = provider;
127 const QList<QWidget *> widgets = this->findChildren<QWidget *>();
131 const auto constWidgets = widgets;
132 for ( QWidget *widget : constWidgets )
133 widget->setVisible(
false );
134 mOptionsWidget->setVisible(
true );
145 const auto constWidgets = widgets;
146 for ( QWidget *widget : constWidgets )
147 widget->setVisible(
true );
149 mProfileButtons->setVisible(
false );
159QString QgsRasterFormatSaveOptionsWidget::pseudoFormat()
const
161 return mPyramids ? QStringLiteral(
"_pyramids" ) : mFormat;
167 const QString format = pseudoFormat();
168 QStringList profileKeys = profiles();
169 QMapIterator<QString, QStringList> it( sBuiltinProfiles );
170 while ( it.hasNext() )
173 const QString profileKey = it.key();
174 if ( !profileKeys.contains( profileKey ) && !it.value().isEmpty() )
177 if ( it.value()[0].isEmpty() || it.value()[0] == format )
179 profileKeys.insert( 0, profileKey );
183 std::sort( profileKeys.begin(), profileKeys.end() );
187 mProfileComboBox->blockSignals(
true );
188 mProfileComboBox->clear();
189 const auto constProfileKeys = profileKeys;
190 for (
const QString &profileKey : constProfileKeys )
192 QString profileName, profileOptions;
193 profileOptions = creationOptions( profileKey );
194 if ( sBuiltinProfiles.contains( profileKey ) )
196 profileName = sBuiltinProfiles[profileKey][1];
197 if ( profileOptions.isEmpty() )
198 profileOptions = sBuiltinProfiles[profileKey][2];
202 profileName = profileKey;
204 mOptionsMap[profileKey] = profileOptions;
205 mProfileComboBox->addItem( profileName, profileKey );
209 mProfileComboBox->blockSignals(
false );
212 mProfileComboBox->setCurrentIndex( mProfileComboBox->findData( mySettings.
value(
213 mProvider +
"/driverOptions/" + format.toLower() +
"/defaultProfile",
219void QgsRasterFormatSaveOptionsWidget::updateOptions()
221 mBlockOptionUpdates++;
222 QString myOptions = mOptionsMap.value( currentProfileKey() );
223 QStringList myOptionsList = myOptions.trimmed().split(
' ', Qt::SkipEmptyParts );
227 if ( mRasterLayer && mRasterLayer->
bandCount() != 3 && myOptions == PYRAMID_JPEG_YCBCR_COMPRESSION )
229 myOptions = PYRAMID_JPEG_COMPRESSION;
232 if ( mTableWidget->isVisible() )
234 mOptionsTable->setRowCount( 0 );
235 for (
int i = 0; i < myOptionsList.count(); i++ )
237 QStringList key_value = myOptionsList[i].split(
'=' );
238 if ( key_value.count() == 2 )
240 mOptionsTable->insertRow( i );
241 mOptionsTable->setItem( i, 0,
new QTableWidgetItem( key_value[0] ) );
242 mOptionsTable->setItem( i, 1,
new QTableWidgetItem( key_value[1] ) );
248 mOptionsLineEdit->setText( myOptions );
249 mOptionsLineEdit->setCursorPosition( 0 );
252 mBlockOptionUpdates--;
258 setCreationOptions();
265 if ( mProvider == QLatin1String(
"gdal" ) && !mFormat.isEmpty() && !mPyramids )
268 if ( message.isEmpty() )
269 message = tr(
"Cannot get create options for driver %1" ).arg( mFormat );
271 else if ( mProvider == QLatin1String(
"gdal" ) && mPyramids )
273 message = tr(
"For details on pyramids options please see the following pages" );
274 message += QLatin1String(
"\n\nhttps://gdal.org/programs/gdaladdo.html\n\nhttps://gdal.org/drivers/raster/gtiff.html" );
277 message = tr(
"No help available" );
281 dlg->setWindowTitle( tr(
"Create Options for %1" ).arg( mFormat ) );
282 QTextEdit *textEdit =
new QTextEdit( dlg );
283 textEdit->setReadOnly(
true );
285 textEdit->setText( message );
286 dlg->
layout()->addWidget( textEdit );
287 dlg->resize( 600, 400 );
297 const QStringList creationOptions =
options();
300 QgsDebugMsgLevel( QStringLiteral(
"layer: [%1] file: [%2] format: [%3]" ).arg( mRasterLayer ? mRasterLayer->id() :
"none", mRasterFileName, mFormat ), 2 );
304 bool tmpLayer =
false;
305 if ( !( mRasterLayer && rasterLayer->
dataProvider() ) && !mRasterFileName.isNull() )
309 options.skipCrsValidation =
true;
310 rasterLayer =
new QgsRasterLayer( mRasterFileName, QFileInfo( mRasterFileName ).baseName(), QStringLiteral(
"gdal" ),
options );
313 if ( mProvider == QLatin1String(
"gdal" ) && mPyramids )
317 QgsDebugMsgLevel( QStringLiteral(
"calling validate pyramids on layer's data provider" ), 2 );
322 message = tr(
"cannot validate pyramid options" );
325 else if ( !creationOptions.isEmpty() && mProvider == QLatin1String(
"gdal" ) && !mFormat.isEmpty() )
329 QgsDebugMsgLevel( QStringLiteral(
"calling validate on layer's data provider" ), 2 );
338 else if ( !creationOptions.isEmpty() )
340 QMessageBox::information(
this, QString(), tr(
"Cannot validate creation options." ), QMessageBox::Close );
348 if ( message.isNull() )
351 QMessageBox::information(
this, QString(), tr(
"Valid" ), QMessageBox::Close );
355 QMessageBox::warning(
this, QString(), tr(
"Invalid %1:\n\n%2\n\nClick on help button to get valid creation options for this format." ).arg( mPyramids ? tr(
"pyramid creation option" ) : tr(
"creation option" ), message ), QMessageBox::Close );
365void QgsRasterFormatSaveOptionsWidget::optionsTableChanged()
367 if ( mBlockOptionUpdates )
370 QTableWidgetItem *key, *value;
372 for (
int i = 0; i < mOptionsTable->rowCount(); i++ )
374 key = mOptionsTable->item( i, 0 );
375 if ( !key || key->text().isEmpty() )
377 value = mOptionsTable->item( i, 1 );
378 if ( !value || value->text().isEmpty() )
380 options += key->text() +
'=' + value->text() +
' ';
383 mOptionsMap[currentProfileKey()] =
options;
384 mOptionsLineEdit->setText(
options );
385 mOptionsLineEdit->setCursorPosition( 0 );
388void QgsRasterFormatSaveOptionsWidget::mOptionsLineEdit_editingFinished()
390 mOptionsMap[currentProfileKey()] = mOptionsLineEdit->text().trimmed();
393void QgsRasterFormatSaveOptionsWidget::mProfileNewButton_clicked()
395 QString profileName = QInputDialog::getText(
this, QString(), tr(
"Profile name:" ) );
396 if ( !profileName.isEmpty() )
398 profileName = profileName.trimmed();
399 mOptionsMap[profileName] = QString();
400 mProfileComboBox->addItem( profileName, profileName );
401 mProfileComboBox->setCurrentIndex( mProfileComboBox->count() - 1 );
405void QgsRasterFormatSaveOptionsWidget::mProfileDeleteButton_clicked()
407 const int index = mProfileComboBox->currentIndex();
408 const QString profileKey = currentProfileKey();
409 if ( index != -1 && !sBuiltinProfiles.contains( profileKey ) )
411 mOptionsMap.remove( profileKey );
412 mProfileComboBox->removeItem( index );
416void QgsRasterFormatSaveOptionsWidget::mProfileResetButton_clicked()
418 const QString profileKey = currentProfileKey();
419 if ( sBuiltinProfiles.contains( profileKey ) )
421 mOptionsMap[profileKey] = sBuiltinProfiles[profileKey][2];
425 mOptionsMap[profileKey] = QString();
427 mOptionsLineEdit->setText( mOptionsMap.value( currentProfileKey() ) );
428 mOptionsLineEdit->setCursorPosition( 0 );
432void QgsRasterFormatSaveOptionsWidget::optionsTableEnableDeleteButton()
434 mOptionsDeleteButton->setEnabled( mOptionsTable->currentRow() >= 0 );
437void QgsRasterFormatSaveOptionsWidget::mOptionsAddButton_clicked()
439 mOptionsTable->insertRow( mOptionsTable->rowCount() );
441 const int newRow = mOptionsTable->rowCount() - 1;
442 QTableWidgetItem *item =
new QTableWidgetItem();
443 mOptionsTable->setItem( newRow, 0, item );
444 mOptionsTable->setCurrentItem( item );
447void QgsRasterFormatSaveOptionsWidget::mOptionsDeleteButton_clicked()
449 if ( mOptionsTable->currentRow() >= 0 )
451 mOptionsTable->removeRow( mOptionsTable->currentRow() );
453 QTableWidgetItem *item = mOptionsTable->item( mOptionsTable->currentRow(), 0 );
454 mOptionsTable->setCurrentItem( item );
455 optionsTableChanged();
459QString QgsRasterFormatSaveOptionsWidget::settingsKey( QString profileName )
const
461 if ( !profileName.isEmpty() )
462 profileName =
"/profile_" + profileName;
464 profileName =
"/profile_default" + profileName;
465 return mProvider +
"/driverOptions/" + pseudoFormat().toLower() + profileName +
"/create";
468QString QgsRasterFormatSaveOptionsWidget::currentProfileKey()
const
470 return mProfileComboBox->currentData().toString();
475 return mOptionsMap.value( currentProfileKey() ).trimmed().split(
' ', Qt::SkipEmptyParts );
478QString QgsRasterFormatSaveOptionsWidget::creationOptions(
const QString &profileName )
const
481 return mySettings.
value( settingsKey( profileName ),
"" ).toString();
484void QgsRasterFormatSaveOptionsWidget::deleteCreationOptions(
const QString &profileName )
486 QgsSettings mySettings;
487 mySettings.
remove( settingsKey( profileName ) );
490void QgsRasterFormatSaveOptionsWidget::setCreationOptions()
492 QgsSettings mySettings;
493 QStringList myProfiles;
494 QMap<QString, QString>::const_iterator i = mOptionsMap.constBegin();
495 while ( i != mOptionsMap.constEnd() )
497 setCreationOptions( i.key(), i.value() );
498 myProfiles << i.key();
501 mySettings.
setValue( mProvider +
"/driverOptions/" + pseudoFormat().toLower() +
"/profiles", myProfiles );
502 mySettings.
setValue( mProvider +
"/driverOptions/" + pseudoFormat().toLower() +
"/defaultProfile", currentProfileKey().trimmed() );
505void QgsRasterFormatSaveOptionsWidget::setCreationOptions(
const QString &profileName,
const QString &options )
507 QgsSettings mySettings;
511void QgsRasterFormatSaveOptionsWidget::setCreationOptions(
const QString &profileName,
const QStringList &options )
513 setCreationOptions( profileName,
options.join( QLatin1Char(
' ' ) ) );
516QStringList QgsRasterFormatSaveOptionsWidget::profiles()
const
518 const QgsSettings mySettings;
519 return mySettings.
value( mProvider +
"/driverOptions/" + pseudoFormat().toLower() +
"/profiles",
"" ).toStringList();
522void QgsRasterFormatSaveOptionsWidget::swapOptionsUI(
int newIndex )
527 bool lineEditMode = mOptionsLineEdit->isVisible();
528 mOptionsLineEdit->setVisible( ( newIndex == -1 && !lineEditMode ) || newIndex == 1 );
529 mTableWidget->setVisible( ( newIndex == -1 && lineEditMode ) || newIndex == 0 );
533void QgsRasterFormatSaveOptionsWidget::updateControls()
535 const bool valid = mProvider == QLatin1String(
"gdal" ) && !mFormat.isEmpty();
536 mOptionsValidateButton->setEnabled( valid );
537 mOptionsHelpButton->setEnabled( valid );
541bool QgsRasterFormatSaveOptionsWidget::eventFilter( QObject *obj, QEvent *event )
543 if ( event->type() == QEvent::ContextMenu )
545 QContextMenuEvent *contextEvent =
static_cast<QContextMenuEvent *
>( event );
546 QMenu *menu =
nullptr;
547 menu = mOptionsLineEdit->createStandardContextMenu();
548 menu->addSeparator();
549 QAction *action =
new QAction( tr(
"Use Table Interface" ), menu );
550 menu->addAction( action );
551 connect( action, &QAction::triggered,
this, [
this] { swapOptionsUI( 0 ); } );
552 menu->exec( contextEvent->globalPos() );
557 return QObject::eventFilter( obj, event );
564 mOptionsTable->horizontalHeader()->resizeSection( 0, mOptionsTable->width() - 115 );
570 mBlockOptionUpdates++;
571 mOptionsTable->clearContents();
573 const QStringList optionsList =
options.trimmed().split(
' ', Qt::SkipEmptyParts );
574 for (
const QString &opt : optionsList )
576 const int rowCount = mOptionsTable->rowCount();
577 mOptionsTable->insertRow( rowCount );
579 const QStringList values = opt.split(
'=' );
580 if ( values.count() == 2 )
582 QTableWidgetItem *nameItem =
new QTableWidgetItem( values.at( 0 ) );
583 mOptionsTable->setItem( rowCount, 0, nameItem );
584 QTableWidgetItem *valueItem =
new QTableWidgetItem( values.at( 1 ) );
585 mOptionsTable->setItem( rowCount, 1, valueItem );
591 mProfileComboBox->setCurrentIndex( 0 );
593 mOptionsMap[currentProfileKey()] =
options.trimmed();
594 mOptionsLineEdit->setText(
options.trimmed() );
595 mOptionsLineEdit->setCursorPosition( 0 );
597 mBlockOptionUpdates--;
A generic dialog with layout and button box.
QVBoxLayout * layout()
Returns the central layout. Widgets added to it must have this dialog as parent.
static QString helpCreationOptionsFormat(const QString &format)
Gets creation options metadata for a given format.
static QString validateCreationOptionsFormat(const QStringList &creationOptions, const QString &format)
Validates creation options for a given format, regardless of layer.
virtual QString validateCreationOptions(const QStringList &createOptions, const QString &format)
Validates creation options for a specific dataset and destination format.
virtual QString validatePyramidsConfigOptions(Qgis::RasterPyramidFormat pyramidsFormat, const QStringList &configOptions, const QString &fileFormat)
Validates pyramid creation options for a specific dataset and destination format.
Represents a raster layer.
int bandCount() const
Returns the number of bands in this layer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define QgsDebugMsgLevel(str, level)
Setting options for loading raster layers.