Open Source Snapmatic picture and Savegame viewer/editor for GTA V https://gta5view.syping.de/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JSHighlighter.cpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*****************************************************************************
  2. * gta5view Grand Theft Auto V Profile Viewer
  3. * Copyright (C) 2017 Syping
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *****************************************************************************/
  18. #include "JSHighlighter.h"
  19. #include <QRegExp>
  20. JSHighlighter::JSHighlighter(QTextDocument *parent) :
  21. QSyntaxHighlighter(parent)
  22. {
  23. HighlightingRule rule;
  24. QBrush keywordBrush(QColor::fromRgb(66, 137, 244));
  25. keywordFormat.setForeground(keywordBrush);
  26. keywordFormat.setFontItalic(true);
  27. QStringList keywordPatterns;
  28. keywordPatterns << "\\btrue\\b" << "\\bfalse\\b";
  29. for (QString pattern : keywordPatterns)
  30. {
  31. rule.pattern = QRegExp(pattern);
  32. rule.format = keywordFormat;
  33. highlightingRules.append(rule);
  34. }
  35. QBrush doubleBrush(QColor::fromRgb(66, 137, 244));
  36. doubleFormat.setForeground(doubleBrush);
  37. rule.pattern = QRegExp("[+-]?\\d*\\.?\\d+");
  38. rule.format = doubleFormat;
  39. highlightingRules.append(rule);
  40. QBrush quotationBrush(QColor::fromRgb(66, 244, 104));
  41. quotationFormat.setForeground(quotationBrush);
  42. rule.pattern = QRegExp("\"[^\"]*\"");
  43. rule.format = quotationFormat;
  44. highlightingRules.append(rule);
  45. QBrush objectBrush(QColor::fromRgb(255, 80, 80));
  46. objectFormat.setForeground(objectBrush);
  47. rule.pattern = QRegExp("\"[^\"]*\"(?=:)");
  48. rule.format = objectFormat;
  49. highlightingRules.append(rule);
  50. }
  51. void JSHighlighter::highlightBlock(const QString &text)
  52. {
  53. for (HighlightingRule rule : highlightingRules)
  54. {
  55. QRegExp expression(rule.pattern);
  56. int index = expression.indexIn(text);
  57. while (index >= 0)
  58. {
  59. int length = expression.matchedLength();
  60. setFormat(index, length, rule.format);
  61. index = expression.indexIn(text, index + length);
  62. }
  63. }
  64. setCurrentBlockState(0);
  65. }