@@ -44,6 +44,7 @@ class TestQgsAttributeForm : public QObject
44
44
void testOKButtonStatus ();
45
45
void testDynamicForm ();
46
46
void testConstraintsOnJoinedFields ();
47
+ void testEditableJoin ();
47
48
};
48
49
49
50
void TestQgsAttributeForm::initTestCase ()
@@ -511,5 +512,124 @@ void TestQgsAttributeForm::testConstraintsOnJoinedFields()
511
512
QCOMPARE ( label->text (), " layerB_" + warningLabel );
512
513
}
513
514
515
+ void TestQgsAttributeForm::testEditableJoin ()
516
+ {
517
+ // make temporary layers
518
+ QString defA = QStringLiteral ( " Point?field=id_a:integer" );
519
+ QgsVectorLayer *layerA = new QgsVectorLayer ( defA, QStringLiteral ( " layerA" ), QStringLiteral ( " memory" ) );
520
+
521
+ QString defB = QStringLiteral ( " Point?field=id_b:integer&field=col0:integer" );
522
+ QgsVectorLayer *layerB = new QgsVectorLayer ( defB, QStringLiteral ( " layerB" ), QStringLiteral ( " memory" ) );
523
+
524
+ QString defC = QStringLiteral ( " Point?field=id_c:integer&field=col0:integer" );
525
+ QgsVectorLayer *layerC = new QgsVectorLayer ( defC, QStringLiteral ( " layerC" ), QStringLiteral ( " memory" ) );
526
+
527
+ // join configuration
528
+ QgsVectorLayerJoinInfo infoJoinAB;
529
+ infoJoinAB.setTargetFieldName ( " id_a" );
530
+ infoJoinAB.setJoinLayer ( layerB );
531
+ infoJoinAB.setJoinFieldName ( " id_b" );
532
+ infoJoinAB.setDynamicFormEnabled ( true );
533
+ infoJoinAB.setEditable ( true );
534
+
535
+ layerA->addJoin ( infoJoinAB );
536
+
537
+ QgsVectorLayerJoinInfo infoJoinAC;
538
+ infoJoinAC.setTargetFieldName ( " id_a" );
539
+ infoJoinAC.setJoinLayer ( layerC );
540
+ infoJoinAC.setJoinFieldName ( " id_c" );
541
+ infoJoinAC.setDynamicFormEnabled ( true );
542
+ infoJoinAC.setEditable ( false );
543
+
544
+ layerA->addJoin ( infoJoinAC );
545
+
546
+ // add features for main layer
547
+ QgsFeature ftA ( layerA->fields () );
548
+ ftA.setAttribute ( QStringLiteral ( " id_a" ), 31 );
549
+ layerA->startEditing ();
550
+ layerA->addFeature ( ftA );
551
+ layerA->commitChanges ();
552
+
553
+ // add features for joined layers
554
+ QgsFeature ft0B ( layerB->fields () );
555
+ ft0B.setAttribute ( QStringLiteral ( " id_b" ), 31 );
556
+ ft0B.setAttribute ( QStringLiteral ( " col0" ), 11 );
557
+ layerB->startEditing ();
558
+ layerB->addFeature ( ft0B );
559
+ layerB->commitChanges ();
560
+
561
+ QgsFeature ft0C ( layerC->fields () );
562
+ ft0C.setAttribute ( QStringLiteral ( " id_c" ), 31 );
563
+ ft0C.setAttribute ( QStringLiteral ( " col0" ), 13 );
564
+ layerC->startEditing ();
565
+ layerC->addFeature ( ft0C );
566
+ layerC->commitChanges ();
567
+
568
+ // start editing layers
569
+ layerA->startEditing ();
570
+ layerB->startEditing ();
571
+ layerC->startEditing ();
572
+
573
+ // build a form with feature A
574
+ ftA = layerA->getFeature ( 1 );
575
+
576
+ QgsAttributeForm form ( layerA );
577
+ form.setMode ( QgsAttributeForm::SingleEditMode );
578
+ form.setFeature ( ftA );
579
+
580
+ for ( int i = 0 ; i < ftA.fields ().count (); i++ )
581
+ std::cout << " - " << ftA.fields ().field ( i ).name ().toStdString () << " : " << ftA.attribute ( i ).toString ().toStdString () << std::endl;
582
+
583
+ // change layerA join id field to join with layerB and layerC
584
+ QgsEditorWidgetWrapper *ww = nullptr ;
585
+
586
+ ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets [0 ] );
587
+ QCOMPARE ( ww->field ().name (), QString ( " id_a" ) );
588
+ QCOMPARE ( ww->value (), QVariant ( 31 ) );
589
+
590
+ ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets [1 ] );
591
+ QCOMPARE ( ww->field ().name (), QString ( " layerB_col0" ) );
592
+ QCOMPARE ( ww->value (), QVariant ( 11 ) );
593
+
594
+ ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets [2 ] );
595
+ QCOMPARE ( ww->field ().name (), QString ( " layerC_col0" ) );
596
+ QCOMPARE ( ww->value (), QVariant ( 13 ) );
597
+
598
+ // test if widget is enabled for layerA
599
+ ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets [0 ] );
600
+ QCOMPARE ( ww->widget ()->isEnabled (), true );
601
+
602
+ // test if widget is enabled for layerB
603
+ ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets [1 ] );
604
+ QCOMPARE ( ww->widget ()->isEnabled (), true );
605
+
606
+ // test if widget is disabled for layerC
607
+ ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets [2 ] );
608
+ QCOMPARE ( ww->widget ()->isEnabled (), false );
609
+
610
+ // change attributes
611
+ form.changeAttribute ( " layerB_col0" , QVariant ( 333 ) );
612
+ form.changeAttribute ( " layerC_col0" , QVariant ( 444 ) );
613
+ form.save ();
614
+
615
+ // commit changes
616
+ layerA->commitChanges ();
617
+ layerB->commitChanges ();
618
+ layerC->commitChanges ();
619
+
620
+ // check attributes
621
+ ft0B = layerB->getFeature ( 1 );
622
+ QCOMPARE ( ft0B.attribute ( " col0" ), QVariant ( 333 ) );
623
+
624
+ ft0C = layerC->getFeature ( 1 );
625
+ QCOMPARE ( ft0C.attribute ( " col0" ), QVariant ( 13 ) );
626
+
627
+ // clean
628
+ delete layerA;
629
+ delete layerB;
630
+ delete layerC;
631
+ }
632
+
633
+
514
634
QGSTEST_MAIN ( TestQgsAttributeForm )
515
635
#include " testqgsattributeform.moc"
0 commit comments