Make it possible to drag a handle between handlers

Make it possible to drag a handle between handlers,
this way the configuration of the dive is more acurate
and easyer to make. I'v discovered a problem where it's
a bit hard to 'grab' the handler, investigating it now.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
This commit is contained in:
Tomaz Canabrava 2013-07-04 10:29:28 -03:00
parent 603d2f5cb3
commit 20ec98f2a5
2 changed files with 26 additions and 33 deletions

View file

@ -22,8 +22,7 @@ bool handlerLessThenMinutes(DiveHandler *d1, DiveHandler *d2){
return d1->sec < d2->sec;
}
DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0),
lastValidPos(0.0, 0.0)
DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0)
{
fill_profile_color();
setBackgroundBrush(profile_color[BACKGROUND].at(0));
@ -185,7 +184,6 @@ void DivePlannerGraphics::mouseDoubleClickEvent(QMouseEvent* event)
item->setPos(QPointF(xpos, ypos));
scene()->addItem(item);
handles << item;
qSort(handles.begin(), handles.end(), handlerLessThenMinutes);
createDecoStops();
}
@ -193,6 +191,7 @@ void DivePlannerGraphics::createDecoStops()
{
qDeleteAll(lines);
lines.clear();
qSort(handles.begin(), handles.end(), handlerLessThenMinutes);
// This needs to be done in the following steps:
// Get the user-input and calculate the dive info
@ -340,33 +339,9 @@ void DivePlannerGraphics::mouseMoveEvent(QMouseEvent* event)
void DivePlannerGraphics::moveActiveHandler(const QPointF& pos)
{
int idx = handles.indexOf(activeDraggedHandler);
double xpos = timeLine->posAtValue(rint(timeLine->valueAt(pos)));
double ypos = depthLine->posAtValue(rint(depthLine->valueAt(pos)));
QPointF newPos(xpos, ypos);
// do not allow it to move between handlers.
if (handles.count() > 1) {
if (idx == 0 ) { // first
if (newPos.x() < handles[1]->x()) {
activeDraggedHandler->setPos(newPos);
lastValidPos = newPos;
}
} else if (idx == handles.count()-1) { // last
if (newPos.x() > handles[idx-1]->x()) {
activeDraggedHandler->setPos(newPos);
lastValidPos = newPos;
}
} else { // middle
if (newPos.x() > handles[idx-1]->x() && newPos.x() < handles[idx+1]->x()) {
activeDraggedHandler->setPos(newPos);
lastValidPos = newPos;
}
}
} else {
activeDraggedHandler->setPos(newPos);
lastValidPos = newPos;
}
activeDraggedHandler->setPos(QPointF(xpos, ypos));
qDeleteAll(lines);
lines.clear();
}
@ -393,6 +368,7 @@ void DivePlannerGraphics::mousePressEvent(QMouseEvent* event)
if (DiveHandler *h = qgraphicsitem_cast<DiveHandler*>(item)) {
activeDraggedHandler = h;
activeDraggedHandler->setBrush(Qt::red);
originalHandlerPos = activeDraggedHandler->pos();
}
}
QGraphicsView::mousePressEvent(event);
@ -401,9 +377,26 @@ void DivePlannerGraphics::mousePressEvent(QMouseEvent* event)
void DivePlannerGraphics::mouseReleaseEvent(QMouseEvent* event)
{
if (activeDraggedHandler) {
activeDraggedHandler->sec = rint(timeLine->valueAt(lastValidPos)) * 60;
activeDraggedHandler->mm = rint(depthLine->valueAt(lastValidPos)) * 1000;
QPointF mappedPos = mapToScene(event->pos());
int minutes = rint(timeLine->valueAt(mappedPos));
int meters = rint(depthLine->valueAt(mappedPos));
double xpos = timeLine->posAtValue(minutes);
double ypos = depthLine->posAtValue(meters);
Q_FOREACH(DiveHandler* handler, handles){
if (xpos == handler->pos().x() && handler != activeDraggedHandler){
qDebug() << "There's already an point at that place.";
//TODO: Move this later to a KMessageWidget.
activeDraggedHandler->setPos(originalHandlerPos);
activeDraggedHandler = NULL;
return;
}
}
activeDraggedHandler->sec = rint(timeLine->valueAt(mappedPos)) * 60;
activeDraggedHandler->mm = rint(depthLine->valueAt(mappedPos)) * 1000;
activeDraggedHandler->setBrush(QBrush(Qt::white));
activeDraggedHandler->setPos(QPointF(xpos, ypos));
createDecoStops();
activeDraggedHandler = 0;
}

View file

@ -73,7 +73,6 @@ protected:
bool isPointOutOfBoundaries(const QPointF& point);
void deleteTemporaryDivePlan(struct divedatapoint* dp);
qreal fromPercent(qreal percent, Qt::Orientation orientation);
private slots:
void increaseTime();
void increaseDepth();
@ -96,8 +95,9 @@ private:
/* This is the handler that's being dragged. */
DiveHandler *activeDraggedHandler;
// helper to save the positions where the drag-handler is valid.
QPointF lastValidPos;
// When we start to move the handler, this pos is saved.
// so we can revert it later.
QPointF originalHandlerPos;
/* this is the background of the dive, the blue-gradient. */
QGraphicsPolygonItem *diveBg;