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