mirror-chatterino2/src/controllers/highlights/HighlightPhrase.cpp

120 lines
3.5 KiB
C++
Raw Normal View History

#include "controllers/highlights/HighlightPhrase.hpp"
namespace chatterino {
namespace {
const QString REGEX_START_BOUNDARY("(\\b|\\s|^)");
const QString REGEX_END_BOUNDARY("(\\b|\\s|$)");
} // namespace
Better Highlights: Fix Wrong Color for Migrated Phrases (#1568) * Better Highlights: Fix wrong color for migrated phrases Prior to this commit, no default color was set when an "old" highlight phrase (one added prior to #1320 / 5957551) was deserialized. This commit makes highlight phrases uses the default self-highlight color for these situations. This approach is reasonably sensible since that color is also similar to the old highlight color. Fixes #1565. * Update default self-highlight color The new default color was suggested in [1] by @RAnders00. Refer to the link for further information. [1]: https://github.com/Chatterino/chatterino2/issues/1565#issuecomment-590441625 * Theme: Remove highlight color As highlight color is independent of the selected theme now, the member has been removed from the `Theme` singleton. Instead, the fallback theme color is defined in `HighlightPhrase` now. Uses of `themes->messages.backgrounds.highlighted` have been replaced with `HighlightPhrase::FALLBACK_COLOR` accordingly. * Update src/controllers/highlights/HighlightPhrase.hpp attempt to just remove constexpr Co-Authored-By: Ruben Anders <ruben.anders@robotty.de> * Initialize FALLBACK_COLOR outside header file * Rename FALLBACK_COLOR to FALLBACK_HIGHLIGHT_COLOR In preparation for the next commit introducing a new variable. * Moved subscription highlight color into HighlightPhrase * Use actual subscription color as a fallback Co-authored-by: pajlada <rasmus.karlsson@pajlada.com> Co-authored-by: Ruben Anders <ruben.anders@robotty.de>
2020-03-22 13:14:07 +01:00
QColor HighlightPhrase::FALLBACK_HIGHLIGHT_COLOR = QColor(127, 63, 73, 127);
QColor HighlightPhrase::FALLBACK_REDEEMED_HIGHLIGHT_COLOR =
2020-08-22 16:48:40 +02:00
QColor(28, 126, 141, 60);
Better Highlights: Fix Wrong Color for Migrated Phrases (#1568) * Better Highlights: Fix wrong color for migrated phrases Prior to this commit, no default color was set when an "old" highlight phrase (one added prior to #1320 / 5957551) was deserialized. This commit makes highlight phrases uses the default self-highlight color for these situations. This approach is reasonably sensible since that color is also similar to the old highlight color. Fixes #1565. * Update default self-highlight color The new default color was suggested in [1] by @RAnders00. Refer to the link for further information. [1]: https://github.com/Chatterino/chatterino2/issues/1565#issuecomment-590441625 * Theme: Remove highlight color As highlight color is independent of the selected theme now, the member has been removed from the `Theme` singleton. Instead, the fallback theme color is defined in `HighlightPhrase` now. Uses of `themes->messages.backgrounds.highlighted` have been replaced with `HighlightPhrase::FALLBACK_COLOR` accordingly. * Update src/controllers/highlights/HighlightPhrase.hpp attempt to just remove constexpr Co-Authored-By: Ruben Anders <ruben.anders@robotty.de> * Initialize FALLBACK_COLOR outside header file * Rename FALLBACK_COLOR to FALLBACK_HIGHLIGHT_COLOR In preparation for the next commit introducing a new variable. * Moved subscription highlight color into HighlightPhrase * Use actual subscription color as a fallback Co-authored-by: pajlada <rasmus.karlsson@pajlada.com> Co-authored-by: Ruben Anders <ruben.anders@robotty.de>
2020-03-22 13:14:07 +01:00
QColor HighlightPhrase::FALLBACK_SUB_COLOR = QColor(196, 102, 255, 100);
bool HighlightPhrase::operator==(const HighlightPhrase &other) const
{
return std::tie(this->pattern_, this->hasSound_, this->hasAlert_,
this->isRegex_, this->isCaseSensitive_, this->soundUrl_,
this->color_) == std::tie(other.pattern_, other.hasSound_,
other.hasAlert_, other.isRegex_,
other.isCaseSensitive_,
other.soundUrl_, other.color_);
}
HighlightPhrase::HighlightPhrase(const QString &pattern, bool hasAlert,
bool hasSound, bool isRegex,
bool isCaseSensitive, const QString &soundUrl,
QColor color)
: pattern_(pattern)
, hasAlert_(hasAlert)
, hasSound_(hasSound)
, isRegex_(isRegex)
, isCaseSensitive_(isCaseSensitive)
, soundUrl_(soundUrl)
, regex_(isRegex_
? pattern
: REGEX_START_BOUNDARY + QRegularExpression::escape(pattern) +
REGEX_END_BOUNDARY,
QRegularExpression::UseUnicodePropertiesOption |
(isCaseSensitive_ ? QRegularExpression::NoPatternOption
: QRegularExpression::CaseInsensitiveOption))
{
this->color_ = std::make_shared<QColor>(color);
}
HighlightPhrase::HighlightPhrase(const QString &pattern, bool hasAlert,
bool hasSound, bool isRegex,
bool isCaseSensitive, const QString &soundUrl,
std::shared_ptr<QColor> color)
: pattern_(pattern)
, hasAlert_(hasAlert)
, hasSound_(hasSound)
, isRegex_(isRegex)
, isCaseSensitive_(isCaseSensitive)
, soundUrl_(soundUrl)
, color_(color)
, regex_(isRegex_
? pattern
: REGEX_START_BOUNDARY + QRegularExpression::escape(pattern) +
REGEX_END_BOUNDARY,
QRegularExpression::UseUnicodePropertiesOption |
(isCaseSensitive_ ? QRegularExpression::NoPatternOption
: QRegularExpression::CaseInsensitiveOption))
{
}
const QString &HighlightPhrase::getPattern() const
{
return this->pattern_;
}
bool HighlightPhrase::hasAlert() const
{
return this->hasAlert_;
}
bool HighlightPhrase::hasSound() const
{
return this->hasSound_;
}
bool HighlightPhrase::hasCustomSound() const
{
return !this->soundUrl_.isEmpty();
}
bool HighlightPhrase::isRegex() const
{
return this->isRegex_;
}
bool HighlightPhrase::isValid() const
{
return !this->pattern_.isEmpty() && this->regex_.isValid();
}
bool HighlightPhrase::isMatch(const QString &subject) const
{
return this->isValid() && this->regex_.match(subject).hasMatch();
}
bool HighlightPhrase::isCaseSensitive() const
{
return this->isCaseSensitive_;
}
const QUrl &HighlightPhrase::getSoundUrl() const
{
return this->soundUrl_;
}
const std::shared_ptr<QColor> HighlightPhrase::getColor() const
{
return this->color_;
}
} // namespace chatterino