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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 17:46:40 +00:00
|
|
|
DivePlanner::DivePlanner(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 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 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-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 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2013-06-20 17:29:32 +00:00
|
|
|
// for the calculated deco. it should return the *first*
|
|
|
|
// line that's calculated, so the
|
2013-06-20 16:20:41 +00:00
|
|
|
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;
|
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-20 17:20:45 +00:00
|
|
|
|
2013-06-20 18:15:10 +00:00
|
|
|
if(activeDraggedHandler)
|
|
|
|
moveActiveHandler(mappedPos);
|
2013-06-20 17:46:40 +00:00
|
|
|
|
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-20 18:15:10 +00:00
|
|
|
void DivePlanner::moveActiveHandler(QPointF pos)
|
|
|
|
{
|
|
|
|
int idx = handles.indexOf(activeDraggedHandler);
|
|
|
|
bool moveLines = false;;
|
|
|
|
// do not allow it to move between handlers.
|
|
|
|
if (handles.count() > 1){
|
|
|
|
if (idx == 0 ){ // first
|
|
|
|
if (pos.x() < handles[1]->x()){
|
|
|
|
activeDraggedHandler->setPos(pos);
|
|
|
|
moveLines = true;
|
|
|
|
}
|
|
|
|
}else if (idx == handles.count()-1){ // last
|
|
|
|
if (pos.x() > handles[idx-1]->x()){
|
|
|
|
activeDraggedHandler->setPos(pos);
|
|
|
|
moveLines = true;
|
|
|
|
}
|
|
|
|
}else{ // middle
|
|
|
|
if (pos.x() > handles[idx-1]->x() && pos.x() < handles[idx+1]->x()){
|
|
|
|
activeDraggedHandler->setPos(pos);
|
|
|
|
moveLines = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
activeDraggedHandler->setPos(pos);
|
|
|
|
moveLines = true;
|
|
|
|
}
|
|
|
|
if (moveLines){
|
|
|
|
if (activeDraggedHandler->from){
|
|
|
|
QLineF f = activeDraggedHandler->from->line();
|
|
|
|
activeDraggedHandler->from->setLine(f.x1(), f.y1(), pos.x(), pos.y());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (activeDraggedHandler->to){
|
|
|
|
QLineF f = activeDraggedHandler->to->line();
|
|
|
|
activeDraggedHandler->to->setLine(pos.x(), pos.y(), f.x2(), f.y2());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(activeDraggedHandler == handles.last()){
|
|
|
|
clear_generated_deco();
|
|
|
|
create_deco_stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2013-06-20 17:29:32 +00:00
|
|
|
|
2013-06-20 17:46:40 +00:00
|
|
|
void DivePlanner::mousePressEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
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 17:46:40 +00:00
|
|
|
void DivePlanner::mouseReleaseEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
if (activeDraggedHandler)
|
|
|
|
activeDraggedHandler = 0;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
qDeleteAll(ticks);
|
|
|
|
max = maximum;
|
|
|
|
updateTicks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ruler::setMinimum(double minimum)
|
|
|
|
{
|
|
|
|
qDeleteAll(ticks);
|
|
|
|
min = minimum;
|
|
|
|
updateTicks();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ruler::Ruler() : orientation(Qt::Horizontal)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ruler::setOrientation(Qt::Orientation o)
|
|
|
|
{
|
|
|
|
orientation = o;
|
|
|
|
updateTicks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ruler::updateTicks()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ruler::setLine(qreal x1, qreal y1, qreal x2, qreal y2)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ruler::setTickInterval(double interval)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|