[英]How can I simply add a css file to change the background color for my shiny app
[英]How can I simply parse a CSS like (!) file in my Qt application?
我有一個* .css(層疊樣式表)格式的文檔,但它有自己的關鍵字。 實際上它是一個個性化的CSS(我稱之為* .pss),具有自己的標簽和屬性。 我在這里摘錄:
/* CSS like style sheet file *.pss */
@include "otherStyleSheet.pss";
/* comment */
[propertyID="1230000"] {
fillColor : #f3f1ed;
minSize : 5;
lineWidth : 3;
}
/* sphere */
[propertyID="124???|123000"] {
lineType : dotted;
}
/* square */
[propertyID="125???"] {
lineType : thinline;
}
/* ring */
[propertyID="133???"] {
lineType : thickline;
[hasInnerRing=true] {
innerLineType : thinline;
}
}
我想很容易地解析它,Qt已經有了一些可以使用的東西嗎? 什么是最簡單的方法?
由於* .css有自己的關鍵字,我不會在CSS解析器中使用。
解析* .pss后我的進一步意圖是將其屬性存儲在Model結構中。
Qt內部沒有任何公開內容。 您當然可以自由使用Qt的私有CSS解析器 - 您可以復制它並進行修改以滿足您的需求。
見qtbase/src/gui/text/qcssparser_p.h
,在qtbase/src/gui/text
。
好消息是,對於上面顯示的示例,修改將非常小。 Qt的CSS解析器已經支持@import
,所以我們只有一些語法是嵌套的選擇器語法 。 如果沒有該語法,您可以按QCss::Parser
使用QCss::Parser
。 解析器是以靈活的方式編寫的,您無需擔心正式的CSS關鍵字:它仍然允許您訪問所有聲明,無論它們是否從正式的CSS觀點來看都是有意義的。
迭代解析樹就像它得到的一樣簡單:
int main() {
QCss::Parser parser(pss);
QCss::StyleSheet styleSheet;
if (!parser.parse(&styleSheet))
return 1;
for (auto rule : styleSheet.styleRules) {
qDebug() << "** Rule **";
for (auto sel : rule.selectors) {
for (auto bSel : sel.basicSelectors)
qDebug() << bSel;
}
for (auto decl : rule.declarations)
qDebug() << decl;
}
}
輸出是我們所期望的:
** Rule **
BasicSelector "propertyID"="1230000"
Declaration "fillColor" = '#f3f1ed' % QColor(ARGB 1, 0.952941, 0.945098, 0.929412)
Declaration "minSize" = '5' % 5
Declaration "lineWidth" = '3'
** Rule **
BasicSelector "propertyID"="124???|123000"
Declaration "lineType" = 'dotted'
** Rule **
BasicSelector "propertyID"="125???"
Declaration "lineType" = 'thinline'
** Rule **
BasicSelector "propertyID"="133???"
Declaration "lineType" = 'thickline'
我們必須自己為QCss
類實現調試流操作符:
QDebug operator<<(QDebug dbg, const QCss::AttributeSelector & sel) {
QDebugStateSaver saver(dbg);
dbg.noquote().nospace() << "\"" << sel.name << "\"";
switch (sel.valueMatchCriterium) {
case QCss::AttributeSelector::MatchEqual:
dbg << "="; break;
case QCss::AttributeSelector::MatchContains:
dbg << "~="; break;
case QCss::AttributeSelector::MatchBeginsWith:
dbg << "^="; break;
case QCss::AttributeSelector::NoMatch:
break;
}
if (sel.valueMatchCriterium != QCss::AttributeSelector::NoMatch && !sel.value.isEmpty())
dbg << "\"" << sel.value << "\"";
return dbg;
}
QDebug operator<<(QDebug dbg, const QCss::BasicSelector & sel) {
QDebugStateSaver saver(dbg);
dbg.noquote().nospace() << "BasicSelector";
if (!sel.elementName.isEmpty())
dbg << " #" << sel.elementName;
for (auto & id : sel.ids)
dbg << " id:" << id;
for (auto & aSel : sel.attributeSelectors)
dbg << " " << aSel;
return dbg;
}
遍歷聲明時, QCss::parser
已經為我們解釋了一些標准值,例如顏色,整數等。
QDebug operator<<(QDebug dbg, const QCss::Declaration & decl) {
QDebugStateSaver saver(dbg);
dbg.noquote().nospace() << "Declaration";
dbg << " \"" << decl.d->property << "\" = ";
bool first = true;
for (auto value : decl.d->values) {
if (!first) dbg << ", ";
dbg << "\'" << value.toString() << "\'";
first = false;
}
if (decl.d->property == "fillColor")
dbg << " % " << decl.colorValue();
else if (decl.d->property == "minSize") {
int i;
if (decl.intValue(&i)) dbg << " % " << i;
}
return dbg;
}
最后,樣板和要解析的樣式表:
// https://github.com/KubaO/stackoverflown/tree/master/questions/css-like-parser-31583622
#include <QtGui>
#include <private/qcssparser_p.h>
const char pss[] =
"/* @include \"otherStyleSheet.pss\"; */ \
[propertyID=\"1230000\"] { \
fillColor : #f3f1ed; \
minSize : 5; \
lineWidth : 3; \
} \
\
/* sphere */ \
[propertyID=\"124???|123000\"] { \
lineType : dotted; \
} \
\
/* square */ \
[propertyID=\"125???\"] { \
lineType : thinline; \
} \
\
/* ring */ \
[propertyID=\"133???\"] { \
lineType : thickline; \
/*[hasInnerRing=true] { \
innerLineType : thinline; \
}*/ \
}";
可以通過修改解析器源來實現對嵌套選擇器/規則的支持。 使Parser::parseRuleset
遞歸所需的更改非常小。 我會留下這個作為讀者的練習:)
總而言之,我認為重用現有的解析器比滾動自己的解析器容易得多,尤其是當您的用戶不可避免地希望您支持越來越多的CSS規范時。
好吧,我猜你不想做編寫Object解析器的事情,你只需要重新發明JSON或YAML等。 因此,最好的辦法是使格式符合已知的配置或對象表示法語言,然后使用某些庫解析它所使用的語言。 通過非常小的修改,您在上面描述的格式可能變成HOCON,這是一個非常好的JSON超集,並且語法更接近您使用的語法:
https://github.com/typesafehub/config/blob/master/HOCON.md
然后你可以使用HOCON解析庫解析它,瞧,你可以擁有內存中的對象,你可以按照你喜歡的方式建模或存儲。 我相信Qt是基於C ++的嗎? 有一個C的hocon庫,我不知道C ++,我猜你需要編寫一個Qt插件來包裝來自其他語言的HOCON解析。
另一種選擇是使用像這樣的CSS->對象解析器: https : //github.com/reworkcss/css
您可能需要根據需要進行分叉和修改。 無論哪種方式,我猜想要集成到Qt應用程序中,您將需要一個插件來處理命令行進程或其他代碼模塊的調用。
我知道兩種可能性:
由於這個事實,你的個性化* .pss並不像CSS那樣復雜(簡單的包圍等),我建議2。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.