Chatterino
HighlightPhrase.hpp
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include <QColor>
8 #include <QRegularExpression>
9 #include <QString>
10 #include <QUrl>
11 #include <pajlada/serialize.hpp>
12 
13 #include <memory>
14 
15 namespace chatterino {
16 
18 {
19 public:
20  bool operator==(const HighlightPhrase &other) const;
21 
27  HighlightPhrase(const QString &pattern, bool showInMentions, bool hasAlert,
28  bool hasSound, bool isRegex, bool isCaseSensitive,
29  const QString &soundUrl, QColor color);
30 
36  HighlightPhrase(const QString &pattern, bool showInMentions, bool hasAlert,
37  bool hasSound, bool isRegex, bool isCaseSensitive,
38  const QString &soundUrl, std::shared_ptr<QColor> color);
39 
40  const QString &getPattern() const;
41  bool showInMentions() const;
42  bool hasAlert() const;
43 
58  bool hasSound() const;
59 
69  bool hasCustomSound() const;
70 
71  bool isRegex() const;
72  bool isValid() const;
73  bool isMatch(const QString &subject) const;
74  bool isCaseSensitive() const;
75  const QUrl &getSoundUrl() const;
76  const std::shared_ptr<QColor> getColor() const;
77 
78  /*
79  * XXX: Use the constexpr constructor here once we are building with
80  * Qt>=5.13.
81  */
82  static QColor FALLBACK_HIGHLIGHT_COLOR;
84  static QColor FALLBACK_SUB_COLOR;
88 
89 private:
90  QString pattern_;
91  bool showInMentions_;
92  bool hasAlert_;
93  bool hasSound_;
94  bool isRegex_;
95  bool isCaseSensitive_;
96  QUrl soundUrl_;
97  std::shared_ptr<QColor> color_;
98  QRegularExpression regex_;
99 };
100 
101 } // namespace chatterino
102 
103 namespace pajlada {
104 
105 namespace {
106  chatterino::HighlightPhrase constructError()
107  {
108  return chatterino::HighlightPhrase(QString(), false, false, false,
109  false, false, QString(), QColor());
110  }
111 } // namespace
112 
113 template <>
114 struct Serialize<chatterino::HighlightPhrase> {
115  static rapidjson::Value get(const chatterino::HighlightPhrase &value,
116  rapidjson::Document::AllocatorType &a)
117  {
118  rapidjson::Value ret(rapidjson::kObjectType);
119 
120  chatterino::rj::set(ret, "pattern", value.getPattern(), a);
121  chatterino::rj::set(ret, "showInMentions", value.showInMentions(), a);
122  chatterino::rj::set(ret, "alert", value.hasAlert(), a);
123  chatterino::rj::set(ret, "sound", value.hasSound(), a);
124  chatterino::rj::set(ret, "regex", value.isRegex(), a);
125  chatterino::rj::set(ret, "case", value.isCaseSensitive(), a);
126  chatterino::rj::set(ret, "soundUrl", value.getSoundUrl().toString(), a);
127  chatterino::rj::set(ret, "color",
128  value.getColor()->name(QColor::HexArgb), a);
129 
130  return ret;
131  }
132 };
133 
134 template <>
135 struct Deserialize<chatterino::HighlightPhrase> {
136  static chatterino::HighlightPhrase get(const rapidjson::Value &value,
137  bool *error = nullptr)
138  {
139  if (!value.IsObject())
140  {
141  PAJLADA_REPORT_ERROR(error)
142 
143  return constructError();
144  }
145 
146  QString _pattern;
147  bool _showInMentions = true;
148  bool _hasAlert = true;
149  bool _hasSound = false;
150  bool _isRegex = false;
151  bool _isCaseSensitive = false;
152  QString _soundUrl;
153  QString encodedColor;
154 
155  chatterino::rj::getSafe(value, "pattern", _pattern);
156  chatterino::rj::getSafe(value, "showInMentions", _showInMentions);
157  chatterino::rj::getSafe(value, "alert", _hasAlert);
158  chatterino::rj::getSafe(value, "sound", _hasSound);
159  chatterino::rj::getSafe(value, "regex", _isRegex);
160  chatterino::rj::getSafe(value, "case", _isCaseSensitive);
161  chatterino::rj::getSafe(value, "soundUrl", _soundUrl);
162  chatterino::rj::getSafe(value, "color", encodedColor);
163 
164  auto _color = QColor(encodedColor);
165  if (!_color.isValid())
167 
168  return chatterino::HighlightPhrase(_pattern, _showInMentions, _hasAlert,
169  _hasSound, _isRegex,
170  _isCaseSensitive, _soundUrl, _color);
171  }
172 };
173 
174 } // namespace pajlada
bool isRegex() const
Definition: HighlightPhrase.cpp:102
static QColor FALLBACK_ELEVATED_MESSAGE_HIGHLIGHT_COLOR
Definition: HighlightPhrase.hpp:86
static QColor FALLBACK_HIGHLIGHT_COLOR
Definition: HighlightPhrase.hpp:82
static QColor FALLBACK_THREAD_HIGHLIGHT_COLOR
Definition: HighlightPhrase.hpp:87
bool hasAlert() const
Definition: HighlightPhrase.cpp:87
static QColor FALLBACK_SUB_COLOR
Definition: HighlightPhrase.hpp:84
bool operator==(const HighlightPhrase &other) const
Definition: HighlightPhrase.cpp:23
Definition: Application.cpp:48
bool showInMentions() const
Definition: HighlightPhrase.cpp:82
bool hasSound() const
Check if this highlight phrase should play a sound when triggered.
Definition: HighlightPhrase.cpp:92
HighlightPhrase(const QString &pattern, bool showInMentions, bool hasAlert, bool hasSound, bool isRegex, bool isCaseSensitive, const QString &soundUrl, QColor color)
Create a new HighlightPhrase.
Definition: HighlightPhrase.cpp:33
bool isValid() const
Definition: HighlightPhrase.cpp:107
const QUrl & getSoundUrl() const
Definition: HighlightPhrase.cpp:122
void set(rapidjson::Value &obj, const char *key, const Type &value, rapidjson::Document::AllocatorType &a)
Definition: RapidjsonHelpers.hpp:22
Definition: Command.hpp:25
const QString & getPattern() const
Definition: HighlightPhrase.cpp:77
bool getSafe(const rapidjson::Value &obj, const char *key, Type &out)
Definition: RapidjsonHelpers.hpp:73
static QColor FALLBACK_REDEEMED_HIGHLIGHT_COLOR
Definition: HighlightPhrase.hpp:83
const std::shared_ptr< QColor > getColor() const
Definition: HighlightPhrase.cpp:127
bool isMatch(const QString &subject) const
Definition: HighlightPhrase.cpp:112
bool isCaseSensitive() const
Definition: HighlightPhrase.cpp:117
static QColor FALLBACK_FIRST_MESSAGE_HIGHLIGHT_COLOR
Definition: HighlightPhrase.hpp:85
bool hasCustomSound() const
Check if this highlight phrase has a custom sound set.
Definition: HighlightPhrase.cpp:97
Definition: HighlightPhrase.hpp:17