From e8cb8fd5f01deb75eb6056e75ca3595cbb91de53 Mon Sep 17 00:00:00 2001 From: Peter Petrik Date: Fri, 27 Oct 2023 12:07:21 +0200 Subject: [PATCH] fix #2879 photo crash on project switch --- app/attributes/attributecontroller.cpp | 73 ++++++++++++++------------ app/qml/FormsStackManager.qml | 14 ++++- app/qml/form/FormWrapper.qml | 2 +- 3 files changed, 52 insertions(+), 37 deletions(-) diff --git a/app/attributes/attributecontroller.cpp b/app/attributes/attributecontroller.cpp index e9344628f7..a4ef8b1764 100644 --- a/app/attributes/attributecontroller.cpp +++ b/app/attributes/attributecontroller.cpp @@ -359,59 +359,59 @@ void AttributeController::clearAll() void AttributeController::updateOnLayerChange() { clearAll(); + QgsVectorLayer *layer = mFeatureLayerPair.layer(); + if ( !layer ) + return; // 1) DATA - QgsVectorLayer *layer = mFeatureLayerPair.layer(); - if ( layer ) + if ( layer->editFormConfig().layout() == Qgis::AttributeFormLayout::DragAndDrop ) { - if ( layer->editFormConfig().layout() == Qgis::AttributeFormLayout::DragAndDrop ) + QgsAttributeEditorContainer *root = layer->editFormConfig().invisibleRootContainer(); + if ( root->columnCount() > 1 ) { - QgsAttributeEditorContainer *root = layer->editFormConfig().invisibleRootContainer(); - if ( root->columnCount() > 1 ) - { - qDebug() << "root tab in manual config has multiple columns. not supported on mobile devices!"; - root->setColumnCount( 1 ); - } + qDebug() << "root tab in manual config has multiple columns. not supported on mobile devices!"; + root->setColumnCount( 1 ); + } - mHasTabs = allowTabs( root ); - if ( mHasTabs ) + mHasTabs = allowTabs( root ); + if ( mHasTabs ) + { + for ( QgsAttributeEditorElement *element : root->children() ) { - for ( QgsAttributeEditorElement *element : root->children() ) + if ( element->type() == Qgis::AttributeEditorType::Container ) { - if ( element->type() == Qgis::AttributeEditorType::Container ) + QgsAttributeEditorContainer *container = static_cast( element ); + if ( container->columnCount() > 1 ) { - QgsAttributeEditorContainer *container = static_cast( element ); - if ( container->columnCount() > 1 ) - { - qDebug() << "tab " << container->name() << " in manual config has multiple columns. not supported on mobile devices!"; - container->setColumnCount( 1 ); - } - createTab( container ); + qDebug() << "tab " << container->name() << " in manual config has multiple columns. not supported on mobile devices!"; + container->setColumnCount( 1 ); } + createTab( container ); } } - else - { - createTab( root ); - } } else { - // Auto-Generated Layout - // We create fake root tab - QgsAttributeEditorContainer *tab = autoLayoutTabContainer(); - - // We need to look for relations and include them into form, - // in auto-generated layout they are not included in form config - discoverRelations( tab ); - - createTab( tab ); + createTab( root ); } + } + else + { + // Auto-Generated Layout + // We create fake root tab + QgsAttributeEditorContainer *tab = autoLayoutTabContainer(); - if ( mRememberAttributesController ) - mRememberAttributesController->storeLayerFields( layer ); + // We need to look for relations and include them into form, + // in auto-generated layout they are not included in form config + discoverRelations( tab ); + + createTab( tab ); } + if ( mRememberAttributesController ) + mRememberAttributesController->storeLayerFields( layer ); + + // 2) MODELS // for all other models, ownership is managed by Qt parent system AttributeTabModel *tabModel = new AttributeTabModel( mAttributeTabProxyModel.get(), this, mTabItems.size() ); @@ -462,6 +462,9 @@ void AttributeController::updateOnLayerChange() void AttributeController::updateOnFeatureChange() { + if (!mFeatureLayerPair.layer()) + return; + const QgsFeature feature = mFeatureLayerPair.feature(); QMap>::iterator formItemsIterator = mFormItems.begin(); diff --git a/app/qml/FormsStackManager.qml b/app/qml/FormsStackManager.qml index a76a58f123..dfbe36f9f2 100644 --- a/app/qml/FormsStackManager.qml +++ b/app/qml/FormsStackManager.qml @@ -62,7 +62,8 @@ Item { signal createLinkedFeatureRequested( var targetLayer, var parentPair ) signal stakeoutFeature( var feature ); - function openForm( pair, formState, panelState ) { + function openForm( pair, formState, panelState ) + { if ( formsStack.depth === 0 ) { let props = { @@ -137,9 +138,20 @@ Item { } function reload() { + // For some reason some forms could be hanging around + // for some time with dangling layer pointers even after + // formsStack.clear() is called + // https://github.com/MerginMaps/input/issues/2879 + for ( let i = 0; i <= formsStack.depth; i++ ) { + let form = formsStack.get( i ) + form.featureLayerPair = __inputUtils.createFeatureLayerPair(null, __inputUtils.emptyGeometry(), __variablesManager ) + form.project = null + } + formsStack.clear() // removes all objects thanks to Qt parent system } + function openLinkedFeature( linkedFeature ) { let props = { featureLayerPair: linkedFeature, diff --git a/app/qml/form/FormWrapper.qml b/app/qml/form/FormWrapper.qml index 26e5aed6b8..8ccec12306 100644 --- a/app/qml/form/FormWrapper.qml +++ b/app/qml/form/FormWrapper.qml @@ -34,7 +34,7 @@ Item { property real previewHeight property real panelHeight - property bool isReadOnly: featureLayerPair ? featureLayerPair.layer.readOnly : false + property bool isReadOnly: featureLayerPair && featureLayerPair.layer ? featureLayerPair.layer.readOnly : false signal closed() signal editGeometry( var pair )