From 48c078acc5fa8aa30a8410986aaf617dd822afb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20G=C3=A2teau?= Date: Tue, 15 Jul 2014 17:37:04 +0200 Subject: [PATCH] Start implementing editing of newly-created partitions This is the beginning of #19 --- .../partition/CreatePartitionDialog.cpp | 48 ++++++++++++------- src/modules/partition/CreatePartitionDialog.h | 9 +++- src/modules/partition/PartitionPage.cpp | 41 +++++++++++++++- src/modules/partition/PartitionPage.h | 6 +++ 4 files changed, 85 insertions(+), 19 deletions(-) diff --git a/src/modules/partition/CreatePartitionDialog.cpp b/src/modules/partition/CreatePartitionDialog.cpp index 463554847..bac85df1c 100644 --- a/src/modules/partition/CreatePartitionDialog.cpp +++ b/src/modules/partition/CreatePartitionDialog.cpp @@ -31,19 +31,20 @@ #include #include -CreatePartitionDialog::CreatePartitionDialog( Device* device, Partition* freePartition, QWidget* parent ) - : QDialog( parent ) +CreatePartitionDialog::CreatePartitionDialog( Device* device, PartitionNode* parentPartition, QWidget* parentWidget ) + : QDialog( parentWidget ) , m_ui( new Ui_CreatePartitionDialog ) , m_device( device ) - , m_freePartition( freePartition ) + , m_parent( parentPartition ) { m_ui->setupUi( this ); FileSystemFactory::init(); + bool parentIsPartitionTable = parentPartition->isRoot(); // Partition types QString fixedPartitionType; - if ( freePartition->roles().has( PartitionRole::Logical ) ) + if ( !parentIsPartitionTable ) { m_role = PartitionRole( PartitionRole::Logical ); fixedPartitionType = tr( "Logical" ); @@ -72,12 +73,6 @@ CreatePartitionDialog::CreatePartitionDialog( Device* device, Partition* freePar } m_ui->fsComboBox->addItems( fsNames ); - // Size - qint64 maxSize = ( freePartition->lastSector() - freePartition->firstSector() + 1 ) * device->logicalSectorSize(); - - m_ui->sizeSpinBox->setMaximum( maxSize / 1024 / 1024 ); - m_ui->sizeSpinBox->setValue( m_ui->sizeSpinBox->maximum() ); - // Connections connect( m_ui->fsComboBox, SIGNAL( activated( int ) ), SLOT( updateMountPointUi() ) ); connect( m_ui->extendedRadioButton, SIGNAL( toggled( bool ) ), SLOT( updateMountPointUi() ) ); @@ -86,6 +81,19 @@ CreatePartitionDialog::CreatePartitionDialog( Device* device, Partition* freePar CreatePartitionDialog::~CreatePartitionDialog() {} +void +CreatePartitionDialog::setSectorRange( qint64 minSector, qint64 maxSector ) +{ + Q_ASSERT( minSector <= maxSector ); + m_minSector = minSector; + m_maxSector = maxSector; + + qint64 maxSize = ( m_maxSector - m_minSector + 1 ) * m_device->logicalSectorSize(); + + m_ui->sizeSpinBox->setMaximum( maxSize / 1024 / 1024 ); + m_ui->sizeSpinBox->setValue( m_ui->sizeSpinBox->maximum() ); +} + PartitionInfo* CreatePartitionDialog::createPartitionInfo() { @@ -98,21 +106,19 @@ CreatePartitionDialog::createPartitionInfo() ); } - qint64 first = m_freePartition->firstSector(); // FIXME: Check rounding errors here - qint64 last = first + qint64( m_ui->sizeSpinBox->value() ) * 1024 * 1024 / m_device->logicalSectorSize(); + qint64 last = m_minSector + qint64( m_ui->sizeSpinBox->value() ) * 1024 * 1024 / m_device->logicalSectorSize(); FileSystem::Type type = m_role.has( PartitionRole::Extended ) ? FileSystem::Extended : FileSystem::typeForName( m_ui->fsComboBox->currentText() ); - FileSystem* fs = FileSystemFactory::create( type, first, last ); + FileSystem* fs = FileSystemFactory::create( type, m_minSector, last ); - PartitionNode* parent = m_freePartition->parent(); auto partition = new Partition( - parent, + m_parent, *m_device, m_role, - fs, first, last, + fs, m_minSector, last, QString() /* path */, PartitionTable::FlagNone /* availableFlags */, QString() /* mountPoint */, @@ -141,3 +147,13 @@ CreatePartitionDialog::updateMountPointUi() m_ui->mountPointLabel->setEnabled( enabled ); m_ui->mountPointComboBox->setEnabled( enabled ); } + +void +CreatePartitionDialog::initFromPartition( Partition* partition ) +{ + Q_ASSERT( partition ); + qint64 maxSize = ( partition->lastSector() - partition->firstSector() + 1 ) * m_device->logicalSectorSize(); + m_ui->sizeSpinBox->setValue( maxSize / 1024 / 1024 ); + + // FIXME: Update other fields +} diff --git a/src/modules/partition/CreatePartitionDialog.h b/src/modules/partition/CreatePartitionDialog.h index c6c7c940c..cd83364c1 100644 --- a/src/modules/partition/CreatePartitionDialog.h +++ b/src/modules/partition/CreatePartitionDialog.h @@ -27,6 +27,7 @@ class Device; class Partition; +class PartitionNode; class PartitionInfo; class Ui_CreatePartitionDialog; @@ -34,9 +35,11 @@ class CreatePartitionDialog : public QDialog { Q_OBJECT public: - CreatePartitionDialog( Device* device, Partition* freePartition, QWidget* parent = nullptr ); + CreatePartitionDialog( Device* device, PartitionNode* parentPartition, QWidget* parentWidget = nullptr ); ~CreatePartitionDialog(); + void setSectorRange( qint64 minSector, qint64 maxSector ); + void initFromPartition( Partition* partition ); PartitionInfo* createPartitionInfo(); private Q_SLOTS: @@ -45,7 +48,9 @@ private Q_SLOTS: private: QScopedPointer< Ui_CreatePartitionDialog > m_ui; Device* m_device; - Partition* m_freePartition; + qint64 m_minSector = 0; + qint64 m_maxSector = 0; + PartitionNode* m_parent; PartitionRole m_role = PartitionRole( PartitionRole::None ); }; diff --git a/src/modules/partition/PartitionPage.cpp b/src/modules/partition/PartitionPage.cpp index 20993b166..17f27b4c2 100644 --- a/src/modules/partition/PartitionPage.cpp +++ b/src/modules/partition/PartitionPage.cpp @@ -29,6 +29,7 @@ // CalaPM #include +#include // Qt #include @@ -72,6 +73,7 @@ PartitionPage::PartitionPage( PartitionCoreModule* core, QWidget* parent ) connect( m_ui->newPartitionTableButton, &QAbstractButton::clicked, this, &PartitionPage::onNewPartitionTableClicked ); connect( m_ui->createButton, &QAbstractButton::clicked, this, &PartitionPage::onCreateClicked ); + connect( m_ui->editButton, &QAbstractButton::clicked, this, &PartitionPage::onEditClicked ); connect( m_ui->deleteButton, &QAbstractButton::clicked, this, &PartitionPage::onDeleteClicked ); } @@ -132,12 +134,29 @@ PartitionPage::onCreateClicked() Partition* partition = model->partitionForIndex( index ); Q_ASSERT( partition ); - QPointer dlg = new CreatePartitionDialog( model->device(), partition, this ); + QPointer dlg = new CreatePartitionDialog( model->device(), partition->parent(), this ); + dlg->setSectorRange( partition->firstSector(), partition->lastSector() ); if ( dlg->exec() == QDialog::Accepted ) m_core->createPartition( model->device(), dlg->createPartitionInfo() ); delete dlg; } +void +PartitionPage::onEditClicked() +{ + QModelIndex index = m_ui->partitionTreeView->currentIndex(); + Q_ASSERT( index.isValid() ); + + const PartitionModel* model = static_cast< const PartitionModel* >( index.model() ); + Partition* partition = model->partitionForIndex( index ); + Q_ASSERT( partition ); + + if ( index.data( PartitionModel::IsNewPartitionRole ).toBool() ) + updatePartitionToCreate( model->device(), partition ); + else + editExistingPartition( partition ); +} + void PartitionPage::onDeleteClicked() { @@ -150,3 +169,23 @@ PartitionPage::onDeleteClicked() m_core->deletePartition( model->device(), partition ); } + +void +PartitionPage::updatePartitionToCreate( Device* device, Partition* partition ) +{ + QPointer dlg = new CreatePartitionDialog( device, partition->parent(), this ); + qint64 extraSectors = device->partitionTable()->freeSectorsAfter( *partition ); + dlg->setSectorRange( partition->firstSector(), partition->lastSector() + extraSectors ); + dlg->initFromPartition( partition ); + if ( dlg->exec() == QDialog::Accepted ) + { + m_core->deletePartition( device, partition ); + m_core->createPartition( device, dlg->createPartitionInfo() ); + } + delete dlg; +} + +void +PartitionPage::editExistingPartition( Partition* partition ) +{ +} diff --git a/src/modules/partition/PartitionPage.h b/src/modules/partition/PartitionPage.h index 75d013778..d4741a5ea 100644 --- a/src/modules/partition/PartitionPage.h +++ b/src/modules/partition/PartitionPage.h @@ -25,7 +25,9 @@ class PartitionCoreModule; class Ui_PartitionPage; +class Device; class DeviceModel; +class Partition; class PartitionPage : public QWidget { @@ -44,7 +46,11 @@ private: void updateButtons(); void onNewPartitionTableClicked(); void onCreateClicked(); + void onEditClicked(); void onDeleteClicked(); + + void updatePartitionToCreate( Device*, Partition* ); + void editExistingPartition( Partition* ); }; #endif // PARTITIONPAGE_H