From 8f0a6d30656fa40232071826f4d63cba201f62be Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 11 Mar 2020 04:49:38 +0100 Subject: [PATCH] [calamares] The ViewManager is its own model - Having a ProgressTreeModel that does nothing but proxy to ViewManager methods is kind of useless. - Move the relevant code from ProgressTreeModel to ViewManager. - Remove now-unused ProgressTreeModel. --- src/calamares/CMakeLists.txt | 1 - src/calamares/CalamaresApplication.cpp | 1 - src/calamares/CalamaresWindow.cpp | 3 +- .../progresstree/ProgressTreeDelegate.cpp | 5 +- .../progresstree/ProgressTreeModel.cpp | 119 ------------------ .../progresstree/ProgressTreeModel.h | 47 ------- src/libcalamaresui/ViewManager.cpp | 83 +++++++++++- src/libcalamaresui/ViewManager.h | 20 ++- 8 files changed, 104 insertions(+), 175 deletions(-) delete mode 100644 src/calamares/progresstree/ProgressTreeModel.cpp delete mode 100644 src/calamares/progresstree/ProgressTreeModel.h diff --git a/src/calamares/CMakeLists.txt b/src/calamares/CMakeLists.txt index 5ae4449a7..78245f614 100644 --- a/src/calamares/CMakeLists.txt +++ b/src/calamares/CMakeLists.txt @@ -7,7 +7,6 @@ set( calamaresSources VariantModel.cpp progresstree/ProgressTreeDelegate.cpp - progresstree/ProgressTreeModel.cpp progresstree/ProgressTreeView.cpp ) diff --git a/src/calamares/CalamaresApplication.cpp b/src/calamares/CalamaresApplication.cpp index 576a7c182..2d36f5a49 100644 --- a/src/calamares/CalamaresApplication.cpp +++ b/src/calamares/CalamaresApplication.cpp @@ -21,7 +21,6 @@ #include "CalamaresConfig.h" #include "CalamaresVersion.h" #include "CalamaresWindow.h" -#include "progresstree/ProgressTreeModel.h" #include "progresstree/ProgressTreeView.h" #include "Branding.h" diff --git a/src/calamares/CalamaresWindow.cpp b/src/calamares/CalamaresWindow.cpp index 33e3ff3c5..2e2c776f3 100644 --- a/src/calamares/CalamaresWindow.cpp +++ b/src/calamares/CalamaresWindow.cpp @@ -25,7 +25,6 @@ #include "DebugWindow.h" #include "Settings.h" #include "ViewManager.h" -#include "progresstree/ProgressTreeModel.h" #include "progresstree/ProgressTreeView.h" #include "utils/CalamaresUtilsGui.h" #include "utils/Logger.h" @@ -94,7 +93,7 @@ CalamaresWindow::getWidgetSidebar( int desiredWidth ) logoLayout->addStretch(); ProgressTreeView* tv = new ProgressTreeView( sideBox ); - tv->setModel( new ProgressTreeModel ); + tv->setModel( Calamares::ViewManager::instance() ); tv->setFocusPolicy( Qt::NoFocus ); sideLayout->addWidget( tv ); diff --git a/src/calamares/progresstree/ProgressTreeDelegate.cpp b/src/calamares/progresstree/ProgressTreeDelegate.cpp index 9db508c6e..e7041d1b9 100644 --- a/src/calamares/progresstree/ProgressTreeDelegate.cpp +++ b/src/calamares/progresstree/ProgressTreeDelegate.cpp @@ -18,11 +18,10 @@ */ #include "ProgressTreeDelegate.h" -#include "ProgressTreeModel.h" #include "CalamaresApplication.h" #include "CalamaresWindow.h" - +#include "ViewManager.h" #include "Branding.h" #include "utils/CalamaresUtilsGui.h" @@ -87,7 +86,7 @@ ProgressTreeDelegate::paintViewStep( QPainter* painter, painter->setFont( font ); bool isCurrent = false; - isCurrent = index.data( ProgressTreeModel::ProgressTreeItemCurrentRole ).toBool(); + isCurrent = index.data( Calamares::ViewManager::ProgressTreeItemCurrentRole ).toBool(); if ( isCurrent ) { diff --git a/src/calamares/progresstree/ProgressTreeModel.cpp b/src/calamares/progresstree/ProgressTreeModel.cpp deleted file mode 100644 index d136cbef7..000000000 --- a/src/calamares/progresstree/ProgressTreeModel.cpp +++ /dev/null @@ -1,119 +0,0 @@ -/* === This file is part of Calamares - === - * - * Copyright 2014-2015, Teo Mrnjavac - * Copyright 2017, Adriaan de Groot - * - * Calamares is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Calamares is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Calamares. If not, see . - */ - -#include "ProgressTreeModel.h" - -#include "Settings.h" -#include "ViewManager.h" - -ProgressTreeModel::ProgressTreeModel( QObject* parent ) - : QAbstractListModel( parent ) -{ -} - -ProgressTreeModel::~ProgressTreeModel() {} - -QVariant -ProgressTreeModel::data( const QModelIndex& index, int role ) const -{ - if ( !index.isValid() ) - { - return QVariant(); - } - - const Calamares::ViewManager* vm = Calamares::ViewManager::instance(); - if ( !vm ) - { - return QVariant(); - } - - const auto steps = vm->viewSteps(); - if ( ( index.row() < 0 ) || ( index.row() >= steps.length() ) ) - { - return QVariant(); - } - - const auto* step = steps.at( index.row() ); - if ( !step ) - { - return QVariant(); - } - - switch ( role ) - { - case Qt::DisplayRole: - return step->prettyName(); - case Qt::ToolTipRole: - if ( Calamares::Settings::instance()->debugMode() ) - { - auto key = step->moduleInstanceKey(); - QString toolTip( "Debug information" ); - toolTip.append( "
Type:\tViewStep" ); - toolTip.append( QString( "
Pretty:\t%1" ).arg( step->prettyName() ) ); - toolTip.append( QString( "
Status:\t%1" ).arg( step->prettyStatus() ) ); - toolTip.append( - QString( "
Source:\t%1" ).arg( key.isValid() ? key.toString() : QStringLiteral( "built-in" ) ) ); - return toolTip; - } - else - { - return QVariant(); - } - case ProgressTreeItemCurrentRole: - return vm->currentStep() == step; - case ProgressTreeItemCompletedRole: - // Every step *before* the current step is considered "complete" - for ( const auto* otherstep : steps ) - { - if ( otherstep == vm->currentStep() ) - { - break; - } - if ( otherstep == step ) - { - return true; - } - } - // .. and the others (including current) are not. - return false; - default: - return QVariant(); - } -} - - -int -ProgressTreeModel::rowCount( const QModelIndex& parent ) const -{ - if ( parent.column() > 0 ) - { - return 0; - } - const Calamares::ViewManager* vm = Calamares::ViewManager::instance(); - return vm ? vm->viewSteps().length() : 0; -} - -QHash< int, QByteArray > -ProgressTreeModel::roleNames() const -{ - auto h = QAbstractListModel::roleNames(); - h.insert( ProgressTreeItemCurrentRole, "current" ); - h.insert( ProgressTreeItemCompletedRole, "completed" ); - return h; -} diff --git a/src/calamares/progresstree/ProgressTreeModel.h b/src/calamares/progresstree/ProgressTreeModel.h deleted file mode 100644 index 3553b97d9..000000000 --- a/src/calamares/progresstree/ProgressTreeModel.h +++ /dev/null @@ -1,47 +0,0 @@ -/* === This file is part of Calamares - === - * - * Copyright 2014, Teo Mrnjavac - * Copyright 2017, Adriaan de Groot - * - * Calamares is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Calamares is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Calamares. If not, see . - */ - -#ifndef PROGRESSTREEMODEL_H -#define PROGRESSTREEMODEL_H - -#include - -/** - * @brief The ProgressTreeModel class implements a model for the ProgressTreeView. - */ -class ProgressTreeModel : public QAbstractListModel -{ - Q_OBJECT -public: - enum Role - { - ProgressTreeItemCurrentRole = Qt::UserRole + 11, ///< Is this the *current* step? - ProgressTreeItemCompletedRole = Qt::UserRole + 12 ///< Are we past this one? - }; - - explicit ProgressTreeModel( QObject* parent = nullptr ); - virtual ~ProgressTreeModel() override; - - QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const override; - int rowCount( const QModelIndex& parent = QModelIndex() ) const override; - - QHash< int, QByteArray > roleNames() const override; -}; - -#endif // PROGRESSTREEMODEL_H diff --git a/src/libcalamaresui/ViewManager.cpp b/src/libcalamaresui/ViewManager.cpp index 7492f1f8b..f9fd48dc5 100644 --- a/src/libcalamaresui/ViewManager.cpp +++ b/src/libcalamaresui/ViewManager.cpp @@ -75,7 +75,7 @@ setButtonIcon( QPushButton* button, const QString& name ) } ViewManager::ViewManager( QObject* parent ) - : QObject( parent ) + : QAbstractListModel( parent ) , m_currentStep( 0 ) , m_widget( new QWidget() ) { @@ -507,4 +507,85 @@ ViewManager::updateCancelEnabled( bool enabled ) emit cancelEnabled( enabled ); } +QVariant +ViewManager::data( const QModelIndex& index, int role ) const +{ + if ( !index.isValid() ) + { + return QVariant(); + } + + if ( ( index.row() < 0 ) || ( index.row() >= m_steps.length() ) ) + { + return QVariant(); + } + + const auto* step = m_steps.at( index.row() ); + if ( !step ) + { + return QVariant(); + } + + switch ( role ) + { + case Qt::DisplayRole: + return step->prettyName(); + case Qt::ToolTipRole: + if ( Calamares::Settings::instance()->debugMode() ) + { + auto key = step->moduleInstanceKey(); + QString toolTip( "Debug information" ); + toolTip.append( "
Type:\tViewStep" ); + toolTip.append( QString( "
Pretty:\t%1" ).arg( step->prettyName() ) ); + toolTip.append( QString( "
Status:\t%1" ).arg( step->prettyStatus() ) ); + toolTip.append( + QString( "
Source:\t%1" ).arg( key.isValid() ? key.toString() : QStringLiteral( "built-in" ) ) ); + return toolTip; + } + else + { + return QVariant(); + } + case ProgressTreeItemCurrentRole: + return currentStep() == step; + case ProgressTreeItemCompletedRole: + // Every step *before* the current step is considered "complete" + for ( const auto* otherstep : m_steps ) + { + if ( otherstep == currentStep() ) + { + break; + } + if ( otherstep == step ) + { + return true; + } + } + // .. and the others (including current) are not. + return false; + default: + return QVariant(); + } +} + + +int +ViewManager::rowCount( const QModelIndex& parent ) const +{ + if ( parent.column() > 0 ) + { + return 0; + } + return m_steps.length(); +} + +QHash< int, QByteArray > +ViewManager::roleNames() const +{ + auto h = QAbstractListModel::roleNames(); + h.insert( ProgressTreeItemCurrentRole, "current" ); + h.insert( ProgressTreeItemCompletedRole, "completed" ); + return h; +} + } // namespace Calamares diff --git a/src/libcalamaresui/ViewManager.h b/src/libcalamaresui/ViewManager.h index 42fb9ad8f..7ca6eb377 100644 --- a/src/libcalamaresui/ViewManager.h +++ b/src/libcalamaresui/ViewManager.h @@ -23,6 +23,7 @@ #include "DllMacro.h" #include "viewpages/ViewStep.h" +#include #include #include #include @@ -33,7 +34,7 @@ namespace Calamares * @brief The ViewManager class handles progression through view pages. * @note Singleton object, only use through ViewManager::instance(). */ -class UIDLLEXPORT ViewManager : public QObject +class UIDLLEXPORT ViewManager : public QAbstractListModel { Q_OBJECT public: @@ -147,6 +148,23 @@ private: QPushButton* m_back; QPushButton* m_next; QPushButton* m_quit; + +public: + /** @section Model + * + * These are the methods and enums used for the as-a-model part + * of the ViewManager. + */ + enum Role + { + ProgressTreeItemCurrentRole = Qt::UserRole + 11, ///< Is this the *current* step? + ProgressTreeItemCompletedRole = Qt::UserRole + 12 ///< Are we past this one? + }; + + QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const override; + int rowCount( const QModelIndex& parent = QModelIndex() ) const override; + + QHash< int, QByteArray > roleNames() const override; }; } // namespace Calamares