From 2e173c183f3d131b592ab92df5ef480ecfefcb88 Mon Sep 17 00:00:00 2001
From: Teo Mrnjavac <teo@kde.org>
Date: Fri, 24 Jun 2016 16:04:48 +0200
Subject: [PATCH] Do not align partition boundaries unless explicitly
 requested.

---
 .../partition/gui/PartitionSizeController.cpp | 43 +++++++++++++++++--
 .../partition/gui/PartitionSizeController.h   |  5 +++
 2 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/src/modules/partition/gui/PartitionSizeController.cpp b/src/modules/partition/gui/PartitionSizeController.cpp
index b13b479b8e..7ecd627ecf 100644
--- a/src/modules/partition/gui/PartitionSizeController.cpp
+++ b/src/modules/partition/gui/PartitionSizeController.cpp
@@ -57,6 +57,8 @@ PartitionSizeController::setPartResizerWidget( PartResizerWidget* widget, bool f
     if ( m_partResizerWidget )
         disconnect( m_partResizerWidget, 0, this, 0 );
 
+    m_dirty = false;
+
     // 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.
@@ -84,7 +86,17 @@ PartitionSizeController::setPartResizerWidget( PartResizerWidget* widget, bool f
         // 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();
+        m_updating = true;
+
+        qint64 firstSector = m_partition->firstSector();
+        qint64 lastSector = m_partition->lastSector();
+
+        // This first time we call doAAUPRW with real first/last sector,
+        // all further calls will come from updatePartResizerWidget, and
+        // will therefore use values calculated from the SpinBox.
+        doAlignAndUpdatePartResizerWidget( firstSector, lastSector );
+
+        m_updating = false;
     }
 }
 
@@ -122,18 +134,35 @@ PartitionSizeController::updatePartResizerWidget()
 
     qint64 firstSector = m_partition->firstSector();
     qint64 lastSector = firstSector + sectorSize - 1;
+
+    doAlignAndUpdatePartResizerWidget( firstSector, lastSector );
+
+    m_updating = false;
+}
+
+void
+PartitionSizeController::doAlignAndUpdatePartResizerWidget( qint64 firstSector,
+                                                            qint64 lastSector )
+{
     if ( lastSector > m_partResizerWidget->maximumLastSector() )
     {
         qint64 delta = lastSector - m_partResizerWidget->maximumLastSector();
         firstSector -= delta;
         lastSector -= delta;
     }
-    m_partResizerWidget->updateLastSector( lastSector );
-    m_partResizerWidget->updateFirstSector( firstSector );
+    if ( lastSector != m_partition->lastSector() )
+    {
+        m_partResizerWidget->updateLastSector( lastSector );
+        m_dirty = true;
+    }
+    if ( firstSector != m_partition->firstSector() )
+    {
+        m_partResizerWidget->updateFirstSector( firstSector );
+        m_dirty = true;
+    }
 
     // Update spinbox value in case it was an impossible value
     doUpdateSpinBox();
-    m_updating = false;
 }
 
 void
@@ -166,3 +195,9 @@ PartitionSizeController::lastSector() const
 {
     return m_partition->lastSector();
 }
+
+bool
+PartitionSizeController::isDirty() const
+{
+    return m_dirty;
+}
diff --git a/src/modules/partition/gui/PartitionSizeController.h b/src/modules/partition/gui/PartitionSizeController.h
index 559fdbe474..a06ee63dc2 100644
--- a/src/modules/partition/gui/PartitionSizeController.h
+++ b/src/modules/partition/gui/PartitionSizeController.h
@@ -53,6 +53,8 @@ public:
     qint64 firstSector() const;
     qint64 lastSector() const;
 
+    bool isDirty() const;
+
 private:
     QPointer< PartResizerWidget > m_partResizerWidget;
     QPointer< QSpinBox > m_spinBox;
@@ -65,6 +67,9 @@ private:
 
     void connectWidgets();
     void doUpdateSpinBox();
+    void doAlignAndUpdatePartResizerWidget( qint64 fistSector, qint64 lastSector );
+
+    bool m_dirty = false;
 
 private Q_SLOTS:
     void updatePartResizerWidget();
-- 
GitLab