Skip to content

Commit a6bd371

Browse files
author
homann
committed
Added zooming to 100%, i.e one raster pixel will occupy one screen pixel. Only supported for raster layers. Right-click on the layer in the legend, and select from pop-up menu
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@6735 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 9fac1f5 commit a6bd371

7 files changed

+72
-5
lines changed

src/app/legend/qgslegend.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,34 @@ void QgsLegend::legendLayerZoom()
16961696
QgsProject::instance()->dirty(true);
16971697
}
16981698

1699+
void QgsLegend::legendLayerZoomNative()
1700+
{
1701+
QgsLegendItem* citem=dynamic_cast<QgsLegendItem*>(currentItem());
1702+
if(!citem)
1703+
{
1704+
return;
1705+
}
1706+
QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>(citem);
1707+
if(!ll)
1708+
{
1709+
return;
1710+
}
1711+
QgsRasterLayer *layer = dynamic_cast<QgsRasterLayer*>(ll->firstMapLayer());
1712+
if(layer)
1713+
{
1714+
QgsDebugMsg("Raster units per pixel : " + QString::number(layer->rasterUnitsPerPixel()));
1715+
QgsDebugMsg("Mupp before : " + QString::number(mMapCanvas->mupp()));
1716+
1717+
mMapCanvas->zoom(fabs( layer->rasterUnitsPerPixel() / mMapCanvas->mupp()));
1718+
mMapCanvas->refresh();
1719+
1720+
QgsDebugMsg("Mupp after : " + QString::number(mMapCanvas->mupp()));
1721+
1722+
// notify the project we've made a change
1723+
QgsProject::instance()->dirty(true);
1724+
}
1725+
}
1726+
16991727
void QgsLegend::legendLayerAttributeTable()
17001728
{
17011729

src/app/legend/qgslegend.h

+4
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ public slots:
216216
/**Zooms to extent of the current legend layer (considers there may be several
217217
legend layer files*/
218218
void legendLayerZoom();
219+
220+
/***Zooms so that the pixels of the raster layer occupies exactly one screen pixel.
221+
Only works on raster layers*/
222+
void legendLayerZoomNative();
219223

220224
/**Show attribute table*/
221225
void legendLayerAttributeTable();

src/app/legend/qgslegendlayer.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "qgslegendlayerfile.h"
2525
#include "qgslegendlayerfilegroup.h"
2626
#include "qgslegendsymbologyitem.h"
27+
#include "qgslogger.h"
2728

2829
#include "qgsapplication.h"
2930
#include "qgsfield.h"
@@ -469,7 +470,11 @@ void QgsLegendLayer::addToPopupMenu(QMenu& theMenu)
469470
// zoom to layer extent
470471
theMenu.addAction(QIcon(iconsPath+QString("/mActionZoomToLayer.png")),
471472
tr("&Zoom to layer extent"), legend(), SLOT(legendLayerZoom()));
472-
473+
if (firstLayer && firstLayer->type() == QgsMapLayer::RASTER)
474+
{
475+
theMenu.addAction(tr("&Zoom to best scale (100%)"), legend(), SLOT(legendLayerZoomNative()));
476+
}
477+
473478
// show in overview
474479
QAction* showInOverviewAction = theMenu.addAction(tr("&Show in overview"), this, SLOT(showInOverview()));
475480
showInOverviewAction->setCheckable(true); // doesn't support tristate

src/core/raster/qgsrasterlayer.cpp

+16-4
Original file line numberDiff line numberDiff line change
@@ -1057,10 +1057,10 @@ bool QgsRasterLayer::draw(QPainter * theQPainter,
10571057
myRasterViewPort->topLeftPoint = theQgsMapToPixel->transform(myRasterExtent.xMin(), myRasterExtent.yMax());
10581058
myRasterViewPort->bottomRightPoint = theQgsMapToPixel->transform(myRasterExtent.xMax(), myRasterExtent.yMin());
10591059

1060-
myRasterViewPort->drawableAreaXDimInt =
1061-
abs(static_cast<int> (myRasterViewPort->clippedWidthInt / theQgsMapToPixel->mapUnitsPerPixel() * adfGeoTransform[1]));
1062-
myRasterViewPort->drawableAreaYDimInt =
1063-
abs(static_cast<int> (myRasterViewPort->clippedHeightInt / theQgsMapToPixel->mapUnitsPerPixel() * adfGeoTransform[5]));
1060+
myRasterViewPort->drawableAreaXDimInt = static_cast<int>
1061+
(fabs( (myRasterViewPort->clippedWidthInt / theQgsMapToPixel->mapUnitsPerPixel() * adfGeoTransform[1])) + 0.5);
1062+
myRasterViewPort->drawableAreaYDimInt = static_cast<int>
1063+
(fabs( (myRasterViewPort->clippedHeightInt / theQgsMapToPixel->mapUnitsPerPixel() * adfGeoTransform[5])) + 0.5);
10641064

10651065
#ifdef QGISDEBUG
10661066
QgsLogger::debug("QgsRasterLayer::draw: mapUnitsPerPixel", theQgsMapToPixel->mapUnitsPerPixel(), 1, __FILE__,\
@@ -5262,5 +5262,17 @@ const unsigned int QgsRasterLayer::getBandCount()
52625262
return rasterStatsVector.size();
52635263
};
52645264

5265+
double QgsRasterLayer::rasterUnitsPerPixel()
5266+
{
5267+
5268+
// We return one raster pixel per map unit pixel
5269+
// One raster pixel can have several raster units...
5270+
5271+
// We can only use one of the adfGeoTransform[], so go with the
5272+
// horisontal one.
5273+
5274+
return fabs(adfGeoTransform[1]);
5275+
}
5276+
52655277

52665278
// ENDS

src/core/raster/qgsrasterlayer.h

+4
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
294294
/** \brief Query gdal to find out the WKT projection string for this layer. This implements the virtual method of the same name defined in QgsMapLayer*/
295295
QString getProjectionWKT();
296296

297+
/** \brief Returns the number of raster units per each raster pixel. For rasters with world file, this is
298+
normally the first row (without the sign) in that file */
299+
double rasterUnitsPerPixel();
300+
297301
/** \brief Draws a thumbnail of the rasterlayer into the supplied pixmap pointer */
298302
void drawThumbnail(QPixmap * theQPixmap);
299303

src/gui/qgsmapcanvas.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1073,3 +1073,13 @@ void QgsMapCanvas::writeProject(QDomDocument & doc)
10731073

10741074

10751075
}
1076+
1077+
void QgsMapCanvas::zoom(double scaleFactor)
1078+
{
1079+
1080+
QgsRect r = mMapRender->extent();
1081+
r.scale(scaleFactor);
1082+
setExtent(r);
1083+
refresh();
1084+
}
1085+

src/gui/qgsmapcanvas.h

+4
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
230230
//! returns last position of mouse cursor
231231
QPoint mouseLastXY();
232232

233+
//! zooms with the factor supplied. Factor > 1 zooms in
234+
void zoom(double scaleFactor);
235+
236+
233237
public slots:
234238

235239
/**Sets dirty=true and calls render()*/

0 commit comments

Comments
 (0)