Skip to content

Commit 34ac1ec

Browse files
committed
Automatically update the editor widget setup according to the standard template of the property newly created
1 parent 821aadc commit 34ac1ec

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

python/core/qgsauxiliarystorage.sip

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class QgsAuxiliaryLayer : QgsVectorLayer
9292
%End
9393
public:
9494

95-
QgsAuxiliaryLayer( const QString &pkField, const QString &filename, const QString &table, const QgsVectorLayer *vlayer );
95+
QgsAuxiliaryLayer( const QString &pkField, const QString &filename, const QString &table, QgsVectorLayer *vlayer );
9696
%Docstring
9797
Constructor
9898

@@ -188,6 +188,17 @@ class QgsAuxiliaryLayer : QgsVectorLayer
188188
:rtype: bool
189189
%End
190190

191+
int indexOfProperty( const QgsPropertyDefinition &definition ) const;
192+
%Docstring
193+
Returns the index of the auxiliary field for a specific property
194+
definition.
195+
196+
\param definition The property definition
197+
198+
:return: The index of the field corresponding to the property or -1
199+
:rtype: int
200+
%End
201+
191202
static int createProperty( QgsPalLayerSettings::Property property, const QString &providerId, QgsVectorLayer *vlayer );
192203
%Docstring
193204
Create if necessary a new auxiliary field for a PAL property and
@@ -320,7 +331,7 @@ class QgsAuxiliaryStorage
320331
:rtype: bool
321332
%End
322333

323-
QgsAuxiliaryLayer *createAuxiliaryLayer( const QgsField &field, const QgsVectorLayer *layer ) const /Factory/;
334+
QgsAuxiliaryLayer *createAuxiliaryLayer( const QgsField &field, QgsVectorLayer *layer ) const /Factory/;
324335
%Docstring
325336
Creates an auxiliary layer for a vector layer. A new table is created if
326337
necessary. The primary key to use to construct the auxiliary layer is

src/core/qgsauxiliarystorage.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ QgsPropertyDefinition QgsAuxiliaryField::propertyDefinition() const
179179
// QgsAuxiliaryLayer
180180
//
181181

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

233233
bool QgsAuxiliaryLayer::exists( const QgsPropertyDefinition &definition ) const
234234
{
235-
return ( fields().indexOf( QgsAuxiliaryField::nameFromProperty( definition ) ) >= 0 );
235+
return ( indexOfProperty( definition ) >= 0 );
236236
}
237237

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

247+
if ( rc )
248+
{
249+
int auxIndex = indexOfProperty( definition );
250+
int index = mLayer->fields().indexOf( QgsAuxiliaryField::nameFromProperty( definition, true ) );
251+
252+
if ( index >= 0 && auxIndex >= 0 )
253+
{
254+
if ( definition.standardTemplate() == QgsPropertyDefinition::ColorNoAlpha
255+
|| definition.standardTemplate() == QgsPropertyDefinition::ColorWithAlpha )
256+
{
257+
QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( QStringLiteral( "Color" ), QVariantMap() );
258+
setEditorWidgetSetup( auxIndex, setup );
259+
}
260+
261+
mLayer->setEditorWidgetSetup( index, editorWidgetSetup( auxIndex ) );
262+
}
263+
}
264+
247265
return rc;
248266
}
249267

@@ -358,6 +376,11 @@ bool QgsAuxiliaryLayer::isHiddenProperty( int index ) const
358376
return hidden;
359377
}
360378

379+
int QgsAuxiliaryLayer::indexOfProperty( const QgsPropertyDefinition &def ) const
380+
{
381+
return fields().indexOf( QgsAuxiliaryField::nameFromProperty( def ) );
382+
}
383+
361384
//
362385
// QgsAuxiliaryStorage
363386
//
@@ -424,7 +447,7 @@ bool QgsAuxiliaryStorage::save() const
424447
}
425448
}
426449

427-
QgsAuxiliaryLayer *QgsAuxiliaryStorage::createAuxiliaryLayer( const QgsField &field, const QgsVectorLayer *layer ) const
450+
QgsAuxiliaryLayer *QgsAuxiliaryStorage::createAuxiliaryLayer( const QgsField &field, QgsVectorLayer *layer ) const
428451
{
429452
QgsAuxiliaryLayer *alayer = nullptr;
430453

src/core/qgsauxiliarystorage.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer
123123
* \param table The table name
124124
* \param vlayer The target vector layer in join definition
125125
*/
126-
QgsAuxiliaryLayer( const QString &pkField, const QString &filename, const QString &table, const QgsVectorLayer *vlayer );
126+
QgsAuxiliaryLayer( const QString &pkField, const QString &filename, const QString &table, QgsVectorLayer *vlayer );
127127

128128
/**
129129
* Destructor
@@ -207,6 +207,16 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer
207207
*/
208208
bool isHiddenProperty( int index ) const;
209209

210+
/**
211+
* Returns the index of the auxiliary field for a specific property
212+
* definition.
213+
*
214+
* \param definition The property definition
215+
*
216+
* \returns The index of the field corresponding to the property or -1
217+
*/
218+
int indexOfProperty( const QgsPropertyDefinition &definition ) const;
219+
210220
/**
211221
* Create if necessary a new auxiliary field for a PAL property and
212222
* activate this property in settings.
@@ -233,7 +243,7 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer
233243

234244
private:
235245
QgsVectorLayerJoinInfo mJoinInfo;
236-
const QgsVectorLayer *mLayer = nullptr;
246+
QgsVectorLayer *mLayer = nullptr;
237247
};
238248

239249

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

347357
/**
348358
* Removes a table from the auxiliary storage.

0 commit comments

Comments
 (0)