|
32 | 32 | #include "qgsmaplayerregistry.h"
|
33 | 33 | #include "qgsproject.h"
|
34 | 34 | #include "qgsrasterlayerproperties.h"
|
| 35 | +#include <float.h> |
35 | 36 | #include <QCoreApplication>
|
36 | 37 | #include <QPixmap>
|
37 | 38 | #include <QMouseEvent>
|
@@ -403,6 +404,7 @@ void QgsLegend::handleRightClickEvent(QTreeWidgetItem* item, const QPoint& posit
|
403 | 404 | else if(li->type() == QgsLegendItem::LEGEND_LAYER)
|
404 | 405 | {
|
405 | 406 | theMenu.addAction(tr("&Properties"), this, SLOT(legendLayerShowProperties()));
|
| 407 | + theMenu.addAction(tr("&Zoom to layer extent"), this, SLOT(zoomToLayerExtent())); |
406 | 408 | theMenu.addAction(QIcon(QPixmap(iconsPath+QString("/mActionAddAllToOverview.png"))), tr("&Add to overview"), this, SLOT(legendLayerAddToOverview()));
|
407 | 409 | theMenu.addAction(QIcon(QPixmap(iconsPath+QString("/mActionRemoveAllFromOverview.png"))), tr("&Remove from overview"), this, SLOT(legendLayerRemoveFromOverview()));
|
408 | 410 | theMenu.addAction(QIcon(QPixmap(iconsPath+QString("/mActionRemove.png"))), tr("&Remove"), this, SLOT(legendLayerRemove()));
|
@@ -1540,3 +1542,70 @@ void QgsLegend::showLegendLayerFileGroups()
|
1540 | 1542 | }
|
1541 | 1543 | while(theItem = nextItem(theItem));
|
1542 | 1544 | }
|
| 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 | +} |
0 commit comments