From 602ebbfe4686052973cb733ad991fba3a3abddca Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Sun, 3 Sep 2017 22:18:20 +0200 Subject: [PATCH] ui: Fixed text color not correctly changed when annotation type changes --- src/lib_gui/qt/element/QtCodeField.cpp | 65 ++++++------------------ src/lib_gui/qt/element/QtCodeField.h | 7 +-- src/lib_gui/qt/utility/QtHighlighter.cpp | 11 ++-- src/lib_gui/qt/utility/QtHighlighter.h | 2 +- 4 files changed, 23 insertions(+), 62 deletions(-) diff --git a/src/lib_gui/qt/element/QtCodeField.cpp b/src/lib_gui/qt/element/QtCodeField.cpp index 9d11af4b..9f4b42e7 100644 --- a/src/lib_gui/qt/element/QtCodeField.cpp +++ b/src/lib_gui/qt/element/QtCodeField.cpp @@ -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(blockBoundingRect(block).height()); } - std::vector> ranges; - for (size_t i : m_colorChangedAnnotationIndices) - { - Annotation& annotation = m_annotations[i]; - ranges.push_back(std::pair(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& tokenIds) bool QtCodeField::annotateText( const std::set& activeSymbolIds, const std::set& activeLocationIds, const std::set& focusedSymbolIds) { - std::vector 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 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); diff --git a/src/lib_gui/qt/element/QtCodeField.h b/src/lib_gui/qt/element/QtCodeField.h index 13cd0ea9..a33f7565 100644 --- a/src/lib_gui/qt/element/QtCodeField.h +++ b/src/lib_gui/qt/element/QtCodeField.h @@ -68,8 +68,6 @@ protected: bool isActive; bool isFocused; - - QColor oldTextColor; }; struct AnnotationColor @@ -95,7 +93,7 @@ protected: std::vector 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 getInteractiveAnnotationsForPosition(int pos) const; @@ -115,10 +113,9 @@ private: QtHighlighter* m_highlighter; std::vector m_lineLengths; - std::set m_colorChangedAnnotationIndices; + std::vector m_linesToRehighlight; int m_endTextEditPosition; - bool m_wasAnnotated; }; #endif // QT_CODE_FIELD_H diff --git a/src/lib_gui/qt/utility/QtHighlighter.cpp b/src/lib_gui/qt/utility/QtHighlighter.cpp index ae6f4453..72b0b920 100644 --- a/src/lib_gui/qt/utility/QtHighlighter.cpp +++ b/src/lib_gui/qt/utility/QtHighlighter.cpp @@ -163,7 +163,7 @@ void QtHighlighter::highlightDocument() highlightMultiLineComments(&m_ranges); } -void QtHighlighter::highlightRange(int startLine, int endLine, std::vector> 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::vectorfindBlockByLineNumber(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> ranges); + void highlightRange(int startLine, int endLine); void rehighlightLines(const std::vector& lines);