From 20ec98f2a5e31a91cc779666fa72ac2961bc87df Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Thu, 4 Jul 2013 10:29:28 -0300 Subject: 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 --- qt-ui/diveplanner.cpp | 53 ++++++++++++++++++++++----------------------------- qt-ui/diveplanner.h | 6 +++--- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/qt-ui/diveplanner.cpp b/qt-ui/diveplanner.cpp index d4c6e9561..5de5ffe6b 100644 --- a/qt-ui/diveplanner.cpp +++ b/qt-ui/diveplanner.cpp @@ -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(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; } diff --git a/qt-ui/diveplanner.h b/qt-ui/diveplanner.h index e9dfd91e9..547e38727 100644 --- a/qt-ui/diveplanner.h +++ b/qt-ui/diveplanner.h @@ -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; -- cgit v1.2.3-70-g09d2