2017-12-20 14:39:09 +01:00
|
|
|
/* === 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>
|
2014-09-04 19:35:03 +02:00
|
|
|
* Copyright 2014, Teo Mrnjavac <teo@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/>.
|
|
|
|
*/
|
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>
|
|
|
|
|
2018-06-07 22:22:22 +02:00
|
|
|
#include <QStandardItemModel>
|
2014-09-04 19:35:03 +02:00
|
|
|
#include <QIcon>
|
|
|
|
|
2014-07-24 19:24:40 +02:00
|
|
|
// STL
|
|
|
|
#include <algorithm>
|
|
|
|
|
2014-06-27 17:25:39 +02:00
|
|
|
DeviceModel::DeviceModel( QObject* parent )
|
|
|
|
: QAbstractListModel( parent )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-27 18:42:15 +02:00
|
|
|
DeviceModel::~DeviceModel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-27 17:25:39 +02:00
|
|
|
void
|
|
|
|
DeviceModel::init( const QList< Device* >& devices )
|
|
|
|
{
|
|
|
|
beginResetModel();
|
2014-06-30 14:12:23 +02:00
|
|
|
m_devices = devices;
|
2014-07-24 19:24:40 +02:00
|
|
|
std::sort( m_devices.begin(), m_devices.end(), []( const Device* dev1, const Device* dev2 )
|
|
|
|
{
|
|
|
|
return dev1->deviceNode() < dev2->deviceNode();
|
|
|
|
} );
|
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() )
|
|
|
|
return QVariant();
|
|
|
|
|
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() )
|
|
|
|
return device->deviceNode();
|
|
|
|
else
|
2018-06-07 22:49:25 +02:00
|
|
|
{
|
2018-06-08 23:52:53 +02:00
|
|
|
if ( device->logicalSize() >= 0 && device->totalLogical() >= 0 )
|
2018-06-07 22:49:25 +02:00
|
|
|
return tr( "%1 - %2 (%3)" )
|
|
|
|
.arg( device->name() )
|
|
|
|
.arg( KFormat().formatByteSize( device->capacity() ) )
|
|
|
|
.arg( device->deviceNode() );
|
|
|
|
// Newly LVM VGs don't have capacity property yet (i.e. always has 1B capacity), so don't show it for a while
|
|
|
|
else
|
|
|
|
return tr( "%1 - (%2)" )
|
|
|
|
.arg( device->name() )
|
|
|
|
.arg( device->deviceNode() );
|
|
|
|
}
|
2014-09-04 19:35:03 +02:00
|
|
|
case Qt::DecorationRole:
|
2015-04-03 12:19:27 +02:00
|
|
|
return CalamaresUtils::defaultPixmap( CalamaresUtils::PartitionDisk,
|
2014-09-04 19:35:03 +02:00
|
|
|
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
|
|
|
|
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() )
|
|
|
|
return nullptr;
|
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 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_devices[ indexOfOldDevice ] = newDevice;
|
|
|
|
|
|
|
|
emit dataChanged( index( indexOfOldDevice ), index( indexOfOldDevice ) );
|
|
|
|
}
|
2018-06-07 22:22:22 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
DeviceModel::addDevice( Device *device )
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
|
|
|
|
m_devices << device;
|
|
|
|
std::sort( m_devices.begin(), m_devices.end(), []( const Device* dev1, const Device* dev2 )
|
|
|
|
{
|
|
|
|
return dev1->deviceNode() < dev2->deviceNode();
|
|
|
|
} );
|
|
|
|
|
|
|
|
endResetModel();
|
|
|
|
}
|
2018-06-09 01:20:05 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
DeviceModel::removeDevice( Device *device )
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
|
|
|
|
m_devices.removeAll( device );
|
|
|
|
std::sort( m_devices.begin(), m_devices.end(), []( const Device* dev1, const Device* dev2 )
|
|
|
|
{
|
|
|
|
return dev1->deviceNode() < dev2->deviceNode();
|
|
|
|
} );
|
|
|
|
|
|
|
|
endResetModel();
|
|
|
|
}
|