Skip to content

Commit

Permalink
[FEATURE] Duplicate features including children (one level deep) when…
Browse files Browse the repository at this point in the history
… relation strength is competition - configuration for the relationsstrength in the relation GUI `[needs-docs]`
  • Loading branch information
signedav committed Oct 25, 2017
1 parent 6297d19 commit f7073d0
Show file tree
Hide file tree
Showing 14 changed files with 503 additions and 40 deletions.
22 changes: 22 additions & 0 deletions python/core/qgsrelation.sip
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class QgsRelation
%End
public:

enum RelationStrength
{
Association,
Composition

};


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

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

void setReferencingLayer( const QString &id );
%Docstring
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
%End

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

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

QString id() const;
%Docstring
A (project-wide) unique id for this relation
Expand Down
42 changes: 42 additions & 0 deletions python/core/qgsvectorlayerutils.sip
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@ class QgsVectorLayerUtils
%End
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 in the member QMap mDuplicatedFeatures
.. 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() );
%Docstring
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,18 @@ class QgsVectorLayerUtils
:rtype: QgsFeature
%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.
.. versionadded:: 3.0
:rtype: QgsFeature
%End

};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
11 changes: 11 additions & 0 deletions src/app/qgsdiscoverrelationsdlg.cpp
Original file line number Diff line number Diff line change
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, 3, new QTableWidgetItem( rel.referencedLayer()->name() ) );
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
Expand Down
8 changes: 8 additions & 0 deletions src/app/qgsrelationadddlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ QgsRelationAddDlg::QgsRelationAddDlg( QWidget *parent )
mCbxReferencedLayer->setFilters( QgsMapLayerProxyModel::VectorLayer );
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]" ) );
checkDefinitionValid();

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

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

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

class QgsVectorLayer;

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

QgsRelation::RelationStrength relationStrength();

private slots:

Expand Down
14 changes: 14 additions & 0 deletions src/app/qgsrelationmanagerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ void QgsRelationManagerDialog::addRelation( const QgsRelation &rel )
item = new QTableWidgetItem( rel.id() );
item->setFlags( Qt::ItemIsEditable );
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 );
}

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

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

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.mRelationId = id;
relation.mRelationName = name;
if ( strength == "Composition" )
{
relation.mRelationStrength = RelationStrength::Composition;
}
else
{
relation.mRelationStrength = RelationStrength::Association;
}

QDomNodeList references = elem.elementsByTagName( QStringLiteral( "fieldRef" ) );
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( "referencingLayer" ), mReferencingLayerId );
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 )
{
Expand All @@ -114,6 +131,12 @@ void QgsRelation::setName( const QString &name )
mRelationName = name;
}


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

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

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

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

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

/**
Expand Down Expand Up @@ -109,6 +120,12 @@ class CORE_EXPORT QgsRelation
*/
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.
*/
Expand Down Expand Up @@ -214,6 +231,14 @@ class CORE_EXPORT QgsRelation
*/
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
*
Expand Down Expand Up @@ -345,6 +370,8 @@ class CORE_EXPORT QgsRelation
//! The parent layer
QgsVectorLayer *mReferencedLayer = nullptr;

RelationStrength mRelationStrength;

/**
* A list of fields which define the relation.
* 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
Q_DECLARE_METATYPE( QgsRelation )
Q_DECLARE_METATYPE( QgsRelation::RelationStrength )

#endif // QGSRELATION_H
Loading

0 comments on commit f7073d0

Please sign in to comment.