ui: Text color for highlights in code view

* Deactivate local symbols by clicking into void
* Refactored QtHightlighter to only highlight once
* Removed undo for local symbol activation
This commit is contained in:
Eberhard Graether
2016-04-18 12:34:35 +02:00
parent 985be86da0
commit 325bc6ac83
9 changed files with 106 additions and 58 deletions
+4
View File
@@ -99,6 +99,7 @@
<active> <active>
<border>#B2B2B2</border> <border>#B2B2B2</border>
<fill>#EDEDED</fill> <fill>#EDEDED</fill>
<text>#000000</text>
</active> </active>
</token> </token>
<local_symbol> <local_symbol>
@@ -133,14 +134,17 @@
<normal> <normal>
<border>transparent</border> <border>transparent</border>
<fill>#80FF0000</fill> <fill>#80FF0000</fill>
<text>#000000</text>
</normal> </normal>
<focus> <focus>
<border>transparent</border> <border>transparent</border>
<fill>#FFFF0000</fill> <fill>#FFFF0000</fill>
<text>#000000</text>
</focus> </focus>
<active> <active>
<border>transparent</border> <border>transparent</border>
<fill>#FFFF0000</fill> <fill>#FFFF0000</fill>
<text>#000000</text>
</active> </active>
</error> </error>
</selection> </selection>
+1
View File
@@ -99,6 +99,7 @@
<active> <active>
<border>#B2B2B2</border> <border>#B2B2B2</border>
<fill>#4A4A4A</fill> <fill>#4A4A4A</fill>
<text>#F7F7F7</text>
</active> </active>
</token> </token>
<local_symbol> <local_symbol>
@@ -53,22 +53,6 @@ void UndoRedoController::handleMessage(MessageActivateFile* message)
processCommand(command); processCommand(command);
} }
void UndoRedoController::handleMessage(MessageActivateLocalSymbols* message)
{
if (m_lastCommand.message &&
m_lastCommand.message->getType() == message->getType() &&
utility::isPermutation(
message->symbolIds,
static_cast<MessageActivateLocalSymbols*>(m_lastCommand.message.get())->symbolIds)
)
{
return;
}
Command command(std::make_shared<MessageActivateLocalSymbols>(*message), 1);
processCommand(command);
}
void UndoRedoController::handleMessage(MessageActivateNodes* message) void UndoRedoController::handleMessage(MessageActivateNodes* message)
{ {
if (m_lastCommand.message && if (m_lastCommand.message &&
@@ -7,7 +7,6 @@
#include "utility/messaging/MessageListener.h" #include "utility/messaging/MessageListener.h"
#include "utility/messaging/type/MessageActivateEdge.h" #include "utility/messaging/type/MessageActivateEdge.h"
#include "utility/messaging/type/MessageActivateFile.h" #include "utility/messaging/type/MessageActivateFile.h"
#include "utility/messaging/type/MessageActivateLocalSymbols.h"
#include "utility/messaging/type/MessageActivateNodes.h" #include "utility/messaging/type/MessageActivateNodes.h"
#include "utility/messaging/type/MessageActivateTokenIds.h" #include "utility/messaging/type/MessageActivateTokenIds.h"
#include "utility/messaging/type/MessageChangeFileView.h" #include "utility/messaging/type/MessageChangeFileView.h"
@@ -33,7 +32,6 @@ class UndoRedoController
: public Controller : public Controller
, public MessageListener<MessageActivateEdge> , public MessageListener<MessageActivateEdge>
, public MessageListener<MessageActivateFile> , public MessageListener<MessageActivateFile>
, public MessageListener<MessageActivateLocalSymbols>
, public MessageListener<MessageActivateNodes> , public MessageListener<MessageActivateNodes>
, public MessageListener<MessageActivateTokenIds> , public MessageListener<MessageActivateTokenIds>
, public MessageListener<MessageChangeFileView> , public MessageListener<MessageChangeFileView>
@@ -67,7 +65,6 @@ private:
virtual void handleMessage(MessageActivateEdge* message); virtual void handleMessage(MessageActivateEdge* message);
virtual void handleMessage(MessageActivateFile* message); virtual void handleMessage(MessageActivateFile* message);
virtual void handleMessage(MessageActivateLocalSymbols* message);
virtual void handleMessage(MessageActivateNodes* message); virtual void handleMessage(MessageActivateNodes* message);
virtual void handleMessage(MessageActivateTokenIds* message); virtual void handleMessage(MessageActivateTokenIds* message);
virtual void handleMessage(MessageChangeFileView* message); virtual void handleMessage(MessageChangeFileView* message);
+42 -9
View File
@@ -96,6 +96,7 @@ QtCodeArea::QtCodeArea(
, m_eventPosition(0, 0) , m_eventPosition(0, 0)
, m_isActiveFile(false) , m_isActiveFile(false)
, m_lineNumbersHidden(false) , m_lineNumbersHidden(false)
, m_wasAnnotated(false)
{ {
setObjectName("code_area"); setObjectName("code_area");
setReadOnly(true); setReadOnly(true);
@@ -114,8 +115,6 @@ QtCodeArea::QtCodeArea(
} }
setPlainText(QString::fromUtf8(displayCode.c_str())); setPlainText(QString::fromUtf8(displayCode.c_str()));
createAnnotations(locationFile);
annotateText();
m_digits = lineNumberDigits(); m_digits = lineNumberDigits();
updateLineNumberAreaWidth(); updateLineNumberAreaWidth();
@@ -130,6 +129,11 @@ QtCodeArea::QtCodeArea(
horizontalScrollBar()->installEventFilter(new MouseWheelOverScrollbarFilter(this)); horizontalScrollBar()->installEventFilter(new MouseWheelOverScrollbarFilter(this));
createActions(); createActions();
m_highlighter->highlightDocument();
createAnnotations(locationFile);
annotateText();
} }
QtCodeArea::~QtCodeArea() QtCodeArea::~QtCodeArea()
@@ -602,12 +606,9 @@ void QtCodeArea::activateLocalSymbols(const std::vector<const Annotation*>& anno
} }
} }
if (!allActive) if (!allActive || !localSymbolIds.size())
{ {
if (localSymbolIds.size()) MessageActivateLocalSymbols(localSymbolIds).dispatch();
{
MessageActivateLocalSymbols(localSymbolIds).dispatch();
}
} }
} }
@@ -686,6 +687,7 @@ void QtCodeArea::annotateText()
{ {
bool wasActive = annotation.isActive; bool wasActive = annotation.isActive;
bool wasFocused = annotation.isFocused; bool wasFocused = annotation.isFocused;
const AnnotationColor& oldColor = getAnnotationColorForAnnotation(annotation);
annotation.isActive = ( annotation.isActive = (
std::find(activeTokenIds.begin(), activeTokenIds.end(), annotation.tokenId) != activeTokenIds.end() || std::find(activeTokenIds.begin(), activeTokenIds.end(), annotation.tokenId) != activeTokenIds.end() ||
@@ -695,6 +697,21 @@ void QtCodeArea::annotateText()
annotation.isError = isError; annotation.isError = isError;
const AnnotationColor& newColor = getAnnotationColorForAnnotation(annotation);
if ((newColor.text != oldColor.text || !m_wasAnnotated))
{
if (newColor.text.size() > 0 && newColor.text != "transparent")
{
annotation.oldTextColor = m_highlighter->getFormat(annotation.start, annotation.end).foreground().color();
setTextColorForAnnotation(annotation, QColor(newColor.text.c_str()));
}
else if (annotation.oldTextColor.isValid())
{
setTextColorForAnnotation(annotation, annotation.oldTextColor);
annotation.oldTextColor = QColor();
}
}
if (wasFocused != annotation.isFocused || wasActive != annotation.isActive) if (wasFocused != annotation.isFocused || wasActive != annotation.isActive)
{ {
needsUpdate = true; needsUpdate = true;
@@ -706,6 +723,8 @@ void QtCodeArea::annotateText()
m_lineNumberArea->update(); m_lineNumberArea->update();
viewport()->update(); viewport()->update();
} }
m_wasAnnotated = true;
} }
void QtCodeArea::setHoveredAnnotations(const std::vector<const Annotation*>& annotations) void QtCodeArea::setHoveredAnnotations(const std::vector<const Annotation*>& annotations)
@@ -838,7 +857,8 @@ std::vector<QRect> QtCodeArea::getCursorRectsForAnnotation(const Annotation& ann
if (line == annotation.endLine) if (line == annotation.endLine)
{ {
// Avoid that annotations at line end span down to first column of the next line. // Avoid that annotations at line end span down to first column of the next line.
if (annotation.startLine != annotation.endLine || document()->findBlockByLineNumber(line - m_startLineNumber).length() != annotation.endCol) if (annotation.startLine != annotation.endLine ||
document()->findBlockByLineNumber(line - m_startLineNumber).length() != annotation.endCol)
{ {
cursor.setPosition(annotation.end); cursor.setPosition(annotation.end);
} }
@@ -849,7 +869,12 @@ std::vector<QRect> QtCodeArea::getCursorRectsForAnnotation(const Annotation& ann
} }
rectEnd = cursorRect(cursor); rectEnd = cursorRect(cursor);
rects.push_back(QRect(rectStart.left(), rectStart.top(), rectEnd.right() - rectStart.left(), rectEnd.bottom() - rectStart.top())); rects.push_back(QRect(
rectStart.left(),
rectStart.top(),
rectEnd.right() - rectStart.left(),
rectEnd.bottom() - rectStart.top()
));
line++; line++;
@@ -886,6 +911,7 @@ const QtCodeArea::AnnotationColor& QtCodeArea::getAnnotationColorForAnnotation(c
AnnotationColor color; AnnotationColor color;
color.border = scheme->getColor("code/snippet/selection/" + type + "/" + state + "/border"); color.border = scheme->getColor("code/snippet/selection/" + type + "/" + state + "/border");
color.fill = scheme->getColor("code/snippet/selection/" + type + "/" + state + "/fill"); color.fill = scheme->getColor("code/snippet/selection/" + type + "/" + state + "/fill");
color.text = scheme->getColor("code/snippet/selection/" + type + "/" + state + "/text");
s_annotationColors.push_back(color); s_annotationColors.push_back(color);
} }
} }
@@ -918,6 +944,13 @@ const QtCodeArea::AnnotationColor& QtCodeArea::getAnnotationColorForAnnotation(c
return s_annotationColors[i]; return s_annotationColors[i];
} }
void QtCodeArea::setTextColorForAnnotation(Annotation& annotation, QColor color) const
{
QTextCharFormat format;
format.setForeground(color);
m_highlighter->applyFormat(annotation.start, annotation.end, format);
}
void QtCodeArea::createActions() void QtCodeArea::createActions()
{ {
m_setIDECursorPositionAction = new QAction(tr("Set IDE Cursor"), this); m_setIDECursorPositionAction = new QAction(tr("Set IDE Cursor"), this);
+5
View File
@@ -130,12 +130,15 @@ private:
bool isActive; bool isActive;
bool isFocused; bool isFocused;
QColor oldTextColor;
}; };
struct AnnotationColor struct AnnotationColor
{ {
std::string border; std::string border;
std::string fill; std::string fill;
std::string text;
}; };
std::vector<const Annotation*> getNonScopeAnnotationsForPosition(int pos) const; std::vector<const Annotation*> getNonScopeAnnotationsForPosition(int pos) const;
@@ -156,6 +159,7 @@ private:
std::vector<QRect> getCursorRectsForAnnotation(const Annotation& annotation) const; std::vector<QRect> getCursorRectsForAnnotation(const Annotation& annotation) const;
const AnnotationColor& getAnnotationColorForAnnotation(const Annotation& annotation); const AnnotationColor& getAnnotationColorForAnnotation(const Annotation& annotation);
void setTextColorForAnnotation(Annotation& annotation, QColor color) const;
void createActions(); void createActions();
@@ -183,6 +187,7 @@ private:
bool m_isActiveFile; bool m_isActiveFile;
bool m_lineNumbersHidden; bool m_lineNumbersHidden;
bool m_wasAnnotated;
}; };
#endif // QT_CODE_AREA_H #endif // QT_CODE_AREA_H
+41 -23
View File
@@ -5,8 +5,11 @@
#include "settings/ColorScheme.h" #include "settings/ColorScheme.h"
QtHighlighter::QtHighlighter(QTextDocument *parent) QVector<QtHighlighter::HighlightingRule> QtHighlighter::s_highlightingRules;
: QSyntaxHighlighter(parent) QtHighlighter::HighlightingRule QtHighlighter::s_quotationRule;
QtHighlighter::HighlightingRule QtHighlighter::s_commentRule;
void QtHighlighter::createHighlightingRules()
{ {
QStringList keywordPatterns; QStringList keywordPatterns;
keywordPatterns keywordPatterns
@@ -41,47 +44,60 @@ QtHighlighter::QtHighlighter(QTextDocument *parent)
QColor quotationColor(scheme->getSyntaxColor("quotation").c_str()); QColor quotationColor(scheme->getSyntaxColor("quotation").c_str());
QColor commentColor = scheme->getSyntaxColor("comment").c_str(); QColor commentColor = scheme->getSyntaxColor("comment").c_str();
s_highlightingRules.clear();
foreach (const QString &pattern, keywordPatterns) foreach (const QString &pattern, keywordPatterns)
{ {
addHighlightingRule(keywordColor, QRegExp("\\b" + pattern + "\\b")); s_highlightingRules.append(HighlightingRule(keywordColor, QRegExp("\\b" + pattern + "\\b")));
} }
foreach (const QString &pattern, typePatterns) foreach (const QString &pattern, typePatterns)
{ {
addHighlightingRule(typeColor, QRegExp("\\b" + pattern + "\\b")); s_highlightingRules.append(HighlightingRule(typeColor, QRegExp("\\b" + pattern + "\\b")));
} }
addHighlightingRule(directiveColor, directiveRegExp); s_highlightingRules.append(HighlightingRule(directiveColor, directiveRegExp));
addHighlightingRule(numberColor, numberRegExp); s_highlightingRules.append(HighlightingRule(numberColor, numberRegExp));
addHighlightingRule(functionColor, functionRegExp); s_highlightingRules.append(HighlightingRule(functionColor, functionRegExp));
addHighlightingRule(quotationColor, quotation2RegExp); s_highlightingRules.append(HighlightingRule(quotationColor, quotation2RegExp));
m_quotationRule = HighlightingRule(quotationColor, quotationRegExp); s_quotationRule = HighlightingRule(quotationColor, quotationRegExp);
m_commentRule = HighlightingRule(commentColor, commentRegExp); s_commentRule = HighlightingRule(commentColor, commentRegExp);
}
void QtHighlighter::clearHighlightingRules()
{
s_highlightingRules.clear();
}
QtHighlighter::QtHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
} }
void QtHighlighter::highlightBlock(const QString& text) void QtHighlighter::highlightBlock(const QString& text)
{ {
if (currentBlock().blockNumber() == 0)
{
highlightDocument();
}
} }
void QtHighlighter::highlightDocument() void QtHighlighter::highlightDocument()
{ {
if (!s_highlightingRules.size())
{
createHighlightingRules();
}
QTextDocument* doc = document(); QTextDocument* doc = document();
std::vector<std::pair<int, int>> ranges; std::vector<std::pair<int, int>> ranges;
for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next()) for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next())
{ {
formatBlock(it, m_quotationRule, &ranges, true); formatBlock(it, s_quotationRule, &ranges, true);
} }
for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next()) for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next())
{ {
foreach (const HighlightingRule &rule, m_highlightingRules) foreach (const HighlightingRule &rule, s_highlightingRules)
{ {
formatBlock(it, rule, &ranges, false); formatBlock(it, rule, &ranges, false);
} }
@@ -91,7 +107,7 @@ void QtHighlighter::highlightDocument()
for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next()) for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next())
{ {
formatBlock(it, m_commentRule, &ranges, true); formatBlock(it, s_commentRule, &ranges, true);
} }
} }
@@ -128,7 +144,7 @@ void QtHighlighter::highlightMultiLineComments(std::vector<std::pair<int, int>>*
break; break;
} }
applyFormat(cursorStart.selectionStart(), cursorEnd.position(), m_commentRule.format); applyFormat(cursorStart.selectionStart(), cursorEnd.position(), s_commentRule.format);
ranges->push_back(std::pair<int, int>(cursorStart.selectionStart(), cursorEnd.position())); ranges->push_back(std::pair<int, int>(cursorStart.selectionStart(), cursorEnd.position()));
cursorStart = cursorEnd; cursorStart = cursorEnd;
@@ -145,11 +161,6 @@ QtHighlighter::HighlightingRule::HighlightingRule(const QColor& color, const QRe
pattern = regExp; pattern = regExp;
} }
void QtHighlighter::addHighlightingRule(const QColor& color, const QRegExp& regExp)
{
m_highlightingRules.append(HighlightingRule(color, regExp));
}
bool QtHighlighter::isInRange(int pos, const std::vector<std::pair<int, int>>& ranges) const bool QtHighlighter::isInRange(int pos, const std::vector<std::pair<int, int>>& ranges) const
{ {
for (const std::pair<int, int> p : ranges) for (const std::pair<int, int> p : ranges)
@@ -197,3 +208,10 @@ void QtHighlighter::applyFormat(int startPosition, int endPosition, const QTextC
cursor.setPosition(endPosition, QTextCursor::KeepAnchor); cursor.setPosition(endPosition, QTextCursor::KeepAnchor);
cursor.setCharFormat(format); cursor.setCharFormat(format);
} }
QTextCharFormat QtHighlighter::getFormat(int startPosition, int endPosition) const
{
QTextCursor cursor(document());
cursor.setPosition(endPosition);
return cursor.charFormat();
}
+10 -7
View File
@@ -12,7 +12,14 @@ class QtHighlighter
Q_OBJECT Q_OBJECT
public: public:
static void createHighlightingRules();
static void clearHighlightingRules();
QtHighlighter(QTextDocument *parent = 0); QtHighlighter(QTextDocument *parent = 0);
void highlightDocument();
void applyFormat(int startPosition, int endPosition, const QTextCharFormat& format);
QTextCharFormat getFormat(int startPosition, int endPosition) const;
protected: protected:
void highlightBlock(const QString& text); void highlightBlock(const QString& text);
@@ -27,18 +34,14 @@ private:
QTextCharFormat format; QTextCharFormat format;
}; };
void highlightDocument();
void highlightMultiLineComments(std::vector<std::pair<int, int>>* ranges); void highlightMultiLineComments(std::vector<std::pair<int, int>>* ranges);
void addHighlightingRule(const QColor& color, const QRegExp& regExp);
bool isInRange(int index, const std::vector<std::pair<int, int>>& ranges) const; bool isInRange(int index, const std::vector<std::pair<int, int>>& ranges) const;
void formatBlock(const QTextBlock& block, const HighlightingRule& rule, std::vector<std::pair<int, int>>* ranges, bool saveRange); void formatBlock(const QTextBlock& block, const HighlightingRule& rule, std::vector<std::pair<int, int>>* ranges, bool saveRange);
void applyFormat(int startPosition, int endPosition, const QTextCharFormat& format);
QVector<HighlightingRule> m_highlightingRules; static QVector<HighlightingRule> s_highlightingRules;
HighlightingRule m_quotationRule; static HighlightingRule s_quotationRule;
HighlightingRule m_commentRule; static HighlightingRule s_commentRule;
}; };
#endif // QT_HIGHLIGHTER_H #endif // QT_HIGHLIGHTER_H
+3
View File
@@ -6,6 +6,7 @@
#include "qt/element/QtCodeArea.h" #include "qt/element/QtCodeArea.h"
#include "qt/element/QtCodeFileList.h" #include "qt/element/QtCodeFileList.h"
#include "qt/utility/QtHighlighter.h"
#include "qt/view/QtViewWidgetWrapper.h" #include "qt/view/QtViewWidgetWrapper.h"
#include "settings/ColorScheme.h" #include "settings/ColorScheme.h"
@@ -121,7 +122,9 @@ void QtCodeView::doRefreshView()
{ {
setStyleSheet(); setStyleSheet();
m_widget->clearCodeSnippets(); m_widget->clearCodeSnippets();
QtCodeArea::clearAnnotationColors(); QtCodeArea::clearAnnotationColors();
QtHighlighter::clearHighlightingRules();
} }
void QtCodeView::doClear() void QtCodeView::doClear()