QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsprocessingpointcloudexpressionlineedit.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingpointcloudexpressionlineedit.cpp
3 ---------------------
4 begin : April 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
20#include "qgsapplication.h"
21#include "qgsfilterlineedit.h"
22#include "qgsgui.h"
23#include "qgspointcloudexpression.h"
24#include "qgspointcloudlayer.h"
25
26#include <QHBoxLayout>
27#include <QListView>
28#include <QMessageBox>
29#include <QPushButton>
30#include <QString>
31#include <QToolButton>
32
33#include "moc_qgsprocessingpointcloudexpressionlineedit.cpp"
34
35using namespace Qt::StringLiterals;
36
38
39QgsProcessingPointCloudExpressionLineEdit::QgsProcessingPointCloudExpressionLineEdit( QWidget *parent )
40 : QWidget( parent )
41{
42 mLineEdit = new QgsFilterLineEdit();
43 mLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
44
45 mButton = new QToolButton();
46 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
47 mButton->setIcon( QgsApplication::getThemeIcon( u"/mIconExpression.svg"_s ) );
48 connect( mButton, &QAbstractButton::clicked, this, &QgsProcessingPointCloudExpressionLineEdit::editExpression );
49
50 QHBoxLayout *layout = new QHBoxLayout();
51 layout->setContentsMargins( 0, 0, 0, 0 );
52 layout->addWidget( mLineEdit );
53 layout->addWidget( mButton );
54 setLayout( layout );
55
56 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
57 setFocusProxy( mLineEdit );
58 connect( mLineEdit, &QLineEdit::textChanged, this, static_cast<void ( QgsProcessingPointCloudExpressionLineEdit::* )( const QString & )>( &QgsProcessingPointCloudExpressionLineEdit::expressionEdited ) );
59
60 setExpression( expression() );
61}
62
63QgsProcessingPointCloudExpressionLineEdit::~QgsProcessingPointCloudExpressionLineEdit() = default;
64
65void QgsProcessingPointCloudExpressionLineEdit::setLayer( QgsPointCloudLayer *layer )
66{
67 mLayer = layer;
68}
69
70QgsPointCloudLayer *QgsProcessingPointCloudExpressionLineEdit::layer() const
71{
72 return mLayer;
73}
74
75QString QgsProcessingPointCloudExpressionLineEdit::expression() const
76{
77 if ( mLineEdit )
78 return mLineEdit->text();
79
80 return QString();
81}
82
83void QgsProcessingPointCloudExpressionLineEdit::setExpression( const QString &newExpression )
84{
85 if ( mLineEdit )
86 mLineEdit->setText( newExpression );
87}
88
89void QgsProcessingPointCloudExpressionLineEdit::editExpression()
90{
91 const QString currentExpression = expression();
92 QgsProcessingPointCloudExpressionDialog dlg( mLayer );
93 dlg.setExpression( currentExpression );
94
95 if ( dlg.exec() )
96 {
97 const QString newExpression = dlg.expression();
98 setExpression( newExpression );
99 }
100}
101
102void QgsProcessingPointCloudExpressionLineEdit::expressionEdited()
103{
104 emit expressionChanged( expression() );
105}
106
107void QgsProcessingPointCloudExpressionLineEdit::expressionEdited( const QString &expression )
108{
109 emit expressionChanged( expression );
110}
111
112
113QgsProcessingPointCloudExpressionDialog::QgsProcessingPointCloudExpressionDialog( QgsPointCloudLayer *layer, const QString &startExpression, QWidget *parent )
114 : QDialog( parent )
115 , mLayer( layer )
116 , mInitialText( startExpression )
117{
118 setupUi( this );
120
121 mModelAttributes = new QStandardItemModel();
122 mModelValues = new QStandardItemModel();
123 lstAttributes->setModel( mModelAttributes );
124 lstValues->setModel( mModelValues );
125
126 populateAttributes();
127
128 connect( lstAttributes->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsProcessingPointCloudExpressionDialog::lstAttributes_currentChanged );
129 connect( lstAttributes, &QListView::doubleClicked, this, &QgsProcessingPointCloudExpressionDialog::lstAttributes_doubleClicked );
130 connect( lstValues, &QListView::doubleClicked, this, &QgsProcessingPointCloudExpressionDialog::lstValues_doubleClicked );
131 connect( btnEqual, &QPushButton::clicked, this, &QgsProcessingPointCloudExpressionDialog::btnEqual_clicked );
132 connect( btnLessThan, &QPushButton::clicked, this, &QgsProcessingPointCloudExpressionDialog::btnLessThan_clicked );
133 connect( btnGreaterThan, &QPushButton::clicked, this, &QgsProcessingPointCloudExpressionDialog::btnGreaterThan_clicked );
134 connect( btnIn, &QPushButton::clicked, this, &QgsProcessingPointCloudExpressionDialog::btnIn_clicked );
135 connect( btnNotIn, &QPushButton::clicked, this, &QgsProcessingPointCloudExpressionDialog::btnNotIn_clicked );
136 connect( btnLessEqual, &QPushButton::clicked, this, &QgsProcessingPointCloudExpressionDialog::btnLessEqual_clicked );
137 connect( btnGreaterEqual, &QPushButton::clicked, this, &QgsProcessingPointCloudExpressionDialog::btnGreaterEqual_clicked );
138 connect( btnNotEqual, &QPushButton::clicked, this, &QgsProcessingPointCloudExpressionDialog::btnNotEqual_clicked );
139 connect( btnAnd, &QPushButton::clicked, this, &QgsProcessingPointCloudExpressionDialog::btnAnd_clicked );
140 connect( btnOr, &QPushButton::clicked, this, &QgsProcessingPointCloudExpressionDialog::btnOr_clicked );
141
142 QPushButton *pbn = new QPushButton( tr( "&Test" ) );
143 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
144 connect( pbn, &QAbstractButton::clicked, this, &QgsProcessingPointCloudExpressionDialog::test );
145
146 pbn = new QPushButton( tr( "&Clear" ) );
147 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
148 connect( pbn, &QAbstractButton::clicked, this, &QgsProcessingPointCloudExpressionDialog::clear );
149
150 mTxtSql->setText( mInitialText );
151}
152
153void QgsProcessingPointCloudExpressionDialog::setExpression( const QString &text )
154{
155 mTxtSql->setText( text );
156}
157
158QString QgsProcessingPointCloudExpressionDialog::expression()
159{
160 return mTxtSql->text();
161}
162
163void QgsProcessingPointCloudExpressionDialog::populateAttributes()
164{
165 if ( !mLayer )
166 {
167 return;
168 }
169
170 const QgsFields &fields = mLayer->dataProvider()->attributes().toFields();
171 mTxtSql->setFields( fields );
172 for ( int idx = 0; idx < fields.count(); ++idx )
173 {
174 QStandardItem *myItem = new QStandardItem( fields.at( idx ).displayNameWithAlias() );
175 mModelAttributes->insertRow( mModelAttributes->rowCount(), myItem );
176 }
177}
178
179void QgsProcessingPointCloudExpressionDialog::lstAttributes_currentChanged( const QModelIndex &current, const QModelIndex &previous )
180{
181 Q_UNUSED( previous )
182
183 mModelValues->clear();
184 const QString attribute = current.data().toString();
185 if ( attribute.compare( "Classification"_L1, Qt::CaseInsensitive ) == 0 )
186 {
187 const QMap<int, QString> codes = QgsPointCloudDataProvider::translatedLasClassificationCodes();
188 for ( int i = 0; i <= 18; ++i )
189 {
190 QStandardItem *item = new QStandardItem( QString( "%1: %2" ).arg( i ).arg( codes.value( i ) ) );
191 item->setData( i, Qt::UserRole );
192 mModelValues->insertRow( mModelValues->rowCount(), item );
193 }
194 }
195 else
196 {
197 const QgsPointCloudStatistics stats = mLayer->statistics();
198 double value = stats.minimum( attribute );
199 QString valueString = std::isnan( value ) ? tr( "n/a" ) : QString::number( value );
200 QStandardItem *item = new QStandardItem( tr( "Minimum: %1" ).arg( valueString ) );
201 item->setData( value, Qt::UserRole );
202 mModelValues->insertRow( mModelValues->rowCount(), item );
203
204 value = stats.maximum( attribute );
205 valueString = std::isnan( value ) ? tr( "n/a" ) : QString::number( value );
206 item = new QStandardItem( tr( "Maximum: %1" ).arg( valueString ) );
207 item->setData( value, Qt::UserRole );
208 mModelValues->insertRow( mModelValues->rowCount(), item );
209
210 value = stats.mean( attribute );
211 valueString = std::isnan( value ) ? tr( "n/a" ) : QString::number( value );
212 item = new QStandardItem( tr( "Mean: %1" ).arg( valueString ) );
213 item->setData( value, Qt::UserRole );
214 mModelValues->insertRow( mModelValues->rowCount(), item );
215
216 value = stats.stDev( attribute );
217 valueString = std::isnan( value ) ? tr( "n/a" ) : QString::number( value );
218 item = new QStandardItem( tr( "StdDev: %1" ).arg( valueString ) );
219 item->setData( value, Qt::UserRole );
220 mModelValues->insertRow( mModelValues->rowCount(), item );
221 }
222}
223
224void QgsProcessingPointCloudExpressionDialog::lstAttributes_doubleClicked( const QModelIndex &index )
225{
226 mTxtSql->insertText( u"%1 "_s.arg( mModelAttributes->data( index ).toString() ) );
227 mTxtSql->setFocus();
228}
229
230void QgsProcessingPointCloudExpressionDialog::lstValues_doubleClicked( const QModelIndex &index )
231{
232 mTxtSql->insertText( u"%1 "_s.arg( mModelValues->data( index, Qt::UserRole ).toString() ) );
233 mTxtSql->setFocus();
234}
235
236void QgsProcessingPointCloudExpressionDialog::btnEqual_clicked()
237{
238 mTxtSql->insertText( u"= "_s );
239 mTxtSql->setFocus();
240}
241
242void QgsProcessingPointCloudExpressionDialog::btnLessThan_clicked()
243{
244 mTxtSql->insertText( u"< "_s );
245 mTxtSql->setFocus();
246}
247
248void QgsProcessingPointCloudExpressionDialog::btnGreaterThan_clicked()
249{
250 mTxtSql->insertText( u"> "_s );
251 mTxtSql->setFocus();
252}
253
254void QgsProcessingPointCloudExpressionDialog::btnIn_clicked()
255{
256 mTxtSql->insertText( u"IN () "_s );
257 int i, j;
258 mTxtSql->getCursorPosition( &i, &j );
259 mTxtSql->setCursorPosition( i, j - 2 );
260 mTxtSql->setFocus();
261}
262
263void QgsProcessingPointCloudExpressionDialog::btnNotIn_clicked()
264{
265 mTxtSql->insertText( u"NOT IN () "_s );
266 int i, j;
267 mTxtSql->getCursorPosition( &i, &j );
268 mTxtSql->setCursorPosition( i, j - 2 );
269 mTxtSql->setFocus();
270}
271
272void QgsProcessingPointCloudExpressionDialog::btnLessEqual_clicked()
273{
274 mTxtSql->insertText( u"<= "_s );
275 mTxtSql->setFocus();
276}
277
278void QgsProcessingPointCloudExpressionDialog::btnGreaterEqual_clicked()
279{
280 mTxtSql->insertText( u">= "_s );
281 mTxtSql->setFocus();
282}
283
284void QgsProcessingPointCloudExpressionDialog::btnNotEqual_clicked()
285{
286 mTxtSql->insertText( u"!= "_s );
287 mTxtSql->setFocus();
288}
289
290void QgsProcessingPointCloudExpressionDialog::btnAnd_clicked()
291{
292 mTxtSql->insertText( u"AND "_s );
293 mTxtSql->setFocus();
294}
295
296void QgsProcessingPointCloudExpressionDialog::btnOr_clicked()
297{
298 mTxtSql->insertText( u"OR "_s );
299 mTxtSql->setFocus();
300}
301
302void QgsProcessingPointCloudExpressionDialog::test()
303{
304 QgsPointCloudExpression expression( mTxtSql->text() );
305
306 if ( !expression.isValid() && !mTxtSql->text().isEmpty() )
307 {
308 QMessageBox::warning( this, tr( "Query Result" ), tr( "An error occurred while parsing the expression:\n%1" ).arg( expression.parserErrorString() ) );
309 }
310 else
311 {
312 const QSet<QString> attributes = expression.referencedAttributes();
313 int offset;
314 for ( const auto &attribute : attributes )
315 {
316 if ( mLayer && mLayer->dataProvider() && !mLayer->dataProvider()->attributes().find( attribute, offset ) )
317 {
318 QMessageBox::warning( this, tr( "Query Result" ), tr( "\"%1\" not recognized as an available attribute." ).arg( attribute ) );
319 return;
320 }
321 }
322 QMessageBox::information( this, tr( "Query Result" ), tr( "The expression was successfully parsed." ) );
323 }
324}
325
326void QgsProcessingPointCloudExpressionDialog::clear()
327{
328 mTxtSql->clear();
329}
330
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QString displayNameWithAlias() const
Returns the name to use when displaying this field and adds the alias in parenthesis if it is defined...
Definition qgsfield.cpp:109
Container of fields for a vector layer.
Definition qgsfields.h:46
int count
Definition qgsfields.h:50
void clear()
Removes all fields.
Definition qgsfields.cpp:64
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
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:224
static QMap< int, QString > translatedLasClassificationCodes()
Returns the map of LAS classification code to translated string value, corresponding to the ASPRS Sta...
Represents a map layer supporting display of point clouds.
Used to store statistics of a point cloud dataset.
double maximum(const QString &attribute) const
Returns the maximum value for the attribute attribute If no matching statistic is available then NaN ...
double stDev(const QString &attribute) const
Returns the standard deviation value for the attribute attribute If no matching statistic is availabl...
double mean(const QString &attribute) const
Returns the mean value for the attribute attribute If no matching statistic is available then NaN wil...
double minimum(const QString &attribute) const
Returns the minimum value for the attribute attribute If no matching statistic is available then NaN ...