QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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 <memory>
20
21#include "qgis_gui.h"
22#include "qgis_sip.h"
23
24#include <QItemDelegate>
25#include <QTreeWidget>
26#include <QWidget>
27
28class QTableWidget;
30class QPushButton;
32class QgsVariableEditorTree;
33class VariableEditorDelegate;
34
43
44class GUI_EXPORT QgsVariableEditorWidget : public QWidget
45{
46 Q_OBJECT
47
48 Q_PROPERTY( QString settingGroup READ settingGroup WRITE setSettingGroup )
49
50 public:
55 QgsVariableEditorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
56
57 ~QgsVariableEditorWidget() override;
58
67
73 QgsExpressionContext *context() const { return mContext.get(); }
74
82 void setEditableScopeIndex( int scopeIndex );
83
89 QgsExpressionContextScope *editableScope() const;
90
98 void setSettingGroup( const QString &group ) { mSettingGroup = group; }
99
107 QString settingGroup() const { return mSettingGroup; }
108
114 QVariantMap variablesInActiveScope() const;
115
116 public slots:
117
123 void reloadContext();
124
125 signals:
126
131
132 protected:
133 void showEvent( QShowEvent *event ) override;
134
135 private:
136 std::unique_ptr<QgsExpressionContext> mContext;
137 int mEditableScopeIndex = -1;
138 QgsVariableEditorTree *mTreeWidget = nullptr;
139 QPushButton *mAddButton = nullptr;
140 QPushButton *mRemoveButton = nullptr;
141 QString mSettingGroup;
142 bool mShown = false;
143
144 QString saveKey() const;
145
146 private slots:
147
148 void mAddButton_clicked();
149 void mRemoveButton_clicked();
150 void selectionChanged();
151};
152
153
154#ifndef SIP_RUN
155
157
158/* QgsVariableEditorTree is NOT part of the public QGIS api. It's only
159 * public here as Qt meta objects can't be nested classes
160 */
161
162class QgsVariableEditorTree : public QTreeWidget
163{
164 Q_OBJECT
165
166 public:
167 enum VariableRoles
168 {
169 ContextIndex = Qt::UserRole,
170 RowBaseColor
171 };
172
173 explicit QgsVariableEditorTree( QWidget *parent = nullptr );
174
175 QTreeWidgetItem *indexToItem( const QModelIndex &index ) const { return itemFromIndex( index ); }
176 QModelIndex itemToIndex( QTreeWidgetItem *item ) const { return indexFromItem( item ); }
177 QString variableNameFromItem( QTreeWidgetItem *item ) const { return item ? item->text( 0 ) : QString(); }
178 QString variableNameFromIndex( const QModelIndex &index ) const { return variableNameFromItem( itemFromIndex( index ) ); }
179 QgsExpressionContextScope *scopeFromItem( QTreeWidgetItem *item ) const;
180 QTreeWidgetItem *itemFromVariable( QgsExpressionContextScope *scope, const QString &name ) const;
181 void setEditableScopeIndex( int scopeIndex ) { mEditableScopeIndex = scopeIndex; }
182 QgsExpressionContextScope *editableScope();
183 void setContext( QgsExpressionContext *context ) { mContext = context; }
184 void refreshTree();
185 void removeItem( QTreeWidgetItem *item );
186 void renameItem( QTreeWidgetItem *item, const QString &name );
187 void resetTree();
188 void emitChanged();
189
190 signals:
191
192 void scopeChanged();
193
194 protected:
195 void keyPressEvent( QKeyEvent *event ) override;
196 void mousePressEvent( QMouseEvent *event ) override;
197 void drawRow( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
198 QColor rowColor( int index ) const;
199 void toggleContextExpanded( QTreeWidgetItem *item );
200 void editNext( const QModelIndex &index );
201
202 QModelIndex moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ) override;
203
204 QIcon mExpandIcon;
205
206 private:
207 VariableEditorDelegate *mEditorDelegate = nullptr;
208 int mEditableScopeIndex = -1;
209 QgsExpressionContext *mContext = nullptr;
210 QMap<QPair<int, QString>, QTreeWidgetItem *> mVariableToItem;
211 QMap<int, QTreeWidgetItem *> mScopeToItem;
212
213 void refreshScopeItems( QgsExpressionContextScope *scope, int scopeIndex );
214 void refreshScopeVariables( QgsExpressionContextScope *scope, int scopeIndex );
215};
216
217
218class VariableEditorDelegate : public QItemDelegate
219{
220 Q_OBJECT
221
222 public:
223 VariableEditorDelegate( QObject *parent = nullptr, QgsVariableEditorTree *tree = nullptr )
224 : QItemDelegate( parent )
225 , mParentTree( tree )
226 {}
227
228 QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
229 void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
230 QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
231 void setModelData( QWidget *widget, QAbstractItemModel *model, const QModelIndex &index ) const override;
232 void setEditorData( QWidget *, const QModelIndex & ) const override {}
233
234 private:
235 QgsVariableEditorTree *mParentTree = nullptr;
236};
237
239
240#endif
241
242#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...
void scopeChanged()
Emitted when the user has modified a scope using the widget.
QgsExpressionContext * context() const
Returns the current expression context for the widget.
void showEvent(QShowEvent *event) override
QgsVariableEditorWidget(QWidget *parent=nullptr)
Constructor for QgsVariableEditorWidget.
QString settingGroup() const
Returns the setting group for the widget.
void setContext(QgsExpressionContext *context)
Overwrites the QgsExpressionContext for the widget.
void setSettingGroup(const QString &group)
Sets the setting group for the widget.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53