Krita Source Code Documentation
Loading...
Searching...
No Matches
SvgCssHelper Class Reference

#include <SvgCssHelper.h>

Classes

class  Private
 

Public Member Functions

QStringList matchStyles (const QDomElement &element) const
 
void parseStylesheet (const QDomElement &)
 Parses css style sheet in given xml element.
 
 SvgCssHelper ()
 
 ~SvgCssHelper ()
 

Private Attributes

Private *const d
 

Detailed Description

Definition at line 14 of file SvgCssHelper.h.

Constructor & Destructor Documentation

◆ SvgCssHelper()

SvgCssHelper::SvgCssHelper ( )

Definition at line 599 of file SvgCssHelper.cpp.

600: d(new Private())
601{
602}
Private *const d

◆ ~SvgCssHelper()

SvgCssHelper::~SvgCssHelper ( )

Definition at line 604 of file SvgCssHelper.cpp.

605{
606 delete d;
607}

References d.

Member Function Documentation

◆ matchStyles()

QStringList SvgCssHelper::matchStyles ( const QDomElement & element) const

Matches css styles to given xml element and returns them

Parameters
elementthe element to match styles for
Returns
list of matching css styles sorted by priority

Definition at line 654 of file SvgCssHelper.cpp.

655{
656 QMap<int, QString> prioritizedRules;
657 // match rules to element
658 Q_FOREACH (const CssRule &rule, d->cssRules) {
659 Q_FOREACH (CssSelectorBase *s, rule.first) {
660 bool matched = s->match(element);
661 if (matched)
662 prioritizedRules[s->priority()] = rule.second;
663 }
664 }
665
666 // css style attribute has the priority of 100
667 QString styleAttribute = element.attribute("style").simplified();
668 if (!styleAttribute.isEmpty())
669 prioritizedRules[100] = styleAttribute;
670
671 QStringList cssStyles;
672 // add matching styles in correct order to style list
673 QMapIterator<int, QString> it(prioritizedRules);
674 while (it.hasNext()) {
675 it.next();
676 cssStyles.append(it.value());
677 }
678
679 return cssStyles;
680}
QPair< SelectorGroup, QString > CssRule
A css rule consisting of group of selectors corresponding to a style.
Selector base class, merely an interface.
virtual bool match(const QDomElement &)=0
Matches the given element.
virtual int priority()
QList< CssRule > cssRules

References SvgCssHelper::Private::cssRules, d, CssSelectorBase::match(), and CssSelectorBase::priority().

◆ parseStylesheet()

void SvgCssHelper::parseStylesheet ( const QDomElement & e)

Parses css style sheet in given xml element.

Definition at line 609 of file SvgCssHelper.cpp.

610{
611 QString data;
612
613 if (e.hasChildNodes()) {
614 QDomNode c = e.firstChild();
615 if (c.isCDATASection()) {
616 QDomCDATASection cdata = c.toCDATASection();
617 data = cdata.data().simplified();
618 } else if (c.isText()) {
619 QDomText text = c.toText();
620 data = text.data().simplified();
621 }
622 }
623 if (data.isEmpty())
624 return;
625
626 // Remove comments
627 // NOTE: that must not be greedy as per definition of css-comments,
628 // the first closing '*/' sequence closes the entire comment
629 // block
630 QRegularExpression commentExp("\\/\\*.*?\\*\\/");
631 data.remove(commentExp);
632
633 QStringList defs = data.split('}', Qt::SkipEmptyParts);
634 for (int i = 0; i < defs.count(); ++i) {
635 QStringList def = defs[i].split('{');
636 if( def.count() != 2 )
637 continue;
638 QString pattern = def[0].simplified();
639 if (pattern.isEmpty())
640 break;
641 QString style = def[1].simplified();
642 if (style.isEmpty())
643 break;
644 QStringList selectors = pattern.split(',', Qt::SkipEmptyParts);
645 for (int i = 0; i < selectors.count(); ++i ) {
646 QString selector = selectors[i].simplified();
647 d->cssStyles[selector] = style;
648 }
649 SelectorGroup group = d->parsePattern(pattern);
650 d->cssRules.append(CssRule(group, style));
651 }
652}
QMap< QString, QString > cssStyles
SelectorGroup parsePattern(const QString &pattern)

References SvgCssHelper::Private::cssRules, SvgCssHelper::Private::cssStyles, d, and SvgCssHelper::Private::parsePattern().

Member Data Documentation

◆ d

Private* const SvgCssHelper::d
private

Definition at line 32 of file SvgCssHelper.h.


The documentation for this class was generated from the following files: