[calamares] Refactor sidebar creation

- None of these need to be methods of the main window,
  and it can all be put tidy away as static free functions.
main
Adriaan de Groot 4 years ago
parent a8463a8763
commit 0f50085bb9

@ -55,9 +55,25 @@ windowDimensionToPixels( const Calamares::Branding::WindowDimension& u )
return 0; return 0;
} }
/** @brief Get a button-sized icon. */
static inline QPixmap
getButtonIcon( const QString& name )
{
return Calamares::Branding::instance()->image( name, QSize( 22, 22 ) );
}
QWidget* static inline void
CalamaresWindow::getWidgetSidebar( QWidget* parent, int desiredWidth ) setButtonIcon( QPushButton* button, const QString& name )
{
auto icon = getButtonIcon( name );
if ( button && !icon.isNull() )
{
button->setIcon( icon );
}
}
static QWidget*
getWidgetSidebar( CalamaresWindow* window, Calamares::ViewManager* viewManager, QWidget* parent, int desiredWidth )
{ {
const Calamares::Branding* const branding = Calamares::Branding::instance(); const Calamares::Branding* const branding = Calamares::Branding::instance();
@ -91,7 +107,7 @@ CalamaresWindow::getWidgetSidebar( QWidget* parent, int desiredWidth )
logoLayout->addStretch(); logoLayout->addStretch();
ProgressTreeView* tv = new ProgressTreeView( sideBox ); ProgressTreeView* tv = new ProgressTreeView( sideBox );
tv->setModel( Calamares::ViewManager::instance() ); tv->setModel( viewManager );
tv->setFocusPolicy( Qt::NoFocus ); tv->setFocusPolicy( Qt::NoFocus );
sideLayout->addWidget( tv ); sideLayout->addWidget( tv );
@ -99,37 +115,22 @@ CalamaresWindow::getWidgetSidebar( QWidget* parent, int desiredWidth )
{ {
QPushButton* debugWindowBtn = new QPushButton; QPushButton* debugWindowBtn = new QPushButton;
debugWindowBtn->setObjectName( "debugButton" ); debugWindowBtn->setObjectName( "debugButton" );
CALAMARES_RETRANSLATE( debugWindowBtn->setText( tr( "Show debug information" ) ); ) CALAMARES_RETRANSLATE_WIDGET( debugWindowBtn,
debugWindowBtn->setText( QCoreApplication::translate(
CalamaresWindow::staticMetaObject.className(), "Show debug information" ) ); )
sideLayout->addWidget( debugWindowBtn ); sideLayout->addWidget( debugWindowBtn );
debugWindowBtn->setFlat( true ); debugWindowBtn->setFlat( true );
debugWindowBtn->setCheckable( true ); debugWindowBtn->setCheckable( true );
connect( debugWindowBtn, &QPushButton::clicked, this, &CalamaresWindow::showDebugWindow ); QObject::connect( debugWindowBtn, &QPushButton::clicked, window, &CalamaresWindow::showDebugWindow );
connect( this, &CalamaresWindow::debugWindowShown, debugWindowBtn, &QPushButton::setChecked ); QObject::connect( window, &CalamaresWindow::debugWindowShown, debugWindowBtn, &QPushButton::setChecked );
} }
CalamaresUtils::unmarginLayout( sideLayout ); CalamaresUtils::unmarginLayout( sideLayout );
return sideBox; return sideBox;
} }
/** @brief Get a button-sized icon. */ static QWidget*
static inline QPixmap getWidgetNavigation( CalamaresWindow*, Calamares::ViewManager* viewManager, QWidget* parent )
getButtonIcon( const QString& name )
{
return Calamares::Branding::instance()->image( name, QSize( 22, 22 ) );
}
static inline void
setButtonIcon( QPushButton* button, const QString& name )
{
auto icon = getButtonIcon( name );
if ( button && !icon.isNull() )
{
button->setIcon( icon );
}
}
QWidget*
CalamaresWindow::getWidgetNavigation( QWidget* parent )
{ {
QWidget* navigation = new QWidget( parent ); QWidget* navigation = new QWidget( parent );
QBoxLayout* bottomLayout = new QHBoxLayout; QBoxLayout* bottomLayout = new QHBoxLayout;
@ -137,43 +138,51 @@ CalamaresWindow::getWidgetNavigation( QWidget* parent )
// Create buttons and sets an initial icon; the icons may change // Create buttons and sets an initial icon; the icons may change
{ {
auto* back = new QPushButton( getButtonIcon( QStringLiteral( "go-previous" ) ), tr( "&Back" ), navigation ); auto* back
= new QPushButton( getButtonIcon( QStringLiteral( "go-previous" ) ),
QCoreApplication::translate( CalamaresWindow::staticMetaObject.className(), "&Back" ),
navigation );
back->setObjectName( "view-button-back" ); back->setObjectName( "view-button-back" );
back->setEnabled( m_viewManager->backEnabled() ); back->setEnabled( viewManager->backEnabled() );
connect( back, &QPushButton::clicked, m_viewManager, &Calamares::ViewManager::back ); QObject::connect( back, &QPushButton::clicked, viewManager, &Calamares::ViewManager::back );
connect( m_viewManager, &Calamares::ViewManager::backEnabledChanged, back, &QPushButton::setEnabled ); QObject::connect( viewManager, &Calamares::ViewManager::backEnabledChanged, back, &QPushButton::setEnabled );
connect( m_viewManager, &Calamares::ViewManager::backLabelChanged, back, &QPushButton::setText ); QObject::connect( viewManager, &Calamares::ViewManager::backLabelChanged, back, &QPushButton::setText );
connect( m_viewManager, &Calamares::ViewManager::backIconChanged, this, [=]( QString n ) { QObject::connect(
setButtonIcon( back, n ); viewManager, &Calamares::ViewManager::backIconChanged, [=]( QString n ) { setButtonIcon( back, n ); } );
} ); QObject::connect(
connect( m_viewManager, &Calamares::ViewManager::backAndNextVisibleChanged, back, &QPushButton::setVisible ); viewManager, &Calamares::ViewManager::backAndNextVisibleChanged, back, &QPushButton::setVisible );
bottomLayout->addWidget( back ); bottomLayout->addWidget( back );
} }
{ {
auto* next = new QPushButton( getButtonIcon( QStringLiteral( "go-next" ) ), tr( "&Next" ), navigation ); auto* next
= new QPushButton( getButtonIcon( QStringLiteral( "go-next" ) ),
QCoreApplication::translate( CalamaresWindow::staticMetaObject.className(), "&Next" ),
navigation );
next->setObjectName( "view-button-next" ); next->setObjectName( "view-button-next" );
next->setEnabled( m_viewManager->nextEnabled() ); next->setEnabled( viewManager->nextEnabled() );
connect( next, &QPushButton::clicked, m_viewManager, &Calamares::ViewManager::next ); QObject::connect( next, &QPushButton::clicked, viewManager, &Calamares::ViewManager::next );
connect( m_viewManager, &Calamares::ViewManager::nextEnabledChanged, next, &QPushButton::setEnabled ); QObject::connect( viewManager, &Calamares::ViewManager::nextEnabledChanged, next, &QPushButton::setEnabled );
connect( m_viewManager, &Calamares::ViewManager::nextLabelChanged, next, &QPushButton::setText ); QObject::connect( viewManager, &Calamares::ViewManager::nextLabelChanged, next, &QPushButton::setText );
connect( m_viewManager, &Calamares::ViewManager::nextIconChanged, this, [=]( QString n ) { QObject::connect(
setButtonIcon( next, n ); viewManager, &Calamares::ViewManager::nextIconChanged, [=]( QString n ) { setButtonIcon( next, n ); } );
} ); QObject::connect(
connect( m_viewManager, &Calamares::ViewManager::backAndNextVisibleChanged, next, &QPushButton::setVisible ); viewManager, &Calamares::ViewManager::backAndNextVisibleChanged, next, &QPushButton::setVisible );
bottomLayout->addWidget( next ); bottomLayout->addWidget( next );
} }
bottomLayout->addSpacing( 12 ); bottomLayout->addSpacing( 12 );
{ {
auto* quit = new QPushButton( getButtonIcon( QStringLiteral( "dialog-cancel" ) ), tr( "&Cancel" ), navigation ); auto* quit
= new QPushButton( getButtonIcon( QStringLiteral( "dialog-cancel" ) ),
QCoreApplication::translate( CalamaresWindow::staticMetaObject.className(), "&Cancel" ),
navigation );
quit->setObjectName( "view-button-cancel" ); quit->setObjectName( "view-button-cancel" );
connect( quit, &QPushButton::clicked, m_viewManager, &Calamares::ViewManager::quit ); QObject::connect( quit, &QPushButton::clicked, viewManager, &Calamares::ViewManager::quit );
connect( m_viewManager, &Calamares::ViewManager::quitEnabledChanged, quit, &QPushButton::setEnabled ); QObject::connect( viewManager, &Calamares::ViewManager::quitEnabledChanged, quit, &QPushButton::setEnabled );
connect( m_viewManager, &Calamares::ViewManager::quitLabelChanged, quit, &QPushButton::setText ); QObject::connect( viewManager, &Calamares::ViewManager::quitLabelChanged, quit, &QPushButton::setText );
connect( m_viewManager, &Calamares::ViewManager::quitIconChanged, this, [=]( QString n ) { QObject::connect(
setButtonIcon( quit, n ); viewManager, &Calamares::ViewManager::quitIconChanged, [=]( QString n ) { setButtonIcon( quit, n ); } );
} ); QObject::connect( viewManager, &Calamares::ViewManager::quitTooltipChanged, quit, &QPushButton::setToolTip );
connect( m_viewManager, &Calamares::ViewManager::quitTooltipChanged, quit, &QPushButton::setToolTip ); QObject::connect( viewManager, &Calamares::ViewManager::quitVisibleChanged, quit, &QPushButton::setVisible );
connect( m_viewManager, &Calamares::ViewManager::quitVisibleChanged, quit, &QPushButton::setVisible );
bottomLayout->addWidget( quit ); bottomLayout->addWidget( quit );
} }
@ -183,8 +192,8 @@ CalamaresWindow::getWidgetNavigation( QWidget* parent )
} }
#ifdef WITH_QML #ifdef WITH_QML
QWidget* static QWidget*
CalamaresWindow::getQmlSidebar( QWidget* parent, int desiredWidth ) getQmlSidebar( CalamaresWindow*, Calamares::ViewManager*, QWidget* parent, int desiredWidth )
{ {
CalamaresUtils::registerQmlModels(); CalamaresUtils::registerQmlModels();
QQuickWidget* w = new QQuickWidget( parent ); QQuickWidget* w = new QQuickWidget( parent );
@ -196,8 +205,8 @@ CalamaresWindow::getQmlSidebar( QWidget* parent, int desiredWidth )
return w; return w;
} }
QWidget* static QWidget*
CalamaresWindow::getQmlNavigation( QWidget* parent ) getQmlNavigation( CalamaresWindow*, Calamares::ViewManager*, QWidget* parent )
{ {
CalamaresUtils::registerQmlModels(); CalamaresUtils::registerQmlModels();
QQuickWidget* w = new QQuickWidget( parent ); QQuickWidget* w = new QQuickWidget( parent );
@ -219,18 +228,19 @@ CalamaresWindow::getQmlNavigation( QWidget* parent )
} }
#else #else
// Bogus to keep the linker happy // Bogus to keep the linker happy
QWidget* //
CalamaresWindow::getQmlSidebar( QWidget*, int ) // Calls to flavoredWidget() still refer to these *names*
// even if they are subsequently not used.
static QWidget*
getQmlSidebar( CalamaresWindow*, Calamares::ViewManager*, QWidget* parent, int desiredWidth )
{ {
return nullptr; return nullptr;
} }
QWidget* static QWidget*
CalamaresWindow::getQmlNavigation( QWidget* ) getQmlNavigation( CalamaresWindow*, Calamares::ViewManager*, QWidget* parent )
{ {
return nullptr; return nullptr;
} }
#endif #endif
/**@brief Picks one of two methods to call /**@brief Picks one of two methods to call
@ -250,14 +260,14 @@ flavoredWidget( Calamares::Branding::PanelFlavor flavor,
#ifndef WITH_QML #ifndef WITH_QML
Q_UNUSED( qml ) Q_UNUSED( qml )
#endif #endif
// Member-function calling syntax is (object.*member)(args) auto* viewManager = Calamares::ViewManager::instance();
switch ( flavor ) switch ( flavor )
{ {
case Calamares::Branding::PanelFlavor::Widget: case Calamares::Branding::PanelFlavor::Widget:
return ( w->*widget )( parent, a... ); return widget( w, viewManager, parent, a... );
#ifdef WITH_QML #ifdef WITH_QML
case Calamares::Branding::PanelFlavor::Qml: case Calamares::Branding::PanelFlavor::Qml:
return ( w->*qml )( parent, a... ); return qml( w, viewManager, parent, a... );
#endif #endif
case Calamares::Branding::PanelFlavor::None: case Calamares::Branding::PanelFlavor::None:
return nullptr; return nullptr;
@ -361,14 +371,11 @@ CalamaresWindow::CalamaresWindow( QWidget* parent )
branding->sidebarFlavor(), branding->sidebarFlavor(),
this, this,
baseWidget, baseWidget,
&CalamaresWindow::getWidgetSidebar, ::getWidgetSidebar,
&CalamaresWindow::getQmlSidebar, ::getQmlSidebar,
qBound( 100, CalamaresUtils::defaultFontHeight() * 12, w < windowPreferredWidth ? 100 : 190 ) ); qBound( 100, CalamaresUtils::defaultFontHeight() * 12, w < windowPreferredWidth ? 100 : 190 ) );
QWidget* navigation = flavoredWidget( branding->navigationFlavor(), QWidget* navigation
this, = flavoredWidget( branding->navigationFlavor(), this, baseWidget, ::getWidgetNavigation, ::getQmlNavigation );
baseWidget,
&CalamaresWindow::getWidgetNavigation,
&CalamaresWindow::getQmlNavigation );
// Build up the contentsLayout (a VBox) top-to-bottom // Build up the contentsLayout (a VBox) top-to-bottom
// .. note that the bottom is mirrored wrt. the top // .. note that the bottom is mirrored wrt. the top

@ -53,14 +53,6 @@ protected:
virtual void closeEvent( QCloseEvent* e ) override; virtual void closeEvent( QCloseEvent* e ) override;
private: private:
// Two variations on sidebar (the progress view)
QWidget* getWidgetSidebar( QWidget* parent, int desiredWidth );
QWidget* getQmlSidebar( QWidget* parent, int desiredWidth );
// Two variations on navigation (buttons at bottom)
QWidget* getWidgetNavigation( QWidget* parent );
QWidget* getQmlNavigation( QWidget* parent );
QPointer< Calamares::DebugWindow > m_debugWindow; // Managed by self QPointer< Calamares::DebugWindow > m_debugWindow; // Managed by self
Calamares::ViewManager* m_viewManager; Calamares::ViewManager* m_viewManager;
}; };

Loading…
Cancel
Save