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