QGIS API Documentation  2.14.0-Essen
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 
72  void reloadContext();
73 
80  void setEditableScopeIndex( int scopeIndex );
81 
86  QgsExpressionContextScope* editableScope() const;
87 
94  void setSettingGroup( const QString &group ) { mSettingGroup = group; }
95 
102  QString settingGroup() const { return mSettingGroup; }
103 
108  QgsStringMap variablesInActiveScope() const;
109 
110  signals:
111 
114  void scopeChanged();
115 
116  protected:
117 
118  void showEvent( QShowEvent *event ) override;
119 
120  private:
121 
123  int mEditableScopeIndex;
124  QgsVariableEditorTree* mTreeWidget;
125  QPushButton* mAddButton;
126  QPushButton* mRemoveButton;
127  QString mSettingGroup;
128  bool mShown;
129 
130  QString saveKey() const;
131 
132  private slots:
133 
134  void on_mAddButton_clicked();
135  void on_mRemoveButton_clicked();
136  void selectionChanged();
137 
138 };
139 
140 
142 
143 /* QgsVariableEditorTree is NOT part of the public QGIS api. It's only
144  * public here as Qt meta objects can't be nested classes
145  */
146 
147 class QgsVariableEditorTree : public QTreeWidget
148 {
149  Q_OBJECT
150 
151  public:
152 
153  enum VaribleRoles
154  {
155  ContextIndex = Qt::UserRole,
156  RowBaseColor
157  };
158 
159  explicit QgsVariableEditorTree( QWidget *parent = nullptr );
160 
161  QTreeWidgetItem *indexToItem( const QModelIndex &index ) const { return itemFromIndex( index ); }
162  QModelIndex itemToIndex( QTreeWidgetItem* item ) const { return indexFromItem( item ); }
163  QString variableNameFromItem( QTreeWidgetItem* item ) const { return item ? item->text( 0 ) : QString(); }
164  QString variableNameFromIndex( const QModelIndex& index ) const { return variableNameFromItem( itemFromIndex( index ) ); }
165  QgsExpressionContextScope* scopeFromItem( QTreeWidgetItem* item ) const;
166  QTreeWidgetItem* itemFromVariable( QgsExpressionContextScope* scope, const QString& name ) const;
167  void setEditableScopeIndex( int scopeIndex ) { mEditableScopeIndex = scopeIndex; }
168  QgsExpressionContextScope* editableScope();
169  void setContext( QgsExpressionContext* context ) { mContext = context; }
170  void refreshTree();
171  void removeItem( QTreeWidgetItem* item );
172  void renameItem( QTreeWidgetItem *item, const QString &name );
173  void resetTree();
174  void emitChanged();
175 
176  signals:
177 
178  void scopeChanged();
179 
180  protected:
181  void keyPressEvent( QKeyEvent *event ) override;
182  void mousePressEvent( QMouseEvent *event ) override;
183  void drawRow( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
184  QColor rowColor( int index ) const;
185  void toggleContextExpanded( QTreeWidgetItem *item );
186  void editNext( const QModelIndex &index );
187 
188  QModelIndex moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ) override;
189 
190  static QIcon mExpandIcon;
191 
192  private:
193 
194  VariableEditorDelegate* mEditorDelegate;
195  int mEditableScopeIndex;
196  QgsExpressionContext* mContext;
197  QMap< QPair<int, QString>, QTreeWidgetItem* > mVariableToItem;
198  QMap< int, QTreeWidgetItem* > mScopeToItem;
199 
200  void refreshScopeItems( QgsExpressionContextScope* scope, int scopeIndex );
201  void refreshScopeVariables( QgsExpressionContextScope* scope, int scopeIndex );
202 };
203 
204 
205 class VariableEditorDelegate : public QItemDelegate
206 {
207  Q_OBJECT
208 
209  public:
210  VariableEditorDelegate( QObject *parent = nullptr, QgsVariableEditorTree *tree = nullptr )
211  : QItemDelegate( parent )
212  , mParentTree( tree )
213  {}
214 
215  QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option,
216  const QModelIndex &index ) const override;
217  void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option,
218  const QModelIndex &index ) const override;
219  QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
220  void setModelData( QWidget* widget, QAbstractItemModel* model,
221  const QModelIndex & index ) const override;
222  void setEditorData( QWidget *, const QModelIndex & ) const override {}
223 
224  private:
225  QgsVariableEditorTree *mParentTree;
226 };
227 
229 
230 #endif //QGSVARIABLEEDITORWIDGET_H
static unsigned index
QgsExpressionContext * context() const
Returns the current expression context for the widget.
A tree based widget for editing expression context scope variables.
virtual void showEvent(QShowEvent *event)
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 settingGroup() const
Returns the setting group for the widget.
QString text(int column) const
typedef KeyboardModifiers