QGIS API Documentation 3.43.0-Master (c67cf405802)
qgsexpressiontreeview.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsexpressiontreeview.h
3 --------------------------------------
4 Date : march 2020 - quarantine day 9
5 Copyright : (C) 2020 by Denis Rouzaud
6 Email : denis@opengis.ch
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 QGSEXPRESSIONTREEVIEW_H
17#define QGSEXPRESSIONTREEVIEW_H
18
19#include <QTreeView>
20#include <QStandardItemModel>
21#include <QSortFilterProxyModel>
22#include <QPointer>
23
24#include "qgis_gui.h"
25#include "qgis_sip.h"
27#include "qgsproject.h"
28
29
30class QgsVectorLayer;
31
32
37class GUI_EXPORT QgsExpressionItem : public QStandardItem
38{
39 public:
41 {
44 ExpressionNode
45 };
46
47 QgsExpressionItem( const QString &label, const QString &expressionText, const QString &helpText, QgsExpressionItem::ItemType itemType = ExpressionNode )
48 : QStandardItem( label )
49 {
50 mExpressionText = expressionText;
51 mHelpText = helpText;
52 mType = itemType;
53 setData( itemType, ITEM_TYPE_ROLE );
54 }
55
56 QgsExpressionItem( const QString &label, const QString &expressionText, QgsExpressionItem::ItemType itemType = ExpressionNode )
57 : QStandardItem( label )
58 {
59 mExpressionText = expressionText;
60 mType = itemType;
61 setData( itemType, ITEM_TYPE_ROLE );
62 }
63
64 QString getExpressionText() const { return mExpressionText; }
65
71 QString getHelpText() const { return mHelpText; }
72
78 void setHelpText( const QString &helpText ) { mHelpText = helpText; }
79
85 QgsExpressionItem::ItemType getItemType() const { return mType; }
86
88 static const int CUSTOM_SORT_ROLE = Qt::UserRole + 1;
90 static const int ITEM_TYPE_ROLE = Qt::UserRole + 2;
92 static const int SEARCH_TAGS_ROLE = Qt::UserRole + 3;
94 static const int ITEM_NAME_ROLE = Qt::UserRole + 4;
96 static const int LAYER_ID_ROLE = Qt::UserRole + 5;
97
98 private:
99 QString mExpressionText;
100 QString mHelpText;
102};
103
104
112class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
113{
114 Q_OBJECT
115
116 public:
118
119 bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
120
126 void setFilterString( const QString &string );
127
128 protected:
129 bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
130
131 private:
132 QString mFilterString;
133};
134
143class GUI_EXPORT QgsExpressionTreeView : public QTreeView
144{
145 Q_OBJECT
146 public:
155 {
156 public:
157 explicit MenuProvider() = default;
158 virtual ~MenuProvider() = default;
159
162 {
163 Q_UNUSED( item )
164 return nullptr;
165 }
166 };
167
169 QgsExpressionTreeView( QWidget *parent = nullptr );
170
174 void setLayer( QgsVectorLayer *layer );
175
179 void loadFieldNames( const QgsFields &fields );
180
187 void setExpressionContext( const QgsExpressionContext &context );
188
194 QgsExpressionContext expressionContext() const { return mExpressionContext; }
195
200 QgsProject *project();
201
207 void setProject( QgsProject *project );
208
213 void setMenuProvider( MenuProvider *provider );
214
218 void refresh();
219
223 QgsExpressionItem *currentItem() const;
224
231 Q_DECL_DEPRECATED QStandardItemModel *model() SIP_SKIP; // TODO remove QGIS 4
232
237 void loadRecent( const QString &collection = QStringLiteral( "generic" ) );
238
243 void saveToRecent( const QString &expressionText, const QString &collection = "generic" );
244
248 void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText );
249
253 void removeFromUserExpressions( const QString &label );
254
259 void loadUserExpressions();
260
264 const QList<QgsExpressionItem *> findExpressions( const QString &label );
265
269 QStringList userExpressionLabels() const SIP_SKIP;
270
275 QJsonDocument exportUserExpressions();
276
281 void loadExpressionsFromJson( const QJsonDocument &expressionsDocument );
282
283 signals:
285 void expressionItemDoubleClicked( const QString &text );
286
288 void currentExpressionItemChanged( QgsExpressionItem *item );
289
290 public slots:
292 void setSearchText( const QString &text );
293
294
295 private slots:
296 void onDoubleClicked( const QModelIndex &index );
297
298 void showContextMenu( QPoint pt );
299
300 void currentItemChanged( const QModelIndex &index, const QModelIndex & );
301
302 private:
303 void updateFunctionTree();
304
318 QgsExpressionItem *registerItem( const QString &group, const QString &label, const QString &expressionText, const QString &helpText = QString(), QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode, bool highlightedItem = false, int sortOrder = 1, const QIcon &icon = QIcon(), const QStringList &tags = QStringList(), const QString &name = QString() );
319
331 void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText, const QString &helpText = QString(), QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode, bool highlightedItem = false, int sortOrder = 1, const QStringList &tags = QStringList() );
332
333 void loadExpressionContext();
334 void loadRelations();
335 void loadLayers();
336 void loadLayerFields( QgsVectorLayer *layer, QgsExpressionItem *parentItem );
337 void loadFieldNames();
338
350 void showMessageBoxConfirmExpressionOverwrite( bool &isApplyToAll, bool &isOkToOverwrite, const QString &label, const QString &oldExpression, const QString &newExpression );
351
352
353 std::unique_ptr<QStandardItemModel> mModel;
354 std::unique_ptr<QgsExpressionItemSearchProxy> mProxyModel;
355 QMap<QString, QgsExpressionItem *> mExpressionGroups;
356
357 MenuProvider *mMenuProvider = nullptr;
358
359 QgsVectorLayer *mLayer = nullptr;
360 QPointer<QgsProject> mProject;
361 QgsExpressionContext mExpressionContext;
362 QString mRecentKey;
363
364 QStringList mUserExpressionLabels;
365};
366
367#endif // QGSEXPRESSIONTREEVIEW_H
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Search proxy used to filter the QgsExpressionBuilderWidget tree.
An expression item that can be used in the QgsExpressionBuilderWidget tree.
void setHelpText(const QString &helpText)
Set the help text for the current item.
QString getExpressionText() const
QgsExpressionItem(const QString &label, const QString &expressionText, const QString &helpText, QgsExpressionItem::ItemType itemType=ExpressionNode)
QgsExpressionItem::ItemType getItemType() const
Gets the type of expression item, e.g., header, field, ExpressionNode.
QString getHelpText() const
Gets the help text that is associated with this expression item.
QgsExpressionItem(const QString &label, const QString &expressionText, QgsExpressionItem::ItemType itemType=ExpressionNode)
Implementation of this interface can be implemented to allow QgsExpressionTreeView instance to provid...
virtual QMenu * createContextMenu(QgsExpressionItem *item)
Returns a newly created menu instance.
A tree view to list all expressions functions, variables and fields that can be used in an expression...
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
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 vector layer which manages a vector based dataset.
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_FACTORY
Definition qgis_sip.h:76