[calamares] Indulge in template-fu to refactor
- since we've got two blocks of code copy-pasted, which both decide to call one or the other of two member functions based on a flavor value, turn that into a templated function. - passing member functions looks a bit weird, and calling them is syntactically surprising, but it cuts down the code a lot.
This commit is contained in:
parent
2dcf265c40
commit
9f66b63c00
@ -186,6 +186,31 @@ CalamaresWindow::getQmlNavigation()
|
|||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**@brief Picks one of two methods to call
|
||||||
|
*
|
||||||
|
* Calls method (member function) @p widget or @p qml with arguments @p a
|
||||||
|
* on the given window, based on the flavor.
|
||||||
|
*/
|
||||||
|
template < typename widgetMaker, typename... args >
|
||||||
|
QWidget*
|
||||||
|
flavoredWidget( Calamares::Branding::PanelFlavor flavor,
|
||||||
|
CalamaresWindow* w,
|
||||||
|
widgetMaker widget,
|
||||||
|
widgetMaker qml,
|
||||||
|
args... a )
|
||||||
|
{
|
||||||
|
// Member-function calling syntax is (object.*member)(args)
|
||||||
|
switch ( flavor )
|
||||||
|
{
|
||||||
|
case Calamares::Branding::PanelFlavor::Widget:
|
||||||
|
return ( w->*widget )( a... );
|
||||||
|
case Calamares::Branding::PanelFlavor::Qml:
|
||||||
|
return ( w->*qml )( a... );
|
||||||
|
case Calamares::Branding::PanelFlavor::None:
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CalamaresWindow::CalamaresWindow( QWidget* parent )
|
CalamaresWindow::CalamaresWindow( QWidget* parent )
|
||||||
: QWidget( parent )
|
: QWidget( parent )
|
||||||
, m_debugWindow( nullptr )
|
, m_debugWindow( nullptr )
|
||||||
@ -231,20 +256,12 @@ CalamaresWindow::CalamaresWindow( QWidget* parent )
|
|||||||
QBoxLayout* mainLayout = new QHBoxLayout;
|
QBoxLayout* mainLayout = new QHBoxLayout;
|
||||||
setLayout( mainLayout );
|
setLayout( mainLayout );
|
||||||
|
|
||||||
QWidget* sideBox = nullptr;
|
QWidget* sideBox = flavoredWidget(
|
||||||
switch ( branding->sidebarFlavor() )
|
branding->sidebarFlavor(),
|
||||||
{
|
this,
|
||||||
case Calamares::Branding::PanelFlavor::Widget:
|
&CalamaresWindow::getWidgetSidebar,
|
||||||
sideBox = getWidgetSidebar(
|
&CalamaresWindow::getQmlSidebar,
|
||||||
qBound( 100, CalamaresUtils::defaultFontHeight() * 12, w < windowPreferredWidth ? 100 : 190 ) );
|
qBound( 100, CalamaresUtils::defaultFontHeight() * 12, w < windowPreferredWidth ? 100 : 190 ) );
|
||||||
break;
|
|
||||||
case Calamares::Branding::PanelFlavor::Qml:
|
|
||||||
sideBox = getQmlSidebar(
|
|
||||||
qBound( 100, CalamaresUtils::defaultFontHeight() * 12, w < windowPreferredWidth ? 100 : 190 ) );
|
|
||||||
break;
|
|
||||||
case Calamares::Branding::PanelFlavor::None:
|
|
||||||
sideBox = nullptr;
|
|
||||||
}
|
|
||||||
if ( sideBox )
|
if ( sideBox )
|
||||||
{
|
{
|
||||||
mainLayout->addWidget( sideBox );
|
mainLayout->addWidget( sideBox );
|
||||||
@ -264,18 +281,8 @@ CalamaresWindow::CalamaresWindow( QWidget* parent )
|
|||||||
// event, which is also the ViewManager's responsibility.
|
// event, which is also the ViewManager's responsibility.
|
||||||
QBoxLayout* contentsLayout = new QVBoxLayout;
|
QBoxLayout* contentsLayout = new QVBoxLayout;
|
||||||
contentsLayout->addWidget( m_viewManager->centralWidget() );
|
contentsLayout->addWidget( m_viewManager->centralWidget() );
|
||||||
QWidget* navigation = nullptr;
|
QWidget* navigation = flavoredWidget(
|
||||||
switch ( branding->navigationFlavor() )
|
branding->navigationFlavor(), this, &CalamaresWindow::getWidgetNavigation, &CalamaresWindow::getQmlNavigation );
|
||||||
{
|
|
||||||
case Calamares::Branding::PanelFlavor::Widget:
|
|
||||||
navigation = getWidgetNavigation();
|
|
||||||
break;
|
|
||||||
case Calamares::Branding::PanelFlavor::Qml:
|
|
||||||
navigation = getQmlNavigation();
|
|
||||||
break;
|
|
||||||
case Calamares::Branding::PanelFlavor::None:
|
|
||||||
navigation = nullptr;
|
|
||||||
}
|
|
||||||
if ( navigation )
|
if ( navigation )
|
||||||
{
|
{
|
||||||
contentsLayout->addWidget( navigation );
|
contentsLayout->addWidget( navigation );
|
||||||
|
Loading…
Reference in New Issue
Block a user