File indexing completed on 2025-02-23 04:35:29
0001 /* 0002 SPDX-FileCopyrightText: 2021-2022 Mladen Milinkovic <max@smoothware.net> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef RICHCSS_H 0008 #define RICHCSS_H 0009 0010 #include <QByteArray> 0011 #include <QMap> 0012 #include <QObject> 0013 #include <QSet> 0014 #include <QString> 0015 #include <QStringList> 0016 #include <QVector> 0017 0018 #include <list> 0019 #include <memory> 0020 0021 namespace SubtitleComposer { 0022 0023 class RichCSS : public QObject 0024 { 0025 Q_OBJECT 0026 0027 public: 0028 RichCSS(QObject *parent=nullptr); 0029 RichCSS(RichCSS &other); 0030 0031 RichCSS & operator=(const RichCSS &rhs); 0032 0033 typedef QString Selector; 0034 struct Rule { QByteArray name; QString value; }; 0035 class RuleList : public QVector<Rule> { 0036 public: 0037 QStringList toStringList(); 0038 inline QString toString() { return toStringList().join(QString()); } 0039 }; 0040 struct Block { Selector selector; RuleList rules; }; 0041 typedef QVector<Block> Stylesheet; 0042 0043 inline void parse(const QString &css) { parse(css.data()); } 0044 void parse(const QChar *css); 0045 0046 inline const QString & unformattedCSS() const { return m_unformatted; } 0047 0048 void clear(); 0049 0050 QMap<QByteArray, QString> match(QSet<QString> selectors) const; 0051 0052 /** 0053 * @return list of all defined classes 0054 */ 0055 QSet<QString> classes() const; 0056 0057 signals: 0058 void changed(); 0059 0060 private: 0061 static Selector parseCssSelector(const QChar **stylesheet); 0062 static RuleList parseCssRules(const QChar **stylesheet); 0063 static QString parseCssKey(const QChar **stylesheet); 0064 static QString parseCssValue(const QChar **stylesheet); 0065 0066 void mergeCssRules(RuleList &base, const RuleList &override) const; 0067 0068 private: 0069 QString m_unformatted; 0070 Stylesheet m_stylesheet; 0071 }; 0072 } 0073 0074 #endif // RICHCSS_H