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