summaryrefslogtreecommitdiffstats
path: root/qt-models/weightmodel.cpp
blob: 21e363592f8b46aca0aaa62f84421f3b86f63982 (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
// SPDX-License-Identifier: GPL-2.0
#include "qt-models/weightmodel.h"
#include "core/subsurface-string.h"
#include "core/gettextfromc.h"
#include "core/metrics.h"
#include "core/qthelper.h"
#include "core/subsurface-qt/DiveListNotifier.h"
#include "qt-models/weightsysteminfomodel.h"
#ifndef SUBSURFACE_MOBILE
#include "commands/command.h"
#endif

WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent),
	d(nullptr),
	tempRow(-1),
	tempWS(empty_weightsystem)
{
	//enum Column {REMOVE, TYPE, WEIGHT};
	setHeaderDataStrings(QStringList() << tr("") << tr("Type") << tr("Weight"));
	connect(&diveListNotifier, &DiveListNotifier::weightsystemsReset, this, &WeightModel::weightsystemsReset);
	connect(&diveListNotifier, &DiveListNotifier::weightAdded, this, &WeightModel::weightAdded);
	connect(&diveListNotifier, &DiveListNotifier::weightRemoved, this, &WeightModel::weightRemoved);
	connect(&diveListNotifier, &DiveListNotifier::weightEdited, this, &WeightModel::weightEdited);
}

weightsystem_t WeightModel::weightSystemAt(const QModelIndex &index) const
{
	int row = index.row();
	if (row < 0 || row >= d->weightsystems.nr) {
		qWarning("WeightModel: Accessing invalid weightsystem %d (of %d)", row, d->weightsystems.nr);
		return empty_weightsystem;
	}
	return d->weightsystems.weightsystems[index.row()];
}

void WeightModel::clear()
{
	updateDive(nullptr);
}

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

	weightsystem_t ws = index.row() == tempRow ? tempWS : weightSystemAt(index);

	switch (role) {
	case Qt::FontRole:
		return defaultModelFont();
	case Qt::TextAlignmentRole:
		return Qt::AlignCenter;
	case Qt::DisplayRole:
	case Qt::EditRole:
		switch (index.column()) {
		case TYPE:
			return gettextFromC::tr(ws.description);
		case WEIGHT:
			return get_weight_string(ws.weight, true);
		}
		break;
	case Qt::DecorationRole:
		if (index.column() == REMOVE)
			return trashIcon();
		break;
	case Qt::SizeHintRole:
		if (index.column() == REMOVE)
			return trashIcon().size();
		break;
	case Qt::ToolTipRole:
		if (index.column() == REMOVE)
			return tr("Clicking here will remove this weight system.");
		break;
	}
	return QVariant();
}

// Ownership of passed in weight system will be taken. Caller must not use it any longer.
void WeightModel::setTempWS(int row, weightsystem_t ws)
{
	if (!d || row < 0 || row >= d->weightsystems.nr) // Sanity check: row must exist
		return;

	clearTempWS(); // Shouldn't be necessary, just in case: Reset old temporary row.

	// It is really hard to get the editor-close-hints and setModelData calls under
	// control. Therefore, if the row is set to the already existing entry, don't
	// enter temporary mode.
	if (same_string(d->weightsystems.weightsystems[row].description, ws.description)) {
		free_weightsystem(ws);
	} else {
		tempRow = row;
		tempWS = ws;
	}
	dataChanged(index(row, TYPE), index(row, WEIGHT));
}

void WeightModel::clearTempWS()
{
	if (tempRow < 0)
		return;
	int oldRow = tempRow;
	tempRow = -1;
	free_weightsystem(tempWS);
	dataChanged(index(oldRow, TYPE), index(oldRow, WEIGHT));
}

void WeightModel::commitTempWS()
{
#ifndef SUBSURFACE_MOBILE
	if (tempRow < 0 || !d || tempRow > d->weightsystems.nr)
		return;
	// Only submit a command if the type changed
	weightsystem_t ws = d->weightsystems.weightsystems[tempRow];
	if (!same_string(ws.description, tempWS.description) || gettextFromC::tr(ws.description) != QString(tempWS.description))
		Command::editWeight(tempRow, tempWS, false);
	tempRow = -1;
#endif
}

bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
#ifndef SUBSURFACE_MOBILE
	QString vString = value.toString();
	weightsystem_t ws = weightSystemAt(index);
	switch (index.column()) {
	case WEIGHT:
		ws.weight = string_to_weight(qPrintable(vString));
		Command::editWeight(index.row(), ws, false);
		return true;
	}
	return false;
#endif
}

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

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

void WeightModel::updateDive(dive *dIn)
{
	beginResetModel();
	d = dIn;
	endResetModel();
}

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

	// And update the model..
	updateDive(d);
}

void WeightModel::weightAdded(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 WeightModel::weightRemoved(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 WeightModel::weightEdited(struct dive *changed, int pos)
{
	if (d != changed)
		return;

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