QGIS API Documentation  2.3.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 "qgsproject.h"
21 #include "qgsvectorlayer.h"
22 
24  : QObject( project )
25  , mProject( project )
26 {
27  connect( project, SIGNAL( readProject( const QDomDocument& ) ), SLOT( readProject( const QDomDocument& ) ) );
28  connect( project, SIGNAL( writeProject( QDomDocument& ) ), SLOT( writeProject( QDomDocument& ) ) );
29 }
30 
31 void QgsRelationManager::setRelations( const QList<QgsRelation>& relations )
32 {
33  mRelations.clear();
34  foreach ( const QgsRelation& rel, relations )
35  {
36  addRelation( rel );
37  }
38 }
39 
40 const QMap<QString, QgsRelation>& QgsRelationManager::relations() const
41 {
42  return mRelations;
43 }
44 
46 {
47  if ( !relation.isValid() )
48  return;
49 
50  mRelations.insert( relation.id(), relation );
51 
52  mProject->dirty( true );
53 }
54 
55 void QgsRelationManager::removeRelation( const QString& name )
56 {
57  mRelations.remove( name );
58 }
59 
61 {
62  mRelations.remove( relation.id() );
63 }
64 
65 QgsRelation QgsRelationManager::relation( const QString& id ) const
66 {
67  return mRelations.value( id );
68 }
69 
71 {
72  mRelations.clear();
73 }
74 
75 QList<QgsRelation> QgsRelationManager::referencingRelations( QgsVectorLayer* layer, int fieldIdx ) const
76 {
77  if ( !layer )
78  {
79  return mRelations.values();
80  }
81 
82  QList<QgsRelation> relations;
83 
84  foreach ( const QgsRelation& rel, mRelations )
85  {
86  if ( rel.referencingLayer() == layer )
87  {
88  if ( fieldIdx != -2 )
89  {
90  bool containsField = false;
91  foreach ( const QgsRelation::FieldPair& fp, rel.fieldPairs() )
92  {
93  if ( fieldIdx == layer->fieldNameIndex( fp.referencingField() ) )
94  {
95  containsField = true;
96  break;
97  }
98  }
99 
100  if ( !containsField )
101  {
102  continue;
103  }
104  }
105  relations.append( rel );
106  }
107  }
108 
109  return relations;
110 }
111 
112 QList<QgsRelation> QgsRelationManager::referencedRelations( QgsVectorLayer* layer ) const
113 {
114  if ( !layer )
115  {
116  return mRelations.values();
117  }
118 
119  QList<QgsRelation> relations;
120 
121  foreach ( const QgsRelation& rel, mRelations )
122  {
123  if ( rel.referencedLayer() == layer )
124  {
125  relations.append( rel );
126  }
127  }
128 
129  return relations;
130 }
131 
132 void QgsRelationManager::readProject( const QDomDocument & doc )
133 {
134  mRelations.clear();
135 
136  QDomNodeList nodes = doc.elementsByTagName( "relations" );
137  if ( nodes.count() )
138  {
139  QDomNode node = nodes.item( 0 );
140  QDomNodeList relationNodes = node.childNodes();
141  int relCount = relationNodes.count();
142  for ( int i = 0; i < relCount; ++i )
143  {
144  addRelation( QgsRelation::createFromXML( relationNodes.at( i ) ) );
145  }
146  }
147  else
148  {
149  QgsDebugMsg( "No relations data present in this document" );
150  }
151 
152  emit( relationsLoaded() );
153 }
154 
155 void QgsRelationManager::writeProject( QDomDocument & doc )
156 {
157  QDomNodeList nl = doc.elementsByTagName( "qgis" );
158  if ( !nl.count() )
159  {
160  QgsDebugMsg( "Unable to find qgis element in project file" );
161  return;
162  }
163  QDomNode qgisNode = nl.item( 0 ); // there should only be one
164 
165  QDomElement relationsNode = doc.createElement( "relations" );
166  qgisNode.appendChild( relationsNode );
167 
168  foreach ( const QgsRelation& relation, mRelations )
169  {
170  relation.writeXML( relationsNode, doc );
171  }
172 }
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 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:65
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
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:391