QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  const auto methods {QgsRasterDataProvider::pyramidResamplingMethods( mProvider )};
64  for ( const QPair<QString, QString> &method : methods )
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  const auto constOverviewList = overviewList;
85  for ( int i : constOverviewList )
86  {
87  mOverviewCheckBoxes[ i ] = new QCheckBox( QString::number( i ), this );
88  connect( mOverviewCheckBoxes[ i ], &QCheckBox::toggled,
89  this, &QgsRasterPyramidsOptionsWidget::setOverviewList );
90  layoutPyramidsLevels->addWidget( mOverviewCheckBoxes[ i ] );
91  }
92  }
93  else
94  {
95  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
96  it.value()->setChecked( false );
97  }
98  tmpStr = mySettings.value( prefix + "overviewList", "" ).toString();
99  const auto constSplit = tmpStr.split( ' ', QString::SkipEmptyParts );
100  for ( const QString &lev : constSplit )
101  {
102  if ( mOverviewCheckBoxes.contains( lev.toInt() ) )
103  mOverviewCheckBoxes[ lev.toInt()]->setChecked( true );
104  }
105  setOverviewList();
106 
107  mSaveOptionsWidget->updateProfiles();
108 
109  connect( cbxPyramidsFormat, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
111  connect( cboResamplingMethod, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
113  connect( mSaveOptionsWidget, &QgsRasterFormatSaveOptionsWidget::optionsChanged,
115 }
116 
118 {
119  return cboResamplingMethod->currentData().toString();
120 }
121 
123 {
124  QgsSettings mySettings;
125  QString prefix = mProvider + "/driverOptions/_pyramids/";
126  QString tmpStr;
127 
128  // mySettings.setValue( prefix + "internal", cbxPyramidsInternal->isChecked() );
129  if ( cbxPyramidsFormat->currentIndex() == INTERNAL )
130  tmpStr = QStringLiteral( "internal" );
131  else if ( cbxPyramidsFormat->currentIndex() == ERDAS )
132  tmpStr = QStringLiteral( "external_erdas" );
133  else
134  tmpStr = QStringLiteral( "external" );
135  mySettings.setValue( prefix + "format", tmpStr );
136  mySettings.setValue( prefix + "resampling", resamplingMethod() );
137  mySettings.setValue( prefix + "overviewStr", lePyramidsLevels->text().trimmed() );
138 
139  // overview list
140  tmpStr.clear();
141  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
142  {
143  if ( it.value()->isChecked() )
144  tmpStr += QString::number( it.key() ) + ' ';
145  }
146  mySettings.setValue( prefix + "overviewList", tmpStr.trimmed() );
147 
148  mSaveOptionsWidget->apply();
149 }
150 
152 {
153  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
154  it.value()->setChecked( checked );
155 }
156 
157 void QgsRasterPyramidsOptionsWidget::cbxPyramidsLevelsCustom_toggled( bool toggled )
158 {
159  // if toggled, disable checkboxes and enable line edit
160  lePyramidsLevels->setEnabled( toggled );
161  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
162  it.value()->setEnabled( ! toggled );
163  setOverviewList();
164 }
165 
166 void QgsRasterPyramidsOptionsWidget::cbxPyramidsFormat_currentIndexChanged( int index )
167 {
168  mSaveOptionsWidget->setEnabled( index != ERDAS );
170  switch ( index )
171  {
172  case GTIFF:
173  format = QgsRaster::PyramidsGTiff;
174  break;
175  case INTERNAL:
177  break;
178  case ERDAS:
179  format = QgsRaster::PyramidsErdas;
180  break;
181  default:
182  QgsDebugMsg( QStringLiteral( "Should not happen !" ) );
183  format = QgsRaster::PyramidsGTiff;
184  break;
185  }
186  mSaveOptionsWidget->setPyramidsFormat( format );
187 }
188 
189 void QgsRasterPyramidsOptionsWidget::setOverviewList()
190 {
191 
192  mOverviewList.clear();
193 
194  // if custom levels is toggled, get selection from line edit
195  if ( cbxPyramidsLevelsCustom->isChecked() )
196  {
197  // should we also validate that numbers are increasing?
198  const auto constSplit = lePyramidsLevels->text().trimmed().split( ' ', QString::SkipEmptyParts );
199  for ( const QString &lev : constSplit )
200  {
201  QgsDebugMsg( "lev= " + lev );
202  int tmpInt = lev.toInt();
203  if ( tmpInt > 0 )
204  {
205  QgsDebugMsg( "tmpInt= " + QString::number( tmpInt ) );
206  // if number is valid, add to overview list
207  mOverviewList << tmpInt;
208  }
209  }
210  }
211  else
212  {
213  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
214  {
215  if ( it.value()->isChecked() )
216  mOverviewList << it.key();
217  }
218  }
219 
220  emit overviewListChanged();
221 }
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsRasterPyramidsOptionsWidget::resamplingMethod
QString resamplingMethod() const
Definition: qgsrasterpyramidsoptionswidget.cpp:117
QgsRaster::RasterPyramidsFormat
RasterPyramidsFormat
Definition: qgsraster.h:82
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsRasterPyramidsOptionsWidget::apply
void apply()
Definition: qgsrasterpyramidsoptionswidget.cpp:122
QgsRasterPyramidsOptionsWidget::overviewListChanged
void overviewListChanged()
qgsrasterpyramidsoptionswidget.h
QgsRasterFormatSaveOptionsWidget::optionsChanged
void optionsChanged()
QgsRaster::PyramidsErdas
@ PyramidsErdas
Definition: qgsraster.h:85
QgsRasterPyramidsOptionsWidget::QgsRasterPyramidsOptionsWidget
QgsRasterPyramidsOptionsWidget(QWidget *parent=nullptr, const QString &provider="gdal")
Constructor for QgsRasterPyramidsOptionsWidget.
Definition: qgsrasterpyramidsoptionswidget.cpp:31
QgsRasterDataProvider::pyramidResamplingMethods
static QList< QPair< QString, QString > > pyramidResamplingMethods(const QString &providerKey)
Returns a list of pyramid resampling method name and label pairs for given provider.
Definition: qgsrasterdataprovider.cpp:366
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:289
QgsRaster::PyramidsInternal
@ PyramidsInternal
Definition: qgsraster.h:84
QgsRasterPyramidsOptionsWidget::checkAllLevels
void checkAllLevels(bool checked)
Definition: qgsrasterpyramidsoptionswidget.cpp:151
qgssettings.h
qgsdialog.h
QgsRaster::PyramidsGTiff
@ PyramidsGTiff
Definition: qgsraster.h:83
qgslogger.h
QgsRasterPyramidsOptionsWidget::someValueChanged
void someValueChanged()
QgsRasterFormatSaveOptionsWidget::ProfileLineEdit
@ ProfileLineEdit
Definition: qgsrasterformatsaveoptionswidget.h:43
QgsRasterPyramidsOptionsWidget::overviewList
const QList< int > overviewList() const
Definition: qgsrasterpyramidsoptionswidget.h:42
qgsrasterdataprovider.h