From 8f474fa08f93b0f4fedbef2cf167f33d99c8fa86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20G=C3=A2teau?= Date: Fri, 27 Jun 2014 18:42:15 +0200 Subject: [PATCH] Persistent partition model This will make it possible to update the partition list on changes, regardless of the actual device state --- src/modules/partition/DeviceModel.cpp | 33 +++++++++++++++++++++---- src/modules/partition/DeviceModel.h | 19 ++++++++++++-- src/modules/partition/PartitionPage.cpp | 12 ++++----- src/modules/partition/PartitionPage.h | 2 -- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/modules/partition/DeviceModel.cpp b/src/modules/partition/DeviceModel.cpp index ed57e1047..58cc3e98a 100644 --- a/src/modules/partition/DeviceModel.cpp +++ b/src/modules/partition/DeviceModel.cpp @@ -16,20 +16,43 @@ * along with Calamares. If not, see . */ #include +#include // CalaPM #include +DeviceModel::DeviceInfo::DeviceInfo( Device* dev ) + : device( dev ) + , partitionModel( new PartitionModel ) +{ + partitionModel->init( dev ); +} + +DeviceModel::DeviceInfo::~DeviceInfo() +{ + delete device; + delete partitionModel; +} + DeviceModel::DeviceModel( QObject* parent ) : QAbstractListModel( parent ) { } +DeviceModel::~DeviceModel() +{ + qDeleteAll( m_devices ); +} + void DeviceModel::init( const QList< Device* >& devices ) { beginResetModel(); - m_devices = devices; + m_devices.clear(); + for ( auto device : devices ) + { + m_devices << new DeviceInfo( device ); + } endResetModel(); } @@ -48,7 +71,7 @@ DeviceModel::data( const QModelIndex& index, int role ) const return QVariant(); } - Device* device = m_devices.at( row ); + Device* device = m_devices.at( row )->device; switch ( role ) { @@ -66,13 +89,13 @@ DeviceModel::data( const QModelIndex& index, int role ) const } } -Device* -DeviceModel::deviceForIndex( const QModelIndex& index ) const +PartitionModel* +DeviceModel::partitionModelForIndex( const QModelIndex& index ) const { int row = index.row(); if ( row < 0 || row >= m_devices.count() ) { return nullptr; } - return m_devices.at( row ); + return m_devices.at( row )->partitionModel; } diff --git a/src/modules/partition/DeviceModel.h b/src/modules/partition/DeviceModel.h index 51fce7c5d..938ef3f80 100644 --- a/src/modules/partition/DeviceModel.h +++ b/src/modules/partition/DeviceModel.h @@ -19,23 +19,38 @@ #define DEVICEMODEL_H #include +#include #include class Device; +class PartitionModel; class DeviceModel : public QAbstractListModel { public: DeviceModel( QObject* parent = 0 ); + ~DeviceModel(); + + /** + * Init the model with the list of devices. + * Takes ownership of the devices. + */ void init( const QList< Device* >& devices ); int rowCount( const QModelIndex& parent = QModelIndex() ) const Q_DECL_OVERRIDE; QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const Q_DECL_OVERRIDE; - Device* deviceForIndex( const QModelIndex& index ) const; + PartitionModel* partitionModelForIndex( const QModelIndex& index ) const; private: - QList< Device* > m_devices; + struct DeviceInfo + { + DeviceInfo( Device* dev ); + ~DeviceInfo(); + Device* device; + PartitionModel* partitionModel; + }; + QList< DeviceInfo* > m_devices; }; #endif /* DEVICEMODEL_H */ diff --git a/src/modules/partition/PartitionPage.cpp b/src/modules/partition/PartitionPage.cpp index 0843b6ab5..af1150756 100644 --- a/src/modules/partition/PartitionPage.cpp +++ b/src/modules/partition/PartitionPage.cpp @@ -36,7 +36,6 @@ PartitionPage::PartitionPage( QWidget* parent ) : Calamares::AbstractPage( parent ) , m_ui( new Ui_PartitionPage ) , m_deviceModel( new DeviceModel( this ) ) - , m_partitionModel( new PartitionModel( this ) ) { // FIXME: Should be done at startup if ( !CalaPM::init() ) @@ -46,16 +45,15 @@ PartitionPage::PartitionPage( QWidget* parent ) m_backend = CoreBackendManager::self()->backend(); m_ui->setupUi( this ); m_ui->deviceListView->setModel( m_deviceModel ); - m_ui->partitionListView->setModel( m_partitionModel ); m_deviceModel->init( m_backend->scanDevices() ); connect( m_ui->deviceListView->selectionModel(), &QItemSelectionModel::currentChanged, - [ this ]( const QModelIndex& index, const QModelIndex& oldIndex ) - { - Device* device = m_deviceModel->deviceForIndex( index ); - m_partitionModel->init( device ); - } ); + [ this ]( const QModelIndex& index, const QModelIndex& oldIndex ) + { + PartitionModel* model = m_deviceModel->partitionModelForIndex( index ); + m_ui->partitionListView->setModel( model ); + } ); } PartitionPage::~PartitionPage() diff --git a/src/modules/partition/PartitionPage.h b/src/modules/partition/PartitionPage.h index 24f4d00c5..8b4949e7d 100644 --- a/src/modules/partition/PartitionPage.h +++ b/src/modules/partition/PartitionPage.h @@ -27,7 +27,6 @@ class CoreBackend; class Ui_PartitionPage; class DeviceModel; -class PartitionModel; class PartitionPage : public Calamares::AbstractPage { @@ -43,7 +42,6 @@ public Q_SLOTS: private: QScopedPointer< Ui_PartitionPage > m_ui; DeviceModel* m_deviceModel; - PartitionModel* m_partitionModel; CoreBackend* m_backend; };