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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
|
// SPDX-License-Identifier: GPL-2.0
#include "qt-models/divetripmodel.h"
#include "qt-models/filtermodels.h"
#include "core/gettextfromc.h"
#include "core/metrics.h"
#include "core/trip.h"
#include "core/qthelper.h"
#include "core/subsurface-string.h"
#include "core/tag.h"
#include <QIcon>
#include <QDebug>
#include <memory>
#include <algorithm>
// 1) Base functions
static int nitrox_sort_value(const struct dive *dive)
{
int o2, he, o2max;
get_dive_gas(dive, &o2, &he, &o2max);
return he * 1000 + o2;
}
static QVariant dive_table_alignment(int column)
{
switch (column) {
case DiveTripModelBase::DEPTH:
case DiveTripModelBase::DURATION:
case DiveTripModelBase::TEMPERATURE:
case DiveTripModelBase::TOTALWEIGHT:
case DiveTripModelBase::SAC:
case DiveTripModelBase::OTU:
case DiveTripModelBase::MAXCNS:
// Right align numeric columns
return int(Qt::AlignRight | Qt::AlignVCenter);
// NR needs to be left aligned because its the indent marker for trips too
case DiveTripModelBase::NR:
case DiveTripModelBase::DATE:
case DiveTripModelBase::RATING:
case DiveTripModelBase::SUIT:
case DiveTripModelBase::CYLINDER:
case DiveTripModelBase::GAS:
case DiveTripModelBase::TAGS:
case DiveTripModelBase::PHOTOS:
case DiveTripModelBase::COUNTRY:
case DiveTripModelBase::BUDDIES:
case DiveTripModelBase::LOCATION:
return int(Qt::AlignLeft | Qt::AlignVCenter);
}
return QVariant();
}
QVariant DiveTripModelBase::tripData(const dive_trip *trip, int column, int role)
{
if (role == TRIP_ROLE)
return QVariant::fromValue(const_cast<dive_trip *>(trip)); // Not nice: casting away a const
if (role == Qt::DisplayRole) {
switch (column) {
case DiveTripModelBase::NR:
QString shownText;
bool oneDayTrip = trip_is_single_day(trip);
int countShown = trip_shown_dives(trip);
if (countShown < trip->dives.nr)
shownText = tr("(%1 shown)").arg(countShown);
if (!empty_string(trip->location))
return QString(trip->location) + ", " + get_trip_date_string(trip_date(trip), trip->dives.nr, oneDayTrip) + " "+ shownText;
else
return get_trip_date_string(trip_date(trip), trip->dives.nr, oneDayTrip) + shownText;
}
}
return QVariant();
}
static const QString icon_names[4] = {
QStringLiteral(":zero"),
QStringLiteral(":photo-in-icon"),
QStringLiteral(":photo-out-icon"),
QStringLiteral(":photo-in-out-icon")
};
static int countPhotos(const struct dive *d)
{ // Determine whether dive has pictures, and whether they were taken during or before/after dive.
const int bufperiod = 120; // A 2-min buffer period. Photos within 2 min of dive are assumed as
int diveTotaltime = dive_endtime(d) - d->when; // taken during the dive, not before/after.
int pic_offset, icon_index = 0;
FOR_EACH_PICTURE (d) { // Step through each of the pictures for this dive:
pic_offset = picture->offset.seconds;
if ((pic_offset < -bufperiod) | (pic_offset > diveTotaltime+bufperiod)) {
icon_index |= 0x02; // If picture is before/after the dive
// then set the appropriate bit ...
} else {
icon_index |= 0x01; // else set the bit for picture during the dive
}
}
return icon_index; // return value: 0=no pictures; 1=pictures during dive;
} // 2=pictures before/after; 3=pictures during as well as before/after
static QString displayDuration(const struct dive *d)
{
if (prefs.units.show_units_table)
return get_dive_duration_string(d->duration.seconds, gettextFromC::tr("h"), gettextFromC::tr("min"), "", ":", d->dc.divemode == FREEDIVE);
else
return get_dive_duration_string(d->duration.seconds, "", "", "", ":", d->dc.divemode == FREEDIVE);
}
static QString displayTemperature(const struct dive *d, bool units)
{
if (!d->watertemp.mkelvin)
return QString();
return get_temperature_string(d->watertemp, units);
}
static QString displaySac(const struct dive *d, bool units)
{
if (!d->sac)
return QString();
QString s = get_volume_string(d->sac, units);
return units ? s + gettextFromC::tr("/min") : s;
}
static QString displayWeight(const struct dive *d, bool units)
{
QString s = weight_string(total_weight(d));
if (!units)
return s;
else if (get_units()->weight == units::KG)
return s + gettextFromC::tr("kg");
else
return s + gettextFromC::tr("lbs");
}
QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role)
{
switch (role) {
case Qt::TextAlignmentRole:
return dive_table_alignment(column);
case Qt::DisplayRole:
switch (column) {
case NR:
return d->number;
case DATE:
return get_dive_date_string(d->when);
case DEPTH:
return get_depth_string(d->maxdepth, prefs.units.show_units_table);
case DURATION:
return displayDuration(d);
case TEMPERATURE:
return displayTemperature(d, prefs.units.show_units_table);
case TOTALWEIGHT:
return displayWeight(d, prefs.units.show_units_table);
case SUIT:
return QString(d->suit);
case CYLINDER:
return QString(d->cylinder[0].type.description);
case SAC:
return displaySac(d, prefs.units.show_units_table);
case OTU:
return d->otu;
case MAXCNS:
if (prefs.units.show_units_table)
return QString("%1%").arg(d->maxcns);
else
return d->maxcns;
case TAGS:
return get_taglist_string(d->tag_list);
case PHOTOS:
break;
case COUNTRY:
return QString(get_dive_country(d));
case BUDDIES:
return QString(d->buddy);
case LOCATION:
return QString(get_dive_location(d));
case GAS:
char *gas_string = get_dive_gas_string(d);
QString ret(gas_string);
free(gas_string);
return ret;
}
break;
case Qt::DecorationRole:
switch (column) {
//TODO: ADD A FLAG
case COUNTRY:
return QVariant();
case LOCATION:
if (dive_has_gps_location(d)) {
IconMetrics im = defaultIconMetrics();
return QIcon(":globe-icon").pixmap(im.sz_small, im.sz_small);
}
break;
case PHOTOS:
if (d->picture_list)
{
IconMetrics im = defaultIconMetrics();
return QIcon(icon_names[countPhotos(d)]).pixmap(im.sz_small, im.sz_small);
} // If there are photos, show one of the three photo icons: fish= photos during dive;
break; // sun=photos before/after dive; sun+fish=photos during dive as well as before/after
}
break;
case Qt::ToolTipRole:
switch (column) {
case NR:
return tr("#");
case DATE:
return tr("Date");
case RATING:
return tr("Rating");
case DEPTH:
return tr("Depth(%1)").arg((get_units()->length == units::METERS) ? tr("m") : tr("ft"));
case DURATION:
return tr("Duration");
case TEMPERATURE:
return tr("Temp.(°%1)").arg((get_units()->temperature == units::CELSIUS) ? "C" : "F");
case TOTALWEIGHT:
return tr("Weight(%1)").arg((get_units()->weight == units::KG) ? tr("kg") : tr("lbs"));
case SUIT:
return tr("Suit");
case CYLINDER:
return tr("Cylinder");
case GAS:
return tr("Gas");
case SAC:
const char *unit;
get_volume_units(0, NULL, &unit);
return tr("SAC(%1)").arg(QString(unit).append(tr("/min")));
case OTU:
return tr("OTU");
case MAXCNS:
return tr("Max. CNS");
case TAGS:
return tr("Tags");
case PHOTOS:
return tr("Media before/during/after dive");
case COUNTRY:
return tr("Country");
case BUDDIES:
return tr("Buddy");
case LOCATION:
return tr("Location");
}
break;
case STAR_ROLE:
return d->rating;
case DIVE_ROLE:
return QVariant::fromValue(const_cast<dive *>(d)); // Not nice: casting away a const
case DIVE_IDX:
return get_divenr(d);
case SELECTED_ROLE:
return d->selected;
}
return QVariant();
}
QVariant DiveTripModelBase::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Vertical)
return QVariant();
switch (role) {
case Qt::TextAlignmentRole:
return dive_table_alignment(section);
case Qt::FontRole:
return defaultModelFont();
case Qt::InitialSortOrderRole:
// By default, sort NR and DATE descending, everything else ascending.
return section == NR || section == DATE ? Qt::DescendingOrder : Qt::AscendingOrder;
case Qt::DisplayRole:
switch (section) {
case NR:
return tr("#");
case DATE:
return tr("Date");
case RATING:
return tr("Rating");
case DEPTH:
return tr("Depth");
case DURATION:
return tr("Duration");
case TEMPERATURE:
return tr("Temp.");
case TOTALWEIGHT:
return tr("Weight");
case SUIT:
return tr("Suit");
case CYLINDER:
return tr("Cylinder");
case GAS:
return tr("Gas");
case SAC:
return tr("SAC");
case OTU:
return tr("OTU");
case MAXCNS:
return tr("Max CNS");
case TAGS:
return tr("Tags");
case PHOTOS:
return tr("Media");
case COUNTRY:
return tr("Country");
case BUDDIES:
return tr("Buddy");
case LOCATION:
return tr("Location");
}
break;
case Qt::ToolTipRole:
switch (section) {
case NR:
return tr("#");
case DATE:
return tr("Date");
case RATING:
return tr("Rating");
case DEPTH:
return tr("Depth(%1)").arg((get_units()->length == units::METERS) ? tr("m") : tr("ft"));
case DURATION:
return tr("Duration");
case TEMPERATURE:
return tr("Temp.(°%1)").arg((get_units()->temperature == units::CELSIUS) ? "C" : "F");
case TOTALWEIGHT:
return tr("Weight(%1)").arg((get_units()->weight == units::KG) ? tr("kg") : tr("lbs"));
case SUIT:
return tr("Suit");
case CYLINDER:
return tr("Cylinder");
case GAS:
return tr("Gas");
case SAC:
const char *unit;
get_volume_units(0, NULL, &unit);
return tr("SAC(%1)").arg(QString(unit).append(tr("/min")));
case OTU:
return tr("OTU");
case MAXCNS:
return tr("Max CNS");
case TAGS:
return tr("Tags");
case PHOTOS:
return tr("Media before/during/after dive");
case BUDDIES:
return tr("Buddy");
case LOCATION:
return tr("Location");
}
break;
}
return QVariant();
}
static std::unique_ptr<DiveTripModelBase> currentModel;
DiveTripModelBase *DiveTripModelBase::instance()
{
if (!currentModel)
resetModel(TREE);
return currentModel.get();
}
void DiveTripModelBase::resetModel(DiveTripModelBase::Layout layout)
{
if (layout == TREE)
currentModel.reset(new DiveTripModelTree);
else
currentModel.reset(new DiveTripModelList);
}
DiveTripModelBase::DiveTripModelBase(QObject *parent) : QAbstractItemModel(parent)
{
}
int DiveTripModelBase::columnCount(const QModelIndex&) const
{
return COLUMNS;
}
Qt::ItemFlags DiveTripModelBase::flags(const QModelIndex &index) const
{
dive *d = diveOrNull(index);
Qt::ItemFlags base = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
// Only dives have editable fields and only the number is editable
return d && index.column() == NR ? base | Qt::ItemIsEditable : base;
}
bool DiveTripModelBase::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == SHOWN_ROLE)
return setShown(index, value.value<bool>());
// We only support setting of data for dives and there, only the number.
dive *d = diveOrNull(index);
if (!d)
return false;
if (role != Qt::EditRole)
return false;
if (index.column() != NR)
return false;
int v = value.toInt();
if (v == 0)
return false;
// Only accept numbers that are not already in use by other dives.
int i;
struct dive *dive;
for_each_dive (i, dive) {
if (dive->number == v)
return false;
}
d->number = v;
mark_divelist_changed(true);
return true;
}
// Find a range of matching elements in a vector.
// Input parameters:
// v: vector to be searched
// first: first element to search
// cond: a function that is fed elements and returns an integer:
// - >0: matches
// - 0: doesn't match
// - <0: stop searching, no more elements will be found
// cond is called exactly once per element and from the beginning of the range.
// Returns a pair [first, last) with usual C++ semantics: last-first is the size of the found range.
// If no items were found, first and last are set to the size of the vector.
template <typename Vector, typename Predicate>
std::pair<int, int> findRangeIf(const Vector &v, int first, Predicate cond)
{
int size = (int)v.size();
for (int i = first; i < size; ++i) {
int res = cond(v[i]);
if (res > 0) {
for (int j = i + 1; j < size; ++j) {
if (cond(v[j]) <= 0)
return { i, j };
}
return { i, size };
} else if (res < 0) {
break;
}
}
return { size, size };
}
// Ideally, Qt's model/view functions are processed in batches of contiguous
// items. Therefore, this template is used to process actions on ranges of
// contiguous elements of a vector.
// Input paremeters:
// - items: vector to process, wich must allow random access via [] and the size() function
// - cond: a predicate that is tested for each element. contiguous ranges of elements which
// test for true are collected. cond is fed an element and should return:
// - >0: matches
// - 0: doesn't match
// - <0: stop searching, no more elements will be found
// - action: action that is called with the vector, first and last element of the range.
template<typename Vector, typename Predicate, typename Action>
void processRanges(Vector &items, Predicate cond, Action action)
{
// Note: the "i++" is correct: We know that the last element tested
// negatively -> we can skip it. Thus we avoid checking any element
// twice.
for(int i = 0;; i++) {
std::pair<int,int> range = findRangeIf(items, i, cond);
if (range.first >= (int)items.size())
break;
int delta = action(items, range.first, range.second);
i = range.second + delta;
}
}
// processRangesZip() is a refined version of processRanges(), which operates on two vectors.
// The vectors are supposed to be sorted equivalently. That is, the first matching
// item will of the first vector will match to the first item of the second vector.
// It is supposed that all elements of the second vector will match to an element of
// the first vector.
// Input parameters:
// - items1: vector to process, wich must allow random access via [] and the size() function
// - items2: second vector to process. every item in items2 must match to an item in items1
// in ascending order.
// - cond1: a predicate that is tested for each element of items1 with the next unmatched element
// of items2. returns a boolean
// - action: action that is called with the vectors, first and last element of the first range
// and first element of the last range.
template<typename Vector1, typename Vector2, typename Predicate, typename Action>
void processRangesZip(Vector1 &items1, Vector2 &items2, Predicate cond, Action action)
{
int actItem = 0;
processRanges(items1,
[&](typename Vector1::const_reference &e) mutable -> int { // Condition. Marked mutable so that it can change actItem
if (actItem >= items2.size())
return -1; // No more items -> bail
if (!cond(e, items2[actItem]))
return 0;
++actItem;
return 1;
},
[&](Vector1 &v1, int from, int to) { // Action
return action(v1, items2, from, to, actItem);
});
}
// Add items from vector "v2" to vector "v1" in batches of contiguous objects.
// The items are inserted at places according to a sort order determined by "comp".
// "v1" and "v2" are supposed to be ordered accordingly.
// TODO: We might use binary search with std::lower_bound(), but not sure if it's worth it.
// Input parameters:
// - v1: destination vector
// - v2: source vector
// - comp: compare-function, which is fed elements from v2 and v1. returns true for "insert here".
// - adder: performs the insertion. Perameters: v1, v2, insertion index, from, to range in v2.
template <typename Vector1, typename Vector2, typename Comparator, typename Inserter>
void addInBatches(Vector1 &v1, const Vector2 &v2, Comparator comp, Inserter insert)
{
int idx = 0; // Index where dives will be inserted
int i, j; // Begin and end of range to insert
for (i = 0; i < (int)v2.size(); i = j) {
for (; idx < (int)v1.size() && !comp(v2[i], v1[idx]); ++idx)
; // Pass
// We found the index of the first item to add.
// Now search how many items we should insert there.
if (idx == (int)v1.size()) {
// We were at end -> insert the remaining items
j = v2.size();
} else {
for (j = i + 1; j < (int)v2.size() && comp(v2[j], v1[idx]); ++j)
; // Pass
}
// Now add the batch
insert(v1, v2, idx, i, j);
// Skip over inserted dives for searching the new insertion position plus one.
// If we added at the end, the loop will end anyway.
idx += j - i + 1;
}
}
// 2) TreeModel functions
DiveTripModelTree::DiveTripModelTree(QObject *parent) : DiveTripModelBase(parent)
{
// Stay informed of changes to the divelist
connect(&diveListNotifier, &DiveListNotifier::divesAdded, this, &DiveTripModelTree::divesAdded);
connect(&diveListNotifier, &DiveListNotifier::divesDeleted, this, &DiveTripModelTree::divesDeleted);
connect(&diveListNotifier, &DiveListNotifier::divesChanged, this, &DiveTripModelTree::divesChanged);
connect(&diveListNotifier, &DiveListNotifier::divesMovedBetweenTrips, this, &DiveTripModelTree::divesMovedBetweenTrips);
connect(&diveListNotifier, &DiveListNotifier::divesTimeChanged, this, &DiveTripModelTree::divesTimeChanged);
connect(&diveListNotifier, &DiveListNotifier::divesSelected, this, &DiveTripModelTree::divesSelected);
connect(&diveListNotifier, &DiveListNotifier::tripChanged, this, &DiveTripModelTree::tripChanged);
// Fill model
for (int i = 0; i < dive_table.nr ; ++i) {
dive *d = get_dive(i);
update_cylinder_related_info(d);
dive_trip_t *trip = d->divetrip;
// If this dive doesn't have a trip, add as top-level item.
if (!trip) {
items.emplace_back(d);
continue;
}
// Check if that trip is already known to us: search for the first item
// that corresponds to that trip
auto it = std::find_if(items.begin(), items.end(), [trip](const Item &item)
{ return item.d_or_t.trip == trip; });
if (it == items.end()) {
// We didn't find an entry for this trip -> add one
items.emplace_back(trip, d);
} else {
// We found the trip -> simply add the dive
it->dives.push_back(d);
}
}
}
int DiveTripModelTree::rowCount(const QModelIndex &parent) const
{
// No parent means top level - return the number of top-level items
if (!parent.isValid())
return items.size();
// If the parent has a parent, this is a dive -> no entries
if (parent.parent().isValid())
return 0;
// If this is outside of our top-level list -> no entries
int row = parent.row();
if (row < 0 || row >= (int)items.size())
return 0;
// Only trips have items
const Item &entry = items[parent.row()];
return entry.d_or_t.trip ? entry.dives.size() : 0;
}
static const quintptr noParent = ~(quintptr)0; // This is the "internalId" marker for top-level item
QModelIndex DiveTripModelTree::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
// In the "internalId", we store either ~0 for top-level items or the
// index of the parent item. A top-level item has an invalid parent.
return createIndex(row, column, parent.isValid() ? parent.row() : noParent);
}
QModelIndex DiveTripModelTree::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
// In the "internalId", we store either ~0 for top-level items
// or the index of the parent item.
quintptr id = index.internalId();
if (id == noParent)
return QModelIndex();
// Parent must be top-level item
return createIndex(id, 0, noParent);
}
DiveTripModelTree::Item::Item(dive_trip *t, const QVector<dive *> &divesIn) : d_or_t{nullptr, t},
dives(divesIn.toStdVector()),
shown(std::any_of(dives.begin(), dives.end(), [](dive *d){ return !d->hidden_by_filter; }))
{
}
DiveTripModelTree::Item::Item(dive_trip *t, dive *d) : d_or_t{nullptr, t},
dives({ d }),
shown(!d->hidden_by_filter)
{
}
DiveTripModelTree::Item::Item(dive *d) : d_or_t{d, nullptr},
shown(!d->hidden_by_filter)
{
}
bool DiveTripModelTree::Item::isDive(const dive *d) const
{
return d_or_t.dive == d;
}
dive *DiveTripModelTree::Item::getDive() const
{
return d_or_t.dive;
}
timestamp_t DiveTripModelTree::Item::when() const
{
return d_or_t.trip ? trip_date(d_or_t.trip) : d_or_t.dive->when;
}
dive_or_trip DiveTripModelTree::tripOrDive(const QModelIndex &index) const
{
if (!index.isValid())
return { nullptr, nullptr };
QModelIndex parent = index.parent();
// An invalid parent means that we're at the top-level
if (!parent.isValid())
return items[index.row()].d_or_t;
// Otherwise, we're at a leaf -> thats a dive
return { items[parent.row()].dives[index.row()], nullptr };
}
dive *DiveTripModelTree::diveOrNull(const QModelIndex &index) const
{
return tripOrDive(index).dive;
}
// Set the shown flag that marks whether an entry is shown or hidden by the filter.
// The flag is cached for top-level items (trips and dives outside of trips).
// For dives that belong to a trip (i.e. non-top-level items), the flag is
// simply written through to the core.
bool DiveTripModelTree::setShown(const QModelIndex &idx, bool shown)
{
if (!idx.isValid())
return false;
QModelIndex parent = idx.parent();
if (!parent.isValid()) {
// An invalid parent means that we're at the top-level
Item &item = items[idx.row()];
item.shown = shown; // Cache the flag.
if (item.d_or_t.dive) {
// This is a dive -> also register the flag in the core
filter_dive(item.d_or_t.dive, shown);
}
} else {
// We're not at the top-level. This must be a dive, therefore
// simply write the flag through to the core.
const Item &parentItem = items[parent.row()];
filter_dive(parentItem.dives[idx.row()], shown);
}
return true;
}
QVariant DiveTripModelTree::data(const QModelIndex &index, int role) const
{
if (role == SHOWN_ROLE) {
QModelIndex parent = index.parent();
// An invalid parent means that we're at the top-level
if (!parent.isValid())
return items[index.row()].shown;
return !items[parent.row()].dives[index.row()]->hidden_by_filter;
}
// Set the font for all items alike
if (role == Qt::FontRole)
return defaultModelFont();
dive_or_trip entry = tripOrDive(index);
if (entry.trip)
return tripData(entry.trip, index.column(), role);
else if (entry.dive)
return diveData(entry.dive, index.column(), role);
else
return QVariant();
}
// After a trip changed, the top level might need to be reordered.
// Move the item and send a "data-changed" signal.
void DiveTripModelTree::topLevelChanged(int idx)
{
if (idx < 0 || idx >= (int)items.size())
return;
// First, try to move backwards
int newIdx = idx;
while (newIdx > 0 && dive_or_trip_less_than(items[idx].d_or_t, items[newIdx - 1].d_or_t))
--newIdx;
// If that didn't change, try to move forward
if (newIdx == idx) {
++newIdx;
while (newIdx < (int)items.size() && !dive_or_trip_less_than(items[idx].d_or_t, items[newIdx].d_or_t))
++newIdx;
}
// If index changed, move items
if (newIdx != idx && newIdx != idx + 1) {
beginMoveRows(QModelIndex(), idx, idx, QModelIndex(), newIdx);
moveInVector(items, idx, idx + 1, newIdx);
endMoveRows();
}
// Finally, inform UI of changed trip header
QModelIndex tripIdx = createIndex(newIdx, 0, noParent);
dataChanged(tripIdx, tripIdx);
}
void DiveTripModelTree::addDivesToTrip(int trip, const QVector<dive *> &dives)
{
// Construct the parent index, ie. the index of the trip.
QModelIndex parent = createIndex(trip, 0, noParent);
addInBatches(items[trip].dives, dives,
[](dive *d, dive *d2) { return dive_less_than(d, d2); }, // comp
[&](std::vector<dive *> &items, const QVector<dive *> &dives, int idx, int from, int to) { // inserter
beginInsertRows(parent, idx, idx + to - from - 1);
items.insert(items.begin() + idx, dives.begin() + from, dives.begin() + to);
endInsertRows();
});
// If necessary, move the trip
topLevelChanged(trip);
}
int DiveTripModelTree::findTripIdx(const dive_trip *trip) const
{
for (int i = 0; i < (int)items.size(); ++i)
if (items[i].d_or_t.trip == trip)
return i;
return -1;
}
int DiveTripModelTree::findDiveIdx(const dive *d) const
{
for (int i = 0; i < (int)items.size(); ++i)
if (items[i].isDive(d))
return i;
return -1;
}
int DiveTripModelTree::findDiveInTrip(int tripIdx, const dive *d) const
{
const Item &item = items[tripIdx];
for (int i = 0; i < (int)item.dives.size(); ++i)
if (item.dives[i] == d)
return i;
return -1;
}
int DiveTripModelTree::findInsertionIndex(const dive_trip *trip) const
{
dive_or_trip d_or_t{ nullptr, (dive_trip *)trip };
for (int i = 0; i < (int)items.size(); ++i) {
if (dive_or_trip_less_than(d_or_t, items[i].d_or_t))
return i;
}
return items.size();
}
// This function is used to compare a dive to an arbitrary entry (dive or trip).
// For comparing two dives, use the core function dive_less_than_entry, which
// effectively sorts by timestamp.
// If comparing to a trip, the policy for equal-times is to place the dives
// before the trip in the case of equal timestamps.
bool DiveTripModelTree::dive_before_entry(const dive *d, const Item &entry)
{
dive_or_trip d_or_t { (dive *)d, nullptr };
return dive_or_trip_less_than(d_or_t, entry.d_or_t);
}
void DiveTripModelTree::divesAdded(dive_trip *trip, bool addTrip, const QVector<dive *> &dives)
{
if (!trip) {
// This is outside of a trip. Add dives at the top-level in batches.
addInBatches(items, dives,
&dive_before_entry, // comp
[&](std::vector<Item> &items, const QVector<dive *> &dives, int idx, int from, int to) { // inserter
beginInsertRows(QModelIndex(), idx, idx + to - from - 1);
items.insert(items.begin() + idx, dives.begin() + from, dives.begin() + to);
endInsertRows();
});
} else if (addTrip) {
// We're supposed to add the whole trip. Just insert the trip.
int idx = findInsertionIndex(trip); // Find the place where to insert the trip
beginInsertRows(QModelIndex(), idx, idx);
items.insert(items.begin() + idx, { trip, dives });
endInsertRows();
} else {
// Ok, we have to add dives to an existing trip
// Find the trip...
int idx = findTripIdx(trip);
if (idx < 0) {
// We don't know the trip - this shouldn't happen. We seem to have
// missed some signals!
qWarning() << "DiveTripModelTree::divesAdded(): unknown trip";
return;
}
// ...and add dives.
addDivesToTrip(idx, dives);
}
}
void DiveTripModelTree::divesDeleted(dive_trip *trip, bool deleteTrip, const QVector<dive *> &dives)
{
if (!trip) {
// This is outside of a trip. Delete top-level dives in batches.
processRangesZip(items, dives,
[](const Item &e, dive *d) { return e.getDive() == d; }, // Condition
[&](std::vector<Item> &items, const QVector<dive *> &, int from, int to, int) -> int { // Action
beginRemoveRows(QModelIndex(), from, to - 1);
items.erase(items.begin() + from, items.begin() + to);
endRemoveRows();
return from - to; // Delta: negate the number of items deleted
});
} else {
// Find the trip
int idx = findTripIdx(trip);
if (idx < 0) {
// We don't know the trip - this shouldn't happen. We seem to have
// missed some signals!
qWarning() << "DiveTripModelTree::divesDeleted(): unknown trip";
return;
}
if (deleteTrip) {
// We're supposed to delete the whole trip. Nice, we don't have to
// care about individual dives. Just remove the row.
beginRemoveRows(QModelIndex(), idx, idx);
items.erase(items.begin() + idx);
endRemoveRows();
} else {
// Construct the parent index, ie. the index of the trip.
QModelIndex parent = createIndex(idx, 0, noParent);
// Delete a number of dives in a trip. We do this range-wise.
processRangesZip(items[idx].dives, dives,
[](dive *d1, dive *d2) { return d1 == d2; }, // Condition
[&](std::vector<dive *> &diveList, const QVector<dive *> &, int from, int to, int) -> int { // Action
beginRemoveRows(parent, from, to - 1);
diveList.erase(diveList.begin() + from, diveList.begin() + to);
endRemoveRows();
return from - to; // Delta: negate the number of items deleted
});
// If necessary, move the trip
topLevelChanged(idx);
}
}
}
// The tree-version of the model wants to process the dives per trip.
// This template takes a vector of dives and calls a function batchwise for each trip.
template<typename Function>
void processByTrip(QVector<dive *> dives, Function action)
{
// Sort lexicographically by trip then according to the dive_less_than() function.
std::sort(dives.begin(), dives.end(), [](const dive *d1, const dive *d2)
{ return d1->divetrip == d2->divetrip ? dive_less_than(d1, d2) : d1->divetrip < d2->divetrip; });
// Then, process the dives in batches by trip
int i, j; // Begin and end of batch
for (i = 0; i < dives.size(); i = j) {
dive_trip *trip = dives[i]->divetrip;
for (j = i + 1; j < dives.size() && dives[j]->divetrip == trip; ++j)
; // pass
// Copy dives into a QVector. Some sort of "range_view" would be ideal.
QVector<dive *> divesInTrip(j - i);
for (int k = i; k < j; ++k)
divesInTrip[k - i] = dives[k];
// Finally, emit the signal
action(trip, divesInTrip);
}
}
void DiveTripModelTree::divesChanged(const QVector<dive *> &dives)
{
processByTrip(dives, [this] (dive_trip *trip, const QVector<dive *> &divesInTrip)
{ divesChangedTrip(trip, divesInTrip); });
}
void DiveTripModelTree::divesChangedTrip(dive_trip *trip, const QVector<dive *> &dives)
{
// Update filter flags. TODO: The filter should update the flag by itself when
// recieving the signals below.
bool diveChanged = false;
for (dive *d: dives)
diveChanged |= MultiFilterSortModel::instance()->updateDive(d);
if (!trip) {
// This is outside of a trip. Process top-level items range-wise.
// Since we know that the dive list is sorted, we will only ever search for the first element
// in dives as this must be the first that we encounter. Once we find a range, increase the
// index accordingly.
processRangesZip(items, dives,
[](const Item &e, dive *d) { return e.getDive() == d; }, // Condition
[&](const std::vector<Item> &, const QVector<dive *> &, int from, int to, int) -> int { // Action
// TODO: We might be smarter about which columns changed!
dataChanged(createIndex(from, 0, noParent), createIndex(to - 1, COLUMNS - 1, noParent));
return 0; // No items added or deleted
});
} else {
// Find the trip.
int idx = findTripIdx(trip);
if (idx < 0) {
// We don't know the trip - this shouldn't happen. We seem to have
// missed some signals!
qWarning() << "DiveTripModelTree::divesChanged(): unknown trip";
return;
}
// Change the dives in the trip. We do this range-wise.
processRangesZip(items[idx].dives, dives,
[](const dive *d1, const dive *d2) { return d1 == d2; }, // Condition (std::equal_to only in C++14)
[&](const std::vector<dive *> &, const QVector<dive *> &, int from, int to, int) -> int { // Action
// TODO: We might be smarter about which columns changed!
dataChanged(createIndex(from, 0, idx), createIndex(to - 1, COLUMNS - 1, idx));
return 0; // No items added or deleted
});
// If necessary, move the trip
topLevelChanged(idx);
// If a dive changed, re-render the trip in the list [or actually make it (in)visible].
if (diveChanged)
dataChanged(createIndex(idx, 0, noParent), createIndex(idx, 0, noParent));
}
}
void DiveTripModelTree::tripChanged(dive_trip *trip, TripField)
{
int idx = findTripIdx(trip);
if (idx < 0) {
// We don't know the trip - this shouldn't happen. We seem to have
// missed some signals!
qWarning() << "DiveTripModelTree::divesChanged(): unknown trip";
return;
}
dataChanged(createIndex(idx, 0, noParent), createIndex(idx, COLUMNS - 1, noParent));
}
static QVector<dive *> filterSelectedDives(const QVector<dive *> &dives)
{
QVector<dive *> res;
res.reserve(dives.size());
for (dive *d: dives)
if (d->selected)
res.append(d);
return res;
}
void DiveTripModelTree::divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector<dive *> &dives)
{
// Move dives between trips. This is an "interesting" problem, as we might
// move from trip to trip, from trip to top-level or from top-level to trip.
// Moreover, we might have to add a trip first or delete an old trip.
// For simplicity, we will simply use the already existing divesAdded() / divesDeleted()
// functions. This *is* cheating. But let's just try this and see how graceful
// this is handled by Qt and if it gives some ugly UI behavior!
// But first let's just rule out the trivial case: same-to-same trip move.
if (from == to)
return;
// Cheating!
// Unfortunately, removing the dives means that their selection is lost.
// Thus, remember the selection and re-add it later.
QVector<dive *> selectedDives = filterSelectedDives(dives);
divesAdded(to, createTo, dives);
divesDeleted(from, deleteFrom, dives);
}
void DiveTripModelTree::divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives)
{
processByTrip(dives, [this, delta] (dive_trip *trip, const QVector<dive *> &divesInTrip)
{ divesTimeChangedTrip(trip, delta, divesInTrip); });
}
void DiveTripModelTree::divesTimeChangedTrip(dive_trip *trip, timestamp_t delta, const QVector<dive *> &dives)
{
// As in the case of divesMovedBetweenTrips(), this is a tricky, but solvable, problem.
// We have to consider the direction (delta < 0 or delta >0) and that dives at their destination
// position have different contiguous batches than at their original position. For now,
// cheat and simply do a remove/add pair. Note that for this to work it is crucial the the
// order of the dives don't change. This is indeed the case, as all starting-times were
// moved by the same delta.
// Cheating!
// Unfortunately, removing the dives means that their selection is lost.
// Thus, remember the selection and re-add it later.
QVector<dive *> selectedDives = filterSelectedDives(dives);
divesDeleted(trip, false, dives);
divesAdded(trip, false, dives);
}
void DiveTripModelTree::divesSelected(const QVector<dive *> &dives, dive *current)
{
// We got a number of dives that have been selected. Turn this into QModelIndexes and
// emit a signal, so that views can change the selection.
QVector<QModelIndex> indexes;
indexes.reserve(dives.count());
processByTrip(dives, [this, &indexes] (dive_trip *trip, const QVector<dive *> &divesInTrip)
{ divesSelectedTrip(trip, divesInTrip, indexes); });
emit selectionChanged(indexes);
// The current dive has changed. Transform the current dive into an index and pass it on to the view.
if (!current) {
emit newCurrentDive(QModelIndex()); // No current dive -> tell view to clear current index with an invalid index
return;
}
dive_trip *trip = current->divetrip;
if (!trip) {
// Outside of a trip - search top-level.
int idx = findDiveIdx(current);
if (idx < 0) {
// We don't know this dive. Something is wrong. Warn and bail.
qWarning() << "DiveTripModelTree::diveSelected(): unknown top-level dive";
emit newCurrentDive(QModelIndex());
return;
}
emit newCurrentDive(createIndex(idx, 0, noParent));
} else {
int idx = findTripIdx(trip);
if (idx < 0) {
// We don't know the trip - this shouldn't happen. Warn and bail.
qWarning() << "DiveTripModelTree::diveSelected(): unknown trip";
emit newCurrentDive(QModelIndex());
return;
}
int diveIdx = findDiveInTrip(idx, current);
if (diveIdx < 0) {
// We don't know this dive. Something is wrong. Warn and bail.
qWarning() << "DiveTripModelTree::diveSelected(): unknown dive";
emit newCurrentDive(QModelIndex());
return;
}
emit newCurrentDive(createIndex(diveIdx, 0, idx));
}
}
void DiveTripModelTree::divesSelectedTrip(dive_trip *trip, const QVector<dive *> &dives, QVector<QModelIndex> &indexes)
{
if (!trip) {
// This is at the top level.
// Since both lists are sorted, we can do this linearly. Perhaps a binary search
// would be better?
int j = 0; // Index in items array
for (int i = 0; i < dives.size(); ++i) {
while (j < (int)items.size() && !items[j].isDive(dives[i]))
++j;
if (j >= (int)items.size())
break;
indexes.append(createIndex(j, 0, noParent));
}
} else {
// Find the trip.
int idx = findTripIdx(trip);
if (idx < 0) {
// We don't know the trip - this shouldn't happen. We seem to have
// missed some signals!
qWarning() << "DiveTripModelTree::diveSelectedTrip(): unknown trip";
return;
}
// Locate the indices inside the trip.
// Since both lists are sorted, we can do this linearly. Perhaps a binary search
// would be better?
int j = 0; // Index in items array
const Item &entry = items[idx];
for (int i = 0; i < dives.size(); ++i) {
while (j < (int)entry.dives.size() && entry.dives[j] != dives[i])
++j;
if (j >= (int)entry.dives.size())
break;
indexes.append(createIndex(j, 0, idx));
}
}
}
void DiveTripModelTree::filterFinished()
{
// If the filter finished, update all trip items to show the correct number of displayed dives
// in each trip. Without doing this, only trip headers of expanded trips were updated.
for (int idx = 0; idx < (int)items.size(); ++idx) {
QModelIndex tripIndex = createIndex(idx, 0, noParent);
dataChanged(tripIndex, tripIndex);
}
}
bool DiveTripModelTree::lessThan(const QModelIndex &i1, const QModelIndex &i2) const
{
// In tree mode we don't support any sorting!
// Simply keep the original position.
return i1.row() < i2.row();
}
// 3) ListModel functions
DiveTripModelList::DiveTripModelList(QObject *parent) : DiveTripModelBase(parent)
{
// Stay informed of changes to the divelist
connect(&diveListNotifier, &DiveListNotifier::divesAdded, this, &DiveTripModelList::divesAdded);
connect(&diveListNotifier, &DiveListNotifier::divesDeleted, this, &DiveTripModelList::divesDeleted);
connect(&diveListNotifier, &DiveListNotifier::divesChanged, this, &DiveTripModelList::divesChanged);
// Does nothing in list-view
//connect(&diveListNotifier, &DiveListNotifier::divesMovedBetweenTrips, this, &DiveTripModelList::divesMovedBetweenTrips);
connect(&diveListNotifier, &DiveListNotifier::divesTimeChanged, this, &DiveTripModelList::divesTimeChanged);
connect(&diveListNotifier, &DiveListNotifier::divesSelected, this, &DiveTripModelList::divesSelected);
// Fill model
items.reserve(dive_table.nr);
for (int i = 0; i < dive_table.nr ; ++i)
items.push_back(get_dive(i));
}
int DiveTripModelList::rowCount(const QModelIndex &parent) const
{
// In list-mode there is only one level, i.e only top-level
// (=invalid parent) has items.
return parent.isValid() ? 0 : items.size();
}
QModelIndex DiveTripModelList::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column);
}
QModelIndex DiveTripModelList::parent(const QModelIndex &index) const
{
// In list-mode there is only one level, i.e. no parent
return QModelIndex();
}
dive *DiveTripModelList::diveOrNull(const QModelIndex &index) const
{
int row = index.row();
if (row < 0 || row > (int)items.size())
return nullptr;
return items[row];
}
bool DiveTripModelList::setShown(const QModelIndex &idx, bool shown)
{
dive *d = diveOrNull(idx);
if (!d)
return false;
filter_dive(d, shown);
return true;
}
QVariant DiveTripModelList::data(const QModelIndex &index, int role) const
{
// Set the font for all items alike
if (role == Qt::FontRole)
return defaultModelFont();
dive *d = diveOrNull(index);
if (role == SHOWN_ROLE)
return d && !d->hidden_by_filter;
return d ? diveData(d, index.column(), role) : QVariant();
}
void DiveTripModelList::divesAdded(dive_trip *, bool, const QVector<dive *> &divesIn)
{
QVector<dive *> dives = divesIn;
std::sort(dives.begin(), dives.end(), dive_less_than);
addInBatches(items, dives,
&dive_less_than, // comp
[&](std::vector<dive *> &items, const QVector<dive *> &dives, int idx, int from, int to) { // inserter
beginInsertRows(QModelIndex(), idx, idx + to - from - 1);
items.insert(items.begin() + idx, dives.begin() + from, dives.begin() + to);
endInsertRows();
});
}
void DiveTripModelList::divesDeleted(dive_trip *, bool, const QVector<dive *> &divesIn)
{
QVector<dive *> dives = divesIn;
std::sort(dives.begin(), dives.end(), dive_less_than);
processRangesZip(items, dives,
[](const dive *d1, const dive *d2) { return d1 == d2; }, // Condition (std::equal_to only in C++14)
[&](std::vector<dive *> &items, const QVector<dive *> &, int from, int to, int) -> int { // Action
beginRemoveRows(QModelIndex(), from, to - 1);
items.erase(items.begin() + from, items.begin() + to);
endRemoveRows();
return from - to; // Delta: negate the number of items deleted
});
}
void DiveTripModelList::divesChanged(const QVector<dive *> &divesIn)
{
QVector<dive *> dives = divesIn;
std::sort(dives.begin(), dives.end(), dive_less_than);
// Update filter flags. TODO: The filter should update the flag by itself when
// recieving the signals below.
for (dive *d: dives)
MultiFilterSortModel::instance()->updateDive(d);
// Since we know that the dive list is sorted, we will only ever search for the first element
// in dives as this must be the first that we encounter. Once we find a range, increase the
// index accordingly.
processRangesZip(items, dives,
[](const dive *d1, const dive *d2) { return d1 == d2; }, // Condition (std::equal_to only in C++14)
[&](const std::vector<dive *> &, const QVector<dive *> &, int from, int to, int) -> int { // Action
// TODO: We might be smarter about which columns changed!
dataChanged(createIndex(from, 0, noParent), createIndex(to - 1, COLUMNS - 1, noParent));
return 0; // No items added or deleted
});
}
void DiveTripModelList::divesTimeChanged(timestamp_t delta, const QVector<dive *> &divesIn)
{
QVector<dive *> dives = divesIn;
std::sort(dives.begin(), dives.end(), dive_less_than);
// See comment for DiveTripModelTree::divesTimeChanged above.
QVector<dive *> selectedDives = filterSelectedDives(dives);
divesDeleted(nullptr, false, dives);
divesAdded(nullptr, false, dives);
}
void DiveTripModelList::divesSelected(const QVector<dive *> &dives, dive *current)
{
// We got a number of dives that have been selected. Turn this into QModelIndexes and
// emit a signal, so that views can change the selection.
QVector<QModelIndex> indexes;
indexes.reserve(dives.count());
// Since both lists are sorted, we can do this linearly. Perhaps a binary search
// would be better?
int j = 0; // Index in items array
for (int i = 0; i < dives.size(); ++i) {
while (j < (int)items.size() && items[j] != dives[i])
++j;
if (j >= (int)items.size())
break;
indexes.append(createIndex(j, 0, noParent));
}
emit selectionChanged(indexes);
// Transform the current dive into an index and pass it on to the view.
if (!current) {
emit newCurrentDive(QModelIndex()); // No current dive -> tell view to clear current index with an invalid index
return;
}
auto it = std::find(items.begin(), items.end(), current);
if (it == items.end()) {
// We don't know this dive. Something is wrong. Warn and bail.
qWarning() << "DiveTripModelList::divesSelected(): unknown dive";
emit newCurrentDive(QModelIndex());
return;
}
emit newCurrentDive(createIndex(it - items.begin(), 0));
}
void DiveTripModelList::filterFinished()
{
// In list mode, we don't have to change anything after filter finished.
}
// Simple sorting helper for sorting against a criterium and if
// that is undefined against a different criterium.
// Return true if diff1 < 0, false if diff1 > 0.
// If diff1 == 0 return true if diff2 < 0;
static bool lessThanHelper(int diff1, int diff2)
{
return diff1 < 0 || (diff1 == 0 && diff2 < 0);
}
static int strCmp(const char *s1, const char *s2)
{
if (!s1)
return !s2 ? 0 : -1;
if (!s2)
return 1;
return QString::localeAwareCompare(QString(s1), QString(s2)); // TODO: avoid copy
}
bool DiveTripModelList::lessThan(const QModelIndex &i1, const QModelIndex &i2) const
{
// We assume that i1.column() == i2.column().
int row1 = i1.row();
int row2 = i2.row();
if (row1 < 0 || row1 >= (int)items.size() || row2 < 0 || row2 >= (int)items.size())
return false;
const dive *d1 = items[row1];
const dive *d2 = items[row2];
// This is used as a second sort criterion: For equal values, sorting is chronologically *descending*.
int row_diff = row2 - row1;
switch (i1.column()) {
case NR:
case DATE:
default:
return row1 < row2;
case RATING:
return lessThanHelper(d1->rating - d2->rating, row_diff);
case DEPTH:
return lessThanHelper(d1->maxdepth.mm - d2->maxdepth.mm, row_diff);
case DURATION:
return lessThanHelper(d1->duration.seconds - d2->duration.seconds, row_diff);
case TEMPERATURE:
return lessThanHelper(d1->watertemp.mkelvin - d2->watertemp.mkelvin, row_diff);
case TOTALWEIGHT:
return lessThanHelper(total_weight(d1) - total_weight(d2), row_diff);
case SUIT:
return lessThanHelper(strCmp(d1->suit, d2->suit), row_diff);
case CYLINDER:
return lessThanHelper(strCmp(d1->cylinder[0].type.description, d2->cylinder[0].type.description), row_diff);
case GAS:
return lessThanHelper(nitrox_sort_value(d1) - nitrox_sort_value(d2), row_diff);
case SAC:
return lessThanHelper(d1->sac - d2->sac, row_diff);
case OTU:
return lessThanHelper(d1->otu - d2->otu, row_diff);
case MAXCNS:
return lessThanHelper(d1->maxcns - d2->maxcns, row_diff);
case TAGS: {
char *s1 = taglist_get_tagstring(d1->tag_list);
char *s2 = taglist_get_tagstring(d2->tag_list);
int diff = strCmp(s1, s2);
free(s1);
free(s2);
return lessThanHelper(diff, row_diff);
}
case PHOTOS:
return lessThanHelper(countPhotos(d1) - countPhotos(d2), row_diff);
case COUNTRY:
return lessThanHelper(strCmp(get_dive_country(d1), get_dive_country(d2)), row_diff);
case BUDDIES:
return lessThanHelper(strCmp(d1->buddy, d2->buddy), row_diff);
case LOCATION:
return lessThanHelper(strCmp(get_dive_location(d1), get_dive_location(d2)), row_diff);
}
}
|