subsurface/qt-models/models.cpp
Berthold Stoeger 5ac64ab2cd cleanup: replace Q_FOREACH and foreach by range base for
Q_FOREACH and foreach are anachronisms.

Range based for may cause a performance regression: it can
lead to a copy of shared containers (one reason why Qt's
COW containers are broken). However, as long as there is no
user noticeable delay, there is no point in analyzing each case.
And also no point in slapping an 'asConst' on every container
that is looped over.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2024-03-24 17:53:19 +01:00

94 lines
2.3 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 <QDir>
#include <QLocale>
Qt::ItemFlags GasSelectionModel::flags(const QModelIndex&) const
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
void GasSelectionModel::repopulate(const dive *d)
{
setStringList(get_dive_gas_list(d));
}
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()
{
QStringList modes;
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"));
for (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();
}