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
|
/*
* divetripmodel.cpp
*
* classes for the dive trip list in Subsurface
*/
#include "divetripmodel.h"
DiveItem::DiveItem(int num, QString dt, float dur, float dep, QString loc, DiveItem *p):
number(num), dateTime(dt), duration(dur), depth(dep), location(loc), parentItem(p)
{
if (parentItem)
parentItem->addChild(this);
}
DiveTripModel::DiveTripModel(const QString &filename, QObject *parent) : QAbstractItemModel(parent), filename(filename)
{
rootItem = new DiveItem;
}
Qt::ItemFlags DiveTripModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags diveFlags = QAbstractItemModel::flags(index);
if (index.isValid()) {
diveFlags |= Qt::ItemIsSelectable|Qt::ItemIsEnabled;
}
return diveFlags;
}
QVariant DiveTripModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
DiveItem *item = static_cast<DiveItem*>(index.internalPointer());
QVariant retVal;
switch (index.column()) {
case DIVE_NUMBER:
retVal = QVariant(item->diveNumber());
break;
case DIVE_DATE_TIME:
retVal = QVariant(item->diveDateTime());
break;
case DIVE_DURATION:
retVal = QVariant(item->diveDuration());
break;
case DIVE_DEPTH:
retVal = QVariant(item->diveDepth());
break;
case DIVE_LOCATION:
retVal = QVariant(item->diveLocation());
break;
default:
return QVariant();
};
return retVal;
}
QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == DIVE_NUMBER) {
return tr("Dive number");
} else if (section == DIVE_DATE_TIME) {
return tr("Date");
} else if (section == DIVE_DURATION) {
return tr("Duration");
} else if (section == DIVE_DEPTH) {
return tr("Depth");
} else if (section == DIVE_LOCATION) {
return tr("Location");
}
}
return QVariant();
}
int DiveTripModel::rowCount(const QModelIndex &parent) const
{
/* only allow kids in column 0 */
if (parent.isValid() && parent.column() > 0){
return 0;
}
DiveItem *item = itemForIndex(parent);
return item ? item->childCount() : 0;
}
int DiveTripModel::columnCount(const QModelIndex &parent) const
{
return parent.isValid() && parent.column() != 0 ? 0 : COLUMNS;
}
QModelIndex DiveTripModel::index(int row, int column, const QModelIndex &parent) const
{
if (!rootItem || row < 0 || column < 0 || column >= COLUMNS ||
(parent.isValid() && parent.column() != 0))
return QModelIndex();
DiveItem *parentItem = itemForIndex(parent);
Q_ASSERT(parentItem);
if (DiveItem *item = parentItem->childAt(row)){
return createIndex(row, column, item);
}
return QModelIndex();
}
QModelIndex DiveTripModel::parent(const QModelIndex &childIndex) const
{
if (!childIndex.isValid())
return QModelIndex();
DiveItem *child = static_cast<DiveItem*>(childIndex.internalPointer());
DiveItem *parent = child->parent();
if (parent == rootItem)
return QModelIndex();
return createIndex(parent->rowOfChild(child), 0, parent);
}
DiveItem* DiveTripModel::itemForIndex(const QModelIndex &index) const
{
if (index.isValid()) {
DiveItem *item = static_cast<DiveItem*>(index.internalPointer());
return item;
}
return rootItem;
}
|