From a2cab7265012062dd1a4069d0a826e15acf43988 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20Walenciak?= <Kicer86@gmail.com>
Date: Tue, 21 Apr 2015 00:24:21 +0200
Subject: [PATCH] fix for QTBUG-1248

Change-Id: I09ab18f85dbed36531414a607e51e6715c1f9058
---
 src/widgets/itemviews/qheaderview.cpp | 83 ++++++++++++++++++++++++++++++++---
 src/widgets/itemviews/qheaderview.h   |  3 ++
 src/widgets/itemviews/qheaderview_p.h | 16 +++++--
 3 files changed, 92 insertions(+), 10 deletions(-)

diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp
index 66ff472..6859e33 100644
--- a/src/widgets/itemviews/qheaderview.cpp
+++ b/src/widgets/itemviews/qheaderview.cpp
@@ -580,7 +580,8 @@ int QHeaderView::sectionSizeHint(int logicalIndex) const
     else
         size = sectionSizeFromContents(logicalIndex);
     int hint = d->orientation == Qt::Horizontal ? size.width() : size.height();
-    return qBound(minimumSectionSize(), hint, maximumSectionSize());
+    const int visual = visualIndex(logicalIndex);
+    return qBound(d->headerSectionMinimumSize(visual), hint, d->headerSectionMaximumSize(visual));
 }
 
 /*!
@@ -1582,6 +1583,33 @@ void QHeaderView::setMinimumSectionSize(int size)
         d->maximumSectionSize = size;
 }
 
+
+void QHeaderView::setMinimumSectionSize(int logicalIndex, int size)
+{
+    Q_D(QHeaderView);
+    if (size < -1 || size > maxSizeSection)
+        return;
+
+    const int visual = visualIndex(logicalIndex);
+    Q_ASSERT(visual != -1);
+
+    d->setHeaderSectionMinimumSize(visual, size);
+}
+
+
+void QHeaderView::setMaximumSectionSize(int logicalIndex, int size)
+{
+    Q_D(QHeaderView);
+    if (size < -1 || size > maxSizeSection)
+        return;
+
+    const int visual = visualIndex(logicalIndex);
+    Q_ASSERT(visual != -1);
+
+    d->setHeaderSectionMaximumSize(visual, size);
+}
+
+
 /*!
     \since 5.2
     \property QHeaderView::maximumSectionSize
@@ -2411,7 +2439,8 @@ void QHeaderView::mouseMoveEvent(QMouseEvent *e)
                 d->cascadingResize(visual, d->headerSectionSize(visual) + delta);
             } else {
                 int delta = d->reverse() ? d->firstPos - pos : pos - d->firstPos;
-                int newsize = qBound(minimumSectionSize(), d->originalSize + delta, maximumSectionSize());
+                const int visual = visualIndex(d->section);
+                int newsize = qBound(d->headerSectionMinimumSize(visual), d->originalSize + delta, d->headerSectionMaximumSize(visual));
                 resizeSection(d->section, newsize);
             }
             d->lastPos = pos;
@@ -3235,8 +3264,8 @@ void QHeaderViewPrivate::resizeSections(QHeaderView::ResizeMode globalMode, bool
             int logicalIndex = q->logicalIndex(i);
             sectionSize = qMax(viewSectionSizeHint(logicalIndex),
                                q->sectionSizeHint(logicalIndex));
-            if (sectionSize > q->maximumSectionSize())
-                sectionSize = q->maximumSectionSize();
+            if (sectionSize > headerSectionMaximumSize(i))
+                sectionSize = headerSectionMaximumSize(i);
         }
         section_sizes.append(sectionSize);
         lengthToStretch -= sectionSize;
@@ -3371,8 +3400,7 @@ void QHeaderViewPrivate::flipSortIndicator(int section)
 
 void QHeaderViewPrivate::cascadingResize(int visual, int newSize)
 {
-    Q_Q(QHeaderView);
-    const int minimumSize = q->minimumSectionSize();
+    const int minimumSize = headerSectionMinimumSize(visual);
     const int oldSize = headerSectionSize(visual);
     int delta = newSize - oldSize;
 
@@ -3590,6 +3618,49 @@ void QHeaderViewPrivate::setGlobalHeaderResizeMode(QHeaderView::ResizeMode mode)
         sectionItems[i].resizeMode = mode;
 }
 
+
+void QHeaderViewPrivate::setHeaderSectionMinimumSize(int visual, int size)
+{
+    SectionItem& item = sectionItems[visual];
+    item.minimumSize = size;
+
+    if (item.maximumSize > -1 && item.maximumSize < size)
+        item.maximumSize = size;
+}
+
+
+int QHeaderViewPrivate::headerSectionMinimumSize(int visual) const
+{
+    Q_Q(const QHeaderView);
+    const SectionItem& item = sectionItems.at(visual);
+
+    const int size = item.minimumSize == -1? q->minimumSectionSize(): item.minimumSize;
+
+    return size;
+}
+
+
+void QHeaderViewPrivate::setHeaderSectionMaximumSize(int visual, int size)
+{
+    SectionItem& item = sectionItems[visual];
+    item.maximumSize = size;
+
+    if (item.minimumSize > -1 && item.minimumSize > size)
+        item.minimumSize = size;
+}
+
+
+int QHeaderViewPrivate::headerSectionMaximumSize(int visual) const
+{
+    Q_Q(const QHeaderView);
+    const SectionItem& item = sectionItems.at(visual);
+
+    const int size = item.maximumSize == -1? q->maximumSectionSize(): item.maximumSize;
+
+    return size;
+}
+
+
 int QHeaderViewPrivate::viewSectionSizeHint(int logical) const
 {
     if (QAbstractItemView *view = qobject_cast<QAbstractItemView*>(parent)) {
diff --git a/src/widgets/itemviews/qheaderview.h b/src/widgets/itemviews/qheaderview.h
index e0f5713..a9e48d6 100644
--- a/src/widgets/itemviews/qheaderview.h
+++ b/src/widgets/itemviews/qheaderview.h
@@ -160,8 +160,11 @@ public:
 
     int minimumSectionSize() const;
     void setMinimumSectionSize(int size);
+    void setMinimumSectionSize(int logicalIndex, int size);
+
     int maximumSectionSize() const;
     void setMaximumSectionSize(int size);
+    void setMaximumSectionSize(int logicalIndex, int size);
 
     Qt::Alignment defaultAlignment() const;
     void setDefaultAlignment(Qt::Alignment alignment);
diff --git a/src/widgets/itemviews/qheaderview_p.h b/src/widgets/itemviews/qheaderview_p.h
index 621c3c9..cebd61e 100644
--- a/src/widgets/itemviews/qheaderview_p.h
+++ b/src/widgets/itemviews/qheaderview_p.h
@@ -297,22 +297,25 @@ public:
         uint resizeMode : 5;  // (holding QHeaderView::ResizeMode)
         uint currentlyUnusedPadding : 6;
 
+        int minimumSize;
+        int maximumSize;
+
         union { // This union is made in order to save space and ensure good vector performance (on remove)
             mutable int calculated_startpos; // <- this is the primary used member.
             mutable int tmpLogIdx;         // When one of these 'tmp'-members has been used we call
             int tmpDataStreamSectionCount; // recalcSectionStartPos() or set sectionStartposRecalc to true
         };                                 // to ensure that calculated_startpos will be calculated afterwards.
 
-        inline SectionItem() : size(0), isHidden(0), resizeMode(QHeaderView::Interactive) {}
+        inline SectionItem() : size(0), isHidden(0), resizeMode(QHeaderView::Interactive), minimumSize(-1), maximumSize(-1) {}
         inline SectionItem(int length, QHeaderView::ResizeMode mode)
-            : size(length), isHidden(0), resizeMode(mode), calculated_startpos(-1) {}
+            : size(length), isHidden(0), resizeMode(mode), minimumSize(-1), maximumSize(-1), calculated_startpos(-1) {}
         inline int sectionSize() const { return size; }
         inline int calculatedEndPos() const { return calculated_startpos + size; }
 #ifndef QT_NO_DATASTREAM
         inline void write(QDataStream &out) const
-        { out << static_cast<int>(size); out << 1; out << (int)resizeMode; }
+        { out << static_cast<int>(size); out << 1; out << (int)resizeMode; out << minimumSize; out << maximumSize; }
         inline void read(QDataStream &in)
-        { int m; in >> m; size = m; in >> tmpDataStreamSectionCount; in >> m; resizeMode = m; }
+        { int m; in >> m; size = m; in >> tmpDataStreamSectionCount; in >> m; resizeMode = m; in >> minimumSize; in >> maximumSize; }
 #endif
     };
 
@@ -358,6 +361,11 @@ public:
     QHeaderView::ResizeMode headerSectionResizeMode(int visual) const;
     void setGlobalHeaderResizeMode(QHeaderView::ResizeMode mode);
 
+    void setHeaderSectionMinimumSize(int visual, int size);
+    int headerSectionMinimumSize(int visual) const;
+    void setHeaderSectionMaximumSize(int visual, int size);
+    int headerSectionMaximumSize(int visual) const;
+
     // other
     int viewSectionSizeHint(int logical) const;
     int adjustedVisualIndex(int visualIndex) const;
-- 
2.3.5

