QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgsprocessingaggregatewidgets.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingaggregatewidgets.h
3  ---------------------
4  Date : June 2020
5  Copyright : (C) 2020 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 QGSPROCESSINGAGGREGATEWIDGETS_H
17 #define QGSPROCESSINGAGGREGATEWIDGETS_H
18 
19 #include <QAbstractTableModel>
20 #include <QStyledItemDelegate>
21 #include <QPointer>
22 
23 #include "qgsfields.h"
25 #include "qgsfieldmappingmodel.h"
26 #include "qgspanelwidget.h"
27 
28 class QLineEdit;
29 class QToolButton;
30 class QItemSelectionModel;
31 class QTableView;
32 
33 
41 class GUI_EXPORT QgsAggregateMappingModel: public QAbstractTableModel
42 {
43 
44  Q_OBJECT
45 
46  public:
47 
51  enum class ColumnDataIndex : int
52  {
53  SourceExpression,
54  Aggregate,
55  Delimiter,
56  DestinationName,
57  DestinationType,
58  DestinationLength,
59  DestinationPrecision,
60  };
61 
63 
67  struct Aggregate
68  {
70  QString source;
71 
73  QString aggregate;
74 
76  QString delimiter;
77 
80 
81  };
82 
87  QgsAggregateMappingModel( const QgsFields &sourceFields = QgsFields(),
88  QObject *parent = nullptr );
89 
91  QgsFields sourceFields() const;
92 
94  QList<QgsAggregateMappingModel::Aggregate> mapping() const;
95 
99  void setMapping( const QList<QgsAggregateMappingModel::Aggregate> &mapping );
100 
102  void appendField( const QgsField &field, const QString &source = QString(), const QString &aggregate = QString() );
103 
105  bool removeField( const QModelIndex &index );
106 
108  bool moveUp( const QModelIndex &index );
109 
111  bool moveDown( const QModelIndex &index );
112 
114  void setSourceFields( const QgsFields &sourceFields );
115 
117  QgsExpressionContextGenerator *contextGenerator() const;
118 
123  void setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
124 
125  // QAbstractItemModel interface
126  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
127  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
128  QVariant data( const QModelIndex &index, int role ) const override;
129  QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
130  Qt::ItemFlags flags( const QModelIndex &index ) const override;
131  bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
132 
133  private:
134 
135  bool moveUpOrDown( const QModelIndex &index, bool up = true );
136 
137  QList<Aggregate> mMapping;
138  QgsFields mSourceFields;
139  std::unique_ptr< QgsFieldMappingModel::ExpressionContextGenerator> mExpressionContextGenerator;
140 
141 };
142 
149 class GUI_EXPORT QgsAggregateMappingWidget : public QgsPanelWidget
150 {
151  Q_OBJECT
152 
153  public:
154 
159  explicit QgsAggregateMappingWidget( QWidget *parent = nullptr,
160  const QgsFields &sourceFields = QgsFields() );
161 
163  QgsAggregateMappingModel *model() const;
164 
166  QList<QgsAggregateMappingModel::Aggregate> mapping() const;
167 
171  void setMapping( const QList<QgsAggregateMappingModel::Aggregate> &mapping );
172 
174  QItemSelectionModel *selectionModel();
175 
177  void setSourceFields( const QgsFields &sourceFields );
178 
184  void setSourceLayer( QgsVectorLayer *layer );
185 
193  QgsVectorLayer *sourceLayer();
194 
198  void scrollTo( const QModelIndex &index ) const;
199 
204  void registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
205 
206  signals:
207 
211  void changed();
212 
213  public slots:
214 
216  void appendField( const QgsField &field, const QString &source = QString(), const QString &aggregate = QString() );
217 
219  bool removeSelectedFields( );
220 
222  bool moveSelectedFieldsUp( );
223 
225  bool moveSelectedFieldsDown( );
226 
227  private:
228 
229  QTableView *mTableView = nullptr;
230  QAbstractTableModel *mModel = nullptr;
231  QPointer< QgsVectorLayer > mSourceLayer;
232  void updateColumns();
234  std::list<int> selectedRows( );
235 
236 
237  class AggregateDelegate: public QStyledItemDelegate
238  {
239 
240  public:
241 
242  AggregateDelegate( QObject *parent = nullptr );
243 
244  // QAbstractItemDelegate interface
245  QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
246  void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
247  void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
248 
249  private:
251  static const QStringList aggregates();
252  };
253 
254 };
255 
256 
257 #endif // QGSPROCESSINGAGGREGATEWIDGETS_H
The QgsAggregateMappingModel holds mapping information for defining sets of aggregates of fields from...
Q_ENUM(ColumnDataIndex)
ColumnDataIndex
The ColumnDataIndex enum represents the column index for the view.
The QgsAggregateMappingWidget class creates a mapping for defining sets of aggregates of fields from ...
void changed()
Emitted when the aggregates defined in the widget are changed.
Abstract interface for generating an expression context.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
Container of fields for a vector layer.
Definition: qgsfields.h:45
Base class for any widget that can be shown as a inline panel.
Represents a vector layer which manages a vector based data sets.
const QgsField & field
Definition: qgsfield.h:472
The Aggregate struct holds information about an aggregate column.
QString source
The source expression used as the input for the aggregate calculation.
QgsField field
The field in its current status (it might have been renamed)