-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsprocessingwidgetwrapperimpl.cpp
7013 lines (5941 loc) · 255 KB
/
qgsprocessingwidgetwrapperimpl.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgsprocessingwidgetwrapperimpl.cpp
---------------------
begin : August 2018
copyright : (C) 2018 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 "qgsprocessingwidgetwrapperimpl.h"
#include "qgsprocessingparameters.h"
#include "processing/models/qgsprocessingmodelalgorithm.h"
#include "qgsprocessingoutputs.h"
#include "qgsprojectionselectionwidget.h"
#include "qgsprocessingmatrixparameterdialog.h"
#include "qgsspinbox.h"
#include "qgsdoublespinbox.h"
#include "qgsprocessingcontext.h"
#include "qgsauthconfigselect.h"
#include "qgsapplication.h"
#include "qgsfilewidget.h"
#include "qgssettings.h"
#include "qgsexpressionlineedit.h"
#include "qgsfieldexpressionwidget.h"
#include "qgsexpressionbuilderwidget.h"
#include "qgsprocessingmultipleselectiondialog.h"
#include "qgslayoutmanager.h"
#include "qgsproject.h"
#include "qgslayoutcombobox.h"
#include "qgslayoutitemcombobox.h"
#include "qgsprintlayout.h"
#include "qgsscalewidget.h"
#include "qgssnapindicator.h"
#include "qgsmapmouseevent.h"
#include "qgsfilterlineedit.h"
#include "qgsmapcanvas.h"
#include "qgsmessagebar.h"
#include "qgscolorbutton.h"
#include "qgscoordinateoperationwidget.h"
#include "qgsdatumtransformdialog.h"
#include "qgsfieldcombobox.h"
#include "qgsmapthemecollection.h"
#include "qgsdatetimeedit.h"
#include "qgsproviderconnectioncombobox.h"
#include "qgsdatabaseschemacombobox.h"
#include "qgsdatabasetablecombobox.h"
#include "qgsextentwidget.h"
#include "qgsprocessingenummodelerwidget.h"
#include "qgsprocessingmatrixmodelerwidget.h"
#include "qgsprocessingmaplayercombobox.h"
#include "qgsrasterbandcombobox.h"
#include "qgsprocessingoutputdestinationwidget.h"
#include "qgscheckablecombobox.h"
#include <QToolButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QComboBox>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QRadioButton>
#include <QButtonGroup>
#include <QMenu>
#include <QFileDialog>
///@cond PRIVATE
//
// QgsProcessingBooleanWidgetWrapper
//
QgsProcessingBooleanParameterDefinitionWidget::QgsProcessingBooleanParameterDefinitionWidget( 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 );
mDefaultCheckBox = new QCheckBox( tr( "Checked" ) );
if ( const QgsProcessingParameterBoolean *boolParam = dynamic_cast<const QgsProcessingParameterBoolean *>( definition ) )
mDefaultCheckBox->setChecked( QgsProcessingParameters::parameterAsBool( boolParam, boolParam->defaultValueForGui(), context ) );
else
mDefaultCheckBox->setChecked( false );
vlayout->addWidget( mDefaultCheckBox );
setLayout( vlayout );
}
QgsProcessingParameterDefinition *QgsProcessingBooleanParameterDefinitionWidget::createParameter( const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags ) const
{
auto param = qgis::make_unique< QgsProcessingParameterBoolean >( name, description, mDefaultCheckBox->isChecked() );
param->setFlags( flags );
return param.release();
}
QgsProcessingBooleanWidgetWrapper::QgsProcessingBooleanWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QWidget *parent )
: QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
{
}
QWidget *QgsProcessingBooleanWidgetWrapper::createWidget()
{
switch ( type() )
{
case QgsProcessingGui::Standard:
{
QString description = parameterDefinition()->description();
if ( parameterDefinition()->flags() & QgsProcessingParameterDefinition::FlagOptional )
description = QObject::tr( "%1 [optional]" ).arg( description );
mCheckBox = new QCheckBox( description );
mCheckBox->setToolTip( parameterDefinition()->toolTip() );
connect( mCheckBox, &QCheckBox::toggled, this, [ = ]
{
emit widgetValueHasChanged( this );
} );
return mCheckBox;
}
case QgsProcessingGui::Batch:
case QgsProcessingGui::Modeler:
{
mComboBox = new QComboBox();
mComboBox->addItem( tr( "Yes" ), true );
mComboBox->addItem( tr( "No" ), false );
mComboBox->setToolTip( parameterDefinition()->toolTip() );
connect( mComboBox, qgis::overload< int>::of( &QComboBox::currentIndexChanged ), this, [ = ]
{
emit widgetValueHasChanged( this );
} );
return mComboBox;
}
}
return nullptr;
}
QLabel *QgsProcessingBooleanWidgetWrapper::createLabel()
{
// avoid creating labels in standard dialogs
if ( type() == QgsProcessingGui::Standard )
return nullptr;
else
return QgsAbstractProcessingParameterWidgetWrapper::createLabel();
}
void QgsProcessingBooleanWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
{
switch ( type() )
{
case QgsProcessingGui::Standard:
{
const bool v = QgsProcessingParameters::parameterAsBool( parameterDefinition(), value, context );
mCheckBox->setChecked( v );
break;
}
case QgsProcessingGui::Batch:
case QgsProcessingGui::Modeler:
{
const bool v = QgsProcessingParameters::parameterAsBool( parameterDefinition(), value, context );
mComboBox->setCurrentIndex( mComboBox->findData( v ) );
break;
}
}
}
QVariant QgsProcessingBooleanWidgetWrapper::widgetValue() const
{
switch ( type() )
{
case QgsProcessingGui::Standard:
return mCheckBox->isChecked();
case QgsProcessingGui::Batch:
case QgsProcessingGui::Modeler:
return mComboBox->currentData();
}
return QVariant();
}
QStringList QgsProcessingBooleanWidgetWrapper::compatibleParameterTypes() const
{
//pretty much everything is compatible here and can be converted to a bool!
return QStringList() << QgsProcessingParameterBoolean::typeName()
<< QgsProcessingParameterString::typeName()
<< QgsProcessingParameterNumber::typeName()
<< QgsProcessingParameterDistance::typeName()
<< QgsProcessingParameterScale::typeName()
<< QgsProcessingParameterFile::typeName()
<< QgsProcessingParameterField::typeName()
<< QgsProcessingParameterFeatureSource::typeName()
<< QgsProcessingParameterMapLayer::typeName()
<< QgsProcessingParameterRasterLayer::typeName()
<< QgsProcessingParameterVectorLayer::typeName()
<< QgsProcessingParameterMeshLayer::typeName()
<< QgsProcessingParameterExpression::typeName()
<< QgsProcessingParameterProviderConnection::typeName();
}
QStringList QgsProcessingBooleanWidgetWrapper::compatibleOutputTypes() const
{
return QStringList() << QgsProcessingOutputNumber::typeName()
<< QgsProcessingOutputMapLayer::typeName()
<< QgsProcessingOutputFile::typeName()
<< QgsProcessingOutputRasterLayer::typeName()
<< QgsProcessingOutputVectorLayer::typeName()
<< QgsProcessingOutputString::typeName()
<< QgsProcessingOutputBoolean::typeName();
}
QString QgsProcessingBooleanWidgetWrapper::parameterType() const
{
return QgsProcessingParameterBoolean::typeName();
}
QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingBooleanWidgetWrapper::createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type )
{
return new QgsProcessingBooleanWidgetWrapper( parameter, type );
}
QgsProcessingAbstractParameterDefinitionWidget *QgsProcessingBooleanWidgetWrapper::createParameterDefinitionWidget( QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition, const QgsProcessingAlgorithm *algorithm )
{
return new QgsProcessingBooleanParameterDefinitionWidget( context, widgetContext, definition, algorithm );
}
//
// QgsProcessingCrsWidgetWrapper
//
QgsProcessingCrsParameterDefinitionWidget::QgsProcessingCrsParameterDefinitionWidget( 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( "Default value" ) ) );
mCrsSelector = new QgsProjectionSelectionWidget();
if ( const QgsProcessingParameterCrs *crsParam = dynamic_cast<const QgsProcessingParameterCrs *>( definition ) )
mCrsSelector->setCrs( QgsProcessingParameters::parameterAsCrs( crsParam, crsParam->defaultValueForGui(), context ) );
else
mCrsSelector->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
vlayout->addWidget( mCrsSelector );
setLayout( vlayout );
}
QgsProcessingParameterDefinition *QgsProcessingCrsParameterDefinitionWidget::createParameter( const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags ) const
{
auto param = qgis::make_unique< QgsProcessingParameterCrs >( name, description, mCrsSelector->crs().authid() );
param->setFlags( flags );
return param.release();
}
QgsProcessingCrsWidgetWrapper::QgsProcessingCrsWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QWidget *parent )
: QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
{
}
QWidget *QgsProcessingCrsWidgetWrapper::createWidget()
{
Q_ASSERT( mProjectionSelectionWidget == nullptr );
mProjectionSelectionWidget = new QgsProjectionSelectionWidget();
mProjectionSelectionWidget->setToolTip( parameterDefinition()->toolTip() );
if ( parameterDefinition()->flags() & QgsProcessingParameterDefinition::FlagOptional )
mProjectionSelectionWidget->setOptionVisible( QgsProjectionSelectionWidget::CrsNotSet, true );
else
mProjectionSelectionWidget->setOptionVisible( QgsProjectionSelectionWidget::CrsNotSet, false );
connect( mProjectionSelectionWidget, &QgsProjectionSelectionWidget::crsChanged, this, [ = ]
{
emit widgetValueHasChanged( this );
} );
switch ( type() )
{
case QgsProcessingGui::Standard:
case QgsProcessingGui::Batch:
{
return mProjectionSelectionWidget;
}
case QgsProcessingGui::Modeler:
{
QWidget *w = new QWidget();
w->setToolTip( parameterDefinition()->toolTip() );
QVBoxLayout *vl = new QVBoxLayout();
vl->setContentsMargins( 0, 0, 0, 0 );
w->setLayout( vl );
mUseProjectCrsCheckBox = new QCheckBox( tr( "Use project CRS" ) );
mUseProjectCrsCheckBox->setToolTip( tr( "Always use the current project CRS when running the model" ) );
vl->addWidget( mUseProjectCrsCheckBox );
connect( mUseProjectCrsCheckBox, &QCheckBox::toggled, mProjectionSelectionWidget, &QgsProjectionSelectionWidget::setDisabled );
connect( mUseProjectCrsCheckBox, &QCheckBox::toggled, this, [ = ]
{
emit widgetValueHasChanged( this );
} );
vl->addWidget( mProjectionSelectionWidget );
return w;
}
}
return nullptr;
}
void QgsProcessingCrsWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
{
if ( mUseProjectCrsCheckBox )
{
if ( value.toString().compare( QLatin1String( "ProjectCrs" ), Qt::CaseInsensitive ) == 0 )
{
mUseProjectCrsCheckBox->setChecked( true );
return;
}
else
{
mUseProjectCrsCheckBox->setChecked( false );
}
}
const QgsCoordinateReferenceSystem v = QgsProcessingParameters::parameterAsCrs( parameterDefinition(), value, context );
if ( mProjectionSelectionWidget )
mProjectionSelectionWidget->setCrs( v );
}
QVariant QgsProcessingCrsWidgetWrapper::widgetValue() const
{
if ( mUseProjectCrsCheckBox && mUseProjectCrsCheckBox->isChecked() )
return QStringLiteral( "ProjectCrs" );
else if ( mProjectionSelectionWidget )
return mProjectionSelectionWidget->crs().isValid() ? mProjectionSelectionWidget->crs() : QVariant();
else
return QVariant();
}
QStringList QgsProcessingCrsWidgetWrapper::compatibleParameterTypes() const
{
return QStringList()
<< QgsProcessingParameterCrs::typeName()
<< QgsProcessingParameterExpression::typeName()
<< QgsProcessingParameterString::typeName()
<< QgsProcessingParameterRasterLayer::typeName()
<< QgsProcessingParameterVectorLayer::typeName()
<< QgsProcessingParameterMeshLayer::typeName()
<< QgsProcessingParameterFeatureSource::typeName();
}
QStringList QgsProcessingCrsWidgetWrapper::compatibleOutputTypes() const
{
return QStringList() << QgsProcessingOutputVectorLayer::typeName()
<< QgsProcessingOutputRasterLayer::typeName()
<< QgsProcessingOutputMapLayer::typeName()
<< QgsProcessingOutputString::typeName();
}
QString QgsProcessingCrsWidgetWrapper::modelerExpressionFormatString() const
{
return tr( "string as EPSG code, WKT or PROJ format, or a string identifying a map layer" );
}
QString QgsProcessingCrsWidgetWrapper::parameterType() const
{
return QgsProcessingParameterCrs::typeName();
}
QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingCrsWidgetWrapper::createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type )
{
return new QgsProcessingCrsWidgetWrapper( parameter, type );
}
QgsProcessingAbstractParameterDefinitionWidget *QgsProcessingCrsWidgetWrapper::createParameterDefinitionWidget( QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition, const QgsProcessingAlgorithm *algorithm )
{
return new QgsProcessingCrsParameterDefinitionWidget( context, widgetContext, definition, algorithm );
}
//
// QgsProcessingStringWidgetWrapper
//
QgsProcessingStringParameterDefinitionWidget::QgsProcessingStringParameterDefinitionWidget( 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( "Default value" ) ) );
mDefaultLineEdit = new QLineEdit();
if ( const QgsProcessingParameterString *stringParam = dynamic_cast<const QgsProcessingParameterString *>( definition ) )
mDefaultLineEdit->setText( QgsProcessingParameters::parameterAsString( stringParam, stringParam->defaultValueForGui(), context ) );
vlayout->addWidget( mDefaultLineEdit );
mMultiLineCheckBox = new QCheckBox( tr( "Multiline input" ) );
if ( const QgsProcessingParameterString *stringParam = dynamic_cast<const QgsProcessingParameterString *>( definition ) )
mMultiLineCheckBox->setChecked( stringParam->multiLine() );
vlayout->addWidget( mMultiLineCheckBox );
setLayout( vlayout );
}
QgsProcessingParameterDefinition *QgsProcessingStringParameterDefinitionWidget::createParameter( const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags ) const
{
auto param = qgis::make_unique< QgsProcessingParameterString >( name, description, mDefaultLineEdit->text(), mMultiLineCheckBox->isChecked() );
param->setFlags( flags );
return param.release();
}
QgsProcessingStringWidgetWrapper::QgsProcessingStringWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QWidget *parent )
: QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
{
}
QWidget *QgsProcessingStringWidgetWrapper::createWidget()
{
switch ( type() )
{
case QgsProcessingGui::Standard:
case QgsProcessingGui::Modeler:
{
if ( static_cast< const QgsProcessingParameterString * >( parameterDefinition() )->multiLine() )
{
mPlainTextEdit = new QPlainTextEdit();
mPlainTextEdit->setToolTip( parameterDefinition()->toolTip() );
connect( mPlainTextEdit, &QPlainTextEdit::textChanged, this, [ = ]
{
emit widgetValueHasChanged( this );
} );
return mPlainTextEdit;
}
else
{
mLineEdit = new QLineEdit();
mLineEdit->setToolTip( parameterDefinition()->toolTip() );
connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]
{
emit widgetValueHasChanged( this );
} );
return mLineEdit;
}
}
case QgsProcessingGui::Batch:
{
mLineEdit = new QLineEdit();
mLineEdit->setToolTip( parameterDefinition()->toolTip() );
connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]
{
emit widgetValueHasChanged( this );
} );
return mLineEdit;
}
}
return nullptr;
}
void QgsProcessingStringWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
{
const QString v = QgsProcessingParameters::parameterAsString( parameterDefinition(), value, context );
if ( mLineEdit )
mLineEdit->setText( v );
if ( mPlainTextEdit )
mPlainTextEdit->setPlainText( v );
}
QVariant QgsProcessingStringWidgetWrapper::widgetValue() const
{
if ( mLineEdit )
return mLineEdit->text();
else if ( mPlainTextEdit )
return mPlainTextEdit->toPlainText();
else
return QVariant();
}
QStringList QgsProcessingStringWidgetWrapper::compatibleParameterTypes() const
{
return QStringList()
<< QgsProcessingParameterString::typeName()
<< QgsProcessingParameterAuthConfig::typeName()
<< QgsProcessingParameterNumber::typeName()
<< QgsProcessingParameterDistance::typeName()
<< QgsProcessingParameterScale::typeName()
<< QgsProcessingParameterFile::typeName()
<< QgsProcessingParameterField::typeName()
<< QgsProcessingParameterExpression::typeName()
<< QgsProcessingParameterCoordinateOperation::typeName()
<< QgsProcessingParameterProviderConnection::typeName();
}
QStringList QgsProcessingStringWidgetWrapper::compatibleOutputTypes() const
{
return QStringList() << QgsProcessingOutputNumber::typeName()
<< QgsProcessingOutputFile::typeName()
<< QgsProcessingOutputFolder::typeName()
<< QgsProcessingOutputString::typeName();
}
QString QgsProcessingStringWidgetWrapper::parameterType() const
{
return QgsProcessingParameterString::typeName();
}
QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingStringWidgetWrapper::createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type )
{
return new QgsProcessingStringWidgetWrapper( parameter, type );
}
QgsProcessingAbstractParameterDefinitionWidget *QgsProcessingStringWidgetWrapper::createParameterDefinitionWidget( QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition, const QgsProcessingAlgorithm *algorithm )
{
return new QgsProcessingStringParameterDefinitionWidget( context, widgetContext, definition, algorithm );
}
//
// QgsProcessingAuthConfigWidgetWrapper
//
QgsProcessingAuthConfigWidgetWrapper::QgsProcessingAuthConfigWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QWidget *parent )
: QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
{
}
QWidget *QgsProcessingAuthConfigWidgetWrapper::createWidget()
{
switch ( type() )
{
case QgsProcessingGui::Standard:
case QgsProcessingGui::Modeler:
case QgsProcessingGui::Batch:
{
mAuthConfigSelect = new QgsAuthConfigSelect();
mAuthConfigSelect->setToolTip( parameterDefinition()->toolTip() );
connect( mAuthConfigSelect, &QgsAuthConfigSelect::selectedConfigIdChanged, this, [ = ]
{
emit widgetValueHasChanged( this );
} );
return mAuthConfigSelect;
}
}
return nullptr;
}
void QgsProcessingAuthConfigWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
{
const QString v = QgsProcessingParameters::parameterAsString( parameterDefinition(), value, context );
if ( mAuthConfigSelect )
mAuthConfigSelect->setConfigId( v );
}
QVariant QgsProcessingAuthConfigWidgetWrapper::widgetValue() const
{
if ( mAuthConfigSelect )
return mAuthConfigSelect->configId();
else
return QVariant();
}
QStringList QgsProcessingAuthConfigWidgetWrapper::compatibleParameterTypes() const
{
return QStringList()
<< QgsProcessingParameterAuthConfig::typeName()
<< QgsProcessingParameterString::typeName()
<< QgsProcessingParameterExpression::typeName();
}
QStringList QgsProcessingAuthConfigWidgetWrapper::compatibleOutputTypes() const
{
return QStringList() << QgsProcessingOutputString::typeName();
}
QString QgsProcessingAuthConfigWidgetWrapper::parameterType() const
{
return QgsProcessingParameterAuthConfig::typeName();
}
QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingAuthConfigWidgetWrapper::createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type )
{
return new QgsProcessingAuthConfigWidgetWrapper( parameter, type );
}
//
// QgsProcessingNumericWidgetWrapper
//
QgsProcessingNumberParameterDefinitionWidget::QgsProcessingNumberParameterDefinitionWidget( 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( "Number type" ) ) );
mTypeComboBox = new QComboBox();
mTypeComboBox->addItem( tr( "Float" ), QgsProcessingParameterNumber::Double );
mTypeComboBox->addItem( tr( "Integer" ), QgsProcessingParameterNumber::Integer );
vlayout->addWidget( mTypeComboBox );
vlayout->addWidget( new QLabel( tr( "Minimum value" ) ) );
mMinLineEdit = new QLineEdit();
vlayout->addWidget( mMinLineEdit );
vlayout->addWidget( new QLabel( tr( "Maximum value" ) ) );
mMaxLineEdit = new QLineEdit();
vlayout->addWidget( mMaxLineEdit );
vlayout->addWidget( new QLabel( tr( "Default value" ) ) );
mDefaultLineEdit = new QLineEdit();
vlayout->addWidget( mDefaultLineEdit );
if ( const QgsProcessingParameterNumber *numberParam = dynamic_cast<const QgsProcessingParameterNumber *>( definition ) )
{
mTypeComboBox->setCurrentIndex( mTypeComboBox->findData( numberParam->dataType() ) );
mMinLineEdit->setText( QString::number( numberParam->minimum() ) );
mMaxLineEdit->setText( QString::number( numberParam->maximum() ) );
mDefaultLineEdit->setText( numberParam->defaultValueForGui().toString() );
}
setLayout( vlayout );
}
QgsProcessingParameterDefinition *QgsProcessingNumberParameterDefinitionWidget::createParameter( const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags ) const
{
bool ok;
double val = mDefaultLineEdit->text().toDouble( &ok );
QgsProcessingParameterNumber::Type dataType = static_cast< QgsProcessingParameterNumber::Type >( mTypeComboBox->currentData().toInt() );
auto param = qgis::make_unique< QgsProcessingParameterNumber >( name, description, dataType, ok ? val : QVariant() );
val = mMinLineEdit->text().toDouble( &ok );
if ( ok )
{
param->setMinimum( val );
}
val = mMaxLineEdit->text().toDouble( &ok );
if ( ok )
{
param->setMaximum( val );
}
param->setFlags( flags );
return param.release();
}
QgsProcessingNumericWidgetWrapper::QgsProcessingNumericWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QWidget *parent )
: QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
{
}
QWidget *QgsProcessingNumericWidgetWrapper::createWidget()
{
const QgsProcessingParameterNumber *numberDef = static_cast< const QgsProcessingParameterNumber * >( parameterDefinition() );
const QVariantMap metadata = numberDef->metadata();
const int decimals = metadata.value( QStringLiteral( "widget_wrapper" ) ).toMap().value( QStringLiteral( "decimals" ), 6 ).toInt();
switch ( type() )
{
case QgsProcessingGui::Standard:
case QgsProcessingGui::Modeler:
case QgsProcessingGui::Batch:
{
// lots of duplicate code here -- but there's no common interface between QSpinBox/QDoubleSpinBox which would allow us to avoid this
QAbstractSpinBox *spinBox = nullptr;
switch ( numberDef->dataType() )
{
case QgsProcessingParameterNumber::Double:
mDoubleSpinBox = new QgsDoubleSpinBox();
mDoubleSpinBox->setExpressionsEnabled( true );
mDoubleSpinBox->setDecimals( decimals );
// guess reasonable step value for double spin boxes
if ( !qgsDoubleNear( numberDef->maximum(), std::numeric_limits<double>::max() ) &&
!qgsDoubleNear( numberDef->minimum(), std::numeric_limits<double>::lowest() + 1 ) )
{
double singleStep = calculateStep( numberDef->minimum(), numberDef->maximum() );
singleStep = std::max( singleStep, std::pow( 10, -decimals ) );
mDoubleSpinBox->setSingleStep( singleStep );
}
spinBox = mDoubleSpinBox;
break;
case QgsProcessingParameterNumber::Integer:
mSpinBox = new QgsSpinBox();
mSpinBox->setExpressionsEnabled( true );
spinBox = mSpinBox;
break;
}
spinBox->setToolTip( parameterDefinition()->toolTip() );
double max = 999999999;
if ( !qgsDoubleNear( numberDef->maximum(), std::numeric_limits<double>::max() ) )
{
max = numberDef->maximum();
}
double min = -999999999;
if ( !qgsDoubleNear( numberDef->minimum(), std::numeric_limits<double>::lowest() ) )
{
min = numberDef->minimum();
}
if ( mDoubleSpinBox )
{
mDoubleSpinBox->setMinimum( min );
mDoubleSpinBox->setMaximum( max );
}
else
{
mSpinBox->setMinimum( static_cast< int >( min ) );
mSpinBox->setMaximum( static_cast< int >( max ) );
}
if ( numberDef->flags() & QgsProcessingParameterDefinition::FlagOptional )
{
mAllowingNull = true;
if ( mDoubleSpinBox )
{
mDoubleSpinBox->setShowClearButton( true );
const double min = mDoubleSpinBox->minimum() - 1;
mDoubleSpinBox->setMinimum( min );
mDoubleSpinBox->setValue( min );
}
else
{
mSpinBox->setShowClearButton( true );
const int min = mSpinBox->minimum() - 1;
mSpinBox->setMinimum( min );
mSpinBox->setValue( min );
}
spinBox->setSpecialValueText( tr( "Not set" ) );
}
else
{
if ( numberDef->defaultValueForGui().isValid() )
{
// if default value for parameter, we clear to that
bool ok = false;
if ( mDoubleSpinBox )
{
double defaultVal = numberDef->defaultValueForGui().toDouble( &ok );
if ( ok )
mDoubleSpinBox->setClearValue( defaultVal );
}
else
{
int intVal = numberDef->defaultValueForGui().toInt( &ok );
if ( ok )
mSpinBox->setClearValue( intVal );
}
}
else if ( !qgsDoubleNear( numberDef->minimum(), std::numeric_limits<double>::lowest() ) )
{
// otherwise we clear to the minimum, if it's set
if ( mDoubleSpinBox )
mDoubleSpinBox->setClearValue( numberDef->minimum() );
else
mSpinBox->setClearValue( static_cast< int >( numberDef->minimum() ) );
}
else
{
// last resort, we clear to 0
if ( mDoubleSpinBox )
{
mDoubleSpinBox->setValue( 0 );
mDoubleSpinBox->setClearValue( 0 );
}
else
{
mSpinBox->setValue( 0 );
mSpinBox->setClearValue( 0 );
}
}
}
if ( mDoubleSpinBox )
connect( mDoubleSpinBox, qgis::overload<double>::of( &QgsDoubleSpinBox::valueChanged ), this, [ = ] { emit widgetValueHasChanged( this ); } );
else if ( mSpinBox )
connect( mSpinBox, qgis::overload<int>::of( &QgsSpinBox::valueChanged ), this, [ = ] { emit widgetValueHasChanged( this ); } );
return spinBox;
}
}
return nullptr;
}
void QgsProcessingNumericWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
{
if ( mDoubleSpinBox )
{
if ( mAllowingNull && !value.isValid() )
mDoubleSpinBox->clear();
else
{
const double v = QgsProcessingParameters::parameterAsDouble( parameterDefinition(), value, context );
mDoubleSpinBox->setValue( v );
}
}
else if ( mSpinBox )
{
if ( mAllowingNull && !value.isValid() )
mSpinBox->clear();
else
{
const int v = QgsProcessingParameters::parameterAsInt( parameterDefinition(), value, context );
mSpinBox->setValue( v );
}
}
}
QVariant QgsProcessingNumericWidgetWrapper::widgetValue() const
{
if ( mDoubleSpinBox )
{
if ( mAllowingNull && qgsDoubleNear( mDoubleSpinBox->value(), mDoubleSpinBox->minimum() ) )
return QVariant();
else
return mDoubleSpinBox->value();
}
else if ( mSpinBox )
{
if ( mAllowingNull && mSpinBox->value() == mSpinBox->minimum() )
return QVariant();
else
return mSpinBox->value();
}
else
return QVariant();
}
QStringList QgsProcessingNumericWidgetWrapper::compatibleParameterTypes() const
{
return QStringList()
<< QgsProcessingParameterString::typeName()
<< QgsProcessingParameterNumber::typeName()
<< QgsProcessingParameterDistance::typeName()
<< QgsProcessingParameterScale::typeName();
}
QStringList QgsProcessingNumericWidgetWrapper::compatibleOutputTypes() const
{
return QStringList() << QgsProcessingOutputNumber::typeName()
<< QgsProcessingOutputString::typeName();
}
double QgsProcessingNumericWidgetWrapper::calculateStep( const double minimum, const double maximum )
{
const double valueRange = maximum - minimum;
if ( valueRange <= 1.0 )
{
const double step = valueRange / 10.0;
// round to 1 significant figure
return qgsRound( step, -std::floor( std::log( step ) ) );
}
else
{
return 1.0;
}
}
QString QgsProcessingNumericWidgetWrapper::parameterType() const
{
return QgsProcessingParameterNumber::typeName();
}
QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingNumericWidgetWrapper::createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type )
{
return new QgsProcessingNumericWidgetWrapper( parameter, type );
}
QgsProcessingAbstractParameterDefinitionWidget *QgsProcessingNumericWidgetWrapper::createParameterDefinitionWidget( QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition, const QgsProcessingAlgorithm *algorithm )
{
return new QgsProcessingNumberParameterDefinitionWidget( context, widgetContext, definition, algorithm );
}
//
// QgsProcessingDistanceWidgetWrapper
//
QgsProcessingDistanceParameterDefinitionWidget::QgsProcessingDistanceParameterDefinitionWidget( 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( "Linked input" ) ) );
mParentLayerComboBox = new QComboBox();
QString initialParent;
if ( const QgsProcessingParameterDistance *distParam = dynamic_cast<const QgsProcessingParameterDistance *>( definition ) )
initialParent = distParam->parentParameterName();
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 );
}
}
else if ( const QgsProcessingParameterMapLayer *definition = dynamic_cast< const QgsProcessingParameterMapLayer * >( 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 QgsProcessingParameterCrs *definition = dynamic_cast< const QgsProcessingParameterCrs * >( 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() == 0 && !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 );
vlayout->addWidget( new QLabel( tr( "Minimum value" ) ) );
mMinLineEdit = new QLineEdit();
vlayout->addWidget( mMinLineEdit );
vlayout->addWidget( new QLabel( tr( "Maximum value" ) ) );
mMaxLineEdit = new QLineEdit();
vlayout->addWidget( mMaxLineEdit );
vlayout->addWidget( new QLabel( tr( "Default value" ) ) );
mDefaultLineEdit = new QLineEdit();
vlayout->addWidget( mDefaultLineEdit );
if ( const QgsProcessingParameterDistance *distParam = dynamic_cast<const QgsProcessingParameterDistance *>( definition ) )
{
mMinLineEdit->setText( QString::number( distParam->minimum() ) );
mMaxLineEdit->setText( QString::number( distParam->maximum() ) );
mDefaultLineEdit->setText( distParam->defaultValueForGui().toString() );
}
setLayout( vlayout );
}
QgsProcessingParameterDefinition *QgsProcessingDistanceParameterDefinitionWidget::createParameter( const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags ) const
{
bool ok;
double val = mDefaultLineEdit->text().toDouble( &ok );
auto param = qgis::make_unique< QgsProcessingParameterDistance >( name, description, ok ? val : QVariant(), mParentLayerComboBox->currentData().toString() );