Skip to content

Commit

Permalink
Add support for favorites and kfl in search highlighter
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed Jul 12, 2022
1 parent 2b1976f commit 176250a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
62 changes: 45 additions & 17 deletions src/lib/src/search-syntax-highlighter.cpp
Original file line number Diff line number Diff line change
@@ -1,53 +1,81 @@
#include "search-syntax-highlighter.h"
#include <QColor>
#include <QRegularExpression>
#include "models/profile.h"


SearchSyntaxHighlighter::SearchSyntaxHighlighter(bool full, QTextDocument *parent)
: QSyntaxHighlighter(parent)
SearchSyntaxHighlighter::SearchSyntaxHighlighter(bool full, QTextDocument *parent, Profile *profile)
: QSyntaxHighlighter(parent), m_profile(profile)
{
favoritesFormat.setForeground(QColor("#ffc0cb"));
keptForLaterFormat.setForeground(QColor("#000000"));

HighlightingRule rule;

// Or format "~tag"
orFormat.setForeground(Qt::green);
rule.pattern = QRegularExpression("(?: |^)~([^ ]+)");
rule.format = orFormat;
rule.format.setForeground(Qt::green);
highlightingRules.append(rule);

// Exclusion format "-tag"
excludeFormat.setForeground(Qt::red);
rule.pattern = QRegularExpression("(?: |^)-([^ ]+)");
rule.format = excludeFormat;
rule.format.setForeground(Qt::red);
highlightingRules.append(rule);

if (!full) {
// Meta other format "unknown_meta:value"
metaOtherFormat.setForeground(QColor("#ff0000"));
rule.pattern = QRegularExpression("(?: |^)([^:]+):([^: ][^ ]*)?(?: |$)");
rule.format = metaOtherFormat;
rule.format.setForeground(QColor("#ff0000")); // red
highlightingRules.append(rule);
} else {
// MD5 format "qdrg15sdfgs1d2f1gs3dfg"
md5Format.setForeground(QColor("#800080"));
rule.pattern = QRegularExpression("(?: |^)([0-9A-F]{32})", QRegularExpression::CaseInsensitiveOption);
rule.format = md5Format;
rule.format.setForeground(QColor("#800080")); // purple
highlightingRules.append(rule);

// URL format "http://..."
urlFormat.setForeground(Qt::blue);
rule.pattern = QRegularExpression("(?: |^)(https?://[^\\s/$.?#].[^\\s]*)(?: |$)");
rule.format = urlFormat;
rule.format.setForeground(Qt::blue);
highlightingRules.append(rule);
}

// Meta format "meta:value"
metaFormat.setForeground(QColor("#a52a2a"));
rule.pattern = QRegularExpression("(?: |^)(user|fav|md5|pool|rating|source|status|approver|unlocked|sub|id|width|height|score|mpixels|filesize|filetype|date|gentags|arttags|chartags|copytags|status|status|approver|order|parent|sort|grabber):([^: ][^ ]*)?(?: |$)", QRegularExpression::CaseInsensitiveOption);
rule.format = metaFormat;
rule.format.setForeground(QColor("#a52a2a")); // brown
highlightingRules.append(rule);

if (m_profile != nullptr) {
// Favorites format "favorited_tag"
rule.pattern = QRegularExpression("(?: |^)(tomose_shunsaku|open_clothes)(?: |$)");
rule.format.setForeground(QColor("#ffc0cb")); // pink
highlightingRules.append(rule);
m_favoritesRule = &highlightingRules.last();

// Favorites format "kfl_tag"
rule.pattern = QRegularExpression("(?: |^)(kept|for_later)(?: |$)");
rule.format.setForeground(QColor("#000000")); // black
highlightingRules.append(rule);
m_kflRule = &highlightingRules.last();

updateFavorites();
updateKeptForLater();
connect(m_profile, &Profile::favoritesChanged, this, &SearchSyntaxHighlighter::updateFavorites);
connect(m_profile, &Profile::keptForLaterChanged, this, &SearchSyntaxHighlighter::updateKeptForLater);
}
}

void SearchSyntaxHighlighter::updateFavorites()
{
QString favorites;
for (const auto &favorite : m_profile->getFavorites()) {
if (!favorites.isEmpty()) {
favorites += '|';
}
favorites += favorite.getName();
}
m_favoritesRule->pattern.setPattern("(?: |^)(" + favorites + ")(?: |$)");
}

void SearchSyntaxHighlighter::updateKeptForLater()
{
m_kflRule->pattern.setPattern("(?: |^)(" + m_profile->getKeptForLater().join('|') + ")(?: |$)");
}

void SearchSyntaxHighlighter::highlightBlock(const QString &text)
Expand Down
19 changes: 9 additions & 10 deletions src/lib/src/search-syntax-highlighter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,33 @@
#include <QVector>


class Profile;
class QTextDocument;

class SearchSyntaxHighlighter : public QSyntaxHighlighter
{
Q_OBJECT

public:
explicit SearchSyntaxHighlighter(bool full, QTextDocument *parent = nullptr);
explicit SearchSyntaxHighlighter(bool full, QTextDocument *parent = nullptr, Profile *profile = nullptr);

protected:
void highlightBlock(const QString &text) override;

protected slots:
void updateFavorites();
void updateKeptForLater();

private:
Profile *m_profile;
struct HighlightingRule
{
QRegularExpression pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;

QTextCharFormat favoritesFormat;
QTextCharFormat keptForLaterFormat;
QTextCharFormat orFormat;
QTextCharFormat excludeFormat;
QTextCharFormat metaFormat;
QTextCharFormat metaOtherFormat;
QTextCharFormat md5Format;
QTextCharFormat urlFormat;
HighlightingRule *m_favoritesRule;
HighlightingRule *m_kflRule;
};

#endif // SEARCH_SYNTAX_HIGHLIGHTER_H

0 comments on commit 176250a

Please sign in to comment.