desktop: refine auto-fill of weights

In a previous commit, auto-filling of weight based on type was
changed to be only performed if the user hadn't already set a
weight, by testing for weight=0.

However, when the user edited the type and tabbed back and forth,
that counted as an edit and therefore the weight would not
change anymore.

To refine this, introduce an "auto_filled" flag to the weightsystem,
which is set if the weight is automatically filled and cleared if
the weight is edited. Update the weight if it was zero *or* auto-filled.

The flag is not saved to disk, but that should be acceptable. If the
user saves and reloads, we can assume that they meant the weight
to be set to the default value.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-09-12 18:50:06 +02:00 committed by Dirk Hohndel
parent 72312bec2f
commit 6ea0cc62b8
3 changed files with 7 additions and 3 deletions

View file

@ -127,7 +127,7 @@ void add_weightsystem_description(const weightsystem_t *weightsystem)
weightsystem_t clone_weightsystem(weightsystem_t ws)
{
weightsystem_t res = { ws.weight, copy_string(ws.description) };
weightsystem_t res = { ws.weight, copy_string(ws.description), ws.auto_filled };
return res;
}

View file

@ -50,9 +50,10 @@ typedef struct
{
weight_t weight;
const char *description; /* "integrated", "belt", "ankle" */
bool auto_filled; /* weight was automatically derived from the type */
} weightsystem_t;
static const weightsystem_t empty_weightsystem = { { 0 }, 0 };
static const weightsystem_t empty_weightsystem = { { 0 }, 0, false };
/* Table of weightsystems. Attention: this stores weightsystems,
* *not* pointers * to weightsystems. This has two crucial

View file

@ -94,8 +94,10 @@ void WeightModel::setTempWS(int row, weightsystem_t ws)
tempWS = ws;
// If the user had already set a weight, don't overwrite that
if (oldWS.weight.grams)
if (oldWS.weight.grams && !oldWS.auto_filled)
tempWS.weight = oldWS.weight;
else
tempWS.auto_filled = true;
}
dataChanged(index(row, TYPE), index(row, WEIGHT));
}
@ -133,6 +135,7 @@ bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int r
switch (index.column()) {
case WEIGHT:
ws.weight = string_to_weight(qPrintable(vString));
ws.auto_filled = false;
int count = Command::editWeight(index.row(), ws, false);
emit divesEdited(count);
return true;