From 9b5ef6223d518b34feb272926b4b7ebfa59a13fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Blin?= Date: Thu, 13 Apr 2023 14:54:43 -0400 Subject: [PATCH] tipsmodel: retranslate on lang changes GitLab: #1068 Change-Id: Ia876756f513e88d8a074b2b54edefa9f35b8b160 --- src/app/tipsmodel.cpp | 93 ++++++++++++++++++++++++------------------- src/app/tipsmodel.h | 3 ++ 2 files changed, 55 insertions(+), 41 deletions(-) diff --git a/src/app/tipsmodel.cpp b/src/app/tipsmodel.cpp index 085f0e7a..f2025096 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 +TipsModel::roleNames() const +{ + using namespace Tips; + QHash 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 -TipsModel::roleNames() const -{ - using namespace Tips; - QHash 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 1c95d06f..95aa2e35 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 roleNames() const override; +public Q_SLOTS: + void reset(); + private: VectorMapStringString tips_; AppSettingsManager* settingsManager_;