subsurface/qt-models/weightmodel.cpp
Berthold Stoeger 029c9ccf02 Desktop: refactor WSInfoDelegate logic
The WSInfoDelegate (weight-system-info delegate) is used to display
a combo box of known weightsystem-types and auto-fills the weight if
the weightsystem-type is changed.

This would overwrite the weight data of the displayed dive when the
user hovers over the different entries. Moreover, it saves the original
weight in case the user cancels the editing action.

This is not viable when implementing undo of weightsystem changes,
because hovering over entries should not produce individual undo
commands. Instead, implement a special "temporary" row in the
weightsystem model. On canceling of the edit actions, simply reload
the weightsystem from the unmodified dive.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-12-05 10:14:25 -08:00

202 lines
5.7 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#include "qt-models/weightmodel.h"
#include "core/subsurface-string.h"
#include "core/gettextfromc.h"
#include "core/metrics.h"
#include "core/qthelper.h"
#include "core/subsurface-qt/DiveListNotifier.h"
#include "qt-models/weightsysteminfomodel.h"
WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent),
changed(false),
d(nullptr),
tempRow(-1),
tempWS(empty_weightsystem)
{
//enum Column {REMOVE, TYPE, WEIGHT};
setHeaderDataStrings(QStringList() << tr("") << tr("Type") << tr("Weight"));
connect(&diveListNotifier, &DiveListNotifier::weightsystemsReset, this, &WeightModel::weightsystemsReset);
connect(&diveListNotifier, &DiveListNotifier::weightAdded, this, &WeightModel::weightAdded);
connect(&diveListNotifier, &DiveListNotifier::weightRemoved, this, &WeightModel::weightRemoved);
}
weightsystem_t WeightModel::weightSystemAt(const QModelIndex &index) const
{
int row = index.row();
if (row < 0 || row >= d->weightsystems.nr) {
qWarning("WeightModel: Accessing invalid weightsystem %d (of %d)", row, d->weightsystems.nr);
return empty_weightsystem;
}
return d->weightsystems.weightsystems[index.row()];
}
void WeightModel::clear()
{
updateDive(nullptr);
}
QVariant WeightModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= d->weightsystems.nr)
return QVariant();
weightsystem_t ws = index.row() == tempRow ? tempWS : weightSystemAt(index);
switch (role) {
case Qt::FontRole:
return defaultModelFont();
case Qt::TextAlignmentRole:
return Qt::AlignCenter;
case Qt::DisplayRole:
case Qt::EditRole:
switch (index.column()) {
case TYPE:
return gettextFromC::tr(ws.description);
case WEIGHT:
return get_weight_string(ws.weight, true);
}
break;
case Qt::DecorationRole:
if (index.column() == REMOVE)
return trashIcon();
break;
case Qt::SizeHintRole:
if (index.column() == REMOVE)
return trashIcon().size();
break;
case Qt::ToolTipRole:
if (index.column() == REMOVE)
return tr("Clicking here will remove this weight system.");
break;
}
return QVariant();
}
// Ownership of passed in weight system will be taken. Caller must not use it any longer.
void WeightModel::setTempWS(int row, weightsystem_t ws)
{
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.
free_weightsystem(tempWS);
// 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);
tempWS.description = nullptr;
} else {
tempRow = row;
tempWS = ws;
}
dataChanged(index(row, TYPE), index(row, WEIGHT));
}
void WeightModel::clearTempWS()
{
if (tempRow < 0)
return;
int oldRow = tempRow;
tempRow = -1;
dataChanged(index(oldRow, TYPE), index(oldRow, WEIGHT));
}
void WeightModel::commitTempWS()
{
if (tempRow < 0)
return;
tempRow = -1;
setData(index(tempRow, TYPE), QVariant::fromValue(QString(tempWS.description)), Qt::EditRole);
setData(index(tempRow, WEIGHT), QVariant::fromValue(get_weight_string(tempWS.weight, true)), Qt::EditRole);
}
bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
QString vString = value.toString();
weightsystem_t *ws = &d->weightsystems.weightsystems[index.row()];
switch (index.column()) {
case TYPE:
if (!value.isNull()) {
//TODO: C-function weight_system_set_description ?
if (!ws->description || gettextFromC::tr(ws->description) != vString) {
// loop over translations to see if one matches
int i = -1;
while (i < MAX_WS_INFO && ws_info[++i].name) {
if (gettextFromC::tr(ws_info[i].name) == vString) {
ws->description = copy_string(ws_info[i].name);
break;
}
}
if (i == MAX_WS_INFO || ws_info[i].name == NULL) // didn't find a match
ws->description = copy_qstring(vString);
changed = true;
}
}
break;
case WEIGHT:
if (CHANGED()) {
ws->weight = string_to_weight(qPrintable(vString));
// now update the ws_info
changed = true;
WSInfoModel *wsim = WSInfoModel::instance();
QModelIndexList matches = wsim->match(wsim->index(0, 0), Qt::DisplayRole, gettextFromC::tr(ws->description));
if (!matches.isEmpty())
wsim->setData(wsim->index(matches.first().row(), WSInfoModel::GR), ws->weight.grams);
}
break;
}
dataChanged(index, index);
return true;
}
Qt::ItemFlags WeightModel::flags(const QModelIndex &index) const
{
if (index.column() == REMOVE)
return Qt::ItemIsEnabled;
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
int WeightModel::rowCount(const QModelIndex&) const
{
return d ? d->weightsystems.nr : 0;
}
void WeightModel::updateDive(dive *dIn)
{
beginResetModel();
d = dIn;
endResetModel();
}
void WeightModel::weightsystemsReset(const QVector<dive *> &dives)
{
// This model only concerns the currently displayed dive. If this is not among the
// dives that had their weight reset, exit.
if (!d || std::find(dives.begin(), dives.end(), d) == dives.end())
return;
// And update the model..
updateDive(d);
}
void WeightModel::weightAdded(struct dive *changed, int pos)
{
if (d != changed)
return;
// The row was already inserted by the undo command. Just inform the model.
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();
}