diff --git a/python/gui/auto_generated/qgsattributeform.sip.in b/python/gui/auto_generated/qgsattributeform.sip.in index 2ee4d02c1dc..02c9b2369ab 100644 --- a/python/gui/auto_generated/qgsattributeform.sip.in +++ b/python/gui/auto_generated/qgsattributeform.sip.in @@ -44,6 +44,14 @@ class QgsAttributeForm : QWidget const QgsFeature &feature(); + QgsFeature currentFormFeature() const; +%Docstring +Returns the feature that is currently displayed in the form with all +the changes received on editing the values in the widgets. + +.. versionadded:: 3.16 +%End + void displayWarning( const QString &message ); %Docstring Displays a warning message in the form message bar diff --git a/src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp b/src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp index 0a70d9e6da7..f097c10f5ed 100644 --- a/src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp +++ b/src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp @@ -77,7 +77,7 @@ bool QgsExternalResourceWidgetWrapper::valid() const return mLineEdit || mLabel || mQgsWidget; } -void QgsExternalResourceWidgetWrapper::setFeature( const QgsFeature &feature ) +void QgsExternalResourceWidgetWrapper::updateProperties( const QgsFeature &feature ) { if ( mQgsWidget && mPropertyCollection.hasActiveProperties() ) { @@ -115,12 +115,21 @@ void QgsExternalResourceWidgetWrapper::setFeature( const QgsFeature &feature ) } } } +} +void QgsExternalResourceWidgetWrapper::setFeature( const QgsFeature &feature ) +{ + updateProperties( feature ); QgsEditorWidgetWrapper::setFeature( feature ); } QWidget *QgsExternalResourceWidgetWrapper::createWidget( QWidget *parent ) { + mForm = qobject_cast( parent ); + + if ( mForm ) + connect( mForm, &QgsAttributeForm::widgetValueChanged, this, &QgsExternalResourceWidgetWrapper::widgetValueChanged ); + return new QgsExternalResourceWidget( parent ); } @@ -252,6 +261,23 @@ void QgsExternalResourceWidgetWrapper::setEnabled( bool enabled ) mQgsWidget->setReadOnly( !enabled ); } +void QgsExternalResourceWidgetWrapper::widgetValueChanged( const QString &attribute, const QVariant &newValue, bool attributeChanged ) +{ + Q_UNUSED( newValue ); + if ( attributeChanged ) + { + QgsExpression documentViewerContentExp = QgsExpression( mPropertyCollection.property( QgsEditorWidgetWrapper::DocumentViewerContent ).expressionString() ); + QgsExpression rootPathExp = QgsExpression( mPropertyCollection.property( QgsEditorWidgetWrapper::RootPath ).expressionString() ); + + if ( documentViewerContentExp.referencedColumns().contains( attribute ) || + rootPathExp.referencedColumns().contains( attribute ) ) + { + QgsFeature feature = mForm->currentFormFeature(); + updateProperties( feature ); + } + } +} + void QgsExternalResourceWidgetWrapper::updateConstraintWidgetStatus() { if ( mLineEdit ) diff --git a/src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.h b/src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.h index 8fc2b7a65b2..3ec54721c32 100644 --- a/src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.h +++ b/src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.h @@ -23,6 +23,7 @@ class QLineEdit; #include "qgseditorwidgetwrapper.h" #include "qgis_gui.h" +#include "qgsattributeform.h" SIP_NO_FILE @@ -68,12 +69,26 @@ class GUI_EXPORT QgsExternalResourceWidgetWrapper : public QgsEditorWidgetWrappe void setFeature( const QgsFeature &feature ) override; void setEnabled( bool enabled ) override; + /** + * Will be called when a value in the current edited form or table row + * changes + * + * \param attribute The name of the attribute that changed. + * \param newValue The new value of the attribute. + * \param attributeChanged If TRUE, it corresponds to an actual change of the feature attribute + * \since QGIS 3.16 + */ + void widgetValueChanged( const QString &attribute, const QVariant &newValue, bool attributeChanged ); + + private: void updateValues( const QVariant &value, const QVariantList & = QVariantList() ) override; void updateConstraintWidgetStatus() override; + void updateProperties( const QgsFeature &feature ); QLineEdit *mLineEdit = nullptr; QLabel *mLabel = nullptr; + QgsAttributeForm *mForm = nullptr; QgsExternalResourceWidget *mQgsWidget = nullptr; }; diff --git a/src/gui/qgsattributeform.cpp b/src/gui/qgsattributeform.cpp index b1d4348dc4d..e42b037724c 100644 --- a/src/gui/qgsattributeform.cpp +++ b/src/gui/qgsattributeform.cpp @@ -282,6 +282,7 @@ void QgsAttributeForm::setFeature( const QgsFeature &feature ) { mIsSettingFeature = true; mFeature = feature; + mCurrentFormFeature = feature; switch ( mMode ) { @@ -872,6 +873,8 @@ void QgsAttributeForm::onAttributeChanged( const QVariant &value, const QVariant if ( mValuesInitialized ) mDirty = true; + mCurrentFormFeature.setAttribute( eww->field().name(), value ); + switch ( mMode ) { case QgsAttributeEditorContext::SingleEditMode: @@ -957,7 +960,7 @@ void QgsAttributeForm::updateConstraints( QgsEditorWidgetWrapper *eww ) { // get the current feature set in the form QgsFeature ft; - if ( currentFormFeature( ft ) ) + if ( currentFormValuesFeature( ft ) ) { // if the layer is NOT being edited then we only check layer based constraints, and not // any constraints enforced by the provider. Because: @@ -1039,7 +1042,7 @@ void QgsAttributeForm::updateLabels() if ( ! mLabelDataDefinedProperties.isEmpty() ) { QgsFeature currentFeature; - if ( currentFormFeature( currentFeature ) ) + if ( currentFormValuesFeature( currentFeature ) ) { QgsExpressionContext context = createExpressionContext( currentFeature ); @@ -1057,7 +1060,7 @@ void QgsAttributeForm::updateLabels() } } -bool QgsAttributeForm::currentFormFeature( QgsFeature &feature ) +bool QgsAttributeForm::currentFormValuesFeature( QgsFeature &feature ) { bool rc = true; feature = QgsFeature( mFeature ); @@ -2460,7 +2463,7 @@ void QgsAttributeForm::updateJoinedFields( const QgsEditorWidgetWrapper &eww ) QgsField field = eww.layer()->fields().field( eww.fieldIdx() ); QList infos = eww.layer()->joinBuffer()->joinsWhereFieldIsId( field ); - if ( infos.count() == 0 || !currentFormFeature( formFeature ) ) + if ( infos.count() == 0 || !currentFormValuesFeature( formFeature ) ) return; const QString hint = tr( "No feature joined" ); diff --git a/src/gui/qgsattributeform.h b/src/gui/qgsattributeform.h index e6319573a8e..6c250332bb4 100644 --- a/src/gui/qgsattributeform.h +++ b/src/gui/qgsattributeform.h @@ -75,6 +75,14 @@ class GUI_EXPORT QgsAttributeForm : public QWidget const QgsFeature &feature() { return mFeature; } + /** + * Returns the feature that is currently displayed in the form with all + * the changes received on editing the values in the widgets. + * + * \since QGIS 3.16 + */ + QgsFeature currentFormFeature() const { return mCurrentFormFeature; } + /** * Displays a warning message in the form message bar * \param message message string @@ -403,7 +411,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget void updateContainersVisibility(); void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww ); void updateLabels(); - bool currentFormFeature( QgsFeature &feature ); + bool currentFormValuesFeature( QgsFeature &feature ); bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ); QList constraintDependencies( QgsEditorWidgetWrapper *w ); @@ -411,6 +419,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget QgsVectorLayer *mLayer = nullptr; QgsFeature mFeature; + QgsFeature mCurrentFormFeature; QgsMessageBar *mMessageBar = nullptr; bool mOwnsMessageBar; QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr; diff --git a/src/gui/qgsexternalresourcewidget.cpp b/src/gui/qgsexternalresourcewidget.cpp index e06d7a44251..b9779b005a0 100644 --- a/src/gui/qgsexternalresourcewidget.cpp +++ b/src/gui/qgsexternalresourcewidget.cpp @@ -99,7 +99,9 @@ QgsExternalResourceWidget::DocumentViewerContent QgsExternalResourceWidget::docu void QgsExternalResourceWidget::setDocumentViewerContent( QgsExternalResourceWidget::DocumentViewerContent content ) { mDocumentViewerContent = content; - updateDocumentViewer(); + if ( mDocumentViewerContent != Image ) + updateDocumentViewer(); + loadDocument( mFileWidget->filePath() ); } int QgsExternalResourceWidget::documentViewerHeight() const