From f1971eed86ba230fff69c0f3c7b80a2b2e96819e Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 29 Aug 2019 14:53:03 +0200 Subject: [PATCH] [libcalamares] Support calling a slot on retranslation - sometimes a slot is easier than a lambda. Introduce a macro CALAMARES_RETRANSLATE_SLOT that calls a given slot in an object on language change. - extend the retranslator with support for calling slots: - use Qt's signal/slot mechanism alongside the private list of functions to call - provide convenience for obtaining the retranslator of an object. --- src/libcalamares/utils/Retranslator.cpp | 18 ++++++++++-------- src/libcalamares/utils/Retranslator.h | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/libcalamares/utils/Retranslator.cpp b/src/libcalamares/utils/Retranslator.cpp index e154350b9..d761263ac 100644 --- a/src/libcalamares/utils/Retranslator.cpp +++ b/src/libcalamares/utils/Retranslator.cpp @@ -118,8 +118,8 @@ translatorLocaleName() return s_translatorLocaleName; } -void -Retranslator::attachRetranslator( QObject* parent, std::function< void( void ) > retranslateFunc ) +Retranslator* +Retranslator::retranslatorFor( QObject* parent ) { Retranslator* r = nullptr; for ( QObject* child : parent->children() ) @@ -127,16 +127,17 @@ Retranslator::attachRetranslator( QObject* parent, std::function< void( void ) > r = qobject_cast< Retranslator* >( child ); if ( r ) { - break; + return r; } } - if ( !r ) - { - r = new Retranslator( parent ); - } + return new Retranslator( parent ); +} - r->m_retranslateFuncList.append( retranslateFunc ); +void +Retranslator::attachRetranslator( QObject* parent, std::function< void( void ) > retranslateFunc ) +{ + retranslatorFor( parent )->m_retranslateFuncList.append( retranslateFunc ); retranslateFunc(); } @@ -159,6 +160,7 @@ Retranslator::eventFilter( QObject* obj, QEvent* e ) { func(); } + emit languageChange(); } } // pass the event on to the base diff --git a/src/libcalamares/utils/Retranslator.h b/src/libcalamares/utils/Retranslator.h index 32e8dcf41..58c60b761 100644 --- a/src/libcalamares/utils/Retranslator.h +++ b/src/libcalamares/utils/Retranslator.h @@ -46,10 +46,17 @@ class Retranslator : public QObject { Q_OBJECT public: + /// @brief Call @p retranslateFunc when the language changes static void attachRetranslator( QObject* parent, std::function< void( void ) > retranslateFunc ); + /// @brief What retranslator belongs to @p parent (may create one) + static Retranslator* retranslatorFor( QObject* parent ); + /// @brief Call @p retranslateFunc when the language changes void addRetranslateFunc( std::function< void( void ) > retranslateFunc ); +signals: + void languageChange(); + protected: bool eventFilter( QObject* obj, QEvent* e ) override; @@ -65,5 +72,12 @@ private: #define CALAMARES_RETRANSLATE( body ) CalamaresUtils::Retranslator::attachRetranslator( this, [=] { body } ); #define CALAMARES_RETRANSLATE_WIDGET( widget, body ) \ CalamaresUtils::Retranslator::attachRetranslator( widget, [=] { body } ); +#define CALAMARES_RETRANSLATE_SLOT( slotfunc ) \ + { \ + this->connect( CalamaresUtils::Retranslator::retranslatorFor( this ), \ + &CalamaresUtils::Retranslator::languageChange, \ + this, \ + slotfunc ); \ + } #endif