2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2014-06-27 17:25:39 +02:00
|
|
|
*
|
2020-08-22 01:19:58 +02:00
|
|
|
* SPDX-FileCopyrightText: 2014 Aurélien Gâteau <agateau@kde.org>
|
|
|
|
* SPDX-FileCopyrightText: 2014 Teo Mrnjavac <teo@kde.org>
|
|
|
|
* SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2014-06-27 17:25:39 +02:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2014-06-27 17:25:39 +02:00
|
|
|
*
|
|
|
|
*/
|
2015-07-02 13:49:21 +02:00
|
|
|
#include "core/DeviceModel.h"
|
2014-08-08 13:25:56 +02:00
|
|
|
|
2015-07-02 13:49:21 +02:00
|
|
|
#include "core/PartitionModel.h"
|
2014-06-27 17:25:39 +02:00
|
|
|
|
2014-09-04 19:35:03 +02:00
|
|
|
#include "utils/CalamaresUtilsGui.h"
|
2015-12-17 18:42:32 +01:00
|
|
|
#include "utils/Logger.h"
|
2014-09-04 19:35:03 +02:00
|
|
|
|
2015-07-02 13:49:21 +02:00
|
|
|
// KPMcore
|
|
|
|
#include <kpmcore/core/device.h>
|
2014-06-27 17:25:39 +02:00
|
|
|
|
2014-07-21 16:15:53 +02:00
|
|
|
// KF5
|
|
|
|
#include <KFormat>
|
|
|
|
|
2014-09-04 19:35:03 +02:00
|
|
|
#include <QIcon>
|
2020-02-14 11:15:57 +01:00
|
|
|
#include <QStandardItemModel>
|
2014-09-04 19:35:03 +02:00
|
|
|
|
2014-07-24 19:24:40 +02:00
|
|
|
// STL
|
|
|
|
#include <algorithm>
|
|
|
|
|
2019-04-01 14:53:35 +02:00
|
|
|
static void
|
|
|
|
sortDevices( DeviceModel::DeviceList& l )
|
|
|
|
{
|
2020-02-14 11:15:57 +01:00
|
|
|
std::sort( l.begin(), l.end(), []( const Device* dev1, const Device* dev2 ) {
|
2019-04-01 14:53:35 +02:00
|
|
|
return dev1->deviceNode() < dev2->deviceNode();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2014-06-27 17:25:39 +02:00
|
|
|
DeviceModel::DeviceModel( QObject* parent )
|
|
|
|
: QAbstractListModel( parent )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-14 11:15:57 +01:00
|
|
|
DeviceModel::~DeviceModel() {}
|
2014-06-27 18:42:15 +02:00
|
|
|
|
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();
|
2014-06-30 14:12:23 +02:00
|
|
|
m_devices = devices;
|
2019-04-01 14:53:35 +02:00
|
|
|
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() )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2014-06-27 17:25:39 +02:00
|
|
|
return QVariant();
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2014-06-27 17:25:39 +02:00
|
|
|
|
2014-06-30 14:12:23 +02:00
|
|
|
Device* device = m_devices.at( row );
|
2014-06-27 17:25:39 +02:00
|
|
|
|
|
|
|
switch ( role )
|
|
|
|
{
|
|
|
|
case Qt::DisplayRole:
|
2014-09-04 19:35:03 +02:00
|
|
|
case Qt::ToolTipRole:
|
2014-06-27 17:25:39 +02:00
|
|
|
if ( device->name().isEmpty() )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2014-06-27 17:25:39 +02:00
|
|
|
return device->deviceNode();
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2014-06-27 17:25:39 +02:00
|
|
|
else
|
2018-06-07 22:49:25 +02:00
|
|
|
{
|
2018-06-08 23:52:53 +02:00
|
|
|
if ( device->logicalSize() >= 0 && device->totalLogical() >= 0 )
|
2019-04-11 16:28:35 +02:00
|
|
|
{
|
|
|
|
//: device[name] - size[number] (device-node[name])
|
2018-06-07 22:49:25 +02:00
|
|
|
return tr( "%1 - %2 (%3)" )
|
2020-02-14 11:15:57 +01:00
|
|
|
.arg( device->name() )
|
|
|
|
.arg( KFormat().formatByteSize( device->capacity() ) )
|
|
|
|
.arg( device->deviceNode() );
|
2019-04-11 16:28:35 +02:00
|
|
|
}
|
2018-06-07 22:49:25 +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])
|
2020-02-14 11:15:57 +01:00
|
|
|
return tr( "%1 - (%2)" ).arg( device->name() ).arg( device->deviceNode() );
|
2019-04-11 16:28:35 +02:00
|
|
|
}
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2014-09-04 19:35:03 +02:00
|
|
|
case Qt::DecorationRole:
|
2020-02-14 11:15:57 +01:00
|
|
|
return CalamaresUtils::defaultPixmap(
|
|
|
|
CalamaresUtils::PartitionDisk,
|
|
|
|
CalamaresUtils::Original,
|
2021-06-08 14:14:11 +02:00
|
|
|
QSize( CalamaresUtils::defaultIconSize().width() * 2, CalamaresUtils::defaultIconSize().height() * 2 ) );
|
2014-06-27 17:25:39 +02:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 17:58:13 +01:00
|
|
|
|
2014-06-30 14:12:23 +02: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() )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2014-06-27 17:25:39 +02:00
|
|
|
return nullptr;
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2014-06-30 14:12:23 +02:00
|
|
|
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 )
|
2020-02-14 11:15:57 +01:00
|
|
|
{
|
2015-12-17 17:58:13 +01:00
|
|
|
return;
|
2020-02-14 11:15:57 +01:00
|
|
|
}
|
2015-12-17 17:58:13 +01:00
|
|
|
|
|
|
|
m_devices[ indexOfOldDevice ] = newDevice;
|
|
|
|
|
|
|
|
emit dataChanged( index( indexOfOldDevice ), index( indexOfOldDevice ) );
|
|
|
|
}
|
2018-06-07 22:22:22 +02:00
|
|
|
|
|
|
|
void
|
2020-02-14 11:15:57 +01:00
|
|
|
DeviceModel::addDevice( Device* device )
|
2018-06-07 22:22:22 +02:00
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
m_devices << device;
|
2019-04-01 14:53:35 +02:00
|
|
|
sortDevices( m_devices );
|
2018-06-07 22:22:22 +02:00
|
|
|
endResetModel();
|
|
|
|
}
|
2018-06-09 01:20:05 +02:00
|
|
|
|
|
|
|
void
|
2020-02-14 11:15:57 +01:00
|
|
|
DeviceModel::removeDevice( Device* device )
|
2018-06-09 01:20:05 +02:00
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
m_devices.removeAll( device );
|
2019-04-01 14:53:35 +02:00
|
|
|
sortDevices( m_devices );
|
2018-06-09 01:20:05 +02:00
|
|
|
endResetModel();
|
|
|
|
}
|