Skip to content
Snippets Groups Projects
Commit 9b5ef622 authored by Sébastien Blin's avatar Sébastien Blin
Browse files

tipsmodel: retranslate on lang changes

GitLab: #1068
Change-Id: Ia876756f513e88d8a074b2b54edefa9f35b8b160
parent a656070f
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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_;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment