QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgsrelationmanager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrelationmanager.cpp
3  --------------------------------------
4  Date : 1.3.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 "qgsrelationmanager.h"
17 
18 #include "qgsapplication.h"
19 #include "qgslogger.h"
20 #include "qgsmaplayerregistry.h"
21 #include "qgsproject.h"
22 #include "qgsvectorlayer.h"
23 
25  : QObject( project )
26  , mProject( project )
27 {
28  connect( project, SIGNAL( readProject( const QDomDocument& ) ), SLOT( readProject( const QDomDocument& ) ) );
29  connect( project, SIGNAL( writeProject( QDomDocument& ) ), SLOT( writeProject( QDomDocument& ) ) );
30  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersRemoved( QStringList ) ), this, SLOT( layersRemoved( QStringList ) ) );
31 }
32 
33 void QgsRelationManager::setRelations( const QList<QgsRelation>& relations )
34 {
35  mRelations.clear();
36  foreach ( const QgsRelation& rel, relations )
37  {
38  addRelation( rel );
39  }
40 }
41 
42 const QMap<QString, QgsRelation>& QgsRelationManager::relations() const
43 {
44  return mRelations;
45 }
46 
48 {
49  if ( !relation.isValid() )
50  return;
51 
52  mRelations.insert( relation.id(), relation );
53 
54  mProject->dirty( true );
55 }
56 
57 void QgsRelationManager::removeRelation( const QString& name )
58 {
59  mRelations.remove( name );
60 }
61 
63 {
64  mRelations.remove( relation.id() );
65 }
66 
67 QgsRelation QgsRelationManager::relation( const QString& id ) const
68 {
69  return mRelations.value( id );
70 }
71 
73 {
74  mRelations.clear();
75 }
76 
77 QList<QgsRelation> QgsRelationManager::referencingRelations( QgsVectorLayer* layer, int fieldIdx ) const
78 {
79  if ( !layer )
80  {
81  return mRelations.values();
82  }
83 
84  QList<QgsRelation> relations;
85 
86  foreach ( const QgsRelation& rel, mRelations )
87  {
88  if ( rel.referencingLayer() == layer )
89  {
90  if ( fieldIdx != -2 )
91  {
92  bool containsField = false;
93  foreach ( const QgsRelation::FieldPair& fp, rel.fieldPairs() )
94  {
95  if ( fieldIdx == layer->fieldNameIndex( fp.referencingField() ) )
96  {
97  containsField = true;
98  break;
99  }
100  }
101 
102  if ( !containsField )
103  {
104  continue;
105  }
106  }
107  relations.append( rel );
108  }
109  }
110 
111  return relations;
112 }
113 
114 QList<QgsRelation> QgsRelationManager::referencedRelations( QgsVectorLayer* layer ) const
115 {
116  if ( !layer )
117  {
118  return mRelations.values();
119  }
120 
121  QList<QgsRelation> relations;
122 
123  foreach ( const QgsRelation& rel, mRelations )
124  {
125  if ( rel.referencedLayer() == layer )
126  {
127  relations.append( rel );
128  }
129  }
130 
131  return relations;
132 }
133 
134 void QgsRelationManager::readProject( const QDomDocument & doc )
135 {
136  mRelations.clear();
137 
138  QDomNodeList nodes = doc.elementsByTagName( "relations" );
139  if ( nodes.count() )
140  {
141  QDomNode node = nodes.item( 0 );
142  QDomNodeList relationNodes = node.childNodes();
143  int relCount = relationNodes.count();
144  for ( int i = 0; i < relCount; ++i )
145  {
146  addRelation( QgsRelation::createFromXML( relationNodes.at( i ) ) );
147  }
148  }
149  else
150  {
151  QgsDebugMsg( "No relations data present in this document" );
152  }
153 
154  emit( relationsLoaded() );
155 }
156 
157 void QgsRelationManager::writeProject( QDomDocument & doc )
158 {
159  QDomNodeList nl = doc.elementsByTagName( "qgis" );
160  if ( !nl.count() )
161  {
162  QgsDebugMsg( "Unable to find qgis element in project file" );
163  return;
164  }
165  QDomNode qgisNode = nl.item( 0 ); // there should only be one
166 
167  QDomElement relationsNode = doc.createElement( "relations" );
168  qgisNode.appendChild( relationsNode );
169 
170  foreach ( const QgsRelation& relation, mRelations )
171  {
172  relation.writeXML( relationsNode, doc );
173  }
174 }
175 
176 void QgsRelationManager::layersRemoved( const QStringList& layers )
177 {
178  Q_FOREACH ( const QString& layer, layers )
179  {
180  QMapIterator<QString, QgsRelation> it( mRelations );
181 
182  while ( it.hasNext() )
183  {
184  it.next();
185 
186  if ( it.value().referencedLayerId() == layer
187  || it.value().referencingLayerId() == layer )
188  {
189  mRelations.remove( it.key() );
190  }
191  }
192  }
193 }
void readProject(const QDomDocument &doc)
bool isValid() const
Returns the validity of this relation.
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
QMap< QString, QgsRelation > mRelations
The references.
void layersRemoved(const QStringList &layers)
void removeRelation(const QString &name)
QgsVectorLayer * referencedLayer() const
Access the referenced (parent) layer.
static QgsRelation createFromXML(const QDomNode &node)
Creates a relation from an XML structure.
Definition: qgsrelation.cpp:30
void writeXML(QDomNode &node, QDomDocument &doc) const
Writes a relation to an XML structure.
Definition: qgsrelation.cpp:92
void setRelations(const QList< QgsRelation > &relations)
QList< QgsRelation > referencedRelations(QgsVectorLayer *layer=0) const
const QMap< QString, QgsRelation > & relations() const
Defines a relation between matchin fields of the two involved tables of a relation.
Definition: qgsrelation.h:38
QgsRelation relation(const QString &id) const
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 ...
void writeProject(QDomDocument &doc)
Reads and writes project states.
Definition: qgsproject.h:67
QList< QgsRelation > referencingRelations(QgsVectorLayer *layer=0, int fieldIdx=-2) const
QgsRelationManager(QgsProject *project)
const QString & referencingField() const
Get the name of the referencing field.
Definition: qgsrelation.h:50
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QList< FieldPair > fieldPairs() const
Returns the field pairs which form this relation The first element of each pair are the field names f...
Represents a vector layer which manages a vector based data sets.
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
void addRelation(const QgsRelation &relation)
void dirty(bool b)
Definition: qgsproject.cpp:396