diff --git a/src/app/tipsmodel.cpp b/src/app/tipsmodel.cpp index 085f0e7a0e5b6b9b5273e3e065799ebf12aa124f..f2025096dede5de9ac311f9f76fdad810547bcee 100644 --- a/src/app/tipsmodel.cpp +++ b/src/app/tipsmodel.cpp @@ -25,6 +25,56 @@ TipsModel::TipsModel(AppSettingsManager* settingsManager, QObject* parent) : QAbstractListModel(parent) , settingsManager_(settingsManager) { + QObject::connect(settingsManager_, &AppSettingsManager::retranslate, this, &TipsModel::reset); + reset(); +} + +int +TipsModel::rowCount(const QModelIndex& parent) const +{ + if (parent.isValid()) + return 0; + return tips_.size(); +} + +QVariant +TipsModel::data(const QModelIndex& index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + auto tip = tips_.at(index.row()); + + switch (role) { + case Tips::Role::TipId: + return QVariant::fromValue(tip["id"].toInt()); + case Tips::Role::Title: + return QVariant::fromValue(tip["title"]); + case Tips::Role::Description: + return QVariant::fromValue(tip["desc"]); + case Tips::Role::Type: + return QVariant::fromValue(tip["type"]); + } + return QVariant(); +} + +QHash<int, QByteArray> +TipsModel::roleNames() const +{ + using namespace Tips; + QHash<int, QByteArray> roles; +#define X(role) roles[role] = #role; + TIPS_ROLES +#undef X + return roles; +} + +void +TipsModel::reset() +{ + beginResetModel(); + tips_.clear(); + tips_.append({{"id", "0"}, {"title", tr("Customize")}, {"desc", ""}, {"type", "customize"}}); tips_.append({{"id", "13"}, {"title", tr("Backup account")}, {"desc", ""}, {"type", "backup"}}); tips_.append({{"id", "1"}, @@ -102,44 +152,5 @@ TipsModel::TipsModel(AppSettingsManager* settingsManager, QObject* parent) std::random_device rd; std::mt19937 g(rd()); std::shuffle(tips_.begin() + 2, tips_.end(), g); -} - -int -TipsModel::rowCount(const QModelIndex& parent) const -{ - if (parent.isValid()) - return 0; - return tips_.size(); -} - -QVariant -TipsModel::data(const QModelIndex& index, int role) const -{ - if (!index.isValid()) - return QVariant(); - - auto tip = tips_.at(index.row()); - - switch (role) { - case Tips::Role::TipId: - return QVariant::fromValue(tip["id"].toInt()); - case Tips::Role::Title: - return QVariant::fromValue(tip["title"]); - case Tips::Role::Description: - return QVariant::fromValue(tip["desc"]); - case Tips::Role::Type: - return QVariant::fromValue(tip["type"]); - } - return QVariant(); -} - -QHash<int, QByteArray> -TipsModel::roleNames() const -{ - using namespace Tips; - QHash<int, QByteArray> roles; -#define X(role) roles[role] = #role; - TIPS_ROLES -#undef X - return roles; -} + endResetModel(); +} \ No newline at end of file diff --git a/src/app/tipsmodel.h b/src/app/tipsmodel.h index 1c95d06f855a29016a55ea551275a0fd44efd0a6..95aa2e35ac065736de15e9dde057e76395d50ec4 100644 --- a/src/app/tipsmodel.h +++ b/src/app/tipsmodel.h @@ -52,6 +52,9 @@ public: QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; QHash<int, QByteArray> roleNames() const override; +public Q_SLOTS: + void reset(); + private: VectorMapStringString tips_; AppSettingsManager* settingsManager_;