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