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

CylindersModel::CylindersModel(bool planner, QObject *parent) : CleanerTableModel(parent),
	d(nullptr),
	inPlanner(planner),
	tempRow(-1),
	tempCyl(empty_cylinder)
{
	//	enum {REMOVE, TYPE, SIZE, WORKINGPRESS, START, END, O2, HE, DEPTH, MOD, MND, USE, IS_USED};
	setHeaderDataStrings(QStringList() << "" << tr("Type") << tr("Size") << tr("Work press.") << tr("Start press.") << tr("End press.") << tr("O₂%") << tr("He%")
						 << tr("Deco switch at") <<tr("Bot. MOD") <<tr("MND") << tr("Use"));

	connect(&diveListNotifier, &DiveListNotifier::cylindersReset, this, &CylindersModel::cylindersReset);
	connect(&diveListNotifier, &DiveListNotifier::cylinderAdded, this, &CylindersModel::cylinderAdded);
	connect(&diveListNotifier, &DiveListNotifier::cylinderRemoved, this, &CylindersModel::cylinderRemoved);
	connect(&diveListNotifier, &DiveListNotifier::cylinderEdited, this, &CylindersModel::cylinderEdited);
}

QVariant CylindersModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (role == Qt::DisplayRole && orientation == Qt::Horizontal && inPlanner && section == WORKINGPRESS)
		return tr("Start press.");
	else
		return CleanerTableModel::headerData(section, orientation, role);
}

static QString get_cylinder_string(const cylinder_t *cyl)
{
	QString unit;
	int decimals;
	unsigned int ml = cyl->type.size.mliter;
	pressure_t wp = cyl->type.workingpressure;
	double value;

	// We cannot use "get_volume_units()", because even when
	// using imperial units we may need to show the size in
	// liters: if we don't have a working pressure, we cannot
	// convert the cylinder size to cuft.
	if (wp.mbar && prefs.units.volume == units::CUFT) {
		value = ml_to_cuft(ml) * bar_to_atm(wp.mbar / 1000.0);
		decimals = (value > 20.0) ? 0 : (value > 2.0) ? 1 : 2;
		unit = CylindersModel::tr("cuft");
	} else {
		value = ml / 1000.0;
		decimals = 1;
		unit = CylindersModel::tr("ℓ");
	}

	return QString("%L1").arg(value, 0, 'f', decimals) + unit;
}

static QString gas_volume_string(int ml, const char *tail)
{
	double vol;
	const char *unit;
	int decimals;

	vol = get_volume_units(ml, NULL, &unit);
	decimals = (vol > 20.0) ? 0 : (vol > 2.0) ? 1 : 2;

	return QString("%L1 %2 %3").arg(vol, 0, 'f', decimals).arg(unit).arg(tail);
}

static QVariant gas_wp_tooltip(const cylinder_t *cyl);

static QVariant gas_usage_tooltip(const cylinder_t *cyl)
{
	pressure_t startp = cyl->start.mbar ? cyl->start : cyl->sample_start;
	pressure_t endp = cyl->end.mbar ? cyl->end : cyl->sample_end;

	int start, end, used;

	start = gas_volume(cyl, startp);
	end = gas_volume(cyl, endp);
	used = (end && start > end) ? start - end : 0;

	if (!used)
		return gas_wp_tooltip(cyl);

	return gas_volume_string(used, "(") +
		gas_volume_string(start, " -> ") +
		gas_volume_string(end, ")");
}

static QVariant gas_volume_tooltip(const cylinder_t *cyl, pressure_t p)
{
	int vol = gas_volume(cyl, p);
	double Z;

	if (!vol)
		return QVariant();

	Z = gas_compressibility_factor(cyl->gasmix, p.mbar / 1000.0);
	return gas_volume_string(vol, "(Z=") + QString("%1)").arg(Z, 0, 'f', 3);
}

static QVariant gas_wp_tooltip(const cylinder_t *cyl)
{
	return gas_volume_tooltip(cyl, cyl->type.workingpressure);
}

static QVariant gas_start_tooltip(const cylinder_t *cyl)
{
	return gas_volume_tooltip(cyl, cyl->start.mbar ? cyl->start : cyl->sample_start);
}

static QVariant gas_end_tooltip(const cylinder_t *cyl)
{
	return gas_volume_tooltip(cyl, cyl->end.mbar ? cyl->end : cyl->sample_end);
}

static QVariant percent_string(fraction_t fraction)
{
	int permille = fraction.permille;

	if (!permille)
		return QVariant();
	return QString("%L1%").arg(permille / 10.0, 0, 'f', 1);
}

bool CylindersModel::cylinderUsed(int i) const
{
	if (i < 0 || i >= d->cylinders.nr)
		return false;
	if (is_cylinder_used(d, i))
		return true;

	cylinder_t *cyl = get_cylinder(d, i);
	if (cyl->start.mbar || cyl->sample_start.mbar ||
	    cyl->end.mbar || cyl->sample_end.mbar)
		return true;
	if (cyl->manually_added)
		return true;

	/*
	 * The cylinder has some data, but none of it is very interesting,
	 * it has no pressures and no gas switches. Do we want to show it?
	 */
	return false;
}

QVariant CylindersModel::data(const QModelIndex &index, int role) const
{
	if (!d || !index.isValid() || index.row() >= d->cylinders.nr)
		return QVariant();

	if (index.row() >= d->cylinders.nr) {
		qWarning("CylindersModel and dive are out of sync!");
		return QVariant();
	}

	const cylinder_t *cyl = index.row() == tempRow ? &tempCyl : get_cylinder(d, index.row());

	switch (role) {
	case Qt::BackgroundRole: {
		switch (index.column()) {
		// mark the cylinder start / end pressure in red if the values
		// seem implausible
		case START:
		case END:
			pressure_t startp, endp;
			startp = cyl->start.mbar ? cyl->start : cyl->sample_start;
			endp = cyl->end.mbar ? cyl->end : cyl->sample_end;
			if ((startp.mbar && !endp.mbar) ||
					(endp.mbar && startp.mbar <= endp.mbar))
				return REDORANGE1_HIGH_TRANS;
			break;
		}
		break;
	}
	case Qt::FontRole: {
		QFont font = defaultModelFont();
		switch (index.column()) {
		// if we don't have manually set pressure data use italic font
		case START:
			font.setItalic(!cyl->start.mbar);
			break;
		case END:
			font.setItalic(!cyl->end.mbar);
			break;
		}
		return font;
	}
	case Qt::TextAlignmentRole:
		return Qt::AlignCenter;
	case Qt::DisplayRole:
	case Qt::EditRole:
		switch (index.column()) {
		case TYPE:
			return QString(cyl->type.description);
		case SIZE:
			if (cyl->type.size.mliter)
				return get_cylinder_string(cyl);
			break;
		case WORKINGPRESS:
			if (cyl->type.workingpressure.mbar)
				return get_pressure_string(cyl->type.workingpressure, true);
			break;
		case START:
			if (cyl->start.mbar)
				return get_pressure_string(cyl->start, true);
			else if (cyl->sample_start.mbar)
				return get_pressure_string(cyl->sample_start, true);
			break;
		case END:
			if (cyl->end.mbar)
				return get_pressure_string(cyl->end, true);
			else if (cyl->sample_end.mbar)
				return get_pressure_string(cyl->sample_end, true);
			break;
		case O2:
			return percent_string(cyl->gasmix.o2);
		case HE:
			return percent_string(cyl->gasmix.he);
		case DEPTH:
			return get_depth_string(cyl->depth, true);
		case MOD:
			if (cyl->bestmix_o2) {
				return QStringLiteral("*");
			} else {
				pressure_t modpO2;
				modpO2.mbar = prefs.bottompo2;
				return get_depth_string(gas_mod(cyl->gasmix, modpO2, d, M_OR_FT(1,1)), true);
			}
		case MND:
			if (cyl->bestmix_he)
				return QStringLiteral("*");
			else
				return get_depth_string(gas_mnd(cyl->gasmix, prefs.bestmixend, d, M_OR_FT(1,1)), true);
			break;
		case USE:
			return gettextFromC::tr(cylinderuse_text[cyl->cylinder_use]);
		case WORKINGPRESS_INT:
			return static_cast<int>(cyl->type.workingpressure.mbar);
		case SIZE_INT:
			return static_cast<int>(cyl->type.size.mliter);
		}
		break;
	case Qt::DecorationRole:
	case Qt::SizeHintRole:
		if (index.column() == REMOVE) {
			if ((inPlanner && DivePlannerPointsModel::instance()->tankInUse(index.row())) ||
				(!inPlanner && is_cylinder_prot(d, index.row()))) {
					return trashForbiddenIcon();
			}
			return trashIcon();
		}
		break;
	case Qt::ToolTipRole:
		switch (index.column()) {
		case REMOVE:
			if ((inPlanner && DivePlannerPointsModel::instance()->tankInUse(index.row())) ||
				(!inPlanner && is_cylinder_prot(d, index.row()))) {
					return tr("This gas is in use. Only cylinders that are not used in the dive can be removed.");
			}
			return tr("Clicking here will remove this cylinder.");
		case TYPE:
		case SIZE:
			return gas_usage_tooltip(cyl);
		case WORKINGPRESS:
			return gas_wp_tooltip(cyl);
		case START:
			return gas_start_tooltip(cyl);
		case END:
			return gas_end_tooltip(cyl);
		case DEPTH:
			return tr("Switch depth for deco gas. Calculated using Deco pO₂ preference, unless set manually.");
		case MOD:
			return tr("Calculated using Bottom pO₂ preference. Setting MOD adjusts O₂%, set to '*' for best O₂% for max. depth.");
		case MND:
			return tr("Calculated using Best Mix END preference. Setting MND adjusts He%, set to '*' for best He% for max. depth.");
		}
		break;
	}

	return QVariant();
}

cylinder_t *CylindersModel::cylinderAt(const QModelIndex &index)
{
	if (!d)
		return nullptr;
	return get_cylinder(d, index.row());
}

bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
	if (!d)
		return false;

	int row = index.row();
	if (row < 0 || row >= d->cylinders.nr)
		return false;

	// Here we handle a few cases that allow us to set / commit / revert
	// a temporary row. This is a horribly misuse of the model/view system.
	// The reason it is done this way is that the planner and the equipment
	// tab use different model-classes which are not in a superclass / subclass
	// relationship.
	switch (role) {
	case TEMP_ROLE:
		// TEMP_ROLE means that we are not supposed to write through to the
		// actual dive, but a temporary cylinder that is displayed while the
		// user browses throught the cylinder types.
		initTempCyl(index.row());

		switch (index.column()) {
		case TYPE: {
			QString type = value.toString();
			if (!same_string(qPrintable(type), tempCyl.type.description)) {
				free((void *)tempCyl.type.description);
				tempCyl.type.description = strdup(qPrintable(type));
				dataChanged(index, index);
			}
			return true;
		}
		case SIZE:
			if (tempCyl.type.size.mliter != value.toInt()) {
				tempCyl.type.size.mliter = value.toInt();
				dataChanged(index, index);
			}
			return true;
		case WORKINGPRESS:
			if (tempCyl.type.workingpressure.mbar != value.toInt()) {
				tempCyl.type.workingpressure.mbar = value.toInt();
				dataChanged(index, index);
			}
			return true;
		}
		return false;
	case COMMIT_ROLE:
		commitTempCyl(index.row());
		return true;
	case REVERT_ROLE:
		clearTempCyl();
		return true;
	default:
		break;
	}

	QString vString = value.toString();
	bool changed = vString != data(index, role).toString();

	std::string newType; // If we allocate a new type string, this makes sure that it is freed at the end of the function

	// First, we make a shallow copy of the old cylinder. Then we modify the fields inside that copy.
	// At the end, we either place an EditCylinder undo command (EquipmentTab) or copy the cylinder back (planner).
	// Yes, this is not ideal, but the pragmatic thing to do for now.
	cylinder_t cyl = d->cylinders.cylinders[row];

	if (index.column() != TYPE && !changed)
		return false;

	Command::EditCylinderType type = Command::EditCylinderType::TYPE;
	switch (index.column()) {
	case TYPE:
		newType = qPrintable(vString);
		cyl.type.description = newType.c_str();
		type = Command::EditCylinderType::TYPE;
		break;
	case SIZE: {
			TankInfoModel *tanks = TankInfoModel::instance();
			QModelIndexList matches = tanks->match(tanks->index(0, 0), Qt::DisplayRole, cyl.type.description);

			cyl.type.size = string_to_volume(qPrintable(vString), cyl.type.workingpressure);
			if (!matches.isEmpty())
				tanks->setData(tanks->index(matches.first().row(), TankInfoModel::ML), cyl.type.size.mliter);
		}
		type = Command::EditCylinderType::TYPE;
		break;
	case WORKINGPRESS: {
			TankInfoModel *tanks = TankInfoModel::instance();
			QModelIndexList matches = tanks->match(tanks->index(0, 0), Qt::DisplayRole, cyl.type.description);
			cyl.type.workingpressure = string_to_pressure(qPrintable(vString));
			if (!matches.isEmpty())
				tanks->setData(tanks->index(matches.first().row(), TankInfoModel::BAR), cyl.type.workingpressure.mbar / 1000.0);
		}
		type = Command::EditCylinderType::TYPE;
		break;
	case START:
		cyl.start = string_to_pressure(qPrintable(vString));
		type = Command::EditCylinderType::PRESSURE;
		break;
	case END:
		//if (!cyl->start.mbar || string_to_pressure(qPrintable(vString)).mbar <= cyl->start.mbar) {
		cyl.end = string_to_pressure(qPrintable(vString));
		type = Command::EditCylinderType::PRESSURE;
		break;
	case O2: {
			cyl.gasmix.o2 = string_to_fraction(qPrintable(vString));
			// fO2 + fHe must not be greater than 1
			if (get_o2(cyl.gasmix) + get_he(cyl.gasmix) > 1000)
				cyl.gasmix.he.permille = 1000 - get_o2(cyl.gasmix);
			pressure_t modpO2;
			if (d->dc.divemode == PSCR)
				modpO2.mbar = prefs.decopo2 + (1000 - get_o2(cyl.gasmix)) * SURFACE_PRESSURE *
						prefs.o2consumption / prefs.decosac / prefs.pscr_ratio;
			else
				modpO2.mbar = prefs.decopo2;
			cyl.depth = gas_mod(cyl.gasmix, modpO2, d, M_OR_FT(3, 10));
			cyl.bestmix_o2 = false;
		}
		type = Command::EditCylinderType::GASMIX;
		break;
	case HE:
		cyl.gasmix.he = string_to_fraction(qPrintable(vString));
		// fO2 + fHe must not be greater than 1
		if (get_o2(cyl.gasmix) + get_he(cyl.gasmix) > 1000)
			cyl.gasmix.o2.permille = 1000 - get_he(cyl.gasmix);
		cyl.bestmix_he = false;
		type = Command::EditCylinderType::GASMIX;
		break;
	case DEPTH:
		cyl.depth = string_to_depth(qPrintable(vString));
		type = Command::EditCylinderType::GASMIX;
		break;
	case MOD: {
			if (QString::compare(qPrintable(vString), "*") == 0) {
				cyl.bestmix_o2 = true;
				// Calculate fO2 for max. depth
				cyl.gasmix.o2 = best_o2(d->maxdepth, d);
			} else {
				cyl.bestmix_o2 = false;
				// Calculate fO2 for input depth
				cyl.gasmix.o2 = best_o2(string_to_depth(qPrintable(vString)), d);
			}
			pressure_t modpO2;
			modpO2.mbar = prefs.decopo2;
			cyl.depth = gas_mod(cyl.gasmix, modpO2, d, M_OR_FT(3, 10));
		}
		type = Command::EditCylinderType::GASMIX;
		break;
	case MND:
		if (QString::compare(qPrintable(vString), "*") == 0) {
			cyl.bestmix_he = true;
			// Calculate fO2 for max. depth
			cyl.gasmix.he = best_he(d->maxdepth, d, prefs.o2narcotic, cyl.gasmix.o2);
		} else {
			cyl.bestmix_he = false;
			// Calculate fHe for input depth
			cyl.gasmix.he = best_he(string_to_depth(qPrintable(vString)), d, prefs.o2narcotic, cyl.gasmix.o2);
		}
		type = Command::EditCylinderType::GASMIX;
		break;
	case USE: {
			int use = vString.toInt();
			if (use > NUM_GAS_USE - 1 || use < 0)
				use = 0;
			cyl.cylinder_use = (enum cylinderuse)use;
		}
		type = Command::EditCylinderType::TYPE;
		break;
	}

	if (inPlanner) {
		// In the planner - simply overwrite the cylinder in the dive with the modified cylinder.
		// We have only made a shallow copy, therefore copy the new cylinder first.
		cylinder_t copy = clone_cylinder(cyl);
		std::swap(copy, d->cylinders.cylinders[row]);
		free_cylinder(copy);
		dataChanged(index, index);
	} else {
		// On the EquipmentTab - place an editCylinder command.
		int count = Command::editCylinder(index.row(), cyl, type, false);
		emit divesEdited(count);
	}
	return true;
}

int CylindersModel::rowCount(const QModelIndex&) const
{
	return d ? d->cylinders.nr : 0;
}

void CylindersModel::add()
{
	if (!d)
		return;
	int row = d->cylinders.nr;
	cylinder_t cyl = create_new_cylinder(d);
	beginInsertRows(QModelIndex(), row, row);
	add_to_cylinder_table(&d->cylinders, row, cyl);
	endInsertRows();
	emit dataChanged(createIndex(row, 0), createIndex(row, COLUMNS - 1));
}

void CylindersModel::clear()
{
	beginResetModel();
	d = nullptr;
	endResetModel();
}

void CylindersModel::updateDive(dive *dIn)
{
#ifdef DEBUG_CYL
	if (d)
		dump_cylinders(dIn, true);
#endif
	beginResetModel();
	d = dIn;
	endResetModel();
}

Qt::ItemFlags CylindersModel::flags(const QModelIndex &index) const
{
	if (index.column() == REMOVE || index.column() == USE)
		return Qt::ItemIsEnabled;
	return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}

// This function is only invoked from the planner! Therefore, there is
// no need to check whether we are in the planner. Perhaps move some
// of this functionality to the planner itself.
void CylindersModel::remove(QModelIndex index)
{
	if (!d)
		return;
	if (index.column() == USE) {
		cylinder_t *cyl = cylinderAt(index);
		if (cyl->cylinder_use == OC_GAS)
			cyl->cylinder_use = NOT_USED;
		else if (cyl->cylinder_use == NOT_USED)
			cyl->cylinder_use = OC_GAS;
		dataChanged(index, index);
		return;
	}

	if (index.column() != REMOVE)
		return;

	if (DivePlannerPointsModel::instance()->tankInUse(index.row()))
			return;

	beginRemoveRows(QModelIndex(), index.row(), index.row());
	remove_cylinder(d, index.row());
	endRemoveRows();

	std::vector<int> mapping = get_cylinder_map_for_remove(d->cylinders.nr + 1, index.row());
	cylinder_renumber(d, mapping.data());
	DivePlannerPointsModel::instance()->cylinderRenumber(mapping.data());
}

void CylindersModel::cylinderAdded(struct dive *changed, int pos)
{
	if (d != changed)
		return;

	// The row was already inserted by the undo command. Just inform the model.
	beginInsertRows(QModelIndex(), pos, pos);
	endInsertRows();
}

void CylindersModel::cylinderRemoved(struct dive *changed, int pos)
{
	if (d != changed)
		return;

	// The row was already deleted by the undo command. Just inform the model.
	beginRemoveRows(QModelIndex(), pos, pos);
	endRemoveRows();
}

void CylindersModel::cylinderEdited(struct dive *changed, int pos)
{
	if (d != changed)
		return;

	dataChanged(index(pos, TYPE), index(pos, USE));
}

void CylindersModel::moveAtFirst(int cylid)
{
	if (!d)
		return;

	cylinder_t temp_cyl;

	beginMoveRows(QModelIndex(), cylid, cylid, QModelIndex(), 0);
	memmove(&temp_cyl, get_cylinder(d, cylid), sizeof(temp_cyl));
	for (int i = cylid - 1; i >= 0; i--)
		memmove(get_cylinder(d, i + 1), get_cylinder(d, i), sizeof(temp_cyl));
	memmove(get_cylinder(d, 0), &temp_cyl, sizeof(temp_cyl));

	// Create a mapping of cylinder indices:
	// 1) Fill mapping[0]..mapping[cyl] with 0..index
	// 2) Set mapping[cyl] to 0
	// 3) Fill mapping[cyl+1]..mapping[end] with cyl..
	std::vector<int> mapping(d->cylinders.nr);
	std::iota(mapping.begin(), mapping.begin() + cylid, 1);
	mapping[cylid] = 0;
	std::iota(mapping.begin() + (cylid + 1), mapping.end(), cylid);
	cylinder_renumber(d, mapping.data());
	if (inPlanner)
		DivePlannerPointsModel::instance()->cylinderRenumber(mapping.data());
	endMoveRows();
}

void CylindersModel::updateDecoDepths(pressure_t olddecopo2)
{
	if (!d)
		return;

	pressure_t decopo2;
	decopo2.mbar = prefs.decopo2;
	for (int i = 0; i < d->cylinders.nr; i++) {
		cylinder_t *cyl = get_cylinder(d, i);
		/* If the gas's deco MOD matches the old pO2, it will have been automatically calculated and should be updated.
		 * If they don't match, we should leave the user entered depth as it is */
		if (cyl->depth.mm == gas_mod(cyl->gasmix, olddecopo2, d, M_OR_FT(3, 10)).mm) {
			cyl->depth = gas_mod(cyl->gasmix, decopo2, d, M_OR_FT(3, 10));
		}
	}
	emit dataChanged(createIndex(0, 0), createIndex(d->cylinders.nr - 1, COLUMNS - 1));
}

void CylindersModel::updateTrashIcon()
{
	if (!d)
		return;

	emit dataChanged(createIndex(0, 0), createIndex(d->cylinders.nr - 1, 0));
}

bool CylindersModel::updateBestMixes()
{
	if (!d)
		return false;

	// Check if any of the cylinders are best mixes, update if needed
	bool gasUpdated = false;
	for (int i = 0; i < d->cylinders.nr; i++) {
		cylinder_t *cyl = get_cylinder(d, i);
		if (cyl->bestmix_o2) {
			cyl->gasmix.o2 = best_o2(d->maxdepth, d);
			// fO2 + fHe must not be greater than 1
			if (get_o2(cyl->gasmix) + get_he(cyl->gasmix) > 1000)
				cyl->gasmix.he.permille = 1000 - get_o2(cyl->gasmix);
			pressure_t modpO2;
			modpO2.mbar = prefs.decopo2;
			cyl->depth = gas_mod(cyl->gasmix, modpO2, d, M_OR_FT(3, 10));
			gasUpdated = true;
		}
		if (cyl->bestmix_he) {
			cyl->gasmix.he = best_he(d->maxdepth, d, prefs.o2narcotic, cyl->gasmix.o2);
			// fO2 + fHe must not be greater than 1
			if (get_o2(cyl->gasmix) + get_he(cyl->gasmix) > 1000)
				cyl->gasmix.o2.permille = 1000 - get_he(cyl->gasmix);
			gasUpdated = true;
		}
	}
	/* This slot is called when the bottom pO2 and END preferences are updated, we want to
	 * emit dataChanged so MOD and MND are refreshed, even if the gas mix hasn't been changed */
	if (gasUpdated)
		emit dataChanged(createIndex(0, 0), createIndex(d->cylinders.nr - 1, COLUMNS - 1));
	return gasUpdated;
}

void CylindersModel::cylindersReset(const QVector<dive *> &dives)
{
	// This model only concerns the currently displayed dive. If this is not among the
	// dives that had their cylinders reset, exit.
	if (!d || std::find(dives.begin(), dives.end(), d) == dives.end())
		return;

	// And update the model (the actual change was already performed in the backend)..
	beginResetModel();
	endResetModel();
}

// Save the cylinder in the given row so that we can revert if the user cancels a type-editing action.
void CylindersModel::initTempCyl(int row)
{
	if (!d || tempRow == row)
		return;
	clearTempCyl();
	const cylinder_t *cyl = get_cylinder(d, row);
	if (!cyl)
		return;

	tempRow = row;
	tempCyl = clone_cylinder(*cyl);

	dataChanged(index(row, TYPE), index(row, USE));
}

void CylindersModel::clearTempCyl()
{
	if (tempRow < 0)
		return;
	int oldRow = tempRow;
	tempRow = -1;
	free_cylinder(tempCyl);
	dataChanged(index(oldRow, TYPE), index(oldRow, USE));
}

void CylindersModel::commitTempCyl(int row)
{
	if (tempRow < 0)
		return;
	if (row != tempRow)
		return clearTempCyl(); // Huh? We are supposed to commit a different row than the one we stored?
	cylinder_t *cyl = get_cylinder(d, tempRow);
	if (!cyl)
		return;
	// Only submit a command if the type changed
	if (!same_string(cyl->type.description, tempCyl.type.description) || gettextFromC::tr(cyl->type.description) != QString(tempCyl.type.description)) {
		if (inPlanner) {
			std::swap(*cyl, tempCyl);
		} else {
			int count = Command::editCylinder(tempRow, tempCyl, Command::EditCylinderType::TYPE, false);
			emit divesEdited(count);
		}
	}
	free_cylinder(tempCyl);
	tempRow = -1;
}

CylindersModelFiltered::CylindersModelFiltered(QObject *parent) : QSortFilterProxyModel(parent),
	source(false) // Currently, only the EquipmentTab uses the filtered model.
{
	setSourceModel(&source);
}

void CylindersModelFiltered::updateDive(dive *d)
{
	source.updateDive(d);
}

void CylindersModelFiltered::clear()
{
	source.clear();
}

CylindersModel *CylindersModelFiltered::model()
{
	return &source;
}

bool CylindersModelFiltered::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
	return prefs.display_unused_tanks || source.cylinderUsed(source_row);
}