subsurface/qt-models/models.cpp
Berthold Stoeger 7c9f46acd2 Core: remove MAX_CYLINDERS restriction
Instead of using fixed size arrays, use a new cylinder_table structure.
The code copies the weightsystem code, but is significantly more complex
because cylinders are such an integral part of the core.

Two functions to access the cylinders were added:
get_cylinder() and get_or_create_cylinder()
The former does a simple array access and supposes that the cylinder
exists. The latter is used by the parser(s) and if a cylinder with
the given id does not exist, cylinders up to that id are generated.

One point will make C programmers cringe: the cylinder structure is
passed by value. This is due to the way the table-macros work. A
refactoring of the table macros is planned. It has to be noted that
the size of a cylinder_t is 64 bytes, i.e. 8 long words on a 64-bit
architecture, so passing on the stack is probably not even significantly
slower than passing as reference.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-11-09 19:19:04 +01:00

124 lines
3.1 KiB
C++

// SPDX-License-Identifier: GPL-2.0
/*
* models.cpp
*
* classes for the equipment models of Subsurface
*
*/
#include "qt-models/models.h"
#include "core/qthelper.h"
#include "core/dive.h"
#include "core/gettextfromc.h"
#include <QLocale>
Qt::ItemFlags GasSelectionModel::flags(const QModelIndex&) const
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
GasSelectionModel *GasSelectionModel::instance()
{
static GasSelectionModel self;
return &self;
}
static QStringList getGasList()
{
QStringList list;
for (int i = 0; i < displayed_dive.cylinders.nr; i++) {
const cylinder_t *cyl = &displayed_dive.cylinders.cylinders[i];
/* Check if we have the same gasmix two or more times
* If yes return more verbose string */
int same_gas = same_gasmix_cylinder(cyl, i, &displayed_dive, true);
if (same_gas == -1)
list.push_back(get_gas_string(cyl->gasmix));
else
list.push_back(get_gas_string(cyl->gasmix) + QString(" (%1 %2 ").arg(GasSelectionModel::tr("cyl.")).arg(i + 1) +
cyl->type.description + ")");
}
return list;
}
void GasSelectionModel::repopulate()
{
setStringList(getGasList());
}
QVariant GasSelectionModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::FontRole) {
return defaultModelFont();
}
return QStringListModel::data(index, role);
}
// Dive Type Model for the divetype combo box
Qt::ItemFlags DiveTypeSelectionModel::flags(const QModelIndex&) const
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
DiveTypeSelectionModel *DiveTypeSelectionModel::instance()
{
static DiveTypeSelectionModel self;
return &self;
}
void DiveTypeSelectionModel::repopulate()
{
QStringList modes = QStringList();
for (int i = 0; i < FREEDIVE; i++)
modes.append(gettextFromC::tr(divemode_text_ui[i]));
setStringList(modes);
}
QVariant DiveTypeSelectionModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::FontRole) {
return defaultModelFont();
}
return QStringListModel::data(index, role);
}
// Language Model, The Model to populate the list of possible Languages.
LanguageModel *LanguageModel::instance()
{
static LanguageModel *self = new LanguageModel();
QLocale l;
return self;
}
LanguageModel::LanguageModel(QObject *parent) : QAbstractListModel(parent)
{
QDir d(getSubsurfaceDataPath("translations"));
Q_FOREACH (const QString &s, d.entryList()) {
if (s.startsWith("subsurface_") && s.endsWith(".qm")) {
languages.push_back((s == "subsurface_source.qm") ? "English" : s);
}
}
}
QVariant LanguageModel::data(const QModelIndex &index, int role) const
{
QLocale loc;
QString currentString = languages.at(index.row());
if (!index.isValid())
return QVariant();
switch (role) {
case Qt::DisplayRole: {
QLocale l(currentString.remove("subsurface_").remove(".qm"));
return currentString == "English" ? currentString : QString("%1 (%2)").arg(l.languageToString(l.language())).arg(l.countryToString(l.country()));
}
case Qt::UserRole:
return currentString == "English" ? "en_US" : currentString.remove("subsurface_").remove(".qm");
}
return QVariant();
}
int LanguageModel::rowCount(const QModelIndex&) const
{
return languages.count();
}