QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
21#include "qgsfields.h"
22#include "qgspanelwidget.h"
23
24#include <QAbstractTableModel>
25#include <QPointer>
26#include <QStyledItemDelegate>
27
28class QLineEdit;
29class QToolButton;
30class QItemSelectionModel;
31class QTableView;
32
33
41class GUI_EXPORT QgsAggregateMappingModel : public QAbstractTableModel
42{
43 Q_OBJECT
44
45 public:
46
50 enum class ColumnDataIndex : int
51 {
52 SourceExpression,
54 Delimiter,
55 DestinationName,
56 DestinationType,
57 DestinationLength,
58 DestinationPrecision,
59 };
60
62
66 struct Aggregate
67 {
69 QString source;
70
72 QString aggregate;
73
75 QString delimiter;
76
79 };
80
85 QgsAggregateMappingModel( const QgsFields &sourceFields = QgsFields(), QObject *parent = nullptr );
86
88 QgsFields sourceFields() const;
89
91 QList<QgsAggregateMappingModel::Aggregate> mapping() const;
92
96 void setMapping( const QList<QgsAggregateMappingModel::Aggregate> &mapping );
97
99 void appendField( const QgsField &field, const QString &source = QString(), const QString &aggregate = QString() );
100
102 bool removeField( const QModelIndex &index );
103
105 bool moveUp( const QModelIndex &index );
106
108 bool moveDown( const QModelIndex &index );
109
112
115
121
122 // QAbstractItemModel interface
123 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
124 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
125 QVariant data( const QModelIndex &index, int role ) const override;
126 QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
127 Qt::ItemFlags flags( const QModelIndex &index ) const override;
128 bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
129
130 private:
131 bool moveUpOrDown( const QModelIndex &index, bool up = true );
132 static QString qgsFieldToTypeName( const QgsField &field );
133 static void setFieldTypeFromName( QgsField &field, const QString &name );
134
135 QList<Aggregate> mMapping;
136 QgsFields mSourceFields;
137 std::unique_ptr<QgsFieldMappingModel::ExpressionContextGenerator> mExpressionContextGenerator;
138};
139
147{
148 Q_OBJECT
149
150 public:
151
156 explicit QgsAggregateMappingWidget( QWidget *parent = nullptr, const QgsFields &sourceFields = QgsFields() );
157
160
162 QList<QgsAggregateMappingModel::Aggregate> mapping() const;
163
167 void setMapping( const QList<QgsAggregateMappingModel::Aggregate> &mapping );
168
170 QItemSelectionModel *selectionModel();
171
173 void setSourceFields( const QgsFields &sourceFields );
174
180 void setSourceLayer( QgsVectorLayer *layer );
181
190
194 void scrollTo( const QModelIndex &index ) const;
195
201
202 signals:
203
207 void changed();
208
209 public slots:
210
212 void appendField( const QgsField &field, const QString &source = QString(), const QString &aggregate = QString() );
213
216
219
222
223 private:
224 QTableView *mTableView = nullptr;
225 QAbstractTableModel *mModel = nullptr;
226 QPointer<QgsVectorLayer> mSourceLayer;
227 void updateColumns();
229 std::list<int> selectedRows();
230};
231
233
234#ifndef SIP_RUN
235
236class QgsAggregateMappingDelegate : public QStyledItemDelegate
237{
238 Q_OBJECT
239
240 public:
241 QgsAggregateMappingDelegate( QObject *parent = nullptr );
242
243 // QAbstractItemDelegate interface
244 QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
245 void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
246 void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
247
248 private:
250 static const QStringList aggregates();
251};
252
253#endif
254
256
257#endif // QGSPROCESSINGAGGREGATEWIDGETS_H
Holds mapping information for defining sets of aggregates of fields from a QgsFields object.
QgsFields sourceFields() const
Returns a list of source fields.
Q_ENUM(ColumnDataIndex)
void appendField(const QgsField &field, const QString &source=QString(), const QString &aggregate=QString())
Appends a new field to the model, with an optional source and aggregate.
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
ColumnDataIndex
The ColumnDataIndex enum represents the column index for the view.
Qt::ItemFlags flags(const QModelIndex &index) const override
bool removeField(const QModelIndex &index)
Removes the field at index from the model, returns true on success.
bool moveUp(const QModelIndex &index)
Moves down the field at index.
QVariant data(const QModelIndex &index, int role) const override
QList< QgsAggregateMappingModel::Aggregate > mapping() const
Returns a list of Aggregate objects representing the current status of the model.
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QgsExpressionContextGenerator * contextGenerator() const
Returns the context generator with the source fields.
bool moveDown(const QModelIndex &index)
Moves up the field at index.
void setSourceFields(const QgsFields &sourceFields)
Set source fields to sourceFields.
void setMapping(const QList< QgsAggregateMappingModel::Aggregate > &mapping)
Sets the mapping to show in the model.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setBaseExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Sets the base expression context generator, which will generate the expression contexts for expressio...
QgsAggregateMappingModel(const QgsFields &sourceFields=QgsFields(), QObject *parent=nullptr)
Constructs a QgsAggregateMappingModel from a set of sourceFields.
QList< QgsAggregateMappingModel::Aggregate > mapping() const
Returns a list of Aggregate objects representing the current status of the underlying mapping model.
void changed()
Emitted when the aggregates defined in the widget are changed.
void setMapping(const QList< QgsAggregateMappingModel::Aggregate > &mapping)
Sets the mapping to show in the model.
void appendField(const QgsField &field, const QString &source=QString(), const QString &aggregate=QString())
Appends a new field to the model, with an optional source and aggregate.
QgsVectorLayer * sourceLayer()
Returns the source layer for use when generating expression previews.
QgsAggregateMappingModel * model() const
Returns the underlying mapping model.
void setSourceFields(const QgsFields &sourceFields)
Set source fields of the underlying mapping model to sourceFields.
bool moveSelectedFieldsDown()
Moves down currently selected field.
QgsAggregateMappingWidget(QWidget *parent=nullptr, const QgsFields &sourceFields=QgsFields())
Constructs a QgsAggregateMappingWidget from a set of sourceFields.
void registerExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
QItemSelectionModel * selectionModel()
Returns the selection model.
bool moveSelectedFieldsUp()
Moves up currently selected field.
void scrollTo(const QModelIndex &index) const
Scroll the fields view to index.
bool removeSelectedFields()
Removes the currently selected field from the model.
void setSourceLayer(QgsVectorLayer *layer)
Sets a source layer to use when generating expression previews in the widget.
Abstract interface for generating an expression context.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
Container of fields for a vector layer.
Definition qgsfields.h:46
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
Represents a vector layer which manages a vector based dataset.
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).