Skip to content

Commit

Permalink
Merge pull request #33379 from elpaso/widget-dependencies-move-to-for…
Browse files Browse the repository at this point in the history
…matter

Move layerDependencies to formatter
  • Loading branch information
elpaso committed Dec 14, 2019
2 parents 40b198c + a368e44 commit 7464290
Show file tree
Hide file tree
Showing 21 changed files with 380 additions and 83 deletions.
Expand Up @@ -38,6 +38,9 @@ Default constructor of field formatter for a relation reference field.

virtual QVariant createCache( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config ) const;




};

/************************************************************************
Expand Down
Expand Up @@ -128,8 +128,6 @@ Returns the (possibly NULL) layer from the widget's ``config`` and ``project``
.. versionadded:: 3.8
%End



};


Expand Down
3 changes: 3 additions & 0 deletions python/core/auto_generated/qgsfieldformatter.sip.in
Expand Up @@ -85,6 +85,9 @@ make use of a cache if present.

.. versionadded:: 3.0
%End



};

/************************************************************************
Expand Down
Expand Up @@ -10,6 +10,7 @@




class QgsEditorWidgetWrapper : QgsWidgetWrapper
{
%Docstring
Expand Down Expand Up @@ -187,7 +188,6 @@ of attribute values failing enforced field constraints.
.. versionadded:: 3.0
%End


QString constraintFailureReason() const;
%Docstring
Returns the reason why a constraint check has failed (or an empty string
Expand Down
10 changes: 4 additions & 6 deletions src/app/qgisapp.cpp
Expand Up @@ -2001,13 +2001,11 @@ QList<QgsVectorLayerRef> QgisApp::findBrokenWidgetDependencies( QgsVectorLayer *
// Check for missing layer widget dependencies
for ( int i = 0; i < vl->fields().count(); i++ )
{
std::unique_ptr<QgsEditorWidgetWrapper> ww;
ww.reset( QgsGui::editorWidgetRegistry()->create( vl, i, nullptr, nullptr ) );
// ww should never be null in real life, but it is in QgisApp tests because
// QgsEditorWidgetRegistry widget factories is empty
if ( ww )
const QgsEditorWidgetSetup setup = QgsGui::editorWidgetRegistry()->findBest( vl, vl->fields().field( i ).name() );
QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
if ( fieldFormatter )
{
const auto constDependencies { ww->layerDependencies() };
const auto constDependencies { fieldFormatter->layerDependencies( setup.config() ) };
for ( const QgsVectorLayerRef &dependency : constDependencies )
{
const QgsVectorLayer *depVl { QgsVectorLayerRef( dependency ).resolveWeakly(
Expand Down
14 changes: 14 additions & 0 deletions src/core/fieldformatter/qgsrelationreferencefieldformatter.cpp
Expand Up @@ -160,3 +160,17 @@ QVariant QgsRelationReferenceFieldFormatter::createCache( QgsVectorLayer *layer,

return QVariant::fromValue<QMap<QVariant, QString>>( cache );
}


QList<QgsVectorLayerRef> QgsRelationReferenceFieldFormatter::layerDependencies( const QVariantMap &config ) const
{

const QList<QgsVectorLayerRef> result {{
QgsVectorLayerRef(
config.value( QStringLiteral( "ReferencedLayerId" ) ).toString(),
config.value( QStringLiteral( "ReferencedLayerName" ) ).toString(),
config.value( QStringLiteral( "ReferencedLayerDataSource" ) ).toString(),
config.value( QStringLiteral( "ReferencedLayerProviderKey" ) ).toString() )
}};
return result;
}
5 changes: 5 additions & 0 deletions src/core/fieldformatter/qgsrelationreferencefieldformatter.h
Expand Up @@ -43,6 +43,11 @@ class CORE_EXPORT QgsRelationReferenceFieldFormatter : public QgsFieldFormatter
QVariant sortValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const override;

QVariant createCache( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config ) const override;

QList<QgsVectorLayerRef> layerDependencies( const QVariantMap &config ) const override SIP_SKIP;

//friend class TestQgsRelationReferenceFieldFormatter;

};

#endif // QGSRELATIONREFERENCEFIELDKIT_H
16 changes: 15 additions & 1 deletion src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp
Expand Up @@ -168,6 +168,21 @@ QgsValueRelationFieldFormatter::ValueRelationCache QgsValueRelationFieldFormatte
return cache;
}


QList<QgsVectorLayerRef> QgsValueRelationFieldFormatter::layerDependencies( const QVariantMap &config ) const
{
QList<QgsVectorLayerRef> result;
const QString layerId { config.value( QStringLiteral( "Layer" ) ).toString() };
const QString layerName { config.value( QStringLiteral( "LayerName" ) ).toString() };
const QString providerName { config.value( QStringLiteral( "LayerProviderName" ) ).toString() };
const QString layerSource { config.value( QStringLiteral( "LayerSource" ) ).toString() };
if ( ! layerId.isEmpty() && ! layerName.isEmpty() && ! providerName.isEmpty() && ! layerSource.isEmpty() )
{
result.append( QgsVectorLayerRef( layerId, layerName, layerSource, providerName ) );
}
return result;
}

QStringList QgsValueRelationFieldFormatter::valueToStringList( const QVariant &value )
{
QStringList checkList;
Expand Down Expand Up @@ -291,4 +306,3 @@ QgsVectorLayer *QgsValueRelationFieldFormatter::resolveLayer( const QVariantMap
config.value( QStringLiteral( "LayerProviderName" ) ).toString() };
return ref.resolveByIdOrNameOnly( project );
}

3 changes: 1 addition & 2 deletions src/core/fieldformatter/qgsvaluerelationfieldformatter.h
Expand Up @@ -125,8 +125,7 @@ class CORE_EXPORT QgsValueRelationFieldFormatter : public QgsFieldFormatter
*/
static QgsVectorLayer *resolveLayer( const QVariantMap &config, const QgsProject *project );



QList<QgsVectorLayerRef> layerDependencies( const QVariantMap &config ) const override SIP_SKIP;
};

Q_DECLARE_METATYPE( QgsValueRelationFieldFormatter::ValueRelationCache )
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsfieldformatter.cpp
Expand Up @@ -62,3 +62,9 @@ QVariant QgsFieldFormatter::createCache( QgsVectorLayer *layer, int fieldIndex,

return QVariant();
}

QList<QgsVectorLayerRef> QgsFieldFormatter::layerDependencies( const QVariantMap &config ) const
{
Q_UNUSED( config )
return QList<QgsVectorLayerRef>();
}
17 changes: 17 additions & 0 deletions src/core/qgsfieldformatter.h
Expand Up @@ -20,6 +20,7 @@
#include <QVariant>

#include "qgis_core.h"
#include "qgsvectorlayerref.h"

class QgsVectorLayer;

Expand Down Expand Up @@ -96,6 +97,22 @@ class CORE_EXPORT QgsFieldFormatter
* \since QGIS 3.0
*/
virtual QVariant createCache( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config ) const;

/**
* Returns a list of weak layer references to other layers required by this formatter
* for the given \a config.
* The default implementation returns an empty list.
*
* This method should be reimplemented by formatters that handle relations with other layers,
* (e.g. ValueRelation) and can be used by client code to warn the user about
* missing required dependencies or to add some resolution logic in order
* to load the missing dependency.
* \note not available in Python bindings
* \since QGIS 3.12
*/
virtual QList< QgsVectorLayerRef > layerDependencies( const QVariantMap &config ) const SIP_SKIP;


};

#endif // QGSFIELDKIT_H
4 changes: 0 additions & 4 deletions src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp
Expand Up @@ -267,10 +267,6 @@ bool QgsEditorWidgetWrapper::isBlockingCommit() const
return mIsBlockingCommit;
}

QList<QgsVectorLayerRef> QgsEditorWidgetWrapper::layerDependencies() const
{
return QList<QgsVectorLayerRef>();
}

QString QgsEditorWidgetWrapper::constraintFailureReason() const
{
Expand Down
15 changes: 1 addition & 14 deletions src/gui/editorwidgets/core/qgseditorwidgetwrapper.h
Expand Up @@ -27,7 +27,7 @@ class QgsField;
#include "qgswidgetwrapper.h"
#include "qgis_gui.h"
#include "qgis_sip.h"
#include "qgsvectorlayerref.h"


/**
* \ingroup gui
Expand Down Expand Up @@ -193,19 +193,6 @@ class GUI_EXPORT QgsEditorWidgetWrapper : public QgsWidgetWrapper
*/
bool isBlockingCommit() const;

/**
* Returns a list of weak layer references to other layers required by this widget.
* The default implementation returns an empty list.
*
* This method should be reimplemented by widgets that handle relations with other layers,
* (e.g. ValueRelation) and can be used by client code to warn the user about
* missing required dependencies or to add some resolution logic in order
* to load the missing dependency.
* \note not available in Python bindings
* \since QGIS 3.12
*/
virtual QList< QgsVectorLayerRef > layerDependencies() const SIP_SKIP;

/**
* Returns the reason why a constraint check has failed (or an empty string
* if constraint check was successful).
Expand Down
15 changes: 0 additions & 15 deletions src/gui/editorwidgets/qgsrelationreferencewidgetwrapper.cpp
Expand Up @@ -182,21 +182,6 @@ QStringList QgsRelationReferenceWidgetWrapper::additionalFields() const
return fields;
}

QList<QgsVectorLayerRef> QgsRelationReferenceWidgetWrapper::layerDependencies() const
{
QList<QgsVectorLayerRef> result;
if ( mWidget )
{
result.append(
QgsVectorLayerRef(
mWidget->referencedLayerId(),
mWidget->referencedLayerName(),
mWidget->referencedLayerDataSource(),
mWidget->referencedLayerProviderKey() ) );
}
return result;
}

void QgsRelationReferenceWidgetWrapper::updateValues( const QVariant &val, const QVariantList &additionalValues )
{
if ( !mWidget || ( !mIndeterminateState && val == value() && val.isNull() == value().isNull() ) )
Expand Down
3 changes: 0 additions & 3 deletions src/gui/editorwidgets/qgsrelationreferencewidgetwrapper.h
Expand Up @@ -69,7 +69,6 @@ class GUI_EXPORT QgsRelationReferenceWidgetWrapper : public QgsEditorWidgetWrapp

protected:
void updateConstraintWidgetStatus() override;
QList<QgsVectorLayerRef> layerDependencies() const override SIP_SKIP;

private:
void updateValues( const QVariant &val, const QVariantList &additionalValues = QVariantList() ) override;
Expand All @@ -79,8 +78,6 @@ class GUI_EXPORT QgsRelationReferenceWidgetWrapper : public QgsEditorWidgetWrapp
QgsMessageBar *mMessageBar = nullptr;
bool mIndeterminateState;

friend class TestQgsRelationReferenceWidget;

};

#endif // QGSRELATIONREFERENCEWIDGETWRAPPER_H
15 changes: 0 additions & 15 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -452,21 +452,6 @@ void QgsValueRelationWidgetWrapper::setEnabled( bool enabled )
QgsEditorWidgetWrapper::setEnabled( enabled );
}


QList<QgsVectorLayerRef> QgsValueRelationWidgetWrapper::layerDependencies() const
{
QList<QgsVectorLayerRef> result;
const QString layerId { config().value( QStringLiteral( "Layer" ) ).toString() };
const QString layerName { config().value( QStringLiteral( "LayerName" ) ).toString() };
const QString providerName { config().value( QStringLiteral( "LayerProviderName" ) ).toString() };
const QString layerSource { config().value( QStringLiteral( "LayerSource" ) ).toString() };
if ( ! layerId.isEmpty() && ! layerName.isEmpty() && ! providerName.isEmpty() && ! layerSource.isEmpty() )
{
result.append( QgsVectorLayerRef( layerId, layerName, layerSource, providerName ) );
}
return result;
}

void QgsValueRelationWidgetWrapper::emitValueChangedInternal( const QString &value )
{
Q_NOWARN_DEPRECATED_PUSH
Expand Down
2 changes: 0 additions & 2 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.h
Expand Up @@ -102,8 +102,6 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper
*/
void setFeature( const QgsFeature &feature ) override;

QList<QgsVectorLayerRef> layerDependencies() const override;

private slots:
void emitValueChangedInternal( const QString &value );

Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -205,6 +205,7 @@ SET(TESTS
testqgsrasterlayer.cpp
testqgsrastersublayer.cpp
testqgsrectangle.cpp
testqgsrelationreferencefieldformatter.cpp
testqgsrenderers.cpp
testqgsrulebasedrenderer.cpp
testqgssettings.cpp
Expand All @@ -223,6 +224,7 @@ SET(TESTS
testqgstracer.cpp
testqgstriangularmesh.cpp
testqgsfontutils.cpp
testqgsvaluerelationfieldformatter.cpp
testqgsvector.cpp
testqgsvectordataprovider.cpp
testqgsvectorlayercache.cpp
Expand Down

0 comments on commit 7464290

Please sign in to comment.