Skip to content

Commit

Permalink
Fix for ticket #415.
Browse files Browse the repository at this point in the history
Mouse wheel action made configurable: zoom (default), zoom and recenter, do nothing.
Also made zoom factor configurable.


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@6177 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Dec 3, 2006
1 parent 453b88f commit 3888620
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 44 deletions.
8 changes: 8 additions & 0 deletions src/gui/qgisapp.cpp
Expand Up @@ -1126,6 +1126,10 @@ void QgisApp::createOverview()
QSettings mySettings;
mMapCanvas->enableAntiAliasing(mySettings.value("/qgis/enable_anti_aliasing",false).toBool());
mMapCanvas->useQImageToRender(mySettings.value("/qgis/use_qimage_to_render",false).toBool());

int action = mySettings.value("/qgis/wheel_action", 0).toInt();
double zoomFactor = mySettings.value("/qgis/zoom_factor", 2).toDouble();
mMapCanvas->setWheelAction((QgsMapCanvas::WheelAction) action, zoomFactor);
}


Expand Down Expand Up @@ -4127,6 +4131,10 @@ void QgisApp::options()
QSettings mySettings;
mMapCanvas->enableAntiAliasing(mySettings.value("/qgis/enable_anti_aliasing").toBool());
mMapCanvas->useQImageToRender(mySettings.value("/qgis/use_qimage_to_render").toBool());

int action = mySettings.value("/qgis/wheel_action", 0).toInt();
double zoomFactor = mySettings.value("/qgis/zoom_factor", 2).toDouble();
mMapCanvas->setWheelAction((QgsMapCanvas::WheelAction) action, zoomFactor);
}
}

Expand Down
64 changes: 42 additions & 22 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -76,10 +76,6 @@ class QgsMapCanvas::CanvasProperties



// But the static members must be initialised outside the class! (or GCC 4 dies)
const double QgsMapCanvas::scaleDefaultMultiple = 2.0;


/** note this is private and so shouldn't be accessible */
QgsMapCanvas::QgsMapCanvas()
{}
Expand All @@ -102,6 +98,8 @@ QgsMapCanvas::QgsMapCanvas()
mFrozen = false;
mDirty = true;

setWheelAction(WheelZoom);

// by default, the canvas is rendered
mRenderFlag = true;

Expand Down Expand Up @@ -740,20 +738,55 @@ void QgsMapCanvas::updateCanvasItemsPositions()
void QgsMapCanvas::wheelEvent(QWheelEvent *e)
{
// Zoom the map canvas in response to a mouse wheel event. Moving the
// wheel forward (away) from the user zooms in by a factor of 2.
// TODO The scale factor needs to be customizable by the user.
// wheel forward (away) from the user zooms in
#ifdef QGISDEBUG
std::cout << "Wheel event delta " << e->delta() << std::endl;
#endif

// change extent
zoomWithCenter(e->x(), e->y(), e->delta() > 0);
switch (mWheelAction)
{
case WheelZoom:
// zoom without changing extent
zoom(e->delta() > 0);
break;

case WheelZoomAndRecenter:
// zoom and don't change extent
zoomWithCenter(e->x(), e->y(), e->delta() > 0);
break;

case WheelNothing:
// well, nothing!
break;
}
}

void QgsMapCanvas::setWheelAction(WheelAction action, double factor)
{
mWheelAction = action;
mWheelZoomFactor = factor;
}

void QgsMapCanvas::zoom(bool zoomIn)
{
double scaleFactor = (zoomIn ? 1/mWheelZoomFactor : mWheelZoomFactor);

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

void QgsMapCanvas::zoomWithCenter(int x, int y, bool zoomIn)
{
zoomByScale(x, y, (zoomIn ? 1/scaleDefaultMultiple : scaleDefaultMultiple));
double scaleFactor = (zoomIn ? 1/mWheelZoomFactor : mWheelZoomFactor);

// transform the mouse pos to map coordinates
QgsPoint center = getCoordinateTransform()->toMapPoint(x, y);
QgsRect r = mMapRender->extent();
r.scale(scaleFactor, &center);
setExtent(r);
refresh();
}


Expand All @@ -777,19 +810,6 @@ void QgsMapCanvas::contentsMouseMoveEvent(QMouseEvent * e)
} // mouseMoveEvent


/**
* Zooms at the given screen x and y by the given scale (< 1, zoom out, > 1, zoom in)
*/
void QgsMapCanvas::zoomByScale(int x, int y, double scaleFactor)
{
// transform the mouse pos to map coordinates
QgsPoint center = getCoordinateTransform()->toMapPoint(x, y);
QgsRect r = mMapRender->extent();
r.scale(scaleFactor, &center);
setExtent(r);
refresh();
}


/** Sets the map tool currently being used on the canvas */
void QgsMapCanvas::setMapTool(QgsMapTool* tool)
Expand Down
22 changes: 15 additions & 7 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -64,7 +64,10 @@ class QgsMapCanvas : public Q3CanvasView
{
Q_OBJECT;

public:
public:

enum WheelAction { WheelZoom, WheelZoomAndRecenter, WheelNothing };

//! Constructor
QgsMapCanvas(QWidget * parent = 0, const char *name = 0);

Expand Down Expand Up @@ -186,8 +189,14 @@ class QgsMapCanvas : public Q3CanvasView

//! returns current layer (set by legend widget)
QgsMapLayer* currentLayer();

//! set wheel action and zoom factor (should be greater than 1)
void setWheelAction(WheelAction action, double factor = 2);

//! Zooms in/out with a given center (uses zoomByScale)
//! Zooms in/out preserving
void zoom(bool zoomIn);

//! Zooms in/out with a given center
void zoomWithCenter(int x, int y, bool zoomIn);

//! used to determine if anti-aliasing is enabled or not
Expand Down Expand Up @@ -340,9 +349,6 @@ class QgsMapCanvas : public Q3CanvasView
//! Overridden draw contents from canvas view
void drawContents(QPainter * p, int cx, int cy, int cw, int ch);

//! Zooms to a given center and scale
void zoomByScale(int x, int y, double scaleFactor);

//! called when panning is in action, reset indicates end of panning
void moveCanvasContents(bool reset = FALSE);

Expand Down Expand Up @@ -372,8 +378,10 @@ class QgsMapCanvas : public Q3CanvasView
QgsRect mLastExtent;

//! Scale factor multiple for default zoom in/out
// TODO Make this customisable by the user
static const double scaleDefaultMultiple;
double mWheelZoomFactor;

//! Mouse wheel action
WheelAction mWheelAction;

}; // class QgsMapCanvas

Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgsoptions.cpp
Expand Up @@ -138,6 +138,9 @@ QgsOptions::QgsOptions(QWidget *parent, Qt::WFlags fl) :
pbnCanvasColor->setPalette( QColor(myRed,myGreen,myBlue) );

capitaliseCheckBox->setChecked(settings.value("qgis/capitaliseLayerName", QVariant(false)).toBool());

cmbWheelAction->setCurrentIndex(settings.value("/qgis/wheel_action", 0).toInt());
spinZoomFactor->setValue(settings.value("/qgis/zoom_factor", 2).toDouble());
}

//! Destructor
Expand Down Expand Up @@ -243,6 +246,9 @@ void QgsOptions::saveOptions()
myGreen = settings.writeEntry("/qgis/default_canvas_color_green",myColor.green());
myBlue = settings.writeEntry("/qgis/default_canvas_color_blue",myColor.blue());

settings.writeEntry("/qgis/wheel_action", cmbWheelAction->currentIndex());
settings.writeEntry("/qgis/zoom_factor", spinZoomFactor->value());

//all done
accept();
}
Expand Down
91 changes: 76 additions & 15 deletions src/ui/qgsoptionsbase.ui
Expand Up @@ -18,17 +18,17 @@
<property name="sizeGripEnabled" >
<bool>true</bool>
</property>
<layout class="QGridLayout" >
<layout class="QVBoxLayout" >
<property name="margin" >
<number>9</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item row="0" column="0" >
<item>
<widget class="QTabWidget" name="tabWidget" >
<property name="currentIndex" >
<number>0</number>
<number>2</number>
</property>
<widget class="QWidget" name="tabAppearance" >
<attribute name="title" >
Expand Down Expand Up @@ -389,7 +389,7 @@
</widget>
<widget class="QWidget" name="tabMap" >
<attribute name="title" >
<string>&amp;Selection &amp;&amp; Measuring</string>
<string>&amp;Map tools</string>
</attribute>
<layout class="QGridLayout" >
<property name="margin" >
Expand All @@ -398,6 +398,19 @@
<property name="spacing" >
<number>6</number>
</property>
<item row="3" column="0" >
<spacer>
<property name="orientation" >
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" >
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0" >
<widget class="QGroupBox" name="groupBox_7" >
<property name="title" >
Expand Down Expand Up @@ -482,17 +495,65 @@ p, li { white-space: pre-wrap; }
</widget>
</item>
<item row="2" column="0" >
<spacer>
<property name="orientation" >
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" >
<size>
<width>20</width>
<height>40</height>
</size>
<widget class="QGroupBox" name="groupBox_10" >
<property name="title" >
<string>Panning and zooming</string>
</property>
</spacer>
<layout class="QGridLayout" >
<property name="margin" >
<number>9</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item row="0" column="1" >
<widget class="QComboBox" name="cmbWheelAction" >
<item>
<property name="text" >
<string>Zoom</string>
</property>
</item>
<item>
<property name="text" >
<string>Zoom and recenter</string>
</property>
</item>
<item>
<property name="text" >
<string>Nothing</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0" >
<widget class="QLabel" name="label_3" >
<property name="text" >
<string>Zoom factor:</string>
</property>
</widget>
</item>
<item row="0" column="0" >
<widget class="QLabel" name="label_2" >
<property name="text" >
<string>Mouse wheel action:</string>
</property>
</widget>
</item>
<item row="1" column="1" >
<widget class="QDoubleSpinBox" name="spinZoomFactor" >
<property name="decimals" >
<number>1</number>
</property>
<property name="minimum" >
<double>1.100000000000000</double>
</property>
<property name="value" >
<double>2.000000000000000</double>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
Expand Down Expand Up @@ -671,7 +732,7 @@ p, li { white-space: pre-wrap; }
</widget>
</widget>
</item>
<item row="1" column="0" >
<item>
<layout class="QHBoxLayout" >
<property name="margin" >
<number>0</number>
Expand Down

0 comments on commit 3888620

Please sign in to comment.