diff options
Diffstat (limited to 'qt-ui/diveplanner.cpp')
-rw-r--r-- | qt-ui/diveplanner.cpp | 229 |
1 files changed, 159 insertions, 70 deletions
diff --git a/qt-ui/diveplanner.cpp b/qt-ui/diveplanner.cpp index ab7bda17d..ceb5ebdba 100644 --- a/qt-ui/diveplanner.cpp +++ b/qt-ui/diveplanner.cpp @@ -5,6 +5,7 @@ #include "ui_diveplanner.h" #include "mainwindow.h" #include "tableview.h" +#include "graphicsview-common.h" #include "../dive.h" #include "../divelist.h" @@ -19,6 +20,7 @@ #include <QModelIndex> #include <QSettings> #include <QTableView> +#include <QColor> #define TIME_INITIAL_MAX 30 @@ -40,6 +42,11 @@ QString strForAir(const divedatapoint& p){ : QObject::tr("Choose Gas"); } +QColor getColor(const color_indice_t i) +{ + return profile_color[i].at(0); +} + static DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance(); DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0) @@ -66,6 +73,7 @@ DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent) fromPercent(100, Qt::Horizontal), fromPercent(0, Qt::Vertical) ); + horizontalLine->setPen(QPen(Qt::DotLine)); scene()->addItem(horizontalLine); @@ -73,6 +81,7 @@ DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent) timeLine->setMinimum(0); timeLine->setMaximum(TIME_INITIAL_MAX); timeLine->setTickInterval(10); + timeLine->setColor(getColor(TIME_GRID)); timeLine->setLine( fromPercent(10, Qt::Horizontal), fromPercent(90, Qt::Vertical), @@ -81,7 +90,7 @@ DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent) ); timeLine->setOrientation(Qt::Horizontal); timeLine->setTickSize(fromPercent(1, Qt::Vertical)); - timeLine->setColor(profile_color[TIME_GRID].at(0)); + timeLine->setTextColor(getColor(TIME_TEXT)); timeLine->updateTicks(); scene()->addItem(timeLine); @@ -97,7 +106,8 @@ DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent) ); depthLine->setOrientation(Qt::Vertical); depthLine->setTickSize(fromPercent(1, Qt::Horizontal)); - depthLine->setColor(profile_color[DEPTH_GRID].at(0)); + depthLine->setColor(getColor(DEPTH_GRID)); + depthLine->setTextColor(getColor(SAMPLE_DEEP)); depthLine->updateTicks(); scene()->addItem(depthLine); @@ -317,7 +327,7 @@ void DivePlannerGraphics::keyEscAction() scene()->clearSelection(); return; } - cancelPlan(); + plannerModel->cancelPlan(); } qreal DivePlannerGraphics::fromPercent(qreal percent, Qt::Orientation orientation) @@ -327,18 +337,6 @@ qreal DivePlannerGraphics::fromPercent(qreal percent, Qt::Orientation orientatio return result; } -void DivePlannerGraphics::cancelPlan() -{ - if (handles.size()){ - if (QMessageBox::warning(mainWindow(), tr("Save the Plan?"), - tr("You have a working plan, \n are you sure that you wanna cancel it?"), - QMessageBox::Ok | QMessageBox::Cancel) != QMessageBox::Ok){ - return; - } - } - mainWindow()->showProfile(); -} - void DivePlannerGraphics::increaseDepth() { if (depthLine->maximum() + 10 > MAX_DEEPNESS) @@ -422,42 +420,14 @@ void DivePlannerGraphics::createDecoStops() qDeleteAll(lines); lines.clear(); - // This needs to be done in the following steps: - // Get the user-input and calculate the dive info - // Not sure if this is the place to create the diveplan... - // We just start with a surface node at time = 0 + plannerModel->createTemporaryPlan(); struct diveplan diveplan = plannerModel->getDiveplan(); - struct divedatapoint *dp = create_dp(0, 0, 209, 0, 0); - dp->entered = TRUE; - diveplan.dp = dp; - - int rowCount = plannerModel->rowCount(); - int lastIndex = -1; - for(int i = 0; i < rowCount; i++){ - divedatapoint p = plannerModel->at(i); - int deltaT = lastIndex != -1 ? p.time - plannerModel->at(lastIndex).time : p.time; - lastIndex = i; - dp = plan_add_segment(&diveplan, deltaT, p.depth, p.o2, p.he, p.po2); - } - -#if DEBUG_PLAN - 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); -#endif - - while(dp->next) + struct divedatapoint *dp = diveplan.dp; + while(dp->next){ dp = dp->next; + } - dpMaxTime = dp->time / 60.0 + 5; - - if (timeLine->maximum() < dp->time / 60.0 + 5 || - dp->time / 60.0 + 15 < timeLine->maximum()) { + if (timeLine->maximum() < dp->time / 60.0 + 5 || dp->time / 60.0 + 15 < timeLine->maximum()) { double newMax = fmax(dp->time / 60.0 + 5, minMinutes); timeLine->setMaximum(newMax); timeLine->updateTicks(); @@ -483,6 +453,7 @@ void DivePlannerGraphics::createDecoStops() QPolygonF poly; poly.append(QPointF(lastx, lasty)); + for (dp = diveplan.dp; dp != NULL; dp = dp->next) { double xpos = timeLine->posAtValue(dp->time / 60.0); double ypos = depthLine->posAtValue(dp->depth / 1000.0); @@ -510,16 +481,7 @@ void DivePlannerGraphics::createDecoStops() pat.setColorAt(0, profile_color[DEPTH_TOP].first()); diveBg->setBrush(pat); - deleteTemporaryDivePlan(diveplan.dp); - delete_single_dive(get_divenr(dive)); -} - -void DivePlannerGraphics::deleteTemporaryDivePlan(divedatapoint* dp) -{ - if (!dp) - return; - deleteTemporaryDivePlan(dp->next); - free(dp); + plannerModel->deleteTemporaryPlan(); } void DivePlannerGraphics::resizeEvent(QResizeEvent* event) @@ -681,6 +643,11 @@ void Ruler::setMinimum(double minimum) min = minimum; } +void Ruler::setTextColor(const QColor& color) +{ + textColor = color; +} + Ruler::Ruler() : orientation(Qt::Horizontal) { } @@ -688,39 +655,70 @@ Ruler::Ruler() : orientation(Qt::Horizontal) void Ruler::setOrientation(Qt::Orientation o) { orientation = o; + // position the elements on the screen. + setMinimum(minimum()); + setMaximum(maximum()); } void Ruler::updateTicks() { qDeleteAll(ticks); ticks.clear(); + qDeleteAll(labels); + labels.clear(); + QLineF m = line(); QGraphicsLineItem *item = NULL; + QGraphicsSimpleTextItem *label = NULL; + + double steps = (max - min) / interval; + qreal pos; + double currValue = min; if (orientation == Qt::Horizontal) { - double steps = (max - min) / interval; double stepSize = (m.x2() - m.x1()) / steps; - qreal pos; - for (pos = m.x1(); pos < m.x2(); pos += stepSize) { + for (pos = m.x1(); pos < m.x2(); pos += stepSize, currValue += interval) { item = new QGraphicsLineItem(pos, m.y1(), pos, m.y1() + tickSize, this); item->setPen(pen()); ticks.push_back(item); + + label = new QGraphicsSimpleTextItem(QString::number(currValue), this); + label->setBrush(QBrush(textColor)); + label->setFlag(ItemIgnoresTransformations); + label->setPos(pos - label->boundingRect().width()/2, m.y1() + tickSize + 5); + labels.push_back(label); } item = new QGraphicsLineItem(pos, m.y1(), pos, m.y1() + tickSize, this); item->setPen(pen()); ticks.push_back(item); + + label = new QGraphicsSimpleTextItem(QString::number(currValue), this); + label->setBrush(QBrush(textColor)); + label->setFlag(ItemIgnoresTransformations); + label->setPos(pos - label->boundingRect().width()/2, m.y1() + tickSize + 5); + labels.push_back(label); } else { - double steps = (max - min) / interval; double stepSize = (m.y2() - m.y1()) / steps; - qreal pos; - for (pos = m.y1(); pos < m.y2(); pos += stepSize) { + for (pos = m.y1(); pos < m.y2(); pos += stepSize, currValue += interval) { item = new QGraphicsLineItem(m.x1(), pos, m.x1() - tickSize, pos, this); item->setPen(pen()); ticks.push_back(item); + + label = new QGraphicsSimpleTextItem(QString::number(currValue), this); + label->setBrush(QBrush(textColor)); + label->setFlag(ItemIgnoresTransformations); + label->setPos(m.x2() - 80, pos); + labels.push_back(label); } item = new QGraphicsLineItem(m.x1(), pos, m.x1() - tickSize, pos, this); item->setPen(pen()); ticks.push_back(item); + + label = new QGraphicsSimpleTextItem(QString::number(currValue), this); + label->setBrush(QBrush(textColor)); + label->setFlag(ItemIgnoresTransformations); + label->setPos(m.x2() - 80, pos); + labels.push_back(label); } } @@ -780,7 +778,12 @@ double Ruler::minimum() const void Ruler::setColor(const QColor& color) { - setPen(QPen(color)); + QPen defaultPen(color); + defaultPen.setJoinStyle(Qt::RoundJoin); + defaultPen.setCapStyle(Qt::RoundCap); + defaultPen.setWidth(2); + defaultPen.setCosmetic(true); + setPen(defaultPen); } Button::Button(QObject* parent): QObject(parent), QGraphicsRectItem() @@ -840,6 +843,13 @@ DivePlannerWidget::DivePlannerWidget(QWidget* parent, Qt::WindowFlags f): QWidge connect(ui->highGF, SIGNAL(textChanged(QString)), this, SLOT(gfhighChanged(QString))); connect(ui->lastStop, SIGNAL(toggled(bool)), this, SLOT(lastStopChanged(bool))); + // Creating the plan + connect(ui->buttonBox, SIGNAL(accepted()), plannerModel, SLOT(createPlan())); + connect(ui->buttonBox, SIGNAL(rejected()), plannerModel, SLOT(cancelPlan())); + connect(plannerModel, SIGNAL(planCreated()), mainWindow(), SLOT(showProfile())); + connect(plannerModel, SIGNAL(planCreated()), mainWindow(), SLOT(refreshDisplay())); + connect(plannerModel, SIGNAL(planCanceled()), mainWindow(), SLOT(showProfile())); + /* set defaults. */ ui->startTime->setTime( QTime(1, 0) ); ui->ATMPressure->setText( "1013" ); @@ -975,11 +985,6 @@ DivePlannerPointsModel* DivePlannerPointsModel::instance() return self; } -void DivePlannerPointsModel::createPlan() -{ - -} - void DivePlannerPointsModel::setBottomSac(int sac) { diveplan.bottomsac = sac; @@ -1094,3 +1099,87 @@ struct diveplan DivePlannerPointsModel::getDiveplan() { return diveplan; } + +void DivePlannerPointsModel::cancelPlan() +{ + if(rowCount()){ + if (QMessageBox::warning(mainWindow(), tr("Save the Plan?"), + tr("You have a working plan, \n are you sure that you wanna cancel it?"), + QMessageBox::Ok | QMessageBox::Cancel) != QMessageBox::Ok){ + return; + } + } + + beginRemoveRows(QModelIndex(), 0, rowCount()-1); + divepoints.clear(); + endRemoveRows(); + emit planCanceled(); +} + +void DivePlannerPointsModel::createTemporaryPlan() +{ + // This needs to be done in the following steps: + // Get the user-input and calculate the dive info + // Not sure if this is the place to create the diveplan... + // We just start with a surface node at time = 0 + struct divedatapoint *dp = create_dp(0, 0, 209, 0, 0); + dp->entered = TRUE; + diveplan.dp = dp; + int lastIndex = -1; + for(int i = 0; i < rowCount(); i++){ + divedatapoint p = at(i); + int deltaT = lastIndex != -1 ? p.time - at(lastIndex).time : p.time; + lastIndex = i; + dp = plan_add_segment(&diveplan, deltaT, p.depth, p.o2, p.he, p.po2); + } +#if DEBUG_PLAN + dump_plan(&diveplan); +#endif + char *cache = NULL; + tempDive = NULL; + char *errorString = NULL; + plan(&diveplan, &cache, &tempDive, &errorString); +#if DEBUG_PLAN + dump_plan(&diveplan); +#endif +} + +void DivePlannerPointsModel::deleteTemporaryPlan() +{ + deleteTemporaryPlan(diveplan.dp); + delete_single_dive(get_divenr(tempDive)); + tempDive = NULL; +} + +void DivePlannerPointsModel::deleteTemporaryPlan(struct divedatapoint *dp) +{ + if (!dp){ + return; + } + + deleteTemporaryPlan(dp->next); + free(dp); +} + +void DivePlannerPointsModel::createPlan() +{ + // Ok, so, here the diveplan creates a dive, + // puts it on the dive list, and we need to remember + // to not delete it later. mumble. ;p + char *cache = NULL; + tempDive = NULL; + char *errorString = NULL; + + createTemporaryPlan(); + plan(&diveplan, &cache, &tempDive, &errorString); + mark_divelist_changed(TRUE); + + // Remove and clean the diveplan, so we don't delete + // the dive by mistake. + diveplan.dp = NULL; + beginRemoveRows(QModelIndex(), 0, rowCount() -1 ); + divepoints.clear(); + endRemoveRows(); + + planCreated(); +} |