QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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.h"
20 #include <QWidget>
21 #include <QTreeWidget>
22 #include <QItemDelegate>
23 
24 class QTableWidget;
26 class QPushButton;
28 class QgsVariableEditorTree;
29 class VariableEditorDelegate;
30 
39 class GUI_EXPORT QgsVariableEditorWidget : public QWidget
40 {
41  Q_OBJECT
42 
43  Q_PROPERTY( QString settingGroup READ settingGroup WRITE setSettingGroup )
44 
45  public:
46 
50  QgsVariableEditorWidget( QWidget *parent = nullptr );
51 
53 
60  void setContext( QgsExpressionContext* context );
61 
66  QgsExpressionContext* context() const { return mContext.data(); }
67 
74  void setEditableScopeIndex( int scopeIndex );
75 
80  QgsExpressionContextScope* editableScope() const;
81 
88  void setSettingGroup( const QString &group ) { mSettingGroup = group; }
89 
96  QString settingGroup() const { return mSettingGroup; }
97 
102  QgsStringMap variablesInActiveScope() const;
103 
104  public slots:
105 
110  void reloadContext();
111 
112  signals:
113 
116  void scopeChanged();
117 
118  protected:
119 
120  void showEvent( QShowEvent *event ) override;
121 
122  private:
123 
125  int mEditableScopeIndex;
126  QgsVariableEditorTree* mTreeWidget;
127  QPushButton* mAddButton;
128  QPushButton* mRemoveButton;
129  QString mSettingGroup;
130  bool mShown;
131 
132  QString saveKey() const;
133 
134  private slots:
135 
136  void on_mAddButton_clicked();
137  void on_mRemoveButton_clicked();
138  void selectionChanged();
139 
140 };
141 
142 
144 
145 /* QgsVariableEditorTree is NOT part of the public QGIS api. It's only
146  * public here as Qt meta objects can't be nested classes
147  */
148 
149 class QgsVariableEditorTree : public QTreeWidget
150 {
151  Q_OBJECT
152 
153  public:
154 
155  enum VaribleRoles
156  {
157  ContextIndex = Qt::UserRole,
158  RowBaseColor
159  };
160 
161  explicit QgsVariableEditorTree( QWidget *parent = nullptr );
162 
163  QTreeWidgetItem *indexToItem( const QModelIndex &index ) const { return itemFromIndex( index ); }
164  QModelIndex itemToIndex( QTreeWidgetItem* item ) const { return indexFromItem( item ); }
165  QString variableNameFromItem( QTreeWidgetItem* item ) const { return item ? item->text( 0 ) : QString(); }
166  QString variableNameFromIndex( const QModelIndex& index ) const { return variableNameFromItem( itemFromIndex( index ) ); }
167  QgsExpressionContextScope* scopeFromItem( QTreeWidgetItem* item ) const;
168  QTreeWidgetItem* itemFromVariable( QgsExpressionContextScope* scope, const QString& name ) const;
169  void setEditableScopeIndex( int scopeIndex ) { mEditableScopeIndex = scopeIndex; }
170  QgsExpressionContextScope* editableScope();
171  void setContext( QgsExpressionContext* context ) { mContext = context; }
172  void refreshTree();
173  void removeItem( QTreeWidgetItem* item );
174  void renameItem( QTreeWidgetItem *item, const QString &name );
175  void resetTree();
176  void emitChanged();
177 
178  signals:
179 
180  void scopeChanged();
181 
182  protected:
183  void keyPressEvent( QKeyEvent *event ) override;
184  void mousePressEvent( QMouseEvent *event ) override;
185  void drawRow( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
186  QColor rowColor( int index ) const;
187  void toggleContextExpanded( QTreeWidgetItem *item );
188  void editNext( const QModelIndex &index );
189 
190  QModelIndex moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ) override;
191 
192  QIcon mExpandIcon;
193 
194  private:
195 
196  VariableEditorDelegate* mEditorDelegate;
197  int mEditableScopeIndex;
198  QgsExpressionContext* mContext;
199  QMap< QPair<int, QString>, QTreeWidgetItem* > mVariableToItem;
200  QMap< int, QTreeWidgetItem* > mScopeToItem;
201 
202  void refreshScopeItems( QgsExpressionContextScope* scope, int scopeIndex );
203  void refreshScopeVariables( QgsExpressionContextScope* scope, int scopeIndex );
204 };
205 
206 
207 class VariableEditorDelegate : public QItemDelegate
208 {
209  Q_OBJECT
210 
211  public:
212  VariableEditorDelegate( QObject *parent = nullptr, QgsVariableEditorTree *tree = nullptr )
213  : QItemDelegate( parent )
214  , mParentTree( tree )
215  {}
216 
217  QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option,
218  const QModelIndex &index ) const override;
219  void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option,
220  const QModelIndex &index ) const override;
221  QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
222  void setModelData( QWidget* widget, QAbstractItemModel* model,
223  const QModelIndex & index ) const override;
224  void setEditorData( QWidget *, const QModelIndex & ) const override {}
225 
226  private:
227  QgsVariableEditorTree *mParentTree;
228 };
229 
231 
232 #endif //QGSVARIABLEEDITORWIDGET_H
QgsExpressionContext * context() const
Returns the current expression context for the widget.
static unsigned index
A tree based widget for editing expression context scope variables.
virtual void showEvent(QShowEvent *event)
QString settingGroup() const
Returns the setting group for the widget.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setSettingGroup(const QString &group)
Sets the setting group for the widget.
QString text(int column) const
typedef KeyboardModifiers