QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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:
50
55 enum Flag SIP_ENUM_BASETYPE( IntFlag )
56 {
57 LoadNothing = 0,
58 LoadRecent = 1 << 1,
59 LoadUserExpressions = 1 << 2,
60 LoadAll = LoadRecent | LoadUserExpressions,
61 };
62 Q_DECLARE_FLAGS( Flags, Flag )
63 Q_FLAG( Flag )
64
65
66
69 QgsExpressionBuilderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
71
76 void init( const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
77
82 void initWithLayer( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
83
88 void initWithFields( const QgsFields &fields, const QgsExpressionContext &context = QgsExpressionContext(), const QString &recentCollection = QStringLiteral( "generic" ), QgsExpressionBuilderWidget::Flags flags = LoadAll );
89
94 void setLayer( QgsVectorLayer *layer );
95
99 QgsVectorLayer *layer() const;
100
102 Q_DECL_DEPRECATED void loadFieldNames() {} SIP_DEPRECATED
103
105 Q_DECL_DEPRECATED void loadFieldNames( const QgsFields &fields ) {mExpressionTreeView->loadFieldNames( fields );} SIP_DEPRECATED
106
111 Q_DECL_DEPRECATED void loadFieldsAndValues( const QMap<QString, QStringList> &fieldValues ) SIP_DEPRECATED;
112
114 void setGeomCalculator( const QgsDistanceArea &da );
115
120 QString expressionText();
121
123 void setExpressionText( const QString &expression );
124
130 QString expectedOutputFormat();
131
138 void setExpectedOutputFormat( const QString &expected );
139
145 QgsExpressionContext expressionContext() const { return mExpressionContext; }
146
153 void setExpressionContext( const QgsExpressionContext &context );
154
156 bool isExpressionValid();
157
158#ifndef SIP_RUN
159
173 void setCustomPreviewGenerator( const QString &label, const QList< QPair< QString, QVariant > > &choices, const std::function< QgsExpressionContext( const QVariant & ) > &previewContextGenerator );
174#else
175
189 void setCustomPreviewGenerator( const QString &label, const QList< QPair< QString, QVariant > > &choices, SIP_PYCALLABLE );
190 % MethodCode
191 Py_XINCREF( a2 );
192 Py_BEGIN_ALLOW_THREADS
193 sipCpp->setCustomPreviewGenerator( *a0, *a1, [a2]( const QVariant &value )->QgsExpressionContext
194 {
196 SIP_BLOCK_THREADS
197 PyObject *s = sipCallMethod( NULL, a2, "D", &value, sipType_QVariant, NULL );
198 int state;
199 int sipIsError = 0;
200 QgsExpressionContext *t1 = reinterpret_cast<QgsExpressionContext *>( sipConvertToType( s, sipType_QgsExpressionContext, 0, SIP_NOT_NONE, &state, &sipIsError ) );
201 if ( sipIsError == 0 )
202 {
203 res = QgsExpressionContext( *t1 );
204 }
205 sipReleaseType( t1, sipType_QgsExpressionContext, state );
206 SIP_UNBLOCK_THREADS
207 return res;
208 } );
209
210 Py_END_ALLOW_THREADS
211 % End
212#endif
213
214
220 Q_DECL_DEPRECATED void saveToRecent( const QString &collection = "generic" ) SIP_DEPRECATED;
221
227 Q_DECL_DEPRECATED void loadRecent( const QString &collection = QStringLiteral( "generic" ) )SIP_DEPRECATED ;
228
233 QgsExpressionTreeView *expressionTree() const;
234
240 Q_DECL_DEPRECATED void loadUserExpressions() SIP_DEPRECATED;
241
247 Q_DECL_DEPRECATED void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText ) SIP_DEPRECATED;
248
254 Q_DECL_DEPRECATED void removeFromUserExpressions( const QString &label ) SIP_DEPRECATED;
255
259 void newFunctionFile( const QString &fileName = "scratch" );
260
264 void saveFunctionFile( QString fileName );
265
269 void loadCodeFromFile( QString path );
270
276 void loadCodeFromProjectFunctions();
277
281 void loadFunctionCode( const QString &code );
282
286 void updateFunctionFileList( const QString &path );
287
293 void saveProjectFunctionsEntry();
294
300 Q_DECL_DEPRECATED QStandardItemModel *model() SIP_DEPRECATED;
301
306 QgsProject *project();
307
313 void setProject( QgsProject *project );
314
320 bool evalError() const;
321
327 bool parserError() const;
328
334 void setExpressionPreviewVisible( bool isVisible );
335
336 public slots:
337
343 void loadSampleValues();
344
350 void loadAllValues();
351
359 void loadSampleUsedValues();
360
368 void loadAllUsedValues();
369
373 void autosave();
374
380 void setAutoSave( bool enabled ) { mAutoSave = enabled; }
381
386 void storeCurrentUserExpression( );
387
393 void removeSelectedUserExpression( );
394
400 void editSelectedUserExpression();
401
407 const QList<QgsExpressionItem *> findExpressions( const QString &label );
408
409
410 private slots:
411 void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
412 void onExpressionParsed( bool state );
413 void expressionTreeItemChanged( QgsExpressionItem *item );
414 void operatorButtonClicked();
415 void btnRun_pressed();
416 void btnNewFile_pressed();
417 void btnRemoveFile_pressed();
418
424 void exportUserExpressions_pressed();
425
431 void importUserExpressions_pressed();
432 void cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem );
433 void insertExpressionText( const QString &text );
434 void txtExpressionString_textChanged();
435 void txtSearchEditValues_textChanged();
436 void mValuesListView_doubleClicked( const QModelIndex &index );
437 void txtPython_textChanged();
438
439 signals:
440
447 void expressionParsed( bool isValid );
448
455
462
463 protected:
464 void showEvent( QShowEvent *e ) override;
465
466 private:
467 class ExpressionTreeMenuProvider : public QgsExpressionTreeView::MenuProvider
468 {
469 public:
470 ExpressionTreeMenuProvider( QgsExpressionBuilderWidget *expressionBuilderWidget )
471 : QgsExpressionTreeView::MenuProvider()
472 , mExpressionBuilderWidget( expressionBuilderWidget ) {}
473
474 QMenu *createContextMenu( QgsExpressionItem *item ) override;
475
476 private:
477 QgsExpressionBuilderWidget *mExpressionBuilderWidget;
478 };
479
480 int FUNCTION_MARKER_ID = 25;
481
482 void createErrorMarkers( const QList<QgsExpression::ParserError> &errors );
483 void createMarkers( const QgsExpressionNode *node );
484 void clearFunctionMarkers();
485 void clearErrors();
486 void runPythonCode( const QString &code );
487 QgsVectorLayer *contextLayer( const QgsExpressionItem *item ) const;
488 void fillFieldValues( const QString &fieldName, QgsVectorLayer *layer, int countLimit, bool forceUsedValues = false );
489 QString getFunctionHelp( QgsExpressionFunction *function );
490 QString loadFunctionHelp( QgsExpressionItem *functionName );
491 QString helpStylesheet() const;
492
493 // To be called whenever expression context has been updated
494 void expressionContextUpdated();
495
496 // Will hold items with
497 // * a display string that matches the represented field values
498 // * custom data in Qt::UserRole + 1 that contains a ready to use expression literal ('quoted string' or NULL or a plain number )
499 std::unique_ptr<QStandardItemModel> mValuesModel;
500 std::unique_ptr<QSortFilterProxyModel> mProxyValues;
501
502 ExpressionTreeMenuProvider *mExpressionTreeMenuProvider = nullptr;
503
504 bool mAutoSave = true;
505 QString mFunctionsPath;
506 QgsVectorLayer *mLayer = nullptr;
507 bool mExpressionValid = false;
508 QgsExpressionContext mExpressionContext;
509 QPointer< QgsProject > mProject;
510
511 // Translated name of the user expressions group
512 QString mUserExpressionsGroupName;
513
514 QgsCodeEditorExpression *txtExpressionString = nullptr;
515};
516
517// clazy:excludeall=qstring-allocations
518
519#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