From ffe9fe58f7055d5adc34839a7d74f06773fb2b98 Mon Sep 17 00:00:00 2001
From: mae <qt-info@nokia.com>
Date: Wed, 29 Apr 2009 15:37:38 +0200
Subject: [PATCH] support old-fashioned +/- folding bar, but improved

---
 src/plugins/texteditor/basetexteditor.cpp     | 110 +++++++++++++++---
 src/plugins/texteditor/basetexteditor.h       |  16 ++-
 src/plugins/texteditor/displaysettings.cpp    |   7 +-
 src/plugins/texteditor/displaysettings.h      |   1 +
 .../texteditor/displaysettingspage.cpp        |   2 +
 src/plugins/texteditor/displaysettingspage.ui |  70 +++++++++--
 src/plugins/texteditor/textfilewizard.cpp     |   1 +
 7 files changed, 177 insertions(+), 30 deletions(-)

diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index 7d611193001..d44c789d6f5 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -1498,6 +1498,12 @@ QRect BaseTextEditor::collapseBox()
         return QRect();
 
     QTextBlock begin = document()->findBlockByNumber(d->m_highlightBlocksInfo.open.last());
+
+    if (!d->m_displaySettings.m_fancyFoldingBar) {
+        if (TextBlockUserData::hasCollapseAfter(begin.previous()))
+            begin = begin.previous();
+    }
+
     QTextBlock end = document()->findBlockByNumber(d->m_highlightBlocksInfo.close.first());
     if (!begin.isValid() || !end.isValid())
         return QRect();
@@ -2294,7 +2300,7 @@ void BaseTextEditor::extraAreaPaintEvent(QPaintEvent *e)
                 }
             }
 
-            if (d->m_codeFoldingVisible) {
+            if (d->m_codeFoldingVisible && d->m_displaySettings.m_fancyFoldingBar) {
                 QRect r(extraAreaWidth+2, top, collapseBoxWidth-4, bottom - top);
                 bool drawBox = !nextBlock.isVisible();
 
@@ -2349,6 +2355,86 @@ void BaseTextEditor::extraAreaPaintEvent(QPaintEvent *e)
                     painter.setRenderHint(QPainter::Antialiasing, false);
                 }
 
+            } else if (d->m_codeFoldingVisible) {
+
+                bool collapseThis = false;
+                bool collapseAfter = false;
+                bool hasClosingCollapse = false;
+
+                if (TextBlockUserData *userData = static_cast<TextBlockUserData*>(block.userData())) {
+                    if (!userData->ifdefedOut()) {
+                        collapseAfter = (userData->collapseMode() == TextBlockUserData::CollapseAfter);
+                        collapseThis = (userData->collapseMode() == TextBlockUserData::CollapseThis);
+                        hasClosingCollapse = userData->hasClosingCollapse() && (previousBraceDepth > 0);
+                    }
+                }
+
+
+                int extraAreaHighlightCollapseBlockNumber = -1;
+                int extraAreaHighlightCollapseEndBlockNumber = -1;
+                bool endIsVisible = false;
+                if (!d->m_highlightBlocksInfo.isEmpty()) {
+                    extraAreaHighlightCollapseBlockNumber =  d->m_highlightBlocksInfo.open.last();
+                    extraAreaHighlightCollapseEndBlockNumber =  d->m_highlightBlocksInfo.close.first();
+                    endIsVisible = doc->findBlockByNumber(extraAreaHighlightCollapseEndBlockNumber).isVisible();
+
+                    if (TextBlockUserData::hasCollapseAfter(
+                            doc->findBlockByNumber(extraAreaHighlightCollapseBlockNumber-1))) {
+                            extraAreaHighlightCollapseBlockNumber--;
+                    }
+                }
+
+                const QRect box(extraAreaWidth + collapseBoxWidth/4, top + collapseBoxWidth/4,
+                                2 * (collapseBoxWidth/4) + 1, 2 * (collapseBoxWidth/4) + 1);
+                const QPoint boxCenter = box.center();
+
+
+                QColor textColorInactive = pal.text().color();
+                textColorInactive.setAlpha(100);
+                QColor textColor = pal.text().color();
+
+                QPen activePen(textColor);
+                QPen inactivePen(textColorInactive);
+
+                TextBlockUserData *nextBlockUserData = TextEditDocumentLayout::testUserData(nextBlock);
+
+                bool collapseNext = nextBlockUserData
+                                    && nextBlockUserData->collapseMode()
+                                    == TextBlockUserData::CollapseThis
+                                    && !nextBlockUserData->ifdefedOut();
+
+                bool nextHasClosingCollapse = nextBlockUserData
+                                              && nextBlockUserData->hasClosingCollapseInside()
+                                              && nextBlockUserData->ifdefedOut();
+
+                bool drawBox = ((collapseAfter || collapseNext) && !nextHasClosingCollapse);
+
+                if (blockNumber > extraAreaHighlightCollapseBlockNumber
+                    && blockNumber < extraAreaHighlightCollapseEndBlockNumber) {
+                    painter.setPen(activePen);
+                    painter.drawLine(boxCenter.x(), top, boxCenter.x(), bottom - 1);
+                } else if (blockNumber == extraAreaHighlightCollapseBlockNumber
+                           && nextVisibleBlockNumber <= extraAreaHighlightCollapseEndBlockNumber) {
+                    painter.setPen(activePen);
+                    painter.drawLine(boxCenter.x(), boxCenter.y(), boxCenter.x(), bottom - 1);
+                } else if (blockNumber == extraAreaHighlightCollapseEndBlockNumber) {
+                    painter.setPen(activePen);
+                    painter.drawLine(boxCenter.x(), top, boxCenter.x(), boxCenter.y());
+                }
+
+                if (drawBox) {
+                    painter.setPen(blockNumber == extraAreaHighlightCollapseBlockNumber ?
+                                   activePen : inactivePen);
+                    painter.setBrush(pal.base());
+                    painter.drawRect(box.adjusted(0, 0, -1, -1));
+                    if (!nextBlock.isVisible())
+                        painter.drawLine(boxCenter.x(), box.top() + 2, boxCenter.x(), box.bottom() - 2);
+                    painter.drawLine(box.left() + 2, boxCenter.y(), box.right() - 2, boxCenter.y());
+                } else if (blockNumber == extraAreaHighlightCollapseEndBlockNumber) {
+                    painter.setPen(activePen);
+                    painter.drawLine(boxCenter.x() + 1, boxCenter.y(), box.right() - 1, boxCenter.y());
+                }
+
             }
 
 
@@ -2391,20 +2477,11 @@ void BaseTextEditor::extraAreaPaintEvent(QPaintEvent *e)
         blockNumber = nextVisibleBlockNumber;
     }
 
-    if (d->m_codeFoldingVisible) {
+    if (d->m_codeFoldingVisible && d->m_displaySettings.m_fancyFoldingBar) {
         painter.drawLine(extraAreaWidth, 0,
                          extraAreaWidth, viewport()->height());
         painter.drawLine(extraAreaWidth + collapseBoxWidth - 1, 0,
                          extraAreaWidth + collapseBoxWidth - 1, viewport()->height());
-//        QRect cb = collapseBox();
-//        if (!cb.isEmpty()) {
-//            QPen pen(baseColor.value() < 128 ? Qt::white : Qt::black);
-//            pen.setWidth(2);
-//            painter.setPen(pen);
-//            painter.setRenderHint(QPainter::Antialiasing, true);
-//            painter.translate(.5, .5);
-//            painter.drawRoundedRect(QRect(cb.adjusted(0, 0,-2, -2)), 4, 4);
-//        }
     }
 }
 
@@ -2617,17 +2694,18 @@ void BaseTextEditor::extraAreaMouseEvent(QMouseEvent *e)
         d->extraAreaHighlightCollapseBlockNumber = -1;
         d->extraAreaHighlightCollapseColumn = -1;
 
-        if (d->m_displaySettings.m_highlightBlocks) {
-            QTextCursor cursor  = textCursor();
-            d->extraAreaHighlightCollapseBlockNumber = cursor.blockNumber();
-            d->extraAreaHighlightCollapseColumn = cursor.position() - cursor.block().position();
-        }
 
         int collapseBoxWidth = fontMetrics().lineSpacing() + 1;
         if (e->pos().x() > extraArea()->width() - collapseBoxWidth) {
             d->extraAreaHighlightCollapseBlockNumber = cursor.blockNumber();
             if (!TextBlockUserData::hasClosingCollapse(cursor.block()))
                 d->extraAreaHighlightCollapseColumn = cursor.block().length()-1;
+            if (!d->m_displaySettings.m_fancyFoldingBar
+                && TextBlockUserData::hasCollapseAfter(cursor.block())) {
+                d->extraAreaHighlightCollapseBlockNumber++;
+                if (!TextBlockUserData::hasClosingCollapse(cursor.block().next()))
+                    d->extraAreaHighlightCollapseColumn = cursor.block().next().length()-1;
+            }
         }
         if (highlightBlockNumber != d->extraAreaHighlightCollapseBlockNumber
             || highlightColumn != d->extraAreaHighlightCollapseColumn)
diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h
index 1c02c6ce4a6..ca488ba1c8d 100644
--- a/src/plugins/texteditor/basetexteditor.h
+++ b/src/plugins/texteditor/basetexteditor.h
@@ -38,6 +38,7 @@
 #include <QtGui/QLabel>
 #include <QtGui/QKeyEvent>
 #include <QtCore/QTimeLine>
+#include <QtCore/QDebug>
 
 QT_BEGIN_NAMESPACE
 class QLabel;
@@ -130,7 +131,7 @@ public:
     inline bool clearIfdefedOut() { bool result = m_ifdefedOut; m_ifdefedOut = false; return result;}
     inline bool ifdefedOut() const { return m_ifdefedOut; }
 
-    inline static TextBlockUserData *canCollapse(const QTextBlock& block) {
+    inline static TextBlockUserData *canCollapse(const QTextBlock &block) {
         TextBlockUserData *data = static_cast<TextBlockUserData*>(block.userData());
         if (!data || data->collapseMode() != CollapseAfter) {
             data = static_cast<TextBlockUserData*>(block.next().userData());
@@ -140,6 +141,19 @@ public:
         return data;
     }
 
+    inline static bool hasCollapseAfter(const QTextBlock & block)
+    {
+        TextBlockUserData *data = static_cast<TextBlockUserData*>(block.userData());
+        if (data && data->collapseMode() != NoCollapse) {
+            return (data->collapseMode() == CollapseAfter);
+        } else if (!data) {
+            data = static_cast<TextBlockUserData*>(block.next().userData());
+            if (data && data->collapseMode() == TextBlockUserData::CollapseThis &&  !data->m_ifdefedOut)
+                return true;
+        }
+        return false;
+    }
+
     inline static bool hasClosingCollapse(const QTextBlock &block) {
         TextBlockUserData *data = static_cast<TextBlockUserData*>(block.userData());
         return (data && data->hasClosingCollapse());
diff --git a/src/plugins/texteditor/displaysettings.cpp b/src/plugins/texteditor/displaysettings.cpp
index b0273a2c887..b16cc1436ea 100644
--- a/src/plugins/texteditor/displaysettings.cpp
+++ b/src/plugins/texteditor/displaysettings.cpp
@@ -43,6 +43,7 @@ static const char * const displayFoldingMarkersKey = "DisplayFoldingMarkers";
 static const char * const highlightCurrentLineKey = "HighlightCurrentLineKeyV2";
 static const char * const highlightBlocksKey = "HighlightBlocksKey";
 static const char * const animateMatchingParenthesesKey= "AnimateMatchingParenthesesKey";
+static const char * const fancyFoldingBarKey= "FancyFoldingBarKey";
 static const char * const groupPostfix = "DisplaySettings";
 
 namespace TextEditor {
@@ -56,7 +57,8 @@ DisplaySettings::DisplaySettings() :
     m_displayFoldingMarkers(true),
     m_highlightCurrentLine(false),
     m_highlightBlocks(false),
-    m_animateMatchingParentheses(true)
+    m_animateMatchingParentheses(true),
+    m_fancyFoldingBar(false)
 {
 }
 
@@ -75,6 +77,7 @@ void DisplaySettings::toSettings(const QString &category, QSettings *s) const
     s->setValue(QLatin1String(highlightCurrentLineKey), m_highlightCurrentLine);
     s->setValue(QLatin1String(highlightBlocksKey), m_highlightBlocks);
     s->setValue(QLatin1String(animateMatchingParenthesesKey), m_animateMatchingParentheses);
+    s->setValue(QLatin1String(fancyFoldingBarKey), m_fancyFoldingBar);
     s->endGroup();
 }
 
@@ -96,6 +99,7 @@ void DisplaySettings::fromSettings(const QString &category, const QSettings *s)
     m_highlightCurrentLine = s->value(group + QLatin1String(highlightCurrentLineKey), m_highlightCurrentLine).toBool();
     m_highlightBlocks = s->value(group + QLatin1String(highlightBlocksKey), m_highlightBlocks).toBool();
     m_animateMatchingParentheses = s->value(group + QLatin1String(animateMatchingParenthesesKey), m_animateMatchingParentheses).toBool();
+    m_fancyFoldingBar = s->value(group + QLatin1String(fancyFoldingBarKey), m_fancyFoldingBar).toBool();
 }
 
 bool DisplaySettings::equals(const DisplaySettings &ds) const
@@ -109,6 +113,7 @@ bool DisplaySettings::equals(const DisplaySettings &ds) const
         && m_highlightCurrentLine == ds.m_highlightCurrentLine
         && m_highlightBlocks == ds.m_highlightBlocks
         && m_animateMatchingParentheses == ds.m_animateMatchingParentheses
+        && m_fancyFoldingBar == ds.m_fancyFoldingBar
         ;
 }
 
diff --git a/src/plugins/texteditor/displaysettings.h b/src/plugins/texteditor/displaysettings.h
index 06a7d0fa381..b05cc7a1915 100644
--- a/src/plugins/texteditor/displaysettings.h
+++ b/src/plugins/texteditor/displaysettings.h
@@ -54,6 +54,7 @@ struct TEXTEDITOR_EXPORT DisplaySettings
     bool m_highlightCurrentLine;
     bool m_highlightBlocks;
     bool m_animateMatchingParentheses;
+    bool m_fancyFoldingBar;
 
     bool equals(const DisplaySettings &ds) const;
 };
diff --git a/src/plugins/texteditor/displaysettingspage.cpp b/src/plugins/texteditor/displaysettingspage.cpp
index ec7ae6a2210..31c7c803d22 100644
--- a/src/plugins/texteditor/displaysettingspage.cpp
+++ b/src/plugins/texteditor/displaysettingspage.cpp
@@ -124,6 +124,7 @@ void DisplaySettingsPage::settingsFromUI(DisplaySettings &displaySettings) const
     displaySettings.m_highlightCurrentLine = m_d->m_page.highlightCurrentLine->isChecked();
     displaySettings.m_highlightBlocks = m_d->m_page.highlightBlocks->isChecked();
     displaySettings.m_animateMatchingParentheses= m_d->m_page.animateMatchingParentheses->isChecked();
+    displaySettings.m_fancyFoldingBar = m_d->m_page.fancyFoldingBar->isChecked();
 }
 
 void DisplaySettingsPage::settingsToUI()
@@ -138,6 +139,7 @@ void DisplaySettingsPage::settingsToUI()
     m_d->m_page.highlightCurrentLine->setChecked(displaySettings.m_highlightCurrentLine);
     m_d->m_page.highlightBlocks->setChecked(displaySettings.m_highlightBlocks);
     m_d->m_page.animateMatchingParentheses->setChecked(displaySettings.m_animateMatchingParentheses);
+    m_d->m_page.fancyFoldingBar->setChecked(displaySettings.m_fancyFoldingBar);
 }
 
 DisplaySettings DisplaySettingsPage::displaySettings() const
diff --git a/src/plugins/texteditor/displaysettingspage.ui b/src/plugins/texteditor/displaysettingspage.ui
index 5524172fcfb..e6d7eb867a6 100644
--- a/src/plugins/texteditor/displaysettingspage.ui
+++ b/src/plugins/texteditor/displaysettingspage.ui
@@ -7,7 +7,7 @@
     <x>0</x>
     <y>0</y>
     <width>381</width>
-    <height>302</height>
+    <height>335</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -32,22 +32,52 @@
      <property name="title">
       <string>Display</string>
      </property>
-     <layout class="QGridLayout" name="gridLayout_2">
-      <item row="0" column="0">
+     <layout class="QVBoxLayout" name="verticalLayout">
+      <item>
        <widget class="QCheckBox" name="displayLineNumbers">
         <property name="text">
          <string>Display line &amp;numbers</string>
         </property>
        </widget>
       </item>
-      <item row="1" column="0">
+      <item>
        <widget class="QCheckBox" name="displayFoldingMarkers">
         <property name="text">
          <string>Display &amp;folding markers</string>
         </property>
        </widget>
       </item>
-      <item row="2" column="0">
+      <item>
+       <layout class="QHBoxLayout" name="horizontalLayout_2">
+        <item>
+         <spacer name="horizontalSpacer_2">
+          <property name="orientation">
+           <enum>Qt::Horizontal</enum>
+          </property>
+          <property name="sizeType">
+           <enum>QSizePolicy::Fixed</enum>
+          </property>
+          <property name="sizeHint" stdset="0">
+           <size>
+            <width>40</width>
+            <height>20</height>
+           </size>
+          </property>
+         </spacer>
+        </item>
+        <item>
+         <widget class="QCheckBox" name="fancyFoldingBar">
+          <property name="enabled">
+           <bool>false</bool>
+          </property>
+          <property name="text">
+           <string>Use fancy style</string>
+          </property>
+         </widget>
+        </item>
+       </layout>
+      </item>
+      <item>
        <widget class="QCheckBox" name="visualizeWhitespace">
         <property name="toolTip">
          <string>Show tabs and spaces.</string>
@@ -57,21 +87,21 @@
         </property>
        </widget>
       </item>
-      <item row="3" column="0">
+      <item>
        <widget class="QCheckBox" name="highlightCurrentLine">
         <property name="text">
          <string>Highlight current &amp;line</string>
         </property>
        </widget>
       </item>
-      <item row="4" column="0">
+      <item>
        <widget class="QCheckBox" name="highlightBlocks">
         <property name="text">
          <string>Highlight &amp;blocks</string>
         </property>
        </widget>
       </item>
-      <item row="5" column="0">
+      <item>
        <widget class="QCheckBox" name="animateMatchingParentheses">
         <property name="text">
          <string>Animate matching parentheses</string>
@@ -142,12 +172,28 @@
    <slot>setEnabled(bool)</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>399</x>
-     <y>308</y>
+     <x>238</x>
+     <y>84</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>299</x>
+     <y>84</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>displayFoldingMarkers</sender>
+   <signal>toggled(bool)</signal>
+   <receiver>fancyFoldingBar</receiver>
+   <slot>setEnabled(bool)</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>60</x>
+     <y>161</y>
     </hint>
     <hint type="destinationlabel">
-     <x>474</x>
-     <y>308</y>
+     <x>87</x>
+     <y>179</y>
     </hint>
    </hints>
   </connection>
diff --git a/src/plugins/texteditor/textfilewizard.cpp b/src/plugins/texteditor/textfilewizard.cpp
index e17e7db1350..32ef28f07d1 100644
--- a/src/plugins/texteditor/textfilewizard.cpp
+++ b/src/plugins/texteditor/textfilewizard.cpp
@@ -30,6 +30,7 @@
 #include "textfilewizard.h"
 #include "basetexteditor.h"
 #include "texteditorconstants.h"
+#include <QDebug>
 
 using namespace TextEditor;
 
-- 
GitLab