Make PartitionSizeController handle cloning the partition for PartResizerWidget
This commit is contained in:
parent
df89a391c4
commit
97980f5fad
@ -46,6 +46,7 @@ static QSet< FileSystem::Type > s_unmountableFS(
|
|||||||
CreatePartitionDialog::CreatePartitionDialog( Device* device, PartitionNode* parentPartition, QWidget* parentWidget )
|
CreatePartitionDialog::CreatePartitionDialog( Device* device, PartitionNode* parentPartition, QWidget* parentWidget )
|
||||||
: QDialog( parentWidget )
|
: QDialog( parentWidget )
|
||||||
, m_ui( new Ui_CreatePartitionDialog )
|
, m_ui( new Ui_CreatePartitionDialog )
|
||||||
|
, m_partitionSizeController( new PartitionSizeController( this ) )
|
||||||
, m_device( device )
|
, m_device( device )
|
||||||
, m_parent( parentPartition )
|
, m_parent( parentPartition )
|
||||||
{
|
{
|
||||||
@ -120,8 +121,8 @@ CreatePartitionDialog::createPartition()
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
qint64 first = m_partResizerWidgetPartition->firstSector();
|
qint64 first = m_partitionSizeController->firstSector();
|
||||||
qint64 last = m_partResizerWidgetPartition->lastSector();
|
qint64 last = m_partitionSizeController->lastSector();
|
||||||
|
|
||||||
FileSystem::Type fsType = m_role.has( PartitionRole::Extended )
|
FileSystem::Type fsType = m_role.has( PartitionRole::Extended )
|
||||||
? FileSystem::Extended
|
? FileSystem::Extended
|
||||||
@ -153,19 +154,12 @@ CreatePartitionDialog::updateMountPointUi()
|
|||||||
void
|
void
|
||||||
CreatePartitionDialog::initPartResizerWidget( Partition* partition )
|
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 )
|
QColor color = PMUtils::isPartitionFreeSpace( partition )
|
||||||
? ColorUtils::colorForPartitionInFreeSpace( partition )
|
? ColorUtils::colorForPartitionInFreeSpace( partition )
|
||||||
: ColorUtils::colorForPartition( partition );
|
: ColorUtils::colorForPartition( partition );
|
||||||
controller->init( m_device, m_partResizerWidgetPartition.data(), color );
|
m_partitionSizeController->init( m_device, partition, color );
|
||||||
controller->setPartResizerWidget( m_ui->partResizerWidget );
|
m_partitionSizeController->setPartResizerWidget( m_ui->partResizerWidget );
|
||||||
controller->setSpinBox( m_ui->sizeSpinBox );
|
m_partitionSizeController->setSpinBox( m_ui->sizeSpinBox );
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
class Device;
|
class Device;
|
||||||
class Partition;
|
class Partition;
|
||||||
class PartitionNode;
|
class PartitionNode;
|
||||||
|
class PartitionSizeController;
|
||||||
class Ui_CreatePartitionDialog;
|
class Ui_CreatePartitionDialog;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,10 +59,10 @@ private Q_SLOTS:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
QScopedPointer< Ui_CreatePartitionDialog > m_ui;
|
QScopedPointer< Ui_CreatePartitionDialog > m_ui;
|
||||||
|
PartitionSizeController* m_partitionSizeController;
|
||||||
Device* m_device;
|
Device* m_device;
|
||||||
PartitionNode* m_parent;
|
PartitionNode* m_parent;
|
||||||
PartitionRole m_role = PartitionRole( PartitionRole::None );
|
PartitionRole m_role = PartitionRole( PartitionRole::None );
|
||||||
QScopedPointer< Partition > m_partResizerWidgetPartition;
|
|
||||||
|
|
||||||
void initGptPartitionTypeUi();
|
void initGptPartitionTypeUi();
|
||||||
void initMbrPartitionTypeUi();
|
void initMbrPartitionTypeUi();
|
||||||
|
@ -45,12 +45,8 @@ EditExistingPartitionDialog::EditExistingPartitionDialog( Device* device, Partit
|
|||||||
{
|
{
|
||||||
m_ui->setupUi( this );
|
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 );
|
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_partitionSizeController->setSpinBox( m_ui->sizeSpinBox );
|
||||||
|
|
||||||
m_ui->mountPointComboBox->setCurrentText( PartitionInfo::mountPoint( partition ) );
|
m_ui->mountPointComboBox->setCurrentText( PartitionInfo::mountPoint( partition ) );
|
||||||
@ -71,8 +67,8 @@ EditExistingPartitionDialog::applyChanges( PartitionCoreModule* core )
|
|||||||
{
|
{
|
||||||
PartitionInfo::setMountPoint( m_partition, m_ui->mountPointComboBox->currentText() );
|
PartitionInfo::setMountPoint( m_partition, m_ui->mountPointComboBox->currentText() );
|
||||||
|
|
||||||
qint64 newFirstSector = m_partResizerWidgetPartition->firstSector();
|
qint64 newFirstSector = m_partitionSizeController->firstSector();
|
||||||
qint64 newLastSector = m_partResizerWidgetPartition->lastSector();
|
qint64 newLastSector = m_partitionSizeController->lastSector();
|
||||||
bool partitionChanged = newFirstSector != m_partition->firstSector() || newLastSector != m_partition->lastSector();
|
bool partitionChanged = newFirstSector != m_partition->firstSector() || newLastSector != m_partition->lastSector();
|
||||||
|
|
||||||
if ( partitionChanged )
|
if ( partitionChanged )
|
||||||
@ -114,29 +110,11 @@ EditExistingPartitionDialog::replacePartResizerWidget()
|
|||||||
* "keep". This is a hack which replaces the existing PartResizerWidget
|
* "keep". This is a hack which replaces the existing PartResizerWidget
|
||||||
* with a new one.
|
* 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 );
|
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 );
|
layout()->replaceWidget( m_ui->partResizerWidget, widget );
|
||||||
delete m_ui->partResizerWidget;
|
delete m_ui->partResizerWidget;
|
||||||
m_ui->partResizerWidget = widget;
|
m_ui->partResizerWidget = widget;
|
||||||
|
|
||||||
m_partitionSizeController->setPartResizerWidget( widget );
|
m_partitionSizeController->setPartResizerWidget( widget, m_ui->formatRadioButton->isChecked() );
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,6 @@ private:
|
|||||||
QScopedPointer< Ui_EditExistingPartitionDialog > m_ui;
|
QScopedPointer< Ui_EditExistingPartitionDialog > m_ui;
|
||||||
Device* m_device;
|
Device* m_device;
|
||||||
Partition* m_partition;
|
Partition* m_partition;
|
||||||
QScopedPointer< Partition > m_partResizerWidgetPartition;
|
|
||||||
PartitionSizeController* m_partitionSizeController;
|
PartitionSizeController* m_partitionSizeController;
|
||||||
|
|
||||||
void replacePartResizerWidget();
|
void replacePartResizerWidget();
|
||||||
|
@ -19,13 +19,13 @@
|
|||||||
#include <gui/PartitionSizeController.h>
|
#include <gui/PartitionSizeController.h>
|
||||||
|
|
||||||
#include <core/ColorUtils.h>
|
#include <core/ColorUtils.h>
|
||||||
|
#include <core/PMUtils.h>
|
||||||
|
|
||||||
// Qt
|
// Qt
|
||||||
#include <QSpinBox>
|
#include <QSpinBox>
|
||||||
|
|
||||||
// CalaPM
|
// CalaPM
|
||||||
#include <core/device.h>
|
#include <core/device.h>
|
||||||
#include <core/partition.h>
|
|
||||||
#include <gui/partresizerwidget.h>
|
#include <gui/partresizerwidget.h>
|
||||||
|
|
||||||
// stdc++
|
// stdc++
|
||||||
@ -36,11 +36,40 @@ PartitionSizeController::PartitionSizeController( QObject* parent )
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
void
|
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 )
|
if ( m_partResizerWidget )
|
||||||
disconnect( m_partResizerWidget, 0, this, 0 );
|
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;
|
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
|
// FIXME: Should be set by PartResizerWidget itself
|
||||||
m_partResizerWidget->setFixedHeight( PartResizerWidget::handleHeight() );
|
m_partResizerWidget->setFixedHeight( PartResizerWidget::handleHeight() );
|
||||||
|
|
||||||
@ -48,7 +77,15 @@ PartitionSizeController::setPartResizerWidget( PartResizerWidget* widget )
|
|||||||
pal.setColor( QPalette::Base, ColorUtils::freeSpaceColor() );
|
pal.setColor( QPalette::Base, ColorUtils::freeSpaceColor() );
|
||||||
pal.setColor( QPalette::Button, m_partitionColor );
|
pal.setColor( QPalette::Button, m_partitionColor );
|
||||||
m_partResizerWidget->setPalette( pal );
|
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
|
void
|
||||||
@ -58,19 +95,11 @@ PartitionSizeController::setSpinBox( QSpinBox* spinBox )
|
|||||||
disconnect( m_spinBox, 0, this, 0 );
|
disconnect( m_spinBox, 0, this, 0 );
|
||||||
m_spinBox = spinBox;
|
m_spinBox = spinBox;
|
||||||
m_spinBox->setMaximum( std::numeric_limits< int >::max() );
|
m_spinBox->setMaximum( std::numeric_limits< int >::max() );
|
||||||
updateConnections();
|
connectWidgets();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PartitionSizeController::init( Device* device, Partition* partition, const QColor& color )
|
PartitionSizeController::connectWidgets()
|
||||||
{
|
|
||||||
m_device = device;
|
|
||||||
m_partition = partition;
|
|
||||||
m_partitionColor = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
PartitionSizeController::updateConnections()
|
|
||||||
{
|
{
|
||||||
if ( !m_spinBox || !m_partResizerWidget )
|
if ( !m_spinBox || !m_partResizerWidget )
|
||||||
return;
|
return;
|
||||||
@ -78,6 +107,8 @@ PartitionSizeController::updateConnections()
|
|||||||
connect( m_spinBox, SIGNAL( editingFinished() ), SLOT( updatePartResizerWidget() ) );
|
connect( m_spinBox, SIGNAL( editingFinished() ), SLOT( updatePartResizerWidget() ) );
|
||||||
connect( m_partResizerWidget, SIGNAL( firstSectorChanged( qint64 ) ), SLOT( updateSpinBox() ) );
|
connect( m_partResizerWidget, SIGNAL( firstSectorChanged( qint64 ) ), SLOT( updateSpinBox() ) );
|
||||||
connect( m_partResizerWidget, SIGNAL( lastSectorChanged( qint64 ) ), SLOT( updateSpinBox() ) );
|
connect( m_partResizerWidget, SIGNAL( lastSectorChanged( qint64 ) ), SLOT( updateSpinBox() ) );
|
||||||
|
|
||||||
|
// Init m_spinBox from m_partResizerWidget
|
||||||
updateSpinBox();
|
updateSpinBox();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,6 +149,20 @@ PartitionSizeController::updateSpinBox()
|
|||||||
void
|
void
|
||||||
PartitionSizeController::doUpdateSpinBox()
|
PartitionSizeController::doUpdateSpinBox()
|
||||||
{
|
{
|
||||||
|
if ( !m_spinBox )
|
||||||
|
return;
|
||||||
qint64 mbSize = ( m_partition->lastSector() - m_partition->firstSector() + 1 ) * m_device->logicalSectorSize() / 1024 / 1024;
|
qint64 mbSize = ( m_partition->lastSector() - m_partition->firstSector() + 1 ) * m_device->logicalSectorSize() / 1024 / 1024;
|
||||||
m_spinBox->setValue( mbSize );
|
m_spinBox->setValue( mbSize );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
qint64
|
||||||
|
PartitionSizeController::firstSector() const
|
||||||
|
{
|
||||||
|
return m_partition->firstSector();
|
||||||
|
}
|
||||||
|
|
||||||
|
qint64
|
||||||
|
PartitionSizeController::lastSector() const
|
||||||
|
{
|
||||||
|
return m_partition->lastSector();
|
||||||
|
}
|
||||||
|
@ -19,9 +19,14 @@
|
|||||||
#ifndef PARTITIONSIZECONTROLLER_H
|
#ifndef PARTITIONSIZECONTROLLER_H
|
||||||
#define PARTITIONSIZECONTROLLER_H
|
#define PARTITIONSIZECONTROLLER_H
|
||||||
|
|
||||||
|
// CalaPM
|
||||||
|
#include <core/partition.h>
|
||||||
|
|
||||||
|
// Qt
|
||||||
#include <QColor>
|
#include <QColor>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QPointer>
|
#include <QPointer>
|
||||||
|
#include <QScopedPointer>
|
||||||
|
|
||||||
class QSpinBox;
|
class QSpinBox;
|
||||||
|
|
||||||
@ -32,25 +37,33 @@ class PartResizerWidget;
|
|||||||
/**
|
/**
|
||||||
* Synchronizes a PartResizerWidget and a QSpinBox, making sure any change made
|
* Synchronizes a PartResizerWidget and a QSpinBox, making sure any change made
|
||||||
* to one is reflected in the other.
|
* 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
|
class PartitionSizeController : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit PartitionSizeController( QObject* parent = nullptr );
|
explicit PartitionSizeController( QObject* parent = nullptr );
|
||||||
void setPartResizerWidget( PartResizerWidget* widget );
|
|
||||||
void setSpinBox( QSpinBox* spinBox );
|
|
||||||
void init( Device* device, Partition* partition, const QColor& color );
|
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:
|
private:
|
||||||
QPointer< PartResizerWidget > m_partResizerWidget;
|
QPointer< PartResizerWidget > m_partResizerWidget;
|
||||||
QPointer< QSpinBox > m_spinBox;
|
QPointer< QSpinBox > m_spinBox;
|
||||||
Device* m_device = nullptr;
|
Device* m_device = nullptr;
|
||||||
Partition* m_partition = nullptr;
|
const Partition* m_originalPartition = nullptr;
|
||||||
|
QScopedPointer< Partition > m_partition;
|
||||||
QColor m_partitionColor;
|
QColor m_partitionColor;
|
||||||
|
|
||||||
bool m_updating = false;
|
bool m_updating = false;
|
||||||
|
|
||||||
void updateConnections();
|
void connectWidgets();
|
||||||
void doUpdateSpinBox();
|
void doUpdateSpinBox();
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
|
Loading…
Reference in New Issue
Block a user