Database: sqlite default to appdata kworship.db
[kworship.git] / kworship / css / KwCssStyle.cpp
blob16a8f4642f25e28ceff0d4c3a4d7034139248dab
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 KwCssStyle.cpp
22 * @brief Typed cascading style property.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwCssStyle.h"
28 #include <QRegExp>
30 #include "KwCssUnprocessed.h"
31 template <>
32 KwCssUnprocessed KwCssStringify<KwCssUnprocessed>(const KwCssUnprocessed& value)
34 return value;
36 template <>
37 KwCssUnprocessed KwCssUnstringify<KwCssUnprocessed>(const KwCssUnprocessed& value, bool* success)
39 *success = true;
40 return value;
43 // Primitives
44 template <>
45 KwCssUnprocessed KwCssStringify<bool>(const bool& value)
47 return KwCssUnprocessed(value ? "true" : "false");
49 template <>
50 bool KwCssUnstringify<bool>(const KwCssUnprocessed& value, bool* success)
52 if (value == "true")
54 *success = true;
55 return true;
57 else if (value == "false")
59 *success = true;
60 return false;
62 else
64 *success = false;
65 return false;
69 template <>
70 KwCssUnprocessed KwCssStringify<int>(const int& value)
72 return KwCssUnprocessed::number(value);
74 template <>
75 int KwCssUnstringify<int>(const KwCssUnprocessed& value, bool* success)
77 return value.toInt(success);
80 template <>
81 KwCssUnprocessed KwCssStringify<float>(const float& value)
83 return KwCssUnprocessed::number(value);
85 template <>
86 float KwCssUnstringify<float>(const KwCssUnprocessed& value, bool* success)
88 return value.toFloat(success);
91 template <>
92 KwCssUnprocessed KwCssStringify<double>(const double& value)
94 return KwCssUnprocessed::number(value);
96 template <>
97 double KwCssUnstringify<double>(const KwCssUnprocessed& value, bool* success)
99 return value.toDouble(success);
102 #include <KwResourceLink.h>
103 template <>
104 KwCssUnprocessed KwCssStringify<KwResourceLink>(const KwResourceLink& value)
106 switch (value.type())
108 case KwResourceLink::Url:
110 QString url = value.url().url();
111 url.replace('\\', "\\\\")
112 .replace('"', "\\\"");
113 return QString("\"%1\"").arg(url);
116 case KwResourceLink::FileRelative:
118 QString url = value.path();
119 url.replace('\\', "\\\\")
120 .replace('"', "\\\"");
121 return QString("frel \"%1\"").arg(url);
124 case KwResourceLink::ArchiveRoot:
126 QString url = value.path();
127 url.replace('\\', "\\\\")
128 .replace('"', "\\\"");
129 return QString("aroot \"%1\"").arg(url);
132 case KwResourceLink::ArchiveRelative:
134 QString url = value.path();
135 url.replace('\\', "\\\\")
136 .replace('"', "\\\"");
137 return QString("arel \"%1\"").arg(url);
140 case KwResourceLink::Null:
141 default:
143 return "null";
148 template <>
149 KwResourceLink KwCssUnstringify<KwResourceLink>(const KwCssUnprocessed& value, bool* success)
151 if ("null" != value)
153 static QRegExp reFormat("((\\w*)\\s+)?\"((\\.|[^\\\"])*)\"");
154 static const QRegExp reUnescape("\\(.)");
155 static const QString unescapeReplace("\\1");
156 if (reFormat.exactMatch(value))
158 QString keyword = reFormat.cap(1);
159 QString file = reFormat.cap(3);
160 file.replace(reUnescape, unescapeReplace);
161 *success = true;
162 if (keyword.isEmpty())
164 return KwResourceLink(KUrl(file));
166 else if ("frel" == keyword)
168 return KwResourceLink(KwResourceLink::FileRelative, file);
170 else if ("aroot" == keyword)
172 return KwResourceLink(KwResourceLink::ArchiveRoot, file);
174 else if ("arel" == keyword)
176 return KwResourceLink(KwResourceLink::ArchiveRelative, file);
180 *success = false;
181 return KwResourceLink();
184 #include <QBrush>
185 template <>
186 KwCssUnprocessed KwCssStringify<QBrush>(const QBrush& value)
188 return "Brush()";
190 template <>
191 QBrush KwCssUnstringify<QBrush>(const KwCssUnprocessed& value, bool* success)
193 *success = true;
194 return QBrush(Qt::black);
197 #include <QPen>
198 template <>
199 KwCssUnprocessed KwCssStringify<QPen>(const QPen& value)
201 return "Pen()";
203 template <>
204 QPen KwCssUnstringify<QPen>(const KwCssUnprocessed& value, bool* success)
206 *success = true;
207 return QPen();
210 #include <QFont>
211 template <>
212 KwCssUnprocessed KwCssStringify<QFont>(const QFont& value)
214 return "Font()";
216 template <>
217 QFont KwCssUnstringify<QFont>(const KwCssUnprocessed& value, bool* success)
219 *success = true;
220 return QFont();