QGIS API Documentation  2.99.0-Master (9fdd060)
qgsrasterpyramidsoptionswidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterpyramidsoptionswidget.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 "qgsrasterdataprovider.h"
20 #include "qgslogger.h"
21 #include "qgsdialog.h"
22 #include "qgssettings.h"
23 
24 #include <QInputDialog>
25 #include <QMessageBox>
26 #include <QTextEdit>
27 #include <QMouseEvent>
28 #include <QMenu>
29 #include <QCheckBox>
30 
31 QgsRasterPyramidsOptionsWidget::QgsRasterPyramidsOptionsWidget( QWidget *parent, const QString &provider )
32  : QWidget( parent )
33  , mProvider( provider )
34 {
35  setupUi( this );
36  connect( cbxPyramidsLevelsCustom, &QCheckBox::toggled, this, &QgsRasterPyramidsOptionsWidget::cbxPyramidsLevelsCustom_toggled );
37  connect( cbxPyramidsFormat, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRasterPyramidsOptionsWidget::cbxPyramidsFormat_currentIndexChanged );
38 
39  mSaveOptionsWidget->setProvider( provider );
40  mSaveOptionsWidget->setPyramidsFormat( QgsRaster::PyramidsGTiff );
41  mSaveOptionsWidget->setType( QgsRasterFormatSaveOptionsWidget::ProfileLineEdit );
42 
43  updateUi();
44 }
45 
46 void QgsRasterPyramidsOptionsWidget::updateUi()
47 {
48  QgsSettings mySettings;
49  QString prefix = mProvider + "/driverOptions/_pyramids/";
50  QString tmpStr;
51 
52  // keep it in sync with qgsrasterlayerproperties.cpp
53  tmpStr = mySettings.value( prefix + "format", "external" ).toString();
54  if ( tmpStr == QLatin1String( "internal" ) )
55  cbxPyramidsFormat->setCurrentIndex( INTERNAL );
56  else if ( tmpStr == QLatin1String( "external_erdas" ) )
57  cbxPyramidsFormat->setCurrentIndex( ERDAS );
58  else
59  cbxPyramidsFormat->setCurrentIndex( GTIFF );
60 
61  // initialize resampling methods
62  cboResamplingMethod->clear();
63  QPair<QString, QString> method;
64  Q_FOREACH ( method, QgsRasterDataProvider::pyramidResamplingMethods( mProvider ) )
65  {
66  cboResamplingMethod->addItem( method.second, method.first );
67  }
68  QString defaultMethod = mySettings.value( prefix + "resampling", "AVERAGE" ).toString();
69  int idx = cboResamplingMethod->findData( defaultMethod );
70  cboResamplingMethod->setCurrentIndex( idx );
71 
72  // validate string, only space-separated positive integers are allowed
73  lePyramidsLevels->setEnabled( cbxPyramidsLevelsCustom->isChecked() );
74  lePyramidsLevels->setValidator( new QRegExpValidator( QRegExp( "(\\d*)(\\s\\d*)*" ), lePyramidsLevels ) );
75  connect( lePyramidsLevels, &QLineEdit::textEdited,
76  this, &QgsRasterPyramidsOptionsWidget::setOverviewList );
77 
78  // overview list
79  if ( mOverviewCheckBoxes.isEmpty() )
80  {
81  QList<int> overviewList;
82  overviewList << 2 << 4 << 8 << 16 << 32 << 64;
83  mOverviewCheckBoxes.clear();
84  Q_FOREACH ( int i, overviewList )
85  {
86  mOverviewCheckBoxes[ i ] = new QCheckBox( QString::number( i ), this );
87  connect( mOverviewCheckBoxes[ i ], &QCheckBox::toggled,
88  this, &QgsRasterPyramidsOptionsWidget::setOverviewList );
89  layoutPyramidsLevels->addWidget( mOverviewCheckBoxes[ i ] );
90  }
91  }
92  else
93  {
94  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
95  it.value()->setChecked( false );
96  }
97  tmpStr = mySettings.value( prefix + "overviewList", "" ).toString();
98  Q_FOREACH ( const QString &lev, tmpStr.split( ' ', QString::SkipEmptyParts ) )
99  {
100  if ( mOverviewCheckBoxes.contains( lev.toInt() ) )
101  mOverviewCheckBoxes[ lev.toInt()]->setChecked( true );
102  }
103  setOverviewList();
104 
105  mSaveOptionsWidget->updateProfiles();
106 
107  connect( cbxPyramidsFormat, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
109  connect( cboResamplingMethod, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
111  connect( mSaveOptionsWidget, &QgsRasterFormatSaveOptionsWidget::optionsChanged,
113 }
114 
116 {
117  return cboResamplingMethod->currentData().toString();
118 }
119 
121 {
122  QgsSettings mySettings;
123  QString prefix = mProvider + "/driverOptions/_pyramids/";
124  QString tmpStr;
125 
126  // mySettings.setValue( prefix + "internal", cbxPyramidsInternal->isChecked() );
127  if ( cbxPyramidsFormat->currentIndex() == INTERNAL )
128  tmpStr = QStringLiteral( "internal" );
129  else if ( cbxPyramidsFormat->currentIndex() == ERDAS )
130  tmpStr = QStringLiteral( "external_erdas" );
131  else
132  tmpStr = QStringLiteral( "external" );
133  mySettings.setValue( prefix + "format", tmpStr );
134  mySettings.setValue( prefix + "resampling", resamplingMethod() );
135  mySettings.setValue( prefix + "overviewStr", lePyramidsLevels->text().trimmed() );
136 
137  // overview list
138  tmpStr.clear();
139  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
140  {
141  if ( it.value()->isChecked() )
142  tmpStr += QString::number( it.key() ) + ' ';
143  }
144  mySettings.setValue( prefix + "overviewList", tmpStr.trimmed() );
145 
146  mSaveOptionsWidget->apply();
147 }
148 
150 {
151  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
152  it.value()->setChecked( checked );
153 }
154 
155 void QgsRasterPyramidsOptionsWidget::cbxPyramidsLevelsCustom_toggled( bool toggled )
156 {
157  // if toggled, disable checkboxes and enable line edit
158  lePyramidsLevels->setEnabled( toggled );
159  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
160  it.value()->setEnabled( ! toggled );
161  setOverviewList();
162 }
163 
164 void QgsRasterPyramidsOptionsWidget::cbxPyramidsFormat_currentIndexChanged( int index )
165 {
166  mSaveOptionsWidget->setEnabled( index != ERDAS );
168  switch ( index )
169  {
170  case GTIFF:
171  format = QgsRaster::PyramidsGTiff;
172  break;
173  case INTERNAL:
175  break;
176  case ERDAS:
177  format = QgsRaster::PyramidsErdas;
178  break;
179  default:
180  QgsDebugMsg( "Should not happen !" );
181  format = QgsRaster::PyramidsGTiff;
182  break;
183  }
184  mSaveOptionsWidget->setPyramidsFormat( format );
185 }
186 
187 void QgsRasterPyramidsOptionsWidget::setOverviewList()
188 {
189 
190  mOverviewList.clear();
191 
192  // if custom levels is toggled, get selection from line edit
193  if ( cbxPyramidsLevelsCustom->isChecked() )
194  {
195  // should we also validate that numbers are increasing?
196  Q_FOREACH ( const QString &lev, lePyramidsLevels->text().trimmed().split( ' ', QString::SkipEmptyParts ) )
197  {
198  QgsDebugMsg( "lev= " + lev );
199  int tmpInt = lev.toInt();
200  if ( tmpInt > 0 )
201  {
202  QgsDebugMsg( "tmpInt= " + QString::number( tmpInt ) );
203  // if number is valid, add to overview list
204  mOverviewList << tmpInt;
205  }
206  }
207  }
208  else
209  {
210  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
211  {
212  if ( it.value()->isChecked() )
213  mOverviewList << it.key();
214  }
215  }
216 
217  emit overviewListChanged();
218 }
This class is a composition of two QSettings instances:
Definition: qgssettings.h:55
#define QgsDebugMsg(str)
Definition: qgslogger.h:37
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QList< QPair< QString, QString > > pyramidResamplingMethods(const QString &providerKey)
Returns a list of pyramid resampling method name and label pairs for given provider.
QgsRasterPyramidsOptionsWidget(QWidget *parent=0, const QString &provider="gdal")
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
RasterPyramidsFormat
Definition: qgsraster.h:81