desktop: automatically reload completion-models

Instead of programatically reload the completion models, listen
to the relevant signals in the models. To that goal, derive all
the models from a base class.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-11-14 17:42:59 +01:00 committed by Dirk Hohndel
parent 52d5125926
commit 38a784f5af
5 changed files with 83 additions and 36 deletions

View file

@ -127,7 +127,6 @@ void TabDiveEquipment::updateData()
{
cylindersModel->updateDive(current_dive);
weightModel->updateDive(current_dive);
suitModel.updateModel();
ui.cylinders->view()->hideColumn(CylindersModel::DEPTH);
bool is_ccr = current_dive && get_dive_dc(current_dive, dc_number)->divemode == CCR;

View file

@ -261,18 +261,12 @@ void MainTab::divesChanged(const QVector<dive *> &dives, DiveField field)
}
if (field.divesite)
updateDiveSite(current_dive);
if (field.tags) {
tagModel.updateModel(); // TODO: Don't do this here
if (field.tags)
ui.tagWidget->setText(get_taglist_string(current_dive->tag_list));
}
if (field.buddy) {
buddyModel.updateModel(); // TODO: Don't do this here
if (field.buddy)
ui.buddy->setText(current_dive->buddy);
}
if (field.divemaster) {
diveMasterModel.updateModel(); // TODO: Don't do this here
if (field.divemaster)
ui.divemaster->setText(current_dive->divemaster);
}
// If duration or depth changed, the profile needs to be replotted
if (field.duration || field.depth)
@ -499,9 +493,6 @@ void MainTab::updateDiveInfo()
void MainTab::reload()
{
buddyModel.updateModel();
diveMasterModel.updateModel();
tagModel.updateModel();
}
void MainTab::refreshDisplayedDiveSite()

View file

@ -452,9 +452,9 @@ void QMLManager::selectSwipeRow(int row)
void QMLManager::updateAllGlobalLists()
{
buddyModel.updateModel(); emit buddyListChanged();
suitModel.updateModel(); emit suitListChanged();
divemasterModel.updateModel(); emit divemasterListChanged();
emit buddyListChanged();
emit suitListChanged();
emit divemasterListChanged();
// TODO: It would be nice if we could export the list of locations via model/view instead of a Q_PROPERTY
emit locationListChanged();
}

View file

@ -3,7 +3,6 @@
#include "core/dive.h"
#include "core/tag.h"
#include <QSet>
#include <QString>
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
#define SKIP_EMPTY Qt::SkipEmptyParts
@ -11,6 +10,24 @@
#define SKIP_EMPTY QString::SkipEmptyParts
#endif
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();
}
static QStringList getCSVList(char *dive::*item)
{
QSet<QString> set;
@ -26,17 +43,27 @@ static QStringList getCSVList(char *dive::*item)
return setList;
}
void BuddyCompletionModel::updateModel()
QStringList BuddyCompletionModel::getStrings()
{
setStringList(getCSVList(&dive::buddy));
return getCSVList(&dive::buddy);
}
void DiveMasterCompletionModel::updateModel()
bool BuddyCompletionModel::relevantDiveField(const DiveField &f)
{
setStringList(getCSVList(&dive::divemaster));
return f.buddy;
}
void SuitCompletionModel::updateModel()
QStringList DiveMasterCompletionModel::getStrings()
{
return getCSVList(&dive::divemaster);
}
bool DiveMasterCompletionModel::relevantDiveField(const DiveField &f)
{
return f.divemaster;
}
QStringList SuitCompletionModel::getStrings()
{
QStringList list;
struct dive *dive;
@ -47,18 +74,29 @@ void SuitCompletionModel::updateModel()
list.append(suit);
}
std::sort(list.begin(), list.end());
setStringList(list);
return list;
}
void TagCompletionModel::updateModel()
bool SuitCompletionModel::relevantDiveField(const DiveField &f)
{
return f.suit;
}
QStringList TagCompletionModel::getStrings()
{
if (g_tag_list == NULL)
return;
return {};
QStringList list;
struct tag_entry *current_tag_entry = g_tag_list;
while (current_tag_entry != NULL) {
list.append(QString(current_tag_entry->tag->name));
current_tag_entry = current_tag_entry->next;
}
setStringList(list);
std::sort(list.begin(), list.end());
return list;
}
bool TagCompletionModel::relevantDiveField(const DiveField &f)
{
return f.tags;
}

View file

@ -2,30 +2,49 @@
#ifndef COMPLETIONMODELS_H
#define COMPLETIONMODELS_H
#include "core/subsurface-qt/divelistnotifier.h"
#include <QStringListModel>
class BuddyCompletionModel : public QStringListModel {
struct dive;
class CompletionModelBase : public QStringListModel {
Q_OBJECT
public:
CompletionModelBase();
private slots:
void updateModel();
void divesChanged(const QVector<dive *> &dives, DiveField field);
protected:
virtual QStringList getStrings() = 0;
virtual bool relevantDiveField(const DiveField &f) = 0;
};
class DiveMasterCompletionModel : public QStringListModel {
class BuddyCompletionModel final : public CompletionModelBase {
Q_OBJECT
public:
void updateModel();
private:
QStringList getStrings() override;
bool relevantDiveField(const DiveField &f) override;
};
class SuitCompletionModel : public QStringListModel {
class DiveMasterCompletionModel final : public CompletionModelBase {
Q_OBJECT
public:
void updateModel();
private:
QStringList getStrings() override;
bool relevantDiveField(const DiveField &f) override;
};
class TagCompletionModel : public QStringListModel {
class SuitCompletionModel final : public CompletionModelBase {
Q_OBJECT
public:
void updateModel();
private:
QStringList getStrings() override;
bool relevantDiveField(const DiveField &f) override;
};
class TagCompletionModel final : public CompletionModelBase {
Q_OBJECT
private:
QStringList getStrings() override;
bool relevantDiveField(const DiveField &f) override;
};
#endif // COMPLETIONMODELS_H