2017-11-22 14:59:40 +01:00
|
|
|
/*****************************************************************************
|
2018-05-24 22:32:00 +02:00
|
|
|
* gta5view Grand Theft Auto V Profile Viewer
|
2020-09-28 05:33:04 +02:00
|
|
|
* Copyright (C) 2017-2020 Syping
|
2017-11-22 14:59:40 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "JSHighlighter.h"
|
|
|
|
|
|
|
|
JSHighlighter::JSHighlighter(QTextDocument *parent) :
|
|
|
|
QSyntaxHighlighter(parent)
|
|
|
|
{
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
|
|
|
QBrush keywordBrush(QColor::fromRgb(66, 137, 244));
|
|
|
|
keywordFormat.setForeground(keywordBrush);
|
|
|
|
keywordFormat.setFontItalic(true);
|
|
|
|
QStringList keywordPatterns;
|
|
|
|
keywordPatterns << "\\btrue\\b" << "\\bfalse\\b";
|
|
|
|
for (QString pattern : keywordPatterns)
|
|
|
|
{
|
2020-09-28 05:33:04 +02:00
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
rule.pattern = QRegularExpression(pattern);
|
|
|
|
#else
|
2017-11-22 14:59:40 +01:00
|
|
|
rule.pattern = QRegExp(pattern);
|
2020-09-28 05:33:04 +02:00
|
|
|
#endif
|
2017-11-22 14:59:40 +01:00
|
|
|
rule.format = keywordFormat;
|
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
QBrush doubleBrush(QColor::fromRgb(66, 137, 244));
|
|
|
|
doubleFormat.setForeground(doubleBrush);
|
2020-09-28 05:33:04 +02:00
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
rule.pattern = QRegularExpression("[+-]?\\d*\\.?\\d+");
|
|
|
|
#else
|
2017-11-22 14:59:40 +01:00
|
|
|
rule.pattern = QRegExp("[+-]?\\d*\\.?\\d+");
|
2020-09-28 05:33:04 +02:00
|
|
|
#endif
|
2017-11-22 14:59:40 +01:00
|
|
|
rule.format = doubleFormat;
|
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
QBrush quotationBrush(QColor::fromRgb(66, 244, 104));
|
|
|
|
quotationFormat.setForeground(quotationBrush);
|
2020-09-28 05:33:04 +02:00
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
rule.pattern = QRegularExpression("\"[^\"]*\"");
|
|
|
|
#else
|
2017-11-22 14:59:40 +01:00
|
|
|
rule.pattern = QRegExp("\"[^\"]*\"");
|
2020-09-28 05:33:04 +02:00
|
|
|
#endif
|
2017-11-22 14:59:40 +01:00
|
|
|
rule.format = quotationFormat;
|
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
QBrush objectBrush(QColor::fromRgb(255, 80, 80));
|
|
|
|
objectFormat.setForeground(objectBrush);
|
2020-09-28 05:33:04 +02:00
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
rule.pattern = QRegularExpression("\"[^\"]*\"(?=:)");
|
|
|
|
#else
|
2017-11-22 14:59:40 +01:00
|
|
|
rule.pattern = QRegExp("\"[^\"]*\"(?=:)");
|
2020-09-28 05:33:04 +02:00
|
|
|
#endif
|
2017-11-22 14:59:40 +01:00
|
|
|
rule.format = objectFormat;
|
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSHighlighter::highlightBlock(const QString &text)
|
|
|
|
{
|
2020-09-28 05:33:04 +02:00
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
for (const HighlightingRule &rule : qAsConst(highlightingRules))
|
|
|
|
#else
|
|
|
|
for (const HighlightingRule &rule : highlightingRules)
|
|
|
|
#endif
|
2017-11-22 14:59:40 +01:00
|
|
|
{
|
2020-09-28 05:33:04 +02:00
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
|
|
|
|
while (matchIterator.hasNext()) {
|
|
|
|
QRegularExpressionMatch match = matchIterator.next();
|
|
|
|
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
|
|
|
|
}
|
|
|
|
#else
|
2017-11-22 14:59:40 +01:00
|
|
|
QRegExp expression(rule.pattern);
|
|
|
|
int index = expression.indexIn(text);
|
|
|
|
while (index >= 0)
|
|
|
|
{
|
|
|
|
int length = expression.matchedLength();
|
|
|
|
setFormat(index, length, rule.format);
|
|
|
|
index = expression.indexIn(text, index + length);
|
|
|
|
}
|
2020-09-28 05:33:04 +02:00
|
|
|
#endif
|
2017-11-22 14:59:40 +01:00
|
|
|
}
|
|
|
|
setCurrentBlockState(0);
|
|
|
|
}
|