QGIS API Documentation  2.12.0-Lyon
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;
25 class QgsVariableEditorTree;
26 class VariableEditorDelegate;
28 class QPushButton;
30 
31 
40 class GUI_EXPORT QgsVariableEditorWidget : public QWidget
41 {
42  Q_OBJECT
43 
44  Q_PROPERTY( QString settingGroup READ settingGroup WRITE setSettingGroup )
45 
46  public:
47 
51  QgsVariableEditorWidget( QWidget *parent = 0 );
52 
54 
61  void setContext( QgsExpressionContext* context );
62 
67  QgsExpressionContext* context() const { return mContext.data(); }
68 
73  void reloadContext();
74 
81  void setEditableScopeIndex( int scopeIndex );
82 
87  QgsExpressionContextScope* editableScope() const;
88 
95  void setSettingGroup( const QString &group ) { mSettingGroup = group; }
96 
103  QString settingGroup() const { return mSettingGroup; }
104 
109  QgsStringMap variablesInActiveScope() const;
110 
111  signals:
112 
115  void scopeChanged();
116 
117  protected:
118 
119  void showEvent( QShowEvent *event ) override;
120 
121  private:
122 
124  int mEditableScopeIndex;
125  QgsVariableEditorTree* mTreeWidget;
126  QPushButton* mAddButton;
127  QPushButton* mRemoveButton;
128  QString mSettingGroup;
129  bool mShown;
130 
131  QString saveKey() const;
132 
133  private slots:
134 
135  void on_mAddButton_clicked();
136  void on_mRemoveButton_clicked();
137  void selectionChanged();
138 
139 };
140 
141 
143 
144 /* QgsVariableEditorTree is NOT part of the public QGIS api. It's only
145  * public here as Qt meta objects can't be nested classes
146  */
147 
148 class QgsVariableEditorTree : public QTreeWidget
149 {
150  Q_OBJECT
151 
152  public:
153 
154  enum VaribleRoles
155  {
156  ContextIndex = Qt::UserRole,
157  RowBaseColor
158  };
159 
160  explicit QgsVariableEditorTree( QWidget *parent = 0 );
161 
162  QTreeWidgetItem *indexToItem( const QModelIndex &index ) const { return itemFromIndex( index ); }
163  QModelIndex itemToIndex( QTreeWidgetItem* item ) const { return indexFromItem( item ); }
164  QString variableNameFromItem( QTreeWidgetItem* item ) const { return item ? item->text( 0 ) : QString(); }
165  QString variableNameFromIndex( const QModelIndex& index ) const { return variableNameFromItem( itemFromIndex( index ) ); }
166  QgsExpressionContextScope* scopeFromItem( QTreeWidgetItem* item ) const;
167  QTreeWidgetItem* itemFromVariable( QgsExpressionContextScope* scope, const QString& name ) const;
168  void setEditableScopeIndex( int scopeIndex ) { mEditableScopeIndex = scopeIndex; }
169  QgsExpressionContextScope* editableScope();
170  void setContext( QgsExpressionContext* context ) { mContext = context; }
171  void refreshTree();
172  void removeItem( QTreeWidgetItem* item );
173  void renameItem( QTreeWidgetItem *item, const QString &name );
174  void resetTree();
175  void emitChanged();
176 
177  signals:
178 
179  void scopeChanged();
180 
181  protected:
182  void keyPressEvent( QKeyEvent *event ) override;
183  void mousePressEvent( QMouseEvent *event ) override;
184  void drawRow( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
185  QColor rowColor( int index ) const;
186  void toggleContextExpanded( QTreeWidgetItem *item );
187  void editNext( const QModelIndex &index );
188 
189  QModelIndex moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ) override;
190 
191  static QIcon mExpandIcon;
192 
193  private:
194 
195  VariableEditorDelegate* mEditorDelegate;
196  int mEditableScopeIndex;
197  QgsExpressionContext* mContext;
198  QMap< QPair<int, QString>, QTreeWidgetItem* > mVariableToItem;
199  QMap< int, QTreeWidgetItem* > mScopeToItem;
200 
201  void refreshScopeItems( QgsExpressionContextScope* scope, int scopeIndex );
202  void refreshScopeVariables( QgsExpressionContextScope* scope, int scopeIndex );
203 };
204 
206 
207 #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 QModelIndex moveCursor(CursorAction cursorAction, QFlags< Qt::KeyboardModifier > modifiers)
QModelIndex indexFromItem(QTreeWidgetItem *item, int column) const
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.
QTreeWidgetItem * itemFromIndex(const QModelIndex &index) const
void setSettingGroup(const QString &group)
Sets the setting group for the widget.
virtual void mousePressEvent(QMouseEvent *event)
virtual void keyPressEvent(QKeyEvent *event)
QString settingGroup() const
Returns the setting group for the widget.
QString text(int column) const
virtual void drawRow(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
typedef KeyboardModifiers