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"
|
2015-05-28 20:17:09 +00:00
|
|
|
|
|
|
|
WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent),
|
|
|
|
changed(false),
|
|
|
|
rows(0)
|
|
|
|
{
|
|
|
|
//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);
|
2015-05-28 20:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
weightsystem_t *WeightModel::weightSystemAt(const QModelIndex &index)
|
|
|
|
{
|
2019-06-26 15:21:03 +00:00
|
|
|
return &displayed_dive.weightsystems.weightsystems[index.row()];
|
2015-05-28 20:17:09 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 22:41:56 +00:00
|
|
|
void WeightModel::remove(QModelIndex index)
|
2015-05-28 20:17:09 +00:00
|
|
|
{
|
2019-06-08 06:49:12 +00:00
|
|
|
if (index.column() != REMOVE)
|
2015-05-28 20:17:09 +00:00
|
|
|
return;
|
2019-06-26 15:21:03 +00:00
|
|
|
beginRemoveRows(QModelIndex(), index.row(), index.row());
|
2015-05-28 20:17:09 +00:00
|
|
|
rows--;
|
|
|
|
remove_weightsystem(&displayed_dive, index.row());
|
|
|
|
changed = true;
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WeightModel::clear()
|
|
|
|
{
|
2019-06-26 15:21:03 +00:00
|
|
|
beginResetModel();
|
|
|
|
rows = 0;
|
|
|
|
endResetModel();
|
2015-05-28 20:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant WeightModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2019-06-26 15:21:03 +00:00
|
|
|
if (!index.isValid() || index.row() >= displayed_dive.weightsystems.nr)
|
2019-06-08 06:49:12 +00:00
|
|
|
return QVariant();
|
2015-05-28 20:17:09 +00:00
|
|
|
|
2019-06-26 15:21:03 +00:00
|
|
|
const weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
|
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-06-08 06:49:12 +00:00
|
|
|
return gettextFromC::tr(ws->description);
|
2015-05-28 20:17:09 +00:00
|
|
|
case WEIGHT:
|
2019-06-08 06:49:12 +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
|
|
|
}
|
|
|
|
|
|
|
|
// this is our magic 'pass data in' function that allows the delegate to get
|
|
|
|
// the data here without silly unit conversions;
|
|
|
|
// so we only implement the two columns we care about
|
|
|
|
void WeightModel::passInData(const QModelIndex &index, const QVariant &value)
|
|
|
|
{
|
2019-06-26 15:21:03 +00:00
|
|
|
weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
|
2015-05-28 20:17:09 +00:00
|
|
|
if (index.column() == WEIGHT) {
|
|
|
|
if (ws->weight.grams != value.toInt()) {
|
|
|
|
ws->weight.grams = value.toInt();
|
|
|
|
dataChanged(index, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
QString vString = value.toString();
|
2019-06-26 15:21:03 +00:00
|
|
|
weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
|
2015-05-28 20:17:09 +00:00
|
|
|
switch (index.column()) {
|
|
|
|
case TYPE:
|
|
|
|
if (!value.isNull()) {
|
2018-05-10 15:35:30 +00:00
|
|
|
//TODO: C-function weight_system_set_description ?
|
2018-06-17 15:55:47 +00:00
|
|
|
if (!ws->description || gettextFromC::tr(ws->description) != vString) {
|
2015-05-28 20:17:09 +00:00
|
|
|
// loop over translations to see if one matches
|
|
|
|
int i = -1;
|
2019-10-26 21:16:09 +00:00
|
|
|
while (i < MAX_WS_INFO && ws_info[++i].name) {
|
2018-06-17 15:55:47 +00:00
|
|
|
if (gettextFromC::tr(ws_info[i].name) == vString) {
|
2015-05-28 20:17:09 +00:00
|
|
|
ws->description = copy_string(ws_info[i].name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-10-26 21:16:09 +00:00
|
|
|
if (i == MAX_WS_INFO || ws_info[i].name == NULL) // didn't find a match
|
2018-02-28 22:37:09 +00:00
|
|
|
ws->description = copy_qstring(vString);
|
2015-05-28 20:17:09 +00:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WEIGHT:
|
|
|
|
if (CHANGED()) {
|
2018-02-25 12:51:41 +00:00
|
|
|
ws->weight = string_to_weight(qPrintable(vString));
|
2015-05-28 20:17:09 +00:00
|
|
|
// now update the ws_info
|
|
|
|
changed = true;
|
|
|
|
WSInfoModel *wsim = WSInfoModel::instance();
|
2018-06-17 15:55:47 +00:00
|
|
|
QModelIndexList matches = wsim->match(wsim->index(0, 0), Qt::DisplayRole, gettextFromC::tr(ws->description));
|
2015-05-28 20:17:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
int WeightModel::rowCount(const QModelIndex&) const
|
2015-05-28 20:17:09 +00:00
|
|
|
{
|
|
|
|
return rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WeightModel::add()
|
|
|
|
{
|
|
|
|
int row = rows;
|
2019-06-26 15:21:03 +00:00
|
|
|
weightsystem_t ws { {0}, "" };
|
2015-05-28 20:17:09 +00:00
|
|
|
beginInsertRows(QModelIndex(), row, row);
|
2019-06-26 15:21:03 +00:00
|
|
|
add_cloned_weightsystem(&displayed_dive.weightsystems, ws);
|
2015-05-28 20:17:09 +00:00
|
|
|
rows++;
|
|
|
|
changed = true;
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WeightModel::updateDive()
|
|
|
|
{
|
2019-06-26 15:21:03 +00:00
|
|
|
beginResetModel();
|
|
|
|
rows = displayed_dive.weightsystems.nr;
|
|
|
|
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
|
|
|
|
// dives that had their cylinders reset, exit.
|
|
|
|
if (!current_dive || std::find(dives.begin(), dives.end(), current_dive) == dives.end())
|
|
|
|
return;
|
|
|
|
|
2019-06-26 15:21:03 +00:00
|
|
|
// Copy the weights from the current dive to the displayed dive.
|
|
|
|
copy_weights(¤t_dive->weightsystems, &displayed_dive.weightsystems);
|
2019-02-23 21:09:34 +00:00
|
|
|
|
|
|
|
// And update the model..
|
|
|
|
updateDive();
|
|
|
|
}
|