Skip to content

Commit 40842d6

Browse files
author
mhugent
committed
added zoomToLayerExtent also to legend layer right click menu
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@4957 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 14df914 commit 40842d6

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/legend/qgslegend.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "qgsmaplayerregistry.h"
3333
#include "qgsproject.h"
3434
#include "qgsrasterlayerproperties.h"
35+
#include <float.h>
3536
#include <QCoreApplication>
3637
#include <QPixmap>
3738
#include <QMouseEvent>
@@ -403,6 +404,7 @@ void QgsLegend::handleRightClickEvent(QTreeWidgetItem* item, const QPoint& posit
403404
else if(li->type() == QgsLegendItem::LEGEND_LAYER)
404405
{
405406
theMenu.addAction(tr("&Properties"), this, SLOT(legendLayerShowProperties()));
407+
theMenu.addAction(tr("&Zoom to layer extent"), this, SLOT(zoomToLayerExtent()));
406408
theMenu.addAction(QIcon(QPixmap(iconsPath+QString("/mActionAddAllToOverview.png"))), tr("&Add to overview"), this, SLOT(legendLayerAddToOverview()));
407409
theMenu.addAction(QIcon(QPixmap(iconsPath+QString("/mActionRemoveAllFromOverview.png"))), tr("&Remove from overview"), this, SLOT(legendLayerRemoveFromOverview()));
408410
theMenu.addAction(QIcon(QPixmap(iconsPath+QString("/mActionRemove.png"))), tr("&Remove"), this, SLOT(legendLayerRemove()));
@@ -1540,3 +1542,70 @@ void QgsLegend::showLegendLayerFileGroups()
15401542
}
15411543
while(theItem = nextItem(theItem));
15421544
}
1545+
1546+
void QgsLegend::zoomToLayerExtent()
1547+
{
1548+
//find current Layer
1549+
QgsLegendLayer* currentLayer=dynamic_cast<QgsLegendLayer*>(currentItem());
1550+
if(!currentLayer)
1551+
{
1552+
return;
1553+
}
1554+
1555+
std::list<QgsLegendLayerFile*> layerFiles = currentLayer->legendLayerFiles();
1556+
if(layerFiles.size() == 0)
1557+
{
1558+
return;
1559+
}
1560+
1561+
double xmin = DBL_MAX;
1562+
double ymin = DBL_MAX;
1563+
double xmax = -DBL_MAX;
1564+
double ymax = -DBL_MAX;
1565+
1566+
QgsRect transformedExtent;
1567+
QgsRect layerExtent;
1568+
QgsCoordinateTransform *ct;
1569+
QgsMapLayer* theLayer;
1570+
1571+
for(std::list<QgsLegendLayerFile*>::iterator it= layerFiles.begin(); it != layerFiles.end(); ++it)
1572+
{
1573+
theLayer = (*it)->layer();
1574+
if(theLayer)
1575+
{
1576+
layerExtent = theLayer->extent();
1577+
ct = theLayer->coordinateTransform();
1578+
if(ct)
1579+
{
1580+
//transform layer extent to canvas coordinate system
1581+
transformedExtent = ct->transform(layerExtent);
1582+
}
1583+
else
1584+
{
1585+
transformedExtent = layerExtent;
1586+
}
1587+
1588+
if(transformedExtent.xMin() < xmin)
1589+
{
1590+
xmin = transformedExtent.xMin();
1591+
}
1592+
if(transformedExtent.yMin() < ymin)
1593+
{
1594+
ymin = transformedExtent.yMin();
1595+
}
1596+
if(transformedExtent.xMax() > xmax)
1597+
{
1598+
xmax = transformedExtent.xMax();
1599+
}
1600+
if(transformedExtent.yMax() > ymax)
1601+
{
1602+
ymax = transformedExtent.yMax();
1603+
}
1604+
}
1605+
}
1606+
1607+
//zoom to bounding box
1608+
mMapCanvas->setExtent(QgsRect(xmin, ymin, xmax, ymax));
1609+
mMapCanvas->render();
1610+
mMapCanvas->refresh();
1611+
}

src/legend/qgslegend.h

+4
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ this item may be moved back to the original position with resetToInitialPosition
253253
void makeToTopLevelItem();
254254
/**Show/ Hide the legend layer file groups*/
255255
void showLegendLayerFileGroups();
256+
/**Zooms to extent of the current legend layer (considers there may be several
257+
legend layer files*/
258+
void zoomToLayerExtent();
259+
256260
private:
257261

258262
/**Pointer to QGisApp, needed for signal/slot reasons*/

0 commit comments

Comments
 (0)