QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
33
38class GUI_EXPORT QgsExpressionItem : public QStandardItem
39{
40 public:
42 {
45 ExpressionNode
46 };
47
48 QgsExpressionItem( const QString &label,
49 const QString &expressionText,
50 const QString &helpText,
51 QgsExpressionItem::ItemType itemType = ExpressionNode )
52 : QStandardItem( label )
53 {
54 mExpressionText = expressionText;
55 mHelpText = helpText;
56 mType = itemType;
57 setData( itemType, ITEM_TYPE_ROLE );
58 }
59
60 QgsExpressionItem( const QString &label,
61 const QString &expressionText,
62 QgsExpressionItem::ItemType itemType = ExpressionNode )
63 : QStandardItem( label )
64 {
65 mExpressionText = expressionText;
66 mType = itemType;
67 setData( itemType, ITEM_TYPE_ROLE );
68 }
69
70 QString getExpressionText() const { return mExpressionText; }
71
77 QString getHelpText() const { return mHelpText; }
78
84 void setHelpText( const QString &helpText ) { mHelpText = helpText; }
85
91 QgsExpressionItem::ItemType getItemType() const { return mType; }
92
94 static const int CUSTOM_SORT_ROLE = Qt::UserRole + 1;
96 static const int ITEM_TYPE_ROLE = Qt::UserRole + 2;
98 static const int SEARCH_TAGS_ROLE = Qt::UserRole + 3;
100 static const int ITEM_NAME_ROLE = Qt::UserRole + 4;
102 static const int LAYER_ID_ROLE = Qt::UserRole + 5;
103
104 private:
105 QString mExpressionText;
106 QString mHelpText;
108};
109
110
117class GUI_EXPORT QgsExpressionItemSearchProxy : public QSortFilterProxyModel
118{
119 Q_OBJECT
120
121 public:
123
124 bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
125
131 void setFilterString( const QString &string );
132
133 protected:
134
135 bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
136
137 private:
138
139 QString mFilterString;
140};
141
150class GUI_EXPORT QgsExpressionTreeView : public QTreeView
151{
152 Q_OBJECT
153 public:
154
163 {
164 public:
165
166 explicit MenuProvider() = default;
167 virtual ~MenuProvider() = default;
168
170 virtual QMenu *createContextMenu( QgsExpressionItem *item ) SIP_FACTORY {Q_UNUSED( item ) return nullptr;}
171 };
172
174 QgsExpressionTreeView( QWidget *parent = nullptr );
175
179 void setLayer( QgsVectorLayer *layer );
180
184 void loadFieldNames( const QgsFields &fields );
185
192 void setExpressionContext( const QgsExpressionContext &context );
193
199 QgsExpressionContext expressionContext() const { return mExpressionContext; }
200
205 QgsProject *project();
206
212 void setProject( QgsProject *project );
213
218 void setMenuProvider( MenuProvider *provider );
219
223 void refresh();
224
228 QgsExpressionItem *currentItem() const;
229
236 Q_DECL_DEPRECATED QStandardItemModel *model() SIP_SKIP; // TODO remove QGIS 4
237
242 void loadRecent( const QString &collection = QStringLiteral( "generic" ) );
243
248 void saveToRecent( const QString &expressionText, const QString &collection = "generic" );
249
253 void saveToUserExpressions( const QString &label, const QString &expression, const QString &helpText );
254
258 void removeFromUserExpressions( const QString &label );
259
264 void loadUserExpressions( );
265
269 const QList<QgsExpressionItem *> findExpressions( const QString &label );
270
274 QStringList userExpressionLabels() const SIP_SKIP;
275
280 QJsonDocument exportUserExpressions();
281
286 void loadExpressionsFromJson( const QJsonDocument &expressionsDocument );
287
288 signals:
290 void expressionItemDoubleClicked( const QString &text );
291
293 void currentExpressionItemChanged( QgsExpressionItem *item );
294
295 public slots:
297 void setSearchText( const QString &text );
298
299
300 private slots:
301 void onDoubleClicked( const QModelIndex &index );
302
303 void showContextMenu( QPoint pt );
304
305 void currentItemChanged( const QModelIndex &index, const QModelIndex & );
306
307 private:
308 void updateFunctionTree();
309
323 QgsExpressionItem *registerItem( const QString &group, const QString &label, const QString &expressionText,
324 const QString &helpText = QString(),
325 QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
326 bool highlightedItem = false, int sortOrder = 1,
327 const QIcon &icon = QIcon(),
328 const QStringList &tags = QStringList(),
329 const QString &name = QString() );
330
342 void registerItemForAllGroups( const QStringList &groups, const QString &label, const QString &expressionText,
343 const QString &helpText = QString(),
344 QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
345 bool highlightedItem = false, int sortOrder = 1, const QStringList &tags = QStringList() );
346
347 void loadExpressionContext();
348 void loadRelations();
349 void loadLayers();
350 void loadLayerFields( QgsVectorLayer *layer, QgsExpressionItem *parentItem );
351 void loadFieldNames();
352
364 void showMessageBoxConfirmExpressionOverwrite( bool &isApplyToAll, bool &isOkToOverwrite, const QString &label, const QString &oldExpression, const QString &newExpression );
365
366
367 std::unique_ptr<QStandardItemModel> mModel;
368 std::unique_ptr<QgsExpressionItemSearchProxy> mProxyModel;
369 QMap<QString, QgsExpressionItem *> mExpressionGroups;
370
371 MenuProvider *mMenuProvider = nullptr;
372
373 QgsVectorLayer *mLayer = nullptr;
374 QPointer< QgsProject > mProject;
375 QgsExpressionContext mExpressionContext;
376 QString mRecentKey;
377
378 QStringList mUserExpressionLabels;
379};
380
381#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