/
qgsprocessingfieldmapwidgetwrapper.cpp
411 lines (344 loc) · 14.5 KB
/
qgsprocessingfieldmapwidgetwrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/***************************************************************************
qgsprocessingfieldmapwidgetwrapper.cpp
---------------------
Date : June 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsprocessingfieldmapwidgetwrapper.h"
#include <QBoxLayout>
#include <QLineEdit>
#include <QMessageBox>
#include <QPushButton>
#include <QStandardItemModel>
#include <QToolButton>
#include "qgspanelwidget.h"
#include "qgsprocessingcontext.h"
#include "qgsprocessingmodelalgorithm.h"
#include "qgsprocessingparameterfieldmap.h"
/// @cond private
//
// QgsProcessingFieldMapPanelWidget
//
QgsProcessingFieldMapPanelWidget::QgsProcessingFieldMapPanelWidget( QWidget *parent )
: QgsPanelWidget( parent )
{
setupUi( this );
mModel = mFieldsView->model();
mFieldsView->setDestinationEditable( true );
mLayerCombo->setAllowEmptyLayer( true );
mLayerCombo->setFilters( QgsMapLayerProxyModel::VectorLayer );
connect( mResetButton, &QPushButton::clicked, this, &QgsProcessingFieldMapPanelWidget::loadFieldsFromLayer );
connect( mAddButton, &QPushButton::clicked, this, &QgsProcessingFieldMapPanelWidget::addField );
connect( mDeleteButton, &QPushButton::clicked, mFieldsView, &QgsFieldMappingWidget::removeSelectedFields );
connect( mUpButton, &QPushButton::clicked, mFieldsView, &QgsFieldMappingWidget::moveSelectedFieldsUp );
connect( mDownButton, &QPushButton::clicked, mFieldsView, &QgsFieldMappingWidget::moveSelectedFieldsDown );
connect( mLoadLayerFieldsButton, &QPushButton::clicked, this, &QgsProcessingFieldMapPanelWidget::loadLayerFields );
connect( mFieldsView, &QgsFieldMappingWidget::changed, this, [ = ]
{
if ( !mBlockChangedSignal )
{
emit changed();
}
} );
}
void QgsProcessingFieldMapPanelWidget::setLayer( QgsVectorLayer *layer )
{
if ( layer == mLayer )
return;
mLayer = layer;
mFieldsView->setSourceLayer( mLayer );
if ( mModel->rowCount() == 0 )
{
loadFieldsFromLayer();
return;
}
QMessageBox dlg( this );
dlg.setText( tr( "Do you want to reset the field mapping?" ) );
dlg.setStandardButtons(
QMessageBox::StandardButtons( QMessageBox::Yes |
QMessageBox::No ) );
dlg.setDefaultButton( QMessageBox::No );
if ( dlg.exec() == QMessageBox::Yes )
{
loadFieldsFromLayer();
}
}
QgsVectorLayer *QgsProcessingFieldMapPanelWidget::layer()
{
return mLayer;
}
QVariant QgsProcessingFieldMapPanelWidget::value() const
{
const QList<QgsFieldMappingModel::Field> mapping = mFieldsView->mapping();
QVariantList results;
results.reserve( mapping.size() );
for ( const QgsFieldMappingModel::Field &field : mapping )
{
QVariantMap def;
def.insert( QStringLiteral( "name" ), field.field.name() );
def.insert( QStringLiteral( "type" ), static_cast< int >( field.field.type() ) );
def.insert( QStringLiteral( "length" ), field.field.length() );
def.insert( QStringLiteral( "precision" ), field.field.precision() );
def.insert( QStringLiteral( "expression" ), field.expression );
results.append( def );
}
return results;
}
void QgsProcessingFieldMapPanelWidget::setValue( const QVariant &value )
{
if ( value.type() != QVariant::List )
return;
QgsFields destinationFields;
QMap<QString, QString> expressions;
const QgsFields layerFields = mLayer ? mLayer->fields() : QgsFields();
const QVariantList fields = value.toList();
for ( const QVariant &field : fields )
{
const QVariantMap map = field.toMap();
QgsField f( map.value( QStringLiteral( "name" ) ).toString(),
static_cast< QVariant::Type >( map.value( QStringLiteral( "type" ), QVariant::Invalid ).toInt() ),
QVariant::typeToName( static_cast< QVariant::Type >( map.value( QStringLiteral( "type" ), QVariant::Invalid ).toInt() ) ),
map.value( QStringLiteral( "length" ), 0 ).toInt(),
map.value( QStringLiteral( "precision" ), 0 ).toInt() );
if ( map.contains( QStringLiteral( "constraints" ) ) )
{
const QgsFieldConstraints::Constraints constraints = static_cast<QgsFieldConstraints::Constraints>( map.value( QStringLiteral( "constraints" ), 0 ).toInt() );
QgsFieldConstraints fieldConstraints;
if ( constraints & QgsFieldConstraints::ConstraintNotNull )
fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintNotNull );
if ( constraints & QgsFieldConstraints::ConstraintUnique )
fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintUnique );
if ( constraints & QgsFieldConstraints::ConstraintExpression )
fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintExpression );
f.setConstraints( fieldConstraints );
}
if ( !map.value( QStringLiteral( "expression" ) ).toString().isEmpty() )
{
expressions.insert( f.name(), map.value( QStringLiteral( "expression" ) ).toString() );
}
destinationFields.append( f );
}
mBlockChangedSignal = true;
if ( destinationFields.size() > 0 )
mFieldsView->setDestinationFields( destinationFields, expressions );
mBlockChangedSignal = false;
emit changed();
}
void QgsProcessingFieldMapPanelWidget::registerExpressionContextGenerator( const QgsExpressionContextGenerator *generator )
{
mFieldsView->registerExpressionContextGenerator( generator );
}
void QgsProcessingFieldMapPanelWidget::loadFieldsFromLayer()
{
if ( mLayer )
{
mFieldsView->setSourceFields( mLayer->fields() );
mFieldsView->setDestinationFields( mLayer->fields() );
}
}
void QgsProcessingFieldMapPanelWidget::addField()
{
const int rowCount = mModel->rowCount();
mModel->appendField( QgsField( QStringLiteral( "new_field" ) ) );
QModelIndex index = mModel->index( rowCount, 0 );
mFieldsView->selectionModel()->select(
index,
QItemSelectionModel::SelectionFlags(
QItemSelectionModel::Clear |
QItemSelectionModel::Select |
QItemSelectionModel::Current |
QItemSelectionModel::Rows ) );
mFieldsView->scrollTo( index );
}
void QgsProcessingFieldMapPanelWidget::loadLayerFields()
{
if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( mLayerCombo->currentLayer() ) )
{
mFieldsView->setSourceFields( vl->fields() );
mFieldsView->setDestinationFields( vl->fields() );
}
}
//
// QgsProcessingFieldMapParameterDefinitionWidget
//
QgsProcessingFieldMapParameterDefinitionWidget::QgsProcessingFieldMapParameterDefinitionWidget( QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition, const QgsProcessingAlgorithm *algorithm, QWidget *parent )
: QgsProcessingAbstractParameterDefinitionWidget( context, widgetContext, definition, algorithm, parent )
{
QVBoxLayout *vlayout = new QVBoxLayout();
vlayout->setContentsMargins( 0, 0, 0, 0 );
vlayout->addWidget( new QLabel( tr( "Parent layer" ) ) );
mParentLayerComboBox = new QComboBox();
mParentLayerComboBox->addItem( tr( "None" ), QVariant() );
QString initialParent;
if ( const QgsProcessingParameterFieldMapping *mapParam = dynamic_cast<const QgsProcessingParameterFieldMapping *>( definition ) )
initialParent = mapParam->parentLayerParameterName();
if ( auto *lModel = widgetContext.model() )
{
// populate combo box with other model input choices
const QMap<QString, QgsProcessingModelParameter> components = lModel->parameterComponents();
for ( auto it = components.constBegin(); it != components.constEnd(); ++it )
{
if ( const QgsProcessingParameterFeatureSource *definition = dynamic_cast< const QgsProcessingParameterFeatureSource * >( lModel->parameterDefinition( it.value().parameterName() ) ) )
{
mParentLayerComboBox-> addItem( definition->description(), definition->name() );
if ( !initialParent.isEmpty() && initialParent == definition->name() )
{
mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
}
}
else if ( const QgsProcessingParameterVectorLayer *definition = dynamic_cast< const QgsProcessingParameterVectorLayer * >( lModel->parameterDefinition( it.value().parameterName() ) ) )
{
mParentLayerComboBox-> addItem( definition->description(), definition->name() );
if ( !initialParent.isEmpty() && initialParent == definition->name() )
{
mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
}
}
}
}
if ( mParentLayerComboBox->count() == 1 && !initialParent.isEmpty() )
{
// if no parent candidates found, we just add the existing one as a placeholder
mParentLayerComboBox->addItem( initialParent, initialParent );
mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
}
vlayout->addWidget( mParentLayerComboBox );
setLayout( vlayout );
}
QgsProcessingParameterDefinition *QgsProcessingFieldMapParameterDefinitionWidget::createParameter( const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags ) const
{
auto param = qgis::make_unique< QgsProcessingParameterFieldMapping >( name, description, mParentLayerComboBox->currentData().toString() );
param->setFlags( flags );
return param.release();
}
//
// QgsProcessingFieldMapWidgetWrapper
//
QgsProcessingFieldMapWidgetWrapper::QgsProcessingFieldMapWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QWidget *parent )
: QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
{
}
QString QgsProcessingFieldMapWidgetWrapper::parameterType() const
{
return QgsProcessingParameterFieldMapping::typeName();
}
QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingFieldMapWidgetWrapper::createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type )
{
return new QgsProcessingFieldMapWidgetWrapper( parameter, type );
}
QWidget *QgsProcessingFieldMapWidgetWrapper::createWidget()
{
mPanel = new QgsProcessingFieldMapPanelWidget( nullptr );
mPanel->setToolTip( parameterDefinition()->toolTip() );
mPanel->registerExpressionContextGenerator( this );
connect( mPanel, &QgsProcessingFieldMapPanelWidget::changed, this, [ = ]
{
emit widgetValueHasChanged( this );
} );
return mPanel;
}
QgsProcessingAbstractParameterDefinitionWidget *QgsProcessingFieldMapWidgetWrapper::createParameterDefinitionWidget( QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition, const QgsProcessingAlgorithm *algorithm )
{
return new QgsProcessingFieldMapParameterDefinitionWidget( context, widgetContext, definition, algorithm );
}
void QgsProcessingFieldMapWidgetWrapper::postInitialize( const QList<QgsAbstractProcessingParameterWidgetWrapper *> &wrappers )
{
QgsAbstractProcessingParameterWidgetWrapper::postInitialize( wrappers );
switch ( type() )
{
case QgsProcessingGui::Standard:
case QgsProcessingGui::Batch:
{
for ( const QgsAbstractProcessingParameterWidgetWrapper *wrapper : wrappers )
{
if ( wrapper->parameterDefinition()->name() == static_cast< const QgsProcessingParameterFieldMapping * >( parameterDefinition() )->parentLayerParameterName() )
{
setParentLayerWrapperValue( wrapper );
connect( wrapper, &QgsAbstractProcessingParameterWidgetWrapper::widgetValueHasChanged, this, [ = ]
{
setParentLayerWrapperValue( wrapper );
} );
break;
}
}
break;
}
case QgsProcessingGui::Modeler:
break;
}
}
int QgsProcessingFieldMapWidgetWrapper::stretch() const
{
return 1;
}
void QgsProcessingFieldMapWidgetWrapper::setParentLayerWrapperValue( const QgsAbstractProcessingParameterWidgetWrapper *parentWrapper )
{
// evaluate value to layer
QgsProcessingContext *context = nullptr;
std::unique_ptr< QgsProcessingContext > tmpContext;
if ( mProcessingContextGenerator )
context = mProcessingContextGenerator->processingContext();
if ( !context )
{
tmpContext = qgis::make_unique< QgsProcessingContext >();
context = tmpContext.get();
}
QgsVectorLayer *layer = QgsProcessingParameters::parameterAsVectorLayer( parentWrapper->parameterDefinition(), parentWrapper->parameterValue(), *context );
if ( !layer )
{
if ( mPanel )
mPanel->setLayer( nullptr );
return;
}
// need to grab ownership of layer if required - otherwise layer may be deleted when context
// goes out of scope
std::unique_ptr< QgsMapLayer > ownedLayer( context->takeResultLayer( layer->id() ) );
if ( ownedLayer && ownedLayer->type() == QgsMapLayerType::VectorLayer )
{
mParentLayer.reset( qobject_cast< QgsVectorLayer * >( ownedLayer.release() ) );
layer = mParentLayer.get();
}
else
{
// don't need ownership of this layer - it wasn't owned by context (so e.g. is owned by the project)
}
if ( mPanel )
mPanel->setLayer( layer );
}
void QgsProcessingFieldMapWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext & )
{
if ( mPanel )
mPanel->setValue( value );
}
QVariant QgsProcessingFieldMapWidgetWrapper::widgetValue() const
{
return mPanel ? mPanel->value() : QVariant();
}
QStringList QgsProcessingFieldMapWidgetWrapper::compatibleParameterTypes() const
{
return QStringList()
<< QgsProcessingParameterFieldMapping::typeName();
}
QStringList QgsProcessingFieldMapWidgetWrapper::compatibleOutputTypes() const
{
return QStringList();
}
QString QgsProcessingFieldMapWidgetWrapper::modelerExpressionFormatString() const
{
return tr( "an array of map items, each containing a 'name', 'type' and 'expression' values (and optional 'length' and 'precision' values)." );
}
const QgsVectorLayer *QgsProcessingFieldMapWidgetWrapper::linkedVectorLayer() const
{
if ( mPanel && mPanel->layer() )
return mPanel->layer();
return QgsAbstractProcessingParameterWidgetWrapper::linkedVectorLayer();
}
/// @endcond