QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
22 #include "qgsfields.h"
24 #include "qgsfieldmappingmodel.h"
25 #include "qgspanelwidget.h"
26 
27 class QLineEdit;
28 class QToolButton;
29 class QItemSelectionModel;
30 class QTableView;
31 
32 
40 class GUI_EXPORT QgsAggregateMappingModel: public QAbstractTableModel
41 {
42 
43  Q_OBJECT
44 
45  public:
46 
50  enum class ColumnDataIndex : int
51  {
52  SourceExpression,
53  Aggregate,
54  Delimiter,
55  DestinationName,
56  DestinationType,
57  DestinationLength,
58  DestinationPrecision,
59  };
60 
61  Q_ENUM( ColumnDataIndex );
62 
66  struct Aggregate
67  {
69  QString source;
70 
72  QString aggregate;
73 
75  QString delimiter;
76 
79 
80  };
81 
86  QgsAggregateMappingModel( const QgsFields &sourceFields = QgsFields(),
87  QObject *parent = nullptr );
88 
90  QgsFields sourceFields() const;
91 
93  QList<QgsAggregateMappingModel::Aggregate> mapping() const;
94 
98  void setMapping( const QList<QgsAggregateMappingModel::Aggregate> &mapping );
99 
101  void appendField( const QgsField &field, const QString &source = QString(), const QString &aggregate = QString() );
102 
104  bool removeField( const QModelIndex &index );
105 
107  bool moveUp( const QModelIndex &index );
108 
110  bool moveDown( const QModelIndex &index );
111 
113  void setSourceFields( const QgsFields &sourceFields );
114 
116  QgsExpressionContextGenerator *contextGenerator() const;
117 
122  void setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
123 
124  // QAbstractItemModel interface
125  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
126  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
127  QVariant data( const QModelIndex &index, int role ) const override;
128  QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
129  Qt::ItemFlags flags( const QModelIndex &index ) const override;
130  bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
131 
132  private:
133 
134  bool moveUpOrDown( const QModelIndex &index, bool up = true );
135 
136  QList<Aggregate> mMapping;
137  QgsFields mSourceFields;
138  std::unique_ptr< QgsFieldMappingModel::ExpressionContextGenerator> mExpressionContextGenerator;
139 
140 };
141 
148 class GUI_EXPORT QgsAggregateMappingWidget : public QgsPanelWidget
149 {
150  Q_OBJECT
151 
152  public:
153 
158  explicit QgsAggregateMappingWidget( QWidget *parent = nullptr,
159  const QgsFields &sourceFields = QgsFields() );
160 
162  QgsAggregateMappingModel *model() const;
163 
165  QList<QgsAggregateMappingModel::Aggregate> mapping() const;
166 
170  void setMapping( const QList<QgsAggregateMappingModel::Aggregate> &mapping );
171 
173  QItemSelectionModel *selectionModel();
174 
176  void setSourceFields( const QgsFields &sourceFields );
177 
181  void scrollTo( const QModelIndex &index ) const;
182 
187  void registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator );
188 
189  signals:
190 
194  void changed();
195 
196  public slots:
197 
199  void appendField( const QgsField &field, const QString &source = QString(), const QString &aggregate = QString() );
200 
202  bool removeSelectedFields( );
203 
205  bool moveSelectedFieldsUp( );
206 
208  bool moveSelectedFieldsDown( );
209 
210  private:
211 
212  QTableView *mTableView = nullptr;
213  QAbstractTableModel *mModel = nullptr;
214  void updateColumns();
216  std::list<int> selectedRows( );
217 
218 
219  class AggregateDelegate: public QStyledItemDelegate
220  {
221 
222  public:
223 
224  AggregateDelegate( QObject *parent = nullptr );
225 
226  // QAbstractItemDelegate interface
227  QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
228  void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
229  void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
230 
231  private:
233  static const QStringList aggregates();
234  };
235 
236 };
237 
238 
239 #endif // QGSPROCESSINGAGGREGATEWIDGETS_H
qgsfields.h
QgsFields
Definition: qgsfields.h:44
QgsAggregateMappingWidget
Definition: qgsprocessingaggregatewidgets.h:148
QgsAggregateMappingModel::ColumnDataIndex
ColumnDataIndex
The ColumnDataIndex enum represents the column index for the view.
Definition: qgsprocessingaggregatewidgets.h:50
QgsPanelWidget
Base class for any widget that can be shown as a inline panel.
Definition: qgspanelwidget.h:29
QgsAggregateMappingModel::Aggregate::aggregate
QString aggregate
Aggregate name.
Definition: qgsprocessingaggregatewidgets.h:72
qgsexpressioncontextgenerator.h
QgsAggregateMappingModel
Definition: qgsprocessingaggregatewidgets.h:40
QgsAggregateMappingModel::Aggregate::field
QgsField field
The field in its current status (it might have been renamed)
Definition: qgsprocessingaggregatewidgets.h:78
qgspanelwidget.h
QgsExpressionContextGenerator
Definition: qgsexpressioncontextgenerator.h:36
QgsAggregateMappingModel::Aggregate::delimiter
QString delimiter
Delimiter string.
Definition: qgsprocessingaggregatewidgets.h:75
qgsfieldmappingmodel.h
QgsAggregateMappingModel::Aggregate::source
QString source
The source expression used as the input for the aggregate calculation.
Definition: qgsprocessingaggregatewidgets.h:69
QgsAggregateMappingModel::Aggregate
The Aggregate struct holds information about an aggregate column.
Definition: qgsprocessingaggregatewidgets.h:66
QgsField
Definition: qgsfield.h:49