Skip to content

Commit

Permalink
- changed return type of QgsMapLayerRegistry::mapLayers() from std::m…
Browse files Browse the repository at this point in the history
…ap to QMap

- wrapped the above function in PyQGIS


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@7984 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Jan 16, 2008
1 parent dd860a6 commit f5afee3
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 48 deletions.
110 changes: 110 additions & 0 deletions python/core/conversions.sip
Expand Up @@ -6,6 +6,7 @@ which are not wrapped by PyQt:
- QSet<int>
- QSet<TYPE>
- QMap<int, QMap<int, TYPE> >
- QMap<TYPE1, TYPE2*>

*/

Expand Down Expand Up @@ -408,3 +409,112 @@ template<TYPE>

};




template<TYPE1, TYPE2>
%MappedType QMap<TYPE1, TYPE2*>
{
%TypeHeaderCode
#include <qmap.h>
%End

%ConvertFromTypeCode
// Create the dictionary.
PyObject *d = PyDict_New();

if (!d)
return NULL;

// Set the dictionary elements.
QMap<TYPE1, TYPE2*>::const_iterator i = sipCpp->constBegin();

while (i != sipCpp->constEnd())
{
TYPE1 *t1 = new TYPE1(i.key());
TYPE2 *t2 = i.value();

PyObject *t1obj = sipConvertFromNewInstance(t1, sipClass_TYPE1, sipTransferObj);
PyObject *t2obj = sipConvertFromInstance(t2, sipClass_TYPE2, sipTransferObj);

if (t1obj == NULL || t2obj == NULL || PyDict_SetItem(d, t1obj, t2obj) < 0)
{
Py_DECREF(d);

if (t1obj)
Py_DECREF(t1obj);
else
delete t1;

if (t2obj)
Py_DECREF(t2obj);
else
delete t2;

return NULL;
}

Py_DECREF(t1obj);
Py_DECREF(t2obj);

++i;
}

return d;
%End

%ConvertToTypeCode
PyObject *t1obj, *t2obj;
#if PY_VERSION_HEX >= 0x02050000
Py_ssize_t i = 0;
#else
int i = 0;
#endif

// Check the type if that is all that is required.
if (sipIsErr == NULL)
{
if (!PyDict_Check(sipPy))
return 0;

while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
{
if (!sipCanConvertToInstance(t1obj, sipClass_TYPE1, SIP_NOT_NONE))
return 0;

if (!sipCanConvertToInstance(t2obj, sipClass_TYPE2, SIP_NOT_NONE))
return 0;
}

return 1;
}

QMap<TYPE1, TYPE2*> *qm = new QMap<TYPE1, TYPE2*>;

while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
{
int state1, state2;

TYPE1 *t1 = reinterpret_cast<TYPE1 *>(sipConvertToInstance(t1obj, sipClass_TYPE1, sipTransferObj, SIP_NOT_NONE, &state1, sipIsErr));
TYPE2 *t2 = reinterpret_cast<TYPE2 *>(sipConvertToInstance(t2obj, sipClass_TYPE2, sipTransferObj, SIP_NOT_NONE, &state2, sipIsErr));

if (*sipIsErr)
{
sipReleaseInstance(t1, sipClass_TYPE1, state1);
sipReleaseInstance(t2, sipClass_TYPE2, state2);

delete qm;
return 0;
}

qm->insert(*t1, t2);

sipReleaseInstance(t1, sipClass_TYPE1, state1);
sipReleaseInstance(t2, sipClass_TYPE2, state2);
}

*sipCppPtr = qm;

return sipGetState(sipTransferObj);
%End
};
3 changes: 1 addition & 2 deletions python/core/qgsmaplayerregistry.sip
Expand Up @@ -25,8 +25,7 @@ public:
QgsMapLayer * mapLayer(QString theLayerId);

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

/** Add a layer to the map of loaded layers
@returns NULL if unable to add layer, otherwise pointer to newly added layer
Expand Down
6 changes: 2 additions & 4 deletions src/app/legend/qgslegend.cpp
Expand Up @@ -1011,9 +1011,8 @@ bool QgsLegend::readXML(QDomNode& legendnode)
else if(childelem.tagName()=="legendlayerfile")
{
//find out the legendlayer
std::map<QString,QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
std::map<QString, QgsMapLayer*>::const_iterator iter = mapLayers.find(childelem.attribute("layerid"));
if(iter == mapLayers.end()) //the layer cannot be found (e.g. the file has been moved)
QgsMapLayer* theMapLayer = QgsMapLayerRegistry::instance()->mapLayer(childelem.attribute("layerid"));
if(theMapLayer == NULL) //the layer cannot be found (e.g. the file has been moved)
{
// remove the whole legendlayer if this is the only legendlayerfile
if(childelem.previousSibling().isNull() && childelem.nextSibling().isNull())
Expand All @@ -1026,7 +1025,6 @@ bool QgsLegend::readXML(QDomNode& legendnode)
}
else if(lastLayerFileGroup)
{
QgsMapLayer* theMapLayer = iter->second;
QgsLegendLayerFile* theLegendLayerFile = new QgsLegendLayerFile(lastLayerFileGroup, QgsLegendLayerFile::nameFromLayer(theMapLayer), theMapLayer);

// load layer's visibility and 'show in overview' flag
Expand Down
12 changes: 6 additions & 6 deletions src/app/qgspastetransformations.cpp
Expand Up @@ -32,26 +32,26 @@ QgsPasteTransformations::QgsPasteTransformations()
{

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

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

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

sourceLayerComboBox ->insertItem( it->second->name() );
destinationLayerComboBox->insertItem( it->second->name() );
sourceLayerComboBox ->insertItem( it.value()->name() );
destinationLayerComboBox->insertItem( it.value()->name() );

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


Expand Down
16 changes: 8 additions & 8 deletions src/app/qgsrasterlayerproperties.cpp
Expand Up @@ -278,13 +278,13 @@ mpRasterLayer( dynamic_cast<QgsRasterLayer*>(lyr) )

cboxTransparencyBand->insertItem(tr(QgsRasterLayer::QSTRING_NOT_SET));
cboxTransparencyLayer->insertItem(tr(QgsRasterLayer::QSTRING_NOT_SET));
std::map<QString, QgsMapLayer *> myLayers = QgsMapLayerRegistry::instance()->mapLayers();
std::map<QString, QgsMapLayer *>::iterator it;
QMap<QString, QgsMapLayer *> myLayers = QgsMapLayerRegistry::instance()->mapLayers();
QMap<QString, QgsMapLayer *>::iterator it;
for(it = myLayers.begin(); it != myLayers.end(); it++)
{
if(QgsMapLayer::RASTER == it->second->type())
if(QgsMapLayer::RASTER == it.value()->type())
{
cboxTransparencyLayer->insertItem(it->second->name());
cboxTransparencyLayer->insertItem(it.value()->name());
}
}

Expand Down Expand Up @@ -1666,13 +1666,13 @@ void QgsRasterLayerProperties::on_cboxTransparencyLayer_currentIndexChanged(cons
}
else
{
std::map<QString, QgsMapLayer *> myLayers = QgsMapLayerRegistry::instance()->mapLayers();
std::map<QString, QgsMapLayer *>::iterator it;
QMap<QString, QgsMapLayer *> myLayers = QgsMapLayerRegistry::instance()->mapLayers();
QMap<QString, QgsMapLayer *>::iterator it;
for(it = myLayers.begin(); it != myLayers.end(); it++)
{
if(theText == it->second->name() && QgsMapLayer::RASTER == it->second->type())
if(theText == it.value()->name() && QgsMapLayer::RASTER == it.value()->type())
{
QgsRasterLayer* myRasterLayer = (QgsRasterLayer*)it->second;
QgsRasterLayer* myRasterLayer = (QgsRasterLayer*)it.value();
int myBandCount = myRasterLayer->getBandCount();
cboxTransparencyBand->clear();
cboxTransparencyBand->insertItem(tr(QgsRasterLayer::QSTRING_NOT_SET));
Expand Down
26 changes: 7 additions & 19 deletions src/core/qgsmaplayerregistry.cpp
Expand Up @@ -60,15 +60,7 @@ const int QgsMapLayerRegistry::count()

QgsMapLayer * QgsMapLayerRegistry::mapLayer(QString theLayerId)
{
QgsMapLayer * myMapLayer = mMapLayers[theLayerId];
if (myMapLayer)
{
return myMapLayer;
}
else
{
return 0;
}
return mMapLayers.value(theLayerId);
}


Expand All @@ -83,7 +75,7 @@ QgsMapLayer *
}

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

std::map<QString, QgsMapLayer *>::iterator myMapIterator = mMapLayers.begin();
while ( myMapIterator != mMapLayers.end() )
QMap<QString, QgsMapLayer *>::iterator it;
for (it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
{
delete myMapIterator->second; // delete the map layer

mMapLayers.erase( myMapIterator );

myMapIterator = mMapLayers.begin(); // since iterator invalidated due to
// erase(), reset to new first element
delete it.value(); // delete the map layer
}
mMapLayers.clear();

} // QgsMapLayerRegistry::removeAllMapLayers()


std::map<QString,QgsMapLayer*> & QgsMapLayerRegistry::mapLayers()
QMap<QString,QgsMapLayer*> & QgsMapLayerRegistry::mapLayers()
{
QgsDebugMsg("QgsMapLayerRegistry::mapLayers");
return mMapLayers;
Expand Down
6 changes: 3 additions & 3 deletions src/core/qgsmaplayerregistry.h
Expand Up @@ -20,7 +20,7 @@
#ifndef QGSMAPLAYERREGISTRY_H
#define QGSMAPLAYERREGISTRY_H

#include <map>
#include <QMap>
#include <QObject>

class QString;
Expand Down Expand Up @@ -52,7 +52,7 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
QgsMapLayer * mapLayer(QString theLayerId);

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

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

static QgsMapLayerRegistry* mInstance;

std::map<QString,QgsMapLayer*> mMapLayers;
QMap<QString,QgsMapLayer*> mMapLayers;

/** debugging member
invoked when a connect() is made to this object
Expand Down
6 changes: 3 additions & 3 deletions src/core/qgsproject.cpp
Expand Up @@ -975,18 +975,18 @@ bool QgsProject::write()
emit writeProject(*doc);

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

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

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

if (ml)
{
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/gps_importer/qgsgpsplugin.cpp
Expand Up @@ -113,12 +113,12 @@ void QgsGPSPlugin::run()
{
// find all GPX layers
std::vector<QgsVectorLayer*> gpxLayers;
std::map<QString, QgsMapLayer*>::const_iterator iter;
QMap<QString, QgsMapLayer*>::const_iterator iter;
QgsMapLayerRegistry* registry = QgsMapLayerRegistry::instance();
for (iter = registry->mapLayers().begin();
iter != registry->mapLayers().end(); ++iter) {
if (iter->second->type() == QgsMapLayer::VECTOR) {
QgsVectorLayer* vLayer = dynamic_cast<QgsVectorLayer*>(iter->second);
if (iter.value()->type() == QgsMapLayer::VECTOR) {
QgsVectorLayer* vLayer = dynamic_cast<QgsVectorLayer*>(iter.value());
if (vLayer->providerType() == "gpx")
gpxLayers.push_back(vLayer);
}
Expand Down

0 comments on commit f5afee3

Please sign in to comment.