/
qgsmaplayerregistry.cpp
154 lines (127 loc) · 4.08 KB
/
qgsmaplayerregistry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/***************************************************************************
* QgsMapLayerRegistry.cpp - Singleton class for tracking mMapLayers.
* -------------------
* begin : Sun June 02 2004
* copyright : (C) 2004 by Tim Sutton
* email : tim@linfiniti.com
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
/* $Id$ */
#include <iostream>
#include "qgsmaplayerregistry.h"
#include "qgsmaplayer.h"
#include "qgslogger.h"
//
// Static calls to enforce singleton behaviour
//
QgsMapLayerRegistry *QgsMapLayerRegistry::mInstance = 0;
QgsMapLayerRegistry *QgsMapLayerRegistry::instance()
{
if ( mInstance == 0 )
{
mInstance = new QgsMapLayerRegistry();
}
return mInstance;
}
//
// Main class begins now...
//
QgsMapLayerRegistry::QgsMapLayerRegistry( QObject *parent ) : QObject( parent )
{
// constructor does nothing
}
QgsMapLayerRegistry::~QgsMapLayerRegistry()
{
removeAllMapLayers();
}
// get the layer count (number of registered layers)
int QgsMapLayerRegistry::count()
{
return mMapLayers.size();
}
QgsMapLayer * QgsMapLayerRegistry::mapLayer( QString theLayerId )
{
return mMapLayers.value( theLayerId );
}
QgsMapLayer *
QgsMapLayerRegistry::addMapLayer( QgsMapLayer * theMapLayer, bool theEmitSignal )
{
if ( !theMapLayer || !theMapLayer->isValid() )
{
QgsDebugMsg( "cannot add invalid layers" );
return 0;
}
//check the layer is not already registered!
QMap<QString, QgsMapLayer*>::iterator myIterator = mMapLayers.find( theMapLayer->id() );
//if myIterator returns mMapLayers.end() then it does not exist in registry and its safe to add it
if ( myIterator == mMapLayers.end() )
{
mMapLayers[theMapLayer->id()] = theMapLayer;
if ( theEmitSignal )
emit layerWasAdded( theMapLayer );
return mMapLayers[theMapLayer->id()];
}
else
{
return 0;
}
} // QgsMapLayerRegistry::addMapLayer
void QgsMapLayerRegistry::removeMapLayer( QString theLayerId, bool theEmitSignal )
{
if ( theEmitSignal )
emit layerWillBeRemoved( theLayerId );
delete mMapLayers[theLayerId];
mMapLayers.remove( theLayerId );
}
void QgsMapLayerRegistry::removeAllMapLayers()
{
// moved before physically removing the layers
emit removedAll();
// now let all canvas observers know to clear themselves,
// and then consequently any of their map legends
while ( mMapLayers.size() > 0 )
{
QString id = mMapLayers.begin().key();
emit layerWillBeRemoved( id );
delete mMapLayers[ id ]; // delete the map layer
mMapLayers.remove( id );
}
mMapLayers.clear();
} // QgsMapLayerRegistry::removeAllMapLayers()
//Added in QGIS 1.4
void QgsMapLayerRegistry::clearAllLayerCaches()
{
QMap<QString, QgsMapLayer *>::iterator it;
for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
{
//the map layer will take care of deleting the QImage
it.value()->setCacheImage( 0 );
}
} // QgsMapLayerRegistry::clearAllLayerCaches()
void QgsMapLayerRegistry::reloadAllLayers()
{
QMap<QString, QgsMapLayer *>::iterator it;
for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
{
QgsMapLayer* layer = it.value();
if ( layer )
{
layer->reload();
}
}
}
QMap<QString, QgsMapLayer*> & QgsMapLayerRegistry::mapLayers()
{
return mMapLayers;
}
void QgsMapLayerRegistry::connectNotify( const char * signal )
{
//QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal));
} // QgsMapLayerRegistry::connectNotify