19#include "moc_qgsrasterformatsaveoptionswidget.cpp"
27#include <QInputDialog>
30#include <QContextMenuEvent>
35QMap< QString, QStringList > QgsRasterFormatSaveOptionsWidget::sBuiltinProfiles;
37static const QString PYRAMID_JPEG_YCBCR_COMPRESSION( QStringLiteral(
"JPEG_QUALITY_OVERVIEW=75 COMPRESS_OVERVIEW=JPEG PHOTOMETRIC_OVERVIEW=YCBCR INTERLEAVE_OVERVIEW=PIXEL" ) );
38static 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" )] =
69 ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"No Compression" )
70 << QStringLiteral(
"COMPRESS=NONE BIGTIFF=IF_NEEDED" ) );
71 sBuiltinProfiles[ QStringLiteral(
"z_gtiff_2medium" )] =
72 ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"Low Compression" )
73 << QStringLiteral(
"COMPRESS=PACKBITS" ) );
74 sBuiltinProfiles[ QStringLiteral(
"z_gtiff_3small" )] =
75 ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"High Compression" )
76 << QStringLiteral(
"COMPRESS=DEFLATE PREDICTOR=2 ZLEVEL=9" ) );
77 sBuiltinProfiles[ QStringLiteral(
"z_gtiff_4jpeg" )] =
78 ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"JPEG Compression" )
79 << QStringLiteral(
"COMPRESS=JPEG JPEG_QUALITY=75" ) );
84 sBuiltinProfiles[ QStringLiteral(
"z__pyramids_gtiff_1big" )] =
85 ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"No Compression" )
86 << QStringLiteral(
"COMPRESS_OVERVIEW=NONE BIGTIFF_OVERVIEW=IF_NEEDED" ) );
87 sBuiltinProfiles[ QStringLiteral(
"z__pyramids_gtiff_2medium" )] =
88 ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"Low Compression" )
89 << QStringLiteral(
"COMPRESS_OVERVIEW=PACKBITS" ) );
90 sBuiltinProfiles[ QStringLiteral(
"z__pyramids_gtiff_3small" )] =
91 ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"High Compression" )
92 << QStringLiteral(
"COMPRESS_OVERVIEW=DEFLATE PREDICTOR_OVERVIEW=2 ZLEVEL=9" ) );
93 sBuiltinProfiles[ QStringLiteral(
"z__pyramids_gtiff_4jpeg" )] =
94 ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"JPEG Compression" )
95 << PYRAMID_JPEG_YCBCR_COMPRESSION );
98 connect( mProfileComboBox, &QComboBox::currentTextChanged,
99 this, &QgsRasterFormatSaveOptionsWidget::updateOptions );
100 connect( mOptionsTable, &QTableWidget::cellChanged,
this, &QgsRasterFormatSaveOptionsWidget::optionsTableChanged );
102 connect( mOptionsValidateButton, &QAbstractButton::clicked,
this, [ = ] {
validateOptions(); } );
105 mOptionsLineEdit->installEventFilter(
this );
108 setContextMenuPolicy( Qt::CustomContextMenu );
109 connect(
this, &QWidget::customContextMenuRequested,
this, [
this]( QPoint pos )
113 if ( mTableWidget->isVisible() )
114 text = tr(
"Use Simple Interface" );
116 text = tr(
"Use Table Interface" );
117 QAction *swapAction = menu.addAction( text );
118 connect( swapAction, &QAction::triggered,
this, [
this]() {swapOptionsUI( -1 ); } );
119 menu.exec( this->mapToGlobal( pos ) );
138 mProvider = provider;
145 const QList< QWidget * > widgets = this->findChildren<QWidget *>();
149 const auto constWidgets = widgets;
150 for ( QWidget *widget : constWidgets )
151 widget->setVisible(
false );
152 mOptionsWidget->setVisible(
true );
163 const auto constWidgets = widgets;
164 for ( QWidget *widget : constWidgets )
165 widget->setVisible(
true );
167 mProfileButtons->setVisible(
false );
177QString QgsRasterFormatSaveOptionsWidget::pseudoFormat()
const
179 return mPyramids ? QStringLiteral(
"_pyramids" ) : mFormat;
185 const QString format = pseudoFormat();
186 QStringList profileKeys = profiles();
187 QMapIterator<QString, QStringList> it( sBuiltinProfiles );
188 while ( it.hasNext() )
191 const QString profileKey = it.key();
192 if ( ! profileKeys.contains( profileKey ) && !it.value().isEmpty() )
195 if ( it.value()[0].isEmpty() || it.value()[0] == format )
197 profileKeys.insert( 0, profileKey );
201 std::sort( profileKeys.begin(), profileKeys.end() );
205 mProfileComboBox->blockSignals(
true );
206 mProfileComboBox->clear();
207 const auto constProfileKeys = profileKeys;
208 for (
const QString &profileKey : constProfileKeys )
210 QString profileName, profileOptions;
211 profileOptions = createOptions( profileKey );
212 if ( sBuiltinProfiles.contains( profileKey ) )
214 profileName = sBuiltinProfiles[ profileKey ][ 1 ];
215 if ( profileOptions.isEmpty() )
216 profileOptions = sBuiltinProfiles[ profileKey ][ 2 ];
220 profileName = profileKey;
222 mOptionsMap[ profileKey ] = profileOptions;
223 mProfileComboBox->addItem( profileName, profileKey );
227 mProfileComboBox->blockSignals(
false );
230 mProfileComboBox->setCurrentIndex( mProfileComboBox->findData( mySettings.
value(
231 mProvider +
"/driverOptions/" + format.toLower() +
"/defaultProfile",
236void QgsRasterFormatSaveOptionsWidget::updateOptions()
238 mBlockOptionUpdates++;
239 QString myOptions = mOptionsMap.value( currentProfileKey() );
240 QStringList myOptionsList = myOptions.trimmed().split(
' ', Qt::SkipEmptyParts );
244 if ( mRasterLayer && mRasterLayer->
bandCount() != 3 &&
245 myOptions == PYRAMID_JPEG_YCBCR_COMPRESSION )
247 myOptions = PYRAMID_JPEG_COMPRESSION;
250 if ( mTableWidget->isVisible() )
252 mOptionsTable->setRowCount( 0 );
253 for (
int i = 0; i < myOptionsList.count(); i++ )
255 QStringList key_value = myOptionsList[i].split(
'=' );
256 if ( key_value.count() == 2 )
258 mOptionsTable->insertRow( i );
259 mOptionsTable->setItem( i, 0,
new QTableWidgetItem( key_value[0] ) );
260 mOptionsTable->setItem( i, 1,
new QTableWidgetItem( key_value[1] ) );
266 mOptionsLineEdit->setText( myOptions );
267 mOptionsLineEdit->setCursorPosition( 0 );
270 mBlockOptionUpdates--;
283 if ( mProvider == QLatin1String(
"gdal" ) && !mFormat.isEmpty() && ! mPyramids )
286 if ( message.isEmpty() )
287 message = tr(
"Cannot get create options for driver %1" ).arg( mFormat );
289 else if ( mProvider == QLatin1String(
"gdal" ) && mPyramids )
291 message = tr(
"For details on pyramids options please see the following pages" );
292 message += QLatin1String(
"\n\nhttps://gdal.org/programs/gdaladdo.html\n\nhttps://gdal.org/drivers/raster/gtiff.html" );
295 message = tr(
"No help available" );
299 dlg->setWindowTitle( tr(
"Create Options for %1" ).arg( mFormat ) );
300 QTextEdit *textEdit =
new QTextEdit( dlg );
301 textEdit->setReadOnly(
true );
303 textEdit->setText( message );
304 dlg->
layout()->addWidget( textEdit );
305 dlg->resize( 600, 400 );
315 const QStringList createOptions =
options();
318 QgsDebugMsgLevel( QStringLiteral(
"layer: [%1] file: [%2] format: [%3]" ).arg( mRasterLayer ? mRasterLayer->
id() :
"none", mRasterFileName, mFormat ), 2 );
322 bool tmpLayer =
false;
323 if ( !( mRasterLayer && rasterLayer->
dataProvider() ) && ! mRasterFileName.isNull() )
328 rasterLayer =
new QgsRasterLayer( mRasterFileName, QFileInfo( mRasterFileName ).baseName(), QStringLiteral(
"gdal" ),
options );
331 if ( mProvider == QLatin1String(
"gdal" ) && mPyramids )
335 QgsDebugMsgLevel( QStringLiteral(
"calling validate pyramids on layer's data provider" ), 2 );
340 message = tr(
"cannot validate pyramid options" );
343 else if ( !createOptions.isEmpty() && mProvider == QLatin1String(
"gdal" ) && !mFormat.isEmpty() )
347 QgsDebugMsgLevel( QStringLiteral(
"calling validate on layer's data provider" ), 2 );
357 else if ( ! createOptions.isEmpty() )
359 QMessageBox::information(
this, QString(), tr(
"Cannot validate creation options." ), QMessageBox::Close );
367 if ( message.isNull() )
370 QMessageBox::information(
this, QString(), tr(
"Valid" ), QMessageBox::Close );
374 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 );
384void QgsRasterFormatSaveOptionsWidget::optionsTableChanged()
386 if ( mBlockOptionUpdates )
389 QTableWidgetItem *key, *value;
391 for (
int i = 0; i < mOptionsTable->rowCount(); i++ )
393 key = mOptionsTable->item( i, 0 );
394 if ( ! key || key->text().isEmpty() )
396 value = mOptionsTable->item( i, 1 );
397 if ( ! value || value->text().isEmpty() )
399 options += key->text() +
'=' + value->text() +
' ';
402 mOptionsMap[ currentProfileKey()] =
options;
403 mOptionsLineEdit->setText(
options );
404 mOptionsLineEdit->setCursorPosition( 0 );
407void QgsRasterFormatSaveOptionsWidget::mOptionsLineEdit_editingFinished()
409 mOptionsMap[ currentProfileKey()] = mOptionsLineEdit->text().trimmed();
412void QgsRasterFormatSaveOptionsWidget::mProfileNewButton_clicked()
414 QString profileName = QInputDialog::getText(
this, QString(), tr(
"Profile name:" ) );
415 if ( ! profileName.isEmpty() )
417 profileName = profileName.trimmed();
418 mOptionsMap[ profileName ] = QString();
419 mProfileComboBox->addItem( profileName, profileName );
420 mProfileComboBox->setCurrentIndex( mProfileComboBox->count() - 1 );
424void QgsRasterFormatSaveOptionsWidget::mProfileDeleteButton_clicked()
426 const int index = mProfileComboBox->currentIndex();
427 const QString profileKey = currentProfileKey();
428 if ( index != -1 && ! sBuiltinProfiles.contains( profileKey ) )
430 mOptionsMap.remove( profileKey );
431 mProfileComboBox->removeItem( index );
435void QgsRasterFormatSaveOptionsWidget::mProfileResetButton_clicked()
437 const QString profileKey = currentProfileKey();
438 if ( sBuiltinProfiles.contains( profileKey ) )
440 mOptionsMap[ profileKey ] = sBuiltinProfiles[ profileKey ][ 2 ];
444 mOptionsMap[ profileKey ] = QString();
446 mOptionsLineEdit->setText( mOptionsMap.value( currentProfileKey() ) );
447 mOptionsLineEdit->setCursorPosition( 0 );
451void QgsRasterFormatSaveOptionsWidget::optionsTableEnableDeleteButton()
453 mOptionsDeleteButton->setEnabled( mOptionsTable->currentRow() >= 0 );
456void QgsRasterFormatSaveOptionsWidget::mOptionsAddButton_clicked()
458 mOptionsTable->insertRow( mOptionsTable->rowCount() );
460 const int newRow = mOptionsTable->rowCount() - 1;
461 QTableWidgetItem *item =
new QTableWidgetItem();
462 mOptionsTable->setItem( newRow, 0, item );
463 mOptionsTable->setCurrentItem( item );
466void QgsRasterFormatSaveOptionsWidget::mOptionsDeleteButton_clicked()
468 if ( mOptionsTable->currentRow() >= 0 )
470 mOptionsTable->removeRow( mOptionsTable->currentRow() );
472 QTableWidgetItem *item = mOptionsTable->item( mOptionsTable->currentRow(), 0 );
473 mOptionsTable->setCurrentItem( item );
474 optionsTableChanged();
478QString QgsRasterFormatSaveOptionsWidget::settingsKey( QString profileName )
const
480 if ( !profileName.isEmpty() )
481 profileName =
"/profile_" + profileName;
483 profileName =
"/profile_default" + profileName;
484 return mProvider +
"/driverOptions/" + pseudoFormat().toLower() + profileName +
"/create";
487QString QgsRasterFormatSaveOptionsWidget::currentProfileKey()
const
489 return mProfileComboBox->currentData().toString();
494 return mOptionsMap.value( currentProfileKey() ).trimmed().split(
' ', Qt::SkipEmptyParts );
497QString QgsRasterFormatSaveOptionsWidget::createOptions(
const QString &profileName )
const
500 return mySettings.
value( settingsKey( profileName ),
"" ).toString();
503void QgsRasterFormatSaveOptionsWidget::deleteCreateOptions(
const QString &profileName )
506 mySettings.
remove( settingsKey( profileName ) );
509void QgsRasterFormatSaveOptionsWidget::setCreateOptions()
512 QStringList myProfiles;
513 QMap< QString, QString >::const_iterator i = mOptionsMap.constBegin();
514 while ( i != mOptionsMap.constEnd() )
516 setCreateOptions( i.key(), i.value() );
517 myProfiles << i.key();
520 mySettings.
setValue( mProvider +
"/driverOptions/" + pseudoFormat().toLower() +
"/profiles",
522 mySettings.
setValue( mProvider +
"/driverOptions/" + pseudoFormat().toLower() +
"/defaultProfile",
523 currentProfileKey().trimmed() );
526void QgsRasterFormatSaveOptionsWidget::setCreateOptions(
const QString &profileName,
const QString &options )
532void QgsRasterFormatSaveOptionsWidget::setCreateOptions(
const QString &profileName,
const QStringList &list )
534 setCreateOptions( profileName, list.join( QLatin1Char(
' ' ) ) );
537QStringList QgsRasterFormatSaveOptionsWidget::profiles()
const
540 return mySettings.
value( mProvider +
"/driverOptions/" + pseudoFormat().toLower() +
"/profiles",
"" ).toStringList();
543void QgsRasterFormatSaveOptionsWidget::swapOptionsUI(
int newIndex )
548 bool lineEditMode = mOptionsLineEdit->isVisible();
549 mOptionsLineEdit->setVisible( ( newIndex == -1 && !lineEditMode ) || newIndex == 1 );
550 mTableWidget->setVisible( ( newIndex == -1 && lineEditMode ) || newIndex == 0 );
554void QgsRasterFormatSaveOptionsWidget::updateControls()
556 const bool valid = mProvider == QLatin1String(
"gdal" ) && !mFormat.isEmpty();
557 mOptionsValidateButton->setEnabled( valid );
558 mOptionsHelpButton->setEnabled( valid );
562bool QgsRasterFormatSaveOptionsWidget::eventFilter( QObject *obj, QEvent *event )
564 if ( event->type() == QEvent::ContextMenu )
566 QContextMenuEvent *contextEvent =
static_cast<QContextMenuEvent *
>( event );
567 QMenu *menu =
nullptr;
568 menu = mOptionsLineEdit->createStandardContextMenu();
569 menu->addSeparator();
570 QAction *action =
new QAction( tr(
"Use Table Interface" ), menu );
571 menu->addAction( action );
572 connect( action, &QAction::triggered,
this, [
this] { swapOptionsUI( 0 ); } );
573 menu->exec( contextEvent->globalPos() );
579 return QObject::eventFilter( obj, event );
585 mOptionsTable->horizontalHeader()->resizeSection( 0, mOptionsTable->width() - 115 );
591 mBlockOptionUpdates++;
592 mOptionsTable->clearContents();
594 const QStringList optionsList =
options.trimmed().split(
' ', Qt::SkipEmptyParts );
595 for (
const QString &opt : optionsList )
597 const int rowCount = mOptionsTable->rowCount();
598 mOptionsTable->insertRow( rowCount );
600 const QStringList values = opt.split(
'=' );
601 if ( values.count() == 2 )
603 QTableWidgetItem *nameItem =
new QTableWidgetItem( values.at( 0 ) );
604 mOptionsTable->setItem( rowCount, 0, nameItem );
605 QTableWidgetItem *valueItem =
new QTableWidgetItem( values.at( 1 ) );
606 mOptionsTable->setItem( rowCount, 1, valueItem );
612 mProfileComboBox->setCurrentIndex( 0 );
614 mOptionsMap[ currentProfileKey()] =
options.trimmed();
615 mOptionsLineEdit->setText(
options.trimmed() );
616 mOptionsLineEdit->setCursorPosition( 0 );
618 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.