QGIS API Documentation 3.43.0-Master (b60ef06885e)
qgsvariableeditorwidget.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsvariableeditorwidget.h
3 -------------------------
4 Date : April 2015
5 Copyright : (C) 2015 by Nyall Dawson
6 Email : nyall dot dawson 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 QGSVARIABLEEDITORWIDGET_H
17#define QGSVARIABLEEDITORWIDGET_H
18
19#include "qgis_sip.h"
20#include <QWidget>
21#include <QTreeWidget>
22#include <QItemDelegate>
23#include "qgis_gui.h"
24#include <memory>
25
26class QTableWidget;
28class QPushButton;
30class QgsVariableEditorTree;
31class VariableEditorDelegate;
32
42class GUI_EXPORT QgsVariableEditorWidget : public QWidget
43{
44 Q_OBJECT
45
46 Q_PROPERTY( QString settingGroup READ settingGroup WRITE setSettingGroup )
47
48 public:
53 QgsVariableEditorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
54
55 ~QgsVariableEditorWidget() override;
56
64 void setContext( QgsExpressionContext *context );
65
71 QgsExpressionContext *context() const { return mContext.get(); }
72
80 void setEditableScopeIndex( int scopeIndex );
81
87 QgsExpressionContextScope *editableScope() const;
88
96 void setSettingGroup( const QString &group ) { mSettingGroup = group; }
97
105 QString settingGroup() const { return mSettingGroup; }
106
112 QVariantMap variablesInActiveScope() const;
113
114 public slots:
115
121 void reloadContext();
122
123 signals:
124
129
130 protected:
131 void showEvent( QShowEvent *event ) override;
132
133 private:
134 std::unique_ptr<QgsExpressionContext> mContext;
135 int mEditableScopeIndex = -1;
136 QgsVariableEditorTree *mTreeWidget = nullptr;
137 QPushButton *mAddButton = nullptr;
138 QPushButton *mRemoveButton = nullptr;
139 QString mSettingGroup;
140 bool mShown = false;
141
142 QString saveKey() const;
143
144 private slots:
145
146 void mAddButton_clicked();
147 void mRemoveButton_clicked();
148 void selectionChanged();
149};
150
151
152#ifndef SIP_RUN
153
155
156/* QgsVariableEditorTree is NOT part of the public QGIS api. It's only
157 * public here as Qt meta objects can't be nested classes
158 */
159
160class QgsVariableEditorTree : public QTreeWidget
161{
162 Q_OBJECT
163
164 public:
165 enum VariableRoles
166 {
167 ContextIndex = Qt::UserRole,
168 RowBaseColor
169 };
170
171 explicit QgsVariableEditorTree( QWidget *parent = nullptr );
172
173 QTreeWidgetItem *indexToItem( const QModelIndex &index ) const { return itemFromIndex( index ); }
174 QModelIndex itemToIndex( QTreeWidgetItem *item ) const { return indexFromItem( item ); }
175 QString variableNameFromItem( QTreeWidgetItem *item ) const { return item ? item->text( 0 ) : QString(); }
176 QString variableNameFromIndex( const QModelIndex &index ) const { return variableNameFromItem( itemFromIndex( index ) ); }
177 QgsExpressionContextScope *scopeFromItem( QTreeWidgetItem *item ) const;
178 QTreeWidgetItem *itemFromVariable( QgsExpressionContextScope *scope, const QString &name ) const;
179 void setEditableScopeIndex( int scopeIndex ) { mEditableScopeIndex = scopeIndex; }
180 QgsExpressionContextScope *editableScope();
181 void setContext( QgsExpressionContext *context ) { mContext = context; }
182 void refreshTree();
183 void removeItem( QTreeWidgetItem *item );
184 void renameItem( QTreeWidgetItem *item, const QString &name );
185 void resetTree();
186 void emitChanged();
187
188 signals:
189
190 void scopeChanged();
191
192 protected:
193 void keyPressEvent( QKeyEvent *event ) override;
194 void mousePressEvent( QMouseEvent *event ) override;
195 void drawRow( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
196 QColor rowColor( int index ) const;
197 void toggleContextExpanded( QTreeWidgetItem *item );
198 void editNext( const QModelIndex &index );
199
200 QModelIndex moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ) override;
201
202 QIcon mExpandIcon;
203
204 private:
205 VariableEditorDelegate *mEditorDelegate = nullptr;
206 int mEditableScopeIndex = -1;
207 QgsExpressionContext *mContext = nullptr;
208 QMap<QPair<int, QString>, QTreeWidgetItem *> mVariableToItem;
209 QMap<int, QTreeWidgetItem *> mScopeToItem;
210
211 void refreshScopeItems( QgsExpressionContextScope *scope, int scopeIndex );
212 void refreshScopeVariables( QgsExpressionContextScope *scope, int scopeIndex );
213};
214
215
216class VariableEditorDelegate : public QItemDelegate
217{
218 Q_OBJECT
219
220 public:
221 VariableEditorDelegate( QObject *parent = nullptr, QgsVariableEditorTree *tree = nullptr )
222 : QItemDelegate( parent )
223 , mParentTree( tree )
224 {}
225
226 QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
227 void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
228 QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
229 void setModelData( QWidget *widget, QAbstractItemModel *model, const QModelIndex &index ) const override;
230 void setEditorData( QWidget *, const QModelIndex & ) const override {}
231
232 private:
233 QgsVariableEditorTree *mParentTree = nullptr;
234};
235
237
238#endif
239
240#endif //QGSVARIABLEEDITORWIDGET_H
Single scope for storing variables and functions for use within a QgsExpressionContext.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
A tree based widget for editing expression context scope variables.
void scopeChanged()
Emitted when the user has modified a scope using the widget.
QgsExpressionContext * context() const
Returns the current expression context for the widget.
QString settingGroup() const
Returns the setting group for the widget.
void setSettingGroup(const QString &group)
Sets the setting group for the widget.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53