Added Mouse based Zoom / Movement.

This patch uses the same code that lubomir used on the
old profile. It strangely didn't worked - most probably
because the scene has a fixed width() and height() of
100. the zoom works, and the movement works, but only
on the 100 first pixels of the profile.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2014-02-04 21:47:50 -02:00 committed by Dirk Hohndel
parent dc077e7bff
commit a649bcc7bc
2 changed files with 76 additions and 2 deletions

View file

@ -14,6 +14,7 @@
#include <QMenu>
#include <QContextMenuEvent>
#include <QDebug>
#include <QScrollBar>
#ifndef QT_NO_DEBUG
#include <QTableView>
@ -24,6 +25,7 @@ ProfileWidget2::ProfileWidget2(QWidget *parent) :
QGraphicsView(parent),
dataModel(new DivePlotDataModel(this)),
currentState(INVALID),
zoomLevel(0),
stateMachine(new QStateMachine(this)),
background (new DivePixmapItem()),
profileYAxis(new DepthAxis()),
@ -50,7 +52,7 @@ ProfileWidget2::ProfileWidget2(QWidget *parent) :
setOptimizationFlags(QGraphicsView::DontSavePainterState);
setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
setMouseTracking(true);
// Creating the needed items.
// ORDER: {BACKGROUND, PROFILE_Y_AXIS, GAS_Y_AXIS, TIME_AXIS, DEPTH_CONTROLLER, TIME_CONTROLLER, COLUMNS};
profileYAxis->setOrientation(DiveCartesianAxis::TopToBottom);
@ -476,3 +478,72 @@ void ProfileWidget2::fixBackgroundPos()
bg->setPixmap(p);
bg->setX(mapToScene(x, 0).x());
}
void ProfileWidget2::wheelEvent(QWheelEvent* event)
{
// if (!toolTip)
// return;
// doesn't seem to work for Qt 4.8.1
// setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
// Scale the view / do the zoom
// QPoint toolTipPos = mapFromScene(toolTip->pos());
double scaleFactor = 1.15;
if (event->delta() > 0 && zoomLevel < 20) {
scale(scaleFactor, scaleFactor);
zoomLevel++;
} else if (event->delta() < 0 && zoomLevel > 0) {
// Zooming out
scale(1.0 / scaleFactor, 1.0 / scaleFactor);
zoomLevel--;
}
scrollViewTo(event->pos());
// toolTip->setPos(mapToScene(toolTipPos));
// toolBarProxy->setPos(mapToScene(TOOLBAR_POS));
// if (zoomLevel != 0) {
// toolBarProxy->hide();
// } else {
// toolBarProxy->show();
// }
}
void ProfileWidget2::scrollViewTo(const QPoint& pos)
{
/* since we cannot use translate() directly on the scene we hack on
* the scroll bars (hidden) functionality */
if (!zoomLevel)
return;
QScrollBar *vs = verticalScrollBar();
QScrollBar *hs = horizontalScrollBar();
const qreal yRat = pos.y() / sceneRect().height();
const qreal xRat = pos.x() / sceneRect().width();
const int vMax = vs->maximum();
const int hMax = hs->maximum();
const int vMin = vs->minimum();
const int hMin = hs->minimum();
/* QScrollBar receives crazy negative values for minimum */
vs->setValue(yRat * (vMax - vMin) + vMin * 0.9);
hs->setValue(xRat * (hMax - hMin) + hMin * 0.9);
}
void ProfileWidget2::mouseMoveEvent(QMouseEvent* event)
{
// if (!toolTip)
// return;
//
// toolTip->refresh(&gc, mapToScene(event->pos()));
// QPoint toolTipPos = mapFromScene(toolTip->pos());
if (zoomLevel == 0) {
QGraphicsView::mouseMoveEvent(event);
} else {/*
toolTip->setPos(mapToScene(toolTipPos));
toolBarProxy->setPos(mapToScene(TOOLBAR_POS));*/
scrollViewTo(event->pos());
}
}

View file

@ -43,6 +43,7 @@ struct PartialGasPressureAxis;
class ProfileWidget2 : public QGraphicsView {
Q_OBJECT
void fixBackgroundPos();
void scrollViewTo(const QPoint& pos);
public:
enum State{ EMPTY, PROFILE, EDIT, ADD, PLAN, INVALID };
enum Items{BACKGROUND, PROFILE_Y_AXIS, GAS_Y_AXIS, TIME_AXIS, DEPTH_CONTROLLER, TIME_CONTROLLER, COLUMNS};
@ -55,6 +56,8 @@ public slots: // Necessary to call from QAction's signals.
protected:
virtual void contextMenuEvent(QContextMenuEvent* event);
virtual void resizeEvent(QResizeEvent* event);
virtual void wheelEvent(QWheelEvent* event);
virtual void mouseMoveEvent(QMouseEvent* event);
signals:
void startProfileState();
@ -70,7 +73,7 @@ private:
DivePlotDataModel *dataModel;
State currentState;
QStateMachine *stateMachine;
int zoomLevel;
DivePixmapItem *background ;
// All those here should probably be merged into one structure,
// So it's esyer to replicate for more dives later.