Skip to content

Commit

Permalink
On change of an attribute the documentViewer expression of the extern…
Browse files Browse the repository at this point in the history
…alRessourceWidget depends on, the documentViewer should be updated.

To not be needed to check for all the widget values in the form, the mCurrentFormFeature is introduced that contains all the values that are received on change of the widget values.

Resolves #37678
  • Loading branch information
signedav committed Aug 17, 2020
1 parent 4e79531 commit 6be231b
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 6 deletions.
8 changes: 8 additions & 0 deletions python/gui/auto_generated/qgsattributeform.sip.in
Expand Up @@ -44,6 +44,14 @@ class QgsAttributeForm : QWidget

const QgsFeature &feature();

const QgsFeature &currentFormFeature();
%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
Expand Down
5 changes: 5 additions & 0 deletions python/gui/auto_generated/qgsexternalresourcewidget.sip.in
Expand Up @@ -133,6 +133,11 @@ is set to QgsFileWidget.RelativeDefaultPath.
%Docstring
Configures the base path which should be used if the relativeStorage property
is set to QgsFileWidget.RelativeDefaultPath.
%End

void reloadDocument();
%Docstring
Reloads the document from the path of the filewidget
%End

signals:
Expand Down
29 changes: 28 additions & 1 deletion src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp
Expand Up @@ -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() )
{
Expand Down Expand Up @@ -112,15 +112,25 @@ void QgsExternalResourceWidgetWrapper::setFeature( const QgsFeature &feature )
dvc = QgsExternalResourceWidget::NoContent;
}
mQgsWidget->setDocumentViewerContent( dvc );
mQgsWidget->reloadDocument();
}
}
}
}

void QgsExternalResourceWidgetWrapper::setFeature( const QgsFeature &feature )
{
updateProperties( feature );
QgsEditorWidgetWrapper::setFeature( feature );
}

QWidget *QgsExternalResourceWidgetWrapper::createWidget( QWidget *parent )
{
mForm = qobject_cast<QgsAttributeForm *>( parent );

if ( mForm )
connect( mForm, &QgsAttributeForm::widgetValueChanged, this, &QgsExternalResourceWidgetWrapper::widgetValueChanged );

return new QgsExternalResourceWidget( parent );
}

Expand Down Expand Up @@ -252,6 +262,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 )
Expand Down
15 changes: 15 additions & 0 deletions src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.h
Expand Up @@ -23,6 +23,7 @@ class QLineEdit;

#include "qgseditorwidgetwrapper.h"
#include "qgis_gui.h"
#include "qgsattributeform.h"

SIP_NO_FILE

Expand Down Expand Up @@ -58,6 +59,7 @@ class GUI_EXPORT QgsExternalResourceWidgetWrapper : public QgsEditorWidgetWrappe
public:
QVariant value() const override;
void showIndeterminateState() override;
void updateProperties( const QgsFeature &feature );

protected:
QWidget *createWidget( QWidget *parent ) override;
Expand All @@ -68,12 +70,25 @@ 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;

QLineEdit *mLineEdit = nullptr;
QLabel *mLabel = nullptr;
QgsAttributeForm *mForm = nullptr;
QgsExternalResourceWidget *mQgsWidget = nullptr;
};

Expand Down
11 changes: 7 additions & 4 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -281,6 +281,7 @@ void QgsAttributeForm::setFeature( const QgsFeature &feature )
{
mIsSettingFeature = true;
mFeature = feature;
mCurrentFormFeature = feature;

switch ( mMode )
{
Expand Down Expand Up @@ -871,6 +872,8 @@ void QgsAttributeForm::onAttributeChanged( const QVariant &value, const QVariant
if ( mValuesInitialized )
mDirty = true;

mCurrentFormFeature.setAttribute( eww->field().name(), value );

switch ( mMode )
{
case QgsAttributeEditorContext::SingleEditMode:
Expand Down Expand Up @@ -956,7 +959,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:
Expand Down Expand Up @@ -1038,7 +1041,7 @@ void QgsAttributeForm::updateLabels()
if ( ! mLabelDataDefinedProperties.isEmpty() )
{
QgsFeature currentFeature;
if ( currentFormFeature( currentFeature ) )
if ( currentFormValuesFeature( currentFeature ) )
{
QgsExpressionContext context = createExpressionContext( currentFeature );

Expand All @@ -1056,7 +1059,7 @@ void QgsAttributeForm::updateLabels()
}
}

bool QgsAttributeForm::currentFormFeature( QgsFeature &feature )
bool QgsAttributeForm::currentFormValuesFeature( QgsFeature &feature )
{
bool rc = true;
feature = QgsFeature( mFeature );
Expand Down Expand Up @@ -2438,7 +2441,7 @@ void QgsAttributeForm::updateJoinedFields( const QgsEditorWidgetWrapper &eww )
QgsField field = eww.layer()->fields().field( eww.fieldIdx() );
QList<const QgsVectorLayerJoinInfo *> 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" );
Expand Down
11 changes: 10 additions & 1 deletion src/gui/qgsattributeform.h
Expand Up @@ -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
*/
const QgsFeature &currentFormFeature() { return mCurrentFormFeature; }

/**
* Displays a warning message in the form message bar
* \param message message string
Expand Down Expand Up @@ -403,14 +411,15 @@ 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<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );

QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context );

QgsVectorLayer *mLayer = nullptr;
QgsFeature mFeature;
QgsFeature mCurrentFormFeature;
QgsMessageBar *mMessageBar = nullptr;
bool mOwnsMessageBar;
QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsexternalresourcewidget.cpp
Expand Up @@ -194,6 +194,11 @@ void QgsExternalResourceWidget::setDefaultRoot( const QString &defaultRoot )
mDefaultRoot = defaultRoot;
}

void QgsExternalResourceWidget::reloadDocument()
{
loadDocument( mFileWidget->filePath() );
}

QgsFileWidget::RelativeStorage QgsExternalResourceWidget::relativeStorage() const
{
return mRelativeStorage;
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsexternalresourcewidget.h
Expand Up @@ -143,6 +143,11 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget
*/
void setDefaultRoot( const QString &defaultRoot );

/**
* Reloads the document from the path of the filewidget
*/
void reloadDocument();

signals:
//! emitteed as soon as the current document changes
void valueChanged( const QString & );
Expand Down

0 comments on commit 6be231b

Please sign in to comment.