/
qgsrelationeditorwidget.cpp
697 lines (584 loc) · 25.4 KB
/
qgsrelationeditorwidget.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
/***************************************************************************
qgsrelationeditor.cpp
--------------------------------------
Date : 17.5.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias at opengis dot ch
***************************************************************************
* *
* 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 "qgsrelationeditorwidget.h"
#include "qgsapplication.h"
#include "qgsdistancearea.h"
#include "qgsfeatureiterator.h"
#include "qgsvectordataprovider.h"
#include "qgsexpression.h"
#include "qgsfeature.h"
#include "qgsfeatureselectiondlg.h"
#include "qgsgenericfeatureselectionmanager.h"
#include "qgsrelation.h"
#include "qgsvectorlayertools.h"
#include "qgsproject.h"
#include "qgstransactiongroup.h"
#include "qgslogger.h"
#include "qgsvectorlayerutils.h"
#include "qgsmapcanvas.h"
#include <QHBoxLayout>
#include <QLabel>
QgsRelationEditorWidget::QgsRelationEditorWidget( QWidget *parent )
: QgsCollapsibleGroupBox( parent )
{
QVBoxLayout *topLayout = new QVBoxLayout( this );
topLayout->setContentsMargins( 0, 9, 0, 0 );
setLayout( topLayout );
// buttons
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->setContentsMargins( 0, 0, 0, 0 );
// toogle editing
mToggleEditingButton = new QToolButton( this );
mToggleEditingButton->setObjectName( QStringLiteral( "mToggleEditingButton" ) );
mToggleEditingButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionToggleEditing.svg" ) ) );
mToggleEditingButton->setText( tr( "Toggle editing" ) );
mToggleEditingButton->setEnabled( false );
mToggleEditingButton->setCheckable( true );
mToggleEditingButton->setToolTip( tr( "Toggle editing mode for child layer" ) );
buttonLayout->addWidget( mToggleEditingButton );
// save Edits
mSaveEditsButton = new QToolButton( this );
mSaveEditsButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionSaveEdits.svg" ) ) );
mSaveEditsButton->setText( tr( "Save child layer edits" ) );
mSaveEditsButton->setToolTip( tr( "Save child layer edits" ) );
mSaveEditsButton->setEnabled( true );
buttonLayout->addWidget( mSaveEditsButton );
// add feature
mAddFeatureButton = new QToolButton( this );
mAddFeatureButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionNewTableRow.svg" ) ) );
mAddFeatureButton->setText( tr( "Add child feature" ) );
mAddFeatureButton->setToolTip( tr( "Add child feature" ) );
mAddFeatureButton->setObjectName( QStringLiteral( "mAddFeatureButton" ) );
buttonLayout->addWidget( mAddFeatureButton );
// duplicate feature
mDuplicateFeatureButton = new QToolButton( this );
mDuplicateFeatureButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateFeature.svg" ) ) );
mDuplicateFeatureButton->setText( tr( "Duplicate child feature" ) );
mDuplicateFeatureButton->setToolTip( tr( "Duplicate child feature" ) );
mDuplicateFeatureButton->setObjectName( QStringLiteral( "mDuplicateFeatureButton" ) );
buttonLayout->addWidget( mDuplicateFeatureButton );
// delete feature
mDeleteFeatureButton = new QToolButton( this );
mDeleteFeatureButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionDeleteSelected.svg" ) ) );
mDeleteFeatureButton->setText( tr( "Delete child feature" ) );
mDeleteFeatureButton->setToolTip( tr( "Delete child feature" ) );
mDeleteFeatureButton->setObjectName( QStringLiteral( "mDeleteFeatureButton" ) );
buttonLayout->addWidget( mDeleteFeatureButton );
// link feature
mLinkFeatureButton = new QToolButton( this );
mLinkFeatureButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionLink.svg" ) ) );
mLinkFeatureButton->setText( tr( "Link existing features" ) );
mLinkFeatureButton->setToolTip( tr( "Link existing child features" ) );
mLinkFeatureButton->setObjectName( QStringLiteral( "mLinkFeatureButton" ) );
buttonLayout->addWidget( mLinkFeatureButton );
// unlink feature
mUnlinkFeatureButton = new QToolButton( this );
mUnlinkFeatureButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionUnlink.svg" ) ) );
mUnlinkFeatureButton->setText( tr( "Unlink feature" ) );
mUnlinkFeatureButton->setToolTip( tr( "Unlink child feature" ) );
mUnlinkFeatureButton->setObjectName( QStringLiteral( "mUnlinkFeatureButton" ) );
buttonLayout->addWidget( mUnlinkFeatureButton );
// zoom to linked feature
mZoomToFeatureButton = new QToolButton( this );
mZoomToFeatureButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionZoomToSelected.svg" ) ) );
mZoomToFeatureButton->setText( tr( "Zoom To Feature" ) );
mZoomToFeatureButton->setToolTip( tr( "Zoom to child feature" ) );
mZoomToFeatureButton->setObjectName( QStringLiteral( "mZoomToFeatureButton" ) );
buttonLayout->addWidget( mZoomToFeatureButton );
// spacer
buttonLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding ) );
// form view
mFormViewButton = new QToolButton( this );
mFormViewButton->setText( tr( "Form view" ) );
mFormViewButton->setToolTip( tr( "Switch to form view" ) );
mFormViewButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionPropertyItem.svg" ) ) );
mFormViewButton->setCheckable( true );
mFormViewButton->setChecked( mViewMode == QgsDualView::AttributeEditor );
buttonLayout->addWidget( mFormViewButton );
// table view
mTableViewButton = new QToolButton( this );
mTableViewButton->setText( tr( "Table view" ) );
mTableViewButton->setToolTip( tr( "Switch to table view" ) );
mTableViewButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionOpenTable.svg" ) ) );
mTableViewButton->setCheckable( true );
mTableViewButton->setChecked( mViewMode == QgsDualView::AttributeTable );
buttonLayout->addWidget( mTableViewButton );
// button group
mViewModeButtonGroup = new QButtonGroup( this );
mViewModeButtonGroup->addButton( mFormViewButton, QgsDualView::AttributeEditor );
mViewModeButtonGroup->addButton( mTableViewButton, QgsDualView::AttributeTable );
// add buttons layout
topLayout->addLayout( buttonLayout );
mRelationLayout = new QGridLayout();
mRelationLayout->setContentsMargins( 0, 0, 0, 0 );
topLayout->addLayout( mRelationLayout );
mDualView = new QgsDualView( this );
mDualView->setView( mViewMode );
mFeatureSelectionMgr = new QgsGenericFeatureSelectionManager( mDualView );
mDualView->setFeatureSelectionManager( mFeatureSelectionMgr );
mRelationLayout->addWidget( mDualView );
connect( this, &QgsCollapsibleGroupBoxBasic::collapsedStateChanged, this, &QgsRelationEditorWidget::onCollapsedStateChanged );
connect( mViewModeButtonGroup, static_cast<void ( QButtonGroup::* )( int )>( &QButtonGroup::buttonClicked ),
this, static_cast<void ( QgsRelationEditorWidget::* )( int )>( &QgsRelationEditorWidget::setViewMode ) );
connect( mToggleEditingButton, &QAbstractButton::clicked, this, &QgsRelationEditorWidget::toggleEditing );
connect( mSaveEditsButton, &QAbstractButton::clicked, this, &QgsRelationEditorWidget::saveEdits );
connect( mAddFeatureButton, &QAbstractButton::clicked, this, &QgsRelationEditorWidget::addFeature );
connect( mDuplicateFeatureButton, &QAbstractButton::clicked, this, &QgsRelationEditorWidget::duplicateFeature );
connect( mDeleteFeatureButton, &QAbstractButton::clicked, this, &QgsRelationEditorWidget::deleteSelectedFeatures );
connect( mLinkFeatureButton, &QAbstractButton::clicked, this, &QgsRelationEditorWidget::linkFeature );
connect( mUnlinkFeatureButton, &QAbstractButton::clicked, this, &QgsRelationEditorWidget::unlinkSelectedFeatures );
connect( mZoomToFeatureButton, &QAbstractButton::clicked, this, &QgsRelationEditorWidget::zoomToSelectedFeatures );
connect( mFeatureSelectionMgr, &QgsIFeatureSelectionManager::selectionChanged, this, &QgsRelationEditorWidget::updateButtons );
connect( mDualView, &QgsDualView::showContextMenuExternally, this, &QgsRelationEditorWidget::showContextMenu );
// Set initial state for add/remove etc. buttons
updateButtons();
}
void QgsRelationEditorWidget::setRelationFeature( const QgsRelation &relation, const QgsFeature &feature )
{
if ( mRelation.isValid() )
{
disconnect( mRelation.referencingLayer(), &QgsVectorLayer::editingStarted, this, &QgsRelationEditorWidget::updateButtons );
disconnect( mRelation.referencingLayer(), &QgsVectorLayer::editingStopped, this, &QgsRelationEditorWidget::updateButtons );
}
mRelation = relation;
mFeature = feature;
connect( mRelation.referencingLayer(), &QgsVectorLayer::editingStarted, this, &QgsRelationEditorWidget::updateButtons );
connect( mRelation.referencingLayer(), &QgsVectorLayer::editingStopped, this, &QgsRelationEditorWidget::updateButtons );
if ( mShowLabel )
setTitle( relation.name() );
QgsVectorLayer *lyr = relation.referencingLayer();
bool canChangeAttributes = lyr->dataProvider()->capabilities() & QgsVectorDataProvider::ChangeAttributeValues;
if ( canChangeAttributes && !lyr->readOnly() )
{
mToggleEditingButton->setEnabled( true );
updateButtons();
}
else
{
mToggleEditingButton->setEnabled( false );
}
setObjectName( QStringLiteral( "referenced/" ) + mRelation.name() );
// If not yet initialized, it is not (yet) visible, so we don't load it to be faster (lazy loading)
// If it is already initialized, it has been set visible before and the currently shown feature is changing
// and the widget needs updating
if ( mVisible )
{
QgsFeatureRequest myRequest = mRelation.getRelatedFeaturesRequest( mFeature );
mDualView->init( mRelation.referencingLayer(), nullptr, myRequest, mEditorContext );
}
}
void QgsRelationEditorWidget::setRelations( const QgsRelation &relation, const QgsRelation &nmrelation )
{
if ( mRelation.isValid() )
{
disconnect( mRelation.referencingLayer(), &QgsVectorLayer::editingStarted, this, &QgsRelationEditorWidget::updateButtons );
disconnect( mRelation.referencingLayer(), &QgsVectorLayer::editingStopped, this, &QgsRelationEditorWidget::updateButtons );
}
if ( mNmRelation.isValid() )
{
disconnect( mNmRelation.referencedLayer(), &QgsVectorLayer::editingStarted, this, &QgsRelationEditorWidget::updateButtons );
disconnect( mNmRelation.referencedLayer(), &QgsVectorLayer::editingStopped, this, &QgsRelationEditorWidget::updateButtons );
}
mRelation = relation;
mNmRelation = nmrelation;
if ( !mRelation.isValid() )
return;
mToggleEditingButton->setVisible( true );
const auto transactionGroups = QgsProject::instance()->transactionGroups();
for ( auto it = transactionGroups.constBegin(); it != transactionGroups.constEnd(); ++it )
{
if ( it.value()->layers().contains( mRelation.referencingLayer() ) )
{
mToggleEditingButton->setVisible( false );
mSaveEditsButton->setVisible( false );
}
}
connect( mRelation.referencingLayer(), &QgsVectorLayer::editingStarted, this, &QgsRelationEditorWidget::updateButtons );
connect( mRelation.referencingLayer(), &QgsVectorLayer::editingStopped, this, &QgsRelationEditorWidget::updateButtons );
if ( mNmRelation.isValid() )
{
connect( mNmRelation.referencedLayer(), &QgsVectorLayer::editingStarted, this, &QgsRelationEditorWidget::updateButtons );
connect( mNmRelation.referencedLayer(), &QgsVectorLayer::editingStopped, this, &QgsRelationEditorWidget::updateButtons );
}
setTitle( relation.name() );
QgsVectorLayer *lyr = relation.referencingLayer();
bool canChangeAttributes = lyr->dataProvider()->capabilities() & QgsVectorDataProvider::ChangeAttributeValues;
if ( canChangeAttributes && !lyr->readOnly() )
{
mToggleEditingButton->setEnabled( true );
updateButtons();
}
else
{
mToggleEditingButton->setEnabled( false );
}
setObjectName( QStringLiteral( "referenced/" ) + mRelation.name() );
updateUi();
}
void QgsRelationEditorWidget::setEditorContext( const QgsAttributeEditorContext &context )
{
mEditorContext = context;
}
QgsIFeatureSelectionManager *QgsRelationEditorWidget::featureSelectionManager()
{
return mFeatureSelectionMgr;
}
void QgsRelationEditorWidget::setViewMode( QgsDualView::ViewMode mode )
{
mDualView->setView( mode );
mViewMode = mode;
}
void QgsRelationEditorWidget::setFeature( const QgsFeature &feature )
{
mFeature = feature;
updateUi();
}
void QgsRelationEditorWidget::updateButtons()
{
bool editable = false;
bool linkable = false;
bool selectionNotEmpty = mFeatureSelectionMgr->selectedFeatureCount();
if ( mRelation.isValid() )
{
editable = mRelation.referencingLayer()->isEditable();
linkable = mRelation.referencingLayer()->isEditable();
}
if ( mNmRelation.isValid() )
{
editable = mNmRelation.referencedLayer()->isEditable();
}
mAddFeatureButton->setEnabled( editable );
mDuplicateFeatureButton->setEnabled( editable && selectionNotEmpty );
mLinkFeatureButton->setEnabled( linkable );
mDeleteFeatureButton->setEnabled( editable && selectionNotEmpty );
mUnlinkFeatureButton->setEnabled( linkable && selectionNotEmpty );
mZoomToFeatureButton->setVisible(
mEditorContext.mapCanvas() && (
(
mNmRelation.isValid() &&
mNmRelation.referencedLayer()->geometryType() != QgsWkbTypes::NullGeometry &&
mNmRelation.referencedLayer()->geometryType() != QgsWkbTypes::UnknownGeometry
)
||
(
mRelation.isValid() &&
mRelation.referencedLayer()->geometryType() != QgsWkbTypes::NullGeometry &&
mRelation.referencedLayer()->geometryType() != QgsWkbTypes::UnknownGeometry
)
)
);
mZoomToFeatureButton->setEnabled( selectionNotEmpty );
mToggleEditingButton->setChecked( editable );
mSaveEditsButton->setEnabled( editable );
}
void QgsRelationEditorWidget::addFeature()
{
QgsAttributeMap keyAttrs;
const QgsVectorLayerTools *vlTools = mEditorContext.vectorLayerTools();
if ( mNmRelation.isValid() )
{
// n:m Relation: first let the user create a new feature on the other table
// and autocreate a new linking feature.
QgsFeature f;
if ( vlTools->addFeature( mNmRelation.referencedLayer(), QgsAttributeMap(), QgsGeometry(), &f ) )
{
// Fields of the linking table
const QgsFields fields = mRelation.referencingLayer()->fields();
// Expression context for the linking table
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext();
QgsAttributeMap linkAttributes;
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mRelation.fieldPairs() )
{
int index = fields.indexOf( fieldPair.first );
linkAttributes.insert( index, mFeature.attribute( fieldPair.second ) );
}
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mNmRelation.fieldPairs() )
{
int index = fields.indexOf( fieldPair.first );
linkAttributes.insert( index, f.attribute( fieldPair.second ) );
}
QgsFeature linkFeature = QgsVectorLayerUtils::createFeature( mRelation.referencingLayer(), QgsGeometry(), linkAttributes, &context );
mRelation.referencingLayer()->addFeature( linkFeature );
updateUi();
}
}
else
{
QgsFields fields = mRelation.referencingLayer()->fields();
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mRelation.fieldPairs() )
{
keyAttrs.insert( fields.indexFromName( fieldPair.referencingField() ), mFeature.attribute( fieldPair.referencedField() ) );
}
vlTools->addFeature( mDualView->masterModel()->layer(), keyAttrs );
}
}
void QgsRelationEditorWidget::linkFeature()
{
QgsVectorLayer *layer = nullptr;
if ( mNmRelation.isValid() )
layer = mNmRelation.referencedLayer();
else
layer = mRelation.referencingLayer();
QgsFeatureSelectionDlg selectionDlg( layer, mEditorContext, this );
if ( selectionDlg.exec() )
{
if ( mNmRelation.isValid() )
{
QgsFeatureIterator it = mNmRelation.referencedLayer()->getFeatures(
QgsFeatureRequest()
.setFilterFids( selectionDlg.selectedFeatures() )
.setSubsetOfAttributes( mNmRelation.referencedFields() ) );
QgsFeature relatedFeature;
QgsFeatureList newFeatures;
// Fields of the linking table
const QgsFields fields = mRelation.referencingLayer()->fields();
// Expression context for the linking table
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext();
QgsAttributeMap linkAttributes;
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mRelation.fieldPairs() )
{
int index = fields.indexOf( fieldPair.first );
linkAttributes.insert( index, mFeature.attribute( fieldPair.second ) );
}
while ( it.nextFeature( relatedFeature ) )
{
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mNmRelation.fieldPairs() )
{
int index = fields.indexOf( fieldPair.first );
linkAttributes.insert( index, relatedFeature.attribute( fieldPair.second ) );
}
const QgsFeature linkFeature = QgsVectorLayerUtils::createFeature( mRelation.referencingLayer(), QgsGeometry(), linkAttributes, &context );
newFeatures << linkFeature;
}
mRelation.referencingLayer()->addFeatures( newFeatures );
QgsFeatureIds ids;
Q_FOREACH ( const QgsFeature &f, newFeatures )
ids << f.id();
mRelation.referencingLayer()->selectByIds( ids );
updateUi();
}
else
{
QMap<int, QVariant> keys;
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mRelation.fieldPairs() )
{
int idx = mRelation.referencingLayer()->fields().lookupField( fieldPair.referencingField() );
QVariant val = mFeature.attribute( fieldPair.referencedField() );
keys.insert( idx, val );
}
Q_FOREACH ( QgsFeatureId fid, selectionDlg.selectedFeatures() )
{
QMapIterator<int, QVariant> it( keys );
while ( it.hasNext() )
{
it.next();
mRelation.referencingLayer()->changeAttributeValue( fid, it.key(), it.value() );
}
}
}
}
}
void QgsRelationEditorWidget::duplicateFeature()
{
QgsVectorLayer *layer = mRelation.referencingLayer();
QgsFeatureIterator fit = layer->getFeatures( QgsFeatureRequest().setFilterFids( mFeatureSelectionMgr->selectedFeatureIds() ) );
QgsFeature f;
while ( fit.nextFeature( f ) )
{
QgsVectorLayerUtils::QgsDuplicateFeatureContext duplicatedFeatureContext;
QgsVectorLayerUtils::duplicateFeature( layer, f, QgsProject::instance(), 0, duplicatedFeatureContext );
}
}
void QgsRelationEditorWidget::deleteFeature( const QgsFeatureId featureid )
{
deleteFeatures( QgsFeatureIds() << featureid );
}
void QgsRelationEditorWidget::deleteSelectedFeatures()
{
deleteFeatures( mFeatureSelectionMgr->selectedFeatureIds() );
}
void QgsRelationEditorWidget::deleteFeatures( const QgsFeatureIds &featureids )
{
QgsVectorLayer *layer = mNmRelation.isValid() ? mNmRelation.referencedLayer() : mRelation.referencingLayer();
layer->deleteFeatures( featureids );
}
void QgsRelationEditorWidget::unlinkFeature( const QgsFeatureId featureid )
{
unlinkFeatures( QgsFeatureIds() << featureid );
}
void QgsRelationEditorWidget::unlinkSelectedFeatures()
{
unlinkFeatures( mFeatureSelectionMgr->selectedFeatureIds() );
}
void QgsRelationEditorWidget::zoomToSelectedFeatures()
{
QgsMapCanvas *c = mEditorContext.mapCanvas();
if ( !c )
return;
c->zoomToFeatureIds(
mNmRelation.isValid() ? mNmRelation.referencedLayer() : mRelation.referencingLayer(),
mFeatureSelectionMgr->selectedFeatureIds()
);
}
void QgsRelationEditorWidget::unlinkFeatures( const QgsFeatureIds &featureids )
{
if ( mNmRelation.isValid() )
{
QgsFeatureIterator selectedIterator = mNmRelation.referencedLayer()->getFeatures(
QgsFeatureRequest()
.setFilterFids( featureids )
.setSubsetOfAttributes( mNmRelation.referencedFields() ) );
QgsFeature f;
QStringList filters;
while ( selectedIterator.nextFeature( f ) )
{
filters << '(' + mNmRelation.getRelatedFeaturesRequest( f ).filterExpression()->expression() + ')';
}
QString filter = QStringLiteral( "(%1) AND (%2)" ).arg(
mRelation.getRelatedFeaturesRequest( mFeature ).filterExpression()->expression(),
filters.join( QStringLiteral( " OR " ) ) );
QgsFeatureIterator linkedIterator = mRelation.referencingLayer()->getFeatures( QgsFeatureRequest()
.setSubsetOfAttributes( QgsAttributeList() )
.setFilterExpression( filter ) );
QgsFeatureIds fids;
while ( linkedIterator.nextFeature( f ) )
{
fids << f.id();
QgsDebugMsgLevel( FID_TO_STRING( f.id() ), 4 );
}
mRelation.referencingLayer()->deleteFeatures( fids );
updateUi();
}
else
{
QMap<int, QgsField> keyFields;
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mRelation.fieldPairs() )
{
int idx = mRelation.referencingLayer()->fields().lookupField( fieldPair.referencingField() );
if ( idx < 0 )
{
QgsDebugMsg( QString( "referencing field %1 not found" ).arg( fieldPair.referencingField() ) );
return;
}
QgsField fld = mRelation.referencingLayer()->fields().at( idx );
keyFields.insert( idx, fld );
}
Q_FOREACH ( QgsFeatureId fid, featureids )
{
QMapIterator<int, QgsField> it( keyFields );
while ( it.hasNext() )
{
it.next();
mRelation.referencingLayer()->changeAttributeValue( fid, it.key(), QVariant( it.value().type() ) );
}
}
}
}
void QgsRelationEditorWidget::toggleEditing( bool state )
{
if ( state )
{
mEditorContext.vectorLayerTools()->startEditing( mRelation.referencingLayer() );
if ( mNmRelation.isValid() )
mEditorContext.vectorLayerTools()->startEditing( mNmRelation.referencedLayer() );
}
else
{
mEditorContext.vectorLayerTools()->stopEditing( mRelation.referencingLayer() );
if ( mNmRelation.isValid() )
mEditorContext.vectorLayerTools()->stopEditing( mNmRelation.referencedLayer() );
}
}
void QgsRelationEditorWidget::saveEdits()
{
mEditorContext.vectorLayerTools()->saveEdits( mRelation.referencingLayer() );
if ( mNmRelation.isValid() )
mEditorContext.vectorLayerTools()->saveEdits( mNmRelation.referencedLayer() );
}
void QgsRelationEditorWidget::onCollapsedStateChanged( bool collapsed )
{
if ( !collapsed )
{
mVisible = true;
updateUi();
}
}
void QgsRelationEditorWidget::updateUi()
{
// If not yet initialized, it is not (yet) visible, so we don't load it to be faster (lazy loading)
// If it is already initialized, it has been set visible before and the currently shown feature is changing
// and the widget needs updating
if ( mVisible )
{
QgsFeatureRequest myRequest = mRelation.getRelatedFeaturesRequest( mFeature );
if ( mNmRelation.isValid() )
{
QgsFeatureIterator it = mRelation.referencingLayer()->getFeatures( myRequest );
QgsFeature fet;
QStringList filters;
while ( it.nextFeature( fet ) )
{
QString filter = mNmRelation.getReferencedFeatureRequest( fet ).filterExpression()->expression();
filters << filter.prepend( '(' ).append( ')' );
}
QgsFeatureRequest nmRequest;
nmRequest.setFilterExpression( filters.join( QStringLiteral( " OR " ) ) );
mDualView->init( mNmRelation.referencedLayer(), nullptr, nmRequest, mEditorContext );
}
else
{
mDualView->init( mRelation.referencingLayer(), nullptr, myRequest, mEditorContext );
}
}
}
bool QgsRelationEditorWidget::showLinkButton() const
{
return mLinkFeatureButton->isVisible();
}
void QgsRelationEditorWidget::setShowLinkButton( bool showLinkButton )
{
mLinkFeatureButton->setVisible( showLinkButton );
}
bool QgsRelationEditorWidget::showUnlinkButton() const
{
return mUnlinkFeatureButton->isVisible();
}
void QgsRelationEditorWidget::setShowUnlinkButton( bool showUnlinkButton )
{
mUnlinkFeatureButton->setVisible( showUnlinkButton );
}
bool QgsRelationEditorWidget::showLabel() const
{
return mShowLabel;
}
void QgsRelationEditorWidget::setShowLabel( bool showLabel )
{
mShowLabel = showLabel;
if ( mShowLabel && mRelation.isValid() )
setTitle( mRelation.name() );
else
setTitle( QString() );
}
void QgsRelationEditorWidget::showContextMenu( QgsActionMenu *menu, const QgsFeatureId fid )
{
if ( mRelation.referencingLayer()->isEditable() )
{
QAction *qAction = nullptr;
qAction = menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionDeleteSelected.svg" ) ), tr( "Delete Feature" ) );
connect( qAction, &QAction::triggered, this, [this, fid]() { deleteFeature( fid ); } );
qAction = menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionUnlink.svg" ) ), tr( "Unlink Feature" ) );
connect( qAction, &QAction::triggered, this, [this, fid]() { unlinkFeature( fid ); } );
}
}