| 
									
										
										
										
											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/divecomputermodel.h"
 | 
					
						
							| 
									
										
										
										
											2020-10-21 23:10:08 +02:00
										 |  |  | #include "commands/command.h"
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "core/dive.h"
 | 
					
						
							|  |  |  | #include "core/divelist.h"
 | 
					
						
							| 
									
										
										
										
											2020-09-12 23:38:45 +02:00
										 |  |  | #include "core/subsurface-qt/divelistnotifier.h"
 | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-21 23:10:08 +02:00
										 |  |  | DiveComputerModel::DiveComputerModel(QObject *parent) : CleanerTableModel(parent) | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-12 23:38:45 +02:00
										 |  |  | 	connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &DiveComputerModel::update); | 
					
						
							| 
									
										
										
										
											2020-10-21 23:10:08 +02:00
										 |  |  | 	connect(&diveListNotifier, &DiveListNotifier::deviceAdded, this, &DiveComputerModel::deviceAdded); | 
					
						
							|  |  |  | 	connect(&diveListNotifier, &DiveListNotifier::deviceDeleted, this, &DiveComputerModel::deviceDeleted); | 
					
						
							|  |  |  | 	connect(&diveListNotifier, &DiveListNotifier::deviceEdited, this, &DiveComputerModel::deviceEdited); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | 	setHeaderDataStrings(QStringList() << "" << tr("Model") << tr("Device ID") << tr("Nickname")); | 
					
						
							| 
									
										
										
										
											2020-09-12 23:38:45 +02:00
										 |  |  | 	update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DiveComputerModel::update() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	beginResetModel(); | 
					
						
							|  |  |  | 	endResetModel(); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QVariant DiveComputerModel::data(const QModelIndex &index, int role) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-21 23:10:08 +02:00
										 |  |  | 	const device *dev = get_device(&device_table, index.row()); | 
					
						
							|  |  |  | 	if (dev == nullptr) | 
					
						
							| 
									
										
										
										
											2018-06-16 14:06:35 +02:00
										 |  |  | 		return QVariant(); | 
					
						
							| 
									
										
										
										
											2020-10-21 23:10:08 +02:00
										 |  |  | 	const device &node = *dev; | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (role == Qt::DisplayRole || role == Qt::EditRole) { | 
					
						
							|  |  |  | 		switch (index.column()) { | 
					
						
							|  |  |  | 		case ID: | 
					
						
							| 
									
										
										
										
											2018-06-16 14:06:35 +02:00
										 |  |  | 			return QString("0x").append(QString::number(node.deviceId, 16)); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | 		case MODEL: | 
					
						
							| 
									
										
										
										
											2020-10-05 09:56:21 +02:00
										 |  |  | 			return QString::fromStdString(node.model); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | 		case NICKNAME: | 
					
						
							| 
									
										
										
										
											2020-10-05 09:56:21 +02:00
										 |  |  | 			return QString::fromStdString(node.nickName); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (index.column() == REMOVE) { | 
					
						
							|  |  |  | 		switch (role) { | 
					
						
							|  |  |  | 		case Qt::DecorationRole: | 
					
						
							| 
									
										
										
										
											2018-06-16 14:06:35 +02:00
										 |  |  | 			return trashIcon(); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | 		case Qt::SizeHintRole: | 
					
						
							| 
									
										
										
										
											2018-06-16 14:06:35 +02:00
										 |  |  | 			return trashIcon().size(); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | 		case Qt::ToolTipRole: | 
					
						
							| 
									
										
										
										
											2018-06-16 14:06:35 +02:00
										 |  |  | 			return tr("Clicking here will remove this dive computer."); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-16 14:06:35 +02:00
										 |  |  | 	return QVariant(); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-21 17:53:42 +02:00
										 |  |  | int DiveComputerModel::rowCount(const QModelIndex&) const | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-21 23:10:08 +02:00
										 |  |  | 	return (int)device_table.devices.size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DiveComputerModel::deviceAdded(int idx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	beginInsertRows(QModelIndex(), idx, idx); | 
					
						
							|  |  |  | 	endInsertRows(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DiveComputerModel::deviceDeleted(int idx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	beginRemoveRows(QModelIndex(), idx, idx); | 
					
						
							|  |  |  | 	endRemoveRows(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DiveComputerModel::deviceEdited(int idx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	dataChanged(index(idx, REMOVE), index(idx, NICKNAME)); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Qt::ItemFlags DiveComputerModel::flags(const QModelIndex &index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Qt::ItemFlags flags = QAbstractItemModel::flags(index); | 
					
						
							|  |  |  | 	if (index.column() == NICKNAME) | 
					
						
							|  |  |  | 		flags |= Qt::ItemIsEditable; | 
					
						
							|  |  |  | 	return flags; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-21 17:53:42 +02:00
										 |  |  | bool DiveComputerModel::setData(const QModelIndex &index, const QVariant &value, int) | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-08 02:25:40 -03:00
										 |  |  | 	// We should test if the role == Qt::EditRole
 | 
					
						
							| 
									
										
										
										
											2020-10-21 23:10:08 +02:00
										 |  |  | 	Command::editDeviceNickname(index.row(), value.toString()); | 
					
						
							| 
									
										
										
										
											2015-05-28 17:51:07 -03:00
										 |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 12:40:27 +02:00
										 |  |  | // Convenience function to access alternative columns
 | 
					
						
							|  |  |  | static QString getData(const QModelIndex &idx, int col) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const QAbstractItemModel *model = idx.model(); | 
					
						
							|  |  |  | 	QModelIndex idx2 = model->index(idx.row(), col, idx.parent()); | 
					
						
							|  |  |  | 	return model->data(idx2).toString(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Helper function: sort data pointed to by the given indexes.
 | 
					
						
							|  |  |  | // For equal data, sort by two alternative rows.
 | 
					
						
							|  |  |  | // All sorting is by case-insensitive string comparison.
 | 
					
						
							|  |  |  | static bool sortHelper(const QModelIndex &i1, const QModelIndex &i2, int altRow1, int altRow2) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(int cmp = i1.data().toString().compare(i2.data().toString())) | 
					
						
							|  |  |  | 		return cmp < 0; | 
					
						
							|  |  |  | 	if(int cmp = getData(i1, altRow1).compare(getData(i2, altRow1))) | 
					
						
							|  |  |  | 		return cmp < 0; | 
					
						
							|  |  |  | 	return getData(i1, altRow2).compare(getData(i2, altRow2)) < 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool DiveComputerSortedModel::lessThan(const QModelIndex &i1, const QModelIndex &i2) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// We assume that i1.column() == i2.column()
 | 
					
						
							|  |  |  | 	switch (i1.column()) { | 
					
						
							|  |  |  | 		case DiveComputerModel::ID: | 
					
						
							|  |  |  | 			return sortHelper(i1, i2, DiveComputerModel::MODEL, DiveComputerModel::NICKNAME); | 
					
						
							|  |  |  | 		case DiveComputerModel::MODEL: | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			return sortHelper(i1, i2, DiveComputerModel::ID, DiveComputerModel::NICKNAME); | 
					
						
							|  |  |  | 		case DiveComputerModel::NICKNAME: | 
					
						
							|  |  |  | 			return sortHelper(i1, i2, DiveComputerModel::MODEL, DiveComputerModel::ID); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-09-12 23:31:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void DiveComputerSortedModel::remove(const QModelIndex &index) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int row = mapToSource(index).row(); | 
					
						
							|  |  |  | 	if (row < 0 || row >= (int)device_table.devices.size()) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-10-21 23:10:08 +02:00
										 |  |  | 	Command::removeDevice(row); | 
					
						
							| 
									
										
										
										
											2020-09-12 23:31:46 +02:00
										 |  |  | } |