diff --git a/src/core/partition.h b/src/core/partition.h index fa034b6..96c3447 100644 --- a/src/core/partition.h +++ b/src/core/partition.h @@ -41,6 +41,7 @@ class InsertDialog; class NewDialog; class EditMountPointDialog; class PartPropsDialog; +class SizeDialogBase; class CreateFileSystemOperation; class RestoreOperation; @@ -86,6 +87,7 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT Partition : public PartitionNode friend class NewDialog; friend class EditMountPointDialog; friend class PartPropsDialog; + friend class SizeDialogBase; friend class CreateFileSystemOperation; friend class RestoreOperation; diff --git a/src/gui/newdialog.cpp b/src/gui/newdialog.cpp index 2c7d0fb..54b57ee 100644 --- a/src/gui/newdialog.cpp +++ b/src/gui/newdialog.cpp @@ -155,7 +155,7 @@ void NewDialog::onFilesystemChanged(int idx) setupConstraints(); - dialogWidget().partResizerWidget().updateLength(partition().length()); + updateLength(partition().length()); dialogWidget().partResizerWidget().update(); updateHideAndShow(); diff --git a/src/gui/partresizerwidget.cpp b/src/gui/partresizerwidget.cpp index e0289fd..302ae8a 100644 --- a/src/gui/partresizerwidget.cpp +++ b/src/gui/partresizerwidget.cpp @@ -410,69 +410,6 @@ bool PartResizerWidget::updateLastSector(qint64 newLastSector) return false; } -/** Updates the Partition's length - @param newLength the new length - @return true on success -*/ -bool PartResizerWidget::updateLength(qint64 newLength) -{ - newLength = qBound(minimumLength(), newLength, qMin(totalSectors(), maximumLength())); - - if (newLength == partition().length()) - return false; - - const qint64 oldLength = partition().length(); - qint64 delta = newLength - oldLength; - - qint64 tmp = qMin(delta, maximumLastSector(align()) - partition().lastSector()); - delta -= tmp; - - if (tmp != 0) - { - qint64 newLastSector = partition().lastSector() + tmp; - - if (align()) - newLastSector = PartitionAlignment::alignedLastSector(device(), partition(), newLastSector, minimumLastSector(align()), maximumLastSector(align()), minimumLength(), maximumLength()); - - if (newLastSector != partition().lastSector()) - { - partition().setLastSector(newLastSector); - partition().fileSystem().setLastSector(newLastSector); - - emit lastSectorChanged(partition().lastSector()); - } - } - - tmp = qMin(delta, partition().firstSector() - minimumFirstSector(align())); - delta -= tmp; - - if (tmp != 0) - { - qint64 newFirstSector = partition().firstSector() - tmp; - - if (align()) - newFirstSector = PartitionAlignment::alignedFirstSector(device(), partition(), newFirstSector, minimumFirstSector(align()), maximumFirstSector(align()), minimumLength(), maximumLength()); - - if (newFirstSector != partition().firstSector()) - { - partition().setFirstSector(newFirstSector); - partition().fileSystem().setFirstSector(newFirstSector); - - emit firstSectorChanged(partition().firstSector()); - } - } - - if (partition().length() != oldLength) - { - emit lengthChanged(partition().length()); - updatePositions(); - - return true; - } - - return false; -} - /** Sets the minimum sectors the Partition can be long. @note This value can never be less than 0 and never be higher than totalSectors() @param s the new minimum length diff --git a/src/gui/partresizerwidget.h b/src/gui/partresizerwidget.h index 8ac53b4..adc8162 100644 --- a/src/gui/partresizerwidget.h +++ b/src/gui/partresizerwidget.h @@ -85,10 +85,8 @@ class PartResizerWidget : public QWidget signals: void firstSectorChanged(qint64); void lastSectorChanged(qint64); - void lengthChanged(qint64); public slots: - bool updateLength(qint64 newLength); bool updateFirstSector(qint64 newFirstSector); bool updateLastSector(qint64 newLastSector); bool movePartition(qint64 newFirstSector); diff --git a/src/gui/sizedetailswidget.h b/src/gui/sizedetailswidget.h index 5824fe8..f9673fa 100644 --- a/src/gui/sizedetailswidget.h +++ b/src/gui/sizedetailswidget.h @@ -38,8 +38,13 @@ class SizeDetailsWidget : public QWidget, public Ui::SizeDetailsWidgetBase public: QDoubleSpinBox& spinFirstSector() { Q_ASSERT(m_SpinFirstSector); return *m_SpinFirstSector; } + const QDoubleSpinBox& spinFirstSector() const { Q_ASSERT(m_SpinFirstSector); return *m_SpinFirstSector; } + QDoubleSpinBox& spinLastSector() { Q_ASSERT(m_SpinLastSector); return *m_SpinLastSector; } + const QDoubleSpinBox& spinLastSector() const { Q_ASSERT(m_SpinLastSector); return *m_SpinLastSector; } + QCheckBox& checkAlign() { Q_ASSERT(m_CheckAlign); return *m_CheckAlign; } + const QCheckBox& checkAlign() const { Q_ASSERT(m_CheckAlign); return *m_CheckAlign; } }; #endif diff --git a/src/gui/sizedialogbase.cpp b/src/gui/sizedialogbase.cpp index 6c3bf8e..a5e0017 100644 --- a/src/gui/sizedialogbase.cpp +++ b/src/gui/sizedialogbase.cpp @@ -111,8 +111,8 @@ void SizeDialogBase::setupConstraints() if (!canShrink() && !canGrow()) dialogWidget().spinCapacity().setEnabled(false); - dialogWidget().partResizerWidget().setMaximumFirstSector(partition().maxFirstSector()); - dialogWidget().partResizerWidget().setMinimumLastSector(partition().minLastSector()); + dialogWidget().partResizerWidget().setMaximumFirstSector(maximumFirstSector()); + dialogWidget().partResizerWidget().setMinimumLastSector(minimumLastSector()); const qint64 totalCapacity = sectorsToDialogUnit(partition(), maximumLastSector() - minimumFirstSector() + 1); @@ -128,7 +128,7 @@ void SizeDialogBase::setupConstraints() detailsWidget().spinFirstSector().setRange(minimumFirstSector(), maximumLastSector()); detailsWidget().spinLastSector().setRange(minimumFirstSector(), maximumLastSector()); - onAlignToggled(detailsWidget().checkAlign().isChecked()); + onAlignToggled(align()); } void SizeDialogBase::setupConnections() @@ -212,17 +212,56 @@ void SizeDialogBase::updateLength(qint64 newLength) dialogWidget().spinCapacity().blockSignals(state); } +/** Updates the Partition's length + @param newLength the new length + @return true on success +*/ void SizeDialogBase::onCapacityChanged(double newCapacity) { - const qint64 newLength = dialogUnitToSectors(partition(), newCapacity); - dialogWidget().partResizerWidget().updateLength(newLength); + qint64 newLength = dialogUnitToSectors(partition(), newCapacity); + + newLength = qBound(minimumLength(), newLength, qMin(maximumLastSector() - minimumFirstSector() + 1, maximumLength())); + + if (newLength == partition().length()) + return; + + const qint64 oldLength = partition().length(); +// const qint64 oldCapacity = sectorsToDialogUnit(partition(), oldLength); + + qint64 delta = newLength - oldLength; + + qint64 tmp = qMin(delta, maximumLastSector() - partition().lastSector()); + delta -= tmp; + + if (tmp != 0) + { + qint64 newLastSector = partition().lastSector() + tmp; + + if (align()) + newLastSector = PartitionAlignment::alignedLastSector(device(), partition(), newLastSector, minimumLastSector(), maximumLastSector(), minimumLength(), maximumLength()); + + dialogWidget().partResizerWidget().updateLastSector(newLastSector); + } + + tmp = qMin(delta, partition().firstSector() - minimumFirstSector()); + delta -= tmp; + + if (tmp != 0) + { + qint64 newFirstSector = partition().firstSector() - tmp; + + if (align()) + newFirstSector = PartitionAlignment::alignedFirstSector(device(), partition(), newFirstSector, minimumFirstSector(), maximumFirstSector(), minimumLength(), maximumLength()); + + dialogWidget().partResizerWidget().updateFirstSector(newFirstSector); + } } void SizeDialogBase::onFreeSpaceBeforeChanged(double newBefore) { qint64 newFirstSector = minimumFirstSector() + dialogUnitToSectors(partition(), newBefore); - if (detailsWidget().checkAlign().isChecked()) + if (align()) { const qint64 oldBefore = sectorsToDialogUnit(partition(), partition().firstSector() - minimumFirstSector()); const qint64 deltaCorrection = newBefore > oldBefore ? PartitionAlignment::firstDelta(device(), partition(), newFirstSector) : 0; @@ -232,20 +271,13 @@ void SizeDialogBase::onFreeSpaceBeforeChanged(double newBefore) if (dialogWidget().partResizerWidget().movePartition(newFirstSector) || dialogWidget().partResizerWidget().updateFirstSector(newFirstSector)) setDirty(); - - // In most cases the capacity free before the partition's first sector that we calculated - // above from newBefore will not exactly equal newBefore (due to rounding errors). So - // make sure to set the spin box to the actual value. - const bool state = dialogWidget().spinFreeBefore().blockSignals(true); - dialogWidget().spinFreeBefore().setValue(sectorsToDialogUnit(partition(), partition().firstSector() - minimumFirstSector())); - dialogWidget().spinFreeBefore().blockSignals(state); } void SizeDialogBase::onFreeSpaceAfterChanged(double newAfter) { qint64 newLastSector = maximumLastSector() - dialogUnitToSectors(partition(), newAfter); - if (detailsWidget().checkAlign().isChecked()) + if (align()) { const double oldAfter = sectorsToDialogUnit(partition(), maximumLastSector() - partition().lastSector()); const qint64 deltaCorrection = newAfter > oldAfter ? PartitionAlignment::lastDelta(device(), partition(), newLastSector) : 0; @@ -257,10 +289,6 @@ void SizeDialogBase::onFreeSpaceAfterChanged(double newAfter) if (dialogWidget().partResizerWidget().movePartition(newFirstSector) || dialogWidget().partResizerWidget().updateLastSector(newLastSector)) setDirty(); - - const bool state = dialogWidget().spinFreeAfter().blockSignals(true); - dialogWidget().spinFreeAfter().setValue(sectorsToDialogUnit(partition(), maximumLastSector() - partition().lastSector())); - dialogWidget().spinFreeAfter().blockSignals(state); } const PartitionTable& SizeDialogBase::partitionTable() const @@ -269,3 +297,19 @@ const PartitionTable& SizeDialogBase::partitionTable() const return *device().partitionTable(); } + +bool SizeDialogBase::align() const +{ + return detailsWidget().checkAlign().isChecked(); +} + +qint64 SizeDialogBase::minimumLastSector() const +{ + return partition().minLastSector(); +} + +qint64 SizeDialogBase::maximumFirstSector() const +{ + return partition().maxFirstSector(); +} + diff --git a/src/gui/sizedialogbase.h b/src/gui/sizedialogbase.h index dcdd54c..5e04eb7 100644 --- a/src/gui/sizedialogbase.h +++ b/src/gui/sizedialogbase.h @@ -70,6 +70,10 @@ class SizeDialogBase : public KDialog virtual void setDirty() {} virtual void updateLength(qint64 newLength); + virtual bool align() const; + virtual qint64 minimumLastSector() const; + virtual qint64 maximumFirstSector() const; + protected slots: void onFirstSectorChanged(qint64 newFirst); void onLastSectorChanged(qint64 newLast);