From 0ebabfafd4fe37efbc5ae76eebc548f838e1a433 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 28 May 2019 17:01:58 +0200 Subject: [PATCH] [partition] Move BootLoaderModel convenience functions - These were hidden inside PartitionPage, but are useful elsewhere. --- .../partition/core/BootLoaderModel.cpp | 49 +++++++++++++++++++ src/modules/partition/core/BootLoaderModel.h | 17 +++++++ src/modules/partition/gui/PartitionPage.cpp | 37 +------------- 3 files changed, 67 insertions(+), 36 deletions(-) diff --git a/src/modules/partition/core/BootLoaderModel.cpp b/src/modules/partition/core/BootLoaderModel.cpp index 16c6ce3c8..eeec12b78 100644 --- a/src/modules/partition/core/BootLoaderModel.cpp +++ b/src/modules/partition/core/BootLoaderModel.cpp @@ -23,9 +23,13 @@ #include "core/PartitionInfo.h" #include "core/KPMHelpers.h" +#include "utils/Logger.h" + // KPMcore #include +#include + static QStandardItem* createBootLoaderItem( const QString& description, const QString& path, bool isPartition ) { @@ -148,3 +152,48 @@ BootLoaderModel::data( const QModelIndex& index, int role ) const } return QStandardItemModel::data( index, role ); } + +namespace Calamares +{ +int +findBootloader( const QAbstractItemModel* model, const QString& path ) +{ + for ( int i = 0; i < model->rowCount(); ++i) + { + const auto index = model->index( i, 0, QModelIndex() ); + if ( !index.isValid() ) + continue; + QVariant var = model->data( index, BootLoaderModel::BootLoaderPathRole ); + if ( var.isValid() && var.toString() == path ) + return i; + } + + return -1; +} + +void +restoreSelectedBootLoader( QComboBox& combo, const QString& path ) +{ + const auto* model = combo.model(); + if ( model->rowCount() < 1 ) + { + cDebug() << "No items in BootLoaderModel"; + return; + } + + int r = -1; + if ( path.isEmpty() ) + { + combo.setCurrentIndex( 0 ); + } + else if ( (r = findBootloader( model, path )) >= 0 ) + { + combo.setCurrentIndex( r ); + } + else + { + combo.setCurrentIndex( 0 ); + } +} + +} // namespace diff --git a/src/modules/partition/core/BootLoaderModel.h b/src/modules/partition/core/BootLoaderModel.h index fbbb9deb2..a2befad00 100644 --- a/src/modules/partition/core/BootLoaderModel.h +++ b/src/modules/partition/core/BootLoaderModel.h @@ -25,6 +25,7 @@ #include class Device; +class QComboBox; /** * This model contains one entry for each device MBR plus one entry for the @@ -63,4 +64,20 @@ private: void updateInternal(); }; +namespace Calamares +{ + /** @brief Returns the row number of boot-loader @p path (e.g. /dev/sda) + * + * Assuming the @p model is a BootLoaderModel, will return a row number + * in the model. Returns -1 otherwise. + */ + int findBootloader( const QAbstractItemModel* model, const QString& path ); + + /** @brief Tries to set @p path as selected item in @p combo + * + * Matches a boot-loader install path (e.g. /dev/sda) with a model + * row and sets that as the current row. + */ + void restoreSelectedBootLoader( QComboBox& combo, const QString& path ); +} // namespace #endif /* BOOTLOADERMODEL_H */ diff --git a/src/modules/partition/gui/PartitionPage.cpp b/src/modules/partition/gui/PartitionPage.cpp index 647a69d5d..3c2ffbdd0 100644 --- a/src/modules/partition/gui/PartitionPage.cpp +++ b/src/modules/partition/gui/PartitionPage.cpp @@ -511,45 +511,10 @@ PartitionPage::updateSelectedBootLoaderIndex() cDebug() << "Selected bootloader index" << m_lastSelectedBootLoaderIndex; } -int -findBootloader( const QAbstractItemModel* model, const QString& path ) -{ - for ( int i = 0; i < model->rowCount(); ++i) - { - const auto index = model->index( i, 0, QModelIndex() ); - if ( !index.isValid() ) - continue; - QVariant var = model->data( index, BootLoaderModel::BootLoaderPathRole ); - if ( var.isValid() && var.toString() == path ) - return i; - } - - return -1; -} - void PartitionPage::restoreSelectedBootLoader() { - const auto* model = m_ui->bootLoaderComboBox->model(); - if ( model->rowCount() < 1 ) - { - cDebug() << "No items in BootLoaderModel"; - return; - } - - int r = -1; - if ( m_core->bootLoaderInstallPath().isEmpty() ) - { - m_ui->bootLoaderComboBox->setCurrentIndex( 0 ); - } - else if ( (r = findBootloader( model, m_core->bootLoaderInstallPath() )) >= 0 ) - { - m_ui->bootLoaderComboBox->setCurrentIndex( r ); - } - else - { - m_ui->bootLoaderComboBox->setCurrentIndex( 0 ); - } + Calamares::restoreSelectedBootLoader( *(m_ui->bootLoaderComboBox), m_core->bootLoaderInstallPath() ); }