[ux][field_calculator] Deactivate field editing for RO layers

If layer is read-only deactivate the provider field editing
functionality.

Fix #63111
This commit is contained in:
Alessandro Pasotti 2025-09-25 09:07:29 +02:00 committed by github-actions[bot]
parent c324704f91
commit cb7a745c8a
2 changed files with 14 additions and 3 deletions

View File

@ -66,9 +66,10 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer *vl, QWidget *parent )
if ( !dataProvider )
return;
const bool layerIsReadOnly { vl->readOnly() };
const Qgis::VectorProviderCapabilities caps = dataProvider->capabilities();
mCanAddAttribute = caps & Qgis::VectorProviderCapability::AddAttributes;
mCanChangeAttributeValue = caps & Qgis::VectorProviderCapability::ChangeAttributeValues;
mCanAddAttribute = !layerIsReadOnly && ( caps & Qgis::VectorProviderCapability::AddAttributes );
mCanChangeAttributeValue = !layerIsReadOnly && ( caps & Qgis::VectorProviderCapability::ChangeAttributeValues );
QgsExpressionContext expContext( QgsExpressionContextUtils::globalProjectLayerScopes( mVectorLayer ) );
@ -218,6 +219,16 @@ void QgsFieldCalculator::calculate()
}
else
{
// If the layer is read-only, show an error
// This should never happen because the read-only state is now checked at construction time
// and the GUI is updated accordingly, but I'm leaving the safeguard here as a remainder just
// in case the GUI will be redesigned in the future.
if ( mVectorLayer->readOnly() )
{
QMessageBox::critical( nullptr, tr( "Read-only layer" ), tr( "The layer was marked read-only in the project properties and cannot be edited." ) );
return;
}
if ( !mVectorLayer->isEditable() )
mVectorLayer->startEditing();

View File

@ -69,7 +69,7 @@ class GUI_EXPORT QgsFieldCalculator : public QDialog, private Ui::QgsFieldCalcul
void setPrecisionMinMax();
void showHelp();
void calculate();
//! show the given message in the Plugin Manager internal message bar
//! show the given message in the dialog internal message bar
void pushMessage( const QString &text, Qgis::MessageLevel level = Qgis::MessageLevel::Info, int duration = -1 );
private: