calamares/src/modules/partition/core/DeviceModel.cpp

164 lines
4.1 KiB
C++
Raw Normal View History

/* === This file is part of Calamares - <https://github.com/calamares> ===
2014-06-27 17:25:39 +02:00
*
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
2019-04-01 14:48:25 +02:00
* Copyright 2019, Adriaan de Groot <groot@kde.org>
2014-06-27 17:25:39 +02:00
*
* 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 "core/DeviceModel.h"
#include "core/PartitionModel.h"
2014-06-27 17:25:39 +02:00
#include "utils/CalamaresUtilsGui.h"
2015-12-17 18:42:32 +01:00
#include "utils/Logger.h"
// KPMcore
#include <kpmcore/core/device.h>
2014-06-27 17:25:39 +02:00
// KF5
#include <KFormat>
#include <QIcon>
#include <QStandardItemModel>
// STL
#include <algorithm>
static void
sortDevices( DeviceModel::DeviceList& l )
{
std::sort( l.begin(), l.end(), []( const Device* dev1, const Device* dev2 ) {
return dev1->deviceNode() < dev2->deviceNode();
} );
}
2014-06-27 17:25:39 +02:00
DeviceModel::DeviceModel( QObject* parent )
: QAbstractListModel( parent )
{
}
DeviceModel::~DeviceModel() {}
2014-06-27 17:25:39 +02:00
void
2019-04-01 14:48:25 +02:00
DeviceModel::init( const DeviceList& devices )
2014-06-27 17:25:39 +02:00
{
beginResetModel();
m_devices = devices;
sortDevices( m_devices );
2014-06-27 17:25:39 +02:00
endResetModel();
}
int
DeviceModel::rowCount( const QModelIndex& parent ) const
{
return parent.isValid() ? 0 : m_devices.count();
}
QVariant
DeviceModel::data( const QModelIndex& index, int role ) const
{
int row = index.row();
if ( row < 0 || row >= m_devices.count() )
{
2014-06-27 17:25:39 +02:00
return QVariant();
}
2014-06-27 17:25:39 +02:00
Device* device = m_devices.at( row );
2014-06-27 17:25:39 +02:00
switch ( role )
{
case Qt::DisplayRole:
case Qt::ToolTipRole:
2014-06-27 17:25:39 +02:00
if ( device->name().isEmpty() )
{
2014-06-27 17:25:39 +02:00
return device->deviceNode();
}
2014-06-27 17:25:39 +02:00
else
{
if ( device->logicalSize() >= 0 && device->totalLogical() >= 0 )
2019-04-11 16:28:35 +02:00
{
//: device[name] - size[number] (device-node[name])
return tr( "%1 - %2 (%3)" )
.arg( device->name() )
.arg( KFormat().formatByteSize( device->capacity() ) )
.arg( device->deviceNode() );
2019-04-11 16:28:35 +02:00
}
else
2019-04-11 16:28:35 +02:00
{
// Newly LVM VGs don't have capacity property yet (i.e.
// always has 1B capacity), so don't show it for a while.
//
//: device[name] - (device-node[name])
return tr( "%1 - (%2)" ).arg( device->name() ).arg( device->deviceNode() );
2019-04-11 16:28:35 +02:00
}
}
case Qt::DecorationRole:
return CalamaresUtils::defaultPixmap(
CalamaresUtils::PartitionDisk,
CalamaresUtils::Original,
QSize( CalamaresUtils::defaultIconSize().width() * 3, CalamaresUtils::defaultIconSize().height() * 3 ) );
2014-06-27 17:25:39 +02:00
default:
return QVariant();
}
}
2015-12-17 17:58:13 +01:00
Device*
DeviceModel::deviceForIndex( const QModelIndex& index ) const
2014-06-27 17:25:39 +02:00
{
int row = index.row();
if ( row < 0 || row >= m_devices.count() )
{
2014-06-27 17:25:39 +02:00
return nullptr;
}
return m_devices.at( row );
2014-06-27 17:25:39 +02:00
}
2015-12-17 17:58:13 +01:00
void
DeviceModel::swapDevice( Device* oldDevice, Device* newDevice )
{
Q_ASSERT( oldDevice );
Q_ASSERT( newDevice );
int indexOfOldDevice = m_devices.indexOf( oldDevice );
if ( indexOfOldDevice < 0 )
{
2015-12-17 17:58:13 +01:00
return;
}
2015-12-17 17:58:13 +01:00
m_devices[ indexOfOldDevice ] = newDevice;
emit dataChanged( index( indexOfOldDevice ), index( indexOfOldDevice ) );
}
void
DeviceModel::addDevice( Device* device )
{
beginResetModel();
m_devices << device;
sortDevices( m_devices );
endResetModel();
}
void
DeviceModel::removeDevice( Device* device )
{
beginResetModel();
m_devices.removeAll( device );
sortDevices( m_devices );
endResetModel();
}