desktop: unglobalize ComboBox-models

The combo-boxes (cylinder type, weightsystem, etc.) were controlled
by global models. Keeping these models up-to-date was very combersome
and buggy.

Create a new model everytime a combobox is opened. Ultimately it
might even be better to create a copy of the strings and switch
to simple QStringListModel. Set data in the core directly and
don't do this via the models.

The result is much simpler and easier to handle.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-03-30 13:40:00 +01:00 committed by Michael Keller
parent de313bd01a
commit 0d011231e6
16 changed files with 130 additions and 215 deletions

View file

@ -1,6 +1,5 @@
// SPDX-License-Identifier: GPL-2.0
#include "cylindermodel.h"
#include "tankinfomodel.h"
#include "models.h"
#include "commands/command.h"
#include "core/qthelper.h"
@ -381,23 +380,12 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
cyl.type.description = newType.c_str();
type = Command::EditCylinderType::TYPE;
break;
case SIZE: {
TankInfoModel *tanks = TankInfoModel::instance();
QModelIndexList matches = tanks->match(tanks->index(0, 0), Qt::DisplayRole, cyl.type.description);
cyl.type.size = string_to_volume(qPrintable(vString), cyl.type.workingpressure);
if (!matches.isEmpty())
tanks->setData(tanks->index(matches.first().row(), TankInfoModel::ML), cyl.type.size.mliter);
}
case SIZE:
cyl.type.size = string_to_volume(qPrintable(vString), cyl.type.workingpressure);
type = Command::EditCylinderType::TYPE;
break;
case WORKINGPRESS: {
TankInfoModel *tanks = TankInfoModel::instance();
QModelIndexList matches = tanks->match(tanks->index(0, 0), Qt::DisplayRole, cyl.type.description);
cyl.type.workingpressure = string_to_pressure(qPrintable(vString));
if (!matches.isEmpty())
tanks->setData(tanks->index(matches.first().row(), TankInfoModel::BAR), cyl.type.workingpressure.mbar / 1000.0);
}
case WORKINGPRESS:
cyl.type.workingpressure = string_to_pressure(qPrintable(vString));
type = Command::EditCylinderType::TYPE;
break;
case START:

View file

@ -18,9 +18,10 @@ Qt::ItemFlags GasSelectionModel::flags(const QModelIndex&) const
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
void GasSelectionModel::repopulate(const dive *d)
GasSelectionModel::GasSelectionModel(const dive &d, QObject *parent)
: QStringListModel(parent)
{
setStringList(get_dive_gas_list(d));
setStringList(get_dive_gas_list(&d));
}
QVariant GasSelectionModel::data(const QModelIndex &index, int role) const
@ -37,7 +38,7 @@ Qt::ItemFlags DiveTypeSelectionModel::flags(const QModelIndex&) const
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
DiveTypeSelectionModel::DiveTypeSelectionModel()
DiveTypeSelectionModel::DiveTypeSelectionModel(QObject *parent) : QStringListModel(parent)
{
QStringList modes;
for (int i = 0; i < FREEDIVE; i++)

View file

@ -24,15 +24,15 @@ struct dive;
class GasSelectionModel : public QStringListModel {
Q_OBJECT
public:
GasSelectionModel(const dive &d, QObject *parent);
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant data(const QModelIndex &index, int role) const override;
void repopulate(const dive *d);
};
class DiveTypeSelectionModel : public QStringListModel {
Q_OBJECT
public:
DiveTypeSelectionModel();
DiveTypeSelectionModel(QObject *parent);
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant data(const QModelIndex &index, int role) const override;
};

View file

@ -5,48 +5,9 @@
#include "core/gettextfromc.h"
#include "core/metrics.h"
TankInfoModel *TankInfoModel::instance()
{
static TankInfoModel self;
return &self;
}
bool TankInfoModel::insertRows(int, int count, const QModelIndex &parent)
{
beginInsertRows(parent, rowCount(), rowCount() + count - 1);
for (int i = 0; i < count; ++i)
add_tank_info_metric(&tank_info_table, "", 0, 0);
endInsertRows();
return true;
}
bool TankInfoModel::setData(const QModelIndex &index, const QVariant &value, int)
{
//WARN Seems wrong, we need to check for role == Qt::EditRole
if (index.row() < 0 || index.row() >= tank_info_table.nr )
return false;
struct tank_info &info = tank_info_table.infos[index.row()];
switch (index.column()) {
case DESCRIPTION:
free((void *)info.name);
info.name = strdup(value.toByteArray().data());
break;
case ML:
info.ml = value.toInt();
break;
case BAR:
info.bar = value.toInt();
break;
}
emit dataChanged(index, index);
return true;
}
QVariant TankInfoModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= tank_info_table.nr)
if (index.row() < 0 || index.row() >= tank_info_table.nr)
return QVariant();
if (role == Qt::FontRole)
return defaultModelFont();
@ -75,16 +36,7 @@ int TankInfoModel::rowCount(const QModelIndex&) const
return tank_info_table.nr;
}
TankInfoModel::TankInfoModel()
TankInfoModel::TankInfoModel(QObject *parent) : CleanerTableModel(parent)
{
setHeaderDataStrings(QStringList() << tr("Description") << tr("ml") << tr("bar"));
connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &TankInfoModel::update);
connect(&diveListNotifier, &DiveListNotifier::settingsChanged, this, &TankInfoModel::update);
update();
}
void TankInfoModel::update()
{
beginResetModel();
endResetModel();
}

View file

@ -9,22 +9,15 @@
class TankInfoModel : public CleanerTableModel {
Q_OBJECT
public:
static TankInfoModel *instance();
enum Column {
DESCRIPTION,
ML,
BAR
};
TankInfoModel();
TankInfoModel(QObject *parent);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
public
slots:
void update();
};
#endif

View file

@ -78,8 +78,10 @@ QVariant WeightModel::data(const QModelIndex &index, int role) const
// 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
if (!d || row < 0 || row >= d->weightsystems.nr) { // Sanity check: row must exist
free_weightsystem(ws);
return;
}
clearTempWS(); // Shouldn't be necessary, just in case: Reset old temporary row.

View file

@ -5,66 +5,27 @@
#include "core/metrics.h"
#include "core/gettextfromc.h"
WSInfoModel *WSInfoModel::instance()
{
static WSInfoModel self;
return &self;
}
bool WSInfoModel::insertRows(int, int count, const QModelIndex &parent)
{
beginInsertRows(parent, rowCount(), rowCount());
rows += count;
endInsertRows();
return true;
}
bool WSInfoModel::setData(const QModelIndex &index, const QVariant &value, int)
{
//WARN: check for Qt::EditRole
struct ws_info_t *info = &ws_info[index.row()];
switch (index.column()) {
case DESCRIPTION:
info->name = strdup(value.toByteArray().data());
break;
case GR:
info->grams = value.toInt();
break;
}
emit dataChanged(index, index);
return true;
}
void WSInfoModel::clear()
{
}
QVariant WSInfoModel::data(const QModelIndex &index, int role) const
{
QVariant ret;
if (!index.isValid()) {
return ret;
}
if (!index.isValid() || index.row() >= rows)
return QVariant();
struct ws_info_t *info = &ws_info[index.row()];
int gr = info->grams;
switch (role) {
case Qt::FontRole:
ret = defaultModelFont();
break;
return defaultModelFont();
case Qt::DisplayRole:
case Qt::EditRole:
switch (index.column()) {
case GR:
ret = gr;
break;
return gr;
case DESCRIPTION:
ret = gettextFromC::tr(info->name);
break;
return gettextFromC::tr(info->name);
}
break;
}
return ret;
return QVariant();
}
int WSInfoModel::rowCount(const QModelIndex&) const
@ -72,18 +33,10 @@ int WSInfoModel::rowCount(const QModelIndex&) const
return rows;
}
WSInfoModel::WSInfoModel()
WSInfoModel::WSInfoModel(QObject *parent) : CleanerTableModel(parent)
{
setHeaderDataStrings(QStringList() << tr("Description") << tr("kg"));
connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &WSInfoModel::update);
update();
}
void WSInfoModel::update()
{
beginResetModel();
rows = 0;
for (struct ws_info_t *info = ws_info; info->name && info < ws_info + MAX_WS_INFO; info++, rows++)
;
endResetModel();
}

View file

@ -8,24 +8,17 @@
class WSInfoModel : public CleanerTableModel {
Q_OBJECT
public:
static WSInfoModel *instance();
enum Column {
DESCRIPTION,
GR
};
WSInfoModel();
WSInfoModel(QObject *parent);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
void clear();
void update();
private:
int rows;
QString biggerEntry;
};
#endif