QGIS API Documentation  2.13.0-Master
qgsrelationmanagerdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrelationmanagerdialog.cpp
3  --------------------------------------
4  Date : 23.2.2013
5  Copyright : (C) 2013 Matthias Kuhn
6  Email : matthias at opengis dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsrelationadddlg.h"
18 #include "qgsrelationmanager.h"
19 #include "qgsvectorlayer.h"
20 
22  QWidget( parent ),
23  Ui::QgsRelationManagerDialogBase(),
24  mRelationManager( relationMgr )
25 {
26  setupUi( this );
27 }
28 
30 {
31 }
32 
34 {
35  mLayers = layers;
36 
37  const QList<QgsRelation>& relations = mRelationManager->relations().values();
38 
39  Q_FOREACH ( const QgsRelation& rel, relations )
40  {
41  addRelation( rel );
42  }
43 }
44 
46 {
47  int row = mRelationsTable->rowCount();
48  mRelationsTable->insertRow( row );
49 
50  QTableWidgetItem* item = new QTableWidgetItem( rel.name() );
51  // Save relation in first column's item
52  item->setData( Qt::UserRole, QVariant::fromValue<QgsRelation>( rel ) );
53  mRelationsTable->setItem( row, 0, item );
54 
55 
56  item = new QTableWidgetItem( rel.referencingLayer()->name() );
57  item->setFlags( Qt::ItemIsEditable );
58  mRelationsTable->setItem( row, 1, item );
59 
60  item = new QTableWidgetItem( rel.fieldPairs().at( 0 ).referencingField() );
61  item->setFlags( Qt::ItemIsEditable );
62  mRelationsTable->setItem( row, 2, item );
63 
64  item = new QTableWidgetItem( rel.referencedLayer()->name() );
65  item->setFlags( Qt::ItemIsEditable );
66  mRelationsTable->setItem( row, 3, item );
67 
68  item = new QTableWidgetItem( rel.fieldPairs().at( 0 ).referencedField() );
69  item->setFlags( Qt::ItemIsEditable );
70  mRelationsTable->setItem( row, 4, item );
71 
72  item = new QTableWidgetItem( rel.id() );
73  item->setFlags( Qt::ItemIsEditable );
74  mRelationsTable->setItem( row, 5, item );
75 }
76 
78 {
79  QgsRelationAddDlg addDlg;
80  addDlg.addLayers( mLayers );
81 
82  if ( addDlg.exec() )
83  {
84  QgsRelation relation;
85 
86  relation.setReferencingLayer( addDlg.referencingLayerId() );
87  relation.setReferencedLayer( addDlg.referencedLayerId() );
88  QString relationId = addDlg.relationId();
89  if ( addDlg.relationId() == "" )
90  relationId = QString( "%1_%2_%3_%4" )
91  .arg( addDlg.referencingLayerId(),
92  addDlg.references().at( 0 ).first,
93  addDlg.referencedLayerId(),
94  addDlg.references().at( 0 ).second );
95 
96  QStringList existingNames;
97 
98 
99  Q_FOREACH ( const QgsRelation& rel, relations() )
100  {
101  existingNames << rel.id();
102  }
103 
104  QString tempId = relationId + "_%1";
105  int suffix = 1;
106  while ( existingNames.contains( relationId ) )
107  {
108  relationId = tempId.arg( suffix );
109  ++suffix;
110  }
111  relation.setRelationId( relationId );
112  relation.addFieldPair( addDlg.references().at( 0 ).first, addDlg.references().at( 0 ).second );
113  relation.setRelationName( addDlg.relationName() );
114 
115  addRelation( relation );
116  }
117 }
118 
120 {
121  if ( mRelationsTable->currentIndex().isValid() )
122  mRelationsTable->removeRow( mRelationsTable->currentItem()->row() );
123 }
124 
126 {
128 
129  int rows = mRelationsTable->rowCount();
130  relations.reserve( rows );
131  for ( int i = 0; i < rows; ++i )
132  {
133  QgsRelation relation = mRelationsTable->item( i, 0 )->data( Qt::UserRole ).value<QgsRelation>();
134  // The name can be editted in the table, so apply this one
135  relation.setRelationName( mRelationsTable->item( i, 0 )->data( Qt::DisplayRole ).toString() );
136  relations << relation;
137  }
138 
139  return relations;
140 }
141 
void addRelation(const QgsRelation &rel)
void setupUi(QWidget *widget)
QString name() const
Get the display name of the layer.
QList< T > values() const
void reserve(int alloc)
void setReferencingLayer(const QString &id)
Set the referencing layer id.
const T & at(int i) const
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QgsRelationManagerDialog(QgsRelationManager *relationMgr, QWidget *parent=0)
void addFieldPair(const QString &referencingField, const QString &referencedField)
Add a field pairs which is part of this relation The first element of each pair are the field names f...
QgsVectorLayer * referencedLayer() const
Access the referenced (parent) layer.
int exec()
QString name() const
Returns a human readable name for this relation.
QString id() const
A (project-wide) unique id for this relation.
virtual void setData(int role, const QVariant &value)
const QMap< QString, QgsRelation > & relations() const
Get access to the relations managed by this class.
void setRelationName(const QString &name)
Set a name for this relation.
QgsVectorLayer * referencingLayer() const
Access the referencing (child) layer This is the layer which has the field(s) which point to another ...
void addLayers(const QList< QgsVectorLayer * > &layers)
void setReferencedLayer(const QString &id)
Set the referenced layer id.
QList< QPair< QString, QString > > references()
void setFlags(QFlags< Qt::ItemFlag > flags)
void setRelationId(const QString &id)
Set a name for this relation.
void setLayers(const QList< QgsVectorLayer * > &)
This class manages a set of relations between layers.
QList< FieldPair > fieldPairs() const
Returns the field pairs which form this relation The first element of each pair are the field names f...
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const