bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes, bool showModal ) { if ( !mLayer || !mLayer->isEditable() ) return false; QgsVectorDataProvider *provider = mLayer->dataProvider(); QSettings settings; bool reuseLastValues = settings.value( "/qgis/digitizing/reuseLastValues", false ).toBool(); QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) ); // add the fields to the QgsFeature const QgsFields& fields = mLayer->fields(); mFeature->initAttributes( fields.count() ); for ( int idx = 0; idx < fields.count(); ++idx ) { QVariant v; if ( defaultAttributes.contains( idx ) ) { v = defaultAttributes.value( idx ); } else if ( reuseLastValues && sLastUsedValues.contains( mLayer ) && sLastUsedValues[ mLayer ].contains( idx ) ) { v = sLastUsedValues[ mLayer ][idx]; } else { v = provider->defaultValue( idx ); } mFeature->setAttribute( idx, v ); } //show the dialog to enter attribute values //only show if enabled in settings and layer has fields bool isDisabledAttributeValuesDlg = ( fields.count() == 0 ) || settings.value( "/qgis/digitizing/disable_enter_attribute_values_dialog", false ).toBool(); // override application-wide setting with any layer setting switch ( mLayer->editFormConfig()->suppress() ) { case QgsEditFormConfig::SuppressOn: isDisabledAttributeValuesDlg = true; break; case QgsEditFormConfig::SuppressOff: isDisabledAttributeValuesDlg = false; break; case QgsEditFormConfig::SuppressDefault: break; } if ( isDisabledAttributeValuesDlg ) { mLayer->beginEditCommand( text() ); mFeatureSaved = mLayer->addFeature( *mFeature ); if ( mFeatureSaved ) mLayer->endEditCommand(); else mLayer->destroyEditCommand(); } else { QgsAttributeDialog *dialog = newDialog( false ); dialog->setIsAddDialog( true ); dialog->setEditCommandMessage( text() ); connect( dialog->attributeForm(), SIGNAL( featureSaved( const QgsFeature & ) ), this, SLOT( onFeatureSaved( const QgsFeature & ) ) ); if ( !showModal ) { setParent( dialog ); // keep dialog until the dialog is closed and destructed dialog->show(); // will also delete the dialog on close (show() is overridden) mFeature = 0; return true; } dialog->setAttribute( Qt::WA_DeleteOnClose ); dialog->exec(); } // Will be set in the onFeatureSaved SLOT return mFeatureSaved; }
bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes ) { if ( !mLayer || !mLayer->isEditable() ) return false; QgsVectorDataProvider *provider = mLayer->dataProvider(); QSettings settings; bool reuseLastValues = settings.value( "/qgis/digitizing/reuseLastValues", false ).toBool(); QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) ); // add the fields to the QgsFeature const QgsFields& fields = mLayer->pendingFields(); mFeature.initAttributes( fields.count() ); for ( int idx = 0; idx < fields.count(); ++idx ) { if ( defaultAttributes.contains( idx ) ) { QgsDebugMsg( QString( "Using specified default %1 for %2" ).arg( defaultAttributes.value( idx ).toString() ).arg( idx ) ); mFeature.setAttribute( idx, defaultAttributes.value( idx ) ); } else if ( reuseLastValues && mLastUsedValues.contains( mLayer ) && mLastUsedValues[ mLayer ].contains( idx ) ) { QgsDebugMsg( QString( "reusing %1 for %2" ).arg( mLastUsedValues[ mLayer ][idx].toString() ).arg( idx ) ); mFeature.setAttribute( idx, mLastUsedValues[ mLayer ][idx] ); } else { mFeature.setAttribute( idx, provider->defaultValue( idx ) ); } } bool res = false; mLayer->beginEditCommand( text() ); // show the dialog to enter attribute values bool isDisabledAttributeValuesDlg = settings.value( "/qgis/digitizing/disable_enter_attribute_values_dialog", false ).toBool(); // override application-wide setting with any layer setting switch ( mLayer->featureFormSuppress() ) { case QgsVectorLayer::SuppressOn: isDisabledAttributeValuesDlg = true; break; case QgsVectorLayer::SuppressOff: isDisabledAttributeValuesDlg = false; break; case QgsVectorLayer::SuppressDefault: break; } if ( isDisabledAttributeValuesDlg ) { res = mLayer->addFeature( mFeature ); } else { QgsAttributes origValues; if ( reuseLastValues ) origValues = mFeature.attributes(); QgsAttributeDialog *dialog = newDialog( false ); if ( dialog->exec() ) { if ( reuseLastValues ) { for ( int idx = 0; idx < fields.count(); ++idx ) { const QgsAttributes &newValues = mFeature.attributes(); if ( origValues[idx] != newValues[idx] ) { QgsDebugMsg( QString( "saving %1 for %2" ).arg( mLastUsedValues[ mLayer ][idx].toString() ).arg( idx ) ); mLastUsedValues[ mLayer ][idx] = newValues[idx]; } } } res = mLayer->addFeature( mFeature ); } else { QgsDebugMsg( "Adding feature to layer failed" ); res = false; } } if ( res ) mLayer->endEditCommand(); else mLayer->destroyEditCommand(); return res; }
bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes ) { if ( !mLayer || !mLayer->isEditable() ) return false; QgsVectorDataProvider *provider = mLayer->dataProvider(); QSettings settings; bool reuseLastValues = settings.value( "/qgis/digitizing/reuseLastValues", false ).toBool(); QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) ); // add the fields to the QgsFeature const QgsFields& fields = mLayer->pendingFields(); mFeature.initAttributes( fields.count() ); for ( int idx = 0; idx < fields.count(); ++idx ) { QVariant v; if ( defaultAttributes.contains( idx ) ) { v = defaultAttributes.value( idx ); } else if ( reuseLastValues && sLastUsedValues.contains( mLayer ) && sLastUsedValues[ mLayer ].contains( idx ) ) { v = sLastUsedValues[ mLayer ][idx]; } else { v = provider->defaultValue( idx ); } mFeature.setAttribute( idx, v ); } // show the dialog to enter attribute values bool isDisabledAttributeValuesDlg = settings.value( "/qgis/digitizing/disable_enter_attribute_values_dialog", false ).toBool(); // override application-wide setting with any layer setting switch ( mLayer->featureFormSuppress() ) { case QgsVectorLayer::SuppressOn: isDisabledAttributeValuesDlg = true; break; case QgsVectorLayer::SuppressOff: isDisabledAttributeValuesDlg = false; break; case QgsVectorLayer::SuppressDefault: break; } if ( isDisabledAttributeValuesDlg ) { mLayer->beginEditCommand( text() ); mFeatureSaved = mLayer->addFeature( mFeature ); if ( mFeatureSaved ) mLayer->endEditCommand(); else mLayer->destroyEditCommand(); } else { QgsAttributeDialog *dialog = newDialog( false ); dialog->setIsAddDialog( true ); dialog->setEditCommandMessage( text() ); connect( dialog->attributeForm(), SIGNAL( featureSaved( QgsFeature ) ), this, SLOT( onFeatureSaved( QgsFeature ) ) ); dialog->exec(); } // Will be set in the onFeatureSaved SLOT return mFeatureSaved; }