2013-06-20 15:33:26 +00:00
|
|
|
#include "diveplanner.h"
|
2013-07-02 13:13:06 +00:00
|
|
|
#include "graphicsview-common.h"
|
|
|
|
|
2013-06-24 01:04:35 +00:00
|
|
|
#include "../dive.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-27 19:45:58 +00:00
|
|
|
#include <QGraphicsWidget>
|
|
|
|
#include <QGraphicsProxyWidget>
|
|
|
|
#include <QPushButton>
|
2013-07-02 17:36:52 +00:00
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2013-06-27 19:45:58 +00:00
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
#include "ui_diveplanner.h"
|
2013-06-27 19:45:58 +00:00
|
|
|
#include "mainwindow.h"
|
2013-06-20 15:33:26 +00:00
|
|
|
|
2013-06-27 14:10:03 +00:00
|
|
|
#define TIME_INITIAL_MAX 30
|
|
|
|
|
2013-07-02 17:14:09 +00:00
|
|
|
#define MAX_DEEPNESS 150
|
|
|
|
#define MIN_DEEPNESS 40
|
|
|
|
|
2013-07-04 13:06:52 +00:00
|
|
|
bool handlerLessThenMinutes(DiveHandler *d1, DiveHandler *d2){
|
|
|
|
return d1->sec < d2->sec;
|
|
|
|
}
|
|
|
|
|
2013-07-04 13:29:28 +00:00
|
|
|
DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0)
|
2013-06-20 15:33:26 +00:00
|
|
|
{
|
2013-07-02 13:13:06 +00:00
|
|
|
fill_profile_color();
|
|
|
|
setBackgroundBrush(profile_color[BACKGROUND].at(0));
|
2013-06-20 16:53:12 +00:00
|
|
|
setMouseTracking(true);
|
2013-06-23 20:09:29 +00:00
|
|
|
setScene(new QGraphicsScene());
|
2013-07-02 13:53:08 +00:00
|
|
|
scene()->setSceneRect(0,0,1920,1080);
|
|
|
|
|
|
|
|
verticalLine = new QGraphicsLineItem(
|
|
|
|
fromPercent(0, Qt::Horizontal),
|
|
|
|
fromPercent(0, Qt::Vertical),
|
|
|
|
fromPercent(0, Qt::Horizontal),
|
2013-07-02 14:12:15 +00:00
|
|
|
fromPercent(100, Qt::Vertical)
|
|
|
|
);
|
2013-06-20 16:53:12 +00:00
|
|
|
|
|
|
|
verticalLine->setPen(QPen(Qt::DotLine));
|
|
|
|
scene()->addItem(verticalLine);
|
|
|
|
|
2013-07-02 14:12:15 +00:00
|
|
|
horizontalLine = new QGraphicsLineItem(
|
|
|
|
fromPercent(0, Qt::Horizontal),
|
|
|
|
fromPercent(0, Qt::Vertical),
|
|
|
|
fromPercent(100, Qt::Horizontal),
|
|
|
|
fromPercent(0, Qt::Vertical)
|
|
|
|
);
|
2013-06-20 16:53:12 +00:00
|
|
|
horizontalLine->setPen(QPen(Qt::DotLine));
|
|
|
|
scene()->addItem(horizontalLine);
|
2013-06-20 19:48:24 +00:00
|
|
|
|
|
|
|
timeLine = new Ruler();
|
|
|
|
timeLine->setMinimum(0);
|
2013-06-27 14:10:03 +00:00
|
|
|
timeLine->setMaximum(TIME_INITIAL_MAX);
|
2013-06-20 19:48:24 +00:00
|
|
|
timeLine->setTickInterval(10);
|
2013-07-02 14:12:15 +00:00
|
|
|
timeLine->setLine(
|
|
|
|
fromPercent(10, Qt::Horizontal),
|
|
|
|
fromPercent(90, Qt::Vertical),
|
|
|
|
fromPercent(90, Qt::Horizontal),
|
|
|
|
fromPercent(90, Qt::Vertical)
|
|
|
|
);
|
2013-06-20 19:48:24 +00:00
|
|
|
timeLine->setOrientation(Qt::Horizontal);
|
2013-07-02 14:12:15 +00:00
|
|
|
timeLine->setTickSize(fromPercent(1, Qt::Vertical));
|
2013-07-02 15:01:47 +00:00
|
|
|
timeLine->setColor(profile_color[TIME_GRID].at(0));
|
2013-06-20 19:48:24 +00:00
|
|
|
timeLine->updateTicks();
|
|
|
|
scene()->addItem(timeLine);
|
|
|
|
|
|
|
|
depthLine = new Ruler();
|
|
|
|
depthLine->setMinimum(0);
|
2013-06-27 22:52:58 +00:00
|
|
|
depthLine->setMaximum(40);
|
2013-06-20 19:48:24 +00:00
|
|
|
depthLine->setTickInterval(10);
|
2013-07-02 14:12:15 +00:00
|
|
|
depthLine->setLine(
|
|
|
|
fromPercent(10, Qt::Horizontal),
|
|
|
|
fromPercent(10, Qt::Vertical),
|
|
|
|
fromPercent(10, Qt::Horizontal),
|
|
|
|
fromPercent(90, Qt::Vertical)
|
|
|
|
);
|
2013-06-20 19:48:24 +00:00
|
|
|
depthLine->setOrientation(Qt::Vertical);
|
2013-07-02 14:12:15 +00:00
|
|
|
depthLine->setTickSize(fromPercent(1, Qt::Horizontal));
|
2013-07-02 15:01:47 +00:00
|
|
|
depthLine->setColor(profile_color[DEPTH_GRID].at(0));
|
2013-06-20 19:48:24 +00:00
|
|
|
depthLine->updateTicks();
|
|
|
|
scene()->addItem(depthLine);
|
2013-06-20 20:54:36 +00:00
|
|
|
|
|
|
|
timeString = new QGraphicsSimpleTextItem();
|
|
|
|
timeString->setFlag(QGraphicsItem::ItemIgnoresTransformations);
|
2013-07-02 16:31:25 +00:00
|
|
|
timeString->setBrush(profile_color[TIME_TEXT].at(0));
|
2013-06-20 20:54:36 +00:00
|
|
|
scene()->addItem(timeString);
|
|
|
|
|
|
|
|
depthString = new QGraphicsSimpleTextItem();
|
|
|
|
depthString->setFlag(QGraphicsItem::ItemIgnoresTransformations);
|
2013-07-02 16:31:25 +00:00
|
|
|
depthString->setBrush(profile_color[SAMPLE_DEEP].at(0));
|
2013-06-20 20:54:36 +00:00
|
|
|
scene()->addItem(depthString);
|
2013-06-21 19:44:38 +00:00
|
|
|
|
2013-06-27 22:52:58 +00:00
|
|
|
diveBg = new QGraphicsPolygonItem();
|
2013-07-02 16:39:54 +00:00
|
|
|
diveBg->setPen(QPen(QBrush(),0));
|
2013-06-27 22:52:58 +00:00
|
|
|
scene()->addItem(diveBg);
|
|
|
|
|
2013-06-21 19:44:38 +00:00
|
|
|
plusDepth = new Button();
|
2013-06-27 19:45:58 +00:00
|
|
|
plusDepth->setPixmap(QPixmap(":plus"));
|
2013-07-02 17:14:09 +00:00
|
|
|
plusDepth->setPos(fromPercent(5, Qt::Horizontal), fromPercent(5, Qt::Vertical));
|
|
|
|
plusDepth->setToolTip("Increase maximum depth by 10m");
|
2013-06-21 19:44:38 +00:00
|
|
|
scene()->addItem(plusDepth);
|
2013-06-21 19:51:13 +00:00
|
|
|
connect(plusDepth, SIGNAL(clicked()), this, SLOT(increaseDepth()));
|
2013-06-21 19:44:38 +00:00
|
|
|
|
|
|
|
plusTime = new Button();
|
2013-06-27 19:45:58 +00:00
|
|
|
plusTime->setPixmap(QPixmap(":plus"));
|
2013-07-04 12:45:25 +00:00
|
|
|
plusTime->setPos(fromPercent(95, Qt::Horizontal), fromPercent(95, Qt::Vertical));
|
2013-07-02 17:14:09 +00:00
|
|
|
plusTime->setToolTip("Increase minimum dive time by 10m");
|
2013-06-21 19:44:38 +00:00
|
|
|
scene()->addItem(plusTime);
|
2013-06-21 19:51:13 +00:00
|
|
|
connect(plusTime, SIGNAL(clicked()), this, SLOT(increaseTime()));
|
2013-06-27 19:45:58 +00:00
|
|
|
|
|
|
|
okBtn = new Button();
|
|
|
|
okBtn->setText(tr("Ok"));
|
2013-07-04 12:45:25 +00:00
|
|
|
okBtn->setPos(fromPercent(1, Qt::Horizontal), fromPercent(95, Qt::Vertical));
|
2013-06-27 19:45:58 +00:00
|
|
|
scene()->addItem(okBtn);
|
|
|
|
connect(okBtn, SIGNAL(clicked()), this, SLOT(okClicked()));
|
|
|
|
|
|
|
|
cancelBtn = new Button();
|
|
|
|
cancelBtn->setText(tr("Cancel"));
|
2013-07-04 12:45:25 +00:00
|
|
|
cancelBtn->setPos(okBtn->pos().x() + okBtn->boundingRect().width() + fromPercent(2, Qt::Horizontal),
|
|
|
|
fromPercent(95, Qt::Vertical));
|
2013-06-27 19:45:58 +00:00
|
|
|
scene()->addItem(cancelBtn);
|
|
|
|
connect(cancelBtn, SIGNAL(clicked()), this, SLOT(cancelClicked()));
|
2013-06-27 22:52:58 +00:00
|
|
|
|
2013-07-02 17:36:52 +00:00
|
|
|
minMinutes = TIME_INITIAL_MAX;
|
2013-07-04 14:06:28 +00:00
|
|
|
|
2013-07-04 14:16:42 +00:00
|
|
|
QAction *action = NULL;
|
2013-07-04 14:06:28 +00:00
|
|
|
|
2013-07-04 14:16:42 +00:00
|
|
|
#define ADD_ACTION( SHORTCUT, Slot ) \
|
|
|
|
action = new QAction(this); \
|
|
|
|
action->setShortcut( SHORTCUT ); \
|
2013-07-04 15:30:05 +00:00
|
|
|
action->setShortcutContext(Qt::WindowShortcut); \
|
2013-07-04 14:16:42 +00:00
|
|
|
addAction(action); \
|
|
|
|
connect(action, SIGNAL(triggered(bool)), this, SLOT( Slot ))
|
|
|
|
|
|
|
|
ADD_ACTION(Qt::Key_Escape, keyEscAction());
|
|
|
|
ADD_ACTION(Qt::Key_Delete, keyDeleteAction());
|
2013-07-04 14:28:39 +00:00
|
|
|
ADD_ACTION(Qt::Key_Up, keyUpAction());
|
|
|
|
ADD_ACTION(Qt::Key_Down, keyDownAction());
|
2013-07-04 15:30:05 +00:00
|
|
|
ADD_ACTION(Qt::Key_Left, keyLeftAction());
|
|
|
|
ADD_ACTION(Qt::Key_Right, keyRightAction());
|
2013-07-04 14:16:42 +00:00
|
|
|
#undef ADD_ACTION
|
2013-07-04 14:06:28 +00:00
|
|
|
|
2013-06-27 22:52:58 +00:00
|
|
|
setRenderHint(QPainter::Antialiasing);
|
2013-06-27 19:45:58 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 14:28:39 +00:00
|
|
|
void DivePlannerGraphics::keyDownAction()
|
|
|
|
{
|
|
|
|
if(scene()->selectedItems().count()){
|
|
|
|
Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()){
|
|
|
|
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)){
|
|
|
|
if (handler->mm / 1000 >= depthLine->maximum())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
handler->mm += 1000;
|
|
|
|
double ypos = depthLine->posAtValue(handler->mm / 1000);
|
|
|
|
handler->setPos(handler->pos().x(), ypos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createDecoStops();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerGraphics::keyUpAction()
|
|
|
|
{
|
|
|
|
Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()){
|
|
|
|
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)){
|
|
|
|
if (handler->mm / 1000 <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
handler->mm -= 1000;
|
|
|
|
double ypos = depthLine->posAtValue(handler->mm / 1000);
|
|
|
|
handler->setPos(handler->pos().x(), ypos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createDecoStops();
|
|
|
|
}
|
|
|
|
|
2013-07-04 15:30:05 +00:00
|
|
|
void DivePlannerGraphics::keyLeftAction()
|
|
|
|
{
|
|
|
|
Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()){
|
|
|
|
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)){
|
|
|
|
if (handler->sec / 60 <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// don't overlap positions.
|
|
|
|
// maybe this is a good place for a 'goto'?
|
|
|
|
double xpos = timeLine->posAtValue((handler->sec - 60) / 60);
|
|
|
|
bool nextStep = false;
|
|
|
|
Q_FOREACH(DiveHandler *h, handles){
|
|
|
|
if (h->pos().x() == xpos){
|
|
|
|
nextStep = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(nextStep)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
handler->sec -= 60;
|
|
|
|
handler->setPos(xpos, handler->pos().y());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createDecoStops();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerGraphics::keyRightAction()
|
|
|
|
{
|
|
|
|
Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()){
|
|
|
|
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)){
|
|
|
|
if (handler->sec / 60 >= timeLine->maximum())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// don't overlap positions.
|
|
|
|
// maybe this is a good place for a 'goto'?
|
|
|
|
double xpos = timeLine->posAtValue((handler->sec + 60) / 60);
|
|
|
|
bool nextStep = false;
|
|
|
|
Q_FOREACH(DiveHandler *h, handles){
|
|
|
|
if (h->pos().x() == xpos){
|
|
|
|
nextStep = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(nextStep)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
handler->sec += 60;
|
|
|
|
handler->setPos(xpos, handler->pos().y());
|
|
|
|
}
|
|
|
|
} createDecoStops();
|
|
|
|
}
|
2013-07-04 14:28:39 +00:00
|
|
|
|
2013-07-04 14:16:42 +00:00
|
|
|
void DivePlannerGraphics::keyDeleteAction()
|
|
|
|
{
|
|
|
|
if(scene()->selectedItems().count()){
|
|
|
|
Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()){
|
|
|
|
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)){
|
|
|
|
handles.removeAll(handler);
|
|
|
|
scene()->removeItem(handler);
|
|
|
|
delete i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createDecoStops();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 14:06:28 +00:00
|
|
|
void DivePlannerGraphics::keyEscAction()
|
|
|
|
{
|
|
|
|
if (scene()->selectedItems().count()){
|
|
|
|
scene()->clearSelection();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelClicked();
|
|
|
|
}
|
|
|
|
|
2013-07-02 13:53:08 +00:00
|
|
|
qreal DivePlannerGraphics::fromPercent(qreal percent, Qt::Orientation orientation)
|
|
|
|
{
|
|
|
|
qreal total = orientation == Qt::Horizontal ? sceneRect().width() : sceneRect().height();
|
|
|
|
qreal result = (total * percent) / 100;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:45:58 +00:00
|
|
|
void DivePlannerGraphics::cancelClicked()
|
|
|
|
{
|
|
|
|
mainWindow()->showProfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerGraphics::okClicked()
|
|
|
|
{
|
|
|
|
// todo.
|
2013-06-21 19:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerGraphics::increaseDepth()
|
|
|
|
{
|
2013-07-02 17:14:09 +00:00
|
|
|
if (depthLine->maximum() + 10 > MAX_DEEPNESS)
|
|
|
|
return;
|
|
|
|
depthLine->setMaximum( depthLine->maximum() + 10);
|
|
|
|
depthLine->updateTicks();
|
|
|
|
createDecoStops();
|
2013-06-21 19:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerGraphics::increaseTime()
|
|
|
|
{
|
2013-07-02 17:36:52 +00:00
|
|
|
minMinutes += 10;
|
|
|
|
timeLine->setMaximum( minMinutes );
|
|
|
|
timeLine->updateTicks();
|
|
|
|
createDecoStops();
|
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());
|
2013-06-23 20:09:29 +00:00
|
|
|
if (isPointOutOfBoundaries(mappedPos))
|
2013-06-20 16:56:28 +00:00
|
|
|
return;
|
|
|
|
|
2013-06-24 01:04:35 +00:00
|
|
|
int minutes = rint(timeLine->valueAt(mappedPos));
|
|
|
|
int meters = rint(depthLine->valueAt(mappedPos));
|
|
|
|
double xpos = timeLine->posAtValue(minutes);
|
|
|
|
double ypos = depthLine->posAtValue(meters);
|
2013-07-04 13:06:52 +00:00
|
|
|
Q_FOREACH(DiveHandler* handler, handles){
|
|
|
|
if (xpos == handler->pos().x()){
|
|
|
|
qDebug() << "There's already an point at that place.";
|
|
|
|
//TODO: Move this later to a KMessageWidget.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DiveHandler *item = new DiveHandler ();
|
|
|
|
item->sec = minutes * 60;
|
|
|
|
item->mm = meters * 1000;
|
2013-06-23 20:09:29 +00:00
|
|
|
item->setPos(QPointF(xpos, ypos));
|
|
|
|
scene()->addItem(item);
|
2013-06-20 16:20:41 +00:00
|
|
|
handles << item;
|
2013-06-26 23:41:39 +00:00
|
|
|
createDecoStops();
|
2013-06-20 16:20:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 20:09:29 +00:00
|
|
|
void DivePlannerGraphics::createDecoStops()
|
2013-06-20 16:20:41 +00:00
|
|
|
{
|
2013-06-26 23:41:39 +00:00
|
|
|
qDeleteAll(lines);
|
|
|
|
lines.clear();
|
2013-07-04 13:29:28 +00:00
|
|
|
qSort(handles.begin(), handles.end(), handlerLessThenMinutes);
|
2013-06-26 23:41:39 +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
|
2013-06-24 01:04:35 +00:00
|
|
|
// Not sure if this is the place to create the diveplan...
|
|
|
|
// We just start with a surface node at time = 0
|
2013-06-24 04:27:52 +00:00
|
|
|
struct diveplan diveplan;
|
2013-06-24 01:04:35 +00:00
|
|
|
struct divedatapoint *dp = create_dp(0, 0, 209, 0, 0);
|
|
|
|
dp->entered = TRUE;
|
2013-06-24 04:27:52 +00:00
|
|
|
diveplan.dp = dp;
|
|
|
|
diveplan.gflow = 30;
|
|
|
|
diveplan.gfhigh = 70;
|
|
|
|
diveplan.surface_pressure = 1013;
|
2013-06-24 01:04:35 +00:00
|
|
|
DiveHandler *lastH = NULL;
|
2013-06-23 20:09:29 +00:00
|
|
|
Q_FOREACH(DiveHandler *h, handles) {
|
2013-06-24 01:04:35 +00:00
|
|
|
// these values need to come from the planner UI, eventually
|
|
|
|
int o2 = 209;
|
|
|
|
int he = 0;
|
|
|
|
int po2 = 0;
|
|
|
|
int deltaT = lastH ? h->sec - lastH->sec : h->sec;
|
|
|
|
lastH = h;
|
2013-06-27 08:56:46 +00:00
|
|
|
dp = plan_add_segment(&diveplan, deltaT, h->mm, o2, he, po2);
|
|
|
|
dp->entered = TRUE;
|
2013-06-24 01:04:35 +00:00
|
|
|
qDebug("time %d, depth %d", h->sec, h->mm);
|
2013-06-20 21:25:03 +00:00
|
|
|
}
|
2013-06-24 01:04:35 +00:00
|
|
|
#if DEBUG_PLAN
|
2013-06-24 04:27:52 +00:00
|
|
|
dump_plan(&diveplan);
|
|
|
|
#endif
|
|
|
|
char *cache = NULL;
|
|
|
|
struct dive *dive = NULL;
|
|
|
|
char *errorString = NULL;
|
|
|
|
plan(&diveplan, &cache, &dive, &errorString);
|
|
|
|
#if DEBUG_PLAN
|
|
|
|
dump_plan(&diveplan);
|
2013-06-24 01:04:35 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-24 04:27:52 +00:00
|
|
|
while(dp->next)
|
|
|
|
dp = dp->next;
|
2013-06-26 23:41:39 +00:00
|
|
|
|
2013-06-27 14:10:03 +00:00
|
|
|
if (timeLine->maximum() < dp->time / 60.0 + 5 ||
|
|
|
|
dp->time / 60.0 + 15 < timeLine->maximum()) {
|
2013-07-02 17:36:52 +00:00
|
|
|
double newMax = fmax(dp->time / 60.0 + 5, minMinutes);
|
2013-06-27 14:10:03 +00:00
|
|
|
timeLine->setMaximum(newMax);
|
2013-06-26 22:14:55 +00:00
|
|
|
timeLine->updateTicks();
|
2013-06-27 14:10:03 +00:00
|
|
|
}
|
2013-06-26 22:14:55 +00:00
|
|
|
|
2013-06-20 21:25:03 +00:00
|
|
|
// Re-position the user generated dive handlers
|
2013-06-26 22:14:55 +00:00
|
|
|
Q_FOREACH(DiveHandler *h, handles){
|
2013-07-02 14:47:26 +00:00
|
|
|
h->setPos(timeLine->posAtValue(h->sec / 60), depthLine->posAtValue(h->mm / 1000));
|
2013-06-20 21:25:03 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 12:54:17 +00:00
|
|
|
// (re-) create the profile with different colors for segments that were
|
|
|
|
// entered vs. segments that were calculated
|
|
|
|
double lastx = timeLine->posAtValue(0);
|
|
|
|
double lasty = depthLine->posAtValue(0);
|
2013-06-27 22:52:58 +00:00
|
|
|
|
|
|
|
QPolygonF poly;
|
|
|
|
poly.append(QPointF(lastx, lasty));
|
2013-06-24 04:27:52 +00:00
|
|
|
for (dp = diveplan.dp; dp != NULL; dp = dp->next) {
|
2013-06-27 08:56:46 +00:00
|
|
|
double xpos = timeLine->posAtValue(dp->time / 60.0);
|
|
|
|
double ypos = depthLine->posAtValue(dp->depth / 1000.0);
|
2013-07-02 16:39:54 +00:00
|
|
|
if(!dp->entered){
|
|
|
|
QGraphicsLineItem *item = new QGraphicsLineItem(lastx, lasty, xpos, ypos);
|
|
|
|
item->setPen(QPen(QBrush(Qt::red),0));
|
|
|
|
scene()->addItem(item);
|
|
|
|
lines << item;
|
|
|
|
}
|
2013-06-27 08:56:46 +00:00
|
|
|
lastx = xpos;
|
|
|
|
lasty = ypos;
|
2013-06-27 22:52:58 +00:00
|
|
|
poly.append(QPointF(lastx, lasty));
|
2013-06-24 04:27:52 +00:00
|
|
|
}
|
2013-06-27 20:31:10 +00:00
|
|
|
|
2013-06-27 22:52:58 +00:00
|
|
|
diveBg->setPolygon(poly);
|
|
|
|
QRectF b = poly.boundingRect();
|
2013-07-02 14:21:28 +00:00
|
|
|
QLinearGradient pat(
|
2013-06-27 22:52:58 +00:00
|
|
|
b.x(),
|
|
|
|
b.y(),
|
|
|
|
b.x(),
|
|
|
|
b.height() + b.y()
|
|
|
|
);
|
|
|
|
|
2013-07-02 14:21:28 +00:00
|
|
|
pat.setColorAt(1, profile_color[DEPTH_BOTTOM].first());
|
|
|
|
pat.setColorAt(0, profile_color[DEPTH_TOP].first());
|
|
|
|
diveBg->setBrush(pat);
|
2013-06-27 22:52:58 +00:00
|
|
|
|
2013-06-27 20:31:10 +00:00
|
|
|
deleteTemporaryDivePlan(diveplan.dp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerGraphics::deleteTemporaryDivePlan(divedatapoint* dp)
|
|
|
|
{
|
|
|
|
if (!dp)
|
|
|
|
return;
|
|
|
|
deleteTemporaryDivePlan(dp->next);
|
|
|
|
free(dp);
|
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
|
|
|
{
|
2013-06-23 20:09:29 +00:00
|
|
|
QGraphicsView::resizeEvent(event);
|
2013-06-27 17:48:03 +00:00
|
|
|
fitInView(sceneRect(), Qt::IgnoreAspectRatio);
|
2013-06-20 16:28:04 +00:00
|
|
|
}
|
|
|
|
|
2013-06-20 21:40:59 +00:00
|
|
|
void DivePlannerGraphics::showEvent(QShowEvent* event)
|
2013-06-20 16:28:04 +00:00
|
|
|
{
|
2013-06-23 20:09:29 +00:00
|
|
|
QGraphicsView::showEvent(event);
|
2013-06-27 17:48:03 +00:00
|
|
|
fitInView(sceneRect(), Qt::IgnoreAspectRatio);
|
2013-06-20 16:28:04 +00:00
|
|
|
}
|
|
|
|
|
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-07-02 14:12:15 +00:00
|
|
|
verticalLine->setPos(mappedPos.x(), fromPercent(0, Qt::Vertical));
|
|
|
|
horizontalLine->setPos(fromPercent(0, Qt::Horizontal), mappedPos.y());
|
|
|
|
|
2013-06-24 00:32:23 +00:00
|
|
|
depthString->setText(QString::number(rint(depthLine->valueAt(mappedPos))) + "m" );
|
2013-07-02 14:12:15 +00:00
|
|
|
depthString->setPos(fromPercent(5, Qt::Horizontal), mappedPos.y());
|
2013-06-24 00:32:23 +00:00
|
|
|
timeString->setText(QString::number(rint(timeLine->valueAt(mappedPos))) + "min");
|
2013-07-02 14:12:15 +00:00
|
|
|
timeString->setPos(mappedPos.x()+1, fromPercent(95, Qt::Vertical));
|
2013-06-20 17:20:45 +00:00
|
|
|
|
2013-07-02 16:31:25 +00:00
|
|
|
// calculate the correct color for the depthString.
|
|
|
|
// QGradient doesn't returns it's interpolation, meh.
|
|
|
|
double percent = depthLine->percentAt(mappedPos);
|
|
|
|
QColor& startColor = profile_color[SAMPLE_SHALLOW].first();
|
|
|
|
QColor& endColor = profile_color[SAMPLE_DEEP].first();
|
|
|
|
short redDelta = (endColor.red() - startColor.red()) * percent + startColor.red();
|
|
|
|
short greenDelta = (endColor.green() - startColor.green()) * percent + startColor.green();
|
|
|
|
short blueDelta = (endColor.blue() - startColor.blue()) * percent + startColor.blue();
|
|
|
|
depthString->setBrush( QColor(redDelta, greenDelta, blueDelta));
|
|
|
|
|
2013-06-23 20:09:29 +00:00
|
|
|
if (activeDraggedHandler)
|
2013-06-20 18:15:10 +00:00
|
|
|
moveActiveHandler(mappedPos);
|
2013-06-20 17:20:45 +00:00
|
|
|
if (!handles.count())
|
|
|
|
return;
|
|
|
|
|
2013-06-23 20:09:29 +00:00
|
|
|
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 {
|
2013-06-20 17:20:45 +00:00
|
|
|
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
|
|
|
{
|
2013-06-21 19:28:17 +00:00
|
|
|
double xpos = timeLine->posAtValue(rint(timeLine->valueAt(pos)));
|
|
|
|
double ypos = depthLine->posAtValue(rint(depthLine->valueAt(pos)));
|
2013-07-04 13:29:28 +00:00
|
|
|
activeDraggedHandler->setPos(QPointF(xpos, ypos));
|
2013-06-27 08:56:46 +00:00
|
|
|
qDeleteAll(lines);
|
2013-06-26 23:41:39 +00:00
|
|
|
lines.clear();
|
2013-06-20 18:15:10 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2013-06-23 20:09:29 +00:00
|
|
|
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
|
|
|
{
|
2013-07-04 14:01:59 +00:00
|
|
|
if (event->modifiers()){
|
|
|
|
QGraphicsView::mousePressEvent(event);
|
2013-07-04 15:30:05 +00:00
|
|
|
return;
|
2013-07-04 14:01:59 +00:00
|
|
|
}
|
|
|
|
|
2013-06-20 17:46:40 +00:00
|
|
|
QPointF mappedPos = mapToScene(event->pos());
|
2013-07-04 13:34:30 +00:00
|
|
|
Q_FOREACH(QGraphicsItem *item, scene()->items(mappedPos, Qt::IntersectsItemBoundingRect, Qt::AscendingOrder, transform())){
|
2013-06-23 20:09:29 +00:00
|
|
|
if (DiveHandler *h = qgraphicsitem_cast<DiveHandler*>(item)) {
|
2013-06-20 17:46:40 +00:00
|
|
|
activeDraggedHandler = h;
|
|
|
|
activeDraggedHandler->setBrush(Qt::red);
|
2013-07-04 13:29:28 +00:00
|
|
|
originalHandlerPos = activeDraggedHandler->pos();
|
2013-06-20 17:46:40 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-04 15:30:05 +00:00
|
|
|
QGraphicsView::mousePressEvent(event);
|
2013-06-20 17:46:40 +00:00
|
|
|
}
|
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-23 20:09:29 +00:00
|
|
|
if (activeDraggedHandler) {
|
2013-07-04 13:29:28 +00:00
|
|
|
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;
|
2013-06-27 22:52:58 +00:00
|
|
|
activeDraggedHandler->setBrush(QBrush(Qt::white));
|
2013-07-04 13:29:28 +00:00
|
|
|
activeDraggedHandler->setPos(QPointF(xpos, ypos));
|
|
|
|
|
2013-06-26 23:41:39 +00:00
|
|
|
createDecoStops();
|
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-07-04 13:06:52 +00:00
|
|
|
setRect(-5,-5,10,10);
|
|
|
|
setFlag(QGraphicsItem::ItemIgnoresTransformations);
|
2013-07-04 14:01:59 +00:00
|
|
|
setFlag(QGraphicsItem::ItemIsSelectable);
|
2013-06-27 22:52:58 +00:00
|
|
|
setBrush(Qt::white);
|
|
|
|
setZValue(2);
|
2013-06-20 17:29:32 +00:00
|
|
|
}
|
2013-07-04 14:01:59 +00:00
|
|
|
|
|
|
|
void DiveHandler::mousePressEvent(QGraphicsSceneMouseEvent* event)
|
|
|
|
{
|
|
|
|
if (event->modifiers().testFlag(Qt::ControlModifier)){
|
|
|
|
setSelected(true);
|
|
|
|
}
|
2013-07-04 14:06:28 +00:00
|
|
|
// mousePressEvent 'grabs' the mouse and keyboard, annoying.
|
2013-07-04 14:01:59 +00:00
|
|
|
ungrabMouse();
|
|
|
|
ungrabKeyboard();
|
|
|
|
}
|
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);
|
2013-06-23 20:21:01 +00:00
|
|
|
ticks.clear();
|
2013-06-20 19:48:24 +00:00
|
|
|
QLineF m = line();
|
2013-07-02 15:01:47 +00:00
|
|
|
QGraphicsLineItem *item = NULL;
|
|
|
|
|
2013-06-23 20:09:29 +00:00
|
|
|
if (orientation == Qt::Horizontal) {
|
2013-06-20 19:48:24 +00:00
|
|
|
double steps = (max - min) / interval;
|
|
|
|
double stepSize = (m.x2() - m.x1()) / steps;
|
2013-07-02 14:14:11 +00:00
|
|
|
qreal pos;
|
2013-07-02 20:58:03 +00:00
|
|
|
for (pos = m.x1(); pos < m.x2(); pos += stepSize) {
|
2013-07-02 15:01:47 +00:00
|
|
|
item = new QGraphicsLineItem(pos, m.y1(), pos, m.y1() + tickSize, this);
|
|
|
|
item->setPen(pen());
|
|
|
|
ticks.push_back(item);
|
2013-06-20 19:48:24 +00:00
|
|
|
}
|
2013-07-02 15:01:47 +00:00
|
|
|
item = new QGraphicsLineItem(pos, m.y1(), pos, m.y1() + tickSize, this);
|
|
|
|
item->setPen(pen());
|
|
|
|
ticks.push_back(item);
|
2013-06-23 20:09:29 +00:00
|
|
|
} else {
|
2013-06-20 19:48:24 +00:00
|
|
|
double steps = (max - min) / interval;
|
|
|
|
double stepSize = (m.y2() - m.y1()) / steps;
|
2013-07-02 14:14:11 +00:00
|
|
|
qreal pos;
|
|
|
|
for (pos = m.y1(); pos < m.y2(); pos += stepSize) {
|
2013-07-02 15:01:47 +00:00
|
|
|
item = new QGraphicsLineItem(m.x1(), pos, m.x1() - tickSize, pos, this);
|
|
|
|
item->setPen(pen());
|
|
|
|
ticks.push_back(item);
|
2013-06-20 19:48:24 +00:00
|
|
|
}
|
2013-07-02 15:01:47 +00:00
|
|
|
item = new QGraphicsLineItem(m.x1(), pos, m.x1() - tickSize, pos, this);
|
|
|
|
item->setPen(pen());
|
|
|
|
ticks.push_back(item);
|
2013-06-20 19:48:24 +00:00
|
|
|
}
|
2013-06-20 18:52:27 +00:00
|
|
|
}
|
|
|
|
|
2013-07-02 14:12:15 +00:00
|
|
|
void Ruler::setTickSize(qreal size)
|
|
|
|
{
|
|
|
|
tickSize = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-23 20:09:29 +00:00
|
|
|
double retValue = orientation == Qt::Horizontal ?
|
|
|
|
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;
|
2013-06-23 20:09:29 +00:00
|
|
|
double realSize = orientation == Qt::Horizontal ?
|
|
|
|
m.x2() - m.x1() :
|
|
|
|
m.y2() - m.y1();
|
2013-06-21 18:53:20 +00:00
|
|
|
double retValue = realSize * percent;
|
2013-06-23 20:09:29 +00:00
|
|
|
retValue = (orientation == Qt::Horizontal) ?
|
|
|
|
retValue + m.x1() :
|
|
|
|
retValue + m.y1();
|
2013-06-21 18:53:20 +00:00
|
|
|
return retValue;
|
2013-06-20 21:40:59 +00:00
|
|
|
}
|
|
|
|
|
2013-07-02 16:31:25 +00:00
|
|
|
qreal Ruler::percentAt(const QPointF& p)
|
|
|
|
{
|
|
|
|
qreal value = valueAt(p);
|
|
|
|
double size = max - min;
|
|
|
|
double percent = value / size;
|
|
|
|
return percent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 19:07:44 +00:00
|
|
|
double Ruler::maximum() const
|
|
|
|
{
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Ruler::minimum() const
|
|
|
|
{
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
2013-07-02 15:01:47 +00:00
|
|
|
void Ruler::setColor(const QColor& color)
|
|
|
|
{
|
|
|
|
setPen(QPen(color));
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:45:58 +00:00
|
|
|
Button::Button(QObject* parent): QObject(parent), QGraphicsRectItem()
|
2013-06-21 19:44:38 +00:00
|
|
|
{
|
2013-06-27 19:45:58 +00:00
|
|
|
icon = new QGraphicsPixmapItem(this);
|
|
|
|
text = new QGraphicsSimpleTextItem(this);
|
|
|
|
icon->setPos(0,0);
|
|
|
|
text->setPos(0,0);
|
2013-06-21 19:44:38 +00:00
|
|
|
setFlag(ItemIgnoresTransformations);
|
2013-07-02 17:14:09 +00:00
|
|
|
setPen(QPen(QBrush(), 0));
|
2013-06-27 19:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setPixmap(const QPixmap& pixmap)
|
|
|
|
{
|
2013-07-02 17:14:09 +00:00
|
|
|
icon->setPixmap(pixmap.scaled(20,20, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
2013-06-27 19:45:58 +00:00
|
|
|
if(pixmap.isNull()){
|
|
|
|
icon->hide();
|
|
|
|
}else{
|
|
|
|
icon->show();
|
|
|
|
}
|
|
|
|
setRect(childrenBoundingRect());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setText(const QString& t)
|
|
|
|
{
|
|
|
|
text->setText(t);
|
|
|
|
if(icon->pixmap().isNull()){
|
|
|
|
icon->hide();
|
|
|
|
text->setPos(0,0);
|
|
|
|
}else{
|
|
|
|
icon->show();
|
|
|
|
text->setPos(22,0);
|
|
|
|
}
|
|
|
|
setRect(childrenBoundingRect());
|
2013-06-21 19:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::mousePressEvent(QGraphicsSceneMouseEvent* event)
|
|
|
|
{
|
2013-07-02 17:36:52 +00:00
|
|
|
event->ignore();
|
2013-06-21 19:44:38 +00:00
|
|
|
emit clicked();
|
|
|
|
}
|