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.
This commit is contained in:
Martchus 2023-07-02 17:05:51 +02:00
parent bc3f84e65d
commit dd99862769
5 changed files with 29 additions and 1 deletions

View File

@ -1,6 +1,9 @@
#include "./optioncategory.h"
#include "./optionpage.h"
#include <QCoreApplication>
#include <QEvent>
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.

View File

@ -33,6 +33,7 @@ public:
void assignPages(const QList<OptionPage *> &pages);
bool applyAllPages();
void resetAllPages();
void retranslateAllPages();
bool matches(const QString &searchKeyWord) const;
int currentIndex() const;
void setCurrentIndex(int currentIndex);

View File

@ -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);

View File

@ -20,6 +20,7 @@ public:
Q_SIGNALS:
void paletteChanged();
void retranslationRequired();
protected:
bool event(QEvent *) override;
@ -143,11 +144,14 @@ template <class UiClass> UiFileBasedOptionPage<UiClass>::~UiFileBasedOptionPage(
*/
template <class UiClass> QWidget *UiFileBasedOptionPage<UiClass>::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;
}

View File

@ -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;