2017-04-27 18:25:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "qt-models/divecomputermodel.h"
|
|
|
|
#include "core/dive.h"
|
|
|
|
#include "core/divelist.h"
|
2015-05-28 20:51:07 +00:00
|
|
|
|
2018-06-16 12:06:35 +00:00
|
|
|
DiveComputerModel::DiveComputerModel(QObject *parent) : CleanerTableModel(parent),
|
2020-10-03 09:18:42 +00:00
|
|
|
dcs(device_table.devices)
|
2015-05-28 20:51:07 +00:00
|
|
|
{
|
|
|
|
setHeaderDataStrings(QStringList() << "" << tr("Model") << tr("Device ID") << tr("Nickname"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DiveComputerModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2020-10-05 08:12:12 +00:00
|
|
|
if (index.row() < 0 || index.row() >= (int)dcs.size())
|
2018-06-16 12:06:35 +00:00
|
|
|
return QVariant();
|
2020-10-03 09:18:42 +00:00
|
|
|
const device &node = dcs[index.row()];
|
2015-05-28 20:51:07 +00:00
|
|
|
|
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
|
|
|
switch (index.column()) {
|
|
|
|
case ID:
|
2018-06-16 12:06:35 +00:00
|
|
|
return QString("0x").append(QString::number(node.deviceId, 16));
|
2015-05-28 20:51:07 +00:00
|
|
|
case MODEL:
|
2020-10-05 07:56:21 +00:00
|
|
|
return QString::fromStdString(node.model);
|
2015-05-28 20:51:07 +00:00
|
|
|
case NICKNAME:
|
2020-10-05 07:56:21 +00:00
|
|
|
return QString::fromStdString(node.nickName);
|
2015-05-28 20:51:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index.column() == REMOVE) {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DecorationRole:
|
2018-06-16 12:06:35 +00:00
|
|
|
return trashIcon();
|
2015-05-28 20:51:07 +00:00
|
|
|
case Qt::SizeHintRole:
|
2018-06-16 12:06:35 +00:00
|
|
|
return trashIcon().size();
|
2015-05-28 20:51:07 +00:00
|
|
|
case Qt::ToolTipRole:
|
2018-06-16 12:06:35 +00:00
|
|
|
return tr("Clicking here will remove this dive computer.");
|
2015-05-28 20:51:07 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-16 12:06:35 +00:00
|
|
|
return QVariant();
|
2015-05-28 20:51:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
int DiveComputerModel::rowCount(const QModelIndex&) const
|
2015-05-28 20:51:07 +00:00
|
|
|
{
|
2018-06-16 12:06:35 +00:00
|
|
|
return dcs.size();
|
2015-05-28 20:51:07 +00: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 15:53:42 +00:00
|
|
|
bool DiveComputerModel::setData(const QModelIndex &index, const QVariant &value, int)
|
2015-05-28 20:51:07 +00:00
|
|
|
{
|
2016-03-08 05:25:40 +00:00
|
|
|
// We should test if the role == Qt::EditRole
|
2020-10-05 08:12:12 +00:00
|
|
|
if (index.row() < 0 || index.row() >= (int)dcs.size())
|
2018-06-16 12:06:35 +00:00
|
|
|
return false;
|
2016-03-08 05:25:40 +00:00
|
|
|
|
2020-10-03 09:18:42 +00:00
|
|
|
device &node = dcs[index.row()];
|
2020-10-05 07:56:21 +00:00
|
|
|
node.nickName = value.toString().toStdString();
|
2015-05-28 20:51:07 +00:00
|
|
|
emit dataChanged(index, index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveComputerModel::remove(const QModelIndex &index)
|
|
|
|
{
|
2020-10-05 08:12:12 +00:00
|
|
|
if (index.row() < 0 || index.row() >= (int)dcs.size())
|
2018-06-16 12:06:35 +00:00
|
|
|
return;
|
|
|
|
beginRemoveRows(QModelIndex(), index.row(), index.row());
|
2020-10-05 08:12:12 +00:00
|
|
|
dcs.erase(dcs.begin() + index.row());
|
2018-06-16 12:06:35 +00:00
|
|
|
endRemoveRows();
|
2015-05-28 20:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiveComputerModel::keepWorkingList()
|
|
|
|
{
|
2020-10-03 09:18:42 +00:00
|
|
|
if (device_table.devices != dcs)
|
2015-05-28 20:51:07 +00:00
|
|
|
mark_divelist_changed(true);
|
2020-10-03 09:18:42 +00:00
|
|
|
device_table.devices = dcs;
|
2015-05-28 20:51:07 +00:00
|
|
|
}
|
2020-10-10 10:40:27 +00: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 21:31:46 +00:00
|
|
|
|
|
|
|
void DiveComputerSortedModel::remove(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
int row = mapToSource(index).row();
|
|
|
|
if (row < 0 || row >= (int)device_table.devices.size())
|
|
|
|
return;
|
|
|
|
device_table.devices.erase(device_table.devices.begin() + row);
|
|
|
|
}
|