File indexing completed on 2024-04-21 04:02:03

0001 /*
0002     KBlackBox - A simple game inspired by an emacs module
0003 
0004     SPDX-FileCopyrightText: 2007 Nicolas Roffet <nicolas-kde@roffet.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 
0010 
0011 #ifndef KBBTHEMEMANAGER_H
0012 #define KBBTHEMEMANAGER_H
0013 
0014 
0015 #include <QDomElement>
0016 class QString;
0017 
0018 
0019 #include <QSvgRenderer>
0020 
0021 
0022 #include "kbbscalablegraphicwidget.h"
0023 
0024 
0025 
0026 /**
0027  * @brief Theme manager of the scalable graphic widget
0028  *
0029  * This class manages the graphic theme of the central widget.
0030  * 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.
0031  * @see value;
0032  */
0033 class KBBThemeManager
0034 {
0035     public:
0036         /**
0037          * @brief Constructor
0038          *
0039          * The constructor opens the SVGZ file, creates the SVG renderer and gets the DOM root node.
0040          * @param &svgzFileName Path to the SVGZ file
0041          */
0042         explicit KBBThemeManager(const QString &svgzFileName);
0043         
0044         
0045         /**
0046          * @brief Get the color of the item
0047          *
0048          * @param &itemType Type of the considered item
0049          * @return The color
0050          */
0051         QColor color(const KBBScalableGraphicWidget::itemType itemType);
0052         
0053         /**
0054          * @brief Get the XML "id" value of the item
0055          *
0056          * In the SVG file, each graphic element has an ID that is saved in the attribute "id" of every XML node.
0057          * This methode maps the item type with its XML "id" value.
0058          *
0059          * @param &itemType Type of the considered item
0060          * @return The XML attribute "id" of the item
0061          */
0062         QString elementId(const KBBScalableGraphicWidget::itemType itemType);
0063         
0064         /**
0065          * @brief Get the pen style of the item
0066          *
0067          * @param &itemType Type of the considered item
0068          * @return Qt::DotLine except if the style value "stroke-dasharray" is "none". In that case: Qt::SolidLine
0069          */
0070         Qt::PenStyle style(const KBBScalableGraphicWidget::itemType itemType);
0071         
0072         /**
0073          * @brief Get the shared SVG renderer
0074          * 
0075          * @return The SVG renderer
0076          */
0077         QSvgRenderer* svgRenderer();
0078         
0079         /**
0080          * @brief Get the width of the item
0081          *
0082          * @param &itemType Type of the considered item
0083          * @return The stroke width
0084          */
0085         qreal width(const KBBScalableGraphicWidget::itemType itemType);
0086         
0087         /**
0088          * @brief Get the relative height of the item
0089          *
0090          * Items are stacked horizontally. It's important to know what item is over or unter what other item.
0091          *
0092          * @param &itemType Type of the considered item
0093          * @return item relative height
0094          */
0095         int zValue(const KBBScalableGraphicWidget::itemType itemType);
0096 
0097 
0098     private:
0099         /**
0100          * @brief Get the value of the style attribute in the SVG file for a non SVG item
0101          *
0102          * This methode is useful for laser rays or for the grid of the black box.
0103          * 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>.
0104          *
0105          * Prerequisites: The SVG file must follow a strict structure.
0106          * - 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.
0107          * - 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.
0108          * - The id attribute of the node is important, but the node tag is not. ("g" or "path" or whatever is OK).
0109          *
0110          * @param &itemType Type of the considered item
0111          * @param &styleElement Element of the attribute "style" to consider
0112          * @return Value of the given element in the attribute "style" of the give XML node
0113          * @see KBBGraphicsItemRay
0114          */
0115         QString value(const KBBScalableGraphicWidget::itemType itemType, const QString &styleElement);
0116         
0117         /**
0118          * @brief Root element of the DOM tree of the XML theme file
0119          */
0120         QDomElement m_root;
0121         
0122         /**
0123          * @brief SVG Renderer
0124          *
0125          * The SVG renderer is shared by all SVG graphic items.
0126          */
0127         QSvgRenderer m_svgRenderer;
0128 };
0129 
0130 #endif // KBBTHEMEMANAGER_H