QGIS API Documentation 4.1.0-Master (26185ffb827)
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 "ui_qgsexpressionbuilder.h"
20
21#include "qgis_gui.h"
22#include "qgis_sip.h"
23#include "qgsexpression.h"
26
27#include <QSortFilterProxyModel>
28#include <QStandardItemModel>
29#include <QString>
30#include <QWidget>
31
32using namespace Qt::StringLiterals;
33
34class QgsFields;
36class QgsRelation;
38
39#ifndef SIP_RUN
40static const QString DEFAULT_PROJECT_FUNCTIONS_ITEM_NAME = u"[Project Functions]"_s;
41#endif
42
49class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExpressionBuilderWidgetBase
50{
51 Q_OBJECT
52 public:
64 Q_DECLARE_FLAGS( Flags, Flag )
65 Q_FLAG( Flag )
66
67
68
71 QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
73
78 void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = u"generic"_s, QgsExpressionBuilderWidget::Flags flags = LoadAll );
79
84 void initWithLayer(
85 QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = u"generic"_s, QgsExpressionBuilderWidget::Flags flags = LoadAll
86 );
87
92 void initWithFields(
93 const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = u"generic"_s, QgsExpressionBuilderWidget::Flags flags = LoadAll
94 );
95
101
105 QgsVectorLayer *layer() const;
106
108 Q_DECL_DEPRECATED void loadFieldNames() SIP_DEPRECATED {}
109
111 Q_DECL_DEPRECATED void loadFieldNames( const QgsFields &fields ) SIP_DEPRECATED { mExpressionTreeView->loadFieldNames( fields ); }
112
117 Q_DECL_DEPRECATED void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues ) SIP_DEPRECATED;
118
120 void setGeomCalculator( const QgsDistanceArea &da );
121
126 QString expressionText();
127
129 void setExpressionText( const QString &expression );
130
136 QString expectedOutputFormat();
137
144 void setExpectedOutputFormat( const QString &expected );
145
151 QgsExpressionContext expressionContext() const { return mExpressionContext; }
152
159 void setExpressionContext( const QgsExpressionContext &context );
160
162 bool isExpressionValid();
163
164#ifndef SIP_RUN
165
179 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, const std::function<QgsExpressionContext( const QVariant & )> &previewContextGenerator );
180#else
181
195 void setCustomPreviewGenerator( const QString &label, const QList<QPair<QString, QVariant>> &choices, SIP_PYCALLABLE );
196 //%MethodCode
197 Py_XINCREF( a2 );
198 Py_BEGIN_ALLOW_THREADS sipCpp->setCustomPreviewGenerator( *a0, *a1, [a2]( const QVariant &value ) -> QgsExpressionContext {
200 SIP_BLOCK_THREADS
201 PyObject *s = sipCallMethod( NULL, a2, "D", &value, sipType_QVariant, NULL );
202 int state;
203 int sipIsError = 0;
204 QgsExpressionContext *t1 = reinterpret_cast<QgsExpressionContext *>( sipConvertToType( s, sipType_QgsExpressionContext, 0, SIP_NOT_NONE, &state, &sipIsError ) );
205 if ( sipIsError == 0 )
206 {
207 res = QgsExpressionContext( *t1 );
208 }
209 sipReleaseType( t1, sipType_QgsExpressionContext, state );
210 SIP_UNBLOCK_THREADS
211 return res;
212 } );
213
214 Py_END_ALLOW_THREADS
215 //%End
216#endif
217
218
224 Q_DECL_DEPRECATED void saveToRecent( const QString &collection = "generic" ) SIP_DEPRECATED;
225
231 Q_DECL_DEPRECATED void loadRecent( const QString &collection = u"generic"_s ) SIP_DEPRECATED;
232
237 QgsExpressionTreeView *expressionTree() const;
238
244 Q_DECL_DEPRECATED void loadUserExpressions() SIP_DEPRECATED;
245
251 Q_DECL_DEPRECATED void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText ) SIP_DEPRECATED;
252
258 Q_DECL_DEPRECATED void removeFromUserExpressions( const QString &label ) SIP_DEPRECATED;
259
263 void newFunctionFile( const QString &fileName = "scratch" );
264
268 void saveFunctionFile( QString fileName );
269
273 void loadCodeFromFile( QString path );
274
280 void loadCodeFromProjectFunctions();
281
285 void loadFunctionCode( const QString &code );
286
290 void updateFunctionFileList( const QString &path );
291
298 void saveProjectFunctionsEntry();
299
305 Q_DECL_DEPRECATED QStandardItemModel *model() SIP_DEPRECATED;
306
311 QgsProject *project();
312
318 void setProject( QgsProject *project );
319
325 bool evalError() const;
326
332 bool parserError() const;
333
339 void setExpressionPreviewVisible( bool isVisible );
340
341 public slots:
342
348 void loadSampleValues();
349
355 void loadAllValues();
356
364 void loadSampleUsedValues();
365
373 void loadAllUsedValues();
374
379 void autosave();
380
387 void setAutoSave( bool enabled ) { mAutoSave = enabled; }
388
393 void storeCurrentUserExpression();
394
400 void removeSelectedUserExpression();
401
407 void editSelectedUserExpression();
408
414 const QList<QgsExpressionItem *> findExpressions( const QString &label );
415
416
417 private slots:
418 void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
419 void onExpressionParsed( bool state );
420 void expressionTreeItemChanged( QgsExpressionItem *item );
421 void operatorButtonClicked();
422 void commentLinesClicked();
423 void btnRun_pressed();
424 void btnNewFile_pressed();
425 void btnRemoveFile_pressed();
426
432 void exportUserExpressions_pressed();
433
439 void importUserExpressions_pressed();
440 void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
441 void insertExpressionText( const QString &text );
442 void txtExpressionString_textChanged();
443 void txtSearchEditValues_textChanged();
444 void mValuesListView_doubleClicked( const QModelIndex &index );
445 void txtPython_textChanged();
446
447 signals:
448
455 void expressionParsed( bool isValid );
456
463
470
471 private:
472 class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
473 {
474 public:
475 ExpressionTreeMenuProvider( QgsExpressionBuilderWidget *expressionBuilderWidget )
476 : QgsExpressionTreeView::MenuProvider()
477 , mExpressionBuilderWidget( expressionBuilderWidget )
478 {}
479
480 QMenu *createContextMenu( QgsExpressionItem *item ) override;
481
482 private:
483 QgsExpressionBuilderWidget *mExpressionBuilderWidget;
484 };
485
486 int FUNCTION_MARKER_ID = 25;
487
488 void createErrorMarkers( const QList<QgsExpression::ParserError> &errors );
489 void createMarkers( const QgsExpressionNode *node );
490 void clearFunctionMarkers();
491 void clearErrors();
492 void runPythonCode( const QString &code );
493 void displayTemporaryLabel( const QString &text );
494 QgsVectorLayer *contextLayer( const QgsExpressionItem *item ) const;
495 void fillFieldValues( const QString &fieldName, QgsVectorLayer *layer, int countLimit, bool forceUsedValues = false );
496 QString getFunctionHelp( QgsExpressionFunction *function );
497 QString loadFunctionHelp( QgsExpressionItem *functionName );
498 QString helpStylesheet() const;
499
500 // To be called whenever expression context has been updated
501 void expressionContextUpdated();
502
503 // Will hold items with
504 // * a display string that matches the represented field values
505 // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
506 std::unique_ptr<QStandardItemModel> mValuesModel;
507 std::unique_ptr<QSortFilterProxyModel> mProxyValues;
508
509 ExpressionTreeMenuProvider mExpressionTreeMenuProvider;
510
511 bool mAutoSave = true;
512 QString mFunctionsPath;
513 QgsVectorLayer *mLayer = nullptr;
514 bool mExpressionValid = false;
515 QgsExpressionContext mExpressionContext;
516 QPointer<QgsProject> mProject;
517
518 // Translated name of the user expressions group
519 QString mUserExpressionsGroupName;
520
521 QgsCodeEditorExpression *txtExpressionString = nullptr;
522};
523
524// clazy:excludeall=qstring-allocations
525
526#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 an expression string.
Flag
Flag to determine what should be loaded.
@ LoadRecent
Load recent expressions given the collection key.
@ LoadUserExpressions
Load user expressions.
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 setAutoSave(bool enabled)
Enabled or disable auto saving.
void expressionParsed(bool isValid)
Emitted when the user changes the expression in the widget.
void initWithLayer(QgsVectorLayer *layer, const QgsExpressionContext &context=QgsExpressionContext(), const QString &recentCollection=u"generic"_s, QgsExpressionBuilderWidget::Flags flags=LoadAll)
Initialize with a layer.
Q_DECL_DEPRECATED void loadFieldNames(const QgsFields &fields)
void init(const QgsExpressionContext &context=QgsExpressionContext(), const QString &recentCollection=u"generic"_s, QgsExpressionBuilderWidget::Flags flags=LoadAll)
Initialize without any layer.
QgsVectorLayer * layer() const
Returns the current layer or a nullptr.
QgsExpressionBuilderWidget(QWidget *parent=nullptr)
Create a new expression builder widget with an optional parent.
void setLayer(QgsVectorLayer *layer)
Sets layer in order to get the fields and values.
Q_DECL_DEPRECATED void loadFieldNames()
void initWithFields(const QgsFields &fields, const QgsExpressionContext &context=QgsExpressionContext(), const QString &recentCollection=u"generic"_s, QgsExpressionBuilderWidget::Flags flags=LoadAll)
Initialize with given fields without any layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Syntax highlighter for QGIS expressions.
An expression item that can be used in the QgsExpressionBuilderWidget tree.
Implementation of this interface can be implemented to allow QgsExpressionTreeView instance to provid...
A tree view to list all expressions functions, variables and fields that can be used in an expression...
Container of fields for a vector layer.
Definition qgsfields.h:46
Represents a relationship between two vector layers.
Definition qgsrelation.h:42
Represents a vector layer which manages a vector based dataset.
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:272