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>
<border>#B2B2B2</border>
<fill>#EDEDED</fill>
<text>#000000</text>
</active>
</token>
<local_symbol>
@@ -133,14 +134,17 @@
<normal>
<border>transparent</border>
<fill>#80FF0000</fill>
<text>#000000</text>
</normal>
<focus>
<border>transparent</border>
<fill>#FFFF0000</fill>
<text>#000000</text>
</focus>
<active>
<border>transparent</border>
<fill>#FFFF0000</fill>
<text>#000000</text>
</active>
</error>
</selection>
+1
View File
@@ -99,6 +99,7 @@
<active>
<border>#B2B2B2</border>
<fill>#4A4A4A</fill>
<text>#F7F7F7</text>
</active>
</token>
<local_symbol>
@@ -53,22 +53,6 @@ void UndoRedoController::handleMessage(MessageActivateFile* message)
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)
{
if (m_lastCommand.message &&
@@ -7,7 +7,6 @@
#include "utility/messaging/MessageListener.h"
#include "utility/messaging/type/MessageActivateEdge.h"
#include "utility/messaging/type/MessageActivateFile.h"
#include "utility/messaging/type/MessageActivateLocalSymbols.h"
#include "utility/messaging/type/MessageActivateNodes.h"
#include "utility/messaging/type/MessageActivateTokenIds.h"
#include "utility/messaging/type/MessageChangeFileView.h"
@@ -33,7 +32,6 @@ class UndoRedoController
: public Controller
, public MessageListener<MessageActivateEdge>
, public MessageListener<MessageActivateFile>
, public MessageListener<MessageActivateLocalSymbols>
, public MessageListener<MessageActivateNodes>
, public MessageListener<MessageActivateTokenIds>
, public MessageListener<MessageChangeFileView>
@@ -67,7 +65,6 @@ private:
virtual void handleMessage(MessageActivateEdge* message);
virtual void handleMessage(MessageActivateFile* message);
virtual void handleMessage(MessageActivateLocalSymbols* message);
virtual void handleMessage(MessageActivateNodes* message);
virtual void handleMessage(MessageActivateTokenIds* message);
virtual void handleMessage(MessageChangeFileView* message);
+42 -9
View File
@@ -96,6 +96,7 @@ QtCodeArea::QtCodeArea(
, m_eventPosition(0, 0)
, m_isActiveFile(false)
, m_lineNumbersHidden(false)
, m_wasAnnotated(false)
{
setObjectName("code_area");
setReadOnly(true);
@@ -114,8 +115,6 @@ QtCodeArea::QtCodeArea(
}
setPlainText(QString::fromUtf8(displayCode.c_str()));
createAnnotations(locationFile);
annotateText();
m_digits = lineNumberDigits();
updateLineNumberAreaWidth();
@@ -130,6 +129,11 @@ QtCodeArea::QtCodeArea(
horizontalScrollBar()->installEventFilter(new MouseWheelOverScrollbarFilter(this));
createActions();
m_highlighter->highlightDocument();
createAnnotations(locationFile);
annotateText();
}
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 wasFocused = annotation.isFocused;
const AnnotationColor& oldColor = getAnnotationColorForAnnotation(annotation);
annotation.isActive = (
std::find(activeTokenIds.begin(), activeTokenIds.end(), annotation.tokenId) != activeTokenIds.end() ||
@@ -695,6 +697,21 @@ void QtCodeArea::annotateText()
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)
{
needsUpdate = true;
@@ -706,6 +723,8 @@ void QtCodeArea::annotateText()
m_lineNumberArea->update();
viewport()->update();
}
m_wasAnnotated = true;
}
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)
{
// 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);
}
@@ -849,7 +869,12 @@ std::vector<QRect> QtCodeArea::getCursorRectsForAnnotation(const Annotation& ann
}
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++;
@@ -886,6 +911,7 @@ const QtCodeArea::AnnotationColor& QtCodeArea::getAnnotationColorForAnnotation(c
AnnotationColor color;
color.border = scheme->getColor("code/snippet/selection/" + type + "/" + state + "/border");
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);
}
}
@@ -918,6 +944,13 @@ const QtCodeArea::AnnotationColor& QtCodeArea::getAnnotationColorForAnnotation(c
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()
{
m_setIDECursorPositionAction = new QAction(tr("Set IDE Cursor"), this);
+5
View File
@@ -130,12 +130,15 @@ private:
bool isActive;
bool isFocused;
QColor oldTextColor;
};
struct AnnotationColor
{
std::string border;
std::string fill;
std::string text;
};
std::vector<const Annotation*> getNonScopeAnnotationsForPosition(int pos) const;
@@ -156,6 +159,7 @@ private:
std::vector<QRect> getCursorRectsForAnnotation(const Annotation& annotation) const;
const AnnotationColor& getAnnotationColorForAnnotation(const Annotation& annotation);
void setTextColorForAnnotation(Annotation& annotation, QColor color) const;
void createActions();
@@ -183,6 +187,7 @@ private:
bool m_isActiveFile;
bool m_lineNumbersHidden;
bool m_wasAnnotated;
};
#endif // QT_CODE_AREA_H
+41 -23
View File
@@ -5,8 +5,11 @@
#include "settings/ColorScheme.h"
QtHighlighter::QtHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
QVector<QtHighlighter::HighlightingRule> QtHighlighter::s_highlightingRules;
QtHighlighter::HighlightingRule QtHighlighter::s_quotationRule;
QtHighlighter::HighlightingRule QtHighlighter::s_commentRule;
void QtHighlighter::createHighlightingRules()
{
QStringList keywordPatterns;
keywordPatterns
@@ -41,47 +44,60 @@ QtHighlighter::QtHighlighter(QTextDocument *parent)
QColor quotationColor(scheme->getSyntaxColor("quotation").c_str());
QColor commentColor = scheme->getSyntaxColor("comment").c_str();
s_highlightingRules.clear();
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)
{
addHighlightingRule(typeColor, QRegExp("\\b" + pattern + "\\b"));
s_highlightingRules.append(HighlightingRule(typeColor, QRegExp("\\b" + pattern + "\\b")));
}
addHighlightingRule(directiveColor, directiveRegExp);
addHighlightingRule(numberColor, numberRegExp);
addHighlightingRule(functionColor, functionRegExp);
addHighlightingRule(quotationColor, quotation2RegExp);
s_highlightingRules.append(HighlightingRule(directiveColor, directiveRegExp));
s_highlightingRules.append(HighlightingRule(numberColor, numberRegExp));
s_highlightingRules.append(HighlightingRule(functionColor, functionRegExp));
s_highlightingRules.append(HighlightingRule(quotationColor, quotation2RegExp));
m_quotationRule = HighlightingRule(quotationColor, quotationRegExp);
m_commentRule = HighlightingRule(commentColor, commentRegExp);
s_quotationRule = HighlightingRule(quotationColor, quotationRegExp);
s_commentRule = HighlightingRule(commentColor, commentRegExp);
}
void QtHighlighter::clearHighlightingRules()
{
s_highlightingRules.clear();
}
QtHighlighter::QtHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
}
void QtHighlighter::highlightBlock(const QString& text)
{
if (currentBlock().blockNumber() == 0)
{
highlightDocument();
}
}
void QtHighlighter::highlightDocument()
{
if (!s_highlightingRules.size())
{
createHighlightingRules();
}
QTextDocument* doc = document();
std::vector<std::pair<int, int>> ranges;
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())
{
foreach (const HighlightingRule &rule, m_highlightingRules)
foreach (const HighlightingRule &rule, s_highlightingRules)
{
formatBlock(it, rule, &ranges, false);
}
@@ -91,7 +107,7 @@ void QtHighlighter::highlightDocument()
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;
}
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()));
cursorStart = cursorEnd;
@@ -145,11 +161,6 @@ QtHighlighter::HighlightingRule::HighlightingRule(const QColor& color, const QRe
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
{
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.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
public:
static void createHighlightingRules();
static void clearHighlightingRules();
QtHighlighter(QTextDocument *parent = 0);
void highlightDocument();
void applyFormat(int startPosition, int endPosition, const QTextCharFormat& format);
QTextCharFormat getFormat(int startPosition, int endPosition) const;
protected:
void highlightBlock(const QString& text);
@@ -27,18 +34,14 @@ private:
QTextCharFormat format;
};
void highlightDocument();
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;
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;
HighlightingRule m_quotationRule;
HighlightingRule m_commentRule;
static QVector<HighlightingRule> s_highlightingRules;
static HighlightingRule s_quotationRule;
static HighlightingRule s_commentRule;
};
#endif // QT_HIGHLIGHTER_H
+3
View File
@@ -6,6 +6,7 @@
#include "qt/element/QtCodeArea.h"
#include "qt/element/QtCodeFileList.h"
#include "qt/utility/QtHighlighter.h"
#include "qt/view/QtViewWidgetWrapper.h"
#include "settings/ColorScheme.h"
@@ -121,7 +122,9 @@ void QtCodeView::doRefreshView()
{
setStyleSheet();
m_widget->clearCodeSnippets();
QtCodeArea::clearAnnotationColors();
QtHighlighter::clearHighlightingRules();
}
void QtCodeView::doClear()