2013-06-20 15:33:26 +00:00
|
|
|
#include "diveplanner.h"
|
2013-06-20 15:37:41 +00:00
|
|
|
#include <QMouseEvent>
|
2013-06-20 16:39:41 +00:00
|
|
|
#include <QDebug>
|
2013-06-20 15:33:26 +00:00
|
|
|
|
|
|
|
DivePlanner* DivePlanner::instance()
|
|
|
|
{
|
|
|
|
static DivePlanner *self = new DivePlanner();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
DivePlanner::DivePlanner(QWidget* parent): QGraphicsView(parent)
|
|
|
|
{
|
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 15:33:26 +00:00
|
|
|
}
|
2013-06-20 15:37:41 +00:00
|
|
|
|
|
|
|
void DivePlanner::mouseDoubleClickEvent(QMouseEvent* event)
|
|
|
|
{
|
2013-06-20 16:56:28 +00:00
|
|
|
QPointF mappedPos = mapToScene(event->pos());
|
|
|
|
if(isPointOutOfBoundaries(mappedPos))
|
|
|
|
return;
|
|
|
|
|
2013-06-20 16:28:04 +00:00
|
|
|
QGraphicsEllipseItem *item = new QGraphicsEllipseItem(-5,-5,10,10);
|
|
|
|
item->setFlag(QGraphicsItem::ItemIgnoresTransformations);
|
2013-06-20 16:56:28 +00:00
|
|
|
|
2013-06-20 16:20:41 +00:00
|
|
|
|
|
|
|
item->setPos( mappedPos );
|
2013-06-20 15:37:41 +00:00
|
|
|
scene()->addItem(item);
|
2013-06-20 16:20:41 +00:00
|
|
|
handles << item;
|
|
|
|
|
|
|
|
if (lines.empty()){
|
|
|
|
QGraphicsLineItem *first = new QGraphicsLineItem(0,0, mappedPos.x(), mappedPos.y());
|
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 16:39:41 +00:00
|
|
|
QGraphicsEllipseItem *prevHandle = handles.at( handles.count()-2);
|
|
|
|
QGraphicsLineItem *line = new QGraphicsLineItem(prevHandle->x(), prevHandle->y(), item->x(), item->y());
|
|
|
|
lines.push_back(line);
|
|
|
|
scene()->addItem(line);
|
2013-06-20 16:20:41 +00:00
|
|
|
create_deco_stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlanner::clear_generated_deco()
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlanner::create_deco_stop()
|
|
|
|
{
|
|
|
|
// this needs to create everything
|
|
|
|
// for the calculated deco.
|
|
|
|
QGraphicsLineItem *item = new QGraphicsLineItem(handles.last()->x(), handles.last()->y(), 100, 0);
|
|
|
|
scene()->addItem(item);
|
|
|
|
lines << item;
|
2013-06-20 15:37:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-20 16:28:04 +00:00
|
|
|
void DivePlanner::resizeEvent(QResizeEvent* event)
|
|
|
|
{
|
|
|
|
QGraphicsView::resizeEvent(event);
|
|
|
|
fitInView(sceneRect(), Qt::KeepAspectRatio);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlanner::showEvent(QShowEvent* event)
|
|
|
|
{
|
|
|
|
QGraphicsView::showEvent(event);
|
|
|
|
fitInView(sceneRect(), Qt::KeepAspectRatio);
|
|
|
|
}
|
|
|
|
|
2013-06-20 16:53:12 +00:00
|
|
|
void DivePlanner::mouseMoveEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
verticalLine->setLine(mappedPos.x(), 0, mappedPos.x(), 100);
|
|
|
|
horizontalLine->setLine(0, mappedPos.y(), 100, mappedPos.y());
|
|
|
|
}
|
2013-06-20 16:56:28 +00:00
|
|
|
|
|
|
|
bool DivePlanner::isPointOutOfBoundaries(QPointF point)
|
|
|
|
{
|
|
|
|
if (point.x() > sceneRect().width()
|
|
|
|
|| point.x() < 0
|
|
|
|
|| point.y() < 0
|
|
|
|
|| point.y() > sceneRect().height())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|