Skip to content
Permalink
Browse files

use of context to pass project and avoid QgsProject::instance()

  • Loading branch information
signedav committed Dec 19, 2019
1 parent 9e5657c commit e8b82c15b882e9eb2fa9e5e33a4a66180d3e7fc7
@@ -40,7 +40,7 @@ Default constructor of field formatter for a relation reference field.



virtual QVariantList availableValues( const QVariantMap &config, int countLimit ) const;
virtual QVariantList availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext *context ) const;



@@ -47,7 +47,7 @@ Default constructor of field formatter for a value map field.
virtual QVariant sortValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const;


virtual QVariantList availableValues( const QVariantMap &config, int countLimit ) const;
virtual QVariantList availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext *context ) const;

};

@@ -129,7 +129,7 @@ Returns the (possibly NULL) layer from the widget's ``config`` and ``project``
%End


virtual QVariantList availableValues( const QVariantMap &config, int countLimit ) const;
virtual QVariantList availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext *context ) const;

};

@@ -9,6 +9,40 @@



class QgsFieldFormatterContext
{
%Docstring
A context for field formatter containing information like the project

.. versionadded:: 3.12
%End

%TypeHeaderCode
#include "qgsfieldformatter.h"
%End
public:

QgsFieldFormatterContext();
%Docstring
Constructor
%End

QgsProject *project() const;
%Docstring
Returns the project used in field formatter

.. seealso:: :py:func:`setProject`
%End

void setProject( QgsProject *project );
%Docstring
Sets the ``project`` used in field formatter

.. seealso:: :py:func:`project`
%End

};

class QgsFieldFormatter
{
%Docstring
@@ -17,7 +51,7 @@ A field formatter helps to handle and display values for a field.
It allows for using a shared configuration with the editor widgets
for representation of attribute values.
Field kits normally have one single instance which is managed by the
QgsFieldFormatterRegistry. Custom field formatters should be registered there and
:py:class:`QgsFieldFormatterRegistry`. Custom field formatters should be registered there and
field formatters for use within code should normally be obtained from there.

This is an abstract base class and will always need to be subclassed.
@@ -94,7 +128,7 @@ make use of a cache if present.
%End


virtual QVariantList availableValues( const QVariantMap &config, int countLimit ) const;
virtual QVariantList availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext *context ) const;
%Docstring
Returns a list of the values that would be possible to select with this widget type
On a RelationReference that would be the parents ids or on ValueMap all the configured keys
@@ -180,15 +180,17 @@ QList<QgsVectorLayerRef> QgsRelationReferenceFieldFormatter::layerDependencies(
return result;
}

QVariantList QgsRelationReferenceFieldFormatter::availableValues( const QVariantMap &config, int countLimit ) const
QVariantList QgsRelationReferenceFieldFormatter::availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext *context ) const
{
QVariantList values;

const QgsVectorLayer *referencedLayer = QgsProject::instance()->relationManager()->relation( config[QStringLiteral( "Relation" )].toString() ).referencedLayer();
if ( referencedLayer )
if ( context->project() )
{
int fieldIndex = QgsProject::instance()->relationManager()->relation( config[QStringLiteral( "Relation" )].toString() ).referencedFields().first();
values = referencedLayer->uniqueValues( fieldIndex, countLimit ).toList();
const QgsVectorLayer *referencedLayer = context->project()->relationManager()->relation( config[QStringLiteral( "Relation" )].toString() ).referencedLayer();
if ( referencedLayer )
{
int fieldIndex = context->project()->relationManager()->relation( config[QStringLiteral( "Relation" )].toString() ).referencedFields().first();
values = referencedLayer->uniqueValues( fieldIndex, countLimit ).toList();
}
}
return values;
}
@@ -46,7 +46,7 @@ class CORE_EXPORT QgsRelationReferenceFieldFormatter : public QgsFieldFormatter

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

QVariantList availableValues( const QVariantMap &config, int countLimit ) const override;
QVariantList availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext *context ) const override;

//friend class TestQgsRelationReferenceFieldFormatter;

@@ -68,8 +68,10 @@ QVariant QgsValueMapFieldFormatter::sortValue( QgsVectorLayer *layer, int fieldI
return representValue( layer, fieldIndex, config, cache, value );
}

QVariantList QgsValueMapFieldFormatter::availableValues( const QVariantMap &config, int countLimit ) const
QVariantList QgsValueMapFieldFormatter::availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext *context ) const
{
Q_UNUSED( context )

QVariantList values;
const QList<QVariant> valueList = config.value( QStringLiteral( "map" ) ).toList();
for ( const QVariant &item : valueList )
@@ -57,7 +57,7 @@ class CORE_EXPORT QgsValueMapFieldFormatter : public QgsFieldFormatter

QVariant sortValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const override;

QVariantList availableValues( const QVariantMap &config, int countLimit ) const override;
QVariantList availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext *context ) const override;
};

#endif // QGSVALUEMAPFIELDKIT_H
@@ -188,15 +188,18 @@ QList<QgsVectorLayerRef> QgsValueRelationFieldFormatter::layerDependencies( cons
return result;
}

QVariantList QgsValueRelationFieldFormatter::availableValues( const QVariantMap &config, int countLimit ) const
QVariantList QgsValueRelationFieldFormatter::availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext *context ) const
{
QVariantList values;

const QgsVectorLayer *referencedLayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( config[QStringLiteral( "Layer" )].toString() ) );
if ( referencedLayer )
if ( context->project() )
{
int fieldIndex = referencedLayer->fields().indexOf( config.value( QStringLiteral( "Key" ) ).toString() );
values = referencedLayer->uniqueValues( fieldIndex, countLimit ).toList();
const QgsVectorLayer *referencedLayer = qobject_cast<QgsVectorLayer *>( context->project()->mapLayer( config[QStringLiteral( "Layer" )].toString() ) );
if ( referencedLayer )
{
int fieldIndex = referencedLayer->fields().indexOf( config.value( QStringLiteral( "Key" ) ).toString() );
values = referencedLayer->uniqueValues( fieldIndex, countLimit ).toList();
}
}
return values;
}
@@ -127,7 +127,7 @@ class CORE_EXPORT QgsValueRelationFieldFormatter : public QgsFieldFormatter

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

QVariantList availableValues( const QVariantMap &config, int countLimit ) const override;
QVariantList availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext *context ) const override;
};

Q_DECLARE_METATYPE( QgsValueRelationFieldFormatter::ValueRelationCache )
@@ -69,10 +69,11 @@ QList<QgsVectorLayerRef> QgsFieldFormatter::layerDependencies( const QVariantMap
return QList<QgsVectorLayerRef>();
}

QVariantList QgsFieldFormatter::availableValues( const QVariantMap &config, int countLimit ) const
QVariantList QgsFieldFormatter::availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext *context ) const
{
Q_UNUSED( config )
Q_UNUSED( countLimit )
Q_UNUSED( context )

return QVariantList();
}
@@ -24,6 +24,37 @@

class QgsVectorLayer;

/**
* \ingroup core
* A context for field formatter containing information like the project
*
* \since QGIS 3.12
*/
class CORE_EXPORT QgsFieldFormatterContext
{
public:

/**
* Constructor
*/
QgsFieldFormatterContext() = default;

/**
* Returns the project used in field formatter
* \see setProject()
*/
QgsProject *project() const { return mProject; }

/**
* Sets the \a project used in field formatter
* \see project()
*/
void setProject( QgsProject *project ) { mProject = project; }

private:
QgsProject *mProject = nullptr;
};

/**
* \ingroup core
* A field formatter helps to handle and display values for a field.
@@ -125,7 +156,7 @@ class CORE_EXPORT QgsFieldFormatter
* according to the settings in the \a config
* \since QGIS 3.12
*/
virtual QVariantList availableValues( const QVariantMap &config, int countLimit ) const;
virtual QVariantList availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext *context ) const;

This comment has been minimized.

Copy link
@nyalldawson

nyalldawson Dec 19, 2019

Contributor

I'd make that a reference, not a pointer - Pointers imply optionality


/**
* Returns the flags
@@ -42,7 +42,6 @@
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>


QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
: QWidget( parent )
, mProject( QgsProject::instance() )
@@ -475,7 +474,9 @@ void QgsExpressionBuilderWidget::fillFieldValues( const QString &fieldName, int
QVariantList values;
if ( cbxValuesInUse->isVisible() && !cbxValuesInUse->isChecked() && !forceUsedValues )
{
values = formatter->availableValues( setup.config(), countLimit );
QgsFieldFormatterContext fieldFormatterContext;
fieldFormatterContext.setProject( mProject );
values = formatter->availableValues( setup.config(), countLimit, &fieldFormatterContext );
}
else
{
@@ -3,8 +3,8 @@
---------------------
begin : 7.12.2017
copyright : (C) 2017 by David Signer
email : david@opengis.ch
copyright : (C) 2017 by by David Signer

This comment has been minimized.

Copy link
@roya0045

roya0045 Dec 19, 2019

Contributor

by by?

This comment has been minimized.

Copy link
@signedav

signedav Dec 19, 2019

Author Contributor

bye bye ;-)
sorry typo

email : david at opengis dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *

0 comments on commit e8b82c1

Please sign in to comment.
You can’t perform that action at this time.