QGIS API Documentation 3.41.0-Master (d5b93354e9c)
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:
52 QgsVariableEditorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
53
54 ~QgsVariableEditorWidget() override;
55
63 void setContext( QgsExpressionContext *context );
64
70 QgsExpressionContext *context() const { return mContext.get(); }
71
79 void setEditableScopeIndex( int scopeIndex );
80
86 QgsExpressionContextScope *editableScope() const;
87
95 void setSettingGroup( const QString &group ) { mSettingGroup = group; }
96
104 QString settingGroup() const { return mSettingGroup; }
105
111 QVariantMap variablesInActiveScope() const;
112
113 public slots:
114
120 void reloadContext();
121
122 signals:
123
128
129 protected:
130 void showEvent( QShowEvent *event ) override;
131
132 private:
133 std::unique_ptr<QgsExpressionContext> mContext;
134 int mEditableScopeIndex = -1;
135 QgsVariableEditorTree *mTreeWidget = nullptr;
136 QPushButton *mAddButton = nullptr;
137 QPushButton *mRemoveButton = nullptr;
138 QString mSettingGroup;
139 bool mShown = false;
140
141 QString saveKey() const;
142
143 private slots:
144
145 void mAddButton_clicked();
146 void mRemoveButton_clicked();
147 void selectionChanged();
148};
149
150
151#ifndef SIP_RUN
152
154
155/* QgsVariableEditorTree is NOT part of the public QGIS api. It's only
156 * public here as Qt meta objects can't be nested classes
157 */
158
159class QgsVariableEditorTree : public QTreeWidget
160{
161 Q_OBJECT
162
163 public:
164 enum VariableRoles
165 {
166 ContextIndex = Qt::UserRole,
167 RowBaseColor
168 };
169
170 explicit QgsVariableEditorTree( QWidget *parent = nullptr );
171
172 QTreeWidgetItem *indexToItem( const QModelIndex &index ) const { return itemFromIndex( index ); }
173 QModelIndex itemToIndex( QTreeWidgetItem *item ) const { return indexFromItem( item ); }
174 QString variableNameFromItem( QTreeWidgetItem *item ) const { return item ? item->text( 0 ) : QString(); }
175 QString variableNameFromIndex( const QModelIndex &index ) const { return variableNameFromItem( itemFromIndex( index ) ); }
176 QgsExpressionContextScope *scopeFromItem( QTreeWidgetItem *item ) const;
177 QTreeWidgetItem *itemFromVariable( QgsExpressionContextScope *scope, const QString &name ) const;
178 void setEditableScopeIndex( int scopeIndex ) { mEditableScopeIndex = scopeIndex; }
179 QgsExpressionContextScope *editableScope();
180 void setContext( QgsExpressionContext *context ) { mContext = context; }
181 void refreshTree();
182 void removeItem( QTreeWidgetItem *item );
183 void renameItem( QTreeWidgetItem *item, const QString &name );
184 void resetTree();
185 void emitChanged();
186
187 signals:
188
189 void scopeChanged();
190
191 protected:
192 void keyPressEvent( QKeyEvent *event ) override;
193 void mousePressEvent( QMouseEvent *event ) override;
194 void drawRow( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
195 QColor rowColor( int index ) const;
196 void toggleContextExpanded( QTreeWidgetItem *item );
197 void editNext( const QModelIndex &index );
198
199 QModelIndex moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ) override;
200
201 QIcon mExpandIcon;
202
203 private:
204 VariableEditorDelegate *mEditorDelegate = nullptr;
205 int mEditableScopeIndex = -1;
206 QgsExpressionContext *mContext = nullptr;
207 QMap<QPair<int, QString>, QTreeWidgetItem *> mVariableToItem;
208 QMap<int, QTreeWidgetItem *> mScopeToItem;
209
210 void refreshScopeItems( QgsExpressionContextScope *scope, int scopeIndex );
211 void refreshScopeVariables( QgsExpressionContextScope *scope, int scopeIndex );
212};
213
214
215class VariableEditorDelegate : public QItemDelegate
216{
217 Q_OBJECT
218
219 public:
220 VariableEditorDelegate( QObject *parent = nullptr, QgsVariableEditorTree *tree = nullptr )
221 : QItemDelegate( parent )
222 , mParentTree( tree )
223 {}
224
225 QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
226 void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
227 QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
228 void setModelData( QWidget *widget, QAbstractItemModel *model, const QModelIndex &index ) const override;
229 void setEditorData( QWidget *, const QModelIndex & ) const override {}
230
231 private:
232 QgsVariableEditorTree *mParentTree = nullptr;
233};
234
236
237#endif
238
239#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