2013-10-04 14:47:59 +02:00
|
|
|
/***************************************************************************
|
|
|
|
qgsrelationmanagerdialog.cpp
|
|
|
|
--------------------------------------
|
|
|
|
Date : 23.2.2013
|
|
|
|
Copyright : (C) 2013 Matthias Kuhn
|
2015-08-30 12:59:30 +02:00
|
|
|
Email : matthias at opengis dot ch
|
2013-10-04 14:47:59 +02:00
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2016-09-26 13:51:37 +02:00
|
|
|
#include "qgsdiscoverrelationsdlg.h"
|
2013-10-04 14:47:59 +02:00
|
|
|
#include "qgsrelationadddlg.h"
|
|
|
|
#include "qgsrelationmanagerdialog.h"
|
|
|
|
#include "qgsrelationmanager.h"
|
|
|
|
#include "qgsvectorlayer.h"
|
|
|
|
|
2017-03-03 08:42:00 +01:00
|
|
|
QgsRelationManagerDialog::QgsRelationManagerDialog( QgsRelationManager *relationMgr, QWidget *parent )
|
|
|
|
: QWidget( parent )
|
|
|
|
, Ui::QgsRelationManagerDialogBase()
|
|
|
|
, mRelationManager( relationMgr )
|
2013-10-04 14:47:59 +02:00
|
|
|
{
|
|
|
|
setupUi( this );
|
2017-10-09 08:48:23 +10:00
|
|
|
connect( mBtnAddRelation, &QPushButton::clicked, this, &QgsRelationManagerDialog::mBtnAddRelation_clicked );
|
|
|
|
connect( mBtnDiscoverRelations, &QPushButton::clicked, this, &QgsRelationManagerDialog::mBtnDiscoverRelations_clicked );
|
|
|
|
connect( mBtnRemoveRelation, &QPushButton::clicked, this, &QgsRelationManagerDialog::mBtnRemoveRelation_clicked );
|
2016-11-08 13:36:23 +01:00
|
|
|
|
|
|
|
mBtnRemoveRelation->setEnabled( false );
|
|
|
|
connect( mRelationsTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsRelationManagerDialog::onSelectionChanged );
|
2013-10-04 14:47:59 +02:00
|
|
|
}
|
|
|
|
|
2017-03-03 08:42:00 +01:00
|
|
|
void QgsRelationManagerDialog::setLayers( const QList< QgsVectorLayer * > &layers )
|
2013-10-04 14:47:59 +02:00
|
|
|
{
|
|
|
|
mLayers = layers;
|
|
|
|
|
2017-03-03 08:42:00 +01:00
|
|
|
const QList<QgsRelation> &relations = mRelationManager->relations().values();
|
2013-10-04 14:47:59 +02:00
|
|
|
|
2017-03-03 08:42:00 +01:00
|
|
|
Q_FOREACH ( const QgsRelation &rel, relations )
|
2013-10-04 14:47:59 +02:00
|
|
|
{
|
|
|
|
addRelation( rel );
|
|
|
|
}
|
2016-04-08 08:16:40 +02:00
|
|
|
|
|
|
|
mRelationsTable->sortByColumn( 0, Qt::AscendingOrder );
|
2013-10-04 14:47:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void QgsRelationManagerDialog::addRelation( const QgsRelation &rel )
|
|
|
|
{
|
2016-09-26 13:51:37 +02:00
|
|
|
mRelationsTable->setSortingEnabled( false );
|
2013-10-04 14:47:59 +02:00
|
|
|
int row = mRelationsTable->rowCount();
|
|
|
|
mRelationsTable->insertRow( row );
|
|
|
|
|
2017-03-03 08:42:00 +01:00
|
|
|
QTableWidgetItem *item = new QTableWidgetItem( rel.name() );
|
2013-10-04 14:47:59 +02:00
|
|
|
// Save relation in first column's item
|
|
|
|
item->setData( Qt::UserRole, QVariant::fromValue<QgsRelation>( rel ) );
|
|
|
|
mRelationsTable->setItem( row, 0, item );
|
|
|
|
|
|
|
|
item = new QTableWidgetItem( rel.referencingLayer()->name() );
|
|
|
|
item->setFlags( Qt::ItemIsEditable );
|
|
|
|
mRelationsTable->setItem( row, 1, item );
|
|
|
|
|
2015-10-20 19:24:24 +11:00
|
|
|
item = new QTableWidgetItem( rel.fieldPairs().at( 0 ).referencingField() );
|
2013-10-04 14:47:59 +02:00
|
|
|
item->setFlags( Qt::ItemIsEditable );
|
|
|
|
mRelationsTable->setItem( row, 2, item );
|
|
|
|
|
|
|
|
item = new QTableWidgetItem( rel.referencedLayer()->name() );
|
|
|
|
item->setFlags( Qt::ItemIsEditable );
|
|
|
|
mRelationsTable->setItem( row, 3, item );
|
|
|
|
|
2015-10-20 19:24:24 +11:00
|
|
|
item = new QTableWidgetItem( rel.fieldPairs().at( 0 ).referencedField() );
|
2013-10-04 14:47:59 +02:00
|
|
|
item->setFlags( Qt::ItemIsEditable );
|
|
|
|
mRelationsTable->setItem( row, 4, item );
|
|
|
|
|
|
|
|
item = new QTableWidgetItem( rel.id() );
|
|
|
|
item->setFlags( Qt::ItemIsEditable );
|
|
|
|
mRelationsTable->setItem( row, 5, item );
|
2017-10-25 17:04:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
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 );
|
|
|
|
|
2016-09-26 13:51:37 +02:00
|
|
|
mRelationsTable->setSortingEnabled( true );
|
2013-10-04 14:47:59 +02:00
|
|
|
}
|
|
|
|
|
2017-10-09 08:48:23 +10:00
|
|
|
void QgsRelationManagerDialog::mBtnAddRelation_clicked()
|
2013-10-04 14:47:59 +02:00
|
|
|
{
|
|
|
|
QgsRelationAddDlg addDlg;
|
|
|
|
|
|
|
|
if ( addDlg.exec() )
|
|
|
|
{
|
|
|
|
QgsRelation relation;
|
|
|
|
|
|
|
|
relation.setReferencingLayer( addDlg.referencingLayerId() );
|
|
|
|
relation.setReferencedLayer( addDlg.referencedLayerId() );
|
|
|
|
QString relationId = addDlg.relationId();
|
2017-09-27 10:54:40 +10:00
|
|
|
if ( addDlg.relationId().isEmpty() )
|
2016-10-23 08:30:26 +10:00
|
|
|
relationId = QStringLiteral( "%1_%2_%3_%4" )
|
2017-11-14 10:20:07 +01:00
|
|
|
.arg( addDlg.referencingLayerId().left( 10 ),
|
2015-10-20 19:24:24 +11:00
|
|
|
addDlg.references().at( 0 ).first,
|
2017-11-14 10:20:07 +01:00
|
|
|
addDlg.referencedLayerId().left( 10 ),
|
2015-10-20 19:24:24 +11:00
|
|
|
addDlg.references().at( 0 ).second );
|
2013-10-04 14:47:59 +02:00
|
|
|
|
|
|
|
QStringList existingNames;
|
|
|
|
|
|
|
|
|
2017-03-03 08:42:00 +01:00
|
|
|
Q_FOREACH ( const QgsRelation &rel, relations() )
|
2013-10-04 14:47:59 +02:00
|
|
|
{
|
|
|
|
existingNames << rel.id();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString tempId = relationId + "_%1";
|
|
|
|
int suffix = 1;
|
|
|
|
while ( existingNames.contains( relationId ) )
|
|
|
|
{
|
|
|
|
relationId = tempId.arg( suffix );
|
|
|
|
++suffix;
|
|
|
|
}
|
2017-02-17 16:50:43 +01:00
|
|
|
relation.setId( relationId );
|
2015-10-20 19:24:24 +11:00
|
|
|
relation.addFieldPair( addDlg.references().at( 0 ).first, addDlg.references().at( 0 ).second );
|
2017-02-17 16:50:43 +01:00
|
|
|
relation.setName( addDlg.relationName() );
|
2017-10-25 17:04:48 +02:00
|
|
|
relation.setStrength( addDlg.relationStrength() );
|
2013-10-04 14:47:59 +02:00
|
|
|
|
|
|
|
addRelation( relation );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 08:48:23 +10:00
|
|
|
void QgsRelationManagerDialog::mBtnDiscoverRelations_clicked()
|
2016-09-26 13:51:37 +02:00
|
|
|
{
|
|
|
|
QgsDiscoverRelationsDlg discoverDlg( relations(), mLayers, this );
|
|
|
|
if ( discoverDlg.exec() )
|
|
|
|
{
|
2017-03-03 08:42:00 +01:00
|
|
|
Q_FOREACH ( const QgsRelation &relation, discoverDlg.relations() )
|
2016-09-26 13:51:37 +02:00
|
|
|
{
|
|
|
|
addRelation( relation );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 08:48:23 +10:00
|
|
|
void QgsRelationManagerDialog::mBtnRemoveRelation_clicked()
|
2013-10-04 14:47:59 +02:00
|
|
|
{
|
2016-11-08 13:12:28 +01:00
|
|
|
const QModelIndexList rows = mRelationsTable->selectionModel()->selectedRows();
|
|
|
|
for ( int i = rows.size() - 1; i >= 0; --i )
|
|
|
|
{
|
|
|
|
mRelationsTable->removeRow( rows[i].row() );
|
|
|
|
}
|
2013-10-04 14:47:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QList< QgsRelation > QgsRelationManagerDialog::relations()
|
|
|
|
{
|
|
|
|
QList< QgsRelation > relations;
|
|
|
|
|
2015-10-05 18:43:18 +11:00
|
|
|
int rows = mRelationsTable->rowCount();
|
|
|
|
relations.reserve( rows );
|
|
|
|
for ( int i = 0; i < rows; ++i )
|
2013-10-04 14:47:59 +02:00
|
|
|
{
|
|
|
|
QgsRelation relation = mRelationsTable->item( i, 0 )->data( Qt::UserRole ).value<QgsRelation>();
|
|
|
|
// The name can be editted in the table, so apply this one
|
2017-02-17 16:50:43 +01:00
|
|
|
relation.setName( mRelationsTable->item( i, 0 )->data( Qt::DisplayRole ).toString() );
|
2013-10-04 14:47:59 +02:00
|
|
|
relations << relation;
|
|
|
|
}
|
|
|
|
|
|
|
|
return relations;
|
|
|
|
}
|
|
|
|
|
2016-11-08 13:36:23 +01:00
|
|
|
void QgsRelationManagerDialog::onSelectionChanged()
|
|
|
|
{
|
|
|
|
mBtnRemoveRelation->setEnabled( mRelationsTable->selectionModel()->hasSelection() );
|
2016-11-19 16:04:11 +10:00
|
|
|
}
|