[partition] Move BootLoaderModel convenience functions

- These were hidden inside PartitionPage, but are useful elsewhere.
This commit is contained in:
Adriaan de Groot 2019-05-28 17:01:58 +02:00
parent 8d451622db
commit 0ebabfafd4
3 changed files with 67 additions and 36 deletions

View file

@ -23,9 +23,13 @@
#include "core/PartitionInfo.h"
#include "core/KPMHelpers.h"
#include "utils/Logger.h"
// KPMcore
#include <kpmcore/core/device.h>
#include <QComboBox>
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

View file

@ -25,6 +25,7 @@
#include <QStandardItemModel>
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 */

View file

@ -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() );
}