QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 <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
111class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
112{
113 Q_OBJECT
114
115 public:
117
118 bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
119
125 void setFilterString( const QString &string );
126
127 protected:
128 bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
129
130 private:
131 QString mFilterString;
132};
133
142class GUI_EXPORT QgsExpressionTreeView : public QTreeView
143{
144 Q_OBJECT
145 public:
154 {
155 public:
156 explicit MenuProvider() = default;
157 virtual ~MenuProvider() = default;
158
161 {
162 Q_UNUSED( item )
163 return nullptr;
164 }
165 };
166
168 QgsExpressionTreeView( QWidget *parent = nullptr );
169
173 void setLayer( QgsVectorLayer *layer );
174
178 void loadFieldNames( const QgsFields &fields );
179
186 void setExpressionContext( const QgsExpressionContext &context );
187
193 QgsExpressionContext expressionContext() const { return mExpressionContext; }
194
199 QgsProject *project();
200
206 void setProject( QgsProject *project );
207
212 void setMenuProvider( MenuProvider *provider );
213
217 void refresh();
218
222 QgsExpressionItem *currentItem() const;
223
230 Q_DECL_DEPRECATED QStandardItemModel *model() SIP_SKIP; // TODO remove QGIS 4
231
236 void loadRecent( const QString &collection = QStringLiteral( "generic" ) );
237
242 void saveToRecent( const QString &expressionText, const QString &collection = "generic" );
243
247 void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText );
248
252 void removeFromUserExpressions( const QString &label );
253
258 void loadUserExpressions();
259
263 const QList<QgsExpressionItem *> findExpressions( const QString &label );
264
268 QStringList userExpressionLabels() const SIP_SKIP;
269
274 QJsonDocument exportUserExpressions();
275
280 void loadExpressionsFromJson( const QJsonDocument &expressionsDocument );
281
282 signals:
284 void expressionItemDoubleClicked( const QString &text );
285
287 void currentExpressionItemChanged( QgsExpressionItem *item );
288
289 public slots:
291 void setSearchText( const QString &text );
292
293
294 private slots:
295 void onDoubleClicked( const QModelIndex &index );
296
297 void showContextMenu( QPoint pt );
298
299 void currentItemChanged( const QModelIndex &index, const QModelIndex & );
300
301 private:
302 void updateFunctionTree();
303
317 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() );
318
330 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() );
331
332 void loadExpressionContext();
333 void loadRelations();
334 void loadLayers();
335 void loadLayerFields( QgsVectorLayer *layer, QgsExpressionItem *parentItem );
336 void loadFieldNames();
337
349 void showMessageBoxConfirmExpressionOverwrite( bool &isApplyToAll, bool &isOkToOverwrite, const QString &label, const QString &oldExpression, const QString &newExpression );
350
351
352 std::unique_ptr<QStandardItemModel> mModel;
353 std::unique_ptr<QgsExpressionItemSearchProxy> mProxyModel;
354 QMap<QString, QgsExpressionItem *> mExpressionGroups;
355
356 MenuProvider *mMenuProvider = nullptr;
357
358 QgsVectorLayer *mLayer = nullptr;
359 QPointer<QgsProject> mProject;
360 QgsExpressionContext mExpressionContext;
361 QString mRecentKey;
362
363 QStringList mUserExpressionLabels;
364};
365
366#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.
QgsExpressionTreeView is a tree view to list all expressions functions, variables and fields that can...
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 data sets.
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_FACTORY
Definition qgis_sip.h:76