summaryrefslogtreecommitdiffstats
path: root/qt-ui/diveplanner.cpp
blob: d64eab18f36100e4449df90960ab82875234ec52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "diveplanner.h"
#include <QMouseEvent>
#include <QDebug>

DivePlanner* DivePlanner::instance()
{
	static DivePlanner *self = new DivePlanner();
	return self;
}

DivePlanner::DivePlanner(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0)
{
	setMouseTracking(true);
	setScene( new QGraphicsScene());
	scene()->setSceneRect(0,0,100,100);

	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);
}

void DivePlanner::mouseDoubleClickEvent(QMouseEvent* event)
{
	QPointF mappedPos = mapToScene(event->pos());
	if(isPointOutOfBoundaries(mappedPos))
		return;

	if(handles.count() && handles.last()->x() > mappedPos.x()){
		return;
	}

	DiveHandler  *item = new DiveHandler ();
	item->setRect(-5,-5,10,10);
	item->setFlag(QGraphicsItem::ItemIgnoresTransformations);
	item->setPos( mappedPos );
    scene()->addItem(item);
	handles << item;

	if (lines.empty()){
		QGraphicsLineItem *first = new QGraphicsLineItem(0,0, mappedPos.x(), mappedPos.y());
		item->from = first;
		lines.push_back(first);
		create_deco_stop();
		scene()->addItem(first);
	}else{
		clear_generated_deco();
		DiveHandler *prevHandle = handles.at( handles.count()-2);
		QGraphicsLineItem *line = new QGraphicsLineItem(prevHandle->x(), prevHandle->y(), item->x(), item->y());
		prevHandle->to = line;
		item->from = line;
		lines.push_back(line);
		scene()->addItem(line);
		create_deco_stop();
	}
}

void DivePlanner::clear_generated_deco()
{
	for(int i = handles.count(); i <= lines.count(); i++){
		scene()->removeItem(lines.last());
		delete lines.last();
		lines.removeLast();
	}
}

void DivePlanner::create_deco_stop()
{
	// this needs to create everything
	// for the calculated deco. it should return the *first*
	// line that's calculated, so the
	QGraphicsLineItem *item = new QGraphicsLineItem(handles.last()->x(), handles.last()->y(), 100, 0);
	scene()->addItem(item);
	lines << item;
}

void DivePlanner::resizeEvent(QResizeEvent* event)
{
    QGraphicsView::resizeEvent(event);
	fitInView(sceneRect(), Qt::KeepAspectRatio);
}

void DivePlanner::showEvent(QShowEvent* event)
{
    QGraphicsView::showEvent(event);
	fitInView(sceneRect(), Qt::KeepAspectRatio);
}

void DivePlanner::mouseMoveEvent(QMouseEvent* event)
{
	QPointF mappedPos = mapToScene(event->pos());
	if (isPointOutOfBoundaries(mappedPos))
		return;

	verticalLine->setLine(mappedPos.x(), 0, mappedPos.x(), 100);
	horizontalLine->setLine(0, mappedPos.y(), 100, mappedPos.y());

	if (activeDraggedHandler){
		int idx = handles.indexOf(activeDraggedHandler);
		activeDraggedHandler->setPos(mappedPos);
		if (activeDraggedHandler->from){
			QLineF f = activeDraggedHandler->from->line();
			activeDraggedHandler->from->setLine(f.x1(), f.y1(), mappedPos.x(), mappedPos.y());
		}

		if(activeDraggedHandler == handles.last()){
			clear_generated_deco();
			create_deco_stop();
		}

		if (activeDraggedHandler->to){
			QLineF f = activeDraggedHandler->to->line();
			activeDraggedHandler->to->setLine(mappedPos.x(), mappedPos.y(), f.x2(), f.y2());
		}
	}

	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));
	}
}

bool DivePlanner::isPointOutOfBoundaries(QPointF point)
{
	if (point.x() > sceneRect().width()
	||  point.x() < 0
	||  point.y() < 0
	|| point.y() > sceneRect().height())
	{
		return true;
	}
	return false;
}

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);
		}
	}
}

void DivePlanner::mouseReleaseEvent(QMouseEvent* event)
{
	if (activeDraggedHandler)
		activeDraggedHandler = 0;
}

DiveHandler::DiveHandler(): QGraphicsEllipseItem(), from(0), to(0)
{
}