File indexing completed on 2022-11-23 10:43:26

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 #include "kbbthememanager.h"
0011 
0012 #include <KCompressionDevice>
0013 
0014 #include <QColor>
0015 #include <QDomDocument>
0016 #include <QFile>
0017 #include <QStringList>
0018 
0019 //
0020 // Constructor / Destructor
0021 //
0022 
0023 KBBThemeManager::KBBThemeManager(const QString &svgzFileName)
0024 {
0025     // 1. for SVG items
0026     m_svgRenderer.load(svgzFileName);
0027     
0028     
0029     // 2. for non SVG items
0030     QFile svgzFile(svgzFileName);
0031     KCompressionDevice *f = new KCompressionDevice(&svgzFile, false, KCompressionDevice::GZip);
0032     
0033     if (f) {
0034         QDomDocument doc;
0035         if (doc.setContent(f,true)) {
0036             m_root = doc.documentElement();
0037         }
0038         
0039         delete f;
0040     }
0041 }
0042 
0043 
0044 
0045 //
0046 // Public
0047 //
0048 
0049 QColor KBBThemeManager::color(const KBBScalableGraphicWidget::itemType itemType)
0050 {
0051     return QColor(value(itemType, QStringLiteral("stroke")));
0052 }
0053 
0054 
0055 QString KBBThemeManager::elementId(const KBBScalableGraphicWidget::itemType itemType)
0056 {
0057     QString eId;
0058     
0059     switch (itemType) {
0060         case KBBScalableGraphicWidget::background:
0061             eId = QStringLiteral("background");
0062             break;
0063         case KBBScalableGraphicWidget::blackbox:
0064             eId = QStringLiteral("blackbox");
0065             break;
0066         case KBBScalableGraphicWidget::blackboxGrid:
0067             eId = QStringLiteral("blackbox_grid");
0068             break;
0069         case KBBScalableGraphicWidget::cursor:
0070             eId = QStringLiteral("cursor");
0071             break;
0072         case KBBScalableGraphicWidget::informationBackground:
0073             eId = QStringLiteral("information_background");
0074             break;
0075         case KBBScalableGraphicWidget::interactionInfoDeflection:
0076             eId = QStringLiteral("interaction_info_deflection");
0077             break;
0078         case KBBScalableGraphicWidget::interactionInfoHit:
0079             eId = QStringLiteral("interaction_info_hit");
0080             break;
0081         case KBBScalableGraphicWidget::interactionInfoNothing:
0082             eId = QStringLiteral("interaction_info_nothing");
0083             break;
0084         case KBBScalableGraphicWidget::interactionInfoReflection:
0085             eId = QStringLiteral("interaction_info_reflection");
0086             break;
0087         case KBBScalableGraphicWidget::interactionInfoReflectionSym:
0088             eId = QStringLiteral("interaction_info_reflection_sym");
0089             break;
0090         case KBBScalableGraphicWidget::laser0:
0091             eId = QStringLiteral("laser_0");
0092             break;
0093         case KBBScalableGraphicWidget::laser90:
0094             eId = QStringLiteral("laser_90");
0095             break;
0096         case KBBScalableGraphicWidget::laser180:
0097             eId = QStringLiteral("laser_180");
0098             break;
0099         case KBBScalableGraphicWidget::laser270:
0100             eId = QStringLiteral("laser_270");
0101             break;
0102         case KBBScalableGraphicWidget::markerNothing:
0103             eId = QStringLiteral("marker_nothing");
0104             break;
0105         case KBBScalableGraphicWidget::playerBall:
0106             eId = QStringLiteral("player_ball");
0107             break;
0108         case KBBScalableGraphicWidget::playerRay:
0109             eId = QStringLiteral("player_ray");
0110             break;
0111         case KBBScalableGraphicWidget::resultBackground:
0112             eId = QStringLiteral("result_background");
0113             break;
0114         case KBBScalableGraphicWidget::resultBackgroundHighlight:
0115             eId = QStringLiteral("result_background_highlight");
0116             break;
0117         case KBBScalableGraphicWidget::resultHit:
0118             eId = QStringLiteral("result_hit");
0119             break;
0120         case KBBScalableGraphicWidget::resultReflection:
0121             eId = QStringLiteral("result_reflection");
0122             break;
0123         case KBBScalableGraphicWidget::rightPlayerBall:
0124             eId = QStringLiteral("right_player_ball");
0125             break;
0126         case KBBScalableGraphicWidget::solutionBall:
0127             eId = QStringLiteral("solution_ball");
0128             break;
0129         case KBBScalableGraphicWidget::solutionRay:
0130             eId = QStringLiteral("solution_ray");
0131             break;
0132         case KBBScalableGraphicWidget::tutorialMarker:
0133             eId = QStringLiteral("tutorial_marker");
0134             break;
0135         case KBBScalableGraphicWidget::unsureBall:
0136             eId = QStringLiteral("unsure_ball");
0137             break;
0138         case KBBScalableGraphicWidget::wrongPlayerBall:
0139             eId = QStringLiteral("wrong_player_ball");
0140             break;
0141         default:
0142             eId = QStringLiteral("none");
0143             break;
0144     }
0145     
0146     return eId;
0147 }
0148 
0149 
0150 Qt::PenStyle KBBThemeManager::style(const KBBScalableGraphicWidget::itemType itemType)
0151 {
0152     if (value(itemType, QStringLiteral("stroke-dasharray"))==QLatin1String("none")) {
0153         return Qt::SolidLine;
0154     } else
0155         return Qt::DotLine;
0156 }
0157 
0158 
0159 QSvgRenderer* KBBThemeManager::svgRenderer()
0160 {
0161     return &m_svgRenderer;
0162 }
0163 
0164 
0165 qreal KBBThemeManager::width(const KBBScalableGraphicWidget::itemType itemType)
0166 {
0167     return value(itemType, QStringLiteral("stroke-width")).toFloat();
0168 }
0169 
0170 
0171 int KBBThemeManager::zValue(const KBBScalableGraphicWidget::itemType itemType)
0172 {
0173     return itemType;
0174 }
0175 
0176 
0177 
0178 //
0179 // Private
0180 //
0181 
0182 QString KBBThemeManager::value(const KBBScalableGraphicWidget::itemType itemType, const QString &styleElement)
0183 {
0184     const QString id = elementId(itemType);
0185     QString style;
0186     QString v;
0187     
0188     QDomNode node = m_root.firstChild();
0189     while(!node.isNull()) {
0190         if (node.toElement().attribute(QStringLiteral("id")) == id)
0191             style = node.toElement().attribute(QStringLiteral("style"));
0192         node = node.nextSibling();
0193     }
0194     
0195     QStringList styleList = style.split(QLatin1Char(';'));
0196     for (int i = 0; i < styleList.size(); i++) {
0197         styleList.replace(i, styleList.at(i).trimmed());
0198         if (styleList.at(i).startsWith(styleElement + QLatin1Char(':'))) {
0199             QString s = styleList.at(i);
0200             v = s.right(s.length()-styleElement.length()-1);
0201         }
0202     }
0203     
0204     return v;
0205 }