Correctly prevent time travel in planner

Prior to this change the visual feedback (the handle that is drawn when
the user moves the mouse while pressing the left mouse button) would not
move to an illegal position (one that is impossible without time
travel), but it the user moved the mouse to such an illegal position and
then released the mouse button, we still added that illegal position to
the plan.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2013-06-27 21:38:12 +08:00
parent 47e6e555cc
commit 3cdf8dc4c1
2 changed files with 9 additions and 4 deletions

View file

@ -5,7 +5,8 @@
#include <QDebug> #include <QDebug>
#include "ui_diveplanner.h" #include "ui_diveplanner.h"
DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0) DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0),
lastValidPos(0.0, 0.0)
{ {
setMouseTracking(true); setMouseTracking(true);
setScene(new QGraphicsScene()); setScene(new QGraphicsScene());
@ -213,18 +214,22 @@ void DivePlannerGraphics::moveActiveHandler(const QPointF& pos)
if (idx == 0 ) { // first if (idx == 0 ) { // first
if (newPos.x() < handles[1]->x()) { if (newPos.x() < handles[1]->x()) {
activeDraggedHandler->setPos(newPos); activeDraggedHandler->setPos(newPos);
lastValidPos = newPos;
} }
} else if (idx == handles.count()-1) { // last } else if (idx == handles.count()-1) { // last
if (newPos.x() > handles[idx-1]->x()) { if (newPos.x() > handles[idx-1]->x()) {
activeDraggedHandler->setPos(newPos); activeDraggedHandler->setPos(newPos);
lastValidPos = newPos;
} }
} else { // middle } else { // middle
if (newPos.x() > handles[idx-1]->x() && newPos.x() < handles[idx+1]->x()) { if (newPos.x() > handles[idx-1]->x() && newPos.x() < handles[idx+1]->x()) {
activeDraggedHandler->setPos(newPos); activeDraggedHandler->setPos(newPos);
lastValidPos = newPos;
} }
} }
} else { } else {
activeDraggedHandler->setPos(newPos); activeDraggedHandler->setPos(newPos);
lastValidPos = newPos;
} }
qDeleteAll(lines); qDeleteAll(lines);
lines.clear(); lines.clear();
@ -260,9 +265,8 @@ void DivePlannerGraphics::mousePressEvent(QMouseEvent* event)
void DivePlannerGraphics::mouseReleaseEvent(QMouseEvent* event) void DivePlannerGraphics::mouseReleaseEvent(QMouseEvent* event)
{ {
if (activeDraggedHandler) { if (activeDraggedHandler) {
QPointF mappedPos = mapToScene(event->pos()); activeDraggedHandler->sec = rint(timeLine->valueAt(lastValidPos)) * 60;
activeDraggedHandler->sec = rint(timeLine->valueAt(mappedPos)) * 60; activeDraggedHandler->mm = rint(depthLine->valueAt(lastValidPos)) * 1000;
activeDraggedHandler->mm = rint(depthLine->valueAt(mappedPos)) * 1000;
activeDraggedHandler->setBrush(QBrush()); activeDraggedHandler->setBrush(QBrush());
createDecoStops(); createDecoStops();
activeDraggedHandler = 0; activeDraggedHandler = 0;

View file

@ -87,6 +87,7 @@ private:
Button *lessTime; Button *lessTime;
Button *lessDepth; Button *lessDepth;
QPointF lastValidPos;
}; };
namespace Ui{ namespace Ui{