QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 dot kuhn at gmx 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 
33 void QgsRelationManagerDialog::setLayers( QList< QgsVectorLayer* > layers )
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().first().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().first().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  .arg( addDlg.references().first().first )
93  .arg( addDlg.referencedLayerId() )
94  .arg( addDlg.references().first().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().first().first, addDlg.references().first().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 {
127  QList< QgsRelation > relations;
128 
129  for ( int i = 0; i < mRelationsTable->rowCount(); ++i )
130  {
131  QgsRelation relation = mRelationsTable->item( i, 0 )->data( Qt::UserRole ).value<QgsRelation>();
132  // The name can be editted in the table, so apply this one
133  relation.setRelationName( mRelationsTable->item( i, 0 )->data( Qt::DisplayRole ).toString() );
134  relations << relation;
135  }
136 
137  return relations;
138 }
139 
void setRelationId(QString id)
Set a name for this relation.
void addRelation(const QgsRelation &rel)
const QString name() const
void setRelationName(QString name)
Set a name for this relation.
QgsRelationManagerDialog(QgsRelationManager *relationMgr, QWidget *parent=0)
QgsVectorLayer * referencedLayer() const
Access the referenced (parent) layer.
void setReferencingLayer(QString id)
Set the referencing layer id.
const QString & name() const
Get the display name of the layer.
void setLayers(QList< QgsVectorLayer * >)
void setReferencedLayer(QString id)
Set the referenced layer id.
const QMap< QString, QgsRelation > & relations() const
Get access to the relations managed by this class.
const QString & id() const
The id.
QgsVectorLayer * referencingLayer() const
Access the referencing (child) layer This is the layer which has the field(s) which point to another ...
QList< QPair< QString, QString > > references()
void addLayers(QList< QgsVectorLayer * > layers)
This class manages a set of relations between layers.
void addFieldPair(QString referencingField, QString referencedField)
Add a field pairs which is part of this relation The first element of each pair are the field names f...
QList< FieldPair > fieldPairs() const
Returns the field pairs which form this relation The first element of each pair are the field names f...