src/gui/itemviews/qheaderview.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
child 33 3e2da88830cd
--- a/src/gui/itemviews/qheaderview.cpp	Wed Jun 23 19:07:03 2010 +0300
+++ b/src/gui/itemviews/qheaderview.cpp	Tue Jul 06 15:10:48 2010 +0300
@@ -1698,13 +1698,10 @@
     if (!d->sectionHidden.isEmpty()) {
         QBitArray sectionHidden(d->sectionHidden);
         sectionHidden.resize(sectionHidden.count() + insertCount);
-        //sectionHidden.fill(false, logicalFirst, logicalLast + 1);
-        for (int i = logicalFirst; i <= logicalLast; ++i)
-            // visual == logical in this range (see previous block)
-            sectionHidden.setBit(i, false);
+        sectionHidden.fill(false, logicalFirst, logicalLast + 1);
         for (int j = logicalLast + 1; j < sectionHidden.count(); ++j)
-            sectionHidden.setBit(d->visualIndex(j),
-                    d->sectionHidden.testBit(d->visualIndex(j - insertCount)));
+            //here we simply copy the old sectionHidden
+            sectionHidden.setBit(j, d->sectionHidden.testBit(j - insertCount));
         d->sectionHidden = sectionHidden;
     }
 
@@ -1853,11 +1850,9 @@
         persistentHiddenSections.clear();
         return;
     }
+
+    QBitArray oldSectionHidden = sectionHidden;
     bool sectionCountChanged = false;
-    for (int i = 0; i < sectionHidden.count(); ++i) {
-        if (sectionHidden.testBit(i))
-            q->setSectionHidden(logicalIndex(i), false);
-    }
 
     for (int i = 0; i < persistentHiddenSections.count(); ++i) {
         QModelIndex index = persistentHiddenSections.at(i);
@@ -1866,6 +1861,7 @@
                                  ? index.column()
                                  : index.row());
             q->setSectionHidden(logical, true);
+            oldSectionHidden.setBit(logical, false);
         } else if (!sectionCountChanged && (modelSectionCount() != sectionCount)) {
             sectionCountChanged = true;
             break;
@@ -1873,6 +1869,11 @@
     }
     persistentHiddenSections.clear();
 
+    for (int i = 0; i < oldSectionHidden.count(); ++i) {
+        if (oldSectionHidden.testBit(i))
+            q->setSectionHidden(logicalIndex(i), false);
+    }
+
     // the number of sections changed; we need to reread the state of the model
     if (sectionCountChanged)
         q->initializeSections();
@@ -2033,7 +2034,7 @@
                 updateSection(d->hover);
         }
         break; }
-    case QEvent::Timer: { // ### reimplement timerEvent() instead ?
+    case QEvent::Timer: {
         QTimerEvent *te = static_cast<QTimerEvent*>(e);
         if (te->timerId() == d->delayedResize.timerId()) {
             d->delayedResize.stop();
@@ -2217,24 +2218,27 @@
             return;
         }
         case QHeaderViewPrivate::MoveSection: {
-            if (qAbs(pos - d->firstPos) >= QApplication::startDragDistance()) {
-                int indicatorCenter = (d->orientation == Qt::Horizontal
-                                       ? d->sectionIndicator->width()
-                                       : d->sectionIndicator->height()) / 2;
-                int centerOffset = indicatorCenter - d->sectionIndicatorOffset;
-                // This will drop the moved section to the position under the center of the indicator.
-                // If centerOffset is 0, the section will be moved to the position of the mouse cursor.
-                int visual = visualIndexAt(pos + centerOffset);
+            if (qAbs(pos - d->firstPos) >= QApplication::startDragDistance()
+                || !d->sectionIndicator->isHidden()) {
+                int visual = visualIndexAt(pos);
                 if (visual == -1)
                     return;
-                d->target = d->logicalIndex(visual);
+                int posThreshold = d->headerSectionPosition(visual) + d->headerSectionSize(visual) / 2;
+                int moving = visualIndex(d->section);
+                if (visual < moving) {
+                    if (pos < posThreshold)
+                        d->target = d->logicalIndex(visual);
+                    else
+                        d->target = d->logicalIndex(visual + 1);
+                } else if (visual > moving) {
+                    if (pos > posThreshold)
+                        d->target = d->logicalIndex(visual);
+                    else
+                        d->target = d->logicalIndex(visual - 1);
+                } else {
+                    d->target = d->section;
+                }
                 d->updateSectionIndicator(d->section, pos);
-            } else {
-                int visual = visualIndexAt(d->firstPos);
-                if (visual == -1)
-                    return;
-                d->target = d->logicalIndex(visual);
-                d->updateSectionIndicator(d->section, d->firstPos);
             }
             return;
         }
@@ -2300,7 +2304,7 @@
             int section = logicalIndexAt(pos);
             if (section != -1 && section == d->pressed) {
                 d->flipSortIndicator(section);
-                emit sectionClicked(logicalIndexAt(pos));
+                emit sectionClicked(section);
             }
             if (d->pressed != -1)
                 updateSection(d->pressed);
@@ -2611,7 +2615,7 @@
     Q_D(QHeaderView);
     d->layoutChildren();
     if (d->hasAutoResizeSections())
-        resizeSections();
+        d->doDelayedResizeSections();
 }
 
 /*!