| 
									
										
										
										
											2017-04-27 20:25:32 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "qt-models/weightmodel.h"
 | 
					
						
							| 
									
										
										
										
											2018-05-11 08:25:41 -07:00
										 |  |  | #include "core/subsurface-string.h"
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "core/gettextfromc.h"
 | 
					
						
							|  |  |  | #include "core/metrics.h"
 | 
					
						
							| 
									
										
										
										
											2018-06-03 22:15:19 +02:00
										 |  |  | #include "core/qthelper.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-03 19:33:06 +01:00
										 |  |  | #include "core/subsurface-qt/divelistnotifier.h"
 | 
					
						
							| 
									
										
										
										
											2018-05-10 17:35:30 +02:00
										 |  |  | #include "qt-models/weightsysteminfomodel.h"
 | 
					
						
							| 
									
										
										
										
											2019-11-08 22:47:38 +01:00
										 |  |  | #ifndef SUBSURFACE_MOBILE
 | 
					
						
							|  |  |  | #include "commands/command.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent), | 
					
						
							| 
									
										
										
										
											2019-11-04 20:20:32 +01:00
										 |  |  | 	d(nullptr), | 
					
						
							|  |  |  | 	tempRow(-1), | 
					
						
							|  |  |  | 	tempWS(empty_weightsystem) | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	//enum Column {REMOVE, TYPE, WEIGHT};
 | 
					
						
							|  |  |  | 	setHeaderDataStrings(QStringList() << tr("") << tr("Type") << tr("Weight")); | 
					
						
							| 
									
										
										
										
											2019-02-23 22:09:34 +01:00
										 |  |  | 	connect(&diveListNotifier, &DiveListNotifier::weightsystemsReset, this, &WeightModel::weightsystemsReset); | 
					
						
							| 
									
										
										
										
											2019-11-02 21:19:29 +01:00
										 |  |  | 	connect(&diveListNotifier, &DiveListNotifier::weightAdded, this, &WeightModel::weightAdded); | 
					
						
							|  |  |  | 	connect(&diveListNotifier, &DiveListNotifier::weightRemoved, this, &WeightModel::weightRemoved); | 
					
						
							| 
									
										
										
										
											2019-11-08 22:47:38 +01:00
										 |  |  | 	connect(&diveListNotifier, &DiveListNotifier::weightEdited, this, &WeightModel::weightEdited); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-03 15:32:59 +01:00
										 |  |  | weightsystem_t WeightModel::weightSystemAt(const QModelIndex &index) const | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-02 18:47:56 +01:00
										 |  |  | 	int row = index.row(); | 
					
						
							|  |  |  | 	if (row < 0 || row >= d->weightsystems.nr) { | 
					
						
							|  |  |  | 		qWarning("WeightModel: Accessing invalid weightsystem %d (of %d)", row, d->weightsystems.nr); | 
					
						
							| 
									
										
										
										
											2019-11-03 23:08:32 +01:00
										 |  |  | 		return empty_weightsystem; | 
					
						
							| 
									
										
										
										
											2019-11-02 18:47:56 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-03 15:25:23 +01:00
										 |  |  | 	return d->weightsystems.weightsystems[index.row()]; | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void WeightModel::clear() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-02 18:47:56 +01:00
										 |  |  | 	updateDive(nullptr); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QVariant WeightModel::data(const QModelIndex &index, int role) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-02 18:47:56 +01:00
										 |  |  | 	if (!index.isValid() || index.row() >= d->weightsystems.nr) | 
					
						
							| 
									
										
										
										
											2019-06-08 08:49:12 +02:00
										 |  |  | 		return QVariant(); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-04 20:20:32 +01:00
										 |  |  | 	weightsystem_t ws = index.row() == tempRow ? tempWS : weightSystemAt(index); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	switch (role) { | 
					
						
							|  |  |  | 	case Qt::FontRole: | 
					
						
							| 
									
										
										
										
											2019-06-08 08:49:12 +02:00
										 |  |  | 		return defaultModelFont(); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 	case Qt::TextAlignmentRole: | 
					
						
							| 
									
										
										
										
											2019-06-08 08:49:12 +02:00
										 |  |  | 		return Qt::AlignCenter; | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 	case Qt::DisplayRole: | 
					
						
							|  |  |  | 	case Qt::EditRole: | 
					
						
							|  |  |  | 		switch (index.column()) { | 
					
						
							|  |  |  | 		case TYPE: | 
					
						
							| 
									
										
										
										
											2019-11-03 15:32:59 +01:00
										 |  |  | 			return gettextFromC::tr(ws.description); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 		case WEIGHT: | 
					
						
							| 
									
										
										
										
											2019-11-03 15:32:59 +01:00
										 |  |  | 			return get_weight_string(ws.weight, true); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case Qt::DecorationRole: | 
					
						
							|  |  |  | 		if (index.column() == REMOVE) | 
					
						
							| 
									
										
										
										
											2019-06-08 08:49:12 +02:00
										 |  |  | 			return trashIcon(); | 
					
						
							| 
									
										
										
										
											2019-10-27 07:26:50 -04:00
										 |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 	case Qt::SizeHintRole: | 
					
						
							|  |  |  | 		if (index.column() == REMOVE) | 
					
						
							| 
									
										
										
										
											2019-06-08 08:49:12 +02:00
										 |  |  | 			return trashIcon().size(); | 
					
						
							| 
									
										
										
										
											2019-10-27 07:26:50 -04:00
										 |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 	case Qt::ToolTipRole: | 
					
						
							|  |  |  | 		if (index.column() == REMOVE) | 
					
						
							| 
									
										
										
										
											2019-06-08 08:49:12 +02:00
										 |  |  | 			return tr("Clicking here will remove this weight system."); | 
					
						
							| 
									
										
										
										
											2019-10-27 07:26:50 -04:00
										 |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-06-08 08:49:12 +02:00
										 |  |  | 	return QVariant(); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-04 20:20:32 +01:00
										 |  |  | // Ownership of passed in weight system will be taken. Caller must not use it any longer.
 | 
					
						
							|  |  |  | void WeightModel::setTempWS(int row, weightsystem_t ws) | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-04 20:20:32 +01:00
										 |  |  | 	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.
 | 
					
						
							| 
									
										
										
										
											2020-09-12 12:51:34 +02:00
										 |  |  | 	const weightsystem_t &oldWS = d->weightsystems.weightsystems[row]; | 
					
						
							|  |  |  | 	if (same_string(oldWS.description, ws.description)) { | 
					
						
							| 
									
										
										
										
											2019-11-04 20:20:32 +01:00
										 |  |  | 		free_weightsystem(ws); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		tempRow = row; | 
					
						
							|  |  |  | 		tempWS = ws; | 
					
						
							| 
									
										
										
										
											2020-09-12 12:51:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// If the user had already set a weight, don't overwrite that
 | 
					
						
							| 
									
										
										
										
											2020-09-12 18:50:06 +02:00
										 |  |  | 		if (oldWS.weight.grams && !oldWS.auto_filled) | 
					
						
							| 
									
										
										
										
											2020-09-12 12:51:34 +02:00
										 |  |  | 			tempWS.weight = oldWS.weight; | 
					
						
							| 
									
										
										
										
											2020-09-12 18:50:06 +02:00
										 |  |  | 		else | 
					
						
							|  |  |  | 			tempWS.auto_filled = true; | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-04 20:20:32 +01:00
										 |  |  | 	dataChanged(index(row, TYPE), index(row, WEIGHT)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void WeightModel::clearTempWS() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (tempRow < 0) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	int oldRow = tempRow; | 
					
						
							|  |  |  | 	tempRow = -1; | 
					
						
							| 
									
										
										
										
											2019-11-24 09:27:50 +01:00
										 |  |  | 	free_weightsystem(tempWS); | 
					
						
							| 
									
										
										
										
											2019-11-04 20:20:32 +01:00
										 |  |  | 	dataChanged(index(oldRow, TYPE), index(oldRow, WEIGHT)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void WeightModel::commitTempWS() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-08 22:47:38 +01:00
										 |  |  | #ifndef SUBSURFACE_MOBILE
 | 
					
						
							|  |  |  | 	if (tempRow < 0 || !d || tempRow > d->weightsystems.nr) | 
					
						
							| 
									
										
										
										
											2019-11-04 20:20:32 +01:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2019-11-08 22:47:38 +01:00
										 |  |  | 	// Only submit a command if the type changed
 | 
					
						
							|  |  |  | 	weightsystem_t ws = d->weightsystems.weightsystems[tempRow]; | 
					
						
							| 
									
										
										
										
											2020-03-27 21:49:19 +01:00
										 |  |  | 	if (!same_string(ws.description, tempWS.description) || gettextFromC::tr(ws.description) != QString(tempWS.description)) { | 
					
						
							|  |  |  | 		int count = Command::editWeight(tempRow, tempWS, false); | 
					
						
							|  |  |  | 		emit divesEdited(count); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-04 20:20:32 +01:00
										 |  |  | 	tempRow = -1; | 
					
						
							| 
									
										
										
										
											2019-11-08 22:47:38 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int role) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-08 22:47:38 +01:00
										 |  |  | #ifndef SUBSURFACE_MOBILE
 | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 	QString vString = value.toString(); | 
					
						
							| 
									
										
										
										
											2019-11-08 22:47:38 +01:00
										 |  |  | 	weightsystem_t ws = weightSystemAt(index); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 	switch (index.column()) { | 
					
						
							|  |  |  | 	case WEIGHT: | 
					
						
							| 
									
										
										
										
											2019-11-08 22:47:38 +01:00
										 |  |  | 		ws.weight = string_to_weight(qPrintable(vString)); | 
					
						
							| 
									
										
										
										
											2020-09-12 18:50:06 +02:00
										 |  |  | 		ws.auto_filled = false; | 
					
						
							| 
									
										
										
										
											2020-03-27 21:49:19 +01:00
										 |  |  | 		int count = Command::editWeight(index.row(), ws, false); | 
					
						
							|  |  |  | 		emit divesEdited(count); | 
					
						
							| 
									
										
										
										
											2019-11-08 22:47:38 +01:00
										 |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-08 22:47:38 +01:00
										 |  |  | 	return false; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Qt::ItemFlags WeightModel::flags(const QModelIndex &index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (index.column() == REMOVE) | 
					
						
							|  |  |  | 		return Qt::ItemIsEnabled; | 
					
						
							|  |  |  | 	return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-21 17:53:42 +02:00
										 |  |  | int WeightModel::rowCount(const QModelIndex&) const | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-03 15:32:59 +01:00
										 |  |  | 	return d ? d->weightsystems.nr : 0; | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-02 18:47:56 +01:00
										 |  |  | void WeightModel::updateDive(dive *dIn) | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-26 17:21:03 +02:00
										 |  |  | 	beginResetModel(); | 
					
						
							| 
									
										
										
										
											2019-11-02 18:47:56 +01:00
										 |  |  | 	d = dIn; | 
					
						
							| 
									
										
										
										
											2019-06-26 17:21:03 +02:00
										 |  |  | 	endResetModel(); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:17:09 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-02-23 22:09:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 09:22:26 +02:00
										 |  |  | void WeightModel::weightsystemsReset(const QVector<dive *> &dives) | 
					
						
							| 
									
										
										
										
											2019-02-23 22:09:34 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	// This model only concerns the currently displayed dive. If this is not among the
 | 
					
						
							| 
									
										
										
										
											2019-11-02 18:47:56 +01:00
										 |  |  | 	// dives that had their weight reset, exit.
 | 
					
						
							|  |  |  | 	if (!d || std::find(dives.begin(), dives.end(), d) == dives.end()) | 
					
						
							| 
									
										
										
										
											2019-02-23 22:09:34 +01:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// And update the model..
 | 
					
						
							| 
									
										
										
										
											2019-11-02 18:47:56 +01:00
										 |  |  | 	updateDive(d); | 
					
						
							| 
									
										
										
										
											2019-02-23 22:09:34 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-11-02 21:19:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void WeightModel::weightAdded(struct dive *changed, int pos) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (d != changed) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-03 15:32:59 +01:00
										 |  |  | 	// The row was already inserted by the undo command. Just inform the model.
 | 
					
						
							| 
									
										
										
										
											2019-11-02 21:19:29 +01:00
										 |  |  | 	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(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-11-08 22:47:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void WeightModel::weightEdited(struct dive *changed, int pos) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (d != changed) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dataChanged(index(pos, TYPE), index(pos, WEIGHT)); | 
					
						
							|  |  |  | } |