QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
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 "qgis_gui.h"
20#include "qgis_sip.h"
22#include "qgsproject.h"
23
24#include <QPointer>
25#include <QSortFilterProxyModel>
26#include <QStandardItemModel>
27#include <QString>
28#include <QTreeView>
29
30using namespace Qt::StringLiterals;
31
32class QgsVectorLayer;
33
34
39class GUI_EXPORT QgsExpressionItem : public QStandardItem
40{
41 public:
48
49 QgsExpressionItem( const QString &label, const QString &expressionText, const QString &helpText, QgsExpressionItem::ItemType itemType = ExpressionNode )
50 : QStandardItem( label )
51 {
52 mExpressionText = expressionText;
53 mHelpText = helpText;
54 mType = itemType;
55 setData( itemType, ITEM_TYPE_ROLE );
56 }
57
58 QgsExpressionItem( const QString &label, const QString &expressionText, QgsExpressionItem::ItemType itemType = ExpressionNode )
59 : QStandardItem( label )
60 {
61 mExpressionText = expressionText;
62 mType = itemType;
63 setData( itemType, ITEM_TYPE_ROLE );
64 }
65
66 QString getExpressionText() const { return mExpressionText; }
67
73 QString getHelpText() const { return mHelpText; }
74
80 void setHelpText( const QString &helpText ) { mHelpText = helpText; }
81
87 QgsExpressionItem::ItemType getItemType() const { return mType; }
88
90 static const int CUSTOM_SORT_ROLE = Qt::UserRole + 1;
92 static const int ITEM_TYPE_ROLE = Qt::UserRole + 2;
94 static const int SEARCH_TAGS_ROLE = Qt::UserRole + 3;
96 static const int ITEM_NAME_ROLE = Qt::UserRole + 4;
98 static const int LAYER_ID_ROLE = Qt::UserRole + 5;
99
100 private:
101 QString mExpressionText;
102 QString mHelpText;
104};
105
106
114class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
115{
116 Q_OBJECT
117
118 public:
120
121 bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
122
128 void setFilterString( const QString &string );
129
130 protected:
131 bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
132
133 private:
134 QString mFilterString;
135};
136
145class GUI_EXPORT QgsExpressionTreeView : public QTreeView
146{
147 Q_OBJECT
148 public:
157 {
158 public:
159 explicit MenuProvider() = default;
160 virtual ~MenuProvider() = default;
161
164 {
165 Q_UNUSED( item )
166 return nullptr;
167 }
168 };
169
171 QgsExpressionTreeView( QWidget *parent = nullptr );
172
176 void setLayer( QgsVectorLayer *layer );
177
181 void loadFieldNames( const QgsFields &fields );
182
189 void setExpressionContext( const QgsExpressionContext &context );
190
196 QgsExpressionContext expressionContext() const { return mExpressionContext; }
197
202 QgsProject *project();
203
209 void setProject( QgsProject *project );
210
215 void setMenuProvider( MenuProvider *provider );
216
220 void refresh();
221
225 QgsExpressionItem *currentItem() const;
226
233 Q_DECL_DEPRECATED QStandardItemModel *model() SIP_SKIP; // TODO remove QGIS 5
234
239 void loadRecent( const QString &collection = u"generic"_s );
240
245 void saveToRecent( const QString &expressionText, const QString &collection = "generic" );
246
250 void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText );
251
255 void removeFromUserExpressions( const QString &label );
256
261 void loadUserExpressions();
262
266 const QList<QgsExpressionItem *> findExpressions( const QString &label );
267
271 QStringList userExpressionLabels() const SIP_SKIP;
272
277 QJsonDocument exportUserExpressions();
278
283 void loadExpressionsFromJson( const QJsonDocument &expressionsDocument );
284
285 signals:
287 void expressionItemDoubleClicked( const QString &text );
288
291
292 public slots:
294 void setSearchText( const QString &text );
295
296
297 private slots:
298 void onDoubleClicked( const QModelIndex &index );
299
300 void showContextMenu( QPoint pt );
301
302 void currentItemChanged( const QModelIndex &index, const QModelIndex & );
303
304 private:
305 void updateFunctionTree();
306
320 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() );
321
333 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() );
334
335 void loadExpressionContext();
336 void loadRelations();
337 void loadLayers();
338 void loadLayerFields( QgsVectorLayer *layer, QgsExpressionItem *parentItem );
339 void loadFieldNames();
340
352 void showMessageBoxConfirmExpressionOverwrite( bool &isApplyToAll, bool &isOkToOverwrite, const QString &label, const QString &oldExpression, const QString &newExpression );
353
354
355 std::unique_ptr<QStandardItemModel> mModel;
356 std::unique_ptr<QgsExpressionItemSearchProxy> mProxyModel;
357 QMap<QString, QgsExpressionItem *> mExpressionGroups;
358
359 MenuProvider *mMenuProvider = nullptr;
360
361 QgsVectorLayer *mLayer = nullptr;
362 QPointer<QgsProject> mProject;
363 QgsExpressionContext mExpressionContext;
364 QString mRecentKey;
365
366 QStringList mUserExpressionLabels;
367};
368
369#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.
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
void setFilterString(const QString &string)
Sets the search filter string.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
An expression item that can be used in the QgsExpressionBuilderWidget tree.
void setHelpText(const QString &helpText)
Set the help text for the current item.
static const int LAYER_ID_ROLE
Layer ID role.
QString getExpressionText() const
static const int SEARCH_TAGS_ROLE
Search tags role.
static const int ITEM_TYPE_ROLE
Item type role.
static const int CUSTOM_SORT_ROLE
Custom sort order role.
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)
static const int ITEM_NAME_ROLE
Item name role.
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...
void expressionItemDoubleClicked(const QString &text)
Emitted when a expression item is double clicked.
void currentExpressionItemChanged(QgsExpressionItem *item)
Emitter when the current expression item changed.
QgsExpressionTreeView(QWidget *parent=nullptr)
Constructor.
void setSearchText(const QString &text)
Sets the text to filter the expression tree.
QgsExpressionContext expressionContext() const
Returns the expression context for the widget.
void loadFieldNames(const QgsFields &fields)
This allows loading fields without specifying a layer.
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:112
Represents a vector layer which manages a vector based dataset.
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_FACTORY
Definition qgis_sip.h:84