1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
21 * @file KwCssStyles.cpp
22 * @brief Set of cascading style properties.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwCssStyles.h"
27 #include "KwCssStyleStates.h"
28 #include "KwCssAbstractStyle.h"
29 #include "KwCssAbstractStyleState.h"
30 #include "KwCssSchema.h"
35 * Constructors + destructors
38 /// Default constructor.
39 KwCssStyles::KwCssStyles()
45 KwCssStyles::KwCssStyles(const KwCssStyles
& other
)
48 StyleDictionary::const_iterator it
;
49 for (it
= other
.m_styles
.begin(); it
!= other
.m_styles
.end(); ++it
)
51 m_styles
[it
.key()] = (*it
)->duplicate();
56 KwCssStyles::~KwCssStyles()
58 StyleDictionary::iterator it
;
59 for (it
= m_styles
.begin(); it
!= m_styles
.end(); ++it
)
70 void KwCssStyles::setRawStyle(QString name
, KwCssAbstractStyle
* style
)
72 // Delete previous value
73 StyleDictionary::iterator it
= m_styles
.find(name
);
74 if (it
!= m_styles
.end())
79 m_styles
[name
] = style
;
82 /// Return whether the styles container is empty.
83 bool KwCssStyles::isEmpty() const
85 return m_styles
.isEmpty();
88 /// Convert to CSS-like format.
89 QString
KwCssStyles::toString() const
91 static const QString tmplt
= "%1 : %2;\n";
93 StyleDictionary::const_iterator it
;
94 for (it
= m_styles
.constBegin(); it
!= m_styles
.constEnd(); ++it
)
96 result
+= tmplt
.arg(it
.key()).arg((*it
)->toString());
101 /// Import from CSS-like format into the sheet.
102 int KwCssStyles::import(const KwCssSchema
* schema
, const QString
& sheet
, int start
)
105 static QRegExp
reStyle("^([\\w.]+)\\s*:\\s*(\\S[^;]*);\\s*");
107 while (-1 != (start
= reStyle
.indexIn(sheet
, last
, QRegExp::CaretAtOffset
)))
109 last
= start
+ reStyle
.matchedLength();
110 QString name
= reStyle
.cap(1);
111 KwCssUnprocessed value
= reStyle
.cap(2);
112 setRawStyle(name
, schema
->construct(name
, value
));
122 KwCssStyleStates
& operator << (KwCssStyleStates
& states
, const KwCssStyles
& styles
)
124 // go through styles, adjusting the states
125 KwCssStyles::StyleDictionary::const_iterator it
;
126 for (it
= styles
.m_styles
.begin(); it
!= styles
.m_styles
.end(); ++it
)
128 KwCssAbstractStyleState
*& state
= states
[it
.key()];
131 state
= (*it
)->getNewState();