Skip to content

Commit

Permalink
Added zooming to 100%, i.e one raster pixel will occupy one screen pi…
Browse files Browse the repository at this point in the history
…xel. 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@6735 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
homann committed Feb 28, 2007
1 parent 32c3743 commit d1d5598
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/app/legend/qgslegend.cpp
Expand Up @@ -1696,6 +1696,34 @@ void QgsLegend::legendLayerZoom()
QgsProject::instance()->dirty(true);
}

void QgsLegend::legendLayerZoomNative()
{
QgsLegendItem* citem=dynamic_cast<QgsLegendItem*>(currentItem());
if(!citem)
{
return;
}
QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>(citem);
if(!ll)
{
return;
}
QgsRasterLayer *layer = dynamic_cast<QgsRasterLayer*>(ll->firstMapLayer());
if(layer)
{
QgsDebugMsg("Raster units per pixel : " + QString::number(layer->rasterUnitsPerPixel()));
QgsDebugMsg("Mupp before : " + QString::number(mMapCanvas->mupp()));

mMapCanvas->zoom(fabs( layer->rasterUnitsPerPixel() / mMapCanvas->mupp()));
mMapCanvas->refresh();

QgsDebugMsg("Mupp after : " + QString::number(mMapCanvas->mupp()));

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

void QgsLegend::legendLayerAttributeTable()
{

Expand Down
4 changes: 4 additions & 0 deletions src/app/legend/qgslegend.h
Expand Up @@ -216,6 +216,10 @@ public slots:
/**Zooms to extent of the current legend layer (considers there may be several
legend layer files*/
void legendLayerZoom();

/***Zooms so that the pixels of the raster layer occupies exactly one screen pixel.
Only works on raster layers*/
void legendLayerZoomNative();

/**Show attribute table*/
void legendLayerAttributeTable();
Expand Down
7 changes: 6 additions & 1 deletion src/app/legend/qgslegendlayer.cpp
Expand Up @@ -24,6 +24,7 @@
#include "qgslegendlayerfile.h"
#include "qgslegendlayerfilegroup.h"
#include "qgslegendsymbologyitem.h"
#include "qgslogger.h"

#include "qgsapplication.h"
#include "qgsfield.h"
Expand Down Expand Up @@ -469,7 +470,11 @@ void QgsLegendLayer::addToPopupMenu(QMenu& theMenu)
// zoom to layer extent
theMenu.addAction(QIcon(iconsPath+QString("/mActionZoomToLayer.png")),
tr("&Zoom to layer extent"), legend(), SLOT(legendLayerZoom()));

if (firstLayer && firstLayer->type() == QgsMapLayer::RASTER)
{
theMenu.addAction(tr("&Zoom to best scale (100%)"), legend(), SLOT(legendLayerZoomNative()));
}

// show in overview
QAction* showInOverviewAction = theMenu.addAction(tr("&Show in overview"), this, SLOT(showInOverview()));
showInOverviewAction->setCheckable(true); // doesn't support tristate
Expand Down
20 changes: 16 additions & 4 deletions src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -1057,10 +1057,10 @@ bool QgsRasterLayer::draw(QPainter * theQPainter,
myRasterViewPort->topLeftPoint = theQgsMapToPixel->transform(myRasterExtent.xMin(), myRasterExtent.yMax());
myRasterViewPort->bottomRightPoint = theQgsMapToPixel->transform(myRasterExtent.xMax(), myRasterExtent.yMin());

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

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

double QgsRasterLayer::rasterUnitsPerPixel()
{

// We return one raster pixel per map unit pixel
// One raster pixel can have several raster units...

// We can only use one of the adfGeoTransform[], so go with the
// horisontal one.

return fabs(adfGeoTransform[1]);
}


// ENDS
4 changes: 4 additions & 0 deletions src/core/raster/qgsrasterlayer.h
Expand Up @@ -294,6 +294,10 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
/** \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*/
QString getProjectionWKT();

/** \brief Returns the number of raster units per each raster pixel. For rasters with world file, this is
normally the first row (without the sign) in that file */
double rasterUnitsPerPixel();

/** \brief Draws a thumbnail of the rasterlayer into the supplied pixmap pointer */
void drawThumbnail(QPixmap * theQPixmap);

Expand Down
10 changes: 10 additions & 0 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -1073,3 +1073,13 @@ void QgsMapCanvas::writeProject(QDomDocument & doc)


}

void QgsMapCanvas::zoom(double scaleFactor)
{

QgsRect r = mMapRender->extent();
r.scale(scaleFactor);
setExtent(r);
refresh();
}

4 changes: 4 additions & 0 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -230,6 +230,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! returns last position of mouse cursor
QPoint mouseLastXY();

//! zooms with the factor supplied. Factor > 1 zooms in
void zoom(double scaleFactor);


public slots:

/**Sets dirty=true and calls render()*/
Expand Down

0 comments on commit d1d5598

Please sign in to comment.