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 <utils/asconst.h>
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
#include <QPainter>
|
||||
@@ -53,12 +54,10 @@ public:
|
||||
|
||||
float m_visibleRange;
|
||||
float m_offset;
|
||||
QHash<Id, QSet<int> > m_highlights;
|
||||
QHash<Id, Utils::Theme::Color> m_colors;
|
||||
QHash<Id, HighlightScrollBar::Priority> m_priorities;
|
||||
QHash<Id, QVector<Highlight> > m_highlights;
|
||||
|
||||
bool m_cacheUpdateScheduled;
|
||||
QMap<int, Id> m_cache;
|
||||
QMap<int, Highlight> m_cache;
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *paintEvent) override;
|
||||
@@ -101,13 +100,6 @@ void HighlightScrollBar::setRangeOffset(float 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()
|
||||
{
|
||||
QStyleOptionSlider opt;
|
||||
@@ -120,27 +112,11 @@ void HighlightScrollBar::overlayDestroyed()
|
||||
m_overlay = 0;
|
||||
}
|
||||
|
||||
void HighlightScrollBar::setPriority(Id category, HighlightScrollBar::Priority prio)
|
||||
void HighlightScrollBar::addHighlight(Highlight highlight)
|
||||
{
|
||||
if (!m_overlay)
|
||||
return;
|
||||
m_overlay->m_priorities[category] = prio;
|
||||
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->m_highlights[highlight.category] << highlight;
|
||||
m_overlay->scheduleUpdate();
|
||||
}
|
||||
|
||||
@@ -237,12 +213,12 @@ void HighlightScrollBarOverlay::updateCache()
|
||||
return;
|
||||
|
||||
m_cache.clear();
|
||||
foreach (const Id &category, m_highlights.keys()) {
|
||||
foreach (const int &highlight, m_highlights[category]) {
|
||||
Id highlightCategory = m_cache[highlight];
|
||||
if (highlightCategory.isValid() && (m_priorities[highlightCategory] >= m_priorities[category]))
|
||||
continue;
|
||||
m_cache[highlight] = category;
|
||||
const QList<Id> &categories = m_highlights.keys();
|
||||
for (const Id &category : categories) {
|
||||
for (const Highlight &highlight : Utils::asConst(m_highlights[category])) {
|
||||
Highlight oldHighlight = m_cache[highlight.position];
|
||||
if (highlight.priority > oldHighlight.priority)
|
||||
m_cache[highlight.position] = highlight;
|
||||
}
|
||||
}
|
||||
m_cacheUpdateScheduled = false;
|
||||
@@ -264,7 +240,8 @@ void HighlightScrollBarOverlay::paintEvent(QPaintEvent *paintEvent)
|
||||
|
||||
const QRect &rect = m_scrollBar->overlayRect();
|
||||
|
||||
Id previousCategory;
|
||||
Utils::Theme::Color previousColor = Utils::Theme::TextColorNormal;
|
||||
Highlight::Priority previousPriority = Highlight::LowPriority;
|
||||
QRect *previousRect = 0;
|
||||
|
||||
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;
|
||||
int previousBottom = -1;
|
||||
|
||||
QHash<Id, QVector<QRect> > highlights;
|
||||
QMapIterator<int, Id> it(m_cache);
|
||||
while (it.hasNext()) {
|
||||
const Id currentCategory = it.next().value();
|
||||
|
||||
// Calculate start and end
|
||||
int top = rect.top() + offset + verticalMargin + float(it.key()) / range * rect.height();
|
||||
QHash<Utils::Theme::Color, QVector<QRect> > highlights;
|
||||
for (Highlight currentHighlight : Utils::asConst(m_cache)) {
|
||||
// Calculate top and bottom
|
||||
int top = rect.top() + offset + verticalMargin
|
||||
+ float(currentHighlight.position) / range * rect.height();
|
||||
const int bottom = top + resultHeight;
|
||||
|
||||
if (previousCategory == currentCategory && previousBottom + 1 >= top) {
|
||||
// If the previous highlight has the same category and is directly prior to this highlight
|
||||
if (previousRect && previousColor == currentHighlight.color && previousBottom + 1 >= top) {
|
||||
// If the previous highlight has the same color and is directly prior to this highlight
|
||||
// we just extend the previous highlight.
|
||||
if (QTC_GUARD(previousRect))
|
||||
previousRect->setBottom(bottom - 1);
|
||||
|
||||
} 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
|
||||
// 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
|
||||
// highlight has a higher priority
|
||||
top = previousBottom + 1;
|
||||
@@ -305,26 +279,36 @@ void HighlightScrollBarOverlay::paintEvent(QPaintEvent *paintEvent)
|
||||
} else {
|
||||
previousRect->setBottom(top - 1); // move the end of the last highlight
|
||||
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);
|
||||
previousRect = &highlights[currentCategory].last();
|
||||
previousCategory = currentCategory;
|
||||
previousRect = &highlights[currentHighlight.color].last();
|
||||
previousColor = currentHighlight.color;
|
||||
previousPriority = currentHighlight.priority;
|
||||
}
|
||||
previousBottom = previousRect->bottom();
|
||||
}
|
||||
|
||||
QPainter painter(this);
|
||||
painter.setRenderHint(QPainter::Antialiasing, false);
|
||||
foreach (Id category, highlights.keys()) {
|
||||
const QColor &color = creatorTheme()->color(m_colors[category]);
|
||||
for (int i = 0, total = highlights[category].size(); i < total; ++i) {
|
||||
const QRect rect = highlights[category][i];
|
||||
foreach (Utils::Theme::Color themeColor, highlights.keys()) {
|
||||
const QColor &color = creatorTheme()->color(themeColor);
|
||||
for (int i = 0, total = highlights[themeColor].size(); i < total; ++i) {
|
||||
const QRect rect = highlights[themeColor][i];
|
||||
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
|
||||
|
||||
@@ -35,6 +35,25 @@
|
||||
|
||||
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 CORE_EXPORT HighlightScrollBar : public QScrollBar
|
||||
@@ -47,19 +66,8 @@ public:
|
||||
|
||||
void setVisibleRange(float visibleRange);
|
||||
void setRangeOffset(float offset);
|
||||
void setColor(Id category, Utils::Theme::Color color);
|
||||
|
||||
enum Priority
|
||||
{
|
||||
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 addHighlight(Highlight highlight);
|
||||
|
||||
void removeHighlights(Id id);
|
||||
void removeAllHighlights();
|
||||
|
||||
@@ -635,14 +635,6 @@ void TextEditorWidgetPrivate::setupScrollBar()
|
||||
if (m_highlightScrollBar)
|
||||
return;
|
||||
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);
|
||||
highlightSearchResultsInScrollBar();
|
||||
scheduleUpdateHighlightScrollBar();
|
||||
@@ -4767,11 +4759,11 @@ void TextEditorWidgetPrivate::updateCurrentLineInScrollbar()
|
||||
if (m_highlightScrollBar->maximum() > 0) {
|
||||
const QTextCursor &tc = q->textCursor();
|
||||
if (QTextLayout *layout = tc.block().layout()) {
|
||||
const int lineNumberInBlock =
|
||||
const int pos = q->textCursor().block().firstLineNumber() +
|
||||
layout->lineForTextPosition(tc.positionInBlock()).lineNumber();
|
||||
m_highlightScrollBar->addHighlight(
|
||||
Constants::SCROLL_BAR_CURRENT_LINE,
|
||||
q->textCursor().block().firstLineNumber() + lineNumberInBlock);
|
||||
m_highlightScrollBar->addHighlight({Constants::SCROLL_BAR_CURRENT_LINE, pos,
|
||||
Theme::TextEditor_CurrentLine_ScrollBarColor,
|
||||
Highlight::HighestPriority});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5745,40 +5737,47 @@ void TextEditorWidgetPrivate::scheduleUpdateHighlightScrollBar()
|
||||
QTimer::singleShot(0, this, &TextEditorWidgetPrivate::updateHighlightScrollBarNow);
|
||||
}
|
||||
|
||||
HighlightScrollBar::Priority textMarkPrioToScrollBarPrio(const TextMark::Priority &prio)
|
||||
Highlight::Priority textMarkPrioToScrollBarPrio(const TextMark::Priority &prio)
|
||||
{
|
||||
switch (prio) {
|
||||
case TextMark::LowPriority:
|
||||
return HighlightScrollBar::LowPriority;
|
||||
return Highlight::LowPriority;
|
||||
case TextMark::NormalPriority:
|
||||
return HighlightScrollBar::NormalPriority;
|
||||
return Highlight::NormalPriority;
|
||||
case TextMark::HighPriority:
|
||||
return HighlightScrollBar::HighPriority;
|
||||
return Highlight::HighPriority;
|
||||
default:
|
||||
return HighlightScrollBar::NormalPriority;
|
||||
return Highlight::NormalPriority;
|
||||
}
|
||||
}
|
||||
|
||||
void TextEditorWidgetPrivate::addSearchResultsToScrollBar(QVector<SearchResult> results)
|
||||
{
|
||||
QSet<int> searchResults;
|
||||
if (!m_highlightScrollBar)
|
||||
return;
|
||||
foreach (SearchResult result, results) {
|
||||
const QTextBlock &block = q->document()->findBlock(result.start);
|
||||
if (block.isValid() && block.isVisible()) {
|
||||
const int firstLine = block.layout()->lineForTextPosition(result.start - block.position()).lineNumber();
|
||||
const int lastLine = block.layout()->lineForTextPosition(result.start - block.position() + result.length).lineNumber();
|
||||
for (int line = firstLine; line <= lastLine; ++line)
|
||||
searchResults << block.firstLineNumber() + line;
|
||||
for (int line = firstLine; line <= lastLine; ++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()
|
||||
{
|
||||
typedef QSet<int> IntSet;
|
||||
|
||||
m_scrollBarUpdateScheduled = false;
|
||||
if (!m_highlightScrollBar)
|
||||
return;
|
||||
@@ -5791,21 +5790,13 @@ void TextEditorWidgetPrivate::updateHighlightScrollBarNow()
|
||||
addSearchResultsToScrollBar(m_searchResults);
|
||||
|
||||
// update text marks
|
||||
QHash<Id, IntSet> marks;
|
||||
foreach (TextMark *mark, m_document->marks()) {
|
||||
Id category = mark->category();
|
||||
if (!mark->isVisible() || !TextMark::categoryHasColor(category))
|
||||
continue;
|
||||
m_highlightScrollBar->setPriority(category, textMarkPrioToScrollBarPrio(mark->priority()));
|
||||
const QTextBlock &block = q->document()->findBlockByNumber(mark->lineNumber() - 1);
|
||||
if (block.isVisible())
|
||||
marks[category] << 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());
|
||||
m_highlightScrollBar->addHighlight(markToHighlight(mark, block.firstLineNumber()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user