2013-06-20 15:33:26 +00:00
|
|
|
#include "diveplanner.h"
|
2013-06-21 19:28:17 +00:00
|
|
|
#include <cmath>
|
2013-06-20 15:37:41 +00:00
|
|
|
#include <QMouseEvent>
|
2013-06-20 16:39:41 +00:00
|
|
|
#include <QDebug>
|
2013-06-20 21:40:59 +00:00
|
|
|
#include "ui_diveplanner.h"
|
2013-06-20 15:33:26 +00:00
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0)
|
2013-06-20 15:33:26 +00:00
|
|
|
{
|
2013-06-20 16:53:12 +00:00
|
|
|
setMouseTracking(true);
|
2013-06-20 15:37:41 +00:00
|
|
|
setScene( new QGraphicsScene());
|
|
|
|
scene()->setSceneRect(0,0,100,100);
|
2013-06-20 16:53:12 +00:00
|
|
|
|
|
|
|
verticalLine = new QGraphicsLineItem(0,0,0, 100);
|
|
|
|
verticalLine->setPen(QPen(Qt::DotLine));
|
|
|
|
scene()->addItem(verticalLine);
|
|
|
|
|
|
|
|
horizontalLine = new QGraphicsLineItem(0,0,100,0);
|
|
|
|
horizontalLine->setPen(QPen(Qt::DotLine));
|
|
|
|
scene()->addItem(horizontalLine);
|
2013-06-20 19:48:24 +00:00
|
|
|
|
|
|
|
timeLine = new Ruler();
|
|
|
|
timeLine->setMinimum(0);
|
2013-06-21 19:28:17 +00:00
|
|
|
timeLine->setMaximum(20);
|
2013-06-20 19:48:24 +00:00
|
|
|
timeLine->setTickInterval(10);
|
|
|
|
timeLine->setLine( 10, 90, 99, 90);
|
|
|
|
timeLine->setOrientation(Qt::Horizontal);
|
|
|
|
timeLine->updateTicks();
|
|
|
|
scene()->addItem(timeLine);
|
|
|
|
|
|
|
|
depthLine = new Ruler();
|
|
|
|
depthLine->setMinimum(0);
|
2013-06-21 19:28:17 +00:00
|
|
|
depthLine->setMaximum(10);
|
2013-06-20 19:48:24 +00:00
|
|
|
depthLine->setTickInterval(10);
|
|
|
|
depthLine->setLine( 10, 1, 10, 90);
|
|
|
|
depthLine->setOrientation(Qt::Vertical);
|
|
|
|
depthLine->updateTicks();
|
|
|
|
scene()->addItem(depthLine);
|
2013-06-20 20:54:36 +00:00
|
|
|
|
|
|
|
timeString = new QGraphicsSimpleTextItem();
|
|
|
|
timeString->setFlag(QGraphicsItem::ItemIgnoresTransformations);
|
|
|
|
scene()->addItem(timeString);
|
|
|
|
|
|
|
|
depthString = new QGraphicsSimpleTextItem();
|
|
|
|
depthString->setFlag(QGraphicsItem::ItemIgnoresTransformations);
|
|
|
|
scene()->addItem(depthString);
|
2013-06-20 15:33:26 +00:00
|
|
|
}
|
2013-06-20 15:37:41 +00:00
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::mouseDoubleClickEvent(QMouseEvent* event)
|
2013-06-20 15:37:41 +00:00
|
|
|
{
|
2013-06-20 16:56:28 +00:00
|
|
|
QPointF mappedPos = mapToScene(event->pos());
|
|
|
|
if(isPointOutOfBoundaries(mappedPos))
|
|
|
|
return;
|
|
|
|
|
2013-06-20 17:20:45 +00:00
|
|
|
if(handles.count() && handles.last()->x() > mappedPos.x()){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-20 17:29:32 +00:00
|
|
|
DiveHandler *item = new DiveHandler ();
|
|
|
|
item->setRect(-5,-5,10,10);
|
2013-06-20 16:28:04 +00:00
|
|
|
item->setFlag(QGraphicsItem::ItemIgnoresTransformations);
|
2013-06-21 19:28:17 +00:00
|
|
|
|
|
|
|
double xpos = timeLine->posAtValue(rint(timeLine->valueAt(mappedPos)));
|
|
|
|
double ypos = depthLine->posAtValue(rint(depthLine->valueAt(mappedPos)));
|
|
|
|
item->setPos( QPointF(xpos, ypos));
|
2013-06-20 15:37:41 +00:00
|
|
|
scene()->addItem(item);
|
2013-06-20 16:20:41 +00:00
|
|
|
handles << item;
|
|
|
|
|
|
|
|
if (lines.empty()){
|
2013-06-21 19:12:04 +00:00
|
|
|
double xpos = timeLine->posAtValue(0);
|
|
|
|
double ypos = depthLine->posAtValue(0);
|
|
|
|
QGraphicsLineItem *first = new QGraphicsLineItem(xpos,ypos, mappedPos.x(), mappedPos.y());
|
2013-06-20 17:29:32 +00:00
|
|
|
item->from = first;
|
2013-06-20 16:39:41 +00:00
|
|
|
lines.push_back(first);
|
2013-06-20 16:20:41 +00:00
|
|
|
create_deco_stop();
|
|
|
|
scene()->addItem(first);
|
|
|
|
}else{
|
|
|
|
clear_generated_deco();
|
2013-06-20 17:29:32 +00:00
|
|
|
DiveHandler *prevHandle = handles.at( handles.count()-2);
|
2013-06-20 16:39:41 +00:00
|
|
|
QGraphicsLineItem *line = new QGraphicsLineItem(prevHandle->x(), prevHandle->y(), item->x(), item->y());
|
2013-06-20 17:29:32 +00:00
|
|
|
prevHandle->to = line;
|
|
|
|
item->from = line;
|
2013-06-20 16:39:41 +00:00
|
|
|
lines.push_back(line);
|
|
|
|
scene()->addItem(line);
|
2013-06-20 16:20:41 +00:00
|
|
|
create_deco_stop();
|
|
|
|
}
|
2013-06-20 21:25:03 +00:00
|
|
|
item->time = (timeLine->valueAt(mappedPos));
|
|
|
|
item->depth = (depthLine->valueAt(mappedPos));
|
2013-06-20 16:20:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::clear_generated_deco()
|
2013-06-20 16:20:41 +00:00
|
|
|
{
|
2013-06-20 16:39:41 +00:00
|
|
|
for(int i = handles.count(); i <= lines.count(); i++){
|
2013-06-20 16:20:41 +00:00
|
|
|
scene()->removeItem(lines.last());
|
|
|
|
delete lines.last();
|
|
|
|
lines.removeLast();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::create_deco_stop()
|
2013-06-20 16:20:41 +00:00
|
|
|
{
|
2013-06-20 21:25:03 +00:00
|
|
|
// This needs to be done in the following steps:
|
|
|
|
// Get the user-input and calculate the dive info
|
|
|
|
Q_FOREACH(DiveHandler *h, handles){
|
|
|
|
// use this somewhere.
|
|
|
|
h->time;
|
|
|
|
h->depth;
|
|
|
|
}
|
|
|
|
// create the dive info here.
|
|
|
|
|
|
|
|
// set the new 'end time' of the dive.
|
|
|
|
// note that this is not the user end,
|
|
|
|
// but the real end of the dive.
|
|
|
|
timeLine->setMaximum(60);
|
|
|
|
timeLine->updateTicks();
|
|
|
|
|
|
|
|
// Re-position the user generated dive handlers
|
|
|
|
Q_FOREACH(DiveHandler *h, handles){
|
|
|
|
// uncomment this as soon as the posAtValue is implemented.
|
|
|
|
// h->setPos( timeLine->posAtValue(h->time),
|
|
|
|
// depthLine->posAtValue(h->depth));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create all 'deco' GraphicsLineItems and put it on the canvas.This following three lines will
|
|
|
|
// most probably need to enter on a loop.
|
2013-06-21 19:12:04 +00:00
|
|
|
double xpos = timeLine->posAtValue(timeLine->maximum());
|
|
|
|
double ypos = depthLine->posAtValue(depthLine->minimum());
|
|
|
|
QGraphicsLineItem *item = new QGraphicsLineItem(handles.last()->x(), handles.last()->y(), xpos, ypos);
|
2013-06-20 16:20:41 +00:00
|
|
|
scene()->addItem(item);
|
|
|
|
lines << item;
|
2013-06-20 15:37:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::resizeEvent(QResizeEvent* event)
|
2013-06-20 16:28:04 +00:00
|
|
|
{
|
|
|
|
QGraphicsView::resizeEvent(event);
|
|
|
|
fitInView(sceneRect(), Qt::KeepAspectRatio);
|
|
|
|
}
|
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::showEvent(QShowEvent* event)
|
2013-06-20 16:28:04 +00:00
|
|
|
{
|
|
|
|
QGraphicsView::showEvent(event);
|
|
|
|
fitInView(sceneRect(), Qt::KeepAspectRatio);
|
|
|
|
}
|
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::mouseMoveEvent(QMouseEvent* event)
|
2013-06-20 16:53:12 +00:00
|
|
|
{
|
|
|
|
QPointF mappedPos = mapToScene(event->pos());
|
2013-06-20 16:56:28 +00:00
|
|
|
if (isPointOutOfBoundaries(mappedPos))
|
2013-06-20 16:53:12 +00:00
|
|
|
return;
|
2013-06-20 17:20:45 +00:00
|
|
|
|
2013-06-20 16:53:12 +00:00
|
|
|
verticalLine->setLine(mappedPos.x(), 0, mappedPos.x(), 100);
|
|
|
|
horizontalLine->setLine(0, mappedPos.y(), 100, mappedPos.y());
|
2013-06-21 19:14:22 +00:00
|
|
|
depthString->setText(QString::number( (int) depthLine->valueAt(mappedPos)) + "m" );
|
2013-06-20 20:54:36 +00:00
|
|
|
depthString->setPos(0, mappedPos.y());
|
|
|
|
timeString->setText(QString::number( (int) timeLine->valueAt(mappedPos)) + "min");
|
|
|
|
timeString->setPos(mappedPos.x()+1, 90);
|
2013-06-20 17:20:45 +00:00
|
|
|
|
2013-06-20 18:15:10 +00:00
|
|
|
if(activeDraggedHandler)
|
|
|
|
moveActiveHandler(mappedPos);
|
2013-06-20 17:20:45 +00:00
|
|
|
if (!handles.count())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (handles.last()->x() > mappedPos.x()){
|
|
|
|
verticalLine->setPen( QPen(QBrush(Qt::red), 0, Qt::SolidLine));
|
|
|
|
horizontalLine->setPen( QPen(QBrush(Qt::red), 0, Qt::SolidLine));
|
|
|
|
}else{
|
|
|
|
verticalLine->setPen(QPen(Qt::DotLine));
|
|
|
|
horizontalLine->setPen(QPen(Qt::DotLine));
|
|
|
|
}
|
2013-06-20 16:53:12 +00:00
|
|
|
}
|
2013-06-20 16:56:28 +00:00
|
|
|
|
2013-06-21 19:28:17 +00:00
|
|
|
void DivePlannerGraphics::moveActiveHandler(const QPointF& pos)
|
2013-06-20 18:15:10 +00:00
|
|
|
{
|
|
|
|
int idx = handles.indexOf(activeDraggedHandler);
|
2013-06-21 19:28:17 +00:00
|
|
|
|
|
|
|
double xpos = timeLine->posAtValue(rint(timeLine->valueAt(pos)));
|
|
|
|
double ypos = depthLine->posAtValue(rint(depthLine->valueAt(pos)));
|
|
|
|
|
|
|
|
QPointF newPos(xpos, ypos);
|
2013-06-20 18:15:10 +00:00
|
|
|
bool moveLines = false;;
|
|
|
|
// do not allow it to move between handlers.
|
|
|
|
if (handles.count() > 1){
|
|
|
|
if (idx == 0 ){ // first
|
2013-06-21 19:28:17 +00:00
|
|
|
if (newPos.x() < handles[1]->x()){
|
|
|
|
activeDraggedHandler->setPos(newPos);
|
2013-06-20 18:15:10 +00:00
|
|
|
moveLines = true;
|
|
|
|
}
|
|
|
|
}else if (idx == handles.count()-1){ // last
|
2013-06-21 19:28:17 +00:00
|
|
|
if (newPos.x() > handles[idx-1]->x()){
|
|
|
|
activeDraggedHandler->setPos(newPos);
|
2013-06-20 18:15:10 +00:00
|
|
|
moveLines = true;
|
|
|
|
}
|
|
|
|
}else{ // middle
|
2013-06-21 19:28:17 +00:00
|
|
|
if (newPos.x() > handles[idx-1]->x() && newPos.x() < handles[idx+1]->x()){
|
|
|
|
activeDraggedHandler->setPos(newPos);
|
2013-06-20 18:15:10 +00:00
|
|
|
moveLines = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
2013-06-21 19:28:17 +00:00
|
|
|
activeDraggedHandler->setPos(newPos);
|
2013-06-20 18:15:10 +00:00
|
|
|
moveLines = true;
|
|
|
|
}
|
|
|
|
if (moveLines){
|
|
|
|
if (activeDraggedHandler->from){
|
|
|
|
QLineF f = activeDraggedHandler->from->line();
|
2013-06-21 19:28:17 +00:00
|
|
|
activeDraggedHandler->from->setLine(f.x1(), f.y1(), newPos.x(), newPos.y());
|
2013-06-20 18:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (activeDraggedHandler->to){
|
|
|
|
QLineF f = activeDraggedHandler->to->line();
|
2013-06-21 19:28:17 +00:00
|
|
|
activeDraggedHandler->to->setLine(newPos.x(), newPos.y(), f.x2(), f.y2());
|
2013-06-20 18:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(activeDraggedHandler == handles.last()){
|
|
|
|
clear_generated_deco();
|
|
|
|
create_deco_stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 19:28:17 +00:00
|
|
|
bool DivePlannerGraphics::isPointOutOfBoundaries(const QPointF& point)
|
2013-06-20 16:56:28 +00:00
|
|
|
{
|
2013-06-21 19:07:44 +00:00
|
|
|
double xpos = timeLine->valueAt(point);
|
|
|
|
double ypos = depthLine->valueAt(point);
|
|
|
|
|
|
|
|
if (xpos > timeLine->maximum()
|
|
|
|
|| xpos < timeLine->minimum()
|
|
|
|
|| ypos > depthLine->maximum()
|
|
|
|
|| ypos < depthLine->minimum())
|
2013-06-20 16:56:28 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-20 17:29:32 +00:00
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::mousePressEvent(QMouseEvent* event)
|
2013-06-20 17:46:40 +00:00
|
|
|
{
|
|
|
|
QPointF mappedPos = mapToScene(event->pos());
|
|
|
|
Q_FOREACH(QGraphicsItem *item, scene()->items(mappedPos)){
|
|
|
|
if (DiveHandler *h = qgraphicsitem_cast<DiveHandler*>(item)){
|
|
|
|
activeDraggedHandler = h;
|
|
|
|
activeDraggedHandler->setBrush(Qt::red);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-20 17:29:32 +00:00
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::mouseReleaseEvent(QMouseEvent* event)
|
2013-06-20 17:46:40 +00:00
|
|
|
{
|
2013-06-20 20:34:42 +00:00
|
|
|
if (activeDraggedHandler){
|
|
|
|
QPointF mappedPos = mapToScene(event->pos());
|
2013-06-20 21:25:03 +00:00
|
|
|
activeDraggedHandler->time = (timeLine->valueAt(mappedPos));
|
|
|
|
activeDraggedHandler->depth = (depthLine->valueAt(mappedPos));
|
2013-06-20 21:02:01 +00:00
|
|
|
activeDraggedHandler->setBrush(QBrush());
|
2013-06-20 17:46:40 +00:00
|
|
|
activeDraggedHandler = 0;
|
2013-06-20 20:34:42 +00:00
|
|
|
}
|
2013-06-20 17:46:40 +00:00
|
|
|
}
|
2013-06-20 17:29:32 +00:00
|
|
|
|
|
|
|
DiveHandler::DiveHandler(): QGraphicsEllipseItem(), from(0), to(0)
|
|
|
|
{
|
|
|
|
}
|
2013-06-20 18:52:27 +00:00
|
|
|
|
|
|
|
void Ruler::setMaximum(double maximum)
|
|
|
|
{
|
|
|
|
max = maximum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ruler::setMinimum(double minimum)
|
|
|
|
{
|
|
|
|
min = minimum;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ruler::Ruler() : orientation(Qt::Horizontal)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ruler::setOrientation(Qt::Orientation o)
|
|
|
|
{
|
|
|
|
orientation = o;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ruler::updateTicks()
|
|
|
|
{
|
2013-06-20 19:48:24 +00:00
|
|
|
qDeleteAll(ticks);
|
|
|
|
QLineF m = line();
|
|
|
|
if(orientation == Qt::Horizontal){
|
|
|
|
double steps = (max - min) / interval;
|
|
|
|
double stepSize = (m.x2() - m.x1()) / steps;
|
|
|
|
for(qreal pos = m.x1(); pos < m.x2(); pos += stepSize){
|
|
|
|
QGraphicsLineItem *l = new QGraphicsLineItem(pos, m.y1(), pos, m.y1() + 1, this);
|
2013-06-20 18:52:27 +00:00
|
|
|
|
2013-06-20 19:48:24 +00:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
double steps = (max - min) / interval;
|
|
|
|
double stepSize = (m.y2() - m.y1()) / steps;
|
|
|
|
for(qreal pos = m.y1(); pos < m.y2(); pos += stepSize){
|
|
|
|
QGraphicsLineItem *l = new QGraphicsLineItem(m.x1(), pos, m.x1() - 1, pos, this);
|
|
|
|
}
|
|
|
|
}
|
2013-06-20 18:52:27 +00:00
|
|
|
}
|
|
|
|
|
2013-06-20 19:48:24 +00:00
|
|
|
void Ruler::setTickInterval(double i)
|
2013-06-20 18:52:27 +00:00
|
|
|
{
|
2013-06-20 19:48:24 +00:00
|
|
|
interval = i;
|
2013-06-20 18:52:27 +00:00
|
|
|
}
|
2013-06-20 20:34:42 +00:00
|
|
|
|
|
|
|
qreal Ruler::valueAt(const QPointF& p)
|
|
|
|
{
|
|
|
|
QLineF m = line();
|
2013-06-21 18:53:20 +00:00
|
|
|
double retValue = orientation == Qt::Horizontal
|
2013-06-20 20:34:42 +00:00
|
|
|
? max * (p.x() - m.x1()) / (m.x2() - m.x1())
|
|
|
|
: max * (p.y() - m.y1()) / (m.y2() - m.y1());
|
2013-06-21 18:53:20 +00:00
|
|
|
return retValue;
|
2013-06-20 20:34:42 +00:00
|
|
|
}
|
2013-06-20 21:25:03 +00:00
|
|
|
|
|
|
|
qreal Ruler::posAtValue(qreal value)
|
|
|
|
{
|
|
|
|
QLineF m = line();
|
2013-06-21 18:53:20 +00:00
|
|
|
double size = max - min;
|
|
|
|
double percent = value / size;
|
|
|
|
double realSize = orientation == Qt::Horizontal
|
|
|
|
? m.x2() - m.x1()
|
|
|
|
: m.y2() - m.y1();
|
|
|
|
double retValue = realSize * percent;
|
|
|
|
retValue = (orientation == Qt::Horizontal)
|
|
|
|
? retValue + m.x1()
|
|
|
|
: retValue + m.y1();
|
|
|
|
return retValue;
|
2013-06-20 21:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DivePlanner::DivePlanner() : ui(new Ui::DivePlanner())
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dive* DivePlanner::getDive()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-20 21:25:03 +00:00
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
DivePlanner* DivePlanner::instance()
|
|
|
|
{
|
|
|
|
static DivePlanner *self = new DivePlanner();
|
|
|
|
return self;
|
2013-06-20 21:25:03 +00:00
|
|
|
}
|
2013-06-21 19:07:44 +00:00
|
|
|
|
|
|
|
double Ruler::maximum() const
|
|
|
|
{
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Ruler::minimum() const
|
|
|
|
{
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|