QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgsrasterformatsaveoptionswidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterformatsaveoptionswidget.cpp
3  -------------------
4  begin : July 2012
5  copyright : (C) 2012 by Etienne Tourigny
6  email : etourigny dot dev at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgslogger.h"
20 #include "qgsdialog.h"
21 #include "qgsrasterlayer.h"
22 #include "qgsproviderregistry.h"
23 #include "qgsrasterdataprovider.h"
24 #include "qgssettings.h"
25 #include "qgsgdalutils.h"
26 
27 #include <QInputDialog>
28 #include <QMessageBox>
29 #include <QTextEdit>
30 #include <QMouseEvent>
31 #include <QMenu>
32 
33 
34 QMap< QString, QStringList > QgsRasterFormatSaveOptionsWidget::sBuiltinProfiles;
35 
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" ) );
38 
40  QgsRasterFormatSaveOptionsWidget::Type type, const QString &provider )
41  : QWidget( parent )
42  , mFormat( format )
43  , mProvider( provider )
44 {
45  setupUi( this );
46  connect( mProfileNewButton, &QPushButton::clicked, this, &QgsRasterFormatSaveOptionsWidget::mProfileNewButton_clicked );
47  connect( mProfileDeleteButton, &QPushButton::clicked, this, &QgsRasterFormatSaveOptionsWidget::mProfileDeleteButton_clicked );
48  connect( mProfileResetButton, &QPushButton::clicked, this, &QgsRasterFormatSaveOptionsWidget::mProfileResetButton_clicked );
49  connect( mOptionsAddButton, &QPushButton::clicked, this, &QgsRasterFormatSaveOptionsWidget::mOptionsAddButton_clicked );
50  connect( mOptionsDeleteButton, &QPushButton::clicked, this, &QgsRasterFormatSaveOptionsWidget::mOptionsDeleteButton_clicked );
51  connect( mOptionsLineEdit, &QLineEdit::editingFinished, this, &QgsRasterFormatSaveOptionsWidget::mOptionsLineEdit_editingFinished );
52 
53  setType( type );
54 
55  if ( sBuiltinProfiles.isEmpty() )
56  {
57  // key=profileKey values=format,profileName,options
58  sBuiltinProfiles[ QStringLiteral( "z_adefault" )] = ( QStringList() << QString() << tr( "Default" ) << QString() );
59 
60  // these GTiff profiles are based on Tim's benchmarks at
61  // http://linfiniti.com/2011/05/gdal-efficiency-of-various-compression-algorithms/
62  // big: no compression | medium: reasonable size/speed tradeoff | small: smallest size
63  sBuiltinProfiles[ QStringLiteral( "z_gtiff_1big" )] =
64  ( QStringList() << QStringLiteral( "GTiff" ) << tr( "No Compression" )
65  << QStringLiteral( "COMPRESS=NONE BIGTIFF=IF_NEEDED" ) );
66  sBuiltinProfiles[ QStringLiteral( "z_gtiff_2medium" )] =
67  ( QStringList() << QStringLiteral( "GTiff" ) << tr( "Low Compression" )
68  << QStringLiteral( "COMPRESS=PACKBITS" ) );
69  sBuiltinProfiles[ QStringLiteral( "z_gtiff_3small" )] =
70  ( QStringList() << QStringLiteral( "GTiff" ) << tr( "High Compression" )
71  << QStringLiteral( "COMPRESS=DEFLATE PREDICTOR=2 ZLEVEL=9" ) );
72  sBuiltinProfiles[ QStringLiteral( "z_gtiff_4jpeg" )] =
73  ( QStringList() << QStringLiteral( "GTiff" ) << tr( "JPEG Compression" )
74  << QStringLiteral( "COMPRESS=JPEG JPEG_QUALITY=75" ) );
75 
76  // overview compression schemes for GTiff format, see
77  // http://www.gdal.org/gdaladdo.html and http://www.gdal.org/frmt_gtiff.html
78  // TODO - should we offer GDAL_TIFF_OVR_BLOCKSIZE option here or in QgsRasterPyramidsOptionsWidget ?
79  sBuiltinProfiles[ QStringLiteral( "z__pyramids_gtiff_1big" )] =
80  ( QStringList() << QStringLiteral( "_pyramids" ) << tr( "No Compression" )
81  << QStringLiteral( "COMPRESS_OVERVIEW=NONE BIGTIFF_OVERVIEW=IF_NEEDED" ) );
82  sBuiltinProfiles[ QStringLiteral( "z__pyramids_gtiff_2medium" )] =
83  ( QStringList() << QStringLiteral( "_pyramids" ) << tr( "Low Compression" )
84  << QStringLiteral( "COMPRESS_OVERVIEW=PACKBITS" ) );
85  sBuiltinProfiles[ QStringLiteral( "z__pyramids_gtiff_3small" )] =
86  ( QStringList() << QStringLiteral( "_pyramids" ) << tr( "High Compression" )
87  << QStringLiteral( "COMPRESS_OVERVIEW=DEFLATE PREDICTOR_OVERVIEW=2 ZLEVEL=9" ) ); // how to set zlevel?
88  sBuiltinProfiles[ QStringLiteral( "z__pyramids_gtiff_4jpeg" )] =
89  ( QStringList() << QStringLiteral( "_pyramids" ) << tr( "JPEG Compression" )
90  << PYRAMID_JPEG_YCBCR_COMPRESSION );
91  }
92 
93  connect( mProfileComboBox, &QComboBox::currentTextChanged,
94  this, &QgsRasterFormatSaveOptionsWidget::updateOptions );
95  connect( mOptionsTable, &QTableWidget::cellChanged, this, &QgsRasterFormatSaveOptionsWidget::optionsTableChanged );
96  connect( mOptionsHelpButton, &QAbstractButton::clicked, this, &QgsRasterFormatSaveOptionsWidget::helpOptions );
97  connect( mOptionsValidateButton, &QAbstractButton::clicked, this, [ = ] { validateOptions(); } );
98 
99  // create eventFilter to map right click to swapOptionsUI()
100  // mOptionsLabel->installEventFilter( this );
101  mOptionsLineEdit->installEventFilter( this );
102  mOptionsStackedWidget->installEventFilter( this );
103 
104  updateControls();
105  updateProfiles();
106 
107  QgsDebugMsg( QStringLiteral( "done" ) );
108 }
109 
110 void QgsRasterFormatSaveOptionsWidget::setFormat( const QString &format )
111 {
112  mFormat = format;
113  updateControls();
114  updateProfiles();
115 }
116 
117 void QgsRasterFormatSaveOptionsWidget::setProvider( const QString &provider )
118 {
119  mProvider = provider;
120  updateControls();
121 }
122 
123 // show/hide widgets - we need this function if widget is used in creator
125 {
126  QList< QWidget * > widgets = this->findChildren<QWidget *>();
127  if ( ( type == Table ) || ( type == LineEdit ) )
128  {
129  // hide all controls, except stacked widget
130  const auto constWidgets = widgets;
131  for ( QWidget *widget : constWidgets )
132  widget->setVisible( false );
133  mOptionsStackedWidget->setVisible( true );
134  const auto children { mOptionsStackedWidget->findChildren<QWidget *>() };
135  for ( QWidget *widget : children )
136  widget->setVisible( true );
137 
138  // show relevant page
139  if ( type == Table )
140  swapOptionsUI( 0 );
141  else if ( type == LineEdit )
142  swapOptionsUI( 1 );
143  }
144  else
145  {
146  // show all widgets, except profile buttons (unless Full)
147  const auto constWidgets = widgets;
148  for ( QWidget *widget : constWidgets )
149  widget->setVisible( true );
150  if ( type != Full )
151  mProfileButtons->setVisible( false );
152 
153  // show elevant page
154  if ( type == ProfileLineEdit )
155  swapOptionsUI( 1 );
156  }
157 }
158 
159 QString QgsRasterFormatSaveOptionsWidget::pseudoFormat() const
160 {
161  return mPyramids ? QStringLiteral( "_pyramids" ) : mFormat;
162 }
163 
165 {
166  // build profiles list = user + builtin(last)
167  QString format = pseudoFormat();
168  QStringList profileKeys = profiles();
169  QMapIterator<QString, QStringList> it( sBuiltinProfiles );
170  while ( it.hasNext() )
171  {
172  it.next();
173  QString profileKey = it.key();
174  if ( ! profileKeys.contains( profileKey ) && !it.value().isEmpty() )
175  {
176  // insert key if is for all formats or this format (GTiff)
177  if ( it.value()[0].isEmpty() || it.value()[0] == format )
178  {
179  profileKeys.insert( 0, profileKey );
180  }
181  }
182  }
183  std::sort( profileKeys.begin(), profileKeys.end() );
184 
185  // populate mOptionsMap and mProfileComboBox
186  mOptionsMap.clear();
187  mProfileComboBox->blockSignals( true );
188  mProfileComboBox->clear();
189  const auto constProfileKeys = profileKeys;
190  for ( const QString &profileKey : constProfileKeys )
191  {
192  QString profileName, profileOptions;
193  profileOptions = createOptions( profileKey );
194  if ( sBuiltinProfiles.contains( profileKey ) )
195  {
196  profileName = sBuiltinProfiles[ profileKey ][ 1 ];
197  if ( profileOptions.isEmpty() )
198  profileOptions = sBuiltinProfiles[ profileKey ][ 2 ];
199  }
200  else
201  {
202  profileName = profileKey;
203  }
204  mOptionsMap[ profileKey ] = profileOptions;
205  mProfileComboBox->addItem( profileName, profileKey );
206  }
207 
208  // update UI
209  mProfileComboBox->blockSignals( false );
210  // mProfileComboBox->setCurrentIndex( 0 );
211  QgsSettings mySettings;
212  mProfileComboBox->setCurrentIndex( mProfileComboBox->findData( mySettings.value(
213  mProvider + "/driverOptions/" + format.toLower() + "/defaultProfile",
214  "z_adefault" ) ) );
215  updateOptions();
216 }
217 
218 void QgsRasterFormatSaveOptionsWidget::updateOptions()
219 {
220  mBlockOptionUpdates++;
221  QString myOptions = mOptionsMap.value( currentProfileKey() );
222  QStringList myOptionsList = myOptions.trimmed().split( ' ', QString::SkipEmptyParts );
223 
224  // If the default JPEG compression profile was selected, remove PHOTOMETRIC_OVERVIEW=YCBCR
225  // if the raster is not RGB. Otherwise this is bound to fail afterwards.
226  if ( mRasterLayer && mRasterLayer->bandCount() != 3 &&
227  myOptions == PYRAMID_JPEG_YCBCR_COMPRESSION )
228  {
229  myOptions = PYRAMID_JPEG_COMPRESSION;
230  }
231 
232  if ( mOptionsStackedWidget->currentIndex() == 0 )
233  {
234  mOptionsTable->setRowCount( 0 );
235  for ( int i = 0; i < myOptionsList.count(); i++ )
236  {
237  QStringList key_value = myOptionsList[i].split( '=' );
238  if ( key_value.count() == 2 )
239  {
240  mOptionsTable->insertRow( i );
241  mOptionsTable->setItem( i, 0, new QTableWidgetItem( key_value[0] ) );
242  mOptionsTable->setItem( i, 1, new QTableWidgetItem( key_value[1] ) );
243  }
244  }
245  }
246  else
247  {
248  mOptionsLineEdit->setText( myOptions );
249  mOptionsLineEdit->setCursorPosition( 0 );
250  }
251 
252  mBlockOptionUpdates--;
253  emit optionsChanged();
254 }
255 
257 {
258  setCreateOptions();
259 }
260 
262 {
263  QString message;
264 
265  if ( mProvider == QLatin1String( "gdal" ) && !mFormat.isEmpty() && ! mPyramids )
266  {
267  message = QgsGdalUtils::helpCreationOptionsFormat( mFormat );
268  if ( message.isEmpty() )
269  message = tr( "Cannot get create options for driver %1" ).arg( mFormat );
270  }
271  else if ( mProvider == QLatin1String( "gdal" ) && mPyramids )
272  {
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" );
275  }
276  else
277  message = tr( "No help available" );
278 
279  // show simple non-modal dialog - should we make the basic xml prettier?
280  QgsDialog *dlg = new QgsDialog( this );
281  dlg->setWindowTitle( tr( "Create Options for %1" ).arg( mFormat ) );
282  QTextEdit *textEdit = new QTextEdit( dlg );
283  textEdit->setReadOnly( true );
284  // message = tr( "Create Options:\n\n%1" ).arg( message );
285  textEdit->setText( message );
286  dlg->layout()->addWidget( textEdit );
287  dlg->resize( 600, 400 );
288 #ifdef Q_OS_MAC
289  dlg->exec(); //modal
290 #else
291  dlg->show(); //non modal
292 #endif
293 }
294 
295 QString QgsRasterFormatSaveOptionsWidget::validateOptions( bool gui, bool reportOK )
296 {
297  QStringList createOptions = options();
298  QString message;
299 
300  QgsDebugMsg( QStringLiteral( "layer: [%1] file: [%2] format: [%3]" ).arg( mRasterLayer ? mRasterLayer->id() : "none", mRasterFileName, mFormat ) );
301  // if no rasterLayer is defined, but we have a raster fileName, then create a temp. rasterLayer to validate options
302  // ideally we should keep it for future access, but this is trickier
303  QgsRasterLayer *rasterLayer = mRasterLayer;
304  bool tmpLayer = false;
305  if ( !( mRasterLayer && rasterLayer->dataProvider() ) && ! mRasterFileName.isNull() )
306  {
307  tmpLayer = true;
309  options.skipCrsValidation = true;
310  rasterLayer = new QgsRasterLayer( mRasterFileName, QFileInfo( mRasterFileName ).baseName(), QStringLiteral( "gdal" ), options );
311  }
312 
313  if ( mProvider == QLatin1String( "gdal" ) && mPyramids )
314  {
315  if ( rasterLayer && rasterLayer->dataProvider() )
316  {
317  QgsDebugMsg( QStringLiteral( "calling validate pyramids on layer's data provider" ) );
318  message = rasterLayer->dataProvider()->validatePyramidsConfigOptions( mPyramidsFormat, createOptions, mFormat );
319  }
320  else
321  {
322  message = tr( "cannot validate pyramid options" );
323  }
324  }
325  else if ( !createOptions.isEmpty() && mProvider == QLatin1String( "gdal" ) && !mFormat.isEmpty() )
326  {
327  if ( rasterLayer && rasterLayer->dataProvider() )
328  {
329  QgsDebugMsg( QStringLiteral( "calling validate on layer's data provider" ) );
330  message = rasterLayer->dataProvider()->validateCreationOptions( createOptions, mFormat );
331  }
332  else
333  {
334  // get validateCreationOptionsFormat() function ptr for provider
335  message = QgsGdalUtils::validateCreationOptionsFormat( createOptions, mFormat );
336 
337  }
338  }
339  else if ( ! createOptions.isEmpty() )
340  {
341  QMessageBox::information( this, QString(), tr( "Cannot validate creation options." ), QMessageBox::Close );
342  if ( tmpLayer )
343  delete rasterLayer;
344  return QString();
345  }
346 
347  if ( gui )
348  {
349  if ( message.isNull() )
350  {
351  if ( reportOK )
352  QMessageBox::information( this, QString(), tr( "Valid" ), QMessageBox::Close );
353  }
354  else
355  {
356  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 );
357  }
358  }
359 
360  if ( tmpLayer )
361  delete rasterLayer;
362 
363  return message;
364 }
365 
366 void QgsRasterFormatSaveOptionsWidget::optionsTableChanged()
367 {
368  if ( mBlockOptionUpdates )
369  return;
370 
371  QTableWidgetItem *key, *value;
372  QString options;
373  for ( int i = 0; i < mOptionsTable->rowCount(); i++ )
374  {
375  key = mOptionsTable->item( i, 0 );
376  if ( ! key || key->text().isEmpty() )
377  continue;
378  value = mOptionsTable->item( i, 1 );
379  if ( ! value || value->text().isEmpty() )
380  continue;
381  options += key->text() + '=' + value->text() + ' ';
382  }
383  options = options.trimmed();
384  mOptionsMap[ currentProfileKey()] = options;
385  mOptionsLineEdit->setText( options );
386  mOptionsLineEdit->setCursorPosition( 0 );
387 }
388 
389 void QgsRasterFormatSaveOptionsWidget::mOptionsLineEdit_editingFinished()
390 {
391  mOptionsMap[ currentProfileKey()] = mOptionsLineEdit->text().trimmed();
392 }
393 
394 void QgsRasterFormatSaveOptionsWidget::mProfileNewButton_clicked()
395 {
396  QString profileName = QInputDialog::getText( this, QString(), tr( "Profile name:" ) );
397  if ( ! profileName.isEmpty() )
398  {
399  profileName = profileName.trimmed();
400  mOptionsMap[ profileName ] = QString();
401  mProfileComboBox->addItem( profileName, profileName );
402  mProfileComboBox->setCurrentIndex( mProfileComboBox->count() - 1 );
403  }
404 }
405 
406 void QgsRasterFormatSaveOptionsWidget::mProfileDeleteButton_clicked()
407 {
408  int index = mProfileComboBox->currentIndex();
409  QString profileKey = currentProfileKey();
410  if ( index != -1 && ! sBuiltinProfiles.contains( profileKey ) )
411  {
412  mOptionsMap.remove( profileKey );
413  mProfileComboBox->removeItem( index );
414  }
415 }
416 
417 void QgsRasterFormatSaveOptionsWidget::mProfileResetButton_clicked()
418 {
419  QString profileKey = currentProfileKey();
420  if ( sBuiltinProfiles.contains( profileKey ) )
421  {
422  mOptionsMap[ profileKey ] = sBuiltinProfiles[ profileKey ][ 2 ];
423  }
424  else
425  {
426  mOptionsMap[ profileKey ] = QString();
427  }
428  mOptionsLineEdit->setText( mOptionsMap.value( currentProfileKey() ) );
429  mOptionsLineEdit->setCursorPosition( 0 );
430  updateOptions();
431 }
432 
433 void QgsRasterFormatSaveOptionsWidget::optionsTableEnableDeleteButton()
434 {
435  mOptionsDeleteButton->setEnabled( mOptionsTable->currentRow() >= 0 );
436 }
437 
438 void QgsRasterFormatSaveOptionsWidget::mOptionsAddButton_clicked()
439 {
440  mOptionsTable->insertRow( mOptionsTable->rowCount() );
441  // select the added row
442  int newRow = mOptionsTable->rowCount() - 1;
443  QTableWidgetItem *item = new QTableWidgetItem();
444  mOptionsTable->setItem( newRow, 0, item );
445  mOptionsTable->setCurrentItem( item );
446 }
447 
448 void QgsRasterFormatSaveOptionsWidget::mOptionsDeleteButton_clicked()
449 {
450  if ( mOptionsTable->currentRow() >= 0 )
451  {
452  mOptionsTable->removeRow( mOptionsTable->currentRow() );
453  // select the previous row or the next one if there is no previous row
454  QTableWidgetItem *item = mOptionsTable->item( mOptionsTable->currentRow(), 0 );
455  mOptionsTable->setCurrentItem( item );
456  optionsTableChanged();
457  }
458 }
459 
460 QString QgsRasterFormatSaveOptionsWidget::settingsKey( QString profileName ) const
461 {
462  if ( !profileName.isEmpty() )
463  profileName = "/profile_" + profileName;
464  else
465  profileName = "/profile_default" + profileName;
466  return mProvider + "/driverOptions/" + pseudoFormat().toLower() + profileName + "/create";
467 }
468 
469 QString QgsRasterFormatSaveOptionsWidget::currentProfileKey() const
470 {
471  return mProfileComboBox->currentData().toString();
472 }
473 
475 {
476  return mOptionsMap.value( currentProfileKey() ).trimmed().split( ' ', QString::SkipEmptyParts );
477 }
478 
479 QString QgsRasterFormatSaveOptionsWidget::createOptions( const QString &profileName ) const
480 {
481  QgsSettings mySettings;
482  return mySettings.value( settingsKey( profileName ), "" ).toString();
483 }
484 
485 void QgsRasterFormatSaveOptionsWidget::deleteCreateOptions( const QString &profileName )
486 {
487  QgsSettings mySettings;
488  mySettings.remove( settingsKey( profileName ) );
489 }
490 
491 void QgsRasterFormatSaveOptionsWidget::setCreateOptions()
492 {
493  QgsSettings mySettings;
494  QStringList myProfiles;
495  QMap< QString, QString >::const_iterator i = mOptionsMap.constBegin();
496  while ( i != mOptionsMap.constEnd() )
497  {
498  setCreateOptions( i.key(), i.value() );
499  myProfiles << i.key();
500  ++i;
501  }
502  mySettings.setValue( mProvider + "/driverOptions/" + pseudoFormat().toLower() + "/profiles",
503  myProfiles );
504  mySettings.setValue( mProvider + "/driverOptions/" + pseudoFormat().toLower() + "/defaultProfile",
505  currentProfileKey().trimmed() );
506 }
507 
508 void QgsRasterFormatSaveOptionsWidget::setCreateOptions( const QString &profileName, const QString &options )
509 {
510  QgsSettings mySettings;
511  mySettings.setValue( settingsKey( profileName ), options.trimmed() );
512 }
513 
514 void QgsRasterFormatSaveOptionsWidget::setCreateOptions( const QString &profileName, const QStringList &list )
515 {
516  setCreateOptions( profileName, list.join( QLatin1Char( ' ' ) ) );
517 }
518 
519 QStringList QgsRasterFormatSaveOptionsWidget::profiles() const
520 {
521  QgsSettings mySettings;
522  return mySettings.value( mProvider + "/driverOptions/" + pseudoFormat().toLower() + "/profiles", "" ).toStringList();
523 }
524 
525 void QgsRasterFormatSaveOptionsWidget::swapOptionsUI( int newIndex )
526 {
527  // set new page
528  int oldIndex;
529  if ( newIndex == -1 )
530  {
531  oldIndex = mOptionsStackedWidget->currentIndex();
532  newIndex = ( oldIndex + 1 ) % 2;
533  }
534  else
535  {
536  oldIndex = ( newIndex + 1 ) % 2;
537  }
538 
539  // resize pages to minimum - this works well with gdaltools merge ui, but not raster save as...
540  mOptionsStackedWidget->setCurrentIndex( newIndex );
541  mOptionsStackedWidget->widget( newIndex )->setSizePolicy(
542  QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) );
543  mOptionsStackedWidget->widget( oldIndex )->setSizePolicy(
544  QSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored ) );
545  layout()->activate();
546 
547  updateOptions();
548 }
549 
550 void QgsRasterFormatSaveOptionsWidget::updateControls()
551 {
552  bool valid = mProvider == QLatin1String( "gdal" ) && !mFormat.isEmpty();
553  mOptionsValidateButton->setEnabled( valid );
554  mOptionsHelpButton->setEnabled( valid );
555 }
556 
557 // map options label left mouse click to optionsToggle()
558 bool QgsRasterFormatSaveOptionsWidget::eventFilter( QObject *obj, QEvent *event )
559 {
560  if ( event->type() == QEvent::MouseButtonPress )
561  {
562  QMouseEvent *mouseEvent = static_cast<QMouseEvent *>( event );
563  if ( mouseEvent && ( mouseEvent->button() == Qt::RightButton ) )
564  {
565  QMenu *menu = nullptr;
566  QString text;
567  if ( mOptionsStackedWidget->currentIndex() == 0 )
568  text = tr( "Use simple interface" );
569  else
570  text = tr( "Use table interface" );
571  if ( obj->objectName() == QLatin1String( "mOptionsLineEdit" ) )
572  {
573  menu = mOptionsLineEdit->createStandardContextMenu();
574  menu->addSeparator();
575  }
576  else
577  menu = new QMenu( this );
578  QAction *action = new QAction( text, menu );
579  menu->addAction( action );
580  connect( action, &QAction::triggered, this, &QgsRasterFormatSaveOptionsWidget::swapOptionsUI );
581  menu->exec( mouseEvent->globalPos() );
582  delete menu;
583  return true;
584  }
585  }
586  // standard event processing
587  return QObject::eventFilter( obj, event );
588 }
589 
591 {
592  Q_UNUSED( event )
593  mOptionsTable->horizontalHeader()->resizeSection( 0, mOptionsTable->width() - 115 );
594  QgsDebugMsg( QStringLiteral( "done" ) );
595 }
596 
597 void QgsRasterFormatSaveOptionsWidget::setOptions( const QString &options )
598 {
599  mBlockOptionUpdates++;
600  mOptionsTable->clearContents();
601 
602  const QStringList optionsList = options.trimmed().split( ' ', QString::SkipEmptyParts );
603  for ( const QString &opt : optionsList )
604  {
605  int rowCount = mOptionsTable->rowCount();
606  mOptionsTable->insertRow( rowCount );
607 
608  const QStringList values = opt.split( '=' );
609  if ( values.count() == 2 )
610  {
611  QTableWidgetItem *nameItem = new QTableWidgetItem( values.at( 0 ) );
612  mOptionsTable->setItem( rowCount, 0, nameItem );
613  QTableWidgetItem *valueItem = new QTableWidgetItem( values.at( 1 ) );
614  mOptionsTable->setItem( rowCount, 1, valueItem );
615  }
616  }
617 
618  // reset to no profile index, otherwise we are changing the definition of whichever profile
619  // is currently selected...
620  mProfileComboBox->setCurrentIndex( 0 );
621 
622  mOptionsMap[ currentProfileKey()] = options.trimmed();
623  mOptionsLineEdit->setText( options.trimmed() );
624  mOptionsLineEdit->setCursorPosition( 0 );
625 
626  mBlockOptionUpdates--;
627 }
A generic dialog with layout and button box.
Definition: qgsdialog.h:34
QVBoxLayout * layout()
Returns the central layout. Widgets added to it must have this dialog as parent.
Definition: qgsdialog.h:46
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.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
virtual QString validateCreationOptions(const QStringList &createOptions, const QString &format)
Validates creation options for a specific dataset and destination format.
virtual QString validatePyramidsConfigOptions(QgsRaster::RasterPyramidsFormat pyramidsFormat, const QStringList &configOptions, const QString &fileFormat)
Validates pyramid creation options for a specific dataset and destination format.
void setFormat(const QString &format)
Set output raster format, it is used to determine list of available options.
void helpOptions()
Opens window with options description for given provider and output format.
void setOptions(const QString &options)
Populate widget with user-defined options.
void updateProfiles()
Reloads profiles list from QGIS settings.
QgsRasterFormatSaveOptionsWidget(QWidget *parent SIP_TRANSFERTHIS=nullptr, const QString &format="GTiff", QgsRasterFormatSaveOptionsWidget::Type type=Default, const QString &provider="gdal")
QString validateOptions(bool gui=true, bool reportOk=true)
Validates options correctness.
void setProvider(const QString &provider)
Set provider key, , it is used to determine list of available options.
QStringList options() const
Returns list of selected options.
void setType(QgsRasterFormatSaveOptionsWidget::Type type=Default)
Set widget look and feel.
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:
Definition: qgssettings.h:62
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 QgsDebugMsg(str)
Definition: qgslogger.h:38
Setting options for loading raster layers.