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
|
#include "divecomputermodel.h"
#include "dive.h"
#include "divelist.h"
DiveComputerModel::DiveComputerModel(QMultiMap<QString, DiveComputerNode> &dcMap, QObject *parent) : CleanerTableModel()
{
setHeaderDataStrings(QStringList() << "" << tr("Model") << tr("Device ID") << tr("Nickname"));
dcWorkingMap = dcMap;
numRows = 0;
}
QVariant DiveComputerModel::data(const QModelIndex &index, int role) const
{
QList<DiveComputerNode> values = dcWorkingMap.values();
DiveComputerNode node = values.at(index.row());
QVariant ret;
if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch (index.column()) {
case ID:
ret = QString("0x").append(QString::number(node.deviceId, 16));
break;
case MODEL:
ret = node.model;
break;
case NICKNAME:
ret = node.nickName;
break;
}
}
if (index.column() == REMOVE) {
switch (role) {
case Qt::DecorationRole:
ret = trashIcon();
break;
case Qt::SizeHintRole:
ret = trashIcon().size();
break;
case Qt::ToolTipRole:
ret = tr("Clicking here will remove this dive computer.");
break;
}
}
return ret;
}
int DiveComputerModel::rowCount(const QModelIndex &parent) const
{
return numRows;
}
void DiveComputerModel::update()
{
QList<DiveComputerNode> values = dcWorkingMap.values();
int count = values.count();
if (numRows) {
beginRemoveRows(QModelIndex(), 0, numRows - 1);
numRows = 0;
endRemoveRows();
}
if (count) {
beginInsertRows(QModelIndex(), 0, count - 1);
numRows = count;
endInsertRows();
}
}
Qt::ItemFlags DiveComputerModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
if (index.column() == NICKNAME)
flags |= Qt::ItemIsEditable;
return flags;
}
bool DiveComputerModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
QList<DiveComputerNode> values = dcWorkingMap.values();
DiveComputerNode node = values.at(index.row());
dcWorkingMap.remove(node.model, node);
node.nickName = value.toString();
dcWorkingMap.insert(node.model, node);
emit dataChanged(index, index);
return true;
}
void DiveComputerModel::remove(const QModelIndex &index)
{
QList<DiveComputerNode> values = dcWorkingMap.values();
DiveComputerNode node = values.at(index.row());
dcWorkingMap.remove(node.model, node);
update();
}
void DiveComputerModel::dropWorkingList()
{
// how do I prevent the memory leak ?
}
void DiveComputerModel::keepWorkingList()
{
if (dcList.dcMap != dcWorkingMap)
mark_divelist_changed(true);
dcList.dcMap = dcWorkingMap;
}
|