@@ -215,9 +215,9 @@ void QgsAttributesFormProperties::initInitPython()
215
215
216
216
void QgsAttributesFormProperties::loadAttributeTypeDialog ()
217
217
{
218
- QTreeWidgetItem *currentItem = mAvailableWidgetsTree -> currentItem ();
219
-
220
- if ( ! currentItem )
218
+ // check if item or field with the items name for some reason not available anymore
219
+ if ( ! mAvailableWidgetsTree -> currentItem () ||
220
+ getFieldIndexByName ( mAvailableWidgetsTree -> currentItem ()-> data ( 0 , FieldNameRole ). toString () ) < 0 )
221
221
mAttributeTypeDialog ->setEnabled ( false );
222
222
else
223
223
{
@@ -227,16 +227,13 @@ void QgsAttributesFormProperties::loadAttributeTypeDialog()
227
227
228
228
mAttributeTypeDialog ->setEnabled ( true );
229
229
230
- // AttributeTypeDialog
231
-
230
+ // AttributeTypeDialog delete and recreate
232
231
mAttributeTypeFrame ->layout ()->removeWidget ( mAttributeTypeDialog );
233
232
delete mAttributeTypeDialog ;
234
-
235
-
236
- //
237
233
mAttributeTypeDialog = new QgsAttributeTypeDialog ( mLayer , index, mAttributeTypeFrame );
234
+
238
235
mAttributeTypeDialog ->setAlias ( cfg.mAlias );
239
- mAttributeTypeDialog ->setComment ( mLayer -> fields (). at ( index ). comment () );
236
+ mAttributeTypeDialog ->setComment ( cfg. mComment );
240
237
mAttributeTypeDialog ->setFieldEditable ( cfg.mEditable );
241
238
mAttributeTypeDialog ->setLabelOnTop ( cfg.mLabelOnTop );
242
239
mAttributeTypeDialog ->setNotNull ( cfg.mConstraints & QgsFieldConstraints::ConstraintNotNull );
@@ -393,24 +390,6 @@ void QgsAttributesFormProperties::storeAttributeRelationEdit()
393
390
}
394
391
}
395
392
396
- QgsAttributesFormProperties::FieldConfig QgsAttributesFormProperties::configForChild ( int index )
397
- {
398
- QString fieldName = mLayer ->fields ().at ( index ).name ();
399
-
400
- QTreeWidgetItemIterator itemIt ( mAvailableWidgetsTree );
401
- while ( *itemIt )
402
- {
403
- QTreeWidgetItem *item = *itemIt;
404
- if ( item->data ( 0 , FieldNameRole ).toString () == fieldName )
405
- return item->data ( 0 , FieldConfigRole ).value <FieldConfig>();
406
- ++itemIt;
407
- }
408
-
409
- // Should never get here
410
- Q_ASSERT ( false );
411
- return FieldConfig ();
412
- }
413
-
414
393
QgsAttributesFormProperties::RelationConfig QgsAttributesFormProperties::configForRelation ( const QString &relationName )
415
394
{
416
395
QTreeWidgetItemIterator itemIt ( mAvailableWidgetsTree );
@@ -646,6 +625,18 @@ void QgsAttributesFormProperties::pbnSelectEditForm_clicked()
646
625
mEditFormLineEdit ->setText ( uifilename );
647
626
}
648
627
628
+ int QgsAttributesFormProperties::getFieldIndexByName ( const QString &name )
629
+ {
630
+ for ( int idx = 0 ; idx < mLayer ->fields ().size (); idx++ )
631
+ {
632
+ // only save the files that are still existing in fields
633
+ if ( mLayer ->fields ().at ( idx ).name () == name )
634
+ return idx;
635
+ }
636
+
637
+ return -1 ;
638
+ }
639
+
649
640
void QgsAttributesFormProperties::apply ()
650
641
{
651
642
@@ -656,15 +647,15 @@ void QgsAttributesFormProperties::apply()
656
647
657
648
QTreeWidgetItem *fieldContainer = mAvailableWidgetsTree ->invisibleRootItem ()->child ( 0 );
658
649
659
- int idx;
660
-
661
650
for ( int i = 0 ; i < fieldContainer->childCount (); i++ )
662
651
{
663
652
QTreeWidgetItem *fieldItem = fieldContainer->child ( i );
664
- idx = fieldContainer->indexOfChild ( fieldItem );
653
+ FieldConfig cfg = fieldItem->data ( 0 , FieldConfigRole ).value <FieldConfig>();
654
+
655
+ int idx = getFieldIndexByName ( fieldItem->data ( 0 , FieldNameRole ).toString () );
665
656
666
- QString name = mLayer -> fields (). at ( idx ). name ();
667
- FieldConfig cfg = configForChild ( idx ) ;
657
+ // continue in case field does not exist anymore
658
+ if ( idx < 0 ) continue ;
668
659
669
660
editFormConfig.setReadOnly ( idx, !cfg.mEditable );
670
661
editFormConfig.setLabelOnTop ( idx, cfg.mLabelOnTop );
@@ -697,7 +688,6 @@ void QgsAttributesFormProperties::apply()
697
688
}
698
689
699
690
mLayer ->setFieldAlias ( idx, cfg.mAlias );
700
- // wo?? ( idx, cfg.mComment );
701
691
}
702
692
703
693
// tabs and groups
@@ -736,28 +726,6 @@ void QgsAttributesFormProperties::apply()
736
726
editFormConfig.setWidgetConfig ( itemData.name (), cfg );
737
727
}
738
728
739
- /*
740
-
741
- for ( int i = 0; i < mRelationsList->rowCount(); ++i )
742
- {
743
- QVariantMap cfg;
744
-
745
- QComboBox *cb = qobject_cast<QComboBox *>( mRelationsList->cellWidget( i, RelNmCol ) );
746
- QVariant otherRelation = cb->currentData();
747
-
748
- if ( otherRelation.isValid() )
749
- {
750
- cfg[QStringLiteral( "nm-rel" )] = otherRelation.toString();
751
- }
752
-
753
- DesignerTreeItemData itemData = mRelationsList->item( i, RelNameCol )->data( DesignerTreeRole ).value<DesignerTreeItemData>();
754
-
755
- QString relationName = itemData.name();
756
-
757
- editFormConfig.setWidgetConfig( relationName, cfg );
758
- }
759
- */
760
-
761
729
mLayer ->setEditFormConfig ( editFormConfig );
762
730
}
763
731
@@ -780,6 +748,7 @@ QgsAttributesFormProperties::FieldConfig::FieldConfig( QgsVectorLayer *layer, in
780
748
: mButton( nullptr )
781
749
{
782
750
mAlias = layer->fields ().at ( idx ).alias ();
751
+ mComment = layer->fields ().at ( idx ).comment ();
783
752
mEditable = !layer->editFormConfig ().readOnly ( idx );
784
753
mEditableEnabled = layer->fields ().fieldOrigin ( idx ) != QgsFields::OriginJoin
785
754
&& layer->fields ().fieldOrigin ( idx ) != QgsFields::OriginExpression;
0 commit comments