Introduce PartitionSizeWidget to reduce duplication between dialogs
This commit is contained in:
parent
e482481262
commit
9ce55bfb83
@ -37,8 +37,9 @@ calamares_add_plugin( partition
|
|||||||
PartitionJob.cpp
|
PartitionJob.cpp
|
||||||
PartitionModel.cpp
|
PartitionModel.cpp
|
||||||
PartitionPage.cpp
|
PartitionPage.cpp
|
||||||
PartitionViewStep.cpp
|
|
||||||
PartitionPreview.cpp
|
PartitionPreview.cpp
|
||||||
|
PartitionSizeWidget.cpp
|
||||||
|
PartitionViewStep.cpp
|
||||||
PMUtils.cpp
|
PMUtils.cpp
|
||||||
UI
|
UI
|
||||||
CreatePartitionDialog.ui
|
CreatePartitionDialog.ui
|
||||||
|
@ -118,25 +118,7 @@ CreatePartitionDialog::createPartition()
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
qint64 lastSector;
|
PartitionSizeWidget::SectorRange range = m_ui->sizeSpinBox->sectorRange();
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
FileSystem::Type fsType = m_role.has( PartitionRole::Extended )
|
FileSystem::Type fsType = m_role.has( PartitionRole::Extended )
|
||||||
? FileSystem::Extended
|
? FileSystem::Extended
|
||||||
@ -145,7 +127,7 @@ CreatePartitionDialog::createPartition()
|
|||||||
m_parent,
|
m_parent,
|
||||||
*m_device,
|
*m_device,
|
||||||
m_role,
|
m_role,
|
||||||
fsType, m_minSector, lastSector );
|
fsType, range.first, range.second );
|
||||||
|
|
||||||
PartitionInfo::setMountPoint( partition, m_ui->mountPointComboBox->currentText() );
|
PartitionInfo::setMountPoint( partition, m_ui->mountPointComboBox->currentText() );
|
||||||
PartitionInfo::setFormat( partition, true );
|
PartitionInfo::setFormat( partition, true );
|
||||||
@ -165,21 +147,10 @@ CreatePartitionDialog::updateMountPointUi()
|
|||||||
m_ui->mountPointComboBox->setEnabled( enabled );
|
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
|
void
|
||||||
CreatePartitionDialog::initFromFreeSpace( Partition* freeSpacePartition )
|
CreatePartitionDialog::initFromFreeSpace( Partition* freeSpacePartition )
|
||||||
{
|
{
|
||||||
initSectorRange( freeSpacePartition );
|
m_ui->sizeSpinBox->init( m_device, freeSpacePartition );
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -195,10 +166,7 @@ CreatePartitionDialog::initFromPartitionToCreate( Partition* partition )
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
initSectorRange( partition );
|
m_ui->sizeSpinBox->init( m_device, partition );
|
||||||
|
|
||||||
// Size
|
|
||||||
m_ui->sizeSpinBox->setValue( mbSizeForSectorRange( partition->firstSector(), partition->lastSector() ) );
|
|
||||||
|
|
||||||
// File System
|
// File System
|
||||||
FileSystem::Type fsType = partition->fileSystem().type();
|
FileSystem::Type fsType = partition->fileSystem().type();
|
||||||
@ -209,9 +177,3 @@ CreatePartitionDialog::initFromPartitionToCreate( Partition* partition )
|
|||||||
|
|
||||||
updateMountPointUi();
|
updateMountPointUi();
|
||||||
}
|
}
|
||||||
|
|
||||||
qint64
|
|
||||||
CreatePartitionDialog::mbSizeForSectorRange( qint64 first, qint64 last ) const
|
|
||||||
{
|
|
||||||
return ( last - first + 1 ) * m_device->logicalSectorSize() / 1024 / 1024;
|
|
||||||
}
|
|
||||||
|
@ -47,16 +47,12 @@ private Q_SLOTS:
|
|||||||
private:
|
private:
|
||||||
QScopedPointer< Ui_CreatePartitionDialog > m_ui;
|
QScopedPointer< Ui_CreatePartitionDialog > m_ui;
|
||||||
Device* m_device;
|
Device* m_device;
|
||||||
qint64 m_minSector = 0;
|
|
||||||
qint64 m_maxSector = 0;
|
|
||||||
PartitionNode* m_parent;
|
PartitionNode* m_parent;
|
||||||
PartitionRole m_role = PartitionRole( PartitionRole::None );
|
PartitionRole m_role = PartitionRole( PartitionRole::None );
|
||||||
|
|
||||||
void initGptPartitionTypeUi();
|
void initGptPartitionTypeUi();
|
||||||
void initMbrPartitionTypeUi();
|
void initMbrPartitionTypeUi();
|
||||||
void initSectorRange( Partition* );
|
void initSectorRange( Partition* );
|
||||||
|
|
||||||
qint64 mbSizeForSectorRange( qint64 first, qint64 last ) const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* CREATEPARTITIONDIALOG_H */
|
#endif /* CREATEPARTITIONDIALOG_H */
|
||||||
|
@ -91,7 +91,7 @@
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="3" column="1">
|
<item row="3" column="1">
|
||||||
<widget class="QSpinBox" name="sizeSpinBox">
|
<widget class="PartitionSizeWidget" name="sizeSpinBox">
|
||||||
<property name="suffix">
|
<property name="suffix">
|
||||||
<string> MB</string>
|
<string> MB</string>
|
||||||
</property>
|
</property>
|
||||||
@ -203,6 +203,13 @@
|
|||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
|
<customwidgets>
|
||||||
|
<customwidget>
|
||||||
|
<class>PartitionSizeWidget</class>
|
||||||
|
<extends>QSpinBox</extends>
|
||||||
|
<header location="global">PartitionSizeWidget.h</header>
|
||||||
|
</customwidget>
|
||||||
|
</customwidgets>
|
||||||
<tabstops>
|
<tabstops>
|
||||||
<tabstop>primaryRadioButton</tabstop>
|
<tabstop>primaryRadioButton</tabstop>
|
||||||
<tabstop>fsComboBox</tabstop>
|
<tabstop>fsComboBox</tabstop>
|
||||||
|
@ -38,66 +38,21 @@ EditExistingPartitionDialog::EditExistingPartitionDialog( Device* device, Partit
|
|||||||
, m_partition( partition )
|
, m_partition( partition )
|
||||||
{
|
{
|
||||||
m_ui->setupUi( this );
|
m_ui->setupUi( this );
|
||||||
|
m_ui->sizeSpinBox->init( device, partition );
|
||||||
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->mountPointComboBox->setCurrentText( PartitionInfo::mountPoint( partition ) );
|
m_ui->mountPointComboBox->setCurrentText( PartitionInfo::mountPoint( partition ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
EditExistingPartitionDialog::~EditExistingPartitionDialog()
|
EditExistingPartitionDialog::~EditExistingPartitionDialog()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
qint64
|
|
||||||
EditExistingPartitionDialog::mbSizeForSectorRange( qint64 first, qint64 last ) const
|
|
||||||
{
|
|
||||||
return ( last - first + 1 ) * m_device->logicalSectorSize() / 1024 / 1024;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
EditExistingPartitionDialog::applyChanges( PartitionCoreModule* core )
|
EditExistingPartitionDialog::applyChanges( PartitionCoreModule* core )
|
||||||
{
|
{
|
||||||
PartitionInfo::setMountPoint( m_partition, m_ui->mountPointComboBox->currentText() );
|
PartitionInfo::setMountPoint( m_partition, m_ui->mountPointComboBox->currentText() );
|
||||||
|
|
||||||
qint64 oldSize = mbSizeForSectorRange( m_partition->firstSector(), m_partition->lastSector() );
|
if ( m_ui->sizeSpinBox->isDirty() )
|
||||||
qint64 newSize = m_ui->sizeSpinBox->value();
|
|
||||||
if ( oldSize == newSize )
|
|
||||||
{
|
{
|
||||||
if ( m_ui->formatRadioButton->isChecked() )
|
PartitionSizeWidget::SectorRange range = m_ui->sizeSpinBox->sectorRange();
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( m_ui->formatRadioButton->isChecked() )
|
if ( m_ui->formatRadioButton->isChecked() )
|
||||||
{
|
{
|
||||||
Partition* newPartition = PMUtils::createNewPartition(
|
Partition* newPartition = PMUtils::createNewPartition(
|
||||||
@ -105,8 +60,8 @@ EditExistingPartitionDialog::applyChanges( PartitionCoreModule* core )
|
|||||||
*m_device,
|
*m_device,
|
||||||
m_partition->roles(),
|
m_partition->roles(),
|
||||||
m_partition->fileSystem().type(),
|
m_partition->fileSystem().type(),
|
||||||
m_partition->firstSector(),
|
range.first,
|
||||||
lastSector);
|
range.second);
|
||||||
PartitionInfo::setMountPoint( newPartition, PartitionInfo::mountPoint( m_partition ) );
|
PartitionInfo::setMountPoint( newPartition, PartitionInfo::mountPoint( m_partition ) );
|
||||||
PartitionInfo::setFormat( newPartition, true );
|
PartitionInfo::setFormat( newPartition, true );
|
||||||
|
|
||||||
@ -118,4 +73,12 @@ EditExistingPartitionDialog::applyChanges( PartitionCoreModule* core )
|
|||||||
//core->resizePartition( m_device, m_partition );
|
//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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,8 +40,6 @@ private:
|
|||||||
QScopedPointer< Ui_EditExistingPartitionDialog > m_ui;
|
QScopedPointer< Ui_EditExistingPartitionDialog > m_ui;
|
||||||
Device* m_device;
|
Device* m_device;
|
||||||
Partition* m_partition;
|
Partition* m_partition;
|
||||||
|
|
||||||
qint64 mbSizeForSectorRange( qint64 first, qint64 last ) const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* EDITEXISTINGPARTITIONDIALOG_H */
|
#endif /* EDITEXISTINGPARTITIONDIALOG_H */
|
||||||
|
@ -36,7 +36,7 @@
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="0" column="1">
|
<item row="0" column="1">
|
||||||
<widget class="QSpinBox" name="sizeSpinBox">
|
<widget class="PartitionSizeWidget" name="sizeSpinBox">
|
||||||
<property name="suffix">
|
<property name="suffix">
|
||||||
<string> MB</string>
|
<string> MB</string>
|
||||||
</property>
|
</property>
|
||||||
@ -143,6 +143,13 @@
|
|||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
|
<customwidgets>
|
||||||
|
<customwidget>
|
||||||
|
<class>PartitionSizeWidget</class>
|
||||||
|
<extends>QSpinBox</extends>
|
||||||
|
<header location="global">PartitionSizeWidget.h</header>
|
||||||
|
</customwidget>
|
||||||
|
</customwidgets>
|
||||||
<tabstops>
|
<tabstops>
|
||||||
<tabstop>sizeSpinBox</tabstop>
|
<tabstop>sizeSpinBox</tabstop>
|
||||||
<tabstop>keepRadioButton</tabstop>
|
<tabstop>keepRadioButton</tabstop>
|
||||||
|
100
src/modules/partition/PartitionSizeWidget.cpp
Normal file
100
src/modules/partition/PartitionSizeWidget.cpp
Normal 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 );
|
||||||
|
}
|
50
src/modules/partition/PartitionSizeWidget.h
Normal file
50
src/modules/partition/PartitionSizeWidget.h
Normal 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 */
|
Loading…
Reference in New Issue
Block a user