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