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"
|
2019-02-23 21:09:34 +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),
|
|
|
|
tempRow(-1),
|
|
|
|
tempWS(empty_weightsystem)
|
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();
|
|
|
|
if (row < 0 || row >= d->weightsystems.nr) {
|
|
|
|
qWarning("WeightModel: Accessing invalid weightsystem %d (of %d)", row, d->weightsystems.nr);
|
2019-11-03 22:08:32 +00:00
|
|
|
return empty_weightsystem;
|
2019-11-02 17:47:56 +00:00
|
|
|
}
|
2019-11-03 14:25:23 +00:00
|
|
|
return d->weightsystems.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
|
|
|
|
{
|
2019-11-02 17:47:56 +00:00
|
|
|
if (!index.isValid() || index.row() >= d->weightsystems.nr)
|
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:
|
2019-11-03 14:32:59 +00:00
|
|
|
return gettextFromC::tr(ws.description);
|
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
|
|
|
{
|
2019-11-04 19:20:32 +00: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.
|
|
|
|
if (same_string(d->weightsystems.weightsystems[row].description, ws.description)) {
|
|
|
|
free_weightsystem(ws);
|
|
|
|
} else {
|
|
|
|
tempRow = row;
|
|
|
|
tempWS = ws;
|
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;
|
2019-11-24 08:27:50 +00:00
|
|
|
free_weightsystem(tempWS);
|
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
|
|
|
|
if (tempRow < 0 || !d || tempRow > d->weightsystems.nr)
|
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
|
|
|
|
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);
|
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));
|
|
|
|
Command::editWeight(index.row(), ws, false);
|
|
|
|
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
|
|
|
{
|
2019-11-03 14:32:59 +00:00
|
|
|
return d ? d->weightsystems.nr : 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));
|
|
|
|
}
|