Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Automatically update the editor widget setup according to the standar…
…d template of the property newly created
  • Loading branch information
pblottiere committed Oct 9, 2017
1 parent 821aadc commit 34ac1ec
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
15 changes: 13 additions & 2 deletions python/core/qgsauxiliarystorage.sip
Expand Up @@ -92,7 +92,7 @@ class QgsAuxiliaryLayer : QgsVectorLayer
%End
public:

QgsAuxiliaryLayer( const QString &pkField, const QString &filename, const QString &table, const QgsVectorLayer *vlayer );
QgsAuxiliaryLayer( const QString &pkField, const QString &filename, const QString &table, QgsVectorLayer *vlayer );
%Docstring
Constructor

Expand Down Expand Up @@ -188,6 +188,17 @@ class QgsAuxiliaryLayer : QgsVectorLayer
:rtype: bool
%End

int indexOfProperty( const QgsPropertyDefinition &definition ) const;
%Docstring
Returns the index of the auxiliary field for a specific property
definition.

\param definition The property definition

:return: The index of the field corresponding to the property or -1
:rtype: int
%End

static int createProperty( QgsPalLayerSettings::Property property, const QString &providerId, QgsVectorLayer *vlayer );
%Docstring
Create if necessary a new auxiliary field for a PAL property and
Expand Down Expand Up @@ -320,7 +331,7 @@ class QgsAuxiliaryStorage
:rtype: bool
%End

QgsAuxiliaryLayer *createAuxiliaryLayer( const QgsField &field, const QgsVectorLayer *layer ) const /Factory/;
QgsAuxiliaryLayer *createAuxiliaryLayer( const QgsField &field, QgsVectorLayer *layer ) const /Factory/;
%Docstring
Creates an auxiliary layer for a vector layer. A new table is created if
necessary. The primary key to use to construct the auxiliary layer is
Expand Down
29 changes: 26 additions & 3 deletions src/core/qgsauxiliarystorage.cpp
Expand Up @@ -179,7 +179,7 @@ QgsPropertyDefinition QgsAuxiliaryField::propertyDefinition() const
// QgsAuxiliaryLayer
//

QgsAuxiliaryLayer::QgsAuxiliaryLayer( const QString &pkField, const QString &filename, const QString &table, const QgsVectorLayer *vlayer )
QgsAuxiliaryLayer::QgsAuxiliaryLayer( const QString &pkField, const QString &filename, const QString &table, QgsVectorLayer *vlayer )
: QgsVectorLayer( QString( "%1|layername=%2" ).arg( filename, table ), QString( "%1_auxiliarystorage" ).arg( table ), "ogr" )
, mLayer( vlayer )
{
Expand Down Expand Up @@ -232,7 +232,7 @@ QgsVectorLayerJoinInfo QgsAuxiliaryLayer::joinInfo() const

bool QgsAuxiliaryLayer::exists( const QgsPropertyDefinition &definition ) const
{
return ( fields().indexOf( QgsAuxiliaryField::nameFromProperty( definition ) ) >= 0 );
return ( indexOfProperty( definition ) >= 0 );
}

bool QgsAuxiliaryLayer::addAuxiliaryField( const QgsPropertyDefinition &definition )
Expand All @@ -244,6 +244,24 @@ bool QgsAuxiliaryLayer::addAuxiliaryField( const QgsPropertyDefinition &definiti
const bool rc = addAttribute( af );
updateFields();

if ( rc )
{
int auxIndex = indexOfProperty( definition );
int index = mLayer->fields().indexOf( QgsAuxiliaryField::nameFromProperty( definition, true ) );

if ( index >= 0 && auxIndex >= 0 )
{
if ( definition.standardTemplate() == QgsPropertyDefinition::ColorNoAlpha
|| definition.standardTemplate() == QgsPropertyDefinition::ColorWithAlpha )
{
QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( QStringLiteral( "Color" ), QVariantMap() );
setEditorWidgetSetup( auxIndex, setup );
}

mLayer->setEditorWidgetSetup( index, editorWidgetSetup( auxIndex ) );
}
}

return rc;
}

Expand Down Expand Up @@ -358,6 +376,11 @@ bool QgsAuxiliaryLayer::isHiddenProperty( int index ) const
return hidden;
}

int QgsAuxiliaryLayer::indexOfProperty( const QgsPropertyDefinition &def ) const
{
return fields().indexOf( QgsAuxiliaryField::nameFromProperty( def ) );
}

//
// QgsAuxiliaryStorage
//
Expand Down Expand Up @@ -424,7 +447,7 @@ bool QgsAuxiliaryStorage::save() const
}
}

QgsAuxiliaryLayer *QgsAuxiliaryStorage::createAuxiliaryLayer( const QgsField &field, const QgsVectorLayer *layer ) const
QgsAuxiliaryLayer *QgsAuxiliaryStorage::createAuxiliaryLayer( const QgsField &field, QgsVectorLayer *layer ) const
{
QgsAuxiliaryLayer *alayer = nullptr;

Expand Down
16 changes: 13 additions & 3 deletions src/core/qgsauxiliarystorage.h
Expand Up @@ -123,7 +123,7 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer
* \param table The table name
* \param vlayer The target vector layer in join definition
*/
QgsAuxiliaryLayer( const QString &pkField, const QString &filename, const QString &table, const QgsVectorLayer *vlayer );
QgsAuxiliaryLayer( const QString &pkField, const QString &filename, const QString &table, QgsVectorLayer *vlayer );

/**
* Destructor
Expand Down Expand Up @@ -207,6 +207,16 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer
*/
bool isHiddenProperty( int index ) const;

/**
* Returns the index of the auxiliary field for a specific property
* definition.
*
* \param definition The property definition
*
* \returns The index of the field corresponding to the property or -1
*/
int indexOfProperty( const QgsPropertyDefinition &definition ) const;

/**
* Create if necessary a new auxiliary field for a PAL property and
* activate this property in settings.
Expand All @@ -233,7 +243,7 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer

private:
QgsVectorLayerJoinInfo mJoinInfo;
const QgsVectorLayer *mLayer = nullptr;
QgsVectorLayer *mLayer = nullptr;
};


Expand Down Expand Up @@ -342,7 +352,7 @@ class CORE_EXPORT QgsAuxiliaryStorage
*
* \returns A new auxiliary layer or a nullptr if an error happened.
*/
QgsAuxiliaryLayer *createAuxiliaryLayer( const QgsField &field, const QgsVectorLayer *layer ) const SIP_FACTORY;
QgsAuxiliaryLayer *createAuxiliaryLayer( const QgsField &field, QgsVectorLayer *layer ) const SIP_FACTORY;

/**
* Removes a table from the auxiliary storage.
Expand Down

0 comments on commit 34ac1ec

Please sign in to comment.