[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