Skip to content

Commit

Permalink
Merge pull request #5448 from signedav/relationshipstrength
Browse files Browse the repository at this point in the history
Duplication Feature and its relating Features
  • Loading branch information
m-kuhn committed Oct 26, 2017
2 parents acbd9d1 + 3dbffb0 commit dc5eefb
Show file tree
Hide file tree
Showing 14 changed files with 507 additions and 40 deletions.
22 changes: 22 additions & 0 deletions python/core/qgsrelation.sip
Expand Up @@ -19,6 +19,13 @@ class QgsRelation
%End %End
public: public:


enum RelationStrength
{
Association,
Composition

};



QgsRelation(); QgsRelation();
%Docstring %Docstring
Expand Down Expand Up @@ -53,6 +60,12 @@ class QgsRelation
Set a name for this relation Set a name for this relation
%End %End


void setStrength( const RelationStrength &strength );
%Docstring
Set a strength for this relation
.. versionadded:: 3.0
%End

void setReferencingLayer( const QString &id ); void setReferencingLayer( const QString &id );
%Docstring %Docstring
Set the referencing (child) layer id. This layer will be searched in the registry. Set the referencing (child) layer id. This layer will be searched in the registry.
Expand Down Expand Up @@ -158,6 +171,15 @@ class QgsRelation
:rtype: str :rtype: str
%End %End


RelationStrength strength() const;
%Docstring
Returns the relation strength as a string

:return: strength
.. versionadded:: 3.0
:rtype: RelationStrength
%End

QString id() const; QString id() const;
%Docstring %Docstring
A (project-wide) unique id for this relation A (project-wide) unique id for this relation
Expand Down
44 changes: 44 additions & 0 deletions python/core/qgsvectorlayerutils.sip
Expand Up @@ -22,6 +22,38 @@ class QgsVectorLayerUtils
%End %End
public: public:


class QgsDuplicateFeatureContext
{
%Docstring
Contains mainly the QMap with QgsVectorLayer and QgsFeatureIds do list all the duplicated features

.. versionadded:: 3.0
%End

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

QgsDuplicateFeatureContext();

QList<QgsVectorLayer *> layers() const;
%Docstring
Returns all the layers on which features have been duplicated
.. versionadded:: 3.0
:rtype: list of QgsVectorLayer
%End

QgsFeatureIds duplicatedFeatures( QgsVectorLayer *layer ) const;
%Docstring
Returns the duplicated features in the given layer
.. versionadded:: 3.0
:rtype: QgsFeatureIds
%End


};

static bool valueExists( const QgsVectorLayer *layer, int fieldIndex, const QVariant &value, const QgsFeatureIds &ignoreIds = QgsFeatureIds() ); static bool valueExists( const QgsVectorLayer *layer, int fieldIndex, const QVariant &value, const QgsFeatureIds &ignoreIds = QgsFeatureIds() );
%Docstring %Docstring
Returns true if the specified value already exists within a field. This method can be used to test for uniqueness Returns true if the specified value already exists within a field. This method can be used to test for uniqueness
Expand Down Expand Up @@ -62,8 +94,20 @@ class QgsVectorLayerUtils
:rtype: QgsFeature :rtype: QgsFeature
%End %End


static QgsFeature duplicateFeature( QgsVectorLayer *layer, const QgsFeature &feature, QgsProject *project, int depth, QgsDuplicateFeatureContext &duplicateFeatureContext /Out/ );
%Docstring
Duplicates a feature and it's children (one level deep). It calls CreateFeature, so
default values and constraints (e.g., unique constraints) will automatically be handled.
The duplicated feature will be automatically inserted into the layer.
``depth`` the higher this number the deeper the level - With depth > 0 the children of the feature are not duplicated
``duplicateFeatureContext`` stores all the layers and the featureids of the duplicated features (incl. children)
.. versionadded:: 3.0
:rtype: QgsFeature
%End

}; };



/************************************************************************ /************************************************************************
* This file has been generated automatically from * * This file has been generated automatically from *
* * * *
Expand Down
11 changes: 11 additions & 0 deletions src/app/qgsdiscoverrelationsdlg.cpp
Expand Up @@ -43,6 +43,17 @@ void QgsDiscoverRelationsDlg::addRelation( const QgsRelation &rel )
mRelationsTable->setItem( row, 2, new QTableWidgetItem( rel.fieldPairs().at( 0 ).referencingField() ) ); mRelationsTable->setItem( row, 2, new QTableWidgetItem( rel.fieldPairs().at( 0 ).referencingField() ) );
mRelationsTable->setItem( row, 3, new QTableWidgetItem( rel.referencedLayer()->name() ) ); mRelationsTable->setItem( row, 3, new QTableWidgetItem( rel.referencedLayer()->name() ) );
mRelationsTable->setItem( row, 4, new QTableWidgetItem( rel.fieldPairs().at( 0 ).referencedField() ) ); mRelationsTable->setItem( row, 4, new QTableWidgetItem( rel.fieldPairs().at( 0 ).referencedField() ) );
if ( rel.strength() == QgsRelation::RelationStrength::Composition )
{
mRelationsTable->setItem( row, 5, new QTableWidgetItem( QStringLiteral( "Composition" ) ) );
}
else
{
mRelationsTable->setItem( row, 5, new QTableWidgetItem( QStringLiteral( "Association" ) ) );
}

mRelationsTable->item( row, 5 )->setToolTip( QStringLiteral( "Composition (child features will be copied too) or Association" ) );

} }


QList<QgsRelation> QgsDiscoverRelationsDlg::relations() const QList<QgsRelation> QgsDiscoverRelationsDlg::relations() const
Expand Down
8 changes: 8 additions & 0 deletions src/app/qgsrelationadddlg.cpp
Expand Up @@ -33,6 +33,10 @@ QgsRelationAddDlg::QgsRelationAddDlg( QWidget *parent )
mCbxReferencedLayer->setFilters( QgsMapLayerProxyModel::VectorLayer ); mCbxReferencedLayer->setFilters( QgsMapLayerProxyModel::VectorLayer );
mCbxReferencedField->setLayer( mCbxReferencedLayer->currentLayer() ); mCbxReferencedField->setLayer( mCbxReferencedLayer->currentLayer() );


mCbxRelationStrength->addItem( "Association", QVariant::fromValue( QgsRelation::RelationStrength::Association ) );
mCbxRelationStrength->addItem( "Composition", QVariant::fromValue( QgsRelation::RelationStrength::Composition ) );
mCbxRelationStrength->setToolTip( QStringLiteral( "Composition (child features will be copied too) or Association" ) );

mTxtRelationId->setPlaceholderText( tr( "[Generated automatically]" ) ); mTxtRelationId->setPlaceholderText( tr( "[Generated automatically]" ) );
checkDefinitionValid(); checkDefinitionValid();


Expand Down Expand Up @@ -74,6 +78,10 @@ QString QgsRelationAddDlg::relationName()
return mTxtRelationName->text(); return mTxtRelationName->text();
} }


QgsRelation::RelationStrength QgsRelationAddDlg::relationStrength()
{
return mCbxRelationStrength->currentData().value<QgsRelation::RelationStrength>();
}


void QgsRelationAddDlg::checkDefinitionValid() void QgsRelationAddDlg::checkDefinitionValid()
{ {
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsrelationadddlg.h
Expand Up @@ -18,6 +18,7 @@
#include <QDialog> #include <QDialog>
#include "ui_qgsrelationadddlgbase.h" #include "ui_qgsrelationadddlgbase.h"
#include "qgis_app.h" #include "qgis_app.h"
#include "qgsrelation.h"


class QgsVectorLayer; class QgsVectorLayer;


Expand All @@ -33,7 +34,7 @@ class APP_EXPORT QgsRelationAddDlg : public QDialog, private Ui::QgsRelationAddD
QList< QPair< QString, QString > > references(); QList< QPair< QString, QString > > references();
QString relationId(); QString relationId();
QString relationName(); QString relationName();

QgsRelation::RelationStrength relationStrength();


private slots: private slots:


Expand Down
14 changes: 14 additions & 0 deletions src/app/qgsrelationmanagerdialog.cpp
Expand Up @@ -77,6 +77,19 @@ void QgsRelationManagerDialog::addRelation( const QgsRelation &rel )
item = new QTableWidgetItem( rel.id() ); item = new QTableWidgetItem( rel.id() );
item->setFlags( Qt::ItemIsEditable ); item->setFlags( Qt::ItemIsEditable );
mRelationsTable->setItem( row, 5, item ); mRelationsTable->setItem( row, 5, item );


if ( rel.strength() == QgsRelation::RelationStrength::Composition )
{
item = new QTableWidgetItem( QStringLiteral( "Composition" ) );
}
else
{
item = new QTableWidgetItem( QStringLiteral( "Association" ) );
}
item->setFlags( Qt::ItemIsEditable );
mRelationsTable->setItem( row, 6, item );

mRelationsTable->setSortingEnabled( true ); mRelationsTable->setSortingEnabled( true );
} }


Expand Down Expand Up @@ -116,6 +129,7 @@ void QgsRelationManagerDialog::mBtnAddRelation_clicked()
relation.setId( relationId ); relation.setId( relationId );
relation.addFieldPair( addDlg.references().at( 0 ).first, addDlg.references().at( 0 ).second ); relation.addFieldPair( addDlg.references().at( 0 ).first, addDlg.references().at( 0 ).second );
relation.setName( addDlg.relationName() ); relation.setName( addDlg.relationName() );
relation.setStrength( addDlg.relationStrength() );


addRelation( relation ); addRelation( relation );
} }
Expand Down
28 changes: 28 additions & 0 deletions src/core/qgsrelation.cpp
Expand Up @@ -36,6 +36,7 @@ QgsRelation QgsRelation::createFromXml( const QDomNode &node )
QString referencedLayerId = elem.attribute( QStringLiteral( "referencedLayer" ) ); QString referencedLayerId = elem.attribute( QStringLiteral( "referencedLayer" ) );
QString id = elem.attribute( QStringLiteral( "id" ) ); QString id = elem.attribute( QStringLiteral( "id" ) );
QString name = elem.attribute( QStringLiteral( "name" ) ); QString name = elem.attribute( QStringLiteral( "name" ) );
QString strength = elem.attribute( QStringLiteral( "strength" ) );


const QMap<QString, QgsMapLayer *> &mapLayers = QgsProject::instance()->mapLayers(); const QMap<QString, QgsMapLayer *> &mapLayers = QgsProject::instance()->mapLayers();


Expand Down Expand Up @@ -66,6 +67,14 @@ QgsRelation QgsRelation::createFromXml( const QDomNode &node )
relation.mReferencedLayer = qobject_cast<QgsVectorLayer *>( referencedLayer ); relation.mReferencedLayer = qobject_cast<QgsVectorLayer *>( referencedLayer );
relation.mRelationId = id; relation.mRelationId = id;
relation.mRelationName = name; relation.mRelationName = name;
if ( strength == "Composition" )
{
relation.mRelationStrength = RelationStrength::Composition;
}
else
{
relation.mRelationStrength = RelationStrength::Association;
}


QDomNodeList references = elem.elementsByTagName( QStringLiteral( "fieldRef" ) ); QDomNodeList references = elem.elementsByTagName( QStringLiteral( "fieldRef" ) );
for ( int i = 0; i < references.size(); ++i ) for ( int i = 0; i < references.size(); ++i )
Expand All @@ -90,6 +99,14 @@ void QgsRelation::writeXml( QDomNode &node, QDomDocument &doc ) const
elem.setAttribute( QStringLiteral( "name" ), mRelationName ); elem.setAttribute( QStringLiteral( "name" ), mRelationName );
elem.setAttribute( QStringLiteral( "referencingLayer" ), mReferencingLayerId ); elem.setAttribute( QStringLiteral( "referencingLayer" ), mReferencingLayerId );
elem.setAttribute( QStringLiteral( "referencedLayer" ), mReferencedLayerId ); elem.setAttribute( QStringLiteral( "referencedLayer" ), mReferencedLayerId );
if ( mRelationStrength == RelationStrength::Composition )
{
elem.setAttribute( QStringLiteral( "strength" ), QStringLiteral( "Composition" ) );
}
else
{
elem.setAttribute( QStringLiteral( "strength" ), QStringLiteral( "Association" ) );
}


Q_FOREACH ( const FieldPair &fields, mFieldPairs ) Q_FOREACH ( const FieldPair &fields, mFieldPairs )
{ {
Expand All @@ -114,6 +131,12 @@ void QgsRelation::setName( const QString &name )
mRelationName = name; mRelationName = name;
} }



void QgsRelation::setStrength( const RelationStrength &strength )
{
mRelationStrength = strength;
}

void QgsRelation::setReferencingLayer( const QString &id ) void QgsRelation::setReferencingLayer( const QString &id )
{ {
mReferencingLayerId = id; mReferencingLayerId = id;
Expand Down Expand Up @@ -206,6 +229,11 @@ QString QgsRelation::name() const
return mRelationName; return mRelationName;
} }


QgsRelation::RelationStrength QgsRelation::strength() const
{
return mRelationStrength;
}

QString QgsRelation::id() const QString QgsRelation::id() const
{ {
return mRelationId; return mRelationId;
Expand Down
28 changes: 28 additions & 0 deletions src/core/qgsrelation.h
Expand Up @@ -48,6 +48,17 @@ class CORE_EXPORT QgsRelation


public: public:


/**
* enum for the relation strength
* Association, Composition
*/
enum RelationStrength
{
Association, //!< Loose relation, related elements are not part of the parent and a parent copy will not copy any children.
Composition //!< Fix relation, related elements are part of the parent and a parent copy will copy any children or delete of parent will delete children

};

#ifndef SIP_RUN #ifndef SIP_RUN


/** /**
Expand Down Expand Up @@ -109,6 +120,12 @@ class CORE_EXPORT QgsRelation
*/ */
void setName( const QString &name ); void setName( const QString &name );


/**
* Set a strength for this relation
* \since QGIS 3.0
*/
void setStrength( const RelationStrength &strength );

/** /**
* Set the referencing (child) layer id. This layer will be searched in the registry. * Set the referencing (child) layer id. This layer will be searched in the registry.
*/ */
Expand Down Expand Up @@ -214,6 +231,14 @@ class CORE_EXPORT QgsRelation
*/ */
QString name() const; QString name() const;


/**
* Returns the relation strength as a string
*
* \returns strength
* \since QGIS 3.0
*/
RelationStrength strength() const;

/** /**
* A (project-wide) unique id for this relation * A (project-wide) unique id for this relation
* *
Expand Down Expand Up @@ -345,6 +370,8 @@ class CORE_EXPORT QgsRelation
//! The parent layer //! The parent layer
QgsVectorLayer *mReferencedLayer = nullptr; QgsVectorLayer *mReferencedLayer = nullptr;


RelationStrength mRelationStrength;

/** /**
* A list of fields which define the relation. * A list of fields which define the relation.
* In most cases there will be only one value, but multiple values * In most cases there will be only one value, but multiple values
Expand All @@ -357,5 +384,6 @@ class CORE_EXPORT QgsRelation


// Register QgsRelation for usage with QVariant // Register QgsRelation for usage with QVariant
Q_DECLARE_METATYPE( QgsRelation ) Q_DECLARE_METATYPE( QgsRelation )
Q_DECLARE_METATYPE( QgsRelation::RelationStrength )


#endif // QGSRELATION_H #endif // QGSRELATION_H

0 comments on commit dc5eefb

Please sign in to comment.