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
29#ifndef KBBTHEMEMANAGER_H
30#define KBBTHEMEMANAGER_H
31
32
33#include <QDomElement>
34class QString;
35
36
37#include <qsvgrenderer.h>
38
39
40#include "kbbscalablegraphicwidget.h"
41
42
43
44/**
45 * @brief Theme manager of the scalable graphic widget
46 *
47 * This class manages the graphic theme of the central widget.
48 * Graphics elements of the game are mainly SVG items. Some dynamic elements (like laser rays) are not SVG items but their theme attributes are nonetheless saved in the SVG file.
49 * @see value;
50 */
51class KBBThemeManager
52{
53 public:
54 /**
55 * @brief Constructor
56 *
57 * The constructor opens the SVGZ file, creates the SVG renderer and gets the DOM root node.
58 * @param &svgzFileName Path to the SVGZ file
59 */
60 KBBThemeManager(const QString &svgzFileName);
61
62
63 /**
64 * @brief Get the color of the item
65 *
66 * @param &itemType Type of the considered item
67 * @return The color
68 */
69 QColor color(const KBBScalableGraphicWidget::itemType itemType);
70
71 /**
72 * @brief Get the XML "id" value of the item
73 *
74 * In the SVG file, each graphic element has an ID that is saved in the attribute "id" of every XML node.
75 * This methode maps the item type with its XML "id" value.
76 *
77 * @param &itemType Type of the considered item
78 * @return The XML attribute "id" of the item
79 */
80 QString elementId(const KBBScalableGraphicWidget::itemType itemType);
81
82 /**
83 * @brief Get the pen style of the item
84 *
85 * @param &itemType Type of the considered item
86 * @return Qt::DotLine except if the style value "stroke-dasharray" is "none". In that case: Qt::SolidLine
87 */
88 Qt::PenStyle style(const KBBScalableGraphicWidget::itemType itemType);
89
90 /**
91 * @brief Get the shared SVG renderer
92 *
93 * @return The SVG renderer
94 */
95 QSvgRenderer* svgRenderer();
96
97 /**
98 * @brief Get the width of the item
99 *
100 * @param &itemType Type of the considered item
101 * @return The stroke width
102 */
103 qreal width(const KBBScalableGraphicWidget::itemType itemType);
104
105 /**
106 * @brief Get the relative height of the item
107 *
108 * Items are stacked horizontally. It's important to know what item is over or unter what other item.
109 *
110 * @param &itemType Type of the considered item
111 * @return item relative height
112 */
113 int zValue(const KBBScalableGraphicWidget::itemType itemType);
114
115
116 private:
117 /**
118 * @brief Get the value of the style attribute in the SVG file for a non SVG item
119 *
120 * This methode is useful for laser rays or for the grid of the black box.
121 * Laser rays (for instance) inherit QGraphicsPathItem and not KBBGraphicsItem (i.e. not QGraphicsSvgItem). But we want to be able to configure their attributes (color, width and style) to suit the graphic theme. All other items are saved as SVG items in a single theme file. To make it easy to manage themes, the laser ray attributes are also saved in the same SVG file. And as SVG is a XML format, <b>this methode of this class parses the XML of the SVG file</b>.
122 *
123 * Prerequisites: The SVG file must follow a strict structure.
124 * - Laser ray elements must be defined in a XML node that is a direct child of the root SVG node. It won't work for instance if there is a "layer" node between the XML node of the laser ray and the root node.
125 * - Just the attribute "style" of the laser ray node is used. The color is the value of "stroke:", the width is the value of "stroke-width:", and the style is the value of "stroke-dasharray:". All other values are ignored.
126 * - The id attribute of the node is important, but the node tag is not. ("g" or "path" or whatever is OK).
127 *
128 * @param &itemType Type of the considered item
129 * @param &styleElement Element of the attribute "style" to consider
130 * @return Value of the given element in the attribute "style" of the give XML node
131 * @see KBBGraphicsItemRay
132 */
133 QString value(const KBBScalableGraphicWidget::itemType itemType, const QString &styleElement);
134
135 /**
136 * @brief Root element of the DOM tree of the XML theme file
137 */
138 QDomElement m_root;
139
140 /**
141 * @brief SVG Renderer
142 *
143 * The SVG renderer is shared by all SVG graphic items.
144 */
145 QSvgRenderer m_svgRenderer;
146};
147
148#endif // KBBTHEMEMANAGER_H
149