Skip to content

Commit f5afee3

Browse files
author
wonder
committedJan 16, 2008
- changed return type of QgsMapLayerRegistry::mapLayers() from std::map to QMap
- wrapped the above function in PyQGIS git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@7984 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent dd860a6 commit f5afee3

File tree

9 files changed

+143
-48
lines changed

9 files changed

+143
-48
lines changed
 

‎python/core/conversions.sip

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ which are not wrapped by PyQt:
66
- QSet<int>
77
- QSet<TYPE>
88
- QMap<int, QMap<int, TYPE> >
9+
- QMap<TYPE1, TYPE2*>
910

1011
*/
1112

@@ -408,3 +409,112 @@ template<TYPE>
408409

409410
};
410411

412+
413+
414+
415+
template<TYPE1, TYPE2>
416+
%MappedType QMap<TYPE1, TYPE2*>
417+
{
418+
%TypeHeaderCode
419+
#include <qmap.h>
420+
%End
421+
422+
%ConvertFromTypeCode
423+
// Create the dictionary.
424+
PyObject *d = PyDict_New();
425+
426+
if (!d)
427+
return NULL;
428+
429+
// Set the dictionary elements.
430+
QMap<TYPE1, TYPE2*>::const_iterator i = sipCpp->constBegin();
431+
432+
while (i != sipCpp->constEnd())
433+
{
434+
TYPE1 *t1 = new TYPE1(i.key());
435+
TYPE2 *t2 = i.value();
436+
437+
PyObject *t1obj = sipConvertFromNewInstance(t1, sipClass_TYPE1, sipTransferObj);
438+
PyObject *t2obj = sipConvertFromInstance(t2, sipClass_TYPE2, sipTransferObj);
439+
440+
if (t1obj == NULL || t2obj == NULL || PyDict_SetItem(d, t1obj, t2obj) < 0)
441+
{
442+
Py_DECREF(d);
443+
444+
if (t1obj)
445+
Py_DECREF(t1obj);
446+
else
447+
delete t1;
448+
449+
if (t2obj)
450+
Py_DECREF(t2obj);
451+
else
452+
delete t2;
453+
454+
return NULL;
455+
}
456+
457+
Py_DECREF(t1obj);
458+
Py_DECREF(t2obj);
459+
460+
++i;
461+
}
462+
463+
return d;
464+
%End
465+
466+
%ConvertToTypeCode
467+
PyObject *t1obj, *t2obj;
468+
#if PY_VERSION_HEX >= 0x02050000
469+
Py_ssize_t i = 0;
470+
#else
471+
int i = 0;
472+
#endif
473+
474+
// Check the type if that is all that is required.
475+
if (sipIsErr == NULL)
476+
{
477+
if (!PyDict_Check(sipPy))
478+
return 0;
479+
480+
while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
481+
{
482+
if (!sipCanConvertToInstance(t1obj, sipClass_TYPE1, SIP_NOT_NONE))
483+
return 0;
484+
485+
if (!sipCanConvertToInstance(t2obj, sipClass_TYPE2, SIP_NOT_NONE))
486+
return 0;
487+
}
488+
489+
return 1;
490+
}
491+
492+
QMap<TYPE1, TYPE2*> *qm = new QMap<TYPE1, TYPE2*>;
493+
494+
while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
495+
{
496+
int state1, state2;
497+
498+
TYPE1 *t1 = reinterpret_cast<TYPE1 *>(sipConvertToInstance(t1obj, sipClass_TYPE1, sipTransferObj, SIP_NOT_NONE, &state1, sipIsErr));
499+
TYPE2 *t2 = reinterpret_cast<TYPE2 *>(sipConvertToInstance(t2obj, sipClass_TYPE2, sipTransferObj, SIP_NOT_NONE, &state2, sipIsErr));
500+
501+
if (*sipIsErr)
502+
{
503+
sipReleaseInstance(t1, sipClass_TYPE1, state1);
504+
sipReleaseInstance(t2, sipClass_TYPE2, state2);
505+
506+
delete qm;
507+
return 0;
508+
}
509+
510+
qm->insert(*t1, t2);
511+
512+
sipReleaseInstance(t1, sipClass_TYPE1, state1);
513+
sipReleaseInstance(t2, sipClass_TYPE2, state2);
514+
}
515+
516+
*sipCppPtr = qm;
517+
518+
return sipGetState(sipTransferObj);
519+
%End
520+
};

‎python/core/qgsmaplayerregistry.sip

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public:
2525
QgsMapLayer * mapLayer(QString theLayerId);
2626

2727
//! Retrieve the mapLayers collection (mainly intended for use by projectio)
28-
// TODO: wrap
29-
//std::map<QString,QgsMapLayer*> & mapLayers();
28+
QMap<QString,QgsMapLayer*> & mapLayers();
3029

3130
/** Add a layer to the map of loaded layers
3231
@returns NULL if unable to add layer, otherwise pointer to newly added layer

‎src/app/legend/qgslegend.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,9 +1011,8 @@ bool QgsLegend::readXML(QDomNode& legendnode)
10111011
else if(childelem.tagName()=="legendlayerfile")
10121012
{
10131013
//find out the legendlayer
1014-
std::map<QString,QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
1015-
std::map<QString, QgsMapLayer*>::const_iterator iter = mapLayers.find(childelem.attribute("layerid"));
1016-
if(iter == mapLayers.end()) //the layer cannot be found (e.g. the file has been moved)
1014+
QgsMapLayer* theMapLayer = QgsMapLayerRegistry::instance()->mapLayer(childelem.attribute("layerid"));
1015+
if(theMapLayer == NULL) //the layer cannot be found (e.g. the file has been moved)
10171016
{
10181017
// remove the whole legendlayer if this is the only legendlayerfile
10191018
if(childelem.previousSibling().isNull() && childelem.nextSibling().isNull())
@@ -1026,7 +1025,6 @@ bool QgsLegend::readXML(QDomNode& legendnode)
10261025
}
10271026
else if(lastLayerFileGroup)
10281027
{
1029-
QgsMapLayer* theMapLayer = iter->second;
10301028
QgsLegendLayerFile* theLegendLayerFile = new QgsLegendLayerFile(lastLayerFileGroup, QgsLegendLayerFile::nameFromLayer(theMapLayer), theMapLayer);
10311029

10321030
// load layer's visibility and 'show in overview' flag

‎src/app/qgspastetransformations.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,26 @@ QgsPasteTransformations::QgsPasteTransformations()
3232
{
3333

3434
// Populate the dialog with the loaded layers
35-
std::map<QString, QgsMapLayer*> mapLayers =
35+
QMap<QString, QgsMapLayer*> mapLayers =
3636
QgsMapLayerRegistry::instance()->mapLayers();
3737

38-
for (std::map<QString, QgsMapLayer*>::iterator it = mapLayers.begin();
38+
for (QMap<QString, QgsMapLayer*>::iterator it = mapLayers.begin();
3939
it != mapLayers.end();
4040
++it )
4141
{
4242
#ifdef QGISDEBUG
4343
std::cerr << "QgsPasteTransformations::QgsPasteTransformations: QgsMapLayerRegistry has "
44-
<< it->second->name().toLocal8Bit().data() << "."
44+
<< it.value()->name().toLocal8Bit().data() << "."
4545
<< std::endl;
4646
#endif
4747

4848
// TODO: Test if a VECTOR or DATABASE layer only (not RASTER)
4949

50-
sourceLayerComboBox ->insertItem( it->second->name() );
51-
destinationLayerComboBox->insertItem( it->second->name() );
50+
sourceLayerComboBox ->insertItem( it.value()->name() );
51+
destinationLayerComboBox->insertItem( it.value()->name() );
5252

5353
// store the lookup from the name to the map layer object
54-
mMapNameLookup[ it->second->name() ] = it->second;
54+
mMapNameLookup[ it.value()->name() ] = it.value();
5555
}
5656

5757

‎src/app/qgsrasterlayerproperties.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,13 @@ mpRasterLayer( dynamic_cast<QgsRasterLayer*>(lyr) )
278278

279279
cboxTransparencyBand->insertItem(tr(QgsRasterLayer::QSTRING_NOT_SET));
280280
cboxTransparencyLayer->insertItem(tr(QgsRasterLayer::QSTRING_NOT_SET));
281-
std::map<QString, QgsMapLayer *> myLayers = QgsMapLayerRegistry::instance()->mapLayers();
282-
std::map<QString, QgsMapLayer *>::iterator it;
281+
QMap<QString, QgsMapLayer *> myLayers = QgsMapLayerRegistry::instance()->mapLayers();
282+
QMap<QString, QgsMapLayer *>::iterator it;
283283
for(it = myLayers.begin(); it != myLayers.end(); it++)
284284
{
285-
if(QgsMapLayer::RASTER == it->second->type())
285+
if(QgsMapLayer::RASTER == it.value()->type())
286286
{
287-
cboxTransparencyLayer->insertItem(it->second->name());
287+
cboxTransparencyLayer->insertItem(it.value()->name());
288288
}
289289
}
290290

@@ -1666,13 +1666,13 @@ void QgsRasterLayerProperties::on_cboxTransparencyLayer_currentIndexChanged(cons
16661666
}
16671667
else
16681668
{
1669-
std::map<QString, QgsMapLayer *> myLayers = QgsMapLayerRegistry::instance()->mapLayers();
1670-
std::map<QString, QgsMapLayer *>::iterator it;
1669+
QMap<QString, QgsMapLayer *> myLayers = QgsMapLayerRegistry::instance()->mapLayers();
1670+
QMap<QString, QgsMapLayer *>::iterator it;
16711671
for(it = myLayers.begin(); it != myLayers.end(); it++)
16721672
{
1673-
if(theText == it->second->name() && QgsMapLayer::RASTER == it->second->type())
1673+
if(theText == it.value()->name() && QgsMapLayer::RASTER == it.value()->type())
16741674
{
1675-
QgsRasterLayer* myRasterLayer = (QgsRasterLayer*)it->second;
1675+
QgsRasterLayer* myRasterLayer = (QgsRasterLayer*)it.value();
16761676
int myBandCount = myRasterLayer->getBandCount();
16771677
cboxTransparencyBand->clear();
16781678
cboxTransparencyBand->insertItem(tr(QgsRasterLayer::QSTRING_NOT_SET));

‎src/core/qgsmaplayerregistry.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,7 @@ const int QgsMapLayerRegistry::count()
6060

6161
QgsMapLayer * QgsMapLayerRegistry::mapLayer(QString theLayerId)
6262
{
63-
QgsMapLayer * myMapLayer = mMapLayers[theLayerId];
64-
if (myMapLayer)
65-
{
66-
return myMapLayer;
67-
}
68-
else
69-
{
70-
return 0;
71-
}
63+
return mMapLayers.value(theLayerId);
7264
}
7365

7466

@@ -83,7 +75,7 @@ QgsMapLayer *
8375
}
8476

8577
//check the layer is not already registered!
86-
std::map<QString,QgsMapLayer*>::iterator myIterator = mMapLayers.find(theMapLayer->getLayerID());
78+
QMap<QString,QgsMapLayer*>::iterator myIterator = mMapLayers.find(theMapLayer->getLayerID());
8779
//if myIterator returns mMapLayers.end() then it does not exist in registry and its safe to add it
8880
if (myIterator == mMapLayers.end())
8981
{
@@ -124,21 +116,17 @@ void QgsMapLayerRegistry::removeAllMapLayers()
124116
// themselves, and then consequently any of
125117
// their map legends
126118

127-
std::map<QString, QgsMapLayer *>::iterator myMapIterator = mMapLayers.begin();
128-
while ( myMapIterator != mMapLayers.end() )
119+
QMap<QString, QgsMapLayer *>::iterator it;
120+
for (it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
129121
{
130-
delete myMapIterator->second; // delete the map layer
131-
132-
mMapLayers.erase( myMapIterator );
133-
134-
myMapIterator = mMapLayers.begin(); // since iterator invalidated due to
135-
// erase(), reset to new first element
122+
delete it.value(); // delete the map layer
136123
}
124+
mMapLayers.clear();
137125

138126
} // QgsMapLayerRegistry::removeAllMapLayers()
139127

140128

141-
std::map<QString,QgsMapLayer*> & QgsMapLayerRegistry::mapLayers()
129+
QMap<QString,QgsMapLayer*> & QgsMapLayerRegistry::mapLayers()
142130
{
143131
QgsDebugMsg("QgsMapLayerRegistry::mapLayers");
144132
return mMapLayers;

‎src/core/qgsmaplayerregistry.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#ifndef QGSMAPLAYERREGISTRY_H
2121
#define QGSMAPLAYERREGISTRY_H
2222

23-
#include <map>
23+
#include <QMap>
2424
#include <QObject>
2525

2626
class QString;
@@ -52,7 +52,7 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
5252
QgsMapLayer * mapLayer(QString theLayerId);
5353

5454
//! Retrieve the mapLayers collection (mainly intended for use by projectio)
55-
std::map<QString,QgsMapLayer*> & mapLayers();
55+
QMap<QString,QgsMapLayer*> & mapLayers();
5656

5757
/** Add a layer to the map of loaded layers
5858
@returns NULL if unable to add layer, otherwise pointer to newly added layer
@@ -122,7 +122,7 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
122122

123123
static QgsMapLayerRegistry* mInstance;
124124

125-
std::map<QString,QgsMapLayer*> mMapLayers;
125+
QMap<QString,QgsMapLayer*> mMapLayers;
126126

127127
/** debugging member
128128
invoked when a connect() is made to this object

‎src/core/qgsproject.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -975,18 +975,18 @@ bool QgsProject::write()
975975
emit writeProject(*doc);
976976

977977
// within top level node save list of layers
978-
std::map<QString,QgsMapLayer*> & layers = QgsMapLayerRegistry::instance()->mapLayers();
978+
QMap<QString,QgsMapLayer*> & layers = QgsMapLayerRegistry::instance()->mapLayers();
979979

980980
// Iterate over layers in zOrder
981981
// Call writeXML() on each
982982
QDomElement projectLayersNode = doc->createElement("projectlayers");
983983
projectLayersNode.setAttribute("layercount", qulonglong( layers.size() ));
984984

985-
std::map<QString,QgsMapLayer*>::iterator li = layers.begin();
985+
QMap<QString,QgsMapLayer*>::iterator li = layers.begin();
986986
while (li != layers.end())
987987
{
988988
//QgsMapLayer *ml = QgsMapLayerRegistry::instance()->mapLayer(*li);
989-
QgsMapLayer* ml = li->second;
989+
QgsMapLayer* ml = li.value();
990990

991991
if (ml)
992992
{

‎src/plugins/gps_importer/qgsgpsplugin.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ void QgsGPSPlugin::run()
113113
{
114114
// find all GPX layers
115115
std::vector<QgsVectorLayer*> gpxLayers;
116-
std::map<QString, QgsMapLayer*>::const_iterator iter;
116+
QMap<QString, QgsMapLayer*>::const_iterator iter;
117117
QgsMapLayerRegistry* registry = QgsMapLayerRegistry::instance();
118118
for (iter = registry->mapLayers().begin();
119119
iter != registry->mapLayers().end(); ++iter) {
120-
if (iter->second->type() == QgsMapLayer::VECTOR) {
121-
QgsVectorLayer* vLayer = dynamic_cast<QgsVectorLayer*>(iter->second);
120+
if (iter.value()->type() == QgsMapLayer::VECTOR) {
121+
QgsVectorLayer* vLayer = dynamic_cast<QgsVectorLayer*>(iter.value());
122122
if (vLayer->providerType() == "gpx")
123123
gpxLayers.push_back(vLayer);
124124
}

0 commit comments

Comments
 (0)
Please sign in to comment.