calamares/src/modules/partition/PartitionModel.cpp

115 lines
2.9 KiB
C++
Raw Normal View History

2014-06-27 17:25:39 +02:00
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <PartitionModel.h>
2014-06-30 15:03:29 +02:00
#include <PMUtils.h>
2014-06-27 17:25:39 +02:00
// CalaPM
#include <core/device.h>
#include <core/partition.h>
#include <core/partitiontable.h>
#include <fs/filesystem.h>
2014-07-02 17:25:59 +02:00
// KF5
#include <KFormat>
2014-06-27 17:25:39 +02:00
PartitionModel::PartitionModel( QObject* parent )
: QAbstractListModel( parent )
{
}
void
PartitionModel::init( Device* device )
{
m_device = device;
reload();
}
void
PartitionModel::reload()
{
beginResetModel();
2014-06-27 17:25:39 +02:00
m_partitionList.clear();
if ( m_device )
2014-06-27 17:25:39 +02:00
{
fillPartitionList( m_device->partitionTable() );
}
endResetModel();
}
int
PartitionModel::rowCount( const QModelIndex& parent ) const
{
return parent.isValid() ? 0 : m_partitionList.count();
}
QVariant
PartitionModel::data( const QModelIndex& index, int role ) const
{
int row = index.row();
if ( row < 0 || row >= m_partitionList.count() )
{
return QVariant();
}
Partition* partition = m_partitionList.at( row );
switch ( role )
{
case Qt::DisplayRole:
{
QString text = partition->roles().has( PartitionRole::Logical )
? QStringLiteral( " " ) : QStringLiteral();
2014-06-30 15:03:29 +02:00
if ( PMUtils::isPartitionFreeSpace( partition ) )
2014-06-27 17:25:39 +02:00
{
text += tr( "Free Space" );
}
else
{
text += partition->partitionPath() + " " + partition->fileSystem().name() + " " + partition->mountPoint();
2014-06-27 17:25:39 +02:00
}
qint64 size = ( partition->lastSector() - partition->firstSector() + 1 ) * m_device->logicalSectorSize();
2014-07-02 17:25:59 +02:00
text += tr( " (%1)" ).arg( KFormat().formatByteSize( size ) );
return text;
}
2014-06-27 17:25:39 +02:00
default:
return QVariant();
}
}
void
PartitionModel::fillPartitionList( PartitionNode* parent )
{
for ( auto partition : parent->children() )
{
m_partitionList << partition;
fillPartitionList( partition );
}
}
Partition*
PartitionModel::partitionForIndex( const QModelIndex& index ) const
{
int row = index.row();
if ( row < 0 || row >= m_partitionList.count() )
{
return nullptr;
}
return m_partitionList.at( row );
}