2017-04-27 20:25:32 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-04 22:02:03 -07:00
|
|
|
#include "qt-models/completionmodels.h"
|
|
|
|
#include "core/dive.h"
|
2024-06-07 10:25:09 +02:00
|
|
|
#include "core/divelist.h"
|
|
|
|
#include "core/divelog.h"
|
2019-05-30 18:29:36 +02:00
|
|
|
#include "core/tag.h"
|
2015-09-02 21:22:29 -03:00
|
|
|
#include <QSet>
|
2013-08-13 10:30:22 -03:00
|
|
|
|
2020-10-26 07:27:47 -07:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
|
|
|
#define SKIP_EMPTY Qt::SkipEmptyParts
|
|
|
|
#else
|
|
|
|
#define SKIP_EMPTY QString::SkipEmptyParts
|
|
|
|
#endif
|
|
|
|
|
2020-11-14 17:42:59 +01:00
|
|
|
CompletionModelBase::CompletionModelBase()
|
|
|
|
{
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &CompletionModelBase::updateModel);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesImported, this, &CompletionModelBase::updateModel);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesChanged, this, &CompletionModelBase::divesChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompletionModelBase::updateModel()
|
|
|
|
{
|
|
|
|
setStringList(getStrings());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompletionModelBase::divesChanged(const QVector<dive *> &, DiveField field)
|
|
|
|
{
|
|
|
|
if (relevantDiveField(field))
|
|
|
|
updateModel();
|
|
|
|
}
|
|
|
|
|
2024-05-29 20:40:18 +02:00
|
|
|
static QStringList getCSVList(const std::string dive::*item)
|
2020-11-12 17:48:54 +01:00
|
|
|
{
|
|
|
|
QSet<QString> set;
|
2024-06-07 10:25:09 +02:00
|
|
|
for (auto &dive: divelog.dives) {
|
|
|
|
QString str = QString::fromStdString(dive.get()->*item);
|
2020-11-12 17:48:54 +01:00
|
|
|
for (const QString &value: str.split(",", SKIP_EMPTY))
|
|
|
|
set.insert(value.trimmed());
|
2014-02-27 20:09:57 -08:00
|
|
|
}
|
2020-11-12 17:48:54 +01:00
|
|
|
QStringList setList = set.values();
|
|
|
|
std::sort(setList.begin(), setList.end());
|
|
|
|
return setList;
|
|
|
|
}
|
2014-01-15 22:35:13 +02:00
|
|
|
|
2020-11-14 17:42:59 +01:00
|
|
|
QStringList BuddyCompletionModel::getStrings()
|
|
|
|
{
|
|
|
|
return getCSVList(&dive::buddy);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BuddyCompletionModel::relevantDiveField(const DiveField &f)
|
|
|
|
{
|
|
|
|
return f.buddy;
|
|
|
|
}
|
|
|
|
|
2022-02-12 14:03:18 +01:00
|
|
|
QStringList DiveGuideCompletionModel::getStrings()
|
2020-11-12 17:48:54 +01:00
|
|
|
{
|
2022-02-12 14:03:18 +01:00
|
|
|
return getCSVList(&dive::diveguide);
|
2020-11-12 17:48:54 +01:00
|
|
|
}
|
|
|
|
|
2022-02-12 14:03:18 +01:00
|
|
|
bool DiveGuideCompletionModel::relevantDiveField(const DiveField &f)
|
2020-11-12 17:48:54 +01:00
|
|
|
{
|
2022-02-12 14:03:18 +01:00
|
|
|
return f.diveguide;
|
2020-11-12 17:48:54 +01:00
|
|
|
}
|
|
|
|
|
2020-11-14 17:42:59 +01:00
|
|
|
QStringList SuitCompletionModel::getStrings()
|
2020-11-12 17:48:54 +01:00
|
|
|
{
|
|
|
|
QStringList list;
|
2024-06-07 10:25:09 +02:00
|
|
|
for (auto &dive: divelog.dives) {
|
2024-05-29 20:40:18 +02:00
|
|
|
QString suit = QString::fromStdString(dive->suit);
|
2020-11-12 17:48:54 +01:00
|
|
|
if (!list.contains(suit))
|
|
|
|
list.append(suit);
|
|
|
|
}
|
|
|
|
std::sort(list.begin(), list.end());
|
2020-11-14 17:42:59 +01:00
|
|
|
return list;
|
2020-11-12 17:48:54 +01:00
|
|
|
}
|
2013-08-13 10:30:22 -03:00
|
|
|
|
2020-11-14 17:42:59 +01:00
|
|
|
bool SuitCompletionModel::relevantDiveField(const DiveField &f)
|
|
|
|
{
|
|
|
|
return f.suit;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList TagCompletionModel::getStrings()
|
2013-11-02 02:20:02 +01:00
|
|
|
{
|
|
|
|
QStringList list;
|
2024-03-26 21:06:13 +01:00
|
|
|
for (const std::unique_ptr<divetag> &tag: g_tag_list)
|
|
|
|
list.append(QString::fromStdString(tag->name));
|
2020-11-14 17:42:59 +01:00
|
|
|
std::sort(list.begin(), list.end());
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TagCompletionModel::relevantDiveField(const DiveField &f)
|
|
|
|
{
|
|
|
|
return f.tags;
|
2013-11-02 02:20:02 +01:00
|
|
|
}
|