27 #include <QInputDialog>
28 #include <QMessageBox>
30 #include <QMouseEvent>
34 QMap< QString, QStringList > QgsRasterFormatSaveOptionsWidget::sBuiltinProfiles;
36 static const QString PYRAMID_JPEG_YCBCR_COMPRESSION( QStringLiteral(
"JPEG_QUALITY_OVERVIEW=75 COMPRESS_OVERVIEW=JPEG PHOTOMETRIC_OVERVIEW=YCBCR INTERLEAVE_OVERVIEW=PIXEL" ) );
37 static const QString PYRAMID_JPEG_COMPRESSION( QStringLiteral(
"JPEG_QUALITY_OVERVIEW=75 COMPRESS_OVERVIEW=JPEG INTERLEAVE_OVERVIEW=PIXEL" ) );
43 , mProvider( provider )
46 setMinimumSize( this->fontMetrics().height() * 5, 240 );
48 connect( mProfileNewButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mProfileNewButton_clicked );
49 connect( mProfileDeleteButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mProfileDeleteButton_clicked );
50 connect( mProfileResetButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mProfileResetButton_clicked );
51 connect( mOptionsAddButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mOptionsAddButton_clicked );
52 connect( mOptionsDeleteButton, &QPushButton::clicked,
this, &QgsRasterFormatSaveOptionsWidget::mOptionsDeleteButton_clicked );
53 connect( mOptionsLineEdit, &QLineEdit::editingFinished,
this, &QgsRasterFormatSaveOptionsWidget::mOptionsLineEdit_editingFinished );
57 if ( sBuiltinProfiles.isEmpty() )
60 sBuiltinProfiles[ QStringLiteral(
"z_adefault" )] = ( QStringList() << QString() << tr(
"Default" ) << QString() );
65 sBuiltinProfiles[ QStringLiteral(
"z_gtiff_1big" )] =
66 ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"No Compression" )
67 << QStringLiteral(
"COMPRESS=NONE BIGTIFF=IF_NEEDED" ) );
68 sBuiltinProfiles[ QStringLiteral(
"z_gtiff_2medium" )] =
69 ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"Low Compression" )
70 << QStringLiteral(
"COMPRESS=PACKBITS" ) );
71 sBuiltinProfiles[ QStringLiteral(
"z_gtiff_3small" )] =
72 ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"High Compression" )
73 << QStringLiteral(
"COMPRESS=DEFLATE PREDICTOR=2 ZLEVEL=9" ) );
74 sBuiltinProfiles[ QStringLiteral(
"z_gtiff_4jpeg" )] =
75 ( QStringList() << QStringLiteral(
"GTiff" ) << tr(
"JPEG Compression" )
76 << QStringLiteral(
"COMPRESS=JPEG JPEG_QUALITY=75" ) );
81 sBuiltinProfiles[ QStringLiteral(
"z__pyramids_gtiff_1big" )] =
82 ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"No Compression" )
83 << QStringLiteral(
"COMPRESS_OVERVIEW=NONE BIGTIFF_OVERVIEW=IF_NEEDED" ) );
84 sBuiltinProfiles[ QStringLiteral(
"z__pyramids_gtiff_2medium" )] =
85 ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"Low Compression" )
86 << QStringLiteral(
"COMPRESS_OVERVIEW=PACKBITS" ) );
87 sBuiltinProfiles[ QStringLiteral(
"z__pyramids_gtiff_3small" )] =
88 ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"High Compression" )
89 << QStringLiteral(
"COMPRESS_OVERVIEW=DEFLATE PREDICTOR_OVERVIEW=2 ZLEVEL=9" ) );
90 sBuiltinProfiles[ QStringLiteral(
"z__pyramids_gtiff_4jpeg" )] =
91 ( QStringList() << QStringLiteral(
"_pyramids" ) << tr(
"JPEG Compression" )
92 << PYRAMID_JPEG_YCBCR_COMPRESSION );
95 connect( mProfileComboBox, &QComboBox::currentTextChanged,
96 this, &QgsRasterFormatSaveOptionsWidget::updateOptions );
97 connect( mOptionsTable, &QTableWidget::cellChanged,
this, &QgsRasterFormatSaveOptionsWidget::optionsTableChanged );
99 connect( mOptionsValidateButton, &QAbstractButton::clicked,
this, [ = ] {
validateOptions(); } );
103 mOptionsLineEdit->installEventFilter(
this );
104 mOptionsStackedWidget->installEventFilter(
this );
121 mProvider = provider;
128 const QList< QWidget * > widgets = this->findChildren<QWidget *>();
132 const auto constWidgets = widgets;
133 for ( QWidget *widget : constWidgets )
134 widget->setVisible(
false );
135 mOptionsStackedWidget->setVisible(
true );
136 const auto children { mOptionsStackedWidget->findChildren<QWidget *>() };
137 for ( QWidget *widget : children )
138 widget->setVisible(
true );
149 const auto constWidgets = widgets;
150 for ( QWidget *widget : constWidgets )
151 widget->setVisible(
true );
153 mProfileButtons->setVisible(
false );
161 QString QgsRasterFormatSaveOptionsWidget::pseudoFormat()
const
163 return mPyramids ? QStringLiteral(
"_pyramids" ) : mFormat;
169 const QString format = pseudoFormat();
170 QStringList profileKeys = profiles();
171 QMapIterator<QString, QStringList> it( sBuiltinProfiles );
172 while ( it.hasNext() )
175 const QString profileKey = it.key();
176 if ( ! profileKeys.contains( profileKey ) && !it.value().isEmpty() )
179 if ( it.value()[0].isEmpty() || it.value()[0] == format )
181 profileKeys.insert( 0, profileKey );
185 std::sort( profileKeys.begin(), profileKeys.end() );
189 mProfileComboBox->blockSignals(
true );
190 mProfileComboBox->clear();
191 const auto constProfileKeys = profileKeys;
192 for (
const QString &profileKey : constProfileKeys )
194 QString profileName, profileOptions;
195 profileOptions = createOptions( profileKey );
196 if ( sBuiltinProfiles.contains( profileKey ) )
198 profileName = sBuiltinProfiles[ profileKey ][ 1 ];
199 if ( profileOptions.isEmpty() )
200 profileOptions = sBuiltinProfiles[ profileKey ][ 2 ];
204 profileName = profileKey;
206 mOptionsMap[ profileKey ] = profileOptions;
207 mProfileComboBox->addItem( profileName, profileKey );
211 mProfileComboBox->blockSignals(
false );
214 mProfileComboBox->setCurrentIndex( mProfileComboBox->findData( mySettings.
value(
215 mProvider +
"/driverOptions/" + format.toLower() +
"/defaultProfile",
220 void QgsRasterFormatSaveOptionsWidget::updateOptions()
222 mBlockOptionUpdates++;
223 QString myOptions = mOptionsMap.value( currentProfileKey() );
224 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
225 QStringList myOptionsList = myOptions.trimmed().split(
' ', QString::SkipEmptyParts );
227 QStringList myOptionsList = myOptions.trimmed().split(
' ', Qt::SkipEmptyParts );
232 if ( mRasterLayer && mRasterLayer->
bandCount() != 3 &&
233 myOptions == PYRAMID_JPEG_YCBCR_COMPRESSION )
235 myOptions = PYRAMID_JPEG_COMPRESSION;
238 if ( mOptionsStackedWidget->currentIndex() == 0 )
240 mOptionsTable->setRowCount( 0 );
241 for (
int i = 0; i < myOptionsList.count(); i++ )
243 QStringList key_value = myOptionsList[i].split(
'=' );
244 if ( key_value.count() == 2 )
246 mOptionsTable->insertRow( i );
247 mOptionsTable->setItem( i, 0,
new QTableWidgetItem( key_value[0] ) );
248 mOptionsTable->setItem( i, 1,
new QTableWidgetItem( key_value[1] ) );
254 mOptionsLineEdit->setText( myOptions );
255 mOptionsLineEdit->setCursorPosition( 0 );
258 mBlockOptionUpdates--;
271 if ( mProvider == QLatin1String(
"gdal" ) && !mFormat.isEmpty() && ! mPyramids )
274 if ( message.isEmpty() )
275 message = tr(
"Cannot get create options for driver %1" ).arg( mFormat );
277 else if ( mProvider == QLatin1String(
"gdal" ) && mPyramids )
279 message = tr(
"For details on pyramids options please see the following pages" );
280 message += QLatin1String(
"\n\nhttps://gdal.org/programs/gdaladdo.html\n\nhttps://gdal.org/drivers/raster/gtiff.html" );
283 message = tr(
"No help available" );
287 dlg->setWindowTitle( tr(
"Create Options for %1" ).arg( mFormat ) );
288 QTextEdit *textEdit =
new QTextEdit( dlg );
289 textEdit->setReadOnly(
true );
291 textEdit->setText( message );
292 dlg->
layout()->addWidget( textEdit );
293 dlg->resize( 600, 400 );
303 const QStringList createOptions =
options();
306 QgsDebugMsg( QStringLiteral(
"layer: [%1] file: [%2] format: [%3]" ).arg( mRasterLayer ? mRasterLayer->
id() :
"none", mRasterFileName, mFormat ) );
310 bool tmpLayer =
false;
311 if ( !( mRasterLayer && rasterLayer->
dataProvider() ) && ! mRasterFileName.isNull() )
315 options.skipCrsValidation =
true;
316 rasterLayer =
new QgsRasterLayer( mRasterFileName, QFileInfo( mRasterFileName ).baseName(), QStringLiteral(
"gdal" ),
options );
319 if ( mProvider == QLatin1String(
"gdal" ) && mPyramids )
323 QgsDebugMsg( QStringLiteral(
"calling validate pyramids on layer's data provider" ) );
328 message = tr(
"cannot validate pyramid options" );
331 else if ( !createOptions.isEmpty() && mProvider == QLatin1String(
"gdal" ) && !mFormat.isEmpty() )
335 QgsDebugMsg( QStringLiteral(
"calling validate on layer's data provider" ) );
345 else if ( ! createOptions.isEmpty() )
347 QMessageBox::information(
this, QString(), tr(
"Cannot validate creation options." ), QMessageBox::Close );
355 if ( message.isNull() )
358 QMessageBox::information(
this, QString(), tr(
"Valid" ), QMessageBox::Close );
362 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 );
372 void QgsRasterFormatSaveOptionsWidget::optionsTableChanged()
374 if ( mBlockOptionUpdates )
377 QTableWidgetItem *key, *value;
379 for (
int i = 0; i < mOptionsTable->rowCount(); i++ )
381 key = mOptionsTable->item( i, 0 );
382 if ( ! key || key->text().isEmpty() )
384 value = mOptionsTable->item( i, 1 );
385 if ( ! value || value->text().isEmpty() )
387 options += key->text() +
'=' + value->text() +
' ';
390 mOptionsMap[ currentProfileKey()] =
options;
391 mOptionsLineEdit->setText(
options );
392 mOptionsLineEdit->setCursorPosition( 0 );
395 void QgsRasterFormatSaveOptionsWidget::mOptionsLineEdit_editingFinished()
397 mOptionsMap[ currentProfileKey()] = mOptionsLineEdit->text().trimmed();
400 void QgsRasterFormatSaveOptionsWidget::mProfileNewButton_clicked()
402 QString profileName = QInputDialog::getText(
this, QString(), tr(
"Profile name:" ) );
403 if ( ! profileName.isEmpty() )
405 profileName = profileName.trimmed();
406 mOptionsMap[ profileName ] = QString();
407 mProfileComboBox->addItem( profileName, profileName );
408 mProfileComboBox->setCurrentIndex( mProfileComboBox->count() - 1 );
412 void QgsRasterFormatSaveOptionsWidget::mProfileDeleteButton_clicked()
414 const int index = mProfileComboBox->currentIndex();
415 const QString profileKey = currentProfileKey();
416 if ( index != -1 && ! sBuiltinProfiles.contains( profileKey ) )
418 mOptionsMap.remove( profileKey );
419 mProfileComboBox->removeItem( index );
423 void QgsRasterFormatSaveOptionsWidget::mProfileResetButton_clicked()
425 const QString profileKey = currentProfileKey();
426 if ( sBuiltinProfiles.contains( profileKey ) )
428 mOptionsMap[ profileKey ] = sBuiltinProfiles[ profileKey ][ 2 ];
432 mOptionsMap[ profileKey ] = QString();
434 mOptionsLineEdit->setText( mOptionsMap.value( currentProfileKey() ) );
435 mOptionsLineEdit->setCursorPosition( 0 );
439 void QgsRasterFormatSaveOptionsWidget::optionsTableEnableDeleteButton()
441 mOptionsDeleteButton->setEnabled( mOptionsTable->currentRow() >= 0 );
444 void QgsRasterFormatSaveOptionsWidget::mOptionsAddButton_clicked()
446 mOptionsTable->insertRow( mOptionsTable->rowCount() );
448 const int newRow = mOptionsTable->rowCount() - 1;
449 QTableWidgetItem *item =
new QTableWidgetItem();
450 mOptionsTable->setItem( newRow, 0, item );
451 mOptionsTable->setCurrentItem( item );
454 void QgsRasterFormatSaveOptionsWidget::mOptionsDeleteButton_clicked()
456 if ( mOptionsTable->currentRow() >= 0 )
458 mOptionsTable->removeRow( mOptionsTable->currentRow() );
460 QTableWidgetItem *item = mOptionsTable->item( mOptionsTable->currentRow(), 0 );
461 mOptionsTable->setCurrentItem( item );
462 optionsTableChanged();
466 QString QgsRasterFormatSaveOptionsWidget::settingsKey( QString profileName )
const
468 if ( !profileName.isEmpty() )
469 profileName =
"/profile_" + profileName;
471 profileName =
"/profile_default" + profileName;
472 return mProvider +
"/driverOptions/" + pseudoFormat().toLower() + profileName +
"/create";
475 QString QgsRasterFormatSaveOptionsWidget::currentProfileKey()
const
477 return mProfileComboBox->currentData().toString();
482 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
483 return mOptionsMap.value( currentProfileKey() ).trimmed().split(
' ', QString::SkipEmptyParts );
485 return mOptionsMap.value( currentProfileKey() ).trimmed().split(
' ', Qt::SkipEmptyParts );
489 QString QgsRasterFormatSaveOptionsWidget::createOptions(
const QString &profileName )
const
492 return mySettings.
value( settingsKey( profileName ),
"" ).toString();
495 void QgsRasterFormatSaveOptionsWidget::deleteCreateOptions(
const QString &profileName )
498 mySettings.
remove( settingsKey( profileName ) );
501 void QgsRasterFormatSaveOptionsWidget::setCreateOptions()
504 QStringList myProfiles;
505 QMap< QString, QString >::const_iterator i = mOptionsMap.constBegin();
506 while ( i != mOptionsMap.constEnd() )
508 setCreateOptions( i.key(), i.value() );
509 myProfiles << i.key();
512 mySettings.
setValue( mProvider +
"/driverOptions/" + pseudoFormat().toLower() +
"/profiles",
514 mySettings.
setValue( mProvider +
"/driverOptions/" + pseudoFormat().toLower() +
"/defaultProfile",
515 currentProfileKey().trimmed() );
518 void QgsRasterFormatSaveOptionsWidget::setCreateOptions(
const QString &profileName,
const QString &options )
524 void QgsRasterFormatSaveOptionsWidget::setCreateOptions(
const QString &profileName,
const QStringList &list )
526 setCreateOptions( profileName, list.join( QLatin1Char(
' ' ) ) );
529 QStringList QgsRasterFormatSaveOptionsWidget::profiles()
const
532 return mySettings.
value( mProvider +
"/driverOptions/" + pseudoFormat().toLower() +
"/profiles",
"" ).toStringList();
535 void QgsRasterFormatSaveOptionsWidget::swapOptionsUI(
int newIndex )
539 if ( newIndex == -1 )
541 oldIndex = mOptionsStackedWidget->currentIndex();
542 newIndex = ( oldIndex + 1 ) % 2;
546 oldIndex = ( newIndex + 1 ) % 2;
550 mOptionsStackedWidget->setCurrentIndex( newIndex );
551 mOptionsStackedWidget->widget( newIndex )->setSizePolicy(
552 QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) );
553 mOptionsStackedWidget->widget( oldIndex )->setSizePolicy(
554 QSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored ) );
555 layout()->activate();
560 void QgsRasterFormatSaveOptionsWidget::updateControls()
562 const bool valid = mProvider == QLatin1String(
"gdal" ) && !mFormat.isEmpty();
563 mOptionsValidateButton->setEnabled( valid );
564 mOptionsHelpButton->setEnabled( valid );
568 bool QgsRasterFormatSaveOptionsWidget::eventFilter( QObject *obj, QEvent *event )
570 if ( event->type() == QEvent::MouseButtonPress )
572 QMouseEvent *mouseEvent =
static_cast<QMouseEvent *
>( event );
573 if ( mouseEvent && ( mouseEvent->button() == Qt::RightButton ) )
575 QMenu *menu =
nullptr;
577 if ( mOptionsStackedWidget->currentIndex() == 0 )
578 text = tr(
"Use simple interface" );
580 text = tr(
"Use table interface" );
581 if ( obj->objectName() == QLatin1String(
"mOptionsLineEdit" ) )
583 menu = mOptionsLineEdit->createStandardContextMenu();
584 menu->addSeparator();
587 menu =
new QMenu(
this );
588 QAction *action =
new QAction( text, menu );
589 menu->addAction( action );
590 connect( action, &QAction::triggered,
this, &QgsRasterFormatSaveOptionsWidget::swapOptionsUI );
591 menu->exec( mouseEvent->globalPos() );
597 return QObject::eventFilter( obj, event );
603 mOptionsTable->horizontalHeader()->resizeSection( 0, mOptionsTable->width() - 115 );
609 mBlockOptionUpdates++;
610 mOptionsTable->clearContents();
612 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
613 const QStringList optionsList =
options.trimmed().split(
' ', QString::SkipEmptyParts );
615 const QStringList optionsList =
options.trimmed().split(
' ', Qt::SkipEmptyParts );
617 for (
const QString &opt : optionsList )
619 const int rowCount = mOptionsTable->rowCount();
620 mOptionsTable->insertRow( rowCount );
622 const QStringList values = opt.split(
'=' );
623 if ( values.count() == 2 )
625 QTableWidgetItem *nameItem =
new QTableWidgetItem( values.at( 0 ) );
626 mOptionsTable->setItem( rowCount, 0, nameItem );
627 QTableWidgetItem *valueItem =
new QTableWidgetItem( values.at( 1 ) );
628 mOptionsTable->setItem( rowCount, 1, valueItem );
634 mProfileComboBox->setCurrentIndex( 0 );
636 mOptionsMap[ currentProfileKey()] =
options.trimmed();
637 mOptionsLineEdit->setText(
options.trimmed() );
638 mOptionsLineEdit->setCursorPosition( 0 );
640 mBlockOptionUpdates--;