From 38deb66e42a1481f7eec72d51efea1ac85875731 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 2 Apr 2020 12:31:14 +0200 Subject: [PATCH] [libcalamaresui] Remove *back* button from ViewManager - Now the back button should be done by clients as well - Refactor in CalamaresWindow to avoid leaking local button pointers to surrounding code. - Add macro UPDATE_BUTTON_PROPERTY for convenience in ViewManager (ugh, macro) to change a value and emit corresponding update signal. --- src/calamares/CalamaresWindow.cpp | 49 ++++++++++++++++++------------ src/libcalamaresui/ViewManager.cpp | 49 ++++++++++++------------------ src/libcalamaresui/ViewManager.h | 26 +++++++++++++++- 3 files changed, 75 insertions(+), 49 deletions(-) diff --git a/src/calamares/CalamaresWindow.cpp b/src/calamares/CalamaresWindow.cpp index a3bdf2683..991d81dd3 100644 --- a/src/calamares/CalamaresWindow.cpp +++ b/src/calamares/CalamaresWindow.cpp @@ -164,28 +164,39 @@ QWidget* CalamaresWindow::getWidgetNavigation() { QWidget* navigation = new QWidget( this ); - - // Create buttons and sets an initial icon; the icons may change - auto* back = new QPushButton( getButtonIcon( QStringLiteral( "go-previous" ) ), tr( "&Back" ), navigation ); - back->setObjectName( "view-button-back" ); - connect( back, &QPushButton::clicked, m_viewManager, &Calamares::ViewManager::back ); - auto* next = new QPushButton( getButtonIcon( QStringLiteral( "go-next" ) ), tr( "&Next" ), navigation ); - next->setObjectName( "view-button-next" ); - connect( next, &QPushButton::clicked, m_viewManager, &Calamares::ViewManager::next ); - connect( m_viewManager, &Calamares::ViewManager::nextEnabledChanged, next, &QPushButton::setEnabled ); - connect( m_viewManager, &Calamares::ViewManager::nextLabelChanged, next, &QPushButton::setText ); - connect( - m_viewManager, &Calamares::ViewManager::nextIconChanged, this, [=]( QString n ) { setButtonIcon( next, n ); } ); - auto* quit = new QPushButton( getButtonIcon( QStringLiteral( "dialog-cancel" ) ), tr( "&Cancel" ), navigation ); - quit->setObjectName( "view-button-cancel" ); - connect( quit, &QPushButton::clicked, m_viewManager, &Calamares::ViewManager::quit ); - QBoxLayout* bottomLayout = new QHBoxLayout; bottomLayout->addStretch(); - bottomLayout->addWidget( back ); - bottomLayout->addWidget( next ); + + // Create buttons and sets an initial icon; the icons may change + { + auto* back = new QPushButton( getButtonIcon( QStringLiteral( "go-previous" ) ), tr( "&Back" ), navigation ); + back->setObjectName( "view-button-back" ); + connect( back, &QPushButton::clicked, m_viewManager, &Calamares::ViewManager::back ); + connect( m_viewManager, &Calamares::ViewManager::backEnabledChanged, back, &QPushButton::setEnabled ); + connect( m_viewManager, &Calamares::ViewManager::backLabelChanged, back, &QPushButton::setText ); + connect( m_viewManager, &Calamares::ViewManager::backIconChanged, this, [=]( QString n ) { + setButtonIcon( back, n ); + } ); + bottomLayout->addWidget( back ); + } + { + auto* next = new QPushButton( getButtonIcon( QStringLiteral( "go-next" ) ), tr( "&Next" ), navigation ); + next->setObjectName( "view-button-next" ); + connect( next, &QPushButton::clicked, m_viewManager, &Calamares::ViewManager::next ); + connect( m_viewManager, &Calamares::ViewManager::nextEnabledChanged, next, &QPushButton::setEnabled ); + connect( m_viewManager, &Calamares::ViewManager::nextLabelChanged, next, &QPushButton::setText ); + connect( m_viewManager, &Calamares::ViewManager::nextIconChanged, this, [=]( QString n ) { + setButtonIcon( next, n ); + } ); + bottomLayout->addWidget( next ); + } bottomLayout->addSpacing( 12 ); - bottomLayout->addWidget( quit ); + { + auto* quit = new QPushButton( getButtonIcon( QStringLiteral( "dialog-cancel" ) ), tr( "&Cancel" ), navigation ); + quit->setObjectName( "view-button-cancel" ); + connect( quit, &QPushButton::clicked, m_viewManager, &Calamares::ViewManager::quit ); + bottomLayout->addWidget( quit ); + } navigation->setLayout( bottomLayout ); return navigation; diff --git a/src/libcalamaresui/ViewManager.cpp b/src/libcalamaresui/ViewManager.cpp index 74a1b6bee..a73580fa9 100644 --- a/src/libcalamaresui/ViewManager.cpp +++ b/src/libcalamaresui/ViewManager.cpp @@ -38,6 +38,12 @@ #include #include +#define UPDATE_BUTTON_PROPERTY( name, value ) \ + { \ + m_##name = value; \ + emit name##Changed( m_##name ); \ + } + namespace Calamares { @@ -89,8 +95,6 @@ ViewManager::ViewManager( QObject* parent ) mainLayout->addWidget( m_stack ); // Create buttons and sets an initial icon; the icons may change - m_back = new QPushButton( getButtonIcon( QStringLiteral( "go-previous" ) ), tr( "&Back" ), m_widget ); - m_back->setObjectName( "view-button-back" ); m_quit = new QPushButton( getButtonIcon( QStringLiteral( "dialog-cancel" ) ), tr( "&Cancel" ), m_widget ); m_quit->setObjectName( "view-button-cancel" ); @@ -99,13 +103,9 @@ ViewManager::ViewManager( QObject* parent ) QBoxLayout* bottomLayout = new QHBoxLayout; mainLayout->addLayout( bottomLayout ); bottomLayout->addStretch(); - bottomLayout->addWidget( m_back ); - bottomLayout->addSpacing( 12 ); bottomLayout->addWidget( m_quit ); - connect( m_back, &QPushButton::clicked, this, &ViewManager::back ); connect( m_quit, &QPushButton::clicked, this, &ViewManager::quit ); - m_back->setEnabled( false ); connect( JobQueue::instance(), &JobQueue::failed, this, &ViewManager::onInstallationFailed ); connect( JobQueue::instance(), &JobQueue::finished, this, &ViewManager::next ); @@ -363,9 +363,8 @@ ViewManager::next() { // Reached the end in a weird state (e.g. no finished step after an exec) executing = false; - m_nextEnabled = false; - emit nextEnabledChanged( m_nextEnabled ); - m_back->setEnabled( false ); + UPDATE_BUTTON_PROPERTY( nextEnabled, false ) + UPDATE_BUTTON_PROPERTY( backEnabled, false ) } updateCancelEnabled( !settings->disableCancel() && !( executing && settings->disableCancelDuringExec() ) ); } @@ -376,9 +375,8 @@ ViewManager::next() if ( m_currentStep < m_steps.count() ) { - m_nextEnabled = !executing && m_steps.at( m_currentStep )->isNextEnabled(); - emit nextEnabledChanged( m_nextEnabled ); - m_back->setEnabled( !executing && m_steps.at( m_currentStep )->isBackEnabled() ); + UPDATE_BUTTON_PROPERTY( nextEnabled, !executing && m_steps.at( m_currentStep )->isNextEnabled() ) + UPDATE_BUTTON_PROPERTY( backEnabled, !executing && m_steps.at( m_currentStep )->isBackEnabled() ) } updateButtonLabels(); @@ -400,21 +398,17 @@ ViewManager::updateButtonLabels() // If we're going into the execution step / install phase, other message if ( stepIsExecute( m_steps, m_currentStep + 1 ) ) { - m_nextLabel = nextIsInstallationStep; - m_nextIcon = "run-install"; - emit nextLabelChanged( m_nextLabel ); - emit nextIconChanged( m_nextIcon ); + UPDATE_BUTTON_PROPERTY( nextLabel, nextIsInstallationStep ) + UPDATE_BUTTON_PROPERTY( nextIcon, "run-install" ) } else { - m_nextLabel = tr( "&Next" ); - m_nextIcon = "go-next"; - emit nextLabelChanged( m_nextLabel ); - emit nextIconChanged( m_nextIcon ); + UPDATE_BUTTON_PROPERTY( nextLabel, tr( "&Next" ) ) + UPDATE_BUTTON_PROPERTY( nextIcon, "go-next" ) } // Going back is always simple - m_back->setText( tr( "&Back" ) ); + UPDATE_BUTTON_PROPERTY( backLabel, tr( "&Back" ) ) // Cancel button changes label at the end if ( isAtVeryEnd( m_steps, m_currentStep ) ) @@ -465,14 +459,11 @@ ViewManager::back() return; } - m_nextEnabled = m_steps.at( m_currentStep )->isNextEnabled(); - emit nextEnabledChanged( m_nextEnabled ); - m_back->setEnabled( m_steps.at( m_currentStep )->isBackEnabled() ); - - if ( m_currentStep == 0 && m_steps.first()->isAtBeginning() ) - { - m_back->setEnabled( false ); - } + UPDATE_BUTTON_PROPERTY( nextEnabled, m_steps.at( m_currentStep )->isNextEnabled() ) + UPDATE_BUTTON_PROPERTY( backEnabled, + ( m_currentStep == 0 && m_steps.first()->isAtBeginning() ) + ? false + : m_steps.at( m_currentStep )->isBackEnabled() ) updateButtonLabels(); } diff --git a/src/libcalamaresui/ViewManager.h b/src/libcalamaresui/ViewManager.h index 4e2d5e9e0..00a7509bb 100644 --- a/src/libcalamaresui/ViewManager.h +++ b/src/libcalamaresui/ViewManager.h @@ -38,10 +38,15 @@ class UIDLLEXPORT ViewManager : public QAbstractListModel { Q_OBJECT Q_PROPERTY( int currentStepIndex READ currentStepIndex NOTIFY currentStepChanged FINAL ) + Q_PROPERTY( bool nextEnabled READ nextEnabled NOTIFY nextEnabledChanged FINAL ) Q_PROPERTY( QString nextLabel READ nextLabel NOTIFY nextLabelChanged FINAL ) Q_PROPERTY( QString nextIcon READ nextIcon NOTIFY nextIconChanged FINAL ) + Q_PROPERTY( bool backEnabled READ backEnabled NOTIFY backEnabledChanged FINAL ) + Q_PROPERTY( QString backLabel READ backLabel NOTIFY backLabelChanged FINAL ) + Q_PROPERTY( QString backIcon READ backIcon NOTIFY backIconChanged FINAL ) + public: /** * @brief instance access to the ViewManager singleton. @@ -121,6 +126,18 @@ public Q_SLOTS: * have any pages before the current one. */ void back(); + bool backEnabled() const + { + return m_backEnabled; ///< Is the back-button to be enabled + } + QString backLabel() const + { + return m_backLabel; ///< What should be displayed on the back-button + } + QString backIcon() const + { + return m_backIcon; ///< Name of the icon to show + } /** * @brief Probably quit @@ -152,6 +169,10 @@ signals: void nextLabelChanged( QString ) const; void nextIconChanged( QString ) const; + void backEnabledChanged( bool ) const; + void backLabelChanged( QString ) const; + void backIconChanged( QString ) const; + private: explicit ViewManager( QObject* parent = nullptr ); virtual ~ViewManager() override; @@ -167,13 +188,16 @@ private: QWidget* m_widget; QStackedWidget* m_stack; - QPushButton* m_back; QPushButton* m_quit; bool m_nextEnabled = false; QString m_nextLabel; QString m_nextIcon; ///< Name of icon to show on button + bool m_backEnabled = false; + QString m_backLabel; + QString m_backIcon; + public: /** @section Model *