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