Introduce PartitionSizeWidget to reduce duplication between dialogs

This commit is contained in:
Aurélien Gâteau 2014-08-04 19:26:16 +02:00
parent e482481262
commit 9ce55bfb83
9 changed files with 185 additions and 101 deletions

View File

@ -37,8 +37,9 @@ calamares_add_plugin( partition
PartitionJob.cpp
PartitionModel.cpp
PartitionPage.cpp
PartitionViewStep.cpp
PartitionPreview.cpp
PartitionSizeWidget.cpp
PartitionViewStep.cpp
PMUtils.cpp
UI
CreatePartitionDialog.ui

View File

@ -118,25 +118,7 @@ CreatePartitionDialog::createPartition()
);
}
qint64 lastSector;
int mbSize = m_ui->sizeSpinBox->value();
if ( mbSize == m_ui->sizeSpinBox->maximum() )
{
// If we are at the maximum value, select the last sector to avoid
// potential rounding errors which could leave a few sectors at the end
// unused
lastSector = m_maxSector;
}
else
{
lastSector = m_minSector + qint64( mbSize ) * 1024 * 1024 / m_device->logicalSectorSize();
Q_ASSERT( lastSector <= m_maxSector );
if ( lastSector > m_maxSector )
{
cDebug() << "lastSector (" << lastSector << ") > m_maxSector (" << m_maxSector << "). This should not happen!";
lastSector = m_maxSector;
}
}
PartitionSizeWidget::SectorRange range = m_ui->sizeSpinBox->sectorRange();
FileSystem::Type fsType = m_role.has( PartitionRole::Extended )
? FileSystem::Extended
@ -145,7 +127,7 @@ CreatePartitionDialog::createPartition()
m_parent,
*m_device,
m_role,
fsType, m_minSector, lastSector );
fsType, range.first, range.second );
PartitionInfo::setMountPoint( partition, m_ui->mountPointComboBox->currentText() );
PartitionInfo::setFormat( partition, true );
@ -165,21 +147,10 @@ CreatePartitionDialog::updateMountPointUi()
m_ui->mountPointComboBox->setEnabled( enabled );
}
void
CreatePartitionDialog::initSectorRange( Partition* partition )
{
PartitionTable* table = m_device->partitionTable();
m_minSector = partition->firstSector() - table->freeSectorsBefore( *partition );
m_maxSector = partition->lastSector() + table->freeSectorsAfter( *partition );
m_ui->sizeSpinBox->setMaximum( mbSizeForSectorRange( m_minSector, m_maxSector ) );
m_ui->sizeSpinBox->setValue( m_ui->sizeSpinBox->maximum() );
}
void
CreatePartitionDialog::initFromFreeSpace( Partition* freeSpacePartition )
{
initSectorRange( freeSpacePartition );
m_ui->sizeSpinBox->init( m_device, freeSpacePartition );
}
void
@ -195,10 +166,7 @@ CreatePartitionDialog::initFromPartitionToCreate( Partition* partition )
return;
}
initSectorRange( partition );
// Size
m_ui->sizeSpinBox->setValue( mbSizeForSectorRange( partition->firstSector(), partition->lastSector() ) );
m_ui->sizeSpinBox->init( m_device, partition );
// File System
FileSystem::Type fsType = partition->fileSystem().type();
@ -209,9 +177,3 @@ CreatePartitionDialog::initFromPartitionToCreate( Partition* partition )
updateMountPointUi();
}
qint64
CreatePartitionDialog::mbSizeForSectorRange( qint64 first, qint64 last ) const
{
return ( last - first + 1 ) * m_device->logicalSectorSize() / 1024 / 1024;
}

View File

@ -47,16 +47,12 @@ private Q_SLOTS:
private:
QScopedPointer< Ui_CreatePartitionDialog > m_ui;
Device* m_device;
qint64 m_minSector = 0;
qint64 m_maxSector = 0;
PartitionNode* m_parent;
PartitionRole m_role = PartitionRole( PartitionRole::None );
void initGptPartitionTypeUi();
void initMbrPartitionTypeUi();
void initSectorRange( Partition* );
qint64 mbSizeForSectorRange( qint64 first, qint64 last ) const;
};
#endif /* CREATEPARTITIONDIALOG_H */

View File

@ -91,7 +91,7 @@
</widget>
</item>
<item row="3" column="1">
<widget class="QSpinBox" name="sizeSpinBox">
<widget class="PartitionSizeWidget" name="sizeSpinBox">
<property name="suffix">
<string> MB</string>
</property>
@ -203,6 +203,13 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>PartitionSizeWidget</class>
<extends>QSpinBox</extends>
<header location="global">PartitionSizeWidget.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>primaryRadioButton</tabstop>
<tabstop>fsComboBox</tabstop>

View File

@ -38,66 +38,21 @@ EditExistingPartitionDialog::EditExistingPartitionDialog( Device* device, Partit
, m_partition( partition )
{
m_ui->setupUi( this );
PartitionTable* table = m_device->partitionTable();
qint64 minSector = partition->firstSector() - table->freeSectorsBefore( *partition );
qint64 maxSector = partition->lastSector() + table->freeSectorsAfter( *partition );
m_ui->sizeSpinBox->setMaximum( mbSizeForSectorRange( minSector, maxSector ) );
m_ui->sizeSpinBox->setValue( mbSizeForSectorRange( partition->firstSector(), partition->lastSector() ) );
// Mount point
m_ui->sizeSpinBox->init( device, partition );
m_ui->mountPointComboBox->setCurrentText( PartitionInfo::mountPoint( partition ) );
}
EditExistingPartitionDialog::~EditExistingPartitionDialog()
{}
qint64
EditExistingPartitionDialog::mbSizeForSectorRange( qint64 first, qint64 last ) const
{
return ( last - first + 1 ) * m_device->logicalSectorSize() / 1024 / 1024;
}
void
EditExistingPartitionDialog::applyChanges( PartitionCoreModule* core )
{
PartitionInfo::setMountPoint( m_partition, m_ui->mountPointComboBox->currentText() );
qint64 oldSize = mbSizeForSectorRange( m_partition->firstSector(), m_partition->lastSector() );
qint64 newSize = m_ui->sizeSpinBox->value();
if ( oldSize == newSize )
if ( m_ui->sizeSpinBox->isDirty() )
{
if ( m_ui->formatRadioButton->isChecked() )
core->formatPartition( m_device, m_partition );
else
core->refreshPartition( m_device, m_partition );
}
else
{
// FIXME: Duplicated from CreatePartitionDialog
qint64 maxSector = m_partition->lastSector() + m_device->partitionTable()->freeSectorsAfter( *m_partition );
qint64 lastSector;
int mbSize = m_ui->sizeSpinBox->value();
if ( mbSize == m_ui->sizeSpinBox->maximum() )
{
// If we are at the maximum value, select the last sector to avoid
// potential rounding errors which could leave a few sectors at the end
// unused
lastSector = maxSector;
}
else
{
lastSector = m_partition->firstSector() + qint64( mbSize ) * 1024 * 1024 / m_device->logicalSectorSize();
Q_ASSERT( lastSector <= maxSector );
if ( lastSector > maxSector )
{
cDebug() << "lastSector (" << lastSector << ") > maxSector (" << maxSector << "). This should not happen!";
lastSector = maxSector;
}
}
PartitionSizeWidget::SectorRange range = m_ui->sizeSpinBox->sectorRange();
if ( m_ui->formatRadioButton->isChecked() )
{
Partition* newPartition = PMUtils::createNewPartition(
@ -105,8 +60,8 @@ EditExistingPartitionDialog::applyChanges( PartitionCoreModule* core )
*m_device,
m_partition->roles(),
m_partition->fileSystem().type(),
m_partition->firstSector(),
lastSector);
range.first,
range.second);
PartitionInfo::setMountPoint( newPartition, PartitionInfo::mountPoint( m_partition ) );
PartitionInfo::setFormat( newPartition, true );
@ -118,4 +73,12 @@ EditExistingPartitionDialog::applyChanges( PartitionCoreModule* core )
//core->resizePartition( m_device, m_partition );
}
}
else
{
// No size changes
if ( m_ui->formatRadioButton->isChecked() )
core->formatPartition( m_device, m_partition );
else
core->refreshPartition( m_device, m_partition );
}
}

View File

@ -40,8 +40,6 @@ private:
QScopedPointer< Ui_EditExistingPartitionDialog > m_ui;
Device* m_device;
Partition* m_partition;
qint64 mbSizeForSectorRange( qint64 first, qint64 last ) const;
};
#endif /* EDITEXISTINGPARTITIONDIALOG_H */

View File

@ -36,7 +36,7 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="sizeSpinBox">
<widget class="PartitionSizeWidget" name="sizeSpinBox">
<property name="suffix">
<string> MB</string>
</property>
@ -143,6 +143,13 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>PartitionSizeWidget</class>
<extends>QSpinBox</extends>
<header location="global">PartitionSizeWidget.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>sizeSpinBox</tabstop>
<tabstop>keepRadioButton</tabstop>

View File

@ -0,0 +1,100 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Calamares is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#include <PartitionSizeWidget.h>
#include <utils/Logger.h>
// CalaPM
#include <core/device.h>
#include <core/partition.h>
#include <core/partitiontable.h>
PartitionSizeWidget::PartitionSizeWidget( QWidget* parent )
: QSpinBox( parent )
{
}
void
PartitionSizeWidget::init( Device* device, Partition* partition )
{
m_device = device;
m_partition = partition;
Q_ASSERT( m_device->partitionTable() );
qint64 minSector = computeMinSector();
qint64 maxSector = computeMaxSector();
cLog() << minSector << maxSector;
setMaximum( mbSizeForSectorRange( minSector, maxSector ) );
m_initialValue = mbSizeForSectorRange( partition->firstSector(), partition->lastSector() );
setValue( m_initialValue );
}
PartitionSizeWidget::SectorRange
PartitionSizeWidget::sectorRange() const
{
qint64 minSector = computeMinSector();
qint64 maxSector = computeMaxSector();
int mbSize = value();
if ( mbSize == maximum() )
{
// If we are at the maximum value, select the last sector to avoid
// potential rounding errors which could leave a few sectors at the end
// unused
return SectorRange( minSector, maxSector );
}
qint64 lastSector = minSector + qint64( mbSize ) * 1024 * 1024 / m_device->logicalSectorSize();
Q_ASSERT( lastSector <= maxSector );
if ( lastSector > maxSector )
{
cLog() << "lastSector (" << lastSector << ") > maxSector (" << maxSector << "). This should not happen!";
lastSector = maxSector;
}
return SectorRange( minSector, lastSector );
}
bool
PartitionSizeWidget::isDirty() const
{
return m_initialValue != value();
}
qint64
PartitionSizeWidget::mbSizeForSectorRange( qint64 first, qint64 last ) const
{
return ( last - first + 1 ) * m_device->logicalSectorSize() / 1024 / 1024;
}
qint64
PartitionSizeWidget::computeMaxSector() const
{
Q_ASSERT( m_device );
Q_ASSERT( m_partition );
return m_partition->lastSector() + m_device->partitionTable()->freeSectorsAfter( *m_partition );
}
qint64
PartitionSizeWidget::computeMinSector() const
{
Q_ASSERT( m_device );
Q_ASSERT( m_partition );
return m_partition->firstSector() - m_device->partitionTable()->freeSectorsBefore( *m_partition );
}

View File

@ -0,0 +1,50 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Calamares is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PARTITIONSIZEWIDGET_H
#define PARTITIONSIZEWIDGET_H
#include <QSpinBox>
class Device;
class Partition;
class PartitionSizeWidget : public QSpinBox
{
public:
typedef QPair< qint64, qint64 > SectorRange;
explicit PartitionSizeWidget( QWidget* parent = nullptr );
void init( Device* device, Partition* partition );
SectorRange sectorRange() const;
bool isDirty() const;
private:
Device* m_device = nullptr;
Partition* m_partition = nullptr;
int m_initialValue;
qint64 mbSizeForSectorRange( qint64 first, qint64 last ) const;
qint64 computeMinSector() const;
qint64 computeMaxSector() const;
};
#endif /* PARTITIONSIZEWIDGET_H */