New css schema object for constructing property values from strings
[kworship.git] / kworship / css / KwCssStyles.cpp
blobd13decad440bfb44d6aedfe46043748998fc3ecb
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20 /**
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"
32 #include <QRegExp>
35 * Constructors + destructors
38 /// Default constructor.
39 KwCssStyles::KwCssStyles()
40 : m_styles()
44 /// Copy constructor.
45 KwCssStyles::KwCssStyles(const KwCssStyles& other)
46 : m_styles()
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();
55 /// Destructor.
56 KwCssStyles::~KwCssStyles()
58 StyleDictionary::iterator it;
59 for (it = m_styles.begin(); it != m_styles.end(); ++it)
61 delete *it;
66 * Main interface
69 /// Set a style.
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())
76 delete *it;
78 // Set new value
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";
92 QString result;
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());
98 return result;
101 /// Import from CSS-like format into the sheet.
102 int KwCssStyles::import(const KwCssSchema* schema, const QString& sheet, int start)
104 // Read the styles
105 static QRegExp reStyle("^([\\w.]+)\\s*:\\s*(\\S[^;]*);\\s*");
106 int last = start;
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));
114 return last;
118 * Operators
121 /// Apply styles.
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()];
129 if (0 == state)
131 state = (*it)->getNewState();
133 (*state) << (*it);
135 return states;