QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsexpressionbuilderwidget.h
Go to the documentation of this file.
1/***************************************************************************
2 qgisexpressionbuilderwidget.h - A generic expression builder widget.
3 --------------------------------------
4 Date : 29-May-2011
5 Copyright : (C) 2011 by Nathan Woodrow
6 Email : woodrow.nathan 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
16#ifndef QGSEXPRESSIONBUILDER_H
17#define QGSEXPRESSIONBUILDER_H
18
19#include <QWidget>
20#include <QStandardItemModel>
21#include <QSortFilterProxyModel>
22
23#include "ui_qgsexpressionbuilder.h"
24
25#include "qgis_sip.h"
26#include "qgis_gui.h"
28#include "qgsexpression.h"
30
31
32class QgsFields;
34class QgsRelation;
36
37#ifndef SIP_RUN
38static const QString DEFAULT_PROJECT_FUNCTIONS_ITEM_NAME = QStringLiteral( "[Project Functions]" );
39#endif
40
46class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
47{
48 Q_OBJECT
49 public:
54 enum Flag SIP_ENUM_BASETYPE( IntFlag )
55 {
56 LoadNothing = 0,
57 LoadRecent = 1 << 1,
58 LoadUserExpressions = 1 << 2,
59 LoadAll = LoadRecent | LoadUserExpressions,
60 };
61 Q_DECLARE_FLAGS( Flags, Flag )
62 Q_FLAG( Flag )
63
64
65
68 QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
70
75 void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
76
81 void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
82
87 void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
88
93 void setLayer( QgsVectorLayer *layer );
94
98 QgsVectorLayer *layer() const;
99
101 Q_DECL_DEPRECATED void loadFieldNames() SIP_DEPRECATED {}
102
104 Q_DECL_DEPRECATED void loadFieldNames( const QgsFields &fields ) SIP_DEPRECATED { mExpressionTreeView->loadFieldNames( fields ); }
105
110 Q_DECL_DEPRECATED void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues ) SIP_DEPRECATED;
111
113 void setGeomCalculator( const QgsDistanceArea &da );
114
119 QString expressionText();
120
122 void setExpressionText( const QString &expression );
123
129 QString expectedOutputFormat();
130
137 void setExpectedOutputFormat( const QString &expected );
138
144 QgsExpressionContext expressionContext() const { return mExpressionContext; }
145
152 void setExpressionContext( const QgsExpressionContext &context );
153
155 bool isExpressionValid();
156
157#ifndef SIP_RUN
158
172 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, const std::function<QgsExpressionContext( const QVariant & )> &previewContextGenerator );
173#else
174
188 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, SIP_PYCALLABLE );
189 //%MethodCode
190 Py_XINCREF( a2 );
191 Py_BEGIN_ALLOW_THREADS
192 sipCpp->setCustomPreviewGenerator( *a0, *a1, [a2]( const QVariant &value ) -> QgsExpressionContext {
194 SIP_BLOCK_THREADS
195 PyObject *s = sipCallMethod( NULL, a2, "D", &value, sipType_QVariant, NULL );
196 int state;
197 int sipIsError = 0;
198 QgsExpressionContext *t1 = reinterpret_cast<QgsExpressionContext *>( sipConvertToType( s, sipType_QgsExpressionContext, 0, SIP_NOT_NONE, &state, &sipIsError ) );
199 if ( sipIsError == 0 )
200 {
201 res = QgsExpressionContext( *t1 );
202 }
203 sipReleaseType( t1, sipType_QgsExpressionContext, state );
204 SIP_UNBLOCK_THREADS
205 return res;
206 } );
207
208 Py_END_ALLOW_THREADS
209 //%End
210#endif
211
212
218 Q_DECL_DEPRECATED void saveToRecent( const QString &collection = "generic" ) SIP_DEPRECATED;
219
225 Q_DECL_DEPRECATED void loadRecent( const QString &collection = QStringLiteral( "generic" ) ) SIP_DEPRECATED;
226
231 QgsExpressionTreeView *expressionTree() const;
232
238 Q_DECL_DEPRECATED void loadUserExpressions() SIP_DEPRECATED;
239
245 Q_DECL_DEPRECATED void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText ) SIP_DEPRECATED;
246
252 Q_DECL_DEPRECATED void removeFromUserExpressions( const QString &label ) SIP_DEPRECATED;
253
257 void newFunctionFile( const QString &fileName = "scratch" );
258
262 void saveFunctionFile( QString fileName );
263
267 void loadCodeFromFile( QString path );
268
274 void loadCodeFromProjectFunctions();
275
279 void loadFunctionCode( const QString &code );
280
284 void updateFunctionFileList( const QString &path );
285
291 void saveProjectFunctionsEntry();
292
298 Q_DECL_DEPRECATED QStandardItemModel *model() SIP_DEPRECATED;
299
304 QgsProject *project();
305
311 void setProject( QgsProject *project );
312
318 bool evalError() const;
319
325 bool parserError() const;
326
332 void setExpressionPreviewVisible( bool isVisible );
333
334 public slots:
335
341 void loadSampleValues();
342
348 void loadAllValues();
349
357 void loadSampleUsedValues();
358
366 void loadAllUsedValues();
367
371 void autosave();
372
378 void setAutoSave( bool enabled ) { mAutoSave = enabled; }
379
384 void storeCurrentUserExpression();
385
391 void removeSelectedUserExpression();
392
398 void editSelectedUserExpression();
399
405 const QList<QgsExpressionItem *> findExpressions( const QString &label );
406
407
408 private slots:
409 void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
410 void onExpressionParsed( bool state );
411 void expressionTreeItemChanged( QgsExpressionItem *item );
412 void operatorButtonClicked();
413 void btnRun_pressed();
414 void btnNewFile_pressed();
415 void btnRemoveFile_pressed();
416
422 void exportUserExpressions_pressed();
423
429 void importUserExpressions_pressed();
430 void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
431 void insertExpressionText( const QString &text );
432 void txtExpressionString_textChanged();
433 void txtSearchEditValues_textChanged();
434 void mValuesListView_doubleClicked( const QModelIndex &index );
435 void txtPython_textChanged();
436
437 signals:
438
445 void expressionParsed( bool isValid );
446
453
460
461 protected:
462 void showEvent( QShowEvent *e ) override;
463
464 private:
465 class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
466 {
467 public:
468 ExpressionTreeMenuProvider( QgsExpressionBuilderWidget *expressionBuilderWidget )
469 : QgsExpressionTreeView::MenuProvider()
470 , mExpressionBuilderWidget( expressionBuilderWidget ) {}
471
472 QMenu *createContextMenu( QgsExpressionItem *item ) override;
473
474 private:
475 QgsExpressionBuilderWidget *mExpressionBuilderWidget;
476 };
477
478 int FUNCTION_MARKER_ID = 25;
479
480 void createErrorMarkers( const QList<QgsExpression::ParserError> &errors );
481 void createMarkers( const QgsExpressionNode *node );
482 void clearFunctionMarkers();
483 void clearErrors();
484 void runPythonCode( const QString &code );
485 QgsVectorLayer *contextLayer( const QgsExpressionItem *item ) const;
486 void fillFieldValues( const QString &fieldName, QgsVectorLayer *layer, int countLimit, bool forceUsedValues = false );
487 QString getFunctionHelp( QgsExpressionFunction *function );
488 QString loadFunctionHelp( QgsExpressionItem *functionName );
489 QString helpStylesheet() const;
490
491 // To be called whenever expression context has been updated
492 void expressionContextUpdated();
493
494 // Will hold items with
495 // * a display string that matches the represented field values
496 // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
497 std::unique_ptr<QStandardItemModel> mValuesModel;
498 std::unique_ptr<QSortFilterProxyModel> mProxyValues;
499
500 ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
501
502 bool mAutoSave = true;
503 QString mFunctionsPath;
504 QgsVectorLayer *mLayer = nullptr;
505 bool mExpressionValid = false;
506 QgsExpressionContext mExpressionContext;
507 QPointer<QgsProject> mProject;
508
509 // Translated name of the user expressions group
510 QString mUserExpressionsGroupName;
511
512 QgsCodeEditorExpression *txtExpressionString = nullptr;
513};
514
515// clazy:excludeall=qstring-allocations
516
517#endif // QGSEXPRESSIONBUILDER_H
A QGIS expression editor based on QScintilla2.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
A reusable widget that can be used to build a expression string.
Flag
Flag to determine what should be loaded.
void parserErrorChanged()
Will be set to true if the current expression text reported a parser error with the context.
void evalErrorChanged()
Will be set to true if the current expression text reported an eval error with the context.
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
void expressionParsed(bool isValid)
Emitted when the user changes the expression in the widget.
Q_DECL_DEPRECATED void loadFieldNames(const QgsFields &fields)
Q_DECL_DEPRECATED void loadFieldNames()
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
A abstract base class for defining QgsExpression functions.
An expression item that can be used in the QgsExpressionBuilderWidget tree.
Abstract base class for all nodes that can appear in an expression.
Implementation of this interface can be implemented to allow QgsExpressionTreeView instance to provid...
QgsExpressionTreeView is a tree view to list all expressions functions, variables and fields that can...
Container of fields for a vector layer.
Definition qgsfields.h:46
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
Represents a relationship between two vector layers.
Definition qgsrelation.h:44
Represents a vector layer which manages a vector based data sets.
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:278