Skip to content

Commit

Permalink
fix for activeLayer()
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@4826 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Feb 11, 2006
1 parent 6ed5134 commit 7a8787a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 58 deletions.
92 changes: 37 additions & 55 deletions src/gui/qgisapp.cpp
Expand Up @@ -3995,51 +3995,49 @@ void QgisApp::zoomToLayerExtent()
// zoom only if one or more layers loaded
if(QgsMapLayerRegistry::instance()->count() > 0)
{
// get the selected item
QTreeWidgetItem *li = mMapLegend->currentItem();
QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>(li);
if(llf)
{
QgsMapLayer *layer = llf->layer();
// Check if the layer extent has to be transformed to the map canvas
// coordinate system

QgsMapLayer *layer = mMapLegend->currentLayer();
if(layer)
{
// Check if the layer extent has to be transformed to the map canvas
// coordinate system
#ifdef QGISDEBUG
std::cout << "Layer extent is : " << (layer->extent()).stringRep().toLocal8Bit().data() << std::endl;
std::cout << "Layer extent is : " << (layer->extent()).stringRep().toLocal8Bit().data() << std::endl;
#endif
if (QgsProject::instance()->readNumEntry("SpatialRefSys","/ProjectionsEnabled",0)!=0)
{
QgsCoordinateTransform *ct = layer->coordinateTransform();
try {
QgsRect transformedExtent = ct->transform(layer->extent());
mMapCanvas->setExtent(transformedExtent);
if (QgsProject::instance()->readNumEntry("SpatialRefSys","/ProjectionsEnabled",0)!=0)
{
QgsCoordinateTransform *ct = layer->coordinateTransform();
try {
QgsRect transformedExtent = ct->transform(layer->extent());
mMapCanvas->setExtent(transformedExtent);
#ifdef QGISDEBUG
std::cout << "Canvas extent is : " << transformedExtent.stringRep().toLocal8Bit().data() << std::endl;
std::cout << "Canvas extent is : " << transformedExtent.stringRep().toLocal8Bit().data() << std::endl;
#endif
}
catch(QgsCsException &cse)
{
}
catch(QgsCsException &cse)
{
#ifdef QGISDEBUG
std::cout << "Caught transform error in zoomToLayerExtent(). "
<< "Setting untransformed extents." << std::endl;
std::cout << "Caught transform error in zoomToLayerExtent(). "
<< "Setting untransformed extents." << std::endl;
#endif
mMapCanvas->setExtent(layer->extent());
}
}
else
{
mMapCanvas->setExtent(layer->extent());
}
mMapCanvas->clear();
mMapCanvas->setExtent(layer->extent());
}
}
else
{
mMapCanvas->setExtent(layer->extent());
}
mMapCanvas->clear();
// For Qt4, deprecate direct calling of render(). Let render() be called by the
// paint event loop of the map canvas widget.
// XXX - this doesn't work -- or nobody implemented it so render() is still enabled
// [gsherman]
mMapCanvas->render();
mMapCanvas->update();
mMapCanvas->render();
mMapCanvas->update();

// notify the project we've made a change
QgsProject::instance()->dirty(true);
}
// notify the project we've made a change
QgsProject::instance()->dirty(true);
}
}
} // QgisApp::zoomToLayerExtent()

Expand Down Expand Up @@ -4697,34 +4695,18 @@ void QgisApp::openURL(QString url, bool useQgisDocDirectory)
/** Get a pointer to the currently selected map layer */
QgsMapLayer *QgisApp::activeLayer()
{
QTreeWidgetItem *lvi = mMapLegend->currentItem();
QgsMapLayer *layer = 0;
if (lvi)
{
QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>(lvi);
if(llf)
{
layer = llf->layer();
}
}
return layer;
return (mMapLegend->currentLayer());
}

QString QgisApp::activeLayerSource()
{
QString source;
QTreeWidgetItem *lvi = mMapLegend->currentItem();
QgsMapLayer *layer = 0;
if (lvi)
{
QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>(lvi);
if(llf)
QgsMapLayer* layer = mMapLegend->currentLayer();
if(layer)
{
layer = llf->layer();
return (layer->source());
}
source = layer->source();
}
return source;
return "";
}

/** Add a vector layer directly without prompting user for location
Expand Down
14 changes: 11 additions & 3 deletions src/legend/qgslegend.cpp
Expand Up @@ -482,18 +482,26 @@ QgsMapLayer* QgsLegend::currentLayer()
QgsLegendLayerFile* llf=dynamic_cast<QgsLegendLayerFile*>(citem);
if(llf)
{
return llf->layer();
return llf->layer(); //the current item is itself a legend layer file
}
else
{
QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>(citem);
if(ll)
{
return ll->firstMapLayer();
return ll->firstMapLayer(); //the current item is a legend layer, so return its first layer
}
else
{
return 0;
QgsLegendLayer* lpl = dynamic_cast<QgsLegendLayer*>(citem->parent());
if(lpl)
{
return lpl->firstMapLayer(); //the parent of the current item is a legend layer, return its first layer
}
else
{
return 0;
}
}
}
}
Expand Down

0 comments on commit 7a8787a

Please sign in to comment.