source: calamares/trunk/fuentes/src/modules/partition/core/DeviceModel.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 16 months ago

sync with github

File size: 3.1 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014, Aurélien Gâteau <agateau@kde.org>
4 *   Copyright 2014, Teo Mrnjavac <teo@kde.org>
5 *
6 *   Calamares is free software: you can redistribute it and/or modify
7 *   it under the terms of the GNU General Public License as published by
8 *   the Free Software Foundation, either version 3 of the License, or
9 *   (at your option) any later version.
10 *
11 *   Calamares is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *   GNU General Public License for more details.
15 *
16 *   You should have received a copy of the GNU General Public License
17 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
18 */
19#include "core/DeviceModel.h"
20
21#include "core/PartitionModel.h"
22
23#include "utils/CalamaresUtilsGui.h"
24#include "utils/Logger.h"
25
26// KPMcore
27#include <kpmcore/core/device.h>
28
29// KF5
30#include <KFormat>
31
32#include <QIcon>
33
34// STL
35#include <algorithm>
36
37DeviceModel::DeviceModel( QObject* parent )
38    : QAbstractListModel( parent )
39{
40}
41
42DeviceModel::~DeviceModel()
43{
44}
45
46void
47DeviceModel::init( const QList< Device* >& devices )
48{
49    beginResetModel();
50    m_devices = devices;
51    std::sort( m_devices.begin(), m_devices.end(), []( const Device* dev1, const Device* dev2 )
52    {
53        return dev1->deviceNode() < dev2->deviceNode();
54    } );
55    endResetModel();
56}
57
58int
59DeviceModel::rowCount( const QModelIndex& parent ) const
60{
61    return parent.isValid() ? 0 : m_devices.count();
62}
63
64QVariant
65DeviceModel::data( const QModelIndex& index, int role ) const
66{
67    int row = index.row();
68    if ( row < 0 || row >= m_devices.count() )
69        return QVariant();
70
71    Device* device = m_devices.at( row );
72
73    switch ( role )
74    {
75    case Qt::DisplayRole:
76    case Qt::ToolTipRole:
77        if ( device->name().isEmpty() )
78            return device->deviceNode();
79        else
80            return tr( "%1 - %2 (%3)" )
81                   .arg( device->name() )
82                   .arg( KFormat().formatByteSize( device->capacity() ) )
83                   .arg( device->deviceNode() );
84    case Qt::DecorationRole:
85        return CalamaresUtils::defaultPixmap( CalamaresUtils::PartitionDisk,
86                                              CalamaresUtils::Original,
87                                              QSize( CalamaresUtils::defaultIconSize().width() * 3,
88                                                     CalamaresUtils::defaultIconSize().height() * 3 ) );
89    default:
90        return QVariant();
91    }
92}
93
94
95Device*
96DeviceModel::deviceForIndex( const QModelIndex& index ) const
97{
98    int row = index.row();
99    if ( row < 0 || row >= m_devices.count() )
100        return nullptr;
101    return m_devices.at( row );
102}
103
104
105void
106DeviceModel::swapDevice( Device* oldDevice, Device* newDevice )
107{
108    Q_ASSERT( oldDevice );
109    Q_ASSERT( newDevice );
110
111    int indexOfOldDevice = m_devices.indexOf( oldDevice );
112    if ( indexOfOldDevice < 0 )
113        return;
114
115    m_devices[ indexOfOldDevice ] = newDevice;
116
117    emit dataChanged( index( indexOfOldDevice ), index( indexOfOldDevice ) );
118}
Note: See TracBrowser for help on using the repository browser.