1 | // |
2 | // KBlackBox |
3 | // |
4 | // A simple game inspired by an emacs module |
5 | // |
6 | /*************************************************************************** |
7 | * Copyright (c) 2007, Nicolas Roffet * |
8 | * nicolas-kde@roffet.com * |
9 | * * |
10 | * * |
11 | * This program is free software; you can redistribute it and/or modify * |
12 | * it under the terms of the GNU General Public License as published by * |
13 | * the Free Software Foundation; either version 2 of the License, or * |
14 | * (at your option) any later version. * |
15 | * * |
16 | * This program is distributed in the hope that it will be useful, * |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
19 | * GNU General Public License for more details. * |
20 | * * |
21 | * You should have received a copy of the GNU General Public License * |
22 | * along with this program; if not, write to the * |
23 | * Free Software Foundation, Inc., * |
24 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA * |
25 | ***************************************************************************/ |
26 | |
27 | |
28 | #include "kbbthememanager.h" |
29 | |
30 | #include <QColor> |
31 | #include <QDomDocument> |
32 | #include <QFile> |
33 | |
34 | #include <QStringList> |
35 | |
36 | |
37 | #include <kfilterdev.h> |
38 | #include <qsvgrenderer.h> |
39 | |
40 | |
41 | #include "kbbscalablegraphicwidget.h" |
42 | |
43 | |
44 | |
45 | // |
46 | // Constructor / Destructor |
47 | // |
48 | |
49 | KBBThemeManager::KBBThemeManager(const QString &svgzFileName) |
50 | { |
51 | // 1. for SVG items |
52 | m_svgRenderer.load(svgzFileName); |
53 | |
54 | |
55 | // 2. for non SVG items |
56 | QFile svgzFile(svgzFileName); |
57 | QIODevice *f = KFilterDev::device(&svgzFile, QString::fromLatin1("application/x-gzip" ), false); |
58 | |
59 | if (f) { |
60 | QDomDocument doc; |
61 | if (doc.setContent(f,true)) { |
62 | m_root = doc.documentElement(); |
63 | } |
64 | |
65 | delete f; |
66 | } |
67 | } |
68 | |
69 | |
70 | |
71 | // |
72 | // Public |
73 | // |
74 | |
75 | QColor KBBThemeManager::color(const KBBScalableGraphicWidget::itemType itemType) |
76 | { |
77 | return QColor(value(itemType, "stroke" )); |
78 | } |
79 | |
80 | |
81 | QString KBBThemeManager::elementId(const KBBScalableGraphicWidget::itemType itemType) |
82 | { |
83 | QString eId; |
84 | |
85 | switch (itemType) { |
86 | case KBBScalableGraphicWidget::background: |
87 | eId = "background" ; |
88 | break; |
89 | case KBBScalableGraphicWidget::blackbox: |
90 | eId = "blackbox" ; |
91 | break; |
92 | case KBBScalableGraphicWidget::blackboxGrid: |
93 | eId = "blackbox_grid" ; |
94 | break; |
95 | case KBBScalableGraphicWidget::cursor: |
96 | eId = "cursor" ; |
97 | break; |
98 | case KBBScalableGraphicWidget::informationBackground: |
99 | eId = "information_background" ; |
100 | break; |
101 | case KBBScalableGraphicWidget::interactionInfoDeflection: |
102 | eId = "interaction_info_deflection" ; |
103 | break; |
104 | case KBBScalableGraphicWidget::interactionInfoHit: |
105 | eId = "interaction_info_hit" ; |
106 | break; |
107 | case KBBScalableGraphicWidget::interactionInfoNothing: |
108 | eId = "interaction_info_nothing" ; |
109 | break; |
110 | case KBBScalableGraphicWidget::interactionInfoReflection: |
111 | eId = "interaction_info_reflection" ; |
112 | break; |
113 | case KBBScalableGraphicWidget::interactionInfoReflectionSym: |
114 | eId = "interaction_info_reflection_sym" ; |
115 | break; |
116 | case KBBScalableGraphicWidget::laser0: |
117 | eId = "laser_0" ; |
118 | break; |
119 | case KBBScalableGraphicWidget::laser90: |
120 | eId = "laser_90" ; |
121 | break; |
122 | case KBBScalableGraphicWidget::laser180: |
123 | eId = "laser_180" ; |
124 | break; |
125 | case KBBScalableGraphicWidget::laser270: |
126 | eId = "laser_270" ; |
127 | break; |
128 | case KBBScalableGraphicWidget::markerNothing: |
129 | eId = "marker_nothing" ; |
130 | break; |
131 | case KBBScalableGraphicWidget::playerBall: |
132 | eId = "player_ball" ; |
133 | break; |
134 | case KBBScalableGraphicWidget::playerRay: |
135 | eId = "player_ray" ; |
136 | break; |
137 | case KBBScalableGraphicWidget::resultBackground: |
138 | eId = "result_background" ; |
139 | break; |
140 | case KBBScalableGraphicWidget::resultBackgroundHighlight: |
141 | eId = "result_background_highlight" ; |
142 | break; |
143 | case KBBScalableGraphicWidget::resultHit: |
144 | eId = "result_hit" ; |
145 | break; |
146 | case KBBScalableGraphicWidget::resultReflection: |
147 | eId = "result_reflection" ; |
148 | break; |
149 | case KBBScalableGraphicWidget::rightPlayerBall: |
150 | eId = "right_player_ball" ; |
151 | break; |
152 | case KBBScalableGraphicWidget::solutionBall: |
153 | eId = "solution_ball" ; |
154 | break; |
155 | case KBBScalableGraphicWidget::solutionRay: |
156 | eId = "solution_ray" ; |
157 | break; |
158 | case KBBScalableGraphicWidget::tutorialMarker: |
159 | eId = "tutorial_marker" ; |
160 | break; |
161 | case KBBScalableGraphicWidget::unsureBall: |
162 | eId = "unsure_ball" ; |
163 | break; |
164 | case KBBScalableGraphicWidget::wrongPlayerBall: |
165 | eId = "wrong_player_ball" ; |
166 | break; |
167 | default: |
168 | eId = "none" ; |
169 | break; |
170 | } |
171 | |
172 | return eId; |
173 | } |
174 | |
175 | |
176 | Qt::PenStyle KBBThemeManager::style(const KBBScalableGraphicWidget::itemType itemType) |
177 | { |
178 | if (value(itemType, "stroke-dasharray" )=="none" ) { |
179 | return Qt::SolidLine; |
180 | } else |
181 | return Qt::DotLine; |
182 | } |
183 | |
184 | |
185 | QSvgRenderer* KBBThemeManager::svgRenderer() |
186 | { |
187 | return &m_svgRenderer; |
188 | } |
189 | |
190 | |
191 | qreal KBBThemeManager::width(const KBBScalableGraphicWidget::itemType itemType) |
192 | { |
193 | return value(itemType, "stroke-width" ).toFloat(); |
194 | } |
195 | |
196 | |
197 | int KBBThemeManager::zValue(const KBBScalableGraphicWidget::itemType itemType) |
198 | { |
199 | return itemType; |
200 | } |
201 | |
202 | |
203 | |
204 | // |
205 | // Private |
206 | // |
207 | |
208 | QString KBBThemeManager::value(const KBBScalableGraphicWidget::itemType itemType, const QString &styleElement) |
209 | { |
210 | const QString id = elementId(itemType); |
211 | QString style("" ); |
212 | QString v("" ); |
213 | |
214 | QDomNode node = m_root.firstChild(); |
215 | while(!node.isNull()) { |
216 | if (node.toElement().attribute("id" ) == id) |
217 | style = node.toElement().attribute("style" ); |
218 | node = node.nextSibling(); |
219 | } |
220 | |
221 | QStringList styleList = style.split(';'); |
222 | for (int i = 0; i < styleList.size(); i++) { |
223 | styleList.replace(i, styleList.at(i).trimmed()); |
224 | if (styleList.at(i).startsWith(styleElement + ':')) { |
225 | QString s = styleList.at(i); |
226 | v = s.right(s.length()-styleElement.length()-1); |
227 | } |
228 | } |
229 | |
230 | return v; |
231 | } |
232 | |