From dd99862769dfbf07e6d6b226ed9232b72b24c8e9 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sun, 2 Jul 2023 17:05:51 +0200 Subject: [PATCH] Make the settings dialog react to language changes This covers the settings dialog itself and option pages based on `UiFileBasedOptionPage`. Other option pages need to react to the new `OptionPageWidget::retranslationRequired()` signal or handle the `QEvent::LanguageChange` event which is now propageted to the page's widget. --- settingsdialog/optioncategory.cpp | 17 +++++++++++++++++ settingsdialog/optioncategory.h | 1 + settingsdialog/optionpage.cpp | 3 +++ settingsdialog/optionpage.h | 6 +++++- settingsdialog/settingsdialog.cpp | 3 +++ 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/settingsdialog/optioncategory.cpp b/settingsdialog/optioncategory.cpp index 2a2304b..2a9ec61 100644 --- a/settingsdialog/optioncategory.cpp +++ b/settingsdialog/optioncategory.cpp @@ -1,6 +1,9 @@ #include "./optioncategory.h" #include "./optionpage.h" +#include +#include + namespace QtUtilities { /*! @@ -59,6 +62,20 @@ void OptionCategory::resetAllPages() } } +/*! + * \brief Triggers retranslation of all pages. + * \remarks Has no effect if the pages don't react to the LanguageChange event. + */ +void OptionCategory::retranslateAllPages() +{ + auto event = QEvent(QEvent::LanguageChange); + for (auto *const page : m_pages) { + if (page->hasBeenShown()) { + QCoreApplication::sendEvent(page->widget(), &event); + } + } +} + /*! * \brief Returns whether the option category matches the specified \a * searchKeyWord. diff --git a/settingsdialog/optioncategory.h b/settingsdialog/optioncategory.h index ab37358..1a1bfc6 100644 --- a/settingsdialog/optioncategory.h +++ b/settingsdialog/optioncategory.h @@ -33,6 +33,7 @@ public: void assignPages(const QList &pages); bool applyAllPages(); void resetAllPages(); + void retranslateAllPages(); bool matches(const QString &searchKeyWord) const; int currentIndex() const; void setCurrentIndex(int currentIndex); diff --git a/settingsdialog/optionpage.cpp b/settingsdialog/optionpage.cpp index bdd8eab..8a7298c 100644 --- a/settingsdialog/optionpage.cpp +++ b/settingsdialog/optionpage.cpp @@ -99,6 +99,9 @@ bool OptionPageWidget::event(QEvent *event) case QEvent::PaletteChange: emit paletteChanged(); break; + case QEvent::LanguageChange: + emit retranslationRequired(); + break; default:; } return QWidget::event(event); diff --git a/settingsdialog/optionpage.h b/settingsdialog/optionpage.h index 99a64c0..af1aa9d 100644 --- a/settingsdialog/optionpage.h +++ b/settingsdialog/optionpage.h @@ -20,6 +20,7 @@ public: Q_SIGNALS: void paletteChanged(); + void retranslationRequired(); protected: bool event(QEvent *) override; @@ -143,11 +144,14 @@ template UiFileBasedOptionPage::~UiFileBasedOptionPage( */ template QWidget *UiFileBasedOptionPage::setupWidget() { - QWidget *widget = new OptionPageWidget(); + auto *const widget = new OptionPageWidget(); if (!m_ui) { m_ui.reset(new UiClass); } m_ui->setupUi(widget); + QObject::connect(widget, &OptionPageWidget::retranslationRequired, [this, widget] { + m_ui->retranslateUi(widget); + }); return widget; } diff --git a/settingsdialog/settingsdialog.cpp b/settingsdialog/settingsdialog.cpp index ce5f8e5..92baf57 100644 --- a/settingsdialog/settingsdialog.cpp +++ b/settingsdialog/settingsdialog.cpp @@ -337,6 +337,9 @@ bool SettingsDialog::event(QEvent *event) case QEvent::PaletteChange: setStyleSheet(dialogStyleForPalette(palette())); break; + case QEvent::LanguageChange: + m_ui->retranslateUi(this); + break; default:; } return res;