QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsprocessingalignrasterlayerswidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingalignrasterlayerswidgetwrapper.cpp
3 ---------------------
4 Date : July 2023
5 Copyright : (C) 2023 by Alexander Bruy
6 Email : alexander dot bruy at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgsprocessingalignrasterlayerswidgetwrapper.cpp"
18
19#include <QBoxLayout>
20#include <QLineEdit>
21#include <QMessageBox>
22#include <QPushButton>
23#include <QStandardItemModel>
24#include <QToolButton>
25
26#include "qgspanelwidget.h"
30#include "qgsrasterfilewriter.h"
31#include "qgis.h"
32
34
35//
36// QgsProcessingAlignRasterLayerDetailsWidget
37//
38
39QgsProcessingAlignRasterLayerDetailsWidget::QgsProcessingAlignRasterLayerDetailsWidget( const QVariant &value, QgsProject *project )
40{
41 setupUi( this );
42
43 mOutputFileWidget->setStorageMode( QgsFileWidget::SaveFile );
44 mOutputFileWidget->setConfirmOverwrite( true );
45
46 QStringList extensions = QgsRasterFileWriter::supportedFormatExtensions();
47 QStringList filters;
48 for ( const QString &ext : extensions )
49 {
50 filters << QObject::tr( "%1 files (*.%2)" ).arg( ext.toUpper(), ext.toLower() );
51 }
52 mOutputFileWidget->setFilter( filters.join( QLatin1String( ";;" ) ) + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" ) );
53
54 cmbResamplingMethod->addItem( tr( "Nearest Neighbour" ), static_cast<int>( Qgis::GdalResampleAlgorithm::RA_NearestNeighbour ) );
55 cmbResamplingMethod->addItem( tr( "Bilinear (2x2 Kernel)" ), static_cast<int>( Qgis::GdalResampleAlgorithm::RA_Bilinear ) );
56 cmbResamplingMethod->addItem( tr( "Cubic (4x4 Kernel)" ), static_cast<int>( Qgis::GdalResampleAlgorithm::RA_Cubic ) );
57 cmbResamplingMethod->addItem( tr( "Cubic B-Spline (4x4 Kernel)" ), static_cast<int>( Qgis::GdalResampleAlgorithm::RA_CubicSpline ) );
58 cmbResamplingMethod->addItem( tr( "Lanczos (6x6 Kernel)" ), static_cast<int>( Qgis::GdalResampleAlgorithm::RA_Lanczos ) );
59 cmbResamplingMethod->addItem( tr( "Average" ), static_cast<int>( Qgis::GdalResampleAlgorithm::RA_Average ) );
60 cmbResamplingMethod->addItem( tr( "Mode" ), static_cast<int>( Qgis::GdalResampleAlgorithm::RA_Mode ) );
61 cmbResamplingMethod->addItem( tr( "Maximum" ), static_cast<int>( Qgis::GdalResampleAlgorithm::RA_Max ) );
62 cmbResamplingMethod->addItem( tr( "Minimum" ), static_cast<int>( Qgis::GdalResampleAlgorithm::RA_Min ) );
63 cmbResamplingMethod->addItem( tr( "Median" ), static_cast<int>( Qgis::GdalResampleAlgorithm::RA_Median ) );
64 cmbResamplingMethod->addItem( tr( "First Quartile (Q1)" ), static_cast<int>( Qgis::GdalResampleAlgorithm::RA_Q1 ) );
65 cmbResamplingMethod->addItem( tr( "Third Quartile (Q3)" ), static_cast<int>( Qgis::GdalResampleAlgorithm::RA_Q3 ) );
66
67 mContext.setProject( project );
68
70 mInputPath = item.inputFilename;
71 mOutputFileWidget->setFilePath( item.outputFilename );
72 cmbResamplingMethod->setCurrentIndex( cmbResamplingMethod->findData( static_cast<int>( item.resampleMethod ) ) );
73 chkRescaleValues->setChecked( item.rescaleValues );
74
75 connect( mOutputFileWidget, &QgsFileWidget::fileChanged, this, &QgsPanelWidget::widgetChanged );
76 connect( cmbResamplingMethod, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
77 connect( chkRescaleValues, &QCheckBox::stateChanged, this, &QgsPanelWidget::widgetChanged );
78}
79
80QVariant QgsProcessingAlignRasterLayerDetailsWidget::value() const
81{
82 QgsAlignRasterData::RasterItem item( mInputPath, mOutputFileWidget->filePath() );
83 item.resampleMethod = static_cast<Qgis::GdalResampleAlgorithm>( cmbResamplingMethod->currentData().toInt() );
84 item.rescaleValues = chkRescaleValues->isChecked();
86}
87
88
89//
90// QgsProcessingAlignRasterLayersPanelWidget
91//
92
93QgsProcessingAlignRasterLayersPanelWidget::QgsProcessingAlignRasterLayersPanelWidget(
94 const QVariant &value,
95 QgsProject *project,
96 QWidget *parent
97)
98 : QgsProcessingMultipleSelectionPanelWidget( QVariantList(), QVariantList(), parent )
99 , mProject( project )
100{
101 connect( listView(), &QListView::doubleClicked, this, &QgsProcessingAlignRasterLayersPanelWidget::configureRaster );
102
103 QPushButton *configureLayerButton = new QPushButton( tr( "Configure Raster…" ) );
104 connect( configureLayerButton, &QPushButton::clicked, this, &QgsProcessingAlignRasterLayersPanelWidget::configureRaster );
105 buttonBox()->addButton( configureLayerButton, QDialogButtonBox::ActionRole );
106
107 // populate the list: first layers already selected, then layers from project not yet selected
108 mContext.setProject( project );
109
110 QSet<const QString> seenFiles;
111 const QVariantList valueList = value.toList();
112 for ( const QVariant &v : valueList )
113 {
115 addOption( v, titleForItem( item ), true );
116 seenFiles.insert( item.inputFilename );
117 }
118
119 const QList<QgsRasterLayer *> options = QgsProcessingUtils::compatibleRasterLayers( project );
120 for ( const QgsRasterLayer *layer : options )
121 {
122 if ( seenFiles.contains( layer->source() ) )
123 continue;
124
125 QVariantMap vm;
126 vm["inputFile"] = layer->source();
127 vm["outputFile"] = QString();
128 vm["resampleMethod"] = static_cast<int>( Qgis::GdalResampleAlgorithm::RA_NearestNeighbour );
129 vm["v"] = false;
130
131 const QString title = layer->source();
132 addOption( vm, title, false );
133 }
134}
135
136void QgsProcessingAlignRasterLayersPanelWidget::configureRaster()
137{
138 const QModelIndexList selection = listView()->selectionModel()->selectedIndexes();
139 if ( selection.size() != 1 )
140 {
141 QMessageBox::warning( this, tr( "Configure Raster" ), tr( "Please select a single layer." ) );
142 return;
143 }
144
145 QStandardItem *item = mModel->itemFromIndex( selection[0] );
146 const QVariant value = item->data( Qt::UserRole );
147
149 if ( panel && panel->dockMode() )
150 {
151 QgsProcessingAlignRasterLayerDetailsWidget *widget = new QgsProcessingAlignRasterLayerDetailsWidget( value, mProject );
152 widget->setPanelTitle( tr( "Configure Raster" ) );
153 widget->buttonBox()->hide();
154
155 connect( widget, &QgsProcessingAlignRasterLayerDetailsWidget::widgetChanged, this, [=]() {
156 setItemValue( item, widget->value() );
157 } );
158 panel->openPanel( widget );
159 }
160 else
161 {
162 QDialog dlg;
163 dlg.setWindowTitle( tr( "Configure Raster" ) );
164 QVBoxLayout *vLayout = new QVBoxLayout();
165 QgsProcessingAlignRasterLayerDetailsWidget *widget = new QgsProcessingAlignRasterLayerDetailsWidget( value, mProject );
166 vLayout->addWidget( widget );
167 connect( widget->buttonBox(), &QDialogButtonBox::accepted, &dlg, &QDialog::accept );
168 connect( widget->buttonBox(), &QDialogButtonBox::rejected, &dlg, &QDialog::reject );
169 dlg.setLayout( vLayout );
170 if ( dlg.exec() )
171 {
172 setItemValue( item, widget->value() );
173 }
174 }
175}
176
177void QgsProcessingAlignRasterLayersPanelWidget::setItemValue( QStandardItem *item, const QVariant &value )
178{
179 mContext.setProject( mProject );
180
182
183 item->setText( titleForItem( rasterItem ) );
184 item->setData( value, Qt::UserRole );
185}
186
187QString QgsProcessingAlignRasterLayersPanelWidget::titleForItem( const QgsAlignRasterData::RasterItem &item )
188{
189 return item.inputFilename;
190}
191
192
193//
194// QgsProcessingAlignRasterLayersWidget
195//
196
197QgsProcessingAlignRasterLayersWidget::QgsProcessingAlignRasterLayersWidget( QWidget *parent )
198 : QWidget( parent )
199{
200 QHBoxLayout *hl = new QHBoxLayout();
201 hl->setContentsMargins( 0, 0, 0, 0 );
202
203 mLineEdit = new QLineEdit();
204 mLineEdit->setEnabled( false );
205 hl->addWidget( mLineEdit, 1 );
206
207 mToolButton = new QToolButton();
208 mToolButton->setText( QString( QChar( 0x2026 ) ) );
209 hl->addWidget( mToolButton );
210
211 setLayout( hl );
212
213 updateSummaryText();
214
215 connect( mToolButton, &QToolButton::clicked, this, &QgsProcessingAlignRasterLayersWidget::showDialog );
216}
217
218void QgsProcessingAlignRasterLayersWidget::setValue( const QVariant &value )
219{
220 if ( value.isValid() )
221 mValue = value.userType() == QMetaType::Type::QVariantList ? value.toList() : QVariantList() << value;
222 else
223 mValue.clear();
224
225 updateSummaryText();
226 emit changed();
227}
228
229void QgsProcessingAlignRasterLayersWidget::setProject( QgsProject *project )
230{
231 mProject = project;
232}
233
234void QgsProcessingAlignRasterLayersWidget::showDialog()
235{
237 if ( panel && panel->dockMode() )
238 {
239 QgsProcessingAlignRasterLayersPanelWidget *widget = new QgsProcessingAlignRasterLayersPanelWidget( mValue, mProject );
240 widget->setPanelTitle( tr( "Input layers" ) );
241 connect( widget, &QgsProcessingMultipleSelectionPanelWidget::selectionChanged, this, [=]() {
242 setValue( widget->selectedOptions() );
243 } );
244 connect( widget, &QgsProcessingMultipleSelectionPanelWidget::acceptClicked, widget, &QgsPanelWidget::acceptPanel );
245 panel->openPanel( widget );
246 }
247 else
248 {
249 QDialog dlg;
250 dlg.setWindowTitle( tr( "Input layers" ) );
251 QVBoxLayout *vLayout = new QVBoxLayout();
252 QgsProcessingAlignRasterLayersPanelWidget *widget = new QgsProcessingAlignRasterLayersPanelWidget( mValue, mProject );
253 vLayout->addWidget( widget );
254 widget->buttonBox()->addButton( QDialogButtonBox::Cancel );
255 connect( widget->buttonBox(), &QDialogButtonBox::accepted, &dlg, &QDialog::accept );
256 connect( widget->buttonBox(), &QDialogButtonBox::rejected, &dlg, &QDialog::reject );
257 dlg.setLayout( vLayout );
258 if ( dlg.exec() )
259 {
260 setValue( widget->selectedOptions() );
261 }
262 }
263}
264
265void QgsProcessingAlignRasterLayersWidget::updateSummaryText()
266{
267 mLineEdit->setText( tr( "%n raster layer(s) selected", nullptr, mValue.count() ) );
268}
269
270
271//
272// QgsProcessingAlignRasterLayersWidgetWrapper
273//
274
275QgsProcessingAlignRasterLayersWidgetWrapper::QgsProcessingAlignRasterLayersWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QWidget *parent )
276 : QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
277{
278}
279
280QString QgsProcessingAlignRasterLayersWidgetWrapper::parameterType() const
281{
283}
284
285QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingAlignRasterLayersWidgetWrapper::createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type )
286{
287 return new QgsProcessingAlignRasterLayersWidgetWrapper( parameter, type );
288}
289
290QWidget *QgsProcessingAlignRasterLayersWidgetWrapper::createWidget()
291{
292 mPanel = new QgsProcessingAlignRasterLayersWidget( nullptr );
293 mPanel->setProject( widgetContext().project() );
294 connect( mPanel, &QgsProcessingAlignRasterLayersWidget::changed, this, [=] {
295 emit widgetValueHasChanged( this );
296 } );
297 return mPanel;
298}
299
300void QgsProcessingAlignRasterLayersWidgetWrapper::setWidgetContext( const QgsProcessingParameterWidgetContext &context )
301{
303 if ( mPanel )
304 {
305 mPanel->setProject( context.project() );
306 }
307}
308
309void QgsProcessingAlignRasterLayersWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
310{
311 Q_UNUSED( context )
312 if ( mPanel )
313 {
314 mPanel->setValue( value );
315 }
316}
317
318QVariant QgsProcessingAlignRasterLayersWidgetWrapper::widgetValue() const
319{
320 return mPanel ? mPanel->value() : QVariant();
321}
322
323QStringList QgsProcessingAlignRasterLayersWidgetWrapper::compatibleParameterTypes() const
324{
325 return QStringList()
332}
333
334QStringList QgsProcessingAlignRasterLayersWidgetWrapper::compatibleOutputTypes() const
335{
336 return QStringList()
342}
343
GdalResampleAlgorithm
Resampling algorithm to be used (equivalent to GDAL's enum GDALResampleAlg)
Definition qgis.h:5365
@ RA_Lanczos
Lanczos windowed sinc interpolation (6x6 kernel)
@ RA_Q3
Third quartile (selects the third quartile of all non-NODATA contributing pixels)
@ RA_CubicSpline
Cubic B-Spline Approximation (4x4 kernel)
@ RA_Q1
First quartile (selects the first quartile of all non-NODATA contributing pixels)
@ RA_Min
Minimum (selects the minimum of all non-NODATA contributing pixels)
@ RA_Median
Median (selects the median of all non-NODATA contributing pixels)
@ RA_NearestNeighbour
Nearest neighbour (select on one input pixel)
@ RA_Average
Average (computes the average of all non-NODATA contributing pixels)
@ RA_Max
Maximum (selects the maximum of all non-NODATA contributing pixels)
@ RA_Bilinear
Bilinear (2x2 kernel)
@ RA_Mode
Mode (selects the value which appears most often of all the sampled points)
@ RA_Cubic
Cubic Convolution Approximation (4x4 kernel)
A widget wrapper for Processing parameter value widgets.
virtual void setWidgetContext(const QgsProcessingParameterWidgetContext &context)
Sets the context in which the Processing parameter widget is shown, e.g., the parent model algorithm,...
@ SaveFile
Select a single new or pre-existing file.
void fileChanged(const QString &path)
Emitted whenever the current file or directory path is changed.
Base class for any widget that can be shown as a inline panel.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
void widgetChanged()
Emitted when the widget state changes.
void acceptPanel()
Accept the panel.
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget.
bool dockMode()
Returns the dock mode state.
Contains information about the context in which a processing algorithm is executed.
WidgetType
Types of dialogs which Processing widgets can be created for.
static QString typeName()
Returns the type name for the output class.
static QString typeName()
Returns the type name for the output class.
static QString typeName()
Returns the type name for the output class.
static QString typeName()
Returns the type name for the output class.
static QString typeName()
Returns the type name for the output class.
static QString typeName()
Returns the type name for the parameter class.
static QgsAlignRasterData::RasterItem variantMapAsItem(const QVariantMap &layerVariantMap, QgsProcessingContext &context)
Converts a QVariant value (a QVariantMap) to a single input layer.
static QVariantMap itemAsVariantMap(const QgsAlignRasterData::RasterItem &item)
Converts a single input layer to QVariant representation (a QVariantMap)
Base class for the definition of processing parameters.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
Contains settings which reflect the context in which a Processing parameter widget is shown,...
QgsProject * project() const
Returns the project associated with the widget.
static QList< QgsRasterLayer * > compatibleRasterLayers(QgsProject *project, bool sort=true)
Returns a list of raster layers from a project which are compatible with the processing framework.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
static QStringList supportedFormatExtensions(RasterFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats.
Represents a raster layer.
Definition of one raster layer for alignment.
Qgis::GdalResampleAlgorithm resampleMethod
resampling method to be used
bool rescaleValues
rescaling of values according to the change of pixel size
QString inputFilename
filename of the source raster
QString outputFilename
filename of the newly created aligned raster (will be overwritten if exists already)