QGIS API Documentation 3.41.0-Master (3440c17df1d)
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 "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
41class GUI_EXPORT QgsVariableEditorWidget : public QWidget
42{
43 Q_OBJECT
44
45 Q_PROPERTY( QString settingGroup READ settingGroup WRITE setSettingGroup )
46
47 public:
48
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
132 void showEvent( QShowEvent *event ) override;
133
134 private:
135
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
155#ifndef SIP_RUN
156
158
159/* QgsVariableEditorTree is NOT part of the public QGIS api. It's only
160 * public here as Qt meta objects can't be nested classes
161 */
162
163class QgsVariableEditorTree : public QTreeWidget
164{
165 Q_OBJECT
166
167 public:
168
169 enum VariableRoles
170 {
171 ContextIndex = Qt::UserRole,
172 RowBaseColor
173 };
174
175 explicit QgsVariableEditorTree( QWidget *parent = nullptr );
176
177 QTreeWidgetItem *indexToItem( const QModelIndex &index ) const { return itemFromIndex( index ); }
178 QModelIndex itemToIndex( QTreeWidgetItem *item ) const { return indexFromItem( item ); }
179 QString variableNameFromItem( QTreeWidgetItem *item ) const { return item ? item->text( 0 ) : QString(); }
180 QString variableNameFromIndex( const QModelIndex &index ) const { return variableNameFromItem( itemFromIndex( index ) ); }
181 QgsExpressionContextScope *scopeFromItem( QTreeWidgetItem *item ) const;
182 QTreeWidgetItem *itemFromVariable( QgsExpressionContextScope *scope, const QString &name ) const;
183 void setEditableScopeIndex( int scopeIndex ) { mEditableScopeIndex = scopeIndex; }
184 QgsExpressionContextScope *editableScope();
185 void setContext( QgsExpressionContext *context ) { mContext = context; }
186 void refreshTree();
187 void removeItem( QTreeWidgetItem *item );
188 void renameItem( QTreeWidgetItem *item, const QString &name );
189 void resetTree();
190 void emitChanged();
191
192 signals:
193
194 void scopeChanged();
195
196 protected:
197 void keyPressEvent( QKeyEvent *event ) override;
198 void mousePressEvent( QMouseEvent *event ) override;
199 void drawRow( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
200 QColor rowColor( int index ) const;
201 void toggleContextExpanded( QTreeWidgetItem *item );
202 void editNext( const QModelIndex &index );
203
204 QModelIndex moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ) override;
205
206 QIcon mExpandIcon;
207
208 private:
209
210 VariableEditorDelegate *mEditorDelegate = nullptr;
211 int mEditableScopeIndex = -1;
212 QgsExpressionContext *mContext = nullptr;
213 QMap< QPair<int, QString>, QTreeWidgetItem * > mVariableToItem;
214 QMap< int, QTreeWidgetItem * > mScopeToItem;
215
216 void refreshScopeItems( QgsExpressionContextScope *scope, int scopeIndex );
217 void refreshScopeVariables( QgsExpressionContextScope *scope, int scopeIndex );
218};
219
220
221class VariableEditorDelegate : public QItemDelegate
222{
223 Q_OBJECT
224
225 public:
226 VariableEditorDelegate( QObject *parent = nullptr, QgsVariableEditorTree *tree = nullptr )
227 : QItemDelegate( parent )
228 , mParentTree( tree )
229 {}
230
231 QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option,
232 const QModelIndex &index ) const override;
233 void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option,
234 const QModelIndex &index ) const override;
235 QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
236 void setModelData( QWidget *widget, QAbstractItemModel *model,
237 const QModelIndex &index ) const override;
238 void setEditorData( QWidget *, const QModelIndex & ) const override {}
239
240 private:
241 QgsVariableEditorTree *mParentTree = nullptr;
242};
243
245
246#endif
247
248#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