Make PartitionSizeController handle cloning the partition for PartResizerWidget

This commit is contained in:
Aurélien Gâteau 2014-08-09 11:31:00 +02:00
parent df89a391c4
commit 97980f5fad
6 changed files with 87 additions and 57 deletions

View File

@ -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

View File

@ -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();

View File

@ -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() );
}

View File

@ -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();

View File

@ -19,13 +19,13 @@
#include <gui/PartitionSizeController.h>
#include <core/ColorUtils.h>
#include <core/PMUtils.h>
// Qt
#include <QSpinBox>
// CalaPM
#include <core/device.h>
#include <core/partition.h>
#include <gui/partresizerwidget.h>
// 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();
}

View File

@ -19,9 +19,14 @@
#ifndef PARTITIONSIZECONTROLLER_H
#define PARTITIONSIZECONTROLLER_H
// CalaPM
#include <core/partition.h>
// Qt
#include <QColor>
#include <QObject>
#include <QPointer>
#include <QScopedPointer>
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: