QGIS API Documentation 3.43.0-Master (9e873c7bc91)
Loading...
Searching...
No Matches
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"
26#include "qgspanelwidget.h"
27
28class QLineEdit;
29class QToolButton;
30class QItemSelectionModel;
31class QTableView;
32
33
41class GUI_EXPORT QgsAggregateMappingModel : public QAbstractTableModel
42{
43 Q_OBJECT
44
45 public:
49 enum class ColumnDataIndex : int
50 {
51 SourceExpression,
52 Aggregate,
53 Delimiter,
54 DestinationName,
55 DestinationType,
56 DestinationLength,
57 DestinationPrecision,
58 };
59
61
65 struct Aggregate
66 {
68 QString source;
69
71 QString aggregate;
72
74 QString delimiter;
75
78 };
79
84 QgsAggregateMappingModel( const QgsFields &sourceFields = QgsFields(), QObject *parent = nullptr );
85
87 QgsFields sourceFields() const;
88
90 QList<QgsAggregateMappingModel::Aggregate> mapping() const;
91
95 void setMapping( const QList<QgsAggregateMappingModel::Aggregate> &mapping );
96
98 void appendField( const QgsField &field, const QString &source = QString(), const QString &aggregate = QString() );
99
101 bool removeField( const QModelIndex &index );
102
104 bool moveUp( const QModelIndex &index );
105
107 bool moveDown( const QModelIndex &index );
108
110 void setSourceFields( const QgsFields &sourceFields );
111
113 QgsExpressionContextGenerator *contextGenerator() const;
114
119 void setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
120
121 // QAbstractItemModel interface
122 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
123 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
124 QVariant data( const QModelIndex &index, int role ) const override;
125 QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
126 Qt::ItemFlags flags( const QModelIndex &index ) const override;
127 bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
128
129 private:
130 bool moveUpOrDown( const QModelIndex &index, bool up = true );
131 static QString qgsFieldToTypeName( const QgsField &field );
132 static void setFieldTypeFromName( QgsField &field, const QString &name );
133
134 QList<Aggregate> mMapping;
135 QgsFields mSourceFields;
136 std::unique_ptr<QgsFieldMappingModel::ExpressionContextGenerator> mExpressionContextGenerator;
137};
138
146{
147 Q_OBJECT
148
149 public:
154 explicit QgsAggregateMappingWidget( QWidget *parent = nullptr, const QgsFields &sourceFields = QgsFields() );
155
157 QgsAggregateMappingModel *model() const;
158
160 QList<QgsAggregateMappingModel::Aggregate> mapping() const;
161
165 void setMapping( const QList<QgsAggregateMappingModel::Aggregate> &mapping );
166
168 QItemSelectionModel *selectionModel();
169
171 void setSourceFields( const QgsFields &sourceFields );
172
178 void setSourceLayer( QgsVectorLayer *layer );
179
187 QgsVectorLayer *sourceLayer();
188
192 void scrollTo( const QModelIndex &index ) const;
193
198 void registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
199
200 signals:
201
205 void changed();
206
207 public slots:
208
210 void appendField( const QgsField &field, const QString &source = QString(), const QString &aggregate = QString() );
211
213 bool removeSelectedFields();
214
216 bool moveSelectedFieldsUp();
217
219 bool moveSelectedFieldsDown();
220
221 private:
222 QTableView *mTableView = nullptr;
223 QAbstractTableModel *mModel = nullptr;
224 QPointer<QgsVectorLayer> mSourceLayer;
225 void updateColumns();
227 std::list<int> selectedRows();
228};
229
231
232#ifndef SIP_RUN
233
234class QgsAggregateMappingDelegate : public QStyledItemDelegate
235{
236 Q_OBJECT
237
238 public:
239 QgsAggregateMappingDelegate( QObject *parent = nullptr );
240
241 // QAbstractItemDelegate interface
242 QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
243 void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
244 void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
245
246 private:
248 static const QStringList aggregates();
249};
250
251#endif
252
254
255#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:53
Container of fields for a vector layer.
Definition qgsfields.h:46
Base class for any widget that can be shown as a inline panel.
Represents a vector layer which manages a vector based data sets.
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)