forked from qt-creator/qt-creator
Editor: Separate color and id in the highlighter scrollbar
This allows to define a color for each highlight, and not just for groups. Change-Id: Ia027f1fb42a96c431b5889ec132a59b16ae41fbb Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -25,6 +25,7 @@
|
|||||||
|
|
||||||
#include "highlightscrollbar.h"
|
#include "highlightscrollbar.h"
|
||||||
|
|
||||||
|
#include <utils/asconst.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
@@ -53,12 +54,10 @@ public:
|
|||||||
|
|
||||||
float m_visibleRange;
|
float m_visibleRange;
|
||||||
float m_offset;
|
float m_offset;
|
||||||
QHash<Id, QSet<int> > m_highlights;
|
QHash<Id, QVector<Highlight> > m_highlights;
|
||||||
QHash<Id, Utils::Theme::Color> m_colors;
|
|
||||||
QHash<Id, HighlightScrollBar::Priority> m_priorities;
|
|
||||||
|
|
||||||
bool m_cacheUpdateScheduled;
|
bool m_cacheUpdateScheduled;
|
||||||
QMap<int, Id> m_cache;
|
QMap<int, Highlight> m_cache;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void paintEvent(QPaintEvent *paintEvent) override;
|
void paintEvent(QPaintEvent *paintEvent) override;
|
||||||
@@ -101,13 +100,6 @@ void HighlightScrollBar::setRangeOffset(float offset)
|
|||||||
m_overlay->m_offset = offset;
|
m_overlay->m_offset = offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HighlightScrollBar::setColor(Id category, Theme::Color color)
|
|
||||||
{
|
|
||||||
if (!m_overlay)
|
|
||||||
return;
|
|
||||||
m_overlay->m_colors[category] = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
QRect HighlightScrollBar::overlayRect()
|
QRect HighlightScrollBar::overlayRect()
|
||||||
{
|
{
|
||||||
QStyleOptionSlider opt;
|
QStyleOptionSlider opt;
|
||||||
@@ -120,27 +112,11 @@ void HighlightScrollBar::overlayDestroyed()
|
|||||||
m_overlay = 0;
|
m_overlay = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HighlightScrollBar::setPriority(Id category, HighlightScrollBar::Priority prio)
|
void HighlightScrollBar::addHighlight(Highlight highlight)
|
||||||
{
|
{
|
||||||
if (!m_overlay)
|
if (!m_overlay)
|
||||||
return;
|
return;
|
||||||
m_overlay->m_priorities[category] = prio;
|
m_overlay->m_highlights[highlight.category] << highlight;
|
||||||
m_overlay->scheduleUpdate();
|
|
||||||
}
|
|
||||||
|
|
||||||
void HighlightScrollBar::addHighlights(Id category, const QSet<int> &highlights)
|
|
||||||
{
|
|
||||||
if (!m_overlay)
|
|
||||||
return;
|
|
||||||
m_overlay->m_highlights[category].unite(highlights);
|
|
||||||
m_overlay->scheduleUpdate();
|
|
||||||
}
|
|
||||||
|
|
||||||
void HighlightScrollBar::addHighlight(Id category, int highlight)
|
|
||||||
{
|
|
||||||
if (!m_overlay)
|
|
||||||
return;
|
|
||||||
m_overlay->m_highlights[category] << highlight;
|
|
||||||
m_overlay->scheduleUpdate();
|
m_overlay->scheduleUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,12 +213,12 @@ void HighlightScrollBarOverlay::updateCache()
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
m_cache.clear();
|
m_cache.clear();
|
||||||
foreach (const Id &category, m_highlights.keys()) {
|
const QList<Id> &categories = m_highlights.keys();
|
||||||
foreach (const int &highlight, m_highlights[category]) {
|
for (const Id &category : categories) {
|
||||||
Id highlightCategory = m_cache[highlight];
|
for (const Highlight &highlight : Utils::asConst(m_highlights[category])) {
|
||||||
if (highlightCategory.isValid() && (m_priorities[highlightCategory] >= m_priorities[category]))
|
Highlight oldHighlight = m_cache[highlight.position];
|
||||||
continue;
|
if (highlight.priority > oldHighlight.priority)
|
||||||
m_cache[highlight] = category;
|
m_cache[highlight.position] = highlight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_cacheUpdateScheduled = false;
|
m_cacheUpdateScheduled = false;
|
||||||
@@ -264,7 +240,8 @@ void HighlightScrollBarOverlay::paintEvent(QPaintEvent *paintEvent)
|
|||||||
|
|
||||||
const QRect &rect = m_scrollBar->overlayRect();
|
const QRect &rect = m_scrollBar->overlayRect();
|
||||||
|
|
||||||
Id previousCategory;
|
Utils::Theme::Color previousColor = Utils::Theme::TextColorNormal;
|
||||||
|
Highlight::Priority previousPriority = Highlight::LowPriority;
|
||||||
QRect *previousRect = 0;
|
QRect *previousRect = 0;
|
||||||
|
|
||||||
const int scrollbarRange = m_scrollBar->maximum() + m_scrollBar->pageStep();
|
const int scrollbarRange = m_scrollBar->maximum() + m_scrollBar->pageStep();
|
||||||
@@ -276,19 +253,16 @@ void HighlightScrollBarOverlay::paintEvent(QPaintEvent *paintEvent)
|
|||||||
const int verticalMargin = ((rect.height() / range) - resultHeight) / 2;
|
const int verticalMargin = ((rect.height() / range) - resultHeight) / 2;
|
||||||
int previousBottom = -1;
|
int previousBottom = -1;
|
||||||
|
|
||||||
QHash<Id, QVector<QRect> > highlights;
|
QHash<Utils::Theme::Color, QVector<QRect> > highlights;
|
||||||
QMapIterator<int, Id> it(m_cache);
|
for (Highlight currentHighlight : Utils::asConst(m_cache)) {
|
||||||
while (it.hasNext()) {
|
// Calculate top and bottom
|
||||||
const Id currentCategory = it.next().value();
|
int top = rect.top() + offset + verticalMargin
|
||||||
|
+ float(currentHighlight.position) / range * rect.height();
|
||||||
// Calculate start and end
|
|
||||||
int top = rect.top() + offset + verticalMargin + float(it.key()) / range * rect.height();
|
|
||||||
const int bottom = top + resultHeight;
|
const int bottom = top + resultHeight;
|
||||||
|
|
||||||
if (previousCategory == currentCategory && previousBottom + 1 >= top) {
|
if (previousRect && previousColor == currentHighlight.color && previousBottom + 1 >= top) {
|
||||||
// If the previous highlight has the same category and is directly prior to this highlight
|
// If the previous highlight has the same color and is directly prior to this highlight
|
||||||
// we just extend the previous highlight.
|
// we just extend the previous highlight.
|
||||||
if (QTC_GUARD(previousRect))
|
|
||||||
previousRect->setBottom(bottom - 1);
|
previousRect->setBottom(bottom - 1);
|
||||||
|
|
||||||
} else { // create a new highlight
|
} else { // create a new highlight
|
||||||
@@ -296,7 +270,7 @@ void HighlightScrollBarOverlay::paintEvent(QPaintEvent *paintEvent)
|
|||||||
// make sure that highlights with higher priority are drawn on top of other highlights
|
// make sure that highlights with higher priority are drawn on top of other highlights
|
||||||
// when rectangles are overlapping
|
// when rectangles are overlapping
|
||||||
|
|
||||||
if (m_priorities[previousCategory] > m_priorities[currentCategory]) {
|
if (previousPriority > currentHighlight.priority) {
|
||||||
// Moving the top of the current highlight when the previous
|
// Moving the top of the current highlight when the previous
|
||||||
// highlight has a higher priority
|
// highlight has a higher priority
|
||||||
top = previousBottom + 1;
|
top = previousBottom + 1;
|
||||||
@@ -305,26 +279,36 @@ void HighlightScrollBarOverlay::paintEvent(QPaintEvent *paintEvent)
|
|||||||
} else {
|
} else {
|
||||||
previousRect->setBottom(top - 1); // move the end of the last highlight
|
previousRect->setBottom(top - 1); // move the end of the last highlight
|
||||||
if (previousRect->height() == 0) // if the result is an empty rect, remove it.
|
if (previousRect->height() == 0) // if the result is an empty rect, remove it.
|
||||||
highlights[previousCategory].removeLast();
|
highlights[previousColor].removeLast();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
highlights[currentCategory] << QRect(rect.left() + horizontalMargin, top,
|
highlights[currentHighlight.color] << QRect(rect.left() + horizontalMargin, top,
|
||||||
resultWidth, bottom - top);
|
resultWidth, bottom - top);
|
||||||
previousRect = &highlights[currentCategory].last();
|
previousRect = &highlights[currentHighlight.color].last();
|
||||||
previousCategory = currentCategory;
|
previousColor = currentHighlight.color;
|
||||||
|
previousPriority = currentHighlight.priority;
|
||||||
}
|
}
|
||||||
previousBottom = previousRect->bottom();
|
previousBottom = previousRect->bottom();
|
||||||
}
|
}
|
||||||
|
|
||||||
QPainter painter(this);
|
QPainter painter(this);
|
||||||
painter.setRenderHint(QPainter::Antialiasing, false);
|
painter.setRenderHint(QPainter::Antialiasing, false);
|
||||||
foreach (Id category, highlights.keys()) {
|
foreach (Utils::Theme::Color themeColor, highlights.keys()) {
|
||||||
const QColor &color = creatorTheme()->color(m_colors[category]);
|
const QColor &color = creatorTheme()->color(themeColor);
|
||||||
for (int i = 0, total = highlights[category].size(); i < total; ++i) {
|
for (int i = 0, total = highlights[themeColor].size(); i < total; ++i) {
|
||||||
const QRect rect = highlights[category][i];
|
const QRect rect = highlights[themeColor][i];
|
||||||
painter.fillRect(rect, color);
|
painter.fillRect(rect, color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Highlight::Highlight(Id category_, int position_,
|
||||||
|
Theme::Color color_, Highlight::Priority priority_)
|
||||||
|
: category(category_)
|
||||||
|
, position(position_)
|
||||||
|
, color(color_)
|
||||||
|
, priority(priority_)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Core
|
} // namespace Core
|
||||||
|
|||||||
@@ -35,6 +35,25 @@
|
|||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
|
struct CORE_EXPORT Highlight
|
||||||
|
{
|
||||||
|
enum Priority {
|
||||||
|
Invalid = -1,
|
||||||
|
LowPriority = 0,
|
||||||
|
NormalPriority = 1,
|
||||||
|
HighPriority = 2,
|
||||||
|
HighestPriority = 3
|
||||||
|
};
|
||||||
|
|
||||||
|
Highlight(Id category, int position, Utils::Theme::Color color, Priority priority);
|
||||||
|
Highlight() = default;
|
||||||
|
|
||||||
|
Id category;
|
||||||
|
int position = -1;
|
||||||
|
Utils::Theme::Color color = Utils::Theme::TextColorNormal;
|
||||||
|
Priority priority = Invalid;
|
||||||
|
};
|
||||||
|
|
||||||
class HighlightScrollBarOverlay;
|
class HighlightScrollBarOverlay;
|
||||||
|
|
||||||
class CORE_EXPORT HighlightScrollBar : public QScrollBar
|
class CORE_EXPORT HighlightScrollBar : public QScrollBar
|
||||||
@@ -47,19 +66,8 @@ public:
|
|||||||
|
|
||||||
void setVisibleRange(float visibleRange);
|
void setVisibleRange(float visibleRange);
|
||||||
void setRangeOffset(float offset);
|
void setRangeOffset(float offset);
|
||||||
void setColor(Id category, Utils::Theme::Color color);
|
|
||||||
|
|
||||||
enum Priority
|
void addHighlight(Highlight highlight);
|
||||||
{
|
|
||||||
LowPriority = 0,
|
|
||||||
NormalPriority = 1,
|
|
||||||
HighPriority = 2,
|
|
||||||
HighestPriority = 3
|
|
||||||
};
|
|
||||||
|
|
||||||
void setPriority(Id category, Priority prio);
|
|
||||||
void addHighlight(Id category, int highlight);
|
|
||||||
void addHighlights(Id category, const QSet<int> &highlights);
|
|
||||||
|
|
||||||
void removeHighlights(Id id);
|
void removeHighlights(Id id);
|
||||||
void removeAllHighlights();
|
void removeAllHighlights();
|
||||||
|
|||||||
@@ -635,14 +635,6 @@ void TextEditorWidgetPrivate::setupScrollBar()
|
|||||||
if (m_highlightScrollBar)
|
if (m_highlightScrollBar)
|
||||||
return;
|
return;
|
||||||
m_highlightScrollBar = new HighlightScrollBar(Qt::Vertical, q);
|
m_highlightScrollBar = new HighlightScrollBar(Qt::Vertical, q);
|
||||||
m_highlightScrollBar->setColor(Constants::SCROLL_BAR_SEARCH_RESULT,
|
|
||||||
Theme::TextEditor_SearchResult_ScrollBarColor);
|
|
||||||
m_highlightScrollBar->setColor(Constants::SCROLL_BAR_CURRENT_LINE,
|
|
||||||
Theme::TextEditor_CurrentLine_ScrollBarColor);
|
|
||||||
m_highlightScrollBar->setPriority(
|
|
||||||
Constants::SCROLL_BAR_SEARCH_RESULT, HighlightScrollBar::HighPriority);
|
|
||||||
m_highlightScrollBar->setPriority(
|
|
||||||
Constants::SCROLL_BAR_CURRENT_LINE, HighlightScrollBar::HighestPriority);
|
|
||||||
q->setVerticalScrollBar(m_highlightScrollBar);
|
q->setVerticalScrollBar(m_highlightScrollBar);
|
||||||
highlightSearchResultsInScrollBar();
|
highlightSearchResultsInScrollBar();
|
||||||
scheduleUpdateHighlightScrollBar();
|
scheduleUpdateHighlightScrollBar();
|
||||||
@@ -4767,11 +4759,11 @@ void TextEditorWidgetPrivate::updateCurrentLineInScrollbar()
|
|||||||
if (m_highlightScrollBar->maximum() > 0) {
|
if (m_highlightScrollBar->maximum() > 0) {
|
||||||
const QTextCursor &tc = q->textCursor();
|
const QTextCursor &tc = q->textCursor();
|
||||||
if (QTextLayout *layout = tc.block().layout()) {
|
if (QTextLayout *layout = tc.block().layout()) {
|
||||||
const int lineNumberInBlock =
|
const int pos = q->textCursor().block().firstLineNumber() +
|
||||||
layout->lineForTextPosition(tc.positionInBlock()).lineNumber();
|
layout->lineForTextPosition(tc.positionInBlock()).lineNumber();
|
||||||
m_highlightScrollBar->addHighlight(
|
m_highlightScrollBar->addHighlight({Constants::SCROLL_BAR_CURRENT_LINE, pos,
|
||||||
Constants::SCROLL_BAR_CURRENT_LINE,
|
Theme::TextEditor_CurrentLine_ScrollBarColor,
|
||||||
q->textCursor().block().firstLineNumber() + lineNumberInBlock);
|
Highlight::HighestPriority});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5745,40 +5737,47 @@ void TextEditorWidgetPrivate::scheduleUpdateHighlightScrollBar()
|
|||||||
QTimer::singleShot(0, this, &TextEditorWidgetPrivate::updateHighlightScrollBarNow);
|
QTimer::singleShot(0, this, &TextEditorWidgetPrivate::updateHighlightScrollBarNow);
|
||||||
}
|
}
|
||||||
|
|
||||||
HighlightScrollBar::Priority textMarkPrioToScrollBarPrio(const TextMark::Priority &prio)
|
Highlight::Priority textMarkPrioToScrollBarPrio(const TextMark::Priority &prio)
|
||||||
{
|
{
|
||||||
switch (prio) {
|
switch (prio) {
|
||||||
case TextMark::LowPriority:
|
case TextMark::LowPriority:
|
||||||
return HighlightScrollBar::LowPriority;
|
return Highlight::LowPriority;
|
||||||
case TextMark::NormalPriority:
|
case TextMark::NormalPriority:
|
||||||
return HighlightScrollBar::NormalPriority;
|
return Highlight::NormalPriority;
|
||||||
case TextMark::HighPriority:
|
case TextMark::HighPriority:
|
||||||
return HighlightScrollBar::HighPriority;
|
return Highlight::HighPriority;
|
||||||
default:
|
default:
|
||||||
return HighlightScrollBar::NormalPriority;
|
return Highlight::NormalPriority;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorWidgetPrivate::addSearchResultsToScrollBar(QVector<SearchResult> results)
|
void TextEditorWidgetPrivate::addSearchResultsToScrollBar(QVector<SearchResult> results)
|
||||||
{
|
{
|
||||||
QSet<int> searchResults;
|
if (!m_highlightScrollBar)
|
||||||
|
return;
|
||||||
foreach (SearchResult result, results) {
|
foreach (SearchResult result, results) {
|
||||||
const QTextBlock &block = q->document()->findBlock(result.start);
|
const QTextBlock &block = q->document()->findBlock(result.start);
|
||||||
if (block.isValid() && block.isVisible()) {
|
if (block.isValid() && block.isVisible()) {
|
||||||
const int firstLine = block.layout()->lineForTextPosition(result.start - block.position()).lineNumber();
|
const int firstLine = block.layout()->lineForTextPosition(result.start - block.position()).lineNumber();
|
||||||
const int lastLine = block.layout()->lineForTextPosition(result.start - block.position() + result.length).lineNumber();
|
const int lastLine = block.layout()->lineForTextPosition(result.start - block.position() + result.length).lineNumber();
|
||||||
for (int line = firstLine; line <= lastLine; ++line)
|
for (int line = firstLine; line <= lastLine; ++line) {
|
||||||
searchResults << block.firstLineNumber() + line;
|
m_highlightScrollBar->addHighlight(
|
||||||
|
{Constants::SCROLL_BAR_SEARCH_RESULT, block.firstLineNumber() + line,
|
||||||
|
Theme::TextEditor_SearchResult_ScrollBarColor, Highlight::HighPriority});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (m_highlightScrollBar)
|
}
|
||||||
m_highlightScrollBar->addHighlights(Constants::SCROLL_BAR_SEARCH_RESULT, searchResults);
|
}
|
||||||
|
|
||||||
|
Highlight markToHighlight(TextMark *mark, int lineNumber)
|
||||||
|
{
|
||||||
|
return Highlight(mark->category(), lineNumber,
|
||||||
|
TextMark::categoryColor(mark->category()),
|
||||||
|
textMarkPrioToScrollBarPrio(mark->priority()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorWidgetPrivate::updateHighlightScrollBarNow()
|
void TextEditorWidgetPrivate::updateHighlightScrollBarNow()
|
||||||
{
|
{
|
||||||
typedef QSet<int> IntSet;
|
|
||||||
|
|
||||||
m_scrollBarUpdateScheduled = false;
|
m_scrollBarUpdateScheduled = false;
|
||||||
if (!m_highlightScrollBar)
|
if (!m_highlightScrollBar)
|
||||||
return;
|
return;
|
||||||
@@ -5791,21 +5790,13 @@ void TextEditorWidgetPrivate::updateHighlightScrollBarNow()
|
|||||||
addSearchResultsToScrollBar(m_searchResults);
|
addSearchResultsToScrollBar(m_searchResults);
|
||||||
|
|
||||||
// update text marks
|
// update text marks
|
||||||
QHash<Id, IntSet> marks;
|
|
||||||
foreach (TextMark *mark, m_document->marks()) {
|
foreach (TextMark *mark, m_document->marks()) {
|
||||||
Id category = mark->category();
|
Id category = mark->category();
|
||||||
if (!mark->isVisible() || !TextMark::categoryHasColor(category))
|
if (!mark->isVisible() || !TextMark::categoryHasColor(category))
|
||||||
continue;
|
continue;
|
||||||
m_highlightScrollBar->setPriority(category, textMarkPrioToScrollBarPrio(mark->priority()));
|
|
||||||
const QTextBlock &block = q->document()->findBlockByNumber(mark->lineNumber() - 1);
|
const QTextBlock &block = q->document()->findBlockByNumber(mark->lineNumber() - 1);
|
||||||
if (block.isVisible())
|
if (block.isVisible())
|
||||||
marks[category] << block.firstLineNumber();
|
m_highlightScrollBar->addHighlight(markToHighlight(mark, block.firstLineNumber()));
|
||||||
}
|
|
||||||
QHashIterator<Id, IntSet> it(marks);
|
|
||||||
while (it.hasNext()) {
|
|
||||||
it.next();
|
|
||||||
m_highlightScrollBar->setColor(it.key(), TextMark::categoryColor(it.key()));
|
|
||||||
m_highlightScrollBar->addHighlights(it.key(), it.value());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user