| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/GameConfigHighlighter.h" |
| 5 | |
| 6 | #include <QBrush> |
| 7 | #include <QColor> |
| 8 | #include <QRegularExpression> |
| 9 | #include <QTextBlock> |
| 10 | #include <QTextCharFormat> |
| 11 | #include <QTextDocument> |
| 12 | |
| 13 | #include "DolphinQt/Settings.h" |
| 14 | |
| 15 | struct HighlightingRule |
| 16 | { |
| 17 | QRegularExpression pattern; |
| 18 | QTextCharFormat format; |
| 19 | }; |
| 20 | |
| 21 | GameConfigHighlighter::~GameConfigHighlighter() = default; |
| 22 | |
| 23 | GameConfigHighlighter::GameConfigHighlighter(QTextDocument* parent) : QObject(parent) |
| 24 | { |
| 25 | const bool is_dark_theme = Settings::Instance().IsThemeDark(); |
| 26 | |
| 27 | QTextCharFormat equal_format; |
| 28 | if (is_dark_theme) |
| 29 | equal_format.setForeground(QBrush{QColor(255, 96, 96)}); |
| 30 | else |
| 31 | equal_format.setForeground(Qt::red); |
| 32 | |
| 33 | QTextCharFormat section_format; |
| 34 | section_format.setFontWeight(QFont::Bold); |
| 35 | |
| 36 | QTextCharFormat comment_format; |
| 37 | if (is_dark_theme) |
| 38 | comment_format.setForeground(QBrush{QColor(0, 220, 0)}); |
| 39 | else |
| 40 | comment_format.setForeground(Qt::darkGreen); |
| 41 | comment_format.setFontItalic(true); |
| 42 | |
| 43 | QTextCharFormat const_format; |
| 44 | const_format.setFontWeight(QFont::Bold); |
| 45 | if (is_dark_theme) |
| 46 | const_format.setForeground(QBrush{QColor(132, 132, 255)}); |
| 47 | else |
| 48 | const_format.setForeground(Qt::blue); |
| 49 | |
| 50 | QTextCharFormat num_format; |
| 51 | if (is_dark_theme) |
| 52 | num_format.setForeground(QBrush{QColor(66, 138, 255)}); |
| 53 | else |
| 54 | num_format.setForeground(Qt::darkBlue); |
| 55 | |
| 56 | m_rules.emplace_back(HighlightingRule{QRegularExpression(QStringLiteral("=")), equal_format}); |
| 57 | m_rules.emplace_back( |
| 58 | HighlightingRule{QRegularExpression(QStringLiteral("^\\[.*?\\]")), section_format}); |
| 59 | m_rules.emplace_back( |
| 60 | HighlightingRule{QRegularExpression(QStringLiteral("\\bTrue\\b")), const_format}); |
| 61 | m_rules.emplace_back( |
| 62 | HighlightingRule{QRegularExpression(QStringLiteral("\\bFalse\\b")), const_format}); |
| 63 | m_rules.emplace_back( |
| 64 | HighlightingRule{QRegularExpression(QStringLiteral("\\b[0-9a-fA-F]+\\b")), num_format}); |
| 65 | |
| 66 | m_rules.emplace_back( |
| 67 | HighlightingRule{QRegularExpression(QStringLiteral("^#.*")), comment_format}); |
| 68 | m_rules.emplace_back( |
| 69 | HighlightingRule{QRegularExpression(QStringLiteral("^\\$.*")), comment_format}); |
| 70 | m_rules.emplace_back( |
| 71 | HighlightingRule{QRegularExpression(QStringLiteral("^\\*.*")), comment_format}); |
| 72 | |
| 73 | // Highlight block on change. |
| 74 | // We're manually highlighting blocks because QSyntaxHighlighter |
| 75 | // hangs with large (>2MB) files for some reason. |
| 76 | connect(parent, &QTextDocument::contentsChange, this, |
| 77 | [this, parent](const int pos, int /* removed */, const int added) { |
| 78 | QTextBlock block = parent->findBlock(pos); |
| 79 | const auto pos_end = pos + added; |
| 80 | while (block.isValid() && block.position() <= pos_end) |
| 81 | { |
| 82 | HighlightBlock(block); |
| 83 | block = block.next(); |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | // Highlight all blocks right now. |
| 88 | for (QTextBlock block = parent->begin(); block.isValid(); block = block.next()) |
| 89 | HighlightBlock(block); |
| 90 | } |
| 91 | |
| 92 | void GameConfigHighlighter::HighlightBlock(const QTextBlock& block) |
| 93 | { |
| 94 | QList<QTextLayout::FormatRange> format_ranges; |
| 95 | |
| 96 | for (const auto& rule : m_rules) |
| 97 | { |
| 98 | auto it = rule.pattern.globalMatch(block.text()); |
| 99 | while (it.hasNext()) |
| 100 | { |
| 101 | const auto match = it.next(); |
| 102 | format_ranges.emplace_back(QTextLayout::FormatRange{.start = int(match.capturedStart()), |
| 103 | .length = int(match.capturedLength()), |
| 104 | .format = rule.format}); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | block.layout()->clearFormats(); |
| 109 | block.layout()->setFormats(format_ranges); |
| 110 | } |