diff --git a/python/core/qgsauxiliarystorage.sip b/python/core/qgsauxiliarystorage.sip index a6c0dabaad7..cc6a1b2f788 100644 --- a/python/core/qgsauxiliarystorage.sip +++ b/python/core/qgsauxiliarystorage.sip @@ -12,53 +12,6 @@ -class QgsAuxiliaryField : QgsField -{ -%Docstring - - - Class allowing to manage fields from of auxiliary layers - -.. versionadded:: 3.0 -%End - -%TypeHeaderCode -#include "qgsauxiliarystorage.h" -%End - public: - - explicit QgsAuxiliaryField( const QgsPropertyDefinition &def ); -%Docstring - Constructor - - \param def Definition of the property to be stored by this auxiliary - field. -%End - - virtual ~QgsAuxiliaryField(); -%Docstring - Destructor -%End - - QgsPropertyDefinition propertyDefinition() const; -%Docstring - Returns the property definition corresponding to this field. - :rtype: QgsPropertyDefinition -%End - - static QString nameFromProperty( const QgsPropertyDefinition &def, bool joined = false ); -%Docstring - Returns the name of the auxiliary field for a property definition. - - :return: def The property definition - :return: joined The join prefix is tok into account if true - :rtype: str -%End - -}; - -typedef QList QgsAuxiliaryFields; - class QgsAuxiliaryLayer : QgsVectorLayer { %Docstring @@ -166,10 +119,10 @@ class QgsAuxiliaryLayer : QgsVectorLayer :rtype: bool %End - QgsAuxiliaryFields auxiliaryFields() const; + QgsFields auxiliaryFields() const; %Docstring Returns a list of all auxiliary fields currently managed by the layer. - :rtype: QgsAuxiliaryFields + :rtype: QgsFields %End bool save(); @@ -200,7 +153,7 @@ class QgsAuxiliaryLayer : QgsVectorLayer :rtype: bool %End - int indexOfProperty( const QgsPropertyDefinition &definition ) const; + int indexOfPropertyDefinition( const QgsPropertyDefinition &definition ) const; %Docstring Returns the index of the auxiliary field for a specific property definition. @@ -219,8 +172,6 @@ class QgsAuxiliaryLayer : QgsVectorLayer happened. \param index The index of the field - -.. versionadded:: 3.0 :rtype: int %End @@ -229,14 +180,12 @@ class QgsAuxiliaryLayer : QgsVectorLayer Returns the property definition fir the underlying field index. \param index The index of the field - -.. versionadded:: 3.0 :rtype: QgsPropertyDefinition %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 + Creates if necessary a new auxiliary field for a PAL property and activate this property in settings. \param property The property to create @@ -249,7 +198,7 @@ class QgsAuxiliaryLayer : QgsVectorLayer static int createProperty( QgsDiagramLayerSettings::Property property, QgsVectorLayer *vlayer ); %Docstring - Create if necessary a new auxiliary field for a diagram's property and + Creates if necessary a new auxiliary field for a diagram's property and activate this this property in settings. \param property The property to create @@ -259,6 +208,39 @@ class QgsAuxiliaryLayer : QgsVectorLayer :rtype: int %End + static QgsField createAuxiliaryField( const QgsPropertyDefinition &definition ); +%Docstring + Creates a new auxiliary field from a property definition. + + \param definition The property definition of the auxiliary field to create + :rtype: QgsField +%End + + static QgsField createAuxiliaryField( const QgsField &field ); +%Docstring + Creates a new auxiliary field from a field. + + \param field The field to use to create the auxiliary field + :rtype: QgsField +%End + + static QString nameFromProperty( const QgsPropertyDefinition &def, bool joined = false ); +%Docstring + Returns the name of the auxiliary field for a property definition. + + \param def The property definition + \param joined The join prefix is taken into account if true + :rtype: str +%End + + static QgsPropertyDefinition propertyDefinitionFromField( const QgsField &field ); +%Docstring + Returns the property definition from an auxiliary field. + + \param field The auxiliary field + :rtype: QgsPropertyDefinition +%End + }; diff --git a/src/app/qgsdiagramproperties.cpp b/src/app/qgsdiagramproperties.cpp index cb48f57ccfe..96c20001fbc 100644 --- a/src/app/qgsdiagramproperties.cpp +++ b/src/app/qgsdiagramproperties.cpp @@ -1075,7 +1075,7 @@ void QgsDiagramProperties::createAuxiliaryField() // update property with join field name from auxiliary storage QgsProperty property = button->toProperty(); - property.setField( QgsAuxiliaryField::nameFromProperty( def, true ) ); + property.setField( QgsAuxiliaryLayer::nameFromProperty( def, true ) ); property.setActive( true ); button->updateFieldLists(); button->setToProperty( property ); diff --git a/src/app/qgslabelinggui.cpp b/src/app/qgslabelinggui.cpp index d93a625ac19..3d11e6e8611 100644 --- a/src/app/qgslabelinggui.cpp +++ b/src/app/qgslabelinggui.cpp @@ -638,7 +638,7 @@ void QgsLabelingGui::createAuxiliaryField() // update property with join field name from auxiliary storage QgsProperty property = button->toProperty(); - property.setField( QgsAuxiliaryField::nameFromProperty( def, true ) ); + property.setField( QgsAuxiliaryLayer::nameFromProperty( def, true ) ); property.setActive( true ); button->updateFieldLists(); button->setToProperty( property ); diff --git a/src/app/qgsvectorlayerproperties.cpp b/src/app/qgsvectorlayerproperties.cpp index 982d50d537a..5554752abe7 100644 --- a/src/app/qgsvectorlayerproperties.cpp +++ b/src/app/qgsvectorlayerproperties.cpp @@ -1529,9 +1529,9 @@ void QgsVectorLayerProperties::updateAuxiliaryStoragePage( bool reset ) // add fields mAuxiliaryStorageFieldsTree->clear(); - Q_FOREACH ( const QgsAuxiliaryField &field, alayer->auxiliaryFields() ) + Q_FOREACH ( const QgsField &field, alayer->auxiliaryFields() ) { - const QgsPropertyDefinition prop = field.propertyDefinition(); + const QgsPropertyDefinition prop = QgsAuxiliaryLayer::propertyDefinitionFromField( field ); QTreeWidgetItem *item = new QTreeWidgetItem(); item->setText( 0, prop.origin() ); @@ -1620,7 +1620,7 @@ void QgsVectorLayerProperties::onAuxiliaryLayerDelete() // defined buttons while ( alayer->auxiliaryFields().size() > 0 ) { - QgsAuxiliaryField aField = alayer->auxiliaryFields()[0]; + QgsField aField = alayer->auxiliaryFields()[0]; deleteAuxiliaryField( alayer->fields().indexOf( aField.name() ) ); } @@ -1661,7 +1661,7 @@ void QgsVectorLayerProperties::onAuxiliaryLayerDeleteField() def.setName( item->text( 1 ) ); def.setComment( item->text( 2 ) ); - const QString fieldName = QgsAuxiliaryField::nameFromProperty( def ); + const QString fieldName = QgsAuxiliaryLayer::nameFromProperty( def ); const int index = mLayer->auxiliaryLayer()->fields().indexOf( fieldName ); if ( index < 0 ) diff --git a/src/core/qgsauxiliarystorage.cpp b/src/core/qgsauxiliarystorage.cpp index 7cc90b97f91..f62dcecb546 100644 --- a/src/core/qgsauxiliarystorage.cpp +++ b/src/core/qgsauxiliarystorage.cpp @@ -55,23 +55,351 @@ const QVector palHiddenProperties QgsPalLayerSettings::AlwaysShow }; -QgsAuxiliaryField::QgsAuxiliaryField( const QgsPropertyDefinition &def ) - : QgsField() - , mPropertyDefinition( def ) +// +// QgsAuxiliaryLayer +// + +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" ) + , mFileName( filename ) + , mTable( table ) + , mLayer( vlayer ) { - init( def ); + // init join info + mJoinInfo.setPrefix( AS_JOINPREFIX ); + mJoinInfo.setJoinLayer( this ); + mJoinInfo.setJoinFieldName( AS_JOINFIELD ); + mJoinInfo.setTargetFieldName( pkField ); + mJoinInfo.setEditable( true ); + mJoinInfo.setUpsertOnEdit( true ); + mJoinInfo.setCascadedDelete( true ); + mJoinInfo.setJoinFieldNamesBlackList( QStringList() << QStringLiteral( "rowid" ) ); // introduced by ogr provider } -QgsAuxiliaryField::QgsAuxiliaryField( const QgsField &f ) +QgsAuxiliaryLayer *QgsAuxiliaryLayer::clone( QgsVectorLayer *target ) const { + QgsAuxiliaryStorage::duplicateTable( source(), target->id() ); + return new QgsAuxiliaryLayer( mJoinInfo.targetFieldName(), mFileName, target->id(), target ); +} + +bool QgsAuxiliaryLayer::clear() +{ + bool rc = deleteFeatures( allFeatureIds() ); + commitChanges(); + startEditing(); + return rc; +} + +QgsVectorLayer *QgsAuxiliaryLayer::toSpatialLayer() const +{ + QgsVectorLayer *layer = QgsMemoryProviderUtils::createMemoryLayer( QStringLiteral( "auxiliary_layer" ), fields(), mLayer->wkbType(), mLayer->crs() ); + + QString pkField = mJoinInfo.targetFieldName(); + QgsFeature joinFeature; + QgsFeature targetFeature; + QgsFeatureIterator it = getFeatures(); + + layer->startEditing(); + while ( it.nextFeature( joinFeature ) ) + { + QString filter = QgsExpression::createFieldEqualityExpression( pkField, joinFeature.attribute( AS_JOINFIELD ) ); + + QgsFeatureRequest request; + request.setFilterExpression( filter ); + + mLayer->getFeatures( request ).nextFeature( targetFeature ); + + if ( targetFeature.isValid() ) + { + QgsFeature newFeature( joinFeature ); + newFeature.setGeometry( targetFeature.geometry() ); + layer->addFeature( newFeature ); + } + } + layer->commitChanges(); + + return layer; +} + +QgsVectorLayerJoinInfo QgsAuxiliaryLayer::joinInfo() const +{ + return mJoinInfo; +} + +bool QgsAuxiliaryLayer::exists( const QgsPropertyDefinition &definition ) const +{ + return ( indexOfPropertyDefinition( definition ) >= 0 ); +} + +bool QgsAuxiliaryLayer::addAuxiliaryField( const QgsPropertyDefinition &definition ) +{ + if ( ( definition.name().isEmpty() && definition.comment().isEmpty() ) || exists( definition ) ) + return false; + + const QgsField af = createAuxiliaryField( definition ); + const bool rc = addAttribute( af ); + updateFields(); + + if ( rc ) + { + int auxIndex = indexOfPropertyDefinition( definition ); + int index = mLayer->fields().indexOf( nameFromProperty( definition, true ) ); + + if ( index >= 0 && auxIndex >= 0 ) + { + if ( isHiddenProperty( auxIndex ) ) + { + // update editor widget + QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( QStringLiteral( "Hidden" ), QVariantMap() ); + setEditorWidgetSetup( auxIndex, setup ); + + // column is hidden + QgsAttributeTableConfig attrCfg = mLayer->attributeTableConfig(); + attrCfg.update( mLayer->fields() ); + QVector columns = attrCfg.columns(); + QVector::iterator it; + + for ( it = columns.begin(); it != columns.end(); ++it ) + { + if ( it->name.compare( mLayer->fields().field( index ).name() ) == 0 ) + it->hidden = true; + } + + attrCfg.setColumns( columns ); + mLayer->setAttributeTableConfig( attrCfg ); + } + else 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; +} + +QgsFields QgsAuxiliaryLayer::auxiliaryFields() const +{ + QgsFields afields; + + for ( int i = 2; i < fields().count(); i++ ) // ignore rowid and PK field + afields.append( createAuxiliaryField( fields().field( i ) ) ); + + return afields; +} + +bool QgsAuxiliaryLayer::deleteAttribute( int attr ) +{ + QgsVectorLayer::deleteAttribute( attr ); + bool rc = commitChanges(); + startEditing(); + return rc; +} + +bool QgsAuxiliaryLayer::save() +{ + bool rc = false; + + if ( isEditable() ) + { + rc = commitChanges(); + } + + startEditing(); + + return rc; +} + +int QgsAuxiliaryLayer::createProperty( QgsPalLayerSettings::Property property, const QString &providerId, QgsVectorLayer *layer ) +{ + int index = -1; + + if ( layer && layer->labeling() && layer->auxiliaryLayer() ) + { + const QgsPropertyDefinition def = layer->labeling()->settings( providerId ).propertyDefinitions()[property]; + const QString fieldName = nameFromProperty( def, true ); + + if ( layer->auxiliaryLayer()->addAuxiliaryField( def ) ) + { + const QgsProperty prop = QgsProperty::fromField( fieldName ); + + QgsPalLayerSettings *settings = new QgsPalLayerSettings( layer->labeling()->settings( providerId ) ); + + QgsPropertyCollection c = settings->dataDefinedProperties(); + c.setProperty( property, prop ); + settings->setDataDefinedProperties( c ); + + layer->labeling()->setSettings( settings, providerId ); + } + + index = layer->fields().lookupField( fieldName ); + } + + return index; +} + +int QgsAuxiliaryLayer::createProperty( QgsDiagramLayerSettings::Property property, QgsVectorLayer *layer ) +{ + int index = -1; + + if ( layer && layer->diagramLayerSettings() && layer->auxiliaryLayer() ) + { + const QgsPropertyDefinition def = layer->diagramLayerSettings()->propertyDefinitions()[property]; + + if ( layer->auxiliaryLayer()->addAuxiliaryField( def ) ) + { + const QString fieldName = nameFromProperty( def, true ); + const QgsProperty prop = QgsProperty::fromField( fieldName ); + + QgsDiagramLayerSettings settings( *layer->diagramLayerSettings() ); + + QgsPropertyCollection c = settings.dataDefinedProperties(); + c.setProperty( property, prop ); + settings.setDataDefinedProperties( c ); + + layer->setDiagramLayerSettings( settings ); + + index = layer->fields().lookupField( fieldName ); + } + } + + return index; +} + +bool QgsAuxiliaryLayer::isHiddenProperty( int index ) const +{ + bool hidden = false; + QgsPropertyDefinition def = propertyDefinitionFromIndex( index ); + + if ( def.origin().compare( "labeling" ) == 0 ) + { + Q_FOREACH ( const QgsPalLayerSettings::Property &p, palHiddenProperties ) + { + const QString propName = QgsPalLayerSettings::propertyDefinitions()[ p ].name(); + if ( propName.compare( def.name() ) == 0 ) + { + hidden = true; + break; + } + } + } + + return hidden; +} + +int QgsAuxiliaryLayer::propertyFromIndex( int index ) const +{ + int p = -1; + QgsPropertyDefinition aDef = propertyDefinitionFromIndex( index ); + + if ( aDef.origin().compare( "labeling" ) == 0 ) + { + const QgsPropertiesDefinition defs = QgsPalLayerSettings::propertyDefinitions(); + QgsPropertiesDefinition::const_iterator it = defs.constBegin(); + for ( ; it != defs.constEnd(); ++it ) + { + if ( it->name().compare( aDef.name(), Qt::CaseInsensitive ) == 0 ) + { + p = it.key(); + break; + } + } + } + else if ( aDef.origin().compare( "symbol" ) == 0 ) + { + const QgsPropertiesDefinition defs = QgsSymbolLayer::propertyDefinitions(); + QgsPropertiesDefinition::const_iterator it = defs.constBegin(); + for ( ; it != defs.constEnd(); ++it ) + { + if ( it->name().compare( aDef.name(), Qt::CaseInsensitive ) == 0 ) + { + p = it.key(); + break; + } + } + } + + return p; +} + +QgsPropertyDefinition QgsAuxiliaryLayer::propertyDefinitionFromIndex( int index ) const +{ + return propertyDefinitionFromField( fields().field( index ) ); +} + +int QgsAuxiliaryLayer::indexOfPropertyDefinition( const QgsPropertyDefinition &def ) const +{ + return fields().indexOf( nameFromProperty( def ) ); +} + +QString QgsAuxiliaryLayer::nameFromProperty( const QgsPropertyDefinition &def, bool joined ) +{ + QString fieldName = def.origin(); + + if ( !def.name().isEmpty() ) + fieldName = QString( "%1_%2" ).arg( fieldName, def.name().toLower() ); + + if ( !def.comment().isEmpty() ) + fieldName = QString( "%1_%2" ).arg( fieldName ).arg( def.comment() ); + + if ( joined ) + fieldName = QString( "%1%2" ).arg( AS_JOINPREFIX, fieldName ); + + return fieldName; +} + +QgsField QgsAuxiliaryLayer::createAuxiliaryField( const QgsPropertyDefinition &def ) +{ + QgsField afield; + + if ( !def.name().isEmpty() || !def.comment().isEmpty() ) + { + QVariant::Type type; + QString typeName; + int len( 0 ), precision( 0 ); + switch ( def.dataType() ) + { + case QgsPropertyDefinition::DataTypeString: + type = QVariant::String; + len = 50; + typeName = "String"; + break; + case QgsPropertyDefinition::DataTypeNumeric: + type = QVariant::Double; + len = 0; + precision = 0; + typeName = "Real"; + break; + case QgsPropertyDefinition::DataTypeBoolean: + type = QVariant::Int; // sqlite does not have a bool type + typeName = "Integer"; + break; + } + + afield.setType( type ); + afield.setName( nameFromProperty( def ) ); + afield.setTypeName( typeName ); + afield.setLength( len ); + afield.setPrecision( precision ); + } + + return afield; +} + +QgsPropertyDefinition QgsAuxiliaryLayer::propertyDefinitionFromField( const QgsField &f ) +{ + QgsPropertyDefinition def; const QStringList parts = f.name().split( '_' ); if ( parts.size() <= 1 ) - return; + return def; const QString origin = parts[0]; const QString propertyName = parts[1]; - QgsPropertyDefinition def; if ( origin.compare( "labeling", Qt::CaseInsensitive ) == 0 ) { @@ -121,351 +449,21 @@ QgsAuxiliaryField::QgsAuxiliaryField( const QgsField &f ) def.setComment( propertyName ); } + return def; +} + +QgsField QgsAuxiliaryLayer::createAuxiliaryField( const QgsField &field ) +{ + QgsPropertyDefinition def = propertyDefinitionFromField( field ); + QgsField afield; + if ( !def.name().isEmpty() || !def.comment().isEmpty() ) { - init( def ); - setTypeName( f.typeName() ); - mPropertyDefinition = def; - } -} - -void QgsAuxiliaryField::init( const QgsPropertyDefinition &def ) -{ - if ( !def.name().isEmpty() || !def.comment().isEmpty() ) - { - QVariant::Type type; - QString typeName; - int len( 0 ), precision( 0 ); - switch ( def.dataType() ) - { - case QgsPropertyDefinition::DataTypeString: - type = QVariant::String; - len = 50; - typeName = "String"; - break; - case QgsPropertyDefinition::DataTypeNumeric: - type = QVariant::Double; - len = 0; - precision = 0; - typeName = "Real"; - break; - case QgsPropertyDefinition::DataTypeBoolean: - type = QVariant::Int; // sqlite does not have a bool type - typeName = "Integer"; - break; - } - - setType( type ); - setName( nameFromProperty( def ) ); - setTypeName( typeName ); - setLength( len ); - setPrecision( precision ); - } -} - -bool QgsAuxiliaryLayer::clear() -{ - bool rc = deleteFeatures( allFeatureIds() ); - commitChanges(); - startEditing(); - return rc; -} - -QString QgsAuxiliaryField::nameFromProperty( const QgsPropertyDefinition &def, bool joined ) -{ - QString fieldName = def.origin(); - - if ( !def.name().isEmpty() ) - fieldName = QString( "%1_%2" ).arg( fieldName, def.name().toLower() ); - - if ( !def.comment().isEmpty() ) - fieldName = QString( "%1_%2" ).arg( fieldName ).arg( def.comment() ); - - if ( joined ) - fieldName = QString( "%1%2" ).arg( AS_JOINPREFIX, fieldName ); - - return fieldName; -} - -QgsPropertyDefinition QgsAuxiliaryField::propertyDefinition() const -{ - return mPropertyDefinition; -} - -// -// QgsAuxiliaryLayer -// - -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" ) - , mFileName( filename ) - , mTable( table ) - , mLayer( vlayer ) -{ - // init join info - mJoinInfo.setPrefix( AS_JOINPREFIX ); - mJoinInfo.setJoinLayer( this ); - mJoinInfo.setJoinFieldName( AS_JOINFIELD ); - mJoinInfo.setTargetFieldName( pkField ); - mJoinInfo.setEditable( true ); - mJoinInfo.setUpsertOnEdit( true ); - mJoinInfo.setCascadedDelete( true ); - mJoinInfo.setJoinFieldNamesBlackList( QStringList() << QStringLiteral( "rowid" ) ); // introduced by ogr provider -} - -QgsAuxiliaryLayer *QgsAuxiliaryLayer::clone( QgsVectorLayer *target ) const -{ - QgsAuxiliaryStorage::duplicateTable( source(), target->id() ); - return new QgsAuxiliaryLayer( mJoinInfo.targetFieldName(), mFileName, target->id(), target ); -} - -QgsVectorLayer *QgsAuxiliaryLayer::toSpatialLayer() const -{ - QgsVectorLayer *layer = QgsMemoryProviderUtils::createMemoryLayer( QStringLiteral( "auxiliary_layer" ), fields(), mLayer->wkbType(), mLayer->crs() ); - - QString pkField = mJoinInfo.targetFieldName(); - QgsFeature joinFeature; - QgsFeature targetFeature; - QgsFeatureIterator it = getFeatures(); - - layer->startEditing(); - while ( it.nextFeature( joinFeature ) ) - { - QString filter = QgsExpression::createFieldEqualityExpression( pkField, joinFeature.attribute( AS_JOINFIELD ) ); - - QgsFeatureRequest request; - request.setFilterExpression( filter ); - - mLayer->getFeatures( request ).nextFeature( targetFeature ); - - if ( targetFeature.isValid() ) - { - QgsFeature newFeature( joinFeature ); - newFeature.setGeometry( targetFeature.geometry() ); - layer->addFeature( newFeature ); - } - } - layer->commitChanges(); - - return layer; -} - -QgsVectorLayerJoinInfo QgsAuxiliaryLayer::joinInfo() const -{ - return mJoinInfo; -} - -bool QgsAuxiliaryLayer::exists( const QgsPropertyDefinition &definition ) const -{ - return ( indexOfProperty( definition ) >= 0 ); -} - -bool QgsAuxiliaryLayer::addAuxiliaryField( const QgsPropertyDefinition &definition ) -{ - if ( ( definition.name().isEmpty() && definition.comment().isEmpty() ) || exists( definition ) ) - return false; - - const QgsAuxiliaryField af( definition ); - 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 ( isHiddenProperty( auxIndex ) ) - { - // update editor widget - QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( QStringLiteral( "Hidden" ), QVariantMap() ); - setEditorWidgetSetup( auxIndex, setup ); - - // column is hidden - QgsAttributeTableConfig attrCfg = mLayer->attributeTableConfig(); - attrCfg.update( mLayer->fields() ); - QVector columns = attrCfg.columns(); - QVector::iterator it; - - for ( it = columns.begin(); it != columns.end(); ++it ) - { - if ( it->name.compare( mLayer->fields().field( index ).name() ) == 0 ) - it->hidden = true; - } - - attrCfg.setColumns( columns ); - mLayer->setAttributeTableConfig( attrCfg ); - } - else if ( definition.standardTemplate() == QgsPropertyDefinition::ColorNoAlpha - || definition.standardTemplate() == QgsPropertyDefinition::ColorWithAlpha ) - { - QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( QStringLiteral( "Color" ), QVariantMap() ); - setEditorWidgetSetup( auxIndex, setup ); - } - - mLayer->setEditorWidgetSetup( index, editorWidgetSetup( auxIndex ) ); - } + afield = createAuxiliaryField( def ); + afield.setTypeName( field.typeName() ); } - return rc; -} - -QgsAuxiliaryFields QgsAuxiliaryLayer::auxiliaryFields() const -{ - QgsAuxiliaryFields afields; - - for ( int i = 2; i < fields().count(); i++ ) // ignore rowid and PK field - afields.append( QgsAuxiliaryField( fields().field( i ) ) ); - - return afields; -} - -bool QgsAuxiliaryLayer::deleteAttribute( int attr ) -{ - QgsVectorLayer::deleteAttribute( attr ); - bool rc = commitChanges(); - startEditing(); - return rc; -} - -bool QgsAuxiliaryLayer::save() -{ - bool rc = false; - - if ( isEditable() ) - { - rc = commitChanges(); - } - - startEditing(); - - return rc; -} - -int QgsAuxiliaryLayer::createProperty( QgsPalLayerSettings::Property property, const QString &providerId, QgsVectorLayer *layer ) -{ - int index = -1; - - if ( layer && layer->labeling() && layer->auxiliaryLayer() ) - { - const QgsPropertyDefinition def = layer->labeling()->settings( providerId ).propertyDefinitions()[property]; - const QString fieldName = QgsAuxiliaryField::nameFromProperty( def, true ); - - if ( layer->auxiliaryLayer()->addAuxiliaryField( def ) ) - { - const QgsProperty prop = QgsProperty::fromField( fieldName ); - - QgsPalLayerSettings *settings = new QgsPalLayerSettings( layer->labeling()->settings( providerId ) ); - - QgsPropertyCollection c = settings->dataDefinedProperties(); - c.setProperty( property, prop ); - settings->setDataDefinedProperties( c ); - - layer->labeling()->setSettings( settings, providerId ); - } - - index = layer->fields().lookupField( fieldName ); - } - - return index; -} - -int QgsAuxiliaryLayer::createProperty( QgsDiagramLayerSettings::Property property, QgsVectorLayer *layer ) -{ - int index = -1; - - if ( layer && layer->diagramLayerSettings() && layer->auxiliaryLayer() ) - { - const QgsPropertyDefinition def = layer->diagramLayerSettings()->propertyDefinitions()[property]; - - if ( layer->auxiliaryLayer()->addAuxiliaryField( def ) ) - { - const QString fieldName = QgsAuxiliaryField::nameFromProperty( def, true ); - const QgsProperty prop = QgsProperty::fromField( fieldName ); - - QgsDiagramLayerSettings settings( *layer->diagramLayerSettings() ); - - QgsPropertyCollection c = settings.dataDefinedProperties(); - c.setProperty( property, prop ); - settings.setDataDefinedProperties( c ); - - layer->setDiagramLayerSettings( settings ); - - index = layer->fields().lookupField( fieldName ); - } - } - - return index; -} - -bool QgsAuxiliaryLayer::isHiddenProperty( int index ) const -{ - bool hidden = false; - - QgsAuxiliaryField aField( fields().field( index ) ); - QgsPropertyDefinition def = aField.propertyDefinition(); - - if ( def.origin().compare( "labeling" ) == 0 ) - { - Q_FOREACH ( const QgsPalLayerSettings::Property &p, palHiddenProperties ) - { - const QString propName = QgsPalLayerSettings::propertyDefinitions()[ p ].name(); - if ( propName.compare( def.name() ) == 0 ) - { - hidden = true; - break; - } - } - } - - return hidden; -} - -int QgsAuxiliaryLayer::propertyFromIndex( int index ) const -{ - int p = -1; - QgsAuxiliaryField aField( fields().field( index ) ); - QgsPropertyDefinition aDef = aField.propertyDefinition(); - - if ( aDef.origin().compare( "labeling" ) == 0 ) - { - const QgsPropertiesDefinition defs = QgsPalLayerSettings::propertyDefinitions(); - QgsPropertiesDefinition::const_iterator it = defs.constBegin(); - for ( ; it != defs.constEnd(); ++it ) - { - if ( it->name().compare( aDef.name(), Qt::CaseInsensitive ) == 0 ) - { - p = it.key(); - break; - } - } - } - else if ( aDef.origin().compare( "symbol" ) == 0 ) - { - const QgsPropertiesDefinition defs = QgsSymbolLayer::propertyDefinitions(); - QgsPropertiesDefinition::const_iterator it = defs.constBegin(); - for ( ; it != defs.constEnd(); ++it ) - { - if ( it->name().compare( aDef.name(), Qt::CaseInsensitive ) == 0 ) - { - p = it.key(); - break; - } - } - } - - return p; -} - -QgsPropertyDefinition QgsAuxiliaryLayer::propertyDefinitionFromIndex( int index ) const -{ - return QgsAuxiliaryField( fields().field( index ) ).propertyDefinition(); -} - -int QgsAuxiliaryLayer::indexOfProperty( const QgsPropertyDefinition &def ) const -{ - return fields().indexOf( QgsAuxiliaryField::nameFromProperty( def ) ); + return afield; } // diff --git a/src/core/qgsauxiliarystorage.h b/src/core/qgsauxiliarystorage.h index e7f48f1ef46..4dac3da0e5f 100644 --- a/src/core/qgsauxiliarystorage.h +++ b/src/core/qgsauxiliarystorage.h @@ -31,57 +31,6 @@ class QgsProject; -/** - * \class QgsAuxiliaryField - * - * \ingroup core - * - * \brief Class allowing to manage fields from of auxiliary layers - * - * \since QGIS 3.0 - */ -class CORE_EXPORT QgsAuxiliaryField : public QgsField -{ - public: - - /** - * Constructor - * - * \param def Definition of the property to be stored by this auxiliary - * field. - */ - explicit QgsAuxiliaryField( const QgsPropertyDefinition &def ); - - /** - * Destructor - */ - virtual ~QgsAuxiliaryField() = default; - - /** - * Returns the property definition corresponding to this field. - */ - QgsPropertyDefinition propertyDefinition() const; - - /** - * Returns the name of the auxiliary field for a property definition. - * - * \returns def The property definition - * \returns joined The join prefix is tok into account if true - */ - static QString nameFromProperty( const QgsPropertyDefinition &def, bool joined = false ); - - private: - QgsAuxiliaryField( const QgsField &f ); // only for auxiliary layer - - void init( const QgsPropertyDefinition &def ); - - QgsPropertyDefinition mPropertyDefinition; - - friend class QgsAuxiliaryLayer; -}; - -typedef QList QgsAuxiliaryFields; - /** * \class QgsAuxiliaryLayer * @@ -191,7 +140,7 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer /** * Returns a list of all auxiliary fields currently managed by the layer. */ - QgsAuxiliaryFields auxiliaryFields() const; + QgsFields auxiliaryFields() const; /** * Commit changes and starts editing then. @@ -226,7 +175,7 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer * * \returns The index of the field corresponding to the property or -1 */ - int indexOfProperty( const QgsPropertyDefinition &definition ) const; + int indexOfPropertyDefinition( const QgsPropertyDefinition &definition ) const; /** * Returns the underlying property key for the field index. The key may be @@ -235,8 +184,6 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer * happened. * * \param index The index of the field - * - * \since QGIS 3.0 */ int propertyFromIndex( int index ) const; @@ -244,13 +191,11 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer * Returns the property definition fir the underlying field index. * * \param index The index of the field - * - * \since QGIS 3.0 */ QgsPropertyDefinition propertyDefinitionFromIndex( int index ) const; /** - * Create if necessary a new auxiliary field for a PAL property and + * Creates if necessary a new auxiliary field for a PAL property and * activate this property in settings. * * \param property The property to create @@ -262,7 +207,7 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer static int createProperty( QgsPalLayerSettings::Property property, const QString &providerId, QgsVectorLayer *vlayer ); /** - * Create if necessary a new auxiliary field for a diagram's property and + * Creates if necessary a new auxiliary field for a diagram's property and * activate this this property in settings. * * \param property The property to create @@ -272,6 +217,35 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer */ static int createProperty( QgsDiagramLayerSettings::Property property, QgsVectorLayer *vlayer ); + /** + * Creates a new auxiliary field from a property definition. + * + * \param definition The property definition of the auxiliary field to create + */ + static QgsField createAuxiliaryField( const QgsPropertyDefinition &definition ); + + /** + * Creates a new auxiliary field from a field. + * + * \param field The field to use to create the auxiliary field + */ + static QgsField createAuxiliaryField( const QgsField &field ); + + /** + * Returns the name of the auxiliary field for a property definition. + * + * \param def The property definition + * \param joined The join prefix is taken into account if true + */ + static QString nameFromProperty( const QgsPropertyDefinition &def, bool joined = false ); + + /** + * Returns the property definition from an auxiliary field. + * + * \param field The auxiliary field + */ + static QgsPropertyDefinition propertyDefinitionFromField( const QgsField &field ); + private: QgsVectorLayerJoinInfo mJoinInfo; QString mFileName; diff --git a/src/gui/qgsnewauxiliaryfielddialog.cpp b/src/gui/qgsnewauxiliaryfielddialog.cpp index 4161df2cf57..fe9f5e98ddc 100644 --- a/src/gui/qgsnewauxiliaryfielddialog.cpp +++ b/src/gui/qgsnewauxiliaryfielddialog.cpp @@ -29,8 +29,8 @@ QgsNewAuxiliaryFieldDialog::QgsNewAuxiliaryFieldDialog( const QgsPropertyDefinit setupUi( this ); mType->addItem( tr( "String" ) ); - mType->addItem( tr( "Numeric" ) ); - mType->addItem( tr( "Boolean" ) ); + mType->addItem( tr( "Real" ) ); + mType->addItem( tr( "Integer" ) ); switch ( def.dataType() ) { @@ -38,10 +38,10 @@ QgsNewAuxiliaryFieldDialog::QgsNewAuxiliaryFieldDialog( const QgsPropertyDefinit mType->setCurrentIndex( mType->findText( tr( "String" ) ) ); break; case QgsPropertyDefinition::DataTypeNumeric: - mType->setCurrentIndex( mType->findText( tr( "Numeric" ) ) ); + mType->setCurrentIndex( mType->findText( tr( "Real" ) ) ); break; case QgsPropertyDefinition::DataTypeBoolean: - mType->setCurrentIndex( mType->findText( tr( "Boolean" ) ) ); + mType->setCurrentIndex( mType->findText( tr( "Integer" ) ) ); break; } @@ -62,7 +62,7 @@ void QgsNewAuxiliaryFieldDialog::accept() { def.setDataType( QgsPropertyDefinition::DataTypeString ); } - else if ( mType->currentText().compare( tr( "Numeric" ) ) == 0 ) + else if ( mType->currentText().compare( tr( "Real" ) ) == 0 ) { def.setDataType( QgsPropertyDefinition::DataTypeNumeric ); } @@ -74,7 +74,7 @@ void QgsNewAuxiliaryFieldDialog::accept() def.setOrigin( "user" ); } - QString fieldName = QgsAuxiliaryField::nameFromProperty( def, true ); + QString fieldName = QgsAuxiliaryLayer::nameFromProperty( def, true ); const int idx = mLayer->fields().lookupField( fieldName ); if ( idx >= 0 ) { diff --git a/src/gui/qgspropertyoverridebutton.cpp b/src/gui/qgspropertyoverridebutton.cpp index e0a1ea5751c..4b9c52b621e 100644 --- a/src/gui/qgspropertyoverridebutton.cpp +++ b/src/gui/qgspropertyoverridebutton.cpp @@ -345,7 +345,7 @@ void QgsPropertyOverrideButton::aboutToShowMenu() mActionCreateAuxiliaryField->setChecked( false ); if ( alayer && alayer->exists( mDefinition ) ) { - if ( mProperty.field() == QgsAuxiliaryField::nameFromProperty( mDefinition, true ) ) + if ( mProperty.field() == QgsAuxiliaryLayer::nameFromProperty( mDefinition, true ) ) { mActionCreateAuxiliaryField->setEnabled( false ); mActionCreateAuxiliaryField->setChecked( true ); diff --git a/src/gui/symbology/qgssymbollayerwidget.cpp b/src/gui/symbology/qgssymbollayerwidget.cpp index 92a87464ef2..02d7dda48fb 100644 --- a/src/gui/symbology/qgssymbollayerwidget.cpp +++ b/src/gui/symbology/qgssymbollayerwidget.cpp @@ -150,7 +150,7 @@ void QgsSymbolLayerWidget::createAuxiliaryField() // update property with join field name from auxiliary storage QgsProperty property = button->toProperty(); - property.setField( QgsAuxiliaryField::nameFromProperty( def, true ) ); + property.setField( QgsAuxiliaryLayer::nameFromProperty( def, true ) ); property.setActive( true ); button->updateFieldLists(); button->setToProperty( property ); diff --git a/src/gui/symbology/qgssymbolslistwidget.cpp b/src/gui/symbology/qgssymbolslistwidget.cpp index 91d37d86e31..2171e3c8736 100644 --- a/src/gui/symbology/qgssymbolslistwidget.cpp +++ b/src/gui/symbology/qgssymbolslistwidget.cpp @@ -156,7 +156,7 @@ void QgsSymbolsListWidget::createAuxiliaryField() // update property with join field name from auxiliary storage QgsProperty property = button->toProperty(); - property.setField( QgsAuxiliaryField::nameFromProperty( def, true ) ); + property.setField( QgsAuxiliaryLayer::nameFromProperty( def, true ) ); property.setActive( true ); button->updateFieldLists(); button->setToProperty( property );