2017-04-27 18:25:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-05-28 19:23:49 +00:00
|
|
|
#include "diveplannermodel.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/dive.h"
|
|
|
|
#include "core/helpers.h"
|
|
|
|
#include "qt-models/cylindermodel.h"
|
|
|
|
#include "core/planner.h"
|
|
|
|
#include "qt-models/models.h"
|
2016-11-08 20:17:35 +00:00
|
|
|
#include "core/device.h"
|
2017-11-22 19:42:33 +00:00
|
|
|
#include "core/qthelper.h"
|
2017-01-06 10:10:21 +00:00
|
|
|
#include "core/subsurface-qt/SettingsObjectWrapper.h"
|
2017-01-23 17:11:52 +00:00
|
|
|
#include <QApplication>
|
2017-04-19 13:40:59 +00:00
|
|
|
#include <QTextDocument>
|
2017-08-29 09:41:30 +00:00
|
|
|
#include <QtConcurrent>
|
2015-05-28 19:23:49 +00:00
|
|
|
|
2017-10-03 08:06:15 +00:00
|
|
|
#define UNIT_FACTOR ((prefs.units.length == units::METERS) ? 1000.0 / 60.0 : feet_to_mm(1.0) / 60.0)
|
|
|
|
|
2015-05-28 19:23:49 +00:00
|
|
|
/* TODO: Port this to CleanerTableModel to remove a bit of boilerplate and
|
|
|
|
* use the signal warningMessage() to communicate errors to the MainWindow.
|
|
|
|
*/
|
|
|
|
void DivePlannerPointsModel::removeSelectedPoints(const QVector<int> &rows)
|
|
|
|
{
|
|
|
|
if (!rows.count())
|
|
|
|
return;
|
|
|
|
int firstRow = rowCount() - rows.count();
|
|
|
|
QVector<int> v2 = rows;
|
|
|
|
std::sort(v2.begin(), v2.end());
|
|
|
|
|
|
|
|
beginRemoveRows(QModelIndex(), firstRow, rowCount() - 1);
|
|
|
|
for (int i = v2.count() - 1; i >= 0; i--) {
|
|
|
|
divepoints.remove(v2[i]);
|
|
|
|
}
|
|
|
|
endRemoveRows();
|
2017-03-13 07:25:32 +00:00
|
|
|
CylindersModel::instance()->updateTrashIcon();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::createSimpleDive()
|
|
|
|
{
|
|
|
|
// initialize the start time in the plan
|
|
|
|
diveplan.when = displayed_dive.when;
|
|
|
|
|
2016-07-06 12:40:28 +00:00
|
|
|
// Use gas from the first cylinder
|
|
|
|
int cylinderid = 0;
|
2015-05-28 19:23:49 +00:00
|
|
|
|
|
|
|
// If we're in drop_stone_mode, don't add a first point.
|
|
|
|
// It will be added implicit.
|
|
|
|
if (!prefs.drop_stone_mode)
|
2016-07-06 12:40:28 +00:00
|
|
|
addStop(M_OR_FT(15, 45), 1 * 60, cylinderid, 0, true);
|
2015-05-28 19:23:49 +00:00
|
|
|
|
2016-07-06 12:40:28 +00:00
|
|
|
addStop(M_OR_FT(15, 45), 20 * 60, 0, 0, true);
|
2015-05-28 19:23:49 +00:00
|
|
|
if (!isPlanner()) {
|
2016-07-06 12:40:28 +00:00
|
|
|
addStop(M_OR_FT(5, 15), 42 * 60, 0, cylinderid, true);
|
|
|
|
addStop(M_OR_FT(5, 15), 45 * 60, 0, cylinderid, true);
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
2016-07-06 12:40:35 +00:00
|
|
|
updateMaxDepth();
|
2016-10-26 02:10:34 +00:00
|
|
|
GasSelectionModel::instance()->repopulate();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setupStartTime()
|
|
|
|
{
|
|
|
|
// if the latest dive is in the future, then start an hour after it ends
|
|
|
|
// otherwise start an hour from now
|
|
|
|
startTime = QDateTime::currentDateTimeUtc().addSecs(3600 + gettimezoneoffset());
|
|
|
|
if (dive_table.nr) {
|
|
|
|
struct dive *d = get_dive(dive_table.nr - 1);
|
2017-09-30 16:00:34 +00:00
|
|
|
time_t ends = dive_endtime(d);
|
2015-05-28 19:23:49 +00:00
|
|
|
time_t diff = ends - startTime.toTime_t();
|
|
|
|
if (diff > 0) {
|
|
|
|
startTime = startTime.addSecs(diff + 3600);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit startTimeChanged(startTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::loadFromDive(dive *d)
|
|
|
|
{
|
|
|
|
int depthsum = 0;
|
|
|
|
int samplecount = 0;
|
2017-04-20 06:43:48 +00:00
|
|
|
o2pressure_t last_sp;
|
2015-05-28 19:23:49 +00:00
|
|
|
bool oldRec = recalc;
|
2016-11-08 20:17:35 +00:00
|
|
|
struct divecomputer *dc = &(d->dc);
|
2015-05-28 19:23:49 +00:00
|
|
|
recalc = false;
|
|
|
|
CylindersModel::instance()->updateDive();
|
|
|
|
duration_t lasttime = {};
|
2017-01-23 15:37:04 +00:00
|
|
|
duration_t lastrecordedtime = {};
|
2015-05-28 19:23:49 +00:00
|
|
|
duration_t newtime = {};
|
|
|
|
free_dps(&diveplan);
|
2017-03-05 20:07:11 +00:00
|
|
|
if (mode != PLAN)
|
|
|
|
clear();
|
2015-05-28 19:23:49 +00:00
|
|
|
diveplan.when = d->when;
|
|
|
|
// is this a "new" dive where we marked manually entered samples?
|
|
|
|
// if yes then the first sample should be marked
|
|
|
|
// if it is we only add the manually entered samples as waypoints to the diveplan
|
|
|
|
// otherwise we have to add all of them
|
2015-10-08 15:59:37 +00:00
|
|
|
|
|
|
|
bool hasMarkedSamples = false;
|
|
|
|
|
2016-11-08 20:17:35 +00:00
|
|
|
if (dc->samples)
|
|
|
|
hasMarkedSamples = dc->sample[0].manually_entered;
|
|
|
|
else
|
|
|
|
dc = fake_dc(dc, true);
|
2015-10-08 15:59:37 +00:00
|
|
|
|
2015-05-28 19:23:49 +00:00
|
|
|
// if this dive has more than 100 samples (so it is probably a logged dive),
|
|
|
|
// average samples so we end up with a total of 100 samples.
|
2016-11-08 20:17:35 +00:00
|
|
|
int plansamples = dc->samples <= 100 ? dc->samples : 100;
|
2015-05-28 19:23:49 +00:00
|
|
|
int j = 0;
|
2017-03-05 01:14:22 +00:00
|
|
|
int cylinderid = 0;
|
2017-04-20 06:43:48 +00:00
|
|
|
last_sp.mbar = 0;
|
2015-05-28 19:23:49 +00:00
|
|
|
for (int i = 0; i < plansamples - 1; i++) {
|
2016-11-08 20:17:35 +00:00
|
|
|
while (j * plansamples <= i * dc->samples) {
|
|
|
|
const sample &s = dc->sample[j];
|
2015-05-28 19:23:49 +00:00
|
|
|
if (s.time.seconds != 0 && (!hasMarkedSamples || s.manually_entered)) {
|
|
|
|
depthsum += s.depth.mm;
|
2017-04-20 06:43:48 +00:00
|
|
|
last_sp = s.setpoint;
|
2015-05-28 19:23:49 +00:00
|
|
|
++samplecount;
|
|
|
|
newtime = s.time;
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
if (samplecount) {
|
2017-02-26 04:51:13 +00:00
|
|
|
cylinderid = get_cylinderid_at_time(d, dc, lasttime);
|
2017-01-23 15:37:04 +00:00
|
|
|
if (newtime.seconds - lastrecordedtime.seconds > 10) {
|
2017-04-20 06:43:48 +00:00
|
|
|
addStop(depthsum / samplecount, newtime.seconds, cylinderid, last_sp.mbar, true);
|
2017-01-23 15:37:04 +00:00
|
|
|
lastrecordedtime = newtime;
|
|
|
|
}
|
2015-05-28 19:23:49 +00:00
|
|
|
lasttime = newtime;
|
|
|
|
depthsum = 0;
|
|
|
|
samplecount = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-02-26 04:51:13 +00:00
|
|
|
// make sure we get the last point right so the duration is correct
|
2017-04-20 06:43:48 +00:00
|
|
|
if (!hasMarkedSamples) addStop(0, d->dc.duration.seconds,cylinderid, last_sp.mbar, true);
|
2015-05-28 19:23:49 +00:00
|
|
|
recalc = oldRec;
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy the tanks from the current dive, or the default cylinder
|
|
|
|
// or an unknown cylinder
|
|
|
|
// setup the cylinder widget accordingly
|
|
|
|
void DivePlannerPointsModel::setupCylinders()
|
|
|
|
{
|
2015-10-20 07:56:01 +00:00
|
|
|
int i;
|
2015-05-28 19:23:49 +00:00
|
|
|
if (mode == PLAN && current_dive) {
|
|
|
|
// take the displayed cylinders from the selected dive as starting point
|
|
|
|
CylindersModel::instance()->copyFromDive(current_dive);
|
|
|
|
copy_cylinders(current_dive, &displayed_dive, !prefs.display_unused_tanks);
|
|
|
|
reset_cylinders(&displayed_dive, true);
|
2015-10-20 07:56:01 +00:00
|
|
|
|
|
|
|
for (i = 0; i < MAX_CYLINDERS; i++)
|
|
|
|
if (!cylinder_none(&(displayed_dive.cylinder[i])))
|
|
|
|
return; // We have at least one cylinder
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
if (!same_string(prefs.default_cylinder, "")) {
|
|
|
|
fill_default_cylinder(&displayed_dive.cylinder[0]);
|
2015-10-21 12:49:04 +00:00
|
|
|
}
|
|
|
|
if (cylinder_none(&displayed_dive.cylinder[0])) {
|
2015-05-28 19:23:49 +00:00
|
|
|
// roughly an AL80
|
|
|
|
displayed_dive.cylinder[0].type.description = strdup(tr("unknown").toUtf8().constData());
|
|
|
|
displayed_dive.cylinder[0].type.size.mliter = 11100;
|
|
|
|
displayed_dive.cylinder[0].type.workingpressure.mbar = 207000;
|
|
|
|
}
|
|
|
|
reset_cylinders(&displayed_dive, false);
|
|
|
|
CylindersModel::instance()->copyFromDive(&displayed_dive);
|
|
|
|
}
|
|
|
|
|
2017-03-06 12:27:39 +00:00
|
|
|
// Update the dive's maximum depth. Returns true if max. depth changed
|
2016-07-06 12:40:35 +00:00
|
|
|
bool DivePlannerPointsModel::updateMaxDepth()
|
|
|
|
{
|
|
|
|
int prevMaxDepth = displayed_dive.maxdepth.mm;
|
|
|
|
displayed_dive.maxdepth.mm = 0;
|
|
|
|
for (int i = 0; i < rowCount(); i++) {
|
|
|
|
divedatapoint p = at(i);
|
2017-03-10 12:37:54 +00:00
|
|
|
if (p.depth.mm > displayed_dive.maxdepth.mm)
|
|
|
|
displayed_dive.maxdepth.mm = p.depth.mm;
|
2016-07-06 12:40:35 +00:00
|
|
|
}
|
|
|
|
return (displayed_dive.maxdepth.mm != prevMaxDepth);
|
|
|
|
}
|
|
|
|
|
2015-05-28 19:23:49 +00:00
|
|
|
QStringList &DivePlannerPointsModel::getGasList()
|
|
|
|
{
|
|
|
|
static QStringList list;
|
|
|
|
list.clear();
|
|
|
|
for (int i = 0; i < MAX_CYLINDERS; i++) {
|
|
|
|
cylinder_t *cyl = &displayed_dive.cylinder[i];
|
|
|
|
if (cylinder_nodata(cyl))
|
|
|
|
break;
|
2017-10-08 03:14:57 +00:00
|
|
|
/* Check if we have the same gasmix two or more times
|
|
|
|
* If yes return more verbose string */
|
|
|
|
int same_gas = same_gasmix_cylinder(cyl, i, &displayed_dive, true);
|
|
|
|
if (same_gas == -1)
|
|
|
|
list.push_back(get_gas_string(cyl->gasmix));
|
|
|
|
else
|
|
|
|
list.push_back(get_gas_string(cyl->gasmix) + QString(" (%1 %2 ").arg(tr("cyl.")).arg(i + 1) +
|
|
|
|
cyl->type.description + ")");
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::removeDeco()
|
|
|
|
{
|
|
|
|
bool oldrec = setRecalc(false);
|
|
|
|
QVector<int> computedPoints;
|
|
|
|
for (int i = 0; i < rowCount(); i++)
|
|
|
|
if (!at(i).entered)
|
|
|
|
computedPoints.push_back(i);
|
|
|
|
removeSelectedPoints(computedPoints);
|
|
|
|
setRecalc(oldrec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::addCylinder_clicked()
|
|
|
|
{
|
|
|
|
CylindersModel::instance()->add();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setPlanMode(Mode m)
|
|
|
|
{
|
|
|
|
mode = m;
|
|
|
|
// the planner may reset our GF settings that are used to show deco
|
|
|
|
// reset them to what's in the preferences
|
2016-09-24 08:02:08 +00:00
|
|
|
if (m != PLAN) {
|
2017-09-19 12:38:38 +00:00
|
|
|
set_gf(prefs.gflow, prefs.gfhigh);
|
2016-09-24 08:02:08 +00:00
|
|
|
set_vpmb_conservatism(prefs.vpmb_conservatism);
|
|
|
|
}
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DivePlannerPointsModel::isPlanner()
|
|
|
|
{
|
|
|
|
return mode == PLAN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When the planner adds deco stops to the model, adding those should not trigger a new deco calculation.
|
|
|
|
* We thus start the planner only when recalc is true. */
|
|
|
|
|
|
|
|
bool DivePlannerPointsModel::setRecalc(bool rec)
|
|
|
|
{
|
|
|
|
bool old = recalc;
|
|
|
|
recalc = rec;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DivePlannerPointsModel::recalcQ()
|
|
|
|
{
|
|
|
|
return recalc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DivePlannerPointsModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
2016-03-09 18:16:27 +00:00
|
|
|
Q_UNUSED(parent);
|
2015-05-28 19:23:49 +00:00
|
|
|
return COLUMNS; // to disable CCSETPOINT subtract one
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DivePlannerPointsModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
divedatapoint p = divepoints.at(index.row());
|
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
|
|
|
switch (index.column()) {
|
|
|
|
case CCSETPOINT:
|
|
|
|
return (double)p.setpoint / 1000;
|
|
|
|
case DEPTH:
|
2017-03-11 16:41:41 +00:00
|
|
|
return (int) lrint(get_depth_units(p.depth.mm, NULL, NULL));
|
2015-05-28 19:23:49 +00:00
|
|
|
case RUNTIME:
|
|
|
|
return p.time / 60;
|
|
|
|
case DURATION:
|
|
|
|
if (index.row())
|
|
|
|
return (p.time - divepoints.at(index.row() - 1).time) / 60;
|
|
|
|
else
|
|
|
|
return p.time / 60;
|
|
|
|
case GAS:
|
2017-10-08 03:14:57 +00:00
|
|
|
/* Check if we have the same gasmix two or more times
|
|
|
|
* If yes return more verbose string */
|
|
|
|
int same_gas = same_gasmix_cylinder(&displayed_dive.cylinder[p.cylinderid], p.cylinderid, &displayed_dive, true);
|
|
|
|
if (same_gas == -1)
|
|
|
|
return get_gas_string(displayed_dive.cylinder[p.cylinderid].gasmix);
|
|
|
|
else
|
|
|
|
return get_gas_string(displayed_dive.cylinder[p.cylinderid].gasmix) +
|
|
|
|
QString(" (%1 %2 ").arg(tr("cyl.")).arg(p.cylinderid + 1) +
|
|
|
|
displayed_dive.cylinder[p.cylinderid].type.description + ")";
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
} else if (role == Qt::DecorationRole) {
|
|
|
|
switch (index.column()) {
|
|
|
|
case REMOVE:
|
|
|
|
if (rowCount() > 1)
|
|
|
|
return p.entered ? trashIcon() : QVariant();
|
2015-09-17 22:27:49 +00:00
|
|
|
else
|
|
|
|
return trashForbiddenIcon();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
} else if (role == Qt::SizeHintRole) {
|
|
|
|
switch (index.column()) {
|
|
|
|
case REMOVE:
|
|
|
|
if (rowCount() > 1)
|
|
|
|
return p.entered ? trashIcon().size() : QVariant();
|
2015-09-17 22:27:49 +00:00
|
|
|
else
|
|
|
|
return trashForbiddenIcon().size();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
} else if (role == Qt::FontRole) {
|
|
|
|
if (divepoints.at(index.row()).entered) {
|
|
|
|
return defaultModelFont();
|
|
|
|
} else {
|
|
|
|
QFont font = defaultModelFont();
|
|
|
|
font.setBold(true);
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DivePlannerPointsModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
int i, shift;
|
|
|
|
if (role == Qt::EditRole) {
|
|
|
|
divedatapoint &p = divepoints[index.row()];
|
|
|
|
switch (index.column()) {
|
|
|
|
case DEPTH:
|
2016-07-06 12:40:35 +00:00
|
|
|
if (value.toInt() >= 0) {
|
2017-03-10 12:37:54 +00:00
|
|
|
p.depth = units_to_depth(value.toInt());
|
2016-07-06 12:40:35 +00:00
|
|
|
if (updateMaxDepth())
|
|
|
|
CylindersModel::instance()->updateBestMixes();
|
|
|
|
}
|
2015-05-28 19:23:49 +00:00
|
|
|
break;
|
|
|
|
case RUNTIME:
|
|
|
|
p.time = value.toInt() * 60;
|
|
|
|
break;
|
|
|
|
case DURATION:
|
|
|
|
i = index.row();
|
|
|
|
if (i)
|
|
|
|
shift = divepoints[i].time - divepoints[i - 1].time - value.toInt() * 60;
|
|
|
|
else
|
|
|
|
shift = divepoints[i].time - value.toInt() * 60;
|
|
|
|
while (i < divepoints.size())
|
|
|
|
divepoints[i++].time -= shift;
|
|
|
|
break;
|
|
|
|
case CCSETPOINT: {
|
|
|
|
int po2 = 0;
|
|
|
|
QByteArray gasv = value.toByteArray();
|
|
|
|
if (validate_po2(gasv.data(), &po2))
|
|
|
|
p.setpoint = po2;
|
|
|
|
} break;
|
|
|
|
case GAS:
|
2016-07-06 12:40:28 +00:00
|
|
|
if (value.toInt() >= 0 && value.toInt() < MAX_CYLINDERS)
|
|
|
|
p.cylinderid = value.toInt();
|
2017-11-18 22:59:22 +00:00
|
|
|
/* Did we change the start (dp 0) cylinder to another cylinderid than 0? */
|
|
|
|
if (value.toInt() != 0 && index.row() == 0)
|
|
|
|
CylindersModel::instance()->moveAtFirst(value.toInt());
|
2017-03-13 07:25:32 +00:00
|
|
|
CylindersModel::instance()->updateTrashIcon();
|
2015-05-28 19:23:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
editStop(index.row(), p);
|
|
|
|
}
|
|
|
|
return QAbstractItemModel::setData(index, value, role);
|
|
|
|
}
|
|
|
|
|
2017-10-11 20:03:03 +00:00
|
|
|
void DivePlannerPointsModel::gasChange(const QModelIndex &index, int newcylinderid)
|
2015-05-28 19:23:49 +00:00
|
|
|
{
|
2016-07-06 12:40:28 +00:00
|
|
|
int i = index.row(), oldcylinderid = divepoints[i].cylinderid;
|
|
|
|
while (i < rowCount() && oldcylinderid == divepoints[i].cylinderid)
|
|
|
|
divepoints[i++].cylinderid = newcylinderid;
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 19:27:06 +00:00
|
|
|
void DivePlannerPointsModel::cylinderRenumber(int mapping[])
|
|
|
|
{
|
|
|
|
for (int i = 0; i < rowCount(); i++)
|
|
|
|
divepoints[i].cylinderid = mapping[divepoints[i].cylinderid];
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
|
|
|
|
2015-05-28 19:23:49 +00:00
|
|
|
QVariant DivePlannerPointsModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
|
|
|
|
switch (section) {
|
|
|
|
case DEPTH:
|
|
|
|
return tr("Final depth");
|
|
|
|
case RUNTIME:
|
|
|
|
return tr("Run time");
|
|
|
|
case DURATION:
|
|
|
|
return tr("Duration");
|
|
|
|
case GAS:
|
|
|
|
return tr("Used gas");
|
|
|
|
case CCSETPOINT:
|
2015-09-18 14:28:01 +00:00
|
|
|
return tr("CC setpoint");
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
} else if (role == Qt::FontRole) {
|
|
|
|
return defaultModelFont();
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags DivePlannerPointsModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (index.column() != REMOVE)
|
|
|
|
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
|
|
|
|
else
|
|
|
|
return QAbstractItemModel::flags(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DivePlannerPointsModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2016-03-08 05:26:44 +00:00
|
|
|
Q_UNUSED(parent);
|
2015-05-28 19:23:49 +00:00
|
|
|
return divepoints.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
DivePlannerPointsModel::DivePlannerPointsModel(QObject *parent) : QAbstractTableModel(parent),
|
|
|
|
mode(NOTHING),
|
|
|
|
recalc(false),
|
|
|
|
tempGFHigh(100),
|
|
|
|
tempGFLow(100)
|
|
|
|
{
|
|
|
|
memset(&diveplan, 0, sizeof(diveplan));
|
2016-06-22 20:46:22 +00:00
|
|
|
startTime.setTimeSpec(Qt::UTC);
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DivePlannerPointsModel *DivePlannerPointsModel::instance()
|
|
|
|
{
|
|
|
|
static QScopedPointer<DivePlannerPointsModel> self(new DivePlannerPointsModel());
|
|
|
|
return self.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::emitDataChanged()
|
|
|
|
{
|
|
|
|
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setBottomSac(double sac)
|
|
|
|
{
|
|
|
|
diveplan.bottomsac = units_to_sac(sac);
|
2017-01-06 10:10:21 +00:00
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setBottomSac(diveplan.bottomsac);
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setDecoSac(double sac)
|
|
|
|
{
|
|
|
|
diveplan.decosac = units_to_sac(sac);
|
2017-01-06 10:10:21 +00:00
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setDecoSac(diveplan.decosac);
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 19:24:18 +00:00
|
|
|
void DivePlannerPointsModel::setSacFactor(double factor)
|
|
|
|
{
|
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setSacFactor((int) round(factor * 100));
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setProblemSolvingTime(int minutes)
|
|
|
|
{
|
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setProblemSolvingTime(minutes);
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
|
|
|
|
2015-05-28 19:23:49 +00:00
|
|
|
void DivePlannerPointsModel::setGFHigh(const int gfhigh)
|
|
|
|
{
|
|
|
|
tempGFHigh = gfhigh;
|
|
|
|
// GFHigh <= 34 can cause infinite deco at 6m - don't trigger a recalculation
|
|
|
|
// for smaller GFHigh unless the user explicitly leaves the field
|
|
|
|
if (tempGFHigh > 34)
|
|
|
|
triggerGFHigh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::triggerGFHigh()
|
|
|
|
{
|
|
|
|
if (diveplan.gfhigh != tempGFHigh) {
|
|
|
|
diveplan.gfhigh = tempGFHigh;
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setGFLow(const int ghflow)
|
|
|
|
{
|
|
|
|
tempGFLow = ghflow;
|
|
|
|
triggerGFLow();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setRebreatherMode(int mode)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
displayed_dive.dc.divemode = (dive_comp_type) mode;
|
|
|
|
for (i=0; i < rowCount(); i++)
|
|
|
|
divepoints[i].setpoint = mode == CCR ? prefs.defaultsetpoint : 0;
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::triggerGFLow()
|
|
|
|
{
|
|
|
|
if (diveplan.gflow != tempGFLow) {
|
|
|
|
diveplan.gflow = tempGFLow;
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-24 08:02:07 +00:00
|
|
|
void DivePlannerPointsModel::setVpmbConservatism(int level)
|
2015-08-15 13:16:51 +00:00
|
|
|
{
|
2016-09-24 08:02:08 +00:00
|
|
|
if (diveplan.vpmb_conservatism != level) {
|
|
|
|
diveplan.vpmb_conservatism = level;
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
2015-08-15 13:16:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 19:23:49 +00:00
|
|
|
void DivePlannerPointsModel::setSurfacePressure(int pressure)
|
|
|
|
{
|
|
|
|
diveplan.surface_pressure = pressure;
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setSalinity(int salinity)
|
|
|
|
{
|
|
|
|
diveplan.salinity = salinity;
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DivePlannerPointsModel::getSurfacePressure()
|
|
|
|
{
|
|
|
|
return diveplan.surface_pressure;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setLastStop6m(bool value)
|
|
|
|
{
|
2017-01-06 10:10:21 +00:00
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setLastStop(value);
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 08:06:15 +00:00
|
|
|
void DivePlannerPointsModel::setAscrate75(int rate)
|
|
|
|
{
|
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setAscrate75(lrint(rate * UNIT_FACTOR));
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setAscrate50(int rate)
|
|
|
|
{
|
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setAscrate50(lrint(rate * UNIT_FACTOR));
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setAscratestops(int rate)
|
|
|
|
{
|
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setAscratestops(lrint(rate * UNIT_FACTOR));
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setAscratelast6m(int rate)
|
|
|
|
{
|
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setAscratelast6m(lrint(rate * UNIT_FACTOR));
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setDescrate(int rate)
|
|
|
|
{
|
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setDescrate(lrint(rate * UNIT_FACTOR));
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
|
|
|
|
2015-05-28 19:23:49 +00:00
|
|
|
void DivePlannerPointsModel::setVerbatim(bool value)
|
|
|
|
{
|
2017-01-06 10:10:21 +00:00
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setVerbatimPlan(value);
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setDisplayRuntime(bool value)
|
|
|
|
{
|
2017-01-06 10:10:21 +00:00
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setDisplayRuntime(value);
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setDisplayDuration(bool value)
|
|
|
|
{
|
2017-01-06 10:10:21 +00:00
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setDisplayDuration(value);
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setDisplayTransitions(bool value)
|
|
|
|
{
|
2017-01-06 10:10:21 +00:00
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setDisplayTransitions(value);
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 14:10:47 +00:00
|
|
|
void DivePlannerPointsModel::setDisplayVariations(bool value)
|
|
|
|
{
|
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setDisplayVariations(value);
|
|
|
|
emitDataChanged();
|
|
|
|
}
|
|
|
|
|
2015-07-03 21:07:58 +00:00
|
|
|
void DivePlannerPointsModel::setDecoMode(int mode)
|
2015-05-28 19:23:49 +00:00
|
|
|
{
|
2017-01-06 10:10:21 +00:00
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setDecoMode(deco_mode(mode));
|
2017-01-07 02:11:19 +00:00
|
|
|
emit recreationChanged(mode == int(prefs.planner_deco_mode));
|
2015-05-28 19:23:49 +00:00
|
|
|
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS -1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setSafetyStop(bool value)
|
|
|
|
{
|
2017-01-06 10:10:21 +00:00
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setSafetyStop(value);
|
2015-05-28 19:23:49 +00:00
|
|
|
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS -1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setReserveGas(int reserve)
|
|
|
|
{
|
2017-01-06 10:10:21 +00:00
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
2016-03-22 22:44:59 +00:00
|
|
|
if (prefs.units.pressure == units::BAR)
|
2017-01-06 10:10:21 +00:00
|
|
|
planner->setReserveGas(reserve * 1000);
|
2016-03-22 22:44:59 +00:00
|
|
|
else
|
2017-01-06 10:10:21 +00:00
|
|
|
planner->setReserveGas(psi_to_mbar(reserve));
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setDropStoneMode(bool value)
|
|
|
|
{
|
2017-01-06 10:10:21 +00:00
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setDropStoneMode(value);
|
2015-05-28 19:23:49 +00:00
|
|
|
if (prefs.drop_stone_mode) {
|
|
|
|
/* Remove the first entry if we enable drop_stone_mode */
|
|
|
|
if (rowCount() >= 2) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, 0);
|
|
|
|
divepoints.remove(0);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Add a first entry if we disable drop_stone_mode */
|
|
|
|
beginInsertRows(QModelIndex(), 0, 0);
|
|
|
|
/* Copy the first current point */
|
|
|
|
divedatapoint p = divepoints.at(0);
|
2017-03-10 12:37:54 +00:00
|
|
|
p.time = p.depth.mm / prefs.descrate;
|
2015-05-28 19:23:49 +00:00
|
|
|
divepoints.push_front(p);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 11:48:42 +00:00
|
|
|
void DivePlannerPointsModel::setSwitchAtReqStop(bool value)
|
|
|
|
{
|
2017-01-06 10:10:21 +00:00
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setSwitchAtRequiredStop(value);
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-06-22 11:48:42 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 10:25:03 +00:00
|
|
|
void DivePlannerPointsModel::setMinSwitchDuration(int duration)
|
|
|
|
{
|
2017-01-06 10:10:21 +00:00
|
|
|
auto planner = SettingsObjectWrapper::instance()->planner_settings;
|
|
|
|
planner->setMinSwitchDuration(duration * 60);
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-06-19 10:25:03 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 19:23:49 +00:00
|
|
|
void DivePlannerPointsModel::setStartDate(const QDate &date)
|
|
|
|
{
|
|
|
|
startTime.setDate(date);
|
|
|
|
diveplan.when = startTime.toTime_t();
|
|
|
|
displayed_dive.when = diveplan.when;
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::setStartTime(const QTime &t)
|
|
|
|
{
|
|
|
|
startTime.setTime(t);
|
2016-06-22 20:46:22 +00:00
|
|
|
diveplan.when = startTime.toTime_t();
|
2015-05-28 19:23:49 +00:00
|
|
|
displayed_dive.when = diveplan.when;
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool divePointsLessThan(const divedatapoint &p1, const divedatapoint &p2)
|
|
|
|
{
|
|
|
|
return p1.time <= p2.time;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DivePlannerPointsModel::lastEnteredPoint()
|
|
|
|
{
|
|
|
|
for (int i = divepoints.count() - 1; i >= 0; i--)
|
|
|
|
if (divepoints.at(i).entered)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-02-04 13:12:43 +00:00
|
|
|
// cylinderid_in == -1 means same gas as before.
|
2016-07-06 12:40:28 +00:00
|
|
|
int DivePlannerPointsModel::addStop(int milimeters, int seconds, int cylinderid_in, int ccpoint, bool entered)
|
2015-05-28 19:23:49 +00:00
|
|
|
{
|
2016-07-09 19:37:05 +00:00
|
|
|
int cylinderid = 0;
|
2015-05-28 19:23:49 +00:00
|
|
|
bool usePrevious = false;
|
2017-02-04 13:12:43 +00:00
|
|
|
if (cylinderid_in >= 0)
|
2016-07-06 12:40:28 +00:00
|
|
|
cylinderid = cylinderid_in;
|
2015-05-28 19:23:49 +00:00
|
|
|
else
|
|
|
|
usePrevious = true;
|
|
|
|
if (recalcQ())
|
|
|
|
removeDeco();
|
|
|
|
|
|
|
|
int row = divepoints.count();
|
|
|
|
if (seconds == 0 && milimeters == 0 && row != 0) {
|
|
|
|
/* this is only possible if the user clicked on the 'plus' sign on the DivePoints Table */
|
|
|
|
const divedatapoint t = divepoints.at(lastEnteredPoint());
|
2017-03-10 12:37:54 +00:00
|
|
|
milimeters = t.depth.mm;
|
2015-05-28 19:23:49 +00:00
|
|
|
seconds = t.time + 600; // 10 minutes.
|
2016-07-06 12:40:28 +00:00
|
|
|
cylinderid = t.cylinderid;
|
2015-05-28 19:23:49 +00:00
|
|
|
ccpoint = t.setpoint;
|
|
|
|
} else if (seconds == 0 && milimeters == 0 && row == 0) {
|
|
|
|
milimeters = M_OR_FT(5, 15); // 5m / 15ft
|
|
|
|
seconds = 600; // 10 min
|
2016-07-06 12:40:28 +00:00
|
|
|
// Default to the first cylinder
|
|
|
|
cylinderid = 0;
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if there's already a new stop before this one:
|
|
|
|
for (int i = 0; i < row; i++) {
|
|
|
|
const divedatapoint &dp = divepoints.at(i);
|
|
|
|
if (dp.time == seconds) {
|
|
|
|
row = i;
|
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
divepoints.remove(row);
|
|
|
|
endRemoveRows();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (dp.time > seconds) {
|
|
|
|
row = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Previous, actually means next as we are typically subdiving a segment and the gas for
|
|
|
|
// the segment is determined by the waypoint at the end.
|
|
|
|
if (usePrevious) {
|
|
|
|
if (row < divepoints.count()) {
|
2016-07-06 12:40:28 +00:00
|
|
|
cylinderid = divepoints.at(row).cylinderid;
|
2015-05-28 19:23:49 +00:00
|
|
|
} else if (row > 0) {
|
2016-07-06 12:40:28 +00:00
|
|
|
cylinderid = divepoints.at(row - 1).cylinderid;
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the new stop
|
|
|
|
beginInsertRows(QModelIndex(), row, row);
|
|
|
|
divedatapoint point;
|
2017-03-10 12:37:54 +00:00
|
|
|
point.depth.mm = milimeters;
|
2015-05-28 19:23:49 +00:00
|
|
|
point.time = seconds;
|
2016-07-06 12:40:28 +00:00
|
|
|
point.cylinderid = cylinderid;
|
2015-05-28 19:23:49 +00:00
|
|
|
point.setpoint = ccpoint;
|
|
|
|
point.entered = entered;
|
|
|
|
point.next = NULL;
|
|
|
|
divepoints.append(point);
|
|
|
|
std::sort(divepoints.begin(), divepoints.end(), divePointsLessThan);
|
|
|
|
endInsertRows();
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::editStop(int row, divedatapoint newData)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* When moving divepoints rigorously, we might end up with index
|
|
|
|
* out of range, thus returning the last one instead.
|
|
|
|
*/
|
2017-10-18 07:02:28 +00:00
|
|
|
int old_first_cylid = divepoints[0].cylinderid;
|
2015-05-28 19:23:49 +00:00
|
|
|
if (row >= divepoints.count())
|
|
|
|
return;
|
|
|
|
divepoints[row] = newData;
|
|
|
|
std::sort(divepoints.begin(), divepoints.end(), divePointsLessThan);
|
2016-07-06 12:40:38 +00:00
|
|
|
if (updateMaxDepth())
|
|
|
|
CylindersModel::instance()->updateBestMixes();
|
2017-10-18 07:02:28 +00:00
|
|
|
if (divepoints[0].cylinderid != old_first_cylid)
|
|
|
|
CylindersModel::instance()->moveAtFirst(divepoints[0].cylinderid);
|
2017-02-15 22:15:08 +00:00
|
|
|
emitDataChanged();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DivePlannerPointsModel::size()
|
|
|
|
{
|
|
|
|
return divepoints.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
divedatapoint DivePlannerPointsModel::at(int row)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* When moving divepoints rigorously, we might end up with index
|
|
|
|
* out of range, thus returning the last one instead.
|
|
|
|
*/
|
|
|
|
if (row >= divepoints.count())
|
|
|
|
return divepoints.at(divepoints.count() - 1);
|
|
|
|
return divepoints.at(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::remove(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
if (index.column() != REMOVE || rowCount() == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
divedatapoint dp = at(index.row());
|
|
|
|
if (!dp.entered)
|
|
|
|
return;
|
|
|
|
|
2017-10-18 07:02:28 +00:00
|
|
|
int old_first_cylid = divepoints[0].cylinderid;
|
|
|
|
|
2015-05-28 19:23:49 +00:00
|
|
|
/* TODO: this seems so wrong.
|
|
|
|
* We can't do this here if we plan to use QML on mobile
|
|
|
|
* as mobile has no ControlModifier.
|
|
|
|
* The correct thing to do is to create a new method
|
|
|
|
* remove method that will pass the first and last index of the
|
|
|
|
* removed rows, and remove those in a go.
|
|
|
|
*/
|
2017-01-23 17:11:52 +00:00
|
|
|
int i;
|
|
|
|
int rows = rowCount();
|
|
|
|
if (QApplication::keyboardModifiers() & Qt::ControlModifier) {
|
|
|
|
beginRemoveRows(QModelIndex(), index.row(), rows - 1);
|
|
|
|
for (i = rows - 1; i >= index.row(); i--)
|
|
|
|
divepoints.remove(i);
|
|
|
|
} else {
|
2015-05-28 19:23:49 +00:00
|
|
|
beginRemoveRows(QModelIndex(), index.row(), index.row());
|
|
|
|
divepoints.remove(index.row());
|
2017-01-23 17:11:52 +00:00
|
|
|
}
|
2015-05-28 19:23:49 +00:00
|
|
|
endRemoveRows();
|
2017-03-13 07:25:32 +00:00
|
|
|
CylindersModel::instance()->updateTrashIcon();
|
2017-10-18 07:02:28 +00:00
|
|
|
if (divepoints[0].cylinderid != old_first_cylid)
|
|
|
|
CylindersModel::instance()->moveAtFirst(divepoints[0].cylinderid);
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct diveplan &DivePlannerPointsModel::getDiveplan()
|
|
|
|
{
|
|
|
|
return diveplan;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::cancelPlan()
|
|
|
|
{
|
|
|
|
/* TODO:
|
|
|
|
* This check shouldn't be here - this is the interface responsability.
|
|
|
|
* as soon as the interface thinks that it could cancel the plan, this should be
|
|
|
|
* called.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (mode == PLAN && rowCount()) {
|
|
|
|
if (QMessageBox::warning(MainWindow::instance(), TITLE_OR_TEXT(tr("Discard the plan?"),
|
|
|
|
tr("You are about to discard your plan.")),
|
|
|
|
QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Discard) != QMessageBox::Discard) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
setPlanMode(NOTHING);
|
|
|
|
free_dps(&diveplan);
|
|
|
|
|
|
|
|
emit planCanceled();
|
|
|
|
}
|
|
|
|
|
|
|
|
DivePlannerPointsModel::Mode DivePlannerPointsModel::currentMode() const
|
|
|
|
{
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2016-07-06 12:40:28 +00:00
|
|
|
bool DivePlannerPointsModel::tankInUse(int cylinderid)
|
2015-05-28 19:23:49 +00:00
|
|
|
{
|
|
|
|
for (int j = 0; j < rowCount(); j++) {
|
|
|
|
divedatapoint &p = divepoints[j];
|
|
|
|
if (p.time == 0) // special entries that hold the available gases
|
|
|
|
continue;
|
|
|
|
if (!p.entered) // removing deco gases is ok
|
|
|
|
continue;
|
2016-07-06 12:40:28 +00:00
|
|
|
if (p.cylinderid == cylinderid) // tank is in use
|
2015-05-28 19:23:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::clear()
|
|
|
|
{
|
|
|
|
bool oldRecalc = setRecalc(false);
|
|
|
|
|
|
|
|
CylindersModel::instance()->updateDive();
|
|
|
|
if (rowCount() > 0) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
|
|
|
|
divepoints.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
CylindersModel::instance()->clear();
|
|
|
|
setRecalc(oldRecalc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::createTemporaryPlan()
|
|
|
|
{
|
|
|
|
// Get the user-input and calculate the dive info
|
|
|
|
free_dps(&diveplan);
|
|
|
|
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;
|
2017-02-26 04:07:43 +00:00
|
|
|
if (i == 0 && mode == PLAN && prefs.drop_stone_mode) {
|
2016-07-06 12:40:28 +00:00
|
|
|
/* Okay, we add a first segment where we go down to depth */
|
2017-03-10 12:37:54 +00:00
|
|
|
plan_add_segment(&diveplan, p.depth.mm / prefs.descrate, p.depth.mm, p.cylinderid, p.setpoint, true);
|
|
|
|
deltaT -= p.depth.mm / prefs.descrate;
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
if (p.entered)
|
2017-03-10 12:37:54 +00:00
|
|
|
plan_add_segment(&diveplan, deltaT, p.depth.mm, p.cylinderid, p.setpoint, true);
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// what does the cache do???
|
2017-05-25 22:45:53 +00:00
|
|
|
struct deco_state *cache = NULL;
|
2015-05-28 19:23:49 +00:00
|
|
|
struct divedatapoint *dp = NULL;
|
|
|
|
for (int i = 0; i < MAX_CYLINDERS; i++) {
|
|
|
|
cylinder_t *cyl = &displayed_dive.cylinder[i];
|
2017-01-23 16:35:27 +00:00
|
|
|
if (cyl->depth.mm && cyl->cylinder_use != NOT_USED) {
|
2016-07-06 12:40:28 +00:00
|
|
|
dp = create_dp(0, cyl->depth.mm, i, 0);
|
2015-05-28 19:23:49 +00:00
|
|
|
if (diveplan.dp) {
|
|
|
|
dp->next = diveplan.dp;
|
|
|
|
diveplan.dp = dp;
|
|
|
|
} else {
|
|
|
|
dp->next = NULL;
|
|
|
|
diveplan.dp = dp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if DEBUG_PLAN
|
|
|
|
dump_plan(&diveplan);
|
|
|
|
#endif
|
|
|
|
if (recalcQ() && !diveplan_empty(&diveplan)) {
|
2017-08-27 20:49:41 +00:00
|
|
|
struct decostop stoptable[60];
|
2017-11-22 19:42:33 +00:00
|
|
|
struct deco_state plan_deco_state;
|
|
|
|
plan(&plan_deco_state, &diveplan, &displayed_dive, DECOTIMESTEP, stoptable, &cache, isPlanner(), false);
|
|
|
|
//QtConcurrent::run(this, &DivePlannerPointsModel::computeVariations, &ds_after_previous_dives);
|
2017-11-24 13:17:01 +00:00
|
|
|
final_deco_state = plan_deco_state;
|
2015-06-16 12:37:02 +00:00
|
|
|
emit calculatedPlanNotes();
|
2015-05-28 19:23:49 +00:00
|
|
|
}
|
|
|
|
// throw away the cache
|
|
|
|
free(cache);
|
|
|
|
#if DEBUG_PLAN
|
|
|
|
save_dive(stderr, &displayed_dive);
|
|
|
|
dump_plan(&diveplan);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::deleteTemporaryPlan()
|
|
|
|
{
|
|
|
|
free_dps(&diveplan);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::savePlan()
|
|
|
|
{
|
|
|
|
createPlan(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePlannerPointsModel::saveDuplicatePlan()
|
|
|
|
{
|
|
|
|
createPlan(true);
|
|
|
|
}
|
|
|
|
|
2017-08-28 21:59:58 +00:00
|
|
|
struct divedatapoint * DivePlannerPointsModel::cloneDiveplan(struct diveplan *plan_copy)
|
|
|
|
{
|
|
|
|
divedatapoint *src, *last_segment;
|
|
|
|
divedatapoint **dp;
|
|
|
|
|
2017-11-22 19:42:33 +00:00
|
|
|
lock_planner();
|
2017-08-28 21:59:58 +00:00
|
|
|
src = diveplan.dp;
|
|
|
|
*plan_copy = diveplan;
|
|
|
|
dp = &plan_copy->dp;
|
|
|
|
while (src && (!src->time || src->entered)) {
|
|
|
|
*dp = (struct divedatapoint *)malloc(sizeof(struct divedatapoint));
|
|
|
|
**dp = *src;
|
|
|
|
dp = &(*dp)->next;
|
|
|
|
src = src->next;
|
|
|
|
}
|
|
|
|
(*dp) = NULL;
|
|
|
|
|
|
|
|
last_segment = plan_copy->dp;
|
2017-11-22 19:42:33 +00:00
|
|
|
while (last_segment && last_segment->next && last_segment->next->next)
|
2017-08-28 21:59:58 +00:00
|
|
|
last_segment = last_segment->next;
|
2017-11-22 19:42:33 +00:00
|
|
|
unlock_planner();
|
2017-08-28 21:59:58 +00:00
|
|
|
return last_segment;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:26:00 +00:00
|
|
|
int DivePlannerPointsModel::analyzeVariations(struct decostop *min, struct decostop *mid, struct decostop *max, const char *unit)
|
2017-08-28 21:59:58 +00:00
|
|
|
{
|
|
|
|
int leftsum = 0;
|
|
|
|
int rightsum = 0;
|
|
|
|
while (mid->depth > min->depth)
|
|
|
|
++mid;
|
|
|
|
while (max->depth > mid->depth)
|
|
|
|
++max;
|
|
|
|
|
|
|
|
while (mid->depth) {
|
|
|
|
int left = mid->time - min->time;
|
|
|
|
leftsum += left;
|
|
|
|
int right = max->time - mid->time;
|
|
|
|
rightsum += right;
|
2017-08-29 10:26:00 +00:00
|
|
|
#ifdef SHOWSTOPVARIATIONS
|
2017-08-28 21:59:58 +00:00
|
|
|
if (min->time + mid->time + max->time)
|
|
|
|
printf("%dm: %dmin + %ds/%s +- %ds/%s\n", mid->depth / 1000,
|
|
|
|
(mid->time + 1)/60,
|
|
|
|
(left + right) / 2, unit,
|
|
|
|
(right - left) / 2, unit);
|
2017-10-20 21:25:18 +00:00
|
|
|
#else
|
|
|
|
(void) unit;
|
2017-08-29 10:26:00 +00:00
|
|
|
#endif
|
2017-08-28 21:59:58 +00:00
|
|
|
++min;
|
|
|
|
++mid;
|
|
|
|
++max;
|
|
|
|
}
|
2017-08-29 10:26:00 +00:00
|
|
|
#ifdef SHOWSTOPVARIATIONS
|
|
|
|
printf("Total + %d:%02d/%s +- %d s/%s\n\n", FRACTION((leftsum + rightsum) / 2, 60), unit,
|
|
|
|
(rightsum - leftsum) / 2, unit);
|
|
|
|
#endif
|
|
|
|
return (leftsum + rightsum) / 2;
|
2017-08-28 21:59:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 19:42:33 +00:00
|
|
|
void DivePlannerPointsModel::computeVariations(struct deco_state *ds)
|
2017-08-28 21:59:58 +00:00
|
|
|
{
|
2017-11-22 19:42:33 +00:00
|
|
|
return;
|
2017-08-28 21:59:58 +00:00
|
|
|
bool oldRecalc = setRecalc(false);
|
|
|
|
struct dive *dive = alloc_dive();
|
|
|
|
copy_dive(&displayed_dive, dive);
|
|
|
|
struct decostop original[60], deeper[60], shallower[60], shorter[60], longer[60];
|
|
|
|
struct deco_state *cache = NULL, *save = NULL;
|
|
|
|
struct diveplan plan_copy;
|
|
|
|
struct divedatapoint *last_segment;
|
|
|
|
|
2017-09-18 14:10:47 +00:00
|
|
|
if(in_planner() && prefs.display_variations) {
|
2017-08-29 09:41:30 +00:00
|
|
|
int my_instance = ++instanceCounter;
|
2017-11-22 19:42:33 +00:00
|
|
|
cache_deco_state(ds, &save);
|
2017-09-18 14:10:47 +00:00
|
|
|
cloneDiveplan(&plan_copy);
|
2017-08-29 09:41:30 +00:00
|
|
|
if (my_instance != instanceCounter)
|
|
|
|
return;
|
2017-11-22 19:42:33 +00:00
|
|
|
plan(ds, &plan_copy, dive, 1, original, &cache, true, false);
|
2017-09-18 14:10:47 +00:00
|
|
|
free_dps(&plan_copy);
|
2017-11-22 19:42:33 +00:00
|
|
|
restore_deco_state(save, ds, false);
|
2017-09-18 14:10:47 +00:00
|
|
|
|
|
|
|
last_segment = cloneDiveplan(&plan_copy);
|
|
|
|
last_segment->depth.mm += 1000;
|
|
|
|
last_segment->next->depth.mm += 1000;
|
2017-08-29 09:41:30 +00:00
|
|
|
if (my_instance != instanceCounter)
|
|
|
|
return;
|
2017-11-22 19:42:33 +00:00
|
|
|
plan(ds, &plan_copy, dive, 1, deeper, &cache, true, false);
|
2017-09-18 14:10:47 +00:00
|
|
|
free_dps(&plan_copy);
|
2017-11-22 19:42:33 +00:00
|
|
|
restore_deco_state(save, ds, false);
|
2017-09-18 14:10:47 +00:00
|
|
|
|
|
|
|
last_segment = cloneDiveplan(&plan_copy);
|
|
|
|
last_segment->depth.mm -= 1000;
|
|
|
|
last_segment->next->depth.mm -= 1000;
|
2017-08-29 09:41:30 +00:00
|
|
|
if (my_instance != instanceCounter)
|
|
|
|
return;
|
2017-11-22 19:42:33 +00:00
|
|
|
plan(ds, &plan_copy, dive, 1, shallower, &cache, true, false);
|
2017-09-18 14:10:47 +00:00
|
|
|
free_dps(&plan_copy);
|
2017-11-22 19:42:33 +00:00
|
|
|
restore_deco_state(save, ds, false);
|
2017-09-18 14:10:47 +00:00
|
|
|
|
|
|
|
last_segment = cloneDiveplan(&plan_copy);
|
|
|
|
last_segment->next->time += 60;
|
2017-08-29 09:41:30 +00:00
|
|
|
if (my_instance != instanceCounter)
|
|
|
|
return;
|
2017-11-22 19:42:33 +00:00
|
|
|
plan(ds, &plan_copy, dive, 1, longer, &cache, true, false);
|
2017-09-18 14:10:47 +00:00
|
|
|
free_dps(&plan_copy);
|
2017-11-22 19:42:33 +00:00
|
|
|
restore_deco_state(save, ds, false);
|
2017-09-18 14:10:47 +00:00
|
|
|
|
|
|
|
last_segment = cloneDiveplan(&plan_copy);
|
|
|
|
last_segment->next->time -= 60;
|
2017-08-29 09:41:30 +00:00
|
|
|
if (my_instance != instanceCounter)
|
|
|
|
return;
|
2017-11-22 19:42:33 +00:00
|
|
|
plan(ds, &plan_copy, dive, 1, shorter, &cache, true, false);
|
2017-09-18 14:10:47 +00:00
|
|
|
free_dps(&plan_copy);
|
2017-11-22 19:42:33 +00:00
|
|
|
restore_deco_state(save, ds, false);
|
2017-08-29 10:26:00 +00:00
|
|
|
#ifdef SHOWSTOPVARIATIONS
|
2017-09-18 14:10:47 +00:00
|
|
|
printf("\n\n");
|
2017-08-29 10:26:00 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-18 14:10:47 +00:00
|
|
|
QString notes(displayed_dive.notes);
|
|
|
|
free(displayed_dive.notes);
|
2017-08-29 10:26:00 +00:00
|
|
|
|
2017-09-18 14:10:47 +00:00
|
|
|
char buf[200];
|
|
|
|
sprintf(buf, "+ %d:%02d /m + %d:%02d /min", FRACTION(analyzeVariations(shallower, original, deeper, "m"),60),
|
|
|
|
FRACTION(analyzeVariations(shorter, original, longer, "min"), 60));
|
2017-08-29 10:26:00 +00:00
|
|
|
|
2017-09-18 14:10:47 +00:00
|
|
|
displayed_dive.notes = strdup(notes.replace("VARIATIONS", QString(buf)).toUtf8().data());
|
|
|
|
}
|
2017-08-28 21:59:58 +00:00
|
|
|
setRecalc(oldRecalc);
|
|
|
|
}
|
|
|
|
|
2015-05-28 19:23:49 +00:00
|
|
|
void DivePlannerPointsModel::createPlan(bool replanCopy)
|
|
|
|
{
|
|
|
|
// Ok, so, here the diveplan creates a dive
|
2017-05-25 22:45:53 +00:00
|
|
|
struct deco_state *cache = NULL;
|
2015-05-28 19:23:49 +00:00
|
|
|
bool oldRecalc = setRecalc(false);
|
|
|
|
removeDeco();
|
|
|
|
createTemporaryPlan();
|
|
|
|
setRecalc(oldRecalc);
|
|
|
|
|
|
|
|
//TODO: C-based function here?
|
2017-08-27 20:49:41 +00:00
|
|
|
struct decostop stoptable[60];
|
2017-11-22 19:42:33 +00:00
|
|
|
plan(&ds_after_previous_dives, &diveplan, &displayed_dive, DECOTIMESTEP, stoptable, &cache, isPlanner(), true);
|
2015-06-22 03:24:07 +00:00
|
|
|
free(cache);
|
2015-05-28 19:23:49 +00:00
|
|
|
if (!current_dive || displayed_dive.id != current_dive->id) {
|
|
|
|
// we were planning a new dive, not re-planning an existing on
|
|
|
|
record_dive(clone_dive(&displayed_dive));
|
|
|
|
} else if (current_dive && displayed_dive.id == current_dive->id) {
|
|
|
|
// we are replanning a dive - make sure changes are reflected
|
|
|
|
// correctly in the dive structure and copy it back into the dive table
|
|
|
|
displayed_dive.maxdepth.mm = 0;
|
|
|
|
displayed_dive.dc.maxdepth.mm = 0;
|
|
|
|
fixup_dive(&displayed_dive);
|
2017-03-29 19:25:30 +00:00
|
|
|
// Try to identify old planner output and remove only this part
|
2017-04-19 13:40:59 +00:00
|
|
|
// Treat user provided text as plain text.
|
|
|
|
QTextDocument notesDocument;
|
|
|
|
notesDocument.setHtml(current_dive->notes);
|
|
|
|
QString oldnotes(notesDocument.toPlainText());
|
|
|
|
int disclaimerPosition = oldnotes.indexOf(disclaimer);
|
2017-04-23 11:48:47 +00:00
|
|
|
if (disclaimerPosition == 0)
|
|
|
|
oldnotes.clear();
|
|
|
|
else if (disclaimerPosition >= 1)
|
|
|
|
oldnotes.truncate(disclaimerPosition-1);
|
2017-04-19 13:40:59 +00:00
|
|
|
// Deal with line breaks
|
2017-04-23 11:48:47 +00:00
|
|
|
oldnotes.replace("\n", "<br>");
|
2017-03-29 19:25:30 +00:00
|
|
|
oldnotes.append(displayed_dive.notes);
|
|
|
|
displayed_dive.notes = strdup(oldnotes.toUtf8().data());
|
|
|
|
// If we save as new create a copy of the dive here
|
2015-05-28 19:23:49 +00:00
|
|
|
if (replanCopy) {
|
|
|
|
struct dive *copy = alloc_dive();
|
|
|
|
copy_dive(current_dive, copy);
|
|
|
|
copy->id = 0;
|
2015-12-16 21:47:10 +00:00
|
|
|
copy->selected = false;
|
2015-05-28 19:23:49 +00:00
|
|
|
copy->divetrip = NULL;
|
|
|
|
if (current_dive->divetrip)
|
|
|
|
add_dive_to_trip(copy, current_dive->divetrip);
|
|
|
|
record_dive(copy);
|
|
|
|
}
|
|
|
|
copy_dive(&displayed_dive, current_dive);
|
|
|
|
}
|
|
|
|
mark_divelist_changed(true);
|
2017-10-02 09:17:10 +00:00
|
|
|
sort_table(&dive_table);
|
2015-05-28 19:23:49 +00:00
|
|
|
|
|
|
|
// Remove and clean the diveplan, so we don't delete
|
|
|
|
// the dive by mistake.
|
|
|
|
free_dps(&diveplan);
|
|
|
|
setPlanMode(NOTHING);
|
|
|
|
planCreated();
|
|
|
|
}
|