Skip to content

Commit

Permalink
Merge pull request #38318 from signedav/attachement-expression
Browse files Browse the repository at this point in the history
Update documuent viewer in the attachment widget on attribute change
  • Loading branch information
m-kuhn committed Sep 4, 2020
2 parents 81cf4ee + c3cb69b commit d998bb3
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 7 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();

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
Expand Down
28 changes: 27 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 @@ -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<QgsAttributeForm *>( parent );

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

return new QgsExternalResourceWidget( parent );
}

Expand Down Expand Up @@ -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 )
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 @@ -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;
};

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

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

Expand All @@ -1057,7 +1060,7 @@ void QgsAttributeForm::updateLabels()
}
}

bool QgsAttributeForm::currentFormFeature( QgsFeature &feature )
bool QgsAttributeForm::currentFormValuesFeature( QgsFeature &feature )
{
bool rc = true;
feature = QgsFeature( mFeature );
Expand Down Expand Up @@ -2460,7 +2463,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
*/
QgsFeature currentFormFeature() const { 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
4 changes: 3 additions & 1 deletion src/gui/qgsexternalresourcewidget.cpp
Expand Up @@ -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
Expand Down

0 comments on commit d998bb3

Please sign in to comment.