UI: Translate hotkey labels in configuration

Another request from GillianMC.

The translated strings have been placed in a separate "Hotkeys" context as an alternative
 to having to add the tr function to the Config class, or adding them to ConfigureHotkeys
context which is quite long. The English strings get attached to the items in the Action
column as "data", and are used for RetranslateUI and saving the hotkey configuration.
This commit is contained in:
Kyle K
2022-05-13 06:56:10 -07:00
parent b57df1dcb9
commit 669a9a644d
2 changed files with 40 additions and 26 deletions

View File

@ -61,14 +61,18 @@ ConfigureHotkeys::~ConfigureHotkeys() = default;
void ConfigureHotkeys::Populate(const HotkeyRegistry& registry) {
for (const auto& group : registry.hotkey_groups) {
auto* parent_item = new QStandardItem(group.first);
auto* parent_item =
new QStandardItem(QCoreApplication::translate("Hotkeys", qPrintable(group.first)));
parent_item->setEditable(false);
parent_item->setData(group.first);
for (const auto& hotkey : group.second) {
auto* action = new QStandardItem(hotkey.first);
auto* action =
new QStandardItem(QCoreApplication::translate("Hotkeys", qPrintable(hotkey.first)));
auto* keyseq =
new QStandardItem(hotkey.second.keyseq.toString(QKeySequence::NativeText));
auto* controller_keyseq = new QStandardItem(hotkey.second.controller_keyseq);
action->setEditable(false);
action->setData(hotkey.first);
keyseq->setEditable(false);
controller_keyseq->setEditable(false);
parent_item->appendRow({action, keyseq, controller_keyseq});
@ -93,6 +97,16 @@ void ConfigureHotkeys::RetranslateUI() {
ui->retranslateUi(this);
model->setHorizontalHeaderLabels({tr("Action"), tr("Hotkey"), tr("Controller Hotkey")});
for (int key_id = 0; key_id < model->rowCount(); key_id++) {
QStandardItem* parent = model->item(key_id, 0);
parent->setText(
QCoreApplication::translate("Hotkeys", qPrintable(parent->data().toString())));
for (int key_column_id = 0; key_column_id < parent->rowCount(); key_column_id++) {
QStandardItem* action = parent->child(key_column_id, name_column);
action->setText(
QCoreApplication::translate("Hotkeys", qPrintable(action->data().toString())));
}
}
}
void ConfigureHotkeys::Configure(QModelIndex index) {
@ -273,10 +287,10 @@ void ConfigureHotkeys::ApplyConfiguration(HotkeyRegistry& registry) {
const QStandardItem* controller_keyseq =
parent->child(key_column_id, controller_column);
for (auto& [group, sub_actions] : registry.hotkey_groups) {
if (group != parent->text())
if (group != parent->data())
continue;
for (auto& [action_name, hotkey] : sub_actions) {
if (action_name != action->text())
if (action_name != action->data())
continue;
hotkey.keyseq = QKeySequence(keyseq->text());
hotkey.controller_keyseq = controller_keyseq->text();