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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
|
#include "diveplanner.h"
#include "graphicsview-common.h"
#include "models.h"
#include "modeldelegates.h"
#include "mainwindow.h"
#include "maintab.h"
#include "tableview.h"
#include "dive.h"
#include "divelist.h"
#include "planner.h"
#include "display.h"
#include "helpers.h"
#include <QMouseEvent>
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QMessageBox>
#include <QListView>
#include <QModelIndex>
#include <QSettings>
#include <QTableView>
#include <QColor>
#include <algorithm>
#include <string.h>
#define TIME_INITIAL_MAX 30
#define MAX_DEPTH M_OR_FT(150, 450)
#define MIN_DEPTH M_OR_FT(20, 60)
QString gasToStr(const int o2Permille, const int hePermille)
{
uint o2 = (o2Permille + 5) / 10, he = (hePermille + 5) / 10;
QString result = is_air(o2Permille, hePermille) ? QObject::tr("AIR") : he == 0 ? QString("EAN%1").arg(o2, 2, 10, QChar('0')) : QString("%1/%2").arg(o2).arg(he);
return result;
}
QString dpGasToStr(const divedatapoint &p)
{
return gasToStr(p.o2, p.he);
}
static DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
bool intLessThan(int a, int b)
{
return a <= b;
}
void DivePlannerPointsModel::removeSelectedPoints(const QVector<int> &rows)
{
int firstRow = rowCount() - rows.count();
QVector<int> v2 = rows;
std::sort(v2.begin(), v2.end(), intLessThan);
beginRemoveRows(QModelIndex(), firstRow, rowCount() - 1);
for (int i = v2.count() - 1; i >= 0; i--) {
divepoints.remove(v2[i]);
}
endRemoveRows();
}
void DivePlannerPointsModel::createSimpleDive()
{
int o2 = O2_IN_AIR;
int he = 0;
if (isPlanner()) {
// let's use the gas from the first cylinder
o2 = stagingDive->cylinder[0].gasmix.o2.permille;
he = stagingDive->cylinder[0].gasmix.he.permille;
}
// plannerModel->addStop(0, 0, O2_IN_AIR, 0, 0);
plannerModel->addStop(M_OR_FT(15, 45), 1 * 60, o2, he, 0, true);
plannerModel->addStop(M_OR_FT(15, 45), 40 * 60, o2, he, 0, true);
if (!isPlanner()) {
plannerModel->addStop(M_OR_FT(5, 15), 42 * 60, o2, he, 0, true);
plannerModel->addStop(M_OR_FT(5, 15), 45 * 60, o2, he, 0, true);
}
}
void DivePlannerPointsModel::loadFromDive(dive *d)
{
// We need to make a copy, because as soon as the model is modified, it will
// remove all samples from the dive.
memcpy(&backupDive, d, sizeof(struct dive));
copy_samples(d, &backupDive);
copy_events(d, &backupDive);
copy_cylinders(d, stagingDive); // this way the correct cylinder data is shown
CylindersModel::instance()->setDive(stagingDive);
int lasttime = 0;
// we start with the first gas and see if it was changed
int o2 = get_o2(&backupDive.cylinder[0].gasmix);
int he = get_he(&backupDive.cylinder[0].gasmix);
for (int i = 0; i < backupDive.dc.samples - 1; i++) {
const sample &s = backupDive.dc.sample[i];
if (s.time.seconds == 0)
continue;
get_gas_from_events(&backupDive.dc, lasttime, &o2, &he);
plannerModel->addStop(s.depth.mm, s.time.seconds, o2, he, 0, true);
lasttime = s.time.seconds;
}
}
void DivePlannerPointsModel::restoreBackupDive()
{
memcpy(current_dive, &backupDive, sizeof(struct dive));
}
void DivePlannerPointsModel::copyCylinders(dive *d)
{
copy_cylinders(stagingDive, d);
}
// copy the tanks from the current dive, or the default cylinder
// or an unknown cylinder
// setup the cylinder widget accordingly
void DivePlannerPointsModel::setupCylinders()
{
if (!stagingDive)
return;
if (stagingDive != current_dive) {
if (current_dive) {
copy_cylinders(current_dive, stagingDive);
} else {
if (!same_string(prefs.default_cylinder, "")) {
fill_default_cylinder(&stagingDive->cylinder[0]);
} else {
// roughly an AL80
stagingDive->cylinder[0].type.description = strdup(tr("unknown").toUtf8().constData());
stagingDive->cylinder[0].type.size.mliter = 11100;
stagingDive->cylinder[0].type.workingpressure.mbar = 207000;
stagingDive->cylinder[0].used = true;
}
}
}
reset_cylinders(stagingDive);
CylindersModel::instance()->copyFromDive(stagingDive);
}
QStringList &DivePlannerPointsModel::getGasList()
{
struct dive *activeDive = isPlanner() ? stagingDive : current_dive;
static QStringList list;
list.clear();
if (!activeDive) {
list.push_back(tr("AIR"));
} else {
for (int i = 0; i < MAX_CYLINDERS; i++) {
cylinder_t *cyl = &activeDive->cylinder[i];
if (cylinder_nodata(cyl))
break;
list.push_back(gasToStr(get_o2(&cyl->gasmix), get_he(&cyl->gasmix)));
}
}
return list;
}
void DivePlannerPointsModel::removeDeco()
{
bool oldrec = setRecalc(false);
QVector<int> computedPoints;
for (int i = 0; i < plannerModel->rowCount(); i++)
if (!plannerModel->at(i).entered)
computedPoints.push_back(i);
removeSelectedPoints(computedPoints);
setRecalc(oldrec);
}
#if 0
void DivePlannerGraphics::drawProfile()
{
// Code ported to the new profile is deleted. This part that I left here
// is because I didn't fully understood the reason of the magic with
// the plannerModel.
bool oldRecalc = plannerModel->setRecalc(false);
plannerModel->removeDeco();
// Here we plotted the old planner profile. why there's the magic with the plannerModel here?
plannerModel->setRecalc(oldRecalc);
plannerModel->deleteTemporaryPlan();
}
#endif
DiveHandler::DiveHandler() : QGraphicsEllipseItem()
{
setRect(-5, -5, 10, 10);
setFlags(ItemIgnoresTransformations | ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges);
setBrush(Qt::white);
setZValue(2);
}
int DiveHandler::parentIndex()
{
ProfileWidget2 *view = qobject_cast<ProfileWidget2 *>(scene()->views().first());
return view->handles.indexOf(this);
}
void DiveHandler::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
QMenu m;
GasSelectionModel *model = GasSelectionModel::instance();
model->repopulate();
int rowCount = model->rowCount();
for (int i = 0; i < rowCount; i++) {
QAction *action = new QAction(&m);
action->setText(model->data(model->index(i, 0), Qt::DisplayRole).toString());
connect(action, SIGNAL(triggered(bool)), this, SLOT(changeGas()));
m.addAction(action);
}
m.addSeparator();
m.addAction(QObject::tr("Remove this Point"), this, SLOT(selfRemove()));
m.exec(event->screenPos());
}
void DiveHandler::selfRemove()
{
setSelected(true);
ProfileWidget2 *view = qobject_cast<ProfileWidget2 *>(scene()->views().first());
view->keyDeleteAction();
}
void DiveHandler::changeGas()
{
QAction *action = qobject_cast<QAction *>(sender());
QModelIndex index = plannerModel->index(parentIndex(), DivePlannerPointsModel::GAS);
plannerModel->setData(index, action->text());
}
void DiveHandler::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
ProfileWidget2 *view = qobject_cast<ProfileWidget2*>(scene()->views().first());
if(view->isPointOutOfBoundaries(event->scenePos()))
return;
QGraphicsEllipseItem::mouseMoveEvent(event);
emit moved();
}
DivePlannerWidget::DivePlannerWidget(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f)
{
ui.setupUi(this);
ui.tableWidget->setTitle(tr("Dive Planner Points"));
ui.tableWidget->setModel(DivePlannerPointsModel::instance());
DivePlannerPointsModel::instance()->setRecalc(true);
ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::GAS, new AirTypesDelegate(this));
ui.cylinderTableWidget->setTitle(tr("Available Gases"));
ui.cylinderTableWidget->setModel(CylindersModel::instance());
QTableView *view = ui.cylinderTableWidget->view();
view->setColumnHidden(CylindersModel::START, true);
view->setColumnHidden(CylindersModel::END, true);
view->setColumnHidden(CylindersModel::DEPTH, false);
view->setItemDelegateForColumn(CylindersModel::TYPE, new TankInfoDelegate(this));
connect(ui.cylinderTableWidget, SIGNAL(addButtonClicked()), DivePlannerPointsModel::instance(), SLOT(addCylinder_clicked()));
connect(ui.tableWidget, SIGNAL(addButtonClicked()), DivePlannerPointsModel::instance(), SLOT(addStop()));
connect(CylindersModel::instance(), SIGNAL(dataChanged(QModelIndex, QModelIndex)),
GasSelectionModel::instance(), SLOT(repopulate()));
connect(CylindersModel::instance(), SIGNAL(rowsInserted(QModelIndex, int, int)),
GasSelectionModel::instance(), SLOT(repopulate()));
connect(CylindersModel::instance(), SIGNAL(rowsRemoved(QModelIndex, int, int)),
GasSelectionModel::instance(), SLOT(repopulate()));
connect(CylindersModel::instance(), SIGNAL(dataChanged(QModelIndex, QModelIndex)),
plannerModel, SIGNAL(cylinderModelEdited()));
connect(CylindersModel::instance(), SIGNAL(rowsInserted(QModelIndex, int, int)),
plannerModel, SIGNAL(cylinderModelEdited()));
connect(CylindersModel::instance(), SIGNAL(rowsRemoved(QModelIndex, int, int)),
plannerModel, SIGNAL(cylinderModelEdited()));
ui.tableWidget->setBtnToolTip(tr("add dive data point"));
connect(ui.startTime, SIGNAL(timeChanged(QTime)), plannerModel, SLOT(setStartTime(QTime)));
connect(ui.ATMPressure, SIGNAL(textChanged(QString)), this, SLOT(atmPressureChanged(QString)));
connect(ui.bottomSAC, SIGNAL(textChanged(QString)), this, SLOT(bottomSacChanged(QString)));
connect(ui.decoStopSAC, SIGNAL(textChanged(QString)), this, SLOT(decoSacChanged(QString)));
connect(ui.gfhigh, SIGNAL(valueChanged(int)), plannerModel, SLOT(setGFHigh(int)));
connect(ui.gflow, SIGNAL(valueChanged(int)), plannerModel, SLOT(setGFLow(int)));
connect(ui.lastStop, SIGNAL(toggled(bool)), plannerModel, SLOT(setLastStop6m(bool)));
// Creating (and canceling) the plan
connect(ui.buttonBox, SIGNAL(accepted()), plannerModel, SLOT(createPlan()));
connect(ui.buttonBox, SIGNAL(rejected()), plannerModel, SLOT(cancelPlan()));
/* set defaults. */
ui.startTime->setTime(QTime(1, 0));
ui.ATMPressure->setText("1013");
ui.bottomSAC->setText("20");
ui.decoStopSAC->setText("17");
ui.gflow->setValue(prefs.gflow);
ui.gfhigh->setValue(prefs.gfhigh);
setMinimumWidth(0);
setMinimumHeight(0);
}
void DivePlannerWidget::settingsChanged()
{
ui.gflow->setValue(prefs.gflow);
ui.gfhigh->setValue(prefs.gfhigh);
}
void DivePlannerPointsModel::addCylinder_clicked()
{
CylindersModel::instance()->add();
}
void DivePlannerWidget::atmPressureChanged(const QString &pressure)
{
plannerModel->setSurfacePressure(pressure.toInt());
}
void DivePlannerWidget::bottomSacChanged(const QString &bottomSac)
{
plannerModel->setBottomSac(bottomSac.toInt());
}
void DivePlannerWidget::decoSacChanged(const QString &decosac)
{
plannerModel->setDecoSac(decosac.toInt());
}
void DivePlannerPointsModel::setPlanMode(Mode m)
{
mode = m;
if (m == NOTHING)
stagingDive = NULL;
}
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
{
return COLUMNS;
}
QVariant DivePlannerPointsModel::data(const QModelIndex &index, int role) const
{
divedatapoint p = divepoints.at(index.row());
if (role == Qt::DisplayRole) {
switch (index.column()) {
case CCSETPOINT:
return (double)p.po2 / 1000;
case DEPTH:
return rint(get_depth_units(p.depth, NULL, NULL));
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:
return dpGasToStr(p);
}
} else if (role == Qt::DecorationRole) {
switch (index.column()) {
case REMOVE:
return p.entered ? QIcon(":trash") : QVariant();
}
} 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 o2 = 0;
int he = 0;
int i, shift;
if (role == Qt::EditRole) {
divedatapoint &p = divepoints[index.row()];
switch (index.column()) {
case DEPTH:
p.depth = units_to_depth(value.toInt());
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.po2 = po2;
} break;
case GAS:
QByteArray gasv = value.toByteArray();
if (validate_gas(gasv.data(), &o2, &he)) {
p.o2 = o2;
p.he = he;
}
break;
}
editStop(index.row(), p);
}
return QAbstractItemModel::setData(index, value, role);
}
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:
return tr("CC Set Point");
}
} 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
{
return divepoints.count();
}
DivePlannerPointsModel::DivePlannerPointsModel(QObject *parent) : QAbstractTableModel(parent), mode(NOTHING), tempDive(NULL), stagingDive(NULL)
{
memset(&diveplan, 0, sizeof(diveplan));
memset(&backupDive, 0, sizeof(backupDive));
}
DivePlannerPointsModel *DivePlannerPointsModel::instance()
{
static QScopedPointer<DivePlannerPointsModel> self(new DivePlannerPointsModel());
return self.data();
}
void DivePlannerPointsModel::setBottomSac(int sac)
{
diveplan.bottomsac = sac * 1000;
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1));
}
void DivePlannerPointsModel::setDecoSac(int sac)
{
diveplan.decosac = sac * 1000;
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1));
}
void DivePlannerPointsModel::setGFHigh(const int gfhigh)
{
diveplan.gfhigh = gfhigh;
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1));
}
void DivePlannerPointsModel::setGFLow(const int ghflow)
{
diveplan.gflow = ghflow;
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1));
}
void DivePlannerPointsModel::setSurfacePressure(int pressure)
{
diveplan.surface_pressure = pressure;
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1));
}
void DivePlannerPointsModel::setLastStop6m(bool value)
{
set_last_stop(value);
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1));
}
void DivePlannerPointsModel::setStartTime(const QTime &t)
{
diveplan.when = (t.msec() + QDateTime::currentMSecsSinceEpoch()) / 1000 - gettimezoneoffset();
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1));
}
bool divePointsLessThan(const divedatapoint &p1, const divedatapoint &p2)
{
return p1.time <= p2.time;
}
bool DivePlannerPointsModel::addGas(int o2, int he)
{
struct gasmix mix;
mix.o2.permille = o2;
mix.he.permille = he;
sanitize_gasmix(&mix);
if (is_air(o2, he))
o2 = 0;
for (int i = 0; i < MAX_CYLINDERS; i++) {
cylinder_t *cyl = &stagingDive->cylinder[i];
if (cylinder_nodata(cyl)) {
fill_default_cylinder(cyl);
cyl->gasmix.o2.permille = o2;
cyl->gasmix.he.permille = he;
sanitize_gasmix(&cyl->gasmix);
/* The depth to change to that gas is given by the depth where its pO2 is 1.6 bar.
* The user should be able to change this depth manually. */
pressure_t modppO2;
modppO2.mbar = 1600;
cyl->depth = gas_mod(&cyl->gasmix, modppO2);
CylindersModel::instance()->setDive(stagingDive);
return true;
}
if (!gasmix_distance(&cyl->gasmix, &mix))
return true;
}
qDebug("too many gases");
return false;
}
int DivePlannerPointsModel::lastEnteredPoint()
{
for (int i = divepoints.count() - 1; i >= 0; i--)
if (divepoints.at(i).entered)
return i;
return -1;
}
int DivePlannerPointsModel::addStop(int milimeters, int seconds, int o2, int he, int ccpoint, bool entered)
{
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());
milimeters = t.depth;
seconds = t.time + 600; // 10 minutes.
o2 = t.o2;
he = t.he;
ccpoint = t.po2;
} else if (seconds == 0 && milimeters == 0 && row == 0) {
milimeters = M_OR_FT(5, 15); // 5m / 15ft
seconds = 600; // 10 min
//Default to the first defined gas, if we got one.
cylinder_t *cyl = &stagingDive->cylinder[0];
if (cyl) {
o2 = get_o2(&cyl->gasmix);
he = get_he(&cyl->gasmix);
}
}
if (o2 != -1)
if (!addGas(o2, he))
qDebug("addGas failed"); // FIXME add error propagation
// 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;
}
}
if (o2 == -1) {
if (row > 0) {
o2 = divepoints.at(row - 1).o2;
he = divepoints.at(row - 1).he;
} else {
// when we add a first data point we need to make sure that there is a
// tank for it to use;
// first check to the right, then to the left, but if there's nothing,
// we simply default to AIR
if (row < divepoints.count()) {
o2 = divepoints.at(row).o2;
he = divepoints.at(row).he;
} else {
o2 = O2_IN_AIR;
if (!addGas(o2, 0))
qDebug("addGas failed"); // FIXME add error propagation
}
}
}
// add the new stop
beginInsertRows(QModelIndex(), row, row);
divedatapoint point;
point.depth = milimeters;
point.time = seconds;
point.o2 = o2;
point.he = he;
point.po2 = ccpoint;
point.entered = entered;
divepoints.append(point);
std::sort(divepoints.begin(), divepoints.end(), divePointsLessThan);
endInsertRows();
return row;
}
void DivePlannerPointsModel::editStop(int row, divedatapoint newData)
{
divepoints[row] = newData;
std::sort(divepoints.begin(), divepoints.end(), divePointsLessThan);
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1));
}
int DivePlannerPointsModel::size()
{
return divepoints.size();
}
divedatapoint DivePlannerPointsModel::at(int row)
{
return divepoints.at(row);
}
void DivePlannerPointsModel::remove(const QModelIndex &index)
{
if (index.column() != REMOVE)
return;
divedatapoint dp = at(index.row());
if (!dp.entered)
return;
beginRemoveRows(QModelIndex(), index.row(), index.row());
divepoints.remove(index.row());
endRemoveRows();
}
struct diveplan &DivePlannerPointsModel::getDiveplan()
{
return diveplan;
}
void DivePlannerPointsModel::cancelPlan()
{
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;
}
}
if (mode != ADD) // for ADD stagingDive points at current_dive
free(stagingDive);
stagingDive = NULL; // always reset the stagingDive to NULL
setPlanMode(NOTHING);
diveplan.dp = NULL;
emit planCanceled();
}
DivePlannerPointsModel::Mode DivePlannerPointsModel::currentMode() const
{
return mode;
}
QVector<QPair<int, int> > DivePlannerPointsModel::collectGases(struct dive *d)
{
QVector<QPair<int, int> > l;
for (int i = 0; i < MAX_CYLINDERS; i++) {
cylinder_t *cyl = &d->cylinder[i];
if (!cylinder_nodata(cyl))
l.push_back(qMakePair(get_o2(&cyl->gasmix), get_he(&cyl->gasmix)));
}
return l;
}
void DivePlannerPointsModel::rememberTanks()
{
oldGases = collectGases(stagingDive);
}
bool DivePlannerPointsModel::tankInUse(int o2, int he)
{
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;
if ((p.o2 == o2 && p.he == he) ||
(is_air(p.o2, p.he) && is_air(o2, he)))
return true;
}
return false;
}
void DivePlannerPointsModel::tanksUpdated()
{
// we don't know exactly what changed - what we care about is
// "did a gas change on us". So we look through the diveplan to
// see if there is a gas that is now missing and if there is, we
// replace it with the matching new gas.
QVector<QPair<int, int> > gases = collectGases(stagingDive);
if (gases.count() == oldGases.count()) {
// either nothing relevant changed, or exactly ONE gasmix changed
for (int i = 0; i < gases.count(); i++) {
if (gases.at(i) != oldGases.at(i)) {
if (oldGases.count(oldGases.at(i)) > 1) {
// we had this gas more than once, so don't
// change segments that used this gas as it still exists
break;
}
for (int j = 0; j < rowCount(); j++) {
divedatapoint &p = divepoints[j];
int o2 = oldGases.at(i).first;
int he = oldGases.at(i).second;
if ((p.o2 == o2 && p.he == he) ||
(is_air(p.o2, p.he) && (is_air(o2, he) || (o2 == 0 && he == 0)))) {
p.o2 = gases.at(i).first;
p.he = gases.at(i).second;
}
}
break;
}
}
}
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, COLUMNS - 1));
}
void DivePlannerPointsModel::clear()
{
Q_ASSERT(stagingDive == 0);
if (mode == ADD) {
stagingDive = current_dive;
} else {
stagingDive = alloc_dive();
}
bool oldRecalc = setRecalc(false);
CylindersModel::instance()->setDive(stagingDive);
if (rowCount() > 0) {
beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
divepoints.clear();
endRemoveRows();
}
CylindersModel::instance()->clear();
setRecalc(oldRecalc);
}
void DivePlannerPointsModel::addDecoToModel()
{
struct divedatapoint *dp;
if (diveplan_empty(&diveplan))
return;
bool oldRecalc = plannerModel->setRecalc(false);
plannerModel->removeDeco();
unsigned int lastdepth = 0;
for (dp = diveplan.dp; dp != NULL; dp = dp->next) {
if (dp->time == 0) // magic entry for available tank
continue;
if (!dp->entered) {
if (dp->depth) {
if (dp->depth == lastdepth || dp->o2 != dp->next->o2 || dp->he != dp->next->he)
plannerModel->addStop(dp->depth, dp->time, dp->next->o2, dp->next->he, 0, false);
lastdepth = dp->depth;
}
}
}
plannerModel->setRecalc(oldRecalc);
}
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
if (!stagingDive)
return;
//TODO: this thingy looks like it could be a good C-based function
diveplan.dp = NULL;
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;
if (p.entered)
plan_add_segment(&diveplan, deltaT, p.depth, p.o2, p.he, p.po2, true);
}
char *cache = NULL;
tempDive = NULL;
struct divedatapoint *dp = NULL;
for (int i = 0; i < MAX_CYLINDERS; i++) {
cylinder_t *cyl = &stagingDive->cylinder[i];
if (cyl->depth.mm) {
dp = create_dp(0, cyl->depth.mm, get_o2(&cyl->gasmix), get_he(&cyl->gasmix), 0);
if (diveplan.dp) {
dp->next = diveplan.dp->next;
diveplan.dp->next = dp;
} else {
dp->next = NULL;
diveplan.dp = dp;
}
}
}
#if DEBUG_PLAN
dump_plan(&diveplan);
#endif
if (plannerModel->recalcQ() && !diveplan_empty(&diveplan)) {
plan(&diveplan, &cache, &tempDive, stagingDive, isPlanner());
MainWindow::instance()->setPlanNotes(tempDive->notes);
addDecoToModel();
if (mode == ADD || mode == PLAN) {
// copy the samples and events, but don't overwrite the cylinders
copy_samples(tempDive, current_dive);
copy_events(tempDive, current_dive);
copy_cylinders(tempDive, current_dive);
}
}
// throw away the cache
free(cache);
#if DEBUG_PLAN
dump_plan(&diveplan);
#endif
}
void DivePlannerPointsModel::deleteTemporaryPlan()
{
deleteTemporaryPlan(diveplan.dp);
diveplan.dp = NULL;
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;
bool oldRecalc = plannerModel->setRecalc(false);
removeDeco();
createTemporaryPlan();
plannerModel->setRecalc(oldRecalc);
//TODO: C-based function here?
plan(&diveplan, &cache, &tempDive, stagingDive, isPlanner());
record_dive(tempDive);
mark_divelist_changed(true);
// Remove and clean the diveplan, so we don't delete
// the dive by mistake.
diveplan.dp = NULL;
setPlanMode(NOTHING);
free(stagingDive);
stagingDive = NULL;
planCreated();
}
|