~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/gui/qgsmaplayerregistry.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *  QgsMapLayerRegistry.cpp  -  Singleton class for tracking mMapLayers.
3
 
 *                         -------------------
4
 
 * begin                : Sun June 02 2004
5
 
 * copyright            : (C) 2004 by Tim Sutton
6
 
 * email                : tim@linfiniti.com
7
 
 ***************************************************************************/
8
 
 
9
 
/***************************************************************************
10
 
 *                                                                         *
11
 
 *   This program is free software; you can redistribute it and/or modify  *
12
 
 *   it under the terms of the GNU General Public License as published by  *
13
 
 *   the Free Software Foundation; either version 2 of the License, or     *
14
 
 *   (at your option) any later version.                                   *
15
 
 *                                                                         *
16
 
 ***************************************************************************/
17
 
/* $Id: qgsmaplayerregistry.cpp 5177 2006-04-05 13:01:52Z mhugent $ */
18
 
 
19
 
#include <iostream>
20
 
 
21
 
#include "qgsmaplayerregistry.h"
22
 
#include "qgslogger.h"
23
 
#include "qgsproject.h"
24
 
 
25
 
 
26
 
//
27
 
// Static calls to enforce singleton behaviour
28
 
//
29
 
QgsMapLayerRegistry *QgsMapLayerRegistry::mInstance = 0;
30
 
QgsMapLayerRegistry *QgsMapLayerRegistry::instance()
31
 
{
32
 
  if (mInstance == 0)
33
 
  {
34
 
    mInstance = new QgsMapLayerRegistry();
35
 
  }
36
 
  return mInstance;
37
 
}
38
 
 
39
 
//
40
 
// Main class begins now...
41
 
//
42
 
 
43
 
QgsMapLayerRegistry::QgsMapLayerRegistry(QObject *parent, const char *name) : QObject(parent,name) 
44
 
{
45
 
  QgsDebugMsg("QgsMapLayerRegistry created!");
46
 
  // constructor does nothing
47
 
}
48
 
// get the layer count (number of registered layers)
49
 
const int QgsMapLayerRegistry::count()
50
 
{
51
 
  return mMapLayers.size();
52
 
}
53
 
 //! Get a vector layer from the registry - the the requested key does not exist or
54
 
 //does not correspond to a vector layer, null returned!
55
 
 QgsVectorLayer * QgsMapLayerRegistry::getVectorLayer(QString theLayerId)
56
 
{
57
 
  QgsVectorLayer * myVectorLayer = (QgsVectorLayer*) mMapLayers[theLayerId];
58
 
  if (myVectorLayer)
59
 
  {
60
 
    if (myVectorLayer->type() == QgsMapLayer::VECTOR)
61
 
    {
62
 
      return myVectorLayer;
63
 
    }
64
 
    else
65
 
    {
66
 
      return 0;
67
 
    }
68
 
  }
69
 
  else
70
 
  {
71
 
    return 0;
72
 
  }
73
 
}
74
 
 
75
 
  
76
 
 
77
 
QgsMapLayer * QgsMapLayerRegistry::mapLayer(QString theLayerId)  
78
 
{
79
 
  QgsMapLayer * myMapLayer = mMapLayers[theLayerId];
80
 
  if (myMapLayer)
81
 
  {
82
 
    return myMapLayer;
83
 
  }
84
 
  else
85
 
  {
86
 
    return 0;
87
 
  }
88
 
}
89
 
 
90
 
 
91
 
 
92
 
QgsMapLayer *
93
 
  QgsMapLayerRegistry::addMapLayer( QgsMapLayer * theMapLayer, bool theEmitSignal )
94
 
{
95
 
  QgsDebugMsg("QgsMapLayerRegistry::addMaplayer - '" + theMapLayer->name());
96
 
  //check the layer is not already registered!
97
 
  std::map<QString,QgsMapLayer*>::iterator myIterator = mMapLayers.find(theMapLayer->getLayerID());
98
 
  //if myIterator returns mMapLayers.end() then it does not exist in registry and its safe to add it
99
 
  if (myIterator == mMapLayers.end())
100
 
  {
101
 
    mMapLayers[theMapLayer->getLayerID()] = theMapLayer;
102
 
    
103
 
    if (theEmitSignal)
104
 
      emit layerWasAdded(theMapLayer);
105
 
 
106
 
    // notify the project we've made a change
107
 
    QgsProject::instance()->dirty(true);
108
 
 
109
 
    return mMapLayers[theMapLayer->getLayerID()];
110
 
  }
111
 
  else
112
 
  {
113
 
    QgsDebugMsg("addMaplayer - " + theMapLayer->name() + " already registered");
114
 
    return 0x0;
115
 
  }
116
 
} //  QgsMapLayerRegistry::addMapLayer
117
 
 
118
 
 
119
 
 
120
 
void QgsMapLayerRegistry::removeMapLayer(QString theLayerId, bool theEmitSignal)
121
 
{
122
 
  QgsDebugMsg("QgsMapLayerRegistry::removemaplayer - emitting signal to notify all users of this layer to release it.");
123
 
  if (theEmitSignal)
124
 
    emit layerWillBeRemoved(theLayerId); 
125
 
  QgsDebugMsg("QgsMapLayerRegistry::removemaplayer - deleting map layer.");
126
 
  delete mMapLayers[theLayerId]; 
127
 
  QgsDebugMsg("QgsMapLayerRegistry::removemaplayer - unregistering map layer.");
128
 
  mMapLayers.erase(theLayerId);
129
 
  QgsDebugMsg("QgsMapLayerRegistry::removemaplayer - operation complete.");
130
 
  // notify the project we've made a change
131
 
  QgsProject::instance()->dirty(true);
132
 
}
133
 
 
134
 
void QgsMapLayerRegistry::removeAllMapLayers()
135
 
{
136
 
  QgsDebugMsg("QgsMapLayerRegistry::removeAllMapLayers");
137
 
  
138
 
  // moved before physically removing the layers
139
 
  emit removedAll();            // now let all canvas Observers know to clear
140
 
                                // themselves, and then consequently any of
141
 
                                // their map legends
142
 
  
143
 
  std::map<QString, QgsMapLayer *>::iterator myMapIterator = mMapLayers.begin();
144
 
  while ( myMapIterator != mMapLayers.end() )
145
 
  {
146
 
      delete myMapIterator->second; // delete the map layer
147
 
 
148
 
      mMapLayers.erase( myMapIterator );
149
 
 
150
 
      myMapIterator = mMapLayers.begin(); // since iterator invalidated due to
151
 
                                        // erase(), reset to new first element
152
 
  }
153
 
 
154
 
  // notify the project we've made a change
155
 
  QgsProject::instance()->dirty(true);
156
 
 
157
 
} // QgsMapLayerRegistry::removeAllMapLayers()
158
 
 
159
 
 
160
 
std::map<QString,QgsMapLayer*> & QgsMapLayerRegistry::mapLayers()
161
 
{
162
 
  QgsDebugMsg("QgsMapLayerRegistry::mapLayers");
163
 
  return mMapLayers;
164
 
}
165
 
 
166
 
 
167
 
 
168
 
void QgsMapLayerRegistry::connectNotify( const char * signal )
169
 
{
170
 
    QgsDebugMsg("QgsMapLayerRegistry connected to " + QString(signal));
171
 
} //  QgsMapLayerRegistry::connectNotify