Move PartitionModel management from DeviceModel to PartitionCoreModule

main
Aurélien Gâteau 11 years ago
parent 578f2e4baa
commit 6d0b3218f1

@ -21,18 +21,6 @@
// CalaPM // CalaPM
#include <core/device.h> #include <core/device.h>
DeviceModel::DeviceInfo::DeviceInfo( Device* dev )
: device( dev )
, partitionModel( new PartitionModel )
{
partitionModel->init( dev );
}
DeviceModel::DeviceInfo::~DeviceInfo()
{
delete partitionModel;
}
DeviceModel::DeviceModel( QObject* parent ) DeviceModel::DeviceModel( QObject* parent )
: QAbstractListModel( parent ) : QAbstractListModel( parent )
{ {
@ -46,11 +34,7 @@ void
DeviceModel::init( const QList< Device* >& devices ) DeviceModel::init( const QList< Device* >& devices )
{ {
beginResetModel(); beginResetModel();
m_devices.clear(); m_devices = devices;
for ( auto device : devices )
{
m_devices << new DeviceInfo( device );
}
endResetModel(); endResetModel();
} }
@ -69,7 +53,7 @@ DeviceModel::data( const QModelIndex& index, int role ) const
return QVariant(); return QVariant();
} }
Device* device = m_devices.at( row )->device; Device* device = m_devices.at( row );
switch ( role ) switch ( role )
{ {
@ -87,13 +71,13 @@ DeviceModel::data( const QModelIndex& index, int role ) const
} }
} }
PartitionModel* Device*
DeviceModel::partitionModelForIndex( const QModelIndex& index ) const DeviceModel::deviceForIndex( const QModelIndex& index ) const
{ {
int row = index.row(); int row = index.row();
if ( row < 0 || row >= m_devices.count() ) if ( row < 0 || row >= m_devices.count() )
{ {
return nullptr; return nullptr;
} }
return m_devices.at( row )->partitionModel; return m_devices.at( row );
} }

@ -40,17 +40,10 @@ public:
int rowCount( const QModelIndex& parent = QModelIndex() ) const Q_DECL_OVERRIDE; int rowCount( const QModelIndex& parent = QModelIndex() ) const Q_DECL_OVERRIDE;
QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const Q_DECL_OVERRIDE; QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const Q_DECL_OVERRIDE;
PartitionModel* partitionModelForIndex( const QModelIndex& index ) const; Device* deviceForIndex( const QModelIndex& index ) const;
private: private:
struct DeviceInfo QList< Device* > m_devices;
{
DeviceInfo( Device* dev );
~DeviceInfo();
Device* device;
PartitionModel* partitionModel;
};
QList< DeviceInfo* > m_devices;
}; };
#endif /* DEVICEMODEL_H */ #endif /* DEVICEMODEL_H */

@ -19,12 +19,28 @@
#include <PartitionCoreModule.h> #include <PartitionCoreModule.h>
#include <DeviceModel.h> #include <DeviceModel.h>
#include <PartitionModel.h>
// CalaPM // CalaPM
#include <CalaPM.h> #include <CalaPM.h>
#include <backend/corebackend.h> #include <backend/corebackend.h>
#include <backend/corebackendmanager.h> #include <backend/corebackendmanager.h>
//- DeviceInfo --------------------------------------------
PartitionCoreModule::DeviceInfo::DeviceInfo( Device* dev )
: device( dev )
, partitionModel( new PartitionModel )
{
partitionModel->init( dev );
}
PartitionCoreModule::DeviceInfo::~DeviceInfo()
{
delete partitionModel;
}
//- PartitionCoreModule -----------------------------------
PartitionCoreModule::PartitionCoreModule( QObject* parent ) PartitionCoreModule::PartitionCoreModule( QObject* parent )
: QObject( parent ) : QObject( parent )
, m_deviceModel( new DeviceModel( this ) ) , m_deviceModel( new DeviceModel( this ) )
@ -34,16 +50,20 @@ PartitionCoreModule::PartitionCoreModule( QObject* parent )
{ {
qFatal( "Failed to init CalaPM" ); qFatal( "Failed to init CalaPM" );
} }
CoreBackend* backend = CoreBackendManager::self()->backend(); CoreBackend* backend = CoreBackendManager::self()->backend();
m_devices = backend->scanDevices(); QList< Device* > lst = backend->scanDevices();
m_deviceModel->init( lst );
for ( auto device : lst )
{
m_devices << new DeviceInfo( device );
}
m_deviceModel->init( m_devices );
} }
QList< Device* > PartitionCoreModule::~PartitionCoreModule()
PartitionCoreModule::devices() const
{ {
return m_devices; qDeleteAll( m_devices );
} }
DeviceModel* DeviceModel*
@ -51,3 +71,16 @@ PartitionCoreModule::deviceModel() const
{ {
return m_deviceModel; return m_deviceModel;
} }
PartitionModel*
PartitionCoreModule::partitionModelForDevice( Device* device ) const
{
for ( auto it : m_devices )
{
if ( it->device == device )
{
return it->partitionModel;
}
}
return nullptr;
}

@ -24,6 +24,7 @@
class Device; class Device;
class DeviceModel; class DeviceModel;
class PartitionModel;
/** /**
* Owns the Qt models and the PM devices * Owns the Qt models and the PM devices
@ -32,13 +33,21 @@ class PartitionCoreModule : public QObject
{ {
public: public:
PartitionCoreModule( QObject* parent = nullptr ); PartitionCoreModule( QObject* parent = nullptr );
~PartitionCoreModule();
QList< Device* > devices() const;
DeviceModel* deviceModel() const; DeviceModel* deviceModel() const;
PartitionModel* partitionModelForDevice( Device* device ) const;
private: private:
QList< Device* > m_devices; struct DeviceInfo
{
DeviceInfo( Device* dev );
~DeviceInfo();
Device* device;
PartitionModel* partitionModel;
};
QList< DeviceInfo* > m_devices;
DeviceModel* m_deviceModel; DeviceModel* m_deviceModel;
void listDevices(); void listDevices();

@ -39,7 +39,8 @@ PartitionPage::PartitionPage( QWidget* parent )
connect( m_ui->deviceListView->selectionModel(), &QItemSelectionModel::currentChanged, connect( m_ui->deviceListView->selectionModel(), &QItemSelectionModel::currentChanged,
[ this ]( const QModelIndex& index, const QModelIndex& oldIndex ) [ this ]( const QModelIndex& index, const QModelIndex& oldIndex )
{ {
PartitionModel* model = m_core->deviceModel()->partitionModelForIndex( index ); Device* device = m_core->deviceModel()->deviceForIndex( index );
PartitionModel* model = m_core->partitionModelForDevice( device );
m_ui->partitionListView->setModel( model ); m_ui->partitionListView->setModel( model );
} ); } );
} }

Loading…
Cancel
Save