QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
26 class QTableWidget;
28 class QPushButton;
30 class QgsVariableEditorTree;
31 class VariableEditorDelegate;
32 
42 class GUI_EXPORT QgsVariableEditorWidget : public QWidget
43 {
44  Q_OBJECT
45 
46  Q_PROPERTY( QString settingGroup READ settingGroup WRITE setSettingGroup )
47 
48  public:
49 
54  QgsVariableEditorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
55 
56  ~QgsVariableEditorWidget() override;
57 
65  void setContext( QgsExpressionContext *context );
66 
72  QgsExpressionContext *context() const { return mContext.get(); }
73 
81  void setEditableScopeIndex( int scopeIndex );
82 
88  QgsExpressionContextScope *editableScope() const;
89 
97  void setSettingGroup( const QString &group ) { mSettingGroup = group; }
98 
106  QString settingGroup() const { return mSettingGroup; }
107 
113  QVariantMap variablesInActiveScope() const;
114 
115  public slots:
116 
122  void reloadContext();
123 
124  signals:
125 
129  void scopeChanged();
130 
131  protected:
132 
133  void showEvent( QShowEvent *event ) override;
134 
135  private:
136 
137  std::unique_ptr<QgsExpressionContext> mContext;
138  int mEditableScopeIndex = -1;
139  QgsVariableEditorTree *mTreeWidget = nullptr;
140  QPushButton *mAddButton = nullptr;
141  QPushButton *mRemoveButton = nullptr;
142  QString mSettingGroup;
143  bool mShown = false;
144 
145  QString saveKey() const;
146 
147  private slots:
148 
149  void mAddButton_clicked();
150  void mRemoveButton_clicked();
151  void selectionChanged();
152 
153 };
154 
155 
156 #ifndef SIP_RUN
157 
159 
160 /* QgsVariableEditorTree is NOT part of the public QGIS api. It's only
161  * public here as Qt meta objects can't be nested classes
162  */
163 
164 class QgsVariableEditorTree : public QTreeWidget
165 {
166  Q_OBJECT
167 
168  public:
169 
170  enum VariableRoles
171  {
172  ContextIndex = Qt::UserRole,
173  RowBaseColor
174  };
175 
176  explicit QgsVariableEditorTree( QWidget *parent = nullptr );
177 
178  QTreeWidgetItem *indexToItem( const QModelIndex &index ) const { return itemFromIndex( index ); }
179  QModelIndex itemToIndex( QTreeWidgetItem *item ) const { return indexFromItem( item ); }
180  QString variableNameFromItem( QTreeWidgetItem *item ) const { return item ? item->text( 0 ) : QString(); }
181  QString variableNameFromIndex( const QModelIndex &index ) const { return variableNameFromItem( itemFromIndex( index ) ); }
182  QgsExpressionContextScope *scopeFromItem( QTreeWidgetItem *item ) const;
183  QTreeWidgetItem *itemFromVariable( QgsExpressionContextScope *scope, const QString &name ) const;
184  void setEditableScopeIndex( int scopeIndex ) { mEditableScopeIndex = scopeIndex; }
185  QgsExpressionContextScope *editableScope();
186  void setContext( QgsExpressionContext *context ) { mContext = context; }
187  void refreshTree();
188  void removeItem( QTreeWidgetItem *item );
189  void renameItem( QTreeWidgetItem *item, const QString &name );
190  void resetTree();
191  void emitChanged();
192 
193  signals:
194 
195  void scopeChanged();
196 
197  protected:
198  void keyPressEvent( QKeyEvent *event ) override;
199  void mousePressEvent( QMouseEvent *event ) override;
200  void drawRow( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
201  QColor rowColor( int index ) const;
202  void toggleContextExpanded( QTreeWidgetItem *item );
203  void editNext( const QModelIndex &index );
204 
205  QModelIndex moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ) override;
206 
207  QIcon mExpandIcon;
208 
209  private:
210 
211  VariableEditorDelegate *mEditorDelegate = nullptr;
212  int mEditableScopeIndex = -1;
213  QgsExpressionContext *mContext = nullptr;
214  QMap< QPair<int, QString>, QTreeWidgetItem * > mVariableToItem;
215  QMap< int, QTreeWidgetItem * > mScopeToItem;
216 
217  void refreshScopeItems( QgsExpressionContextScope *scope, int scopeIndex );
218  void refreshScopeVariables( QgsExpressionContextScope *scope, int scopeIndex );
219 };
220 
221 
222 class VariableEditorDelegate : public QItemDelegate
223 {
224  Q_OBJECT
225 
226  public:
227  VariableEditorDelegate( QObject *parent = nullptr, QgsVariableEditorTree *tree = nullptr )
228  : QItemDelegate( parent )
229  , mParentTree( tree )
230  {}
231 
232  QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option,
233  const QModelIndex &index ) const override;
234  void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option,
235  const QModelIndex &index ) const override;
236  QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
237  void setModelData( QWidget *widget, QAbstractItemModel *model,
238  const QModelIndex &index ) const override;
239  void setEditorData( QWidget *, const QModelIndex & ) const override {}
240 
241  private:
242  QgsVariableEditorTree *mParentTree = nullptr;
243 };
244 
246 
247 #endif
248 
249 #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