diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 8670ce4979593f6a14974f26104162ea49c743c7..76df5c71d18d88d13f1813439de9a91d71f4cf41 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -1199,6 +1199,17 @@ bool BaseTextEditor::lineSeparatorsAllowed() const return d->m_lineSeparatorsAllowed; } +void BaseTextEditor::setHighlightBlocks(bool b) +{ + d->m_highlightBlocks = b & d->m_codeFoldingSupported; + viewport()->update(); +} + +bool BaseTextEditor::highlightBlocks() const +{ + return d->m_highlightBlocks; +} + void BaseTextEditor::setCodeFoldingVisible(bool b) { @@ -1262,6 +1273,7 @@ BaseTextEditorPrivate::BaseTextEditorPrivate() m_marksVisible(false), m_codeFoldingVisible(false), m_codeFoldingSupported(false), + m_highlightBlocks(false), m_revisionsVisible(false), m_lineNumbersVisible(true), m_highlightCurrentLine(true), @@ -1662,6 +1674,24 @@ void BaseTextEditorPrivate::moveCursorVisible(bool ensureVisible) q->ensureCursorVisible(); } +static QColor calcBlendColor(const QColor &baseColor, int factor = 1) +{ +const int blendBase = (baseColor.value() > 128) ? 0 : 255; + // Darker backgrounds may need a bit more contrast + // (this calculation is temporary solution until we have a setting for this color) + const int blendFactor = (baseColor.value() > 128) ? 8 : 16; + + QColor blendColor = baseColor; + + while (factor--) { + blendColor = QColor( + (blendBase * blendFactor + blendColor.blue() * (256 - blendFactor)) / 256, + (blendBase * blendFactor + blendColor.green() * (256 - blendFactor)) / 256, + (blendBase * blendFactor + blendColor.blue() * (256 - blendFactor)) / 256); + } + return blendColor; +} + void BaseTextEditor::paintEvent(QPaintEvent *e) { /* @@ -1682,19 +1712,13 @@ void BaseTextEditor::paintEvent(QPaintEvent *e) QRect viewportRect = viewport()->rect(); const QColor baseColor = palette().base().color(); - const int blendBase = (baseColor.value() > 128) ? 0 : 255; - // Darker backgrounds may need a bit more contrast - // (this calculation is temporary solution until we have a setting for this color) - const int blendFactor = (baseColor.value() > 128) ? 8 : 16; - const QColor blendColor( - (blendBase * blendFactor + baseColor.blue() * (256 - blendFactor)) / 256, - (blendBase * blendFactor + baseColor.green() * (256 - blendFactor)) / 256, - (blendBase * blendFactor + baseColor.blue() * (256 - blendFactor)) / 256); + const QColor blendColor = calcBlendColor(baseColor); + if (d->m_visibleWrapColumn > 0) { qreal lineX = fontMetrics().averageCharWidth() * d->m_visibleWrapColumn + offset.x() + 4; painter.fillRect(QRectF(lineX, 0, viewportRect.width() - lineX, viewportRect.height()), blendColor); } - + // keep right margin clean from full-width selection int maxX = offset.x() + qMax((qreal)viewportRect.width(), documentLayout->documentSize().width()) - doc->documentMargin(); @@ -1736,12 +1760,24 @@ void BaseTextEditor::paintEvent(QPaintEvent *e) while (block.isValid()) { - if (!block.isVisible()) { - block = block.next(); - continue; - } - QRectF r = blockBoundingRect(block).translated(offset); + + if (d->m_highlightBlocks) { + QTextBlock previousBlock = block.previous(); + if (previousBlock.isValid()){ + int thisBraceDepth = block.userState(); + if (thisBraceDepth >= 0) + thisBraceDepth >>= 8; + int braceDepth = block.previous().userState(); + if (braceDepth >= 0) + braceDepth >>= 8; + int minBraceDepth = qMin(thisBraceDepth, braceDepth); + if (minBraceDepth > 0) { + painter.fillRect(r, calcBlendColor(baseColor, minBraceDepth)); + } + } + } + QTextLayout *layout = block.layout(); QTextOption option = layout->textOption(); @@ -3413,6 +3449,7 @@ void BaseTextEditor::setDisplaySettings(const DisplaySettings &ds) setVisibleWrapColumn(ds.m_showWrapColumn ? ds.m_wrapColumn : 0); setCodeFoldingVisible(ds.m_displayFoldingMarkers); setHighlightCurrentLine(ds.m_highlightCurrentLine); + setHighlightBlocks(ds.m_highlightBlocks); if (d->m_displaySettings.m_visualizeWhitespace != ds.m_visualizeWhitespace) { if (QSyntaxHighlighter *highlighter = baseTextDocument()->syntaxHighlighter()) diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h index 16005cc1d94989fcaf516ea02ed14683dde7b314..19ce6b367997664738377faa22c0a993f5518983 100644 --- a/src/plugins/texteditor/basetexteditor.h +++ b/src/plugins/texteditor/basetexteditor.h @@ -267,9 +267,13 @@ public: void setHighlightCurrentLine(bool b); bool highlightCurrentLine() const; + void setHighlightBlocks(bool b); + bool highlightBlocks() const; + void setLineNumbersVisible(bool b); bool lineNumbersVisible() const; + void setMarksVisible(bool b); bool marksVisible() const; diff --git a/src/plugins/texteditor/basetexteditor_p.h b/src/plugins/texteditor/basetexteditor_p.h index e7a39515f8d25570245267c738e517953ab7f166..74f158dab5e61b1586181d44293f529e01f4f9f9 100644 --- a/src/plugins/texteditor/basetexteditor_p.h +++ b/src/plugins/texteditor/basetexteditor_p.h @@ -183,6 +183,7 @@ public: uint m_marksVisible : 1; uint m_codeFoldingVisible : 1; uint m_codeFoldingSupported : 1; + uint m_highlightBlocks : 1; uint m_revisionsVisible : 1; uint m_lineNumbersVisible : 1; uint m_highlightCurrentLine : 1; diff --git a/src/plugins/texteditor/displaysettings.cpp b/src/plugins/texteditor/displaysettings.cpp index 7aabe03d123eecf845f512d0bfb07322ceffb0e1..47e9ad4d474a301a42a2d802541888cd097ffe46 100644 --- a/src/plugins/texteditor/displaysettings.cpp +++ b/src/plugins/texteditor/displaysettings.cpp @@ -41,6 +41,7 @@ static const char * const wrapColumnKey = "WrapColumn"; static const char * const visualizeWhitespaceKey = "VisualizeWhitespace"; static const char * const displayFoldingMarkersKey = "DisplayFoldingMarkers"; static const char * const highlightCurrentLineKey = "HighlightCurrentLineKey"; +static const char * const highlightBlocksKey = "HighlightBlocksKey"; static const char * const groupPostfix = "DisplaySettings"; namespace TextEditor { @@ -52,7 +53,8 @@ DisplaySettings::DisplaySettings() : m_wrapColumn(80), m_visualizeWhitespace(false), m_displayFoldingMarkers(true), - m_highlightCurrentLine(true) + m_highlightCurrentLine(true), + m_highlightBlocks(false) { } @@ -69,6 +71,7 @@ void DisplaySettings::toSettings(const QString &category, QSettings *s) const s->setValue(QLatin1String(visualizeWhitespaceKey), m_visualizeWhitespace); s->setValue(QLatin1String(displayFoldingMarkersKey), m_displayFoldingMarkers); s->setValue(QLatin1String(highlightCurrentLineKey), m_highlightCurrentLine); + s->setValue(QLatin1String(highlightBlocksKey), m_highlightBlocks); s->endGroup(); } @@ -88,6 +91,7 @@ void DisplaySettings::fromSettings(const QString &category, const QSettings *s) m_visualizeWhitespace = s->value(group + QLatin1String(visualizeWhitespaceKey), m_visualizeWhitespace).toBool(); m_displayFoldingMarkers = s->value(group + QLatin1String(displayFoldingMarkersKey), m_displayFoldingMarkers).toBool(); m_highlightCurrentLine = s->value(group + QLatin1String(highlightCurrentLineKey), m_highlightCurrentLine).toBool(); + m_highlightBlocks = s->value(group + QLatin1String(highlightBlocksKey), m_highlightBlocks).toBool(); } bool DisplaySettings::equals(const DisplaySettings &ds) const @@ -99,6 +103,7 @@ bool DisplaySettings::equals(const DisplaySettings &ds) const && m_visualizeWhitespace == ds.m_visualizeWhitespace && m_displayFoldingMarkers == ds.m_displayFoldingMarkers && m_highlightCurrentLine == ds.m_highlightCurrentLine + && m_highlightBlocks == ds.m_highlightBlocks ; } diff --git a/src/plugins/texteditor/displaysettings.h b/src/plugins/texteditor/displaysettings.h index c3c5a867c6cc734ed4f85207767796322d0849b3..0601f905b9f5126108e7215d2593dfaf460bd27b 100644 --- a/src/plugins/texteditor/displaysettings.h +++ b/src/plugins/texteditor/displaysettings.h @@ -52,6 +52,7 @@ struct TEXTEDITOR_EXPORT DisplaySettings bool m_visualizeWhitespace; bool m_displayFoldingMarkers; bool m_highlightCurrentLine; + bool m_highlightBlocks; bool equals(const DisplaySettings &ds) const; }; diff --git a/src/plugins/texteditor/displaysettingspage.cpp b/src/plugins/texteditor/displaysettingspage.cpp index bae904b98ff4fa7771ead20d8f529fd6324ec05a..b1ba03ce7fc1f7210e1cefcaa279aa35f3fa0758 100644 --- a/src/plugins/texteditor/displaysettingspage.cpp +++ b/src/plugins/texteditor/displaysettingspage.cpp @@ -122,6 +122,7 @@ void DisplaySettingsPage::settingsFromUI(DisplaySettings &displaySettings) const displaySettings.m_visualizeWhitespace = m_d->m_page.visualizeWhitespace->isChecked(); displaySettings.m_displayFoldingMarkers = m_d->m_page.displayFoldingMarkers->isChecked(); displaySettings.m_highlightCurrentLine = m_d->m_page.highlightCurrentLine->isChecked(); + displaySettings.m_highlightBlocks = m_d->m_page.highlightBlocks->isChecked(); } void DisplaySettingsPage::settingsToUI() @@ -134,6 +135,7 @@ void DisplaySettingsPage::settingsToUI() m_d->m_page.visualizeWhitespace->setChecked(displaySettings.m_visualizeWhitespace); m_d->m_page.displayFoldingMarkers->setChecked(displaySettings.m_displayFoldingMarkers); m_d->m_page.highlightCurrentLine->setChecked(displaySettings.m_highlightCurrentLine); + m_d->m_page.highlightBlocks->setChecked(displaySettings.m_highlightBlocks); } DisplaySettings DisplaySettingsPage::displaySettings() const diff --git a/src/plugins/texteditor/displaysettingspage.ui b/src/plugins/texteditor/displaysettingspage.ui index 59c8d676b26eb6ee411b41c486d7c8bb83cdda06..eb6caa2a211211da04833604f47d01cc07f83fbe 100644 --- a/src/plugins/texteditor/displaysettingspage.ui +++ b/src/plugins/texteditor/displaysettingspage.ui @@ -64,6 +64,13 @@ </property> </widget> </item> + <item row="4" column="0"> + <widget class="QCheckBox" name="highlightBlocks"> + <property name="text"> + <string>Highlight &blocks</string> + </property> + </widget> + </item> </layout> </widget> </item>