26#include <QInputDialog>
29#include <QContextMenuEvent>
34QMap< QString, QStringList > QgsRasterFormatSaveOptionsWidget::sBuiltinProfiles;
36static const QString PYRAMID_JPEG_YCBCR_COMPRESSION( QStringLiteral(
"JPEG_QUALITY_OVERVIEW=75 COMPRESS_OVERVIEW=JPEG PHOTOMETRIC_OVERVIEW=YCBCR INTERLEAVE_OVERVIEW=PIXEL" ) );
37static const QString PYRAMID_JPEG_COMPRESSION( QStringLiteral(
"JPEG_QUALITY_OVERVIEW=75 COMPRESS_OVERVIEW=JPEG INTERLEAVE_OVERVIEW=PIXEL" ) );
43 , mProvider( provider )
48 mOptionsTable->setMinimumSize( 200, mOptionsTable->verticalHeader()->defaultSectionSize() * 4 + mOptionsTable->horizontalHeader()->height() + 2 );
50 connect( mProfileNewButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mProfileNewButton_clicked );
51 connect( mProfileDeleteButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mProfileDeleteButton_clicked );
52 connect( mProfileResetButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mProfileResetButton_clicked );
53 connect( mOptionsAddButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mOptionsAddButton_clicked );
54 connect( mOptionsDeleteButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mOptionsDeleteButton_clicked );
55 connect( mOptionsLineEdit, &QLineEdit::editingFinished,
this, &QgsRasterFormatSaveOptionsWidget::mOptionsLineEdit_editingFinished );
59 if ( sBuiltinProfiles.isEmpty() )
62 sBuiltinProfiles[ QStringLiteral(
"z_adefault" )] = ( QStringList() << QString() << tr(
"Default" ) << QString() );
67 sBuiltinProfiles[ QStringLiteral(
"z_gtiff_1big" )] =
68 ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"No Compression" )
69 << QStringLiteral(
"COMPRESS=NONE BIGTIFF=IF_NEEDED" ) );
70 sBuiltinProfiles[ QStringLiteral(
"z_gtiff_2medium" )] =
71 ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"Low Compression" )
72 << QStringLiteral(
"COMPRESS=PACKBITS" ) );
73 sBuiltinProfiles[ QStringLiteral(
"z_gtiff_3small" )] =
74 ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"High Compression" )
75 << QStringLiteral(
"COMPRESS=DEFLATE PREDICTOR=2 ZLEVEL=9" ) );
76 sBuiltinProfiles[ QStringLiteral(
"z_gtiff_4jpeg" )] =
77 ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"JPEG Compression" )
78 << QStringLiteral(
"COMPRESS=JPEG JPEG_QUALITY=75" ) );
83 sBuiltinProfiles[ QStringLiteral(
"z__pyramids_gtiff_1big" )] =
84 ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"No Compression" )
85 << QStringLiteral(
"COMPRESS_OVERVIEW=NONE BIGTIFF_OVERVIEW=IF_NEEDED" ) );
86 sBuiltinProfiles[ QStringLiteral(
"z__pyramids_gtiff_2medium" )] =
87 ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"Low Compression" )
88 << QStringLiteral(
"COMPRESS_OVERVIEW=PACKBITS" ) );
89 sBuiltinProfiles[ QStringLiteral(
"z__pyramids_gtiff_3small" )] =
90 ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"High Compression" )
91 << QStringLiteral(
"COMPRESS_OVERVIEW=DEFLATE PREDICTOR_OVERVIEW=2 ZLEVEL=9" ) );
92 sBuiltinProfiles[ QStringLiteral(
"z__pyramids_gtiff_4jpeg" )] =
93 ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"JPEG Compression" )
94 << PYRAMID_JPEG_YCBCR_COMPRESSION );
97 connect( mProfileComboBox, &QComboBox::currentTextChanged,
98 this, &QgsRasterFormatSaveOptionsWidget::updateOptions );
99 connect( mOptionsTable, &QTableWidget::cellChanged,
this, &QgsRasterFormatSaveOptionsWidget::optionsTableChanged );
101 connect( mOptionsValidateButton, &QAbstractButton::clicked,
this, [ = ] {
validateOptions(); } );
104 mOptionsLineEdit->installEventFilter(
this );
107 setContextMenuPolicy( Qt::CustomContextMenu );
108 connect(
this, &QWidget::customContextMenuRequested,
this, [
this]( QPoint pos )
112 if ( mTableWidget->isVisible() )
113 text = tr(
"Use Simple Interface" );
115 text = tr(
"Use Table Interface" );
116 QAction *swapAction = menu.addAction( text );
117 connect( swapAction, &QAction::triggered,
this, [
this]() {swapOptionsUI( -1 ); } );
118 menu.exec( this->mapToGlobal( pos ) );
137 mProvider = provider;
144 const QList< QWidget * > widgets = this->findChildren<QWidget *>();
148 const auto constWidgets = widgets;
149 for ( QWidget *widget : constWidgets )
150 widget->setVisible(
false );
151 mOptionsWidget->setVisible(
true );
162 const auto constWidgets = widgets;
163 for ( QWidget *widget : constWidgets )
164 widget->setVisible(
true );
166 mProfileButtons->setVisible(
false );
176QString QgsRasterFormatSaveOptionsWidget::pseudoFormat()
const
178 return mPyramids ? QStringLiteral(
"_pyramids" ) : mFormat;
184 const QString format = pseudoFormat();
185 QStringList profileKeys = profiles();
186 QMapIterator<QString, QStringList> it( sBuiltinProfiles );
187 while ( it.hasNext() )
190 const QString profileKey = it.key();
191 if ( ! profileKeys.contains( profileKey ) && !it.value().isEmpty() )
194 if ( it.value()[0].isEmpty() || it.value()[0] == format )
196 profileKeys.insert( 0, profileKey );
200 std::sort( profileKeys.begin(), profileKeys.end() );
204 mProfileComboBox->blockSignals(
true );
205 mProfileComboBox->clear();
206 const auto constProfileKeys = profileKeys;
207 for (
const QString &profileKey : constProfileKeys )
209 QString profileName, profileOptions;
210 profileOptions = createOptions( profileKey );
211 if ( sBuiltinProfiles.contains( profileKey ) )
213 profileName = sBuiltinProfiles[ profileKey ][ 1 ];
214 if ( profileOptions.isEmpty() )
215 profileOptions = sBuiltinProfiles[ profileKey ][ 2 ];
219 profileName = profileKey;
221 mOptionsMap[ profileKey ] = profileOptions;
222 mProfileComboBox->addItem( profileName, profileKey );
226 mProfileComboBox->blockSignals(
false );
229 mProfileComboBox->setCurrentIndex( mProfileComboBox->findData( mySettings.
value(
230 mProvider +
"/driverOptions/" + format.toLower() +
"/defaultProfile",
235void QgsRasterFormatSaveOptionsWidget::updateOptions()
237 mBlockOptionUpdates++;
238 QString myOptions = mOptionsMap.value( currentProfileKey() );
239 QStringList myOptionsList = myOptions.trimmed().split(
' ', Qt::SkipEmptyParts );
243 if ( mRasterLayer && mRasterLayer->
bandCount() != 3 &&
244 myOptions == PYRAMID_JPEG_YCBCR_COMPRESSION )
246 myOptions = PYRAMID_JPEG_COMPRESSION;
249 if ( mTableWidget->isVisible() )
251 mOptionsTable->setRowCount( 0 );
252 for (
int i = 0; i < myOptionsList.count(); i++ )
254 QStringList key_value = myOptionsList[i].split(
'=' );
255 if ( key_value.count() == 2 )
257 mOptionsTable->insertRow( i );
258 mOptionsTable->setItem( i, 0,
new QTableWidgetItem( key_value[0] ) );
259 mOptionsTable->setItem( i, 1,
new QTableWidgetItem( key_value[1] ) );
265 mOptionsLineEdit->setText( myOptions );
266 mOptionsLineEdit->setCursorPosition( 0 );
269 mBlockOptionUpdates--;
282 if ( mProvider == QLatin1String(
"gdal" ) && !mFormat.isEmpty() && ! mPyramids )
285 if ( message.isEmpty() )
286 message = tr(
"Cannot get create options for driver %1" ).arg( mFormat );
288 else if ( mProvider == QLatin1String(
"gdal" ) && mPyramids )
290 message = tr(
"For details on pyramids options please see the following pages" );
291 message += QLatin1String(
"\n\nhttps://gdal.org/programs/gdaladdo.html\n\nhttps://gdal.org/drivers/raster/gtiff.html" );
294 message = tr(
"No help available" );
298 dlg->setWindowTitle( tr(
"Create Options for %1" ).arg( mFormat ) );
299 QTextEdit *textEdit =
new QTextEdit( dlg );
300 textEdit->setReadOnly(
true );
302 textEdit->setText( message );
303 dlg->
layout()->addWidget( textEdit );
304 dlg->resize( 600, 400 );
314 const QStringList createOptions =
options();
317 QgsDebugMsgLevel( QStringLiteral(
"layer: [%1] file: [%2] format: [%3]" ).arg( mRasterLayer ? mRasterLayer->
id() :
"none", mRasterFileName, mFormat ), 2 );
321 bool tmpLayer =
false;
322 if ( !( mRasterLayer && rasterLayer->
dataProvider() ) && ! mRasterFileName.isNull() )
327 rasterLayer =
new QgsRasterLayer( mRasterFileName, QFileInfo( mRasterFileName ).baseName(), QStringLiteral(
"gdal" ),
options );
330 if ( mProvider == QLatin1String(
"gdal" ) && mPyramids )
334 QgsDebugMsgLevel( QStringLiteral(
"calling validate pyramids on layer's data provider" ), 2 );
339 message = tr(
"cannot validate pyramid options" );
342 else if ( !createOptions.isEmpty() && mProvider == QLatin1String(
"gdal" ) && !mFormat.isEmpty() )
346 QgsDebugMsgLevel( QStringLiteral(
"calling validate on layer's data provider" ), 2 );
356 else if ( ! createOptions.isEmpty() )
358 QMessageBox::information(
this, QString(), tr(
"Cannot validate creation options." ), QMessageBox::Close );
366 if ( message.isNull() )
369 QMessageBox::information(
this, QString(), tr(
"Valid" ), QMessageBox::Close );
373 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 );
383void QgsRasterFormatSaveOptionsWidget::optionsTableChanged()
385 if ( mBlockOptionUpdates )
388 QTableWidgetItem *key, *value;
390 for (
int i = 0; i < mOptionsTable->rowCount(); i++ )
392 key = mOptionsTable->item( i, 0 );
393 if ( ! key || key->text().isEmpty() )
395 value = mOptionsTable->item( i, 1 );
396 if ( ! value || value->text().isEmpty() )
398 options += key->text() +
'=' + value->text() +
' ';
401 mOptionsMap[ currentProfileKey()] =
options;
402 mOptionsLineEdit->setText(
options );
403 mOptionsLineEdit->setCursorPosition( 0 );
406void QgsRasterFormatSaveOptionsWidget::mOptionsLineEdit_editingFinished()
408 mOptionsMap[ currentProfileKey()] = mOptionsLineEdit->text().trimmed();
411void QgsRasterFormatSaveOptionsWidget::mProfileNewButton_clicked()
413 QString profileName = QInputDialog::getText(
this, QString(), tr(
"Profile name:" ) );
414 if ( ! profileName.isEmpty() )
416 profileName = profileName.trimmed();
417 mOptionsMap[ profileName ] = QString();
418 mProfileComboBox->addItem( profileName, profileName );
419 mProfileComboBox->setCurrentIndex( mProfileComboBox->count() - 1 );
423void QgsRasterFormatSaveOptionsWidget::mProfileDeleteButton_clicked()
425 const int index = mProfileComboBox->currentIndex();
426 const QString profileKey = currentProfileKey();
427 if ( index != -1 && ! sBuiltinProfiles.contains( profileKey ) )
429 mOptionsMap.remove( profileKey );
430 mProfileComboBox->removeItem( index );
434void QgsRasterFormatSaveOptionsWidget::mProfileResetButton_clicked()
436 const QString profileKey = currentProfileKey();
437 if ( sBuiltinProfiles.contains( profileKey ) )
439 mOptionsMap[ profileKey ] = sBuiltinProfiles[ profileKey ][ 2 ];
443 mOptionsMap[ profileKey ] = QString();
445 mOptionsLineEdit->setText( mOptionsMap.value( currentProfileKey() ) );
446 mOptionsLineEdit->setCursorPosition( 0 );
450void QgsRasterFormatSaveOptionsWidget::optionsTableEnableDeleteButton()
452 mOptionsDeleteButton->setEnabled( mOptionsTable->currentRow() >= 0 );
455void QgsRasterFormatSaveOptionsWidget::mOptionsAddButton_clicked()
457 mOptionsTable->insertRow( mOptionsTable->rowCount() );
459 const int newRow = mOptionsTable->rowCount() - 1;
460 QTableWidgetItem *item =
new QTableWidgetItem();
461 mOptionsTable->setItem( newRow, 0, item );
462 mOptionsTable->setCurrentItem( item );
465void QgsRasterFormatSaveOptionsWidget::mOptionsDeleteButton_clicked()
467 if ( mOptionsTable->currentRow() >= 0 )
469 mOptionsTable->removeRow( mOptionsTable->currentRow() );
471 QTableWidgetItem *item = mOptionsTable->item( mOptionsTable->currentRow(), 0 );
472 mOptionsTable->setCurrentItem( item );
473 optionsTableChanged();
477QString QgsRasterFormatSaveOptionsWidget::settingsKey( QString profileName )
const
479 if ( !profileName.isEmpty() )
480 profileName =
"/profile_" + profileName;
482 profileName =
"/profile_default" + profileName;
483 return mProvider +
"/driverOptions/" + pseudoFormat().toLower() + profileName +
"/create";
486QString QgsRasterFormatSaveOptionsWidget::currentProfileKey()
const
488 return mProfileComboBox->currentData().toString();
493 return mOptionsMap.value( currentProfileKey() ).trimmed().split(
' ', Qt::SkipEmptyParts );
496QString QgsRasterFormatSaveOptionsWidget::createOptions(
const QString &profileName )
const
499 return mySettings.
value( settingsKey( profileName ),
"" ).toString();
502void QgsRasterFormatSaveOptionsWidget::deleteCreateOptions(
const QString &profileName )
505 mySettings.
remove( settingsKey( profileName ) );
508void QgsRasterFormatSaveOptionsWidget::setCreateOptions()
511 QStringList myProfiles;
512 QMap< QString, QString >::const_iterator i = mOptionsMap.constBegin();
513 while ( i != mOptionsMap.constEnd() )
515 setCreateOptions( i.key(), i.value() );
516 myProfiles << i.key();
519 mySettings.
setValue( mProvider +
"/driverOptions/" + pseudoFormat().toLower() +
"/profiles",
521 mySettings.
setValue( mProvider +
"/driverOptions/" + pseudoFormat().toLower() +
"/defaultProfile",
522 currentProfileKey().trimmed() );
525void QgsRasterFormatSaveOptionsWidget::setCreateOptions(
const QString &profileName,
const QString &options )
531void QgsRasterFormatSaveOptionsWidget::setCreateOptions(
const QString &profileName,
const QStringList &list )
533 setCreateOptions( profileName, list.join( QLatin1Char(
' ' ) ) );
536QStringList QgsRasterFormatSaveOptionsWidget::profiles()
const
539 return mySettings.
value( mProvider +
"/driverOptions/" + pseudoFormat().toLower() +
"/profiles",
"" ).toStringList();
542void QgsRasterFormatSaveOptionsWidget::swapOptionsUI(
int newIndex )
547 bool lineEditMode = mOptionsLineEdit->isVisible();
548 mOptionsLineEdit->setVisible( ( newIndex == -1 && !lineEditMode ) || newIndex == 1 );
549 mTableWidget->setVisible( ( newIndex == -1 && lineEditMode ) || newIndex == 0 );
553void QgsRasterFormatSaveOptionsWidget::updateControls()
555 const bool valid = mProvider == QLatin1String(
"gdal" ) && !mFormat.isEmpty();
556 mOptionsValidateButton->setEnabled( valid );
557 mOptionsHelpButton->setEnabled( valid );
561bool QgsRasterFormatSaveOptionsWidget::eventFilter( QObject *obj, QEvent *event )
563 if ( event->type() == QEvent::ContextMenu )
565 QContextMenuEvent *contextEvent =
static_cast<QContextMenuEvent *
>( event );
566 QMenu *menu =
nullptr;
567 menu = mOptionsLineEdit->createStandardContextMenu();
568 menu->addSeparator();
569 QAction *action =
new QAction( tr(
"Use Table Interface" ), menu );
570 menu->addAction( action );
571 connect( action, &QAction::triggered,
this, [
this] { swapOptionsUI( 0 ); } );
572 menu->exec( contextEvent->globalPos() );
578 return QObject::eventFilter( obj, event );
584 mOptionsTable->horizontalHeader()->resizeSection( 0, mOptionsTable->width() - 115 );
590 mBlockOptionUpdates++;
591 mOptionsTable->clearContents();
593 const QStringList optionsList =
options.trimmed().split(
' ', Qt::SkipEmptyParts );
594 for (
const QString &opt : optionsList )
596 const int rowCount = mOptionsTable->rowCount();
597 mOptionsTable->insertRow( rowCount );
599 const QStringList values = opt.split(
'=' );
600 if ( values.count() == 2 )
602 QTableWidgetItem *nameItem =
new QTableWidgetItem( values.at( 0 ) );
603 mOptionsTable->setItem( rowCount, 0, nameItem );
604 QTableWidgetItem *valueItem =
new QTableWidgetItem( values.at( 1 ) );
605 mOptionsTable->setItem( rowCount, 1, valueItem );
611 mProfileComboBox->setCurrentIndex( 0 );
613 mOptionsMap[ currentProfileKey()] =
options.trimmed();
614 mOptionsLineEdit->setText(
options.trimmed() );
615 mOptionsLineEdit->setCursorPosition( 0 );
617 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 &createOptions, 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.
This class is a composition of two QSettings instances:
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.
bool skipCrsValidation
Controls whether the layer is allowed to have an invalid/unknown CRS.