From 9f66b63c00fa997bc6052ff89034b3ec1abc00f1 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 1 Apr 2020 15:30:47 +0200 Subject: [PATCH] [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. --- src/calamares/CalamaresWindow.cpp | 59 +++++++++++++++++-------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/src/calamares/CalamaresWindow.cpp b/src/calamares/CalamaresWindow.cpp index f29485ea9..9ec5a7405 100644 --- a/src/calamares/CalamaresWindow.cpp +++ b/src/calamares/CalamaresWindow.cpp @@ -186,6 +186,31 @@ CalamaresWindow::getQmlNavigation() 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 ) : QWidget( parent ) , m_debugWindow( nullptr ) @@ -231,20 +256,12 @@ CalamaresWindow::CalamaresWindow( QWidget* parent ) QBoxLayout* mainLayout = new QHBoxLayout; setLayout( mainLayout ); - QWidget* sideBox = nullptr; - switch ( branding->sidebarFlavor() ) - { - case Calamares::Branding::PanelFlavor::Widget: - sideBox = getWidgetSidebar( - 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; - } + QWidget* sideBox = flavoredWidget( + branding->sidebarFlavor(), + this, + &CalamaresWindow::getWidgetSidebar, + &CalamaresWindow::getQmlSidebar, + qBound( 100, CalamaresUtils::defaultFontHeight() * 12, w < windowPreferredWidth ? 100 : 190 ) ); if ( sideBox ) { mainLayout->addWidget( sideBox ); @@ -264,18 +281,8 @@ CalamaresWindow::CalamaresWindow( QWidget* parent ) // event, which is also the ViewManager's responsibility. QBoxLayout* contentsLayout = new QVBoxLayout; contentsLayout->addWidget( m_viewManager->centralWidget() ); - QWidget* navigation = nullptr; - switch ( branding->navigationFlavor() ) - { - case Calamares::Branding::PanelFlavor::Widget: - navigation = getWidgetNavigation(); - break; - case Calamares::Branding::PanelFlavor::Qml: - navigation = getQmlNavigation(); - break; - case Calamares::Branding::PanelFlavor::None: - navigation = nullptr; - } + QWidget* navigation = flavoredWidget( + branding->navigationFlavor(), this, &CalamaresWindow::getWidgetNavigation, &CalamaresWindow::getQmlNavigation ); if ( navigation ) { contentsLayout->addWidget( navigation );