QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
100  const QStringList constSplit = tmpStr.split( ' ', QString::SkipEmptyParts );
101 #else
102  const QStringList constSplit = tmpStr.split( ' ', Qt::SkipEmptyParts );
103 #endif
104  for ( const QString &lev : constSplit )
105  {
106  if ( mOverviewCheckBoxes.contains( lev.toInt() ) )
107  mOverviewCheckBoxes[ lev.toInt()]->setChecked( true );
108  }
109  setOverviewList();
110 
111  mSaveOptionsWidget->updateProfiles();
112 
113  connect( cbxPyramidsFormat, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
115  connect( cboResamplingMethod, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
117  connect( mSaveOptionsWidget, &QgsRasterFormatSaveOptionsWidget::optionsChanged,
119 }
120 
122 {
123  return cboResamplingMethod->currentData().toString();
124 }
125 
127 {
128  QgsSettings mySettings;
129  QString prefix = mProvider + "/driverOptions/_pyramids/";
130  QString tmpStr;
131 
132  // mySettings.setValue( prefix + "internal", cbxPyramidsInternal->isChecked() );
133  if ( cbxPyramidsFormat->currentIndex() == INTERNAL )
134  tmpStr = QStringLiteral( "internal" );
135  else if ( cbxPyramidsFormat->currentIndex() == ERDAS )
136  tmpStr = QStringLiteral( "external_erdas" );
137  else
138  tmpStr = QStringLiteral( "external" );
139  mySettings.setValue( prefix + "format", tmpStr );
140  mySettings.setValue( prefix + "resampling", resamplingMethod() );
141  mySettings.setValue( prefix + "overviewStr", lePyramidsLevels->text().trimmed() );
142 
143  // overview list
144  tmpStr.clear();
145  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
146  {
147  if ( it.value()->isChecked() )
148  tmpStr += QString::number( it.key() ) + ' ';
149  }
150  mySettings.setValue( prefix + "overviewList", tmpStr.trimmed() );
151 
152  mSaveOptionsWidget->apply();
153 }
154 
156 {
157  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
158  it.value()->setChecked( checked );
159 }
160 
161 void QgsRasterPyramidsOptionsWidget::cbxPyramidsLevelsCustom_toggled( bool toggled )
162 {
163  // if toggled, disable checkboxes and enable line edit
164  lePyramidsLevels->setEnabled( toggled );
165  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
166  it.value()->setEnabled( ! toggled );
167  setOverviewList();
168 }
169 
170 void QgsRasterPyramidsOptionsWidget::cbxPyramidsFormat_currentIndexChanged( int index )
171 {
172  mSaveOptionsWidget->setEnabled( index != ERDAS );
174  switch ( index )
175  {
176  case GTIFF:
177  format = QgsRaster::PyramidsGTiff;
178  break;
179  case INTERNAL:
181  break;
182  case ERDAS:
183  format = QgsRaster::PyramidsErdas;
184  break;
185  default:
186  QgsDebugMsg( QStringLiteral( "Should not happen !" ) );
187  format = QgsRaster::PyramidsGTiff;
188  break;
189  }
190  mSaveOptionsWidget->setPyramidsFormat( format );
191 }
192 
193 void QgsRasterPyramidsOptionsWidget::setOverviewList()
194 {
195 
196  mOverviewList.clear();
197 
198  // if custom levels is toggled, get selection from line edit
199  if ( cbxPyramidsLevelsCustom->isChecked() )
200  {
201  // should we also validate that numbers are increasing?
202 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
203  const QStringList constSplit = lePyramidsLevels->text().trimmed().split( ' ', QString::SkipEmptyParts );
204 #else
205  const QStringList constSplit = lePyramidsLevels->text().trimmed().split( ' ', Qt::SkipEmptyParts );
206 #endif
207  for ( const QString &lev : constSplit )
208  {
209  QgsDebugMsg( "lev= " + lev );
210  int tmpInt = lev.toInt();
211  if ( tmpInt > 0 )
212  {
213  QgsDebugMsg( "tmpInt= " + QString::number( tmpInt ) );
214  // if number is valid, add to overview list
215  mOverviewList << tmpInt;
216  }
217  }
218  }
219  else
220  {
221  for ( auto it = mOverviewCheckBoxes.constBegin(); it != mOverviewCheckBoxes.constEnd(); ++it )
222  {
223  if ( it.value()->isChecked() )
224  mOverviewList << it.key();
225  }
226  }
227 
228  emit overviewListChanged();
229 }
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=nullptr, const QString &provider="gdal")
Constructor for QgsRasterPyramidsOptionsWidget.
RasterPyramidsFormat
Definition: qgsraster.h:82
@ PyramidsInternal
Definition: qgsraster.h:84
@ PyramidsErdas
Definition: qgsraster.h:85
@ PyramidsGTiff
Definition: qgsraster.h:83
#define QgsDebugMsg(str)
Definition: qgslogger.h:38