Merge pull request #38318 from signedav/attachement-expression

Update documuent viewer in the attachment widget on attribute change
This commit is contained in:
Matthias Kuhn 2020-09-04 21:13:07 +02:00 committed by GitHub
commit d998bb3d17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 7 deletions

View File

@ -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

View File

@ -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<QgsAttributeForm *>( 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 )

View File

@ -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;
};

View File

@ -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<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" );

View File

@ -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<QgsEditorWidgetWrapper *> 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;

View File

@ -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