QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsprocessingrastercalculatorexpressionlineedit.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingrastercalculatorexpressionlineedit.cpp
3 ---------------------
4 begin : July 2023
5 copyright : (C) 2023 by Alexander Bruy
6 email : alexander dot bruy 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 "moc_qgsprocessingrastercalculatorexpressionlineedit.cpp"
20#include "qgsgui.h"
21#include "qgsapplication.h"
22#include "qgsfilterlineedit.h"
23#include "qgsmaplayer.h"
24
25#include <QHBoxLayout>
26#include <QToolButton>
27#include <QListWidgetItem>
28
30
31QgsProcessingRasterCalculatorExpressionLineEdit::QgsProcessingRasterCalculatorExpressionLineEdit( QWidget *parent )
32 : QWidget( parent )
33{
34 mLineEdit = new QgsFilterLineEdit();
35 mLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
36
37 mButton = new QToolButton();
38 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
39 mButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpression.svg" ) ) );
40 connect( mButton, &QAbstractButton::clicked, this, &QgsProcessingRasterCalculatorExpressionLineEdit::editExpression );
41
42 QHBoxLayout *layout = new QHBoxLayout();
43 layout->setContentsMargins( 0, 0, 0, 0 );
44 layout->addWidget( mLineEdit );
45 layout->addWidget( mButton );
46 setLayout( layout );
47
48 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
49 setFocusProxy( mLineEdit );
50 connect( mLineEdit, &QLineEdit::textChanged, this, static_cast<void ( QgsProcessingRasterCalculatorExpressionLineEdit::* )( const QString & )>( &QgsProcessingRasterCalculatorExpressionLineEdit::expressionEdited ) );
51
52 setExpression( expression() );
53}
54
55QgsProcessingRasterCalculatorExpressionLineEdit::~QgsProcessingRasterCalculatorExpressionLineEdit() = default;
56
57void QgsProcessingRasterCalculatorExpressionLineEdit::setLayers( const QVariantList &layers )
58{
59 mLayers = layers;
60}
61
62QString QgsProcessingRasterCalculatorExpressionLineEdit::expression() const
63{
64 if ( mLineEdit )
65 return mLineEdit->text();
66
67 return QString();
68}
69
70void QgsProcessingRasterCalculatorExpressionLineEdit::setExpression( const QString &newExpression )
71{
72 if ( mLineEdit )
73 mLineEdit->setText( newExpression );
74}
75
76void QgsProcessingRasterCalculatorExpressionLineEdit::editExpression()
77{
78 const QString currentExpression = expression();
79 QgsProcessingRasterCalculatorExpressionDialog dlg( mLayers );
80 dlg.setExpression( currentExpression );
81
82 if ( dlg.exec() )
83 {
84 const QString newExpression = dlg.expression();
85 setExpression( newExpression );
86 }
87}
88
89void QgsProcessingRasterCalculatorExpressionLineEdit::expressionEdited()
90{
91 emit expressionChanged( expression() );
92}
93
94void QgsProcessingRasterCalculatorExpressionLineEdit::expressionEdited( const QString &expression )
95{
96 emit expressionChanged( expression );
97}
98
99
100QgsProcessingRasterCalculatorExpressionDialog::QgsProcessingRasterCalculatorExpressionDialog( const QVariantList &layers, const QString &startExpression, QWidget *parent )
101 : QDialog( parent )
102 , mLayers( layers )
103 , mInitialText( startExpression )
104{
105 setupUi( this );
107
108 populateLayers();
109
110 connect( mLayersList, &QListWidget::itemDoubleClicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mLayersList_itemDoubleClicked );
111
112 connect( mBtnPlus, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnPlus_clicked );
113 connect( mBtnMinus, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnMinus_clicked );
114 connect( mBtnMultiply, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnMultiply_clicked );
115 connect( mBtnDivide, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnDivide_clicked );
116 connect( mBtnPower, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnPower_clicked );
117 connect( mBtnSqrt, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnSqrt_clicked );
118 connect( mBtnOpenBracket, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnOpenBracket_clicked );
119 connect( mBtnCloseBracket, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnCloseBracket_clicked );
120 connect( mBtnGreater, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnGreater_clicked );
121 connect( mBtnGreaterEqual, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnGreaterEqual_clicked );
122 connect( mBtnLess, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnLess_clicked );
123 connect( mBtnLessEqual, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnLessEqual_clicked );
124 connect( mBtnEqual, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnEqual_clicked );
125 connect( mBtnNotEqual, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnNotEqual_clicked );
126 connect( mBtnAnd, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnAnd_clicked );
127 connect( mBtnOr, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnOr_clicked );
128 connect( mBtnIf, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnIf_clicked );
129 connect( mBtnMin, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnMin_clicked );
130 connect( mBtnMax, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnMax_clicked );
131 connect( mBtnAbs, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnAbs_clicked );
132 connect( mBtnSin, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnSin_clicked );
133 connect( mBtnCos, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnCos_clicked );
134 connect( mBtnTan, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnTan_clicked );
135 connect( mBtnLog, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnLog_clicked );
136 connect( mBtnAsin, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnAsin_clicked );
137 connect( mBtnAcos, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnAcos_clicked );
138 connect( mBtnAtan, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnAtan_clicked );
139 connect( mBtnLn, &QPushButton::clicked, this, &QgsProcessingRasterCalculatorExpressionDialog::mBtnLn_clicked );
140
141 mExpressionTextEdit->setPlainText( mInitialText );
142}
143
144void QgsProcessingRasterCalculatorExpressionDialog::setExpression( const QString &text )
145{
146 mExpressionTextEdit->setPlainText( text );
147}
148
149QString QgsProcessingRasterCalculatorExpressionDialog::expression()
150{
151 return mExpressionTextEdit->toPlainText();
152}
153
154void QgsProcessingRasterCalculatorExpressionDialog::populateLayers()
155{
156 if ( mLayers.isEmpty() )
157 {
158 return;
159 }
160
161 for ( const QVariant &layer : mLayers )
162 {
163 QListWidgetItem *item = new QListWidgetItem( layer.toString(), mLayersList );
164 mLayersList->addItem( item );
165 }
166}
167
168QString QgsProcessingRasterCalculatorExpressionDialog::quoteBandEntry( const QString &layerName )
169{
170 // '"' -> '\\"'
171 QString quotedName = layerName;
172 quotedName.replace( '\"', QLatin1String( "\\\"" ) );
173 quotedName.append( '\"' );
174 quotedName.prepend( '\"' );
175 return quotedName;
176}
177
178void QgsProcessingRasterCalculatorExpressionDialog::mLayersList_itemDoubleClicked( QListWidgetItem *item )
179{
180 mExpressionTextEdit->insertPlainText( quoteBandEntry( QStringLiteral( "%1@1" ).arg( item->text() ) ) );
181}
182
183void QgsProcessingRasterCalculatorExpressionDialog::mBtnPlus_clicked()
184{
185 mExpressionTextEdit->insertPlainText( QStringLiteral( " + " ) );
186}
187
188void QgsProcessingRasterCalculatorExpressionDialog::mBtnMinus_clicked()
189{
190 mExpressionTextEdit->insertPlainText( QStringLiteral( " - " ) );
191}
192
193void QgsProcessingRasterCalculatorExpressionDialog::mBtnMultiply_clicked()
194{
195 mExpressionTextEdit->insertPlainText( QStringLiteral( " * " ) );
196}
197
198void QgsProcessingRasterCalculatorExpressionDialog::mBtnDivide_clicked()
199{
200 mExpressionTextEdit->insertPlainText( QStringLiteral( " / " ) );
201}
202
203void QgsProcessingRasterCalculatorExpressionDialog::mBtnPower_clicked()
204{
205 mExpressionTextEdit->insertPlainText( QStringLiteral( " ^ " ) );
206}
207
208void QgsProcessingRasterCalculatorExpressionDialog::mBtnSqrt_clicked()
209{
210 mExpressionTextEdit->insertPlainText( QStringLiteral( " sqrt ( " ) );
211}
212
213void QgsProcessingRasterCalculatorExpressionDialog::mBtnOpenBracket_clicked()
214{
215 mExpressionTextEdit->insertPlainText( QStringLiteral( " ( " ) );
216}
217
218void QgsProcessingRasterCalculatorExpressionDialog::mBtnCloseBracket_clicked()
219{
220 mExpressionTextEdit->insertPlainText( QStringLiteral( " ) " ) );
221}
222
223void QgsProcessingRasterCalculatorExpressionDialog::mBtnGreater_clicked()
224{
225 mExpressionTextEdit->insertPlainText( QStringLiteral( " > " ) );
226}
227
228void QgsProcessingRasterCalculatorExpressionDialog::mBtnGreaterEqual_clicked()
229{
230 mExpressionTextEdit->insertPlainText( QStringLiteral( " >= " ) );
231}
232
233void QgsProcessingRasterCalculatorExpressionDialog::mBtnLess_clicked()
234{
235 mExpressionTextEdit->insertPlainText( QStringLiteral( " < " ) );
236}
237
238void QgsProcessingRasterCalculatorExpressionDialog::mBtnLessEqual_clicked()
239{
240 mExpressionTextEdit->insertPlainText( QStringLiteral( " <= " ) );
241}
242
243void QgsProcessingRasterCalculatorExpressionDialog::mBtnEqual_clicked()
244{
245 mExpressionTextEdit->insertPlainText( QStringLiteral( " = " ) );
246}
247
248void QgsProcessingRasterCalculatorExpressionDialog::mBtnNotEqual_clicked()
249{
250 mExpressionTextEdit->insertPlainText( QStringLiteral( " != " ) );
251}
252
253void QgsProcessingRasterCalculatorExpressionDialog::mBtnAnd_clicked()
254{
255 mExpressionTextEdit->insertPlainText( QStringLiteral( " AND " ) );
256}
257
258void QgsProcessingRasterCalculatorExpressionDialog::mBtnOr_clicked()
259{
260 mExpressionTextEdit->insertPlainText( QStringLiteral( " OR " ) );
261}
262
263void QgsProcessingRasterCalculatorExpressionDialog::mBtnIf_clicked()
264{
265 mExpressionTextEdit->insertPlainText( QStringLiteral( " if ( " ) );
266}
267
268void QgsProcessingRasterCalculatorExpressionDialog::mBtnMin_clicked()
269{
270 mExpressionTextEdit->insertPlainText( QStringLiteral( " MIN ( " ) );
271}
272
273void QgsProcessingRasterCalculatorExpressionDialog::mBtnMax_clicked()
274{
275 mExpressionTextEdit->insertPlainText( QStringLiteral( " MAX ( " ) );
276}
277
278void QgsProcessingRasterCalculatorExpressionDialog::mBtnAbs_clicked()
279{
280 mExpressionTextEdit->insertPlainText( QStringLiteral( " ABS ( " ) );
281}
282
283void QgsProcessingRasterCalculatorExpressionDialog::mBtnSin_clicked()
284{
285 mExpressionTextEdit->insertPlainText( QStringLiteral( " sin ( " ) );
286}
287
288void QgsProcessingRasterCalculatorExpressionDialog::mBtnCos_clicked()
289{
290 mExpressionTextEdit->insertPlainText( QStringLiteral( " cos ( " ) );
291}
292
293void QgsProcessingRasterCalculatorExpressionDialog::mBtnTan_clicked()
294{
295 mExpressionTextEdit->insertPlainText( QStringLiteral( " tan ( " ) );
296}
297
298void QgsProcessingRasterCalculatorExpressionDialog::mBtnLog_clicked()
299{
300 mExpressionTextEdit->insertPlainText( QStringLiteral( " log10 ( " ) );
301}
302
303void QgsProcessingRasterCalculatorExpressionDialog::mBtnAsin_clicked()
304{
305 mExpressionTextEdit->insertPlainText( QStringLiteral( " asin ( " ) );
306}
307
308void QgsProcessingRasterCalculatorExpressionDialog::mBtnAcos_clicked()
309{
310 mExpressionTextEdit->insertPlainText( QStringLiteral( " acos ( " ) );
311}
312
313void QgsProcessingRasterCalculatorExpressionDialog::mBtnAtan_clicked()
314{
315 mExpressionTextEdit->insertPlainText( QStringLiteral( " atan ( " ) );
316}
317
318void QgsProcessingRasterCalculatorExpressionDialog::mBtnLn_clicked()
319{
320 mExpressionTextEdit->insertPlainText( QStringLiteral( " ln ( " ) );
321}
322
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:210