ui: Fixed text color not correctly changed when annotation type changes

This commit is contained in:
Eberhard Graether
2017-09-03 22:18:20 +02:00
parent 9f2509e7de
commit 602ebbfe46
4 changed files with 23 additions and 62 deletions
+16 -49
View File
@@ -32,7 +32,6 @@ QtCodeField::QtCodeField(
, m_code(code)
, m_locationFile(locationFile)
, m_endTextEditPosition(0)
, m_wasAnnotated(false)
{
setObjectName("code_area");
setReadOnly(true);
@@ -153,14 +152,11 @@ void QtCodeField::paintEvent(QPaintEvent* event)
bottom = top + static_cast<int>(blockBoundingRect(block).height());
}
std::vector<std::pair<int, int>> ranges;
for (size_t i : m_colorChangedAnnotationIndices)
{
Annotation& annotation = m_annotations[i];
ranges.push_back(std::pair<int, int>(annotation.start, annotation.end));
}
m_highlighter->rehighlightLines(m_linesToRehighlight);
m_linesToRehighlight.clear();
m_highlighter->highlightRange(firstVisibleLine, lastVisibleLine, ranges);
// TODO: this causes another paint event if lines get rehighlighted
m_highlighter->highlightRange(firstVisibleLine, lastVisibleLine);
firstVisibleLine += m_startLineNumber;
lastVisibleLine += m_startLineNumber;
@@ -176,6 +172,13 @@ void QtCodeField::paintEvent(QPaintEvent* event)
const AnnotationColor& color = getAnnotationColorForAnnotation(annotation);
if (color.text != "transparent" &&
QColor(color.text.c_str()) != m_highlighter->getFormat(annotation.start, annotation.end).foreground().color())
{
// TODO: this causes another paint event if text color changes
setTextColorForAnnotation(annotation, QColor(color.text.c_str()));
}
if (color.border == "transparent" && color.fill == "transparent")
{
continue;
@@ -272,16 +275,11 @@ void QtCodeField::defocusTokenIds(const std::vector<Id>& tokenIds)
bool QtCodeField::annotateText(
const std::set<Id>& activeSymbolIds, const std::set<Id>& activeLocationIds, const std::set<Id>& focusedSymbolIds)
{
std::vector<int> linesToRehighlight;
bool needsUpdate = false;
for (size_t i = 0; i < m_annotations.size(); i++)
{
Annotation& annotation = m_annotations[i];
bool wasActive = annotation.isActive;
bool wasFocused = annotation.isFocused;
const AnnotationColor& oldColor = getAnnotationColorForAnnotation(annotation);
annotation.isActive = (
utility::shareElement(activeSymbolIds, annotation.tokenIds) ||
@@ -293,50 +291,19 @@ bool QtCodeField::annotateText(
annotation.isFocused = utility::shareElement(focusedSymbolIds, annotation.tokenIds);
}
const AnnotationColor& newColor = getAnnotationColorForAnnotation(annotation);
if (newColor.text != oldColor.text || (!m_wasAnnotated && newColor.text != "transparent"))
{
if (newColor.text.size() > 0 && newColor.text != "transparent")
{
if (!annotation.oldTextColor.isValid())
{
annotation.oldTextColor =
m_highlighter->getFormat(annotation.start, annotation.end).foreground().color();
}
setTextColorForAnnotation(annotation, QColor(newColor.text.c_str()));
m_colorChangedAnnotationIndices.insert(i);
}
else if (annotation.oldTextColor.isValid())
{
setTextColorForAnnotation(annotation, annotation.oldTextColor);
annotation.oldTextColor = QColor();
m_colorChangedAnnotationIndices.erase(i);
linesToRehighlight.push_back(annotation.startLine - 1);
}
}
if (wasFocused != annotation.isFocused || wasActive != annotation.isActive)
{
needsUpdate = true;
m_linesToRehighlight.push_back(annotation.startLine - m_startLineNumber);
}
}
if (linesToRehighlight.size())
{
m_highlighter->rehighlightLines(linesToRehighlight);
}
needsUpdate = (needsUpdate && m_wasAnnotated);
if (needsUpdate)
if (m_linesToRehighlight.size())
{
viewport()->update();
return true;
}
m_wasAnnotated = true;
return needsUpdate;
return false;
}
void QtCodeField::createAnnotations(std::shared_ptr<SourceLocationFile> locationFile)
@@ -632,7 +599,7 @@ const QtCodeField::AnnotationColor& QtCodeField::getAnnotationColorForAnnotation
return s_annotationColors[i];
}
void QtCodeField::setTextColorForAnnotation(Annotation& annotation, QColor color) const
void QtCodeField::setTextColorForAnnotation(const Annotation& annotation, QColor color) const
{
QTextCharFormat format;
format.setForeground(color);
+2 -5
View File
@@ -68,8 +68,6 @@ protected:
bool isActive;
bool isFocused;
QColor oldTextColor;
};
struct AnnotationColor
@@ -95,7 +93,7 @@ protected:
std::vector<QRect> getCursorRectsForAnnotation(const Annotation& annotation) const;
const AnnotationColor& getAnnotationColorForAnnotation(const Annotation& annotation);
void setTextColorForAnnotation(Annotation& annotation, QColor color) const;
void setTextColorForAnnotation(const Annotation& annotation, QColor color) const;
std::vector<const Annotation*> getInteractiveAnnotationsForPosition(int pos) const;
@@ -115,10 +113,9 @@ private:
QtHighlighter* m_highlighter;
std::vector<int> m_lineLengths;
std::set<size_t> m_colorChangedAnnotationIndices;
std::vector<int> m_linesToRehighlight;
int m_endTextEditPosition;
bool m_wasAnnotated;
};
#endif // QT_CODE_FIELD_H
+4 -7
View File
@@ -163,7 +163,7 @@ void QtHighlighter::highlightDocument()
highlightMultiLineComments(&m_ranges);
}
void QtHighlighter::highlightRange(int startLine, int endLine, std::vector<std::pair<int, int>> ranges)
void QtHighlighter::highlightRange(int startLine, int endLine)
{
if (m_language == LANGUAGE_UNKNOWN)
{
@@ -190,13 +190,10 @@ void QtHighlighter::highlightRange(int startLine, int endLine, std::vector<std::
return;
}
QTextDocument* doc = document();
QTextBlock start = doc->findBlockByLineNumber(startLine);
QTextBlock end = doc->findBlockByLineNumber(endLine + 1);
ranges.insert(ranges.end(), m_ranges.begin(), m_ranges.end());
int index = startLine;
for (QTextBlock it = start; it != end; it = it.next())
{
@@ -204,7 +201,7 @@ void QtHighlighter::highlightRange(int startLine, int endLine, std::vector<std::
{
foreach (const HighlightingRule &rule, m_highlightingRules)
{
formatBlock(it, rule, &ranges, false);
formatBlock(it, rule, &m_ranges, false);
}
}
index++;
@@ -215,7 +212,7 @@ void QtHighlighter::highlightRange(int startLine, int endLine, std::vector<std::
{
if (!m_highlightedLines[index])
{
formatBlock(it, s_commentRule, &ranges, false);
formatBlock(it, s_commentRule, &m_ranges, false);
}
index++;
}
@@ -327,7 +324,7 @@ void QtHighlighter::formatBlock(
{
int length = expression.matchedLength();
if (!isInRange(pos + index, *ranges))
if (!isInRange(pos + index, *ranges) && !isInRange(pos + index + length, *ranges))
{
applyFormat(pos + index, pos + index + length, rule.format);
}
+1 -1
View File
@@ -16,7 +16,7 @@ public:
QtHighlighter(QTextDocument *parent, LanguageType language);
void highlightDocument();
void highlightRange(int startLine, int endLine, std::vector<std::pair<int, int>> ranges);
void highlightRange(int startLine, int endLine);
void rehighlightLines(const std::vector<int>& lines);