From 97980f5fad3648ffbb5f950926a900c3a4ebe831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20G=C3=A2teau?= Date: Sat, 9 Aug 2014 11:31:00 +0200 Subject: [PATCH] Make PartitionSizeController handle cloning the partition for PartResizerWidget --- .../partition/gui/CreatePartitionDialog.cpp | 18 ++--- .../partition/gui/CreatePartitionDialog.h | 3 +- .../gui/EditExistingPartitionDialog.cpp | 30 ++------ .../gui/EditExistingPartitionDialog.h | 1 - .../partition/gui/PartitionSizeController.cpp | 71 +++++++++++++++---- .../partition/gui/PartitionSizeController.h | 21 ++++-- 6 files changed, 87 insertions(+), 57 deletions(-) diff --git a/src/modules/partition/gui/CreatePartitionDialog.cpp b/src/modules/partition/gui/CreatePartitionDialog.cpp index 344ddf76a..9a77a2a52 100644 --- a/src/modules/partition/gui/CreatePartitionDialog.cpp +++ b/src/modules/partition/gui/CreatePartitionDialog.cpp @@ -46,6 +46,7 @@ static QSet< FileSystem::Type > s_unmountableFS( CreatePartitionDialog::CreatePartitionDialog( Device* device, PartitionNode* parentPartition, QWidget* parentWidget ) : QDialog( parentWidget ) , m_ui( new Ui_CreatePartitionDialog ) + , m_partitionSizeController( new PartitionSizeController( this ) ) , m_device( device ) , m_parent( parentPartition ) { @@ -120,8 +121,8 @@ CreatePartitionDialog::createPartition() ); } - qint64 first = m_partResizerWidgetPartition->firstSector(); - qint64 last = m_partResizerWidgetPartition->lastSector(); + qint64 first = m_partitionSizeController->firstSector(); + qint64 last = m_partitionSizeController->lastSector(); FileSystem::Type fsType = m_role.has( PartitionRole::Extended ) ? FileSystem::Extended @@ -153,19 +154,12 @@ CreatePartitionDialog::updateMountPointUi() void CreatePartitionDialog::initPartResizerWidget( Partition* partition ) { - PartitionSizeController* controller = new PartitionSizeController( this ); - m_partResizerWidgetPartition.reset( PMUtils::clonePartition( m_device, partition ) ); - - qint64 minFirstSector = partition->firstSector() - m_device->partitionTable()->freeSectorsBefore( *partition ); - qint64 maxLastSector = partition->lastSector() + m_device->partitionTable()->freeSectorsAfter( *partition ); - m_ui->partResizerWidget->init( *m_device, *m_partResizerWidgetPartition, minFirstSector, maxLastSector ); - QColor color = PMUtils::isPartitionFreeSpace( partition ) ? ColorUtils::colorForPartitionInFreeSpace( partition ) : ColorUtils::colorForPartition( partition ); - controller->init( m_device, m_partResizerWidgetPartition.data(), color ); - controller->setPartResizerWidget( m_ui->partResizerWidget ); - controller->setSpinBox( m_ui->sizeSpinBox ); + m_partitionSizeController->init( m_device, partition, color ); + m_partitionSizeController->setPartResizerWidget( m_ui->partResizerWidget ); + m_partitionSizeController->setSpinBox( m_ui->sizeSpinBox ); } void diff --git a/src/modules/partition/gui/CreatePartitionDialog.h b/src/modules/partition/gui/CreatePartitionDialog.h index bbaf8a5c4..edfd56cc9 100644 --- a/src/modules/partition/gui/CreatePartitionDialog.h +++ b/src/modules/partition/gui/CreatePartitionDialog.h @@ -28,6 +28,7 @@ class Device; class Partition; class PartitionNode; +class PartitionSizeController; class Ui_CreatePartitionDialog; /** @@ -58,10 +59,10 @@ private Q_SLOTS: private: QScopedPointer< Ui_CreatePartitionDialog > m_ui; + PartitionSizeController* m_partitionSizeController; Device* m_device; PartitionNode* m_parent; PartitionRole m_role = PartitionRole( PartitionRole::None ); - QScopedPointer< Partition > m_partResizerWidgetPartition; void initGptPartitionTypeUi(); void initMbrPartitionTypeUi(); diff --git a/src/modules/partition/gui/EditExistingPartitionDialog.cpp b/src/modules/partition/gui/EditExistingPartitionDialog.cpp index 31eb59fe9..4512a6f69 100644 --- a/src/modules/partition/gui/EditExistingPartitionDialog.cpp +++ b/src/modules/partition/gui/EditExistingPartitionDialog.cpp @@ -45,12 +45,8 @@ EditExistingPartitionDialog::EditExistingPartitionDialog( Device* device, Partit { m_ui->setupUi( this ); - // Create a partition for partResizerWidget because it alters the first and - // last sectors when used - m_partResizerWidgetPartition.reset( PMUtils::clonePartition( m_device, m_partition ) ); - QColor color = ColorUtils::colorForPartition( m_partition ); - m_partitionSizeController->init( m_device, m_partResizerWidgetPartition.data(), color ); + m_partitionSizeController->init( m_device, m_partition, color ); m_partitionSizeController->setSpinBox( m_ui->sizeSpinBox ); m_ui->mountPointComboBox->setCurrentText( PartitionInfo::mountPoint( partition ) ); @@ -71,8 +67,8 @@ EditExistingPartitionDialog::applyChanges( PartitionCoreModule* core ) { PartitionInfo::setMountPoint( m_partition, m_ui->mountPointComboBox->currentText() ); - qint64 newFirstSector = m_partResizerWidgetPartition->firstSector(); - qint64 newLastSector = m_partResizerWidgetPartition->lastSector(); + qint64 newFirstSector = m_partitionSizeController->firstSector(); + qint64 newLastSector = m_partitionSizeController->lastSector(); bool partitionChanged = newFirstSector != m_partition->firstSector() || newLastSector != m_partition->lastSector(); if ( partitionChanged ) @@ -114,29 +110,11 @@ EditExistingPartitionDialog::replacePartResizerWidget() * "keep". This is a hack which replaces the existing PartResizerWidget * with a new one. */ - - bool format = m_ui->formatRadioButton->isChecked(); - - qint64 used = format ? 0 : m_partition->fileSystem().sectorsUsed(); - m_partResizerWidgetPartition->fileSystem().setSectorsUsed( used ); - - qint64 minFirstSector = m_partition->firstSector() - m_device->partitionTable()->freeSectorsBefore( *m_partition ); - qint64 maxLastSector = m_partition->lastSector() + m_device->partitionTable()->freeSectorsAfter( *m_partition ); - PartResizerWidget* widget = new PartResizerWidget( this ); - widget->init( *m_device, *m_partResizerWidgetPartition, minFirstSector, maxLastSector ); - - if ( !format ) - { - // If we are not formatting, make sure the space between the first and - // last sectors is big enough to fit the existing content. - widget->updateLastSector( m_partResizerWidgetPartition->lastSector() ); - widget->updateFirstSector( m_partResizerWidgetPartition->firstSector() ); - } layout()->replaceWidget( m_ui->partResizerWidget, widget ); delete m_ui->partResizerWidget; m_ui->partResizerWidget = widget; - m_partitionSizeController->setPartResizerWidget( widget ); + m_partitionSizeController->setPartResizerWidget( widget, m_ui->formatRadioButton->isChecked() ); } diff --git a/src/modules/partition/gui/EditExistingPartitionDialog.h b/src/modules/partition/gui/EditExistingPartitionDialog.h index 6f887310c..e9cc099ac 100644 --- a/src/modules/partition/gui/EditExistingPartitionDialog.h +++ b/src/modules/partition/gui/EditExistingPartitionDialog.h @@ -47,7 +47,6 @@ private: QScopedPointer< Ui_EditExistingPartitionDialog > m_ui; Device* m_device; Partition* m_partition; - QScopedPointer< Partition > m_partResizerWidgetPartition; PartitionSizeController* m_partitionSizeController; void replacePartResizerWidget(); diff --git a/src/modules/partition/gui/PartitionSizeController.cpp b/src/modules/partition/gui/PartitionSizeController.cpp index a08c08d8c..0b98f4e3c 100644 --- a/src/modules/partition/gui/PartitionSizeController.cpp +++ b/src/modules/partition/gui/PartitionSizeController.cpp @@ -19,13 +19,13 @@ #include #include +#include // Qt #include // CalaPM #include -#include #include // stdc++ @@ -36,11 +36,40 @@ PartitionSizeController::PartitionSizeController( QObject* parent ) {} void -PartitionSizeController::setPartResizerWidget( PartResizerWidget* widget ) +PartitionSizeController::init( Device* device, Partition* partition, const QColor& color ) { + m_device = device; + m_originalPartition = partition; + // PartResizerWidget stores its changes directly in the partition it is + // initialized with. We don't want the changes to be committed that way, + // because it means we would have to revert them if the user cancel the + // dialog the widget is in. Therefore we init PartResizerWidget with a clone + // of the original partition. + m_partition.reset( PMUtils::clonePartition( m_device, partition ) ); + m_partitionColor = color; +} + +void +PartitionSizeController::setPartResizerWidget( PartResizerWidget* widget, bool format ) +{ + Q_ASSERT( m_device ); + if ( m_partResizerWidget ) disconnect( m_partResizerWidget, 0, this, 0 ); + + // Update partition filesystem. This must be done *before* the call to + // PartResizerWidget::init() otherwise it will be ignored by the widget. + // This is why this method accept a `format` boolean. + qint64 used = format ? 0 : m_originalPartition->fileSystem().sectorsUsed(); + m_partition->fileSystem().setSectorsUsed( used ); + + // Init PartResizerWidget m_partResizerWidget = widget; + PartitionTable* table = m_device->partitionTable(); + qint64 minFirstSector = m_originalPartition->firstSector() - table->freeSectorsBefore( *m_originalPartition ); + qint64 maxLastSector = m_originalPartition->lastSector() + table->freeSectorsAfter( *m_originalPartition ); + m_partResizerWidget->init( *m_device, *m_partition.data(), minFirstSector, maxLastSector ); + // FIXME: Should be set by PartResizerWidget itself m_partResizerWidget->setFixedHeight( PartResizerWidget::handleHeight() ); @@ -48,7 +77,15 @@ PartitionSizeController::setPartResizerWidget( PartResizerWidget* widget ) pal.setColor( QPalette::Base, ColorUtils::freeSpaceColor() ); pal.setColor( QPalette::Button, m_partitionColor ); m_partResizerWidget->setPalette( pal ); - updateConnections(); + connectWidgets(); + + if ( !format ) + { + // If we are not formatting, update the widget to make sure the space + // between the first and last sectors is big enough to fit the existing + // content. + updatePartResizerWidget(); + } } void @@ -58,19 +95,11 @@ PartitionSizeController::setSpinBox( QSpinBox* spinBox ) disconnect( m_spinBox, 0, this, 0 ); m_spinBox = spinBox; m_spinBox->setMaximum( std::numeric_limits< int >::max() ); - updateConnections(); + connectWidgets(); } void -PartitionSizeController::init( Device* device, Partition* partition, const QColor& color ) -{ - m_device = device; - m_partition = partition; - m_partitionColor = color; -} - -void -PartitionSizeController::updateConnections() +PartitionSizeController::connectWidgets() { if ( !m_spinBox || !m_partResizerWidget ) return; @@ -78,6 +107,8 @@ PartitionSizeController::updateConnections() connect( m_spinBox, SIGNAL( editingFinished() ), SLOT( updatePartResizerWidget() ) ); connect( m_partResizerWidget, SIGNAL( firstSectorChanged( qint64 ) ), SLOT( updateSpinBox() ) ); connect( m_partResizerWidget, SIGNAL( lastSectorChanged( qint64 ) ), SLOT( updateSpinBox() ) ); + + // Init m_spinBox from m_partResizerWidget updateSpinBox(); } @@ -118,6 +149,20 @@ PartitionSizeController::updateSpinBox() void PartitionSizeController::doUpdateSpinBox() { + if ( !m_spinBox ) + return; qint64 mbSize = ( m_partition->lastSector() - m_partition->firstSector() + 1 ) * m_device->logicalSectorSize() / 1024 / 1024; m_spinBox->setValue( mbSize ); } + +qint64 +PartitionSizeController::firstSector() const +{ + return m_partition->firstSector(); +} + +qint64 +PartitionSizeController::lastSector() const +{ + return m_partition->lastSector(); +} diff --git a/src/modules/partition/gui/PartitionSizeController.h b/src/modules/partition/gui/PartitionSizeController.h index 9b1283df9..93e07bf70 100644 --- a/src/modules/partition/gui/PartitionSizeController.h +++ b/src/modules/partition/gui/PartitionSizeController.h @@ -19,9 +19,14 @@ #ifndef PARTITIONSIZECONTROLLER_H #define PARTITIONSIZECONTROLLER_H +// CalaPM +#include + +// Qt #include #include #include +#include class QSpinBox; @@ -32,25 +37,33 @@ class PartResizerWidget; /** * Synchronizes a PartResizerWidget and a QSpinBox, making sure any change made * to one is reflected in the other. + * + * It does not touch the partition it works on: changes are exposed through the + * firstSector() and lastSector() getters. */ class PartitionSizeController : public QObject { Q_OBJECT public: explicit PartitionSizeController( QObject* parent = nullptr ); - void setPartResizerWidget( PartResizerWidget* widget ); - void setSpinBox( QSpinBox* spinBox ); void init( Device* device, Partition* partition, const QColor& color ); + void setPartResizerWidget( PartResizerWidget* widget, bool format = true ); + void setSpinBox( QSpinBox* spinBox ); + + qint64 firstSector() const; + qint64 lastSector() const; private: QPointer< PartResizerWidget > m_partResizerWidget; QPointer< QSpinBox > m_spinBox; Device* m_device = nullptr; - Partition* m_partition = nullptr; + const Partition* m_originalPartition = nullptr; + QScopedPointer< Partition > m_partition; QColor m_partitionColor; + bool m_updating = false; - void updateConnections(); + void connectWidgets(); void doUpdateSpinBox(); private Q_SLOTS: