[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.
This commit is contained in:
parent
e3f933259b
commit
f1971eed86
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user