File indexing completed on 2024-04-28 07:29:14
0001 // SPDX-FileCopyrightText: 2010 Adam Rakowski <foo-script@o2.pl> 0002 // SPDX-License-Identifier: GPL-2.0-or-later 0003 0004 #include "khmthemefactory.h" 0005 #include <QDebug> 0006 0007 bool KHMThemeFactory::addTheme(const QString &themeFile) 0008 { 0009 QFile file (themeFile); 0010 if (!file.exists()) { 0011 qDebug() << "Themes file doesn't exist"; 0012 return false; 0013 } 0014 0015 if (!file.open(QIODevice::ReadOnly)) { 0016 qDebug()<<"Can't open themes file"; 0017 return false; 0018 } 0019 0020 QDomDocument tree(QStringLiteral("KHangManTheme")); //do we need it? 0021 if (!tree.setContent(&file)) { 0022 file.close(); 0023 qDebug()<<"Can't set content for theme file"; 0024 return false; 0025 } 0026 0027 file.close(); 0028 const QDomElement root=tree.documentElement(); 0029 0030 if (!checkTheme(root, QStringLiteral("1"))) { 0031 qDebug()<<"Incompatible version of theme loaded"; 0032 return false; 0033 } 0034 0035 const QString themeVersion=root.attribute(QStringLiteral("version")); 0036 for (QDomNode themeNode=root.firstChild(); !themeNode.isNull(); themeNode=themeNode.nextSibling()) { 0037 doTheme(themeNode.toElement(), themeVersion); 0038 } 0039 return true; 0040 } 0041 0042 void KHMThemeFactory::walkDirectory(const QDir &dir) //unused! (but works) 0043 { 0044 if (dir.exists()) { 0045 const QStringList allowedExtenstion(QStringLiteral("*.xml")); 0046 const QFileInfoList xmlFilesList=dir.entryInfoList(allowedExtenstion, QDir::Files); 0047 0048 for (const auto &file : xmlFilesList) { 0049 addTheme(file.absoluteFilePath()); 0050 } 0051 } 0052 } 0053 0054 int KHMThemeFactory::getQty() const 0055 { 0056 return themesList.size(); 0057 } 0058 0059 QStringList KHMThemeFactory::getNames() const 0060 { 0061 QStringList list; 0062 for (const auto &theme : themesList) { 0063 list.append(theme.name()); 0064 } 0065 return list; 0066 } 0067 0068 QStringList KHMThemeFactory::themeList() const 0069 { 0070 QStringList list; 0071 for (const auto &theme : themesList) { 0072 list.append(theme.uiName()); 0073 } 0074 return list; 0075 } 0076 0077 const KHMTheme * KHMThemeFactory::getTheme(int id) const 0078 { 0079 if (id >= 0 && id < themesList.size()) { 0080 return &themesList[id]; 0081 } 0082 return nullptr; 0083 } 0084 0085 QRect KHMThemeFactory::makeRect(const QDomElement &element, const QString &propertyName) 0086 { 0087 const QDomElement rect=element.firstChildElement(propertyName); 0088 0089 return QRect( 0090 (int)(rect.attribute(QStringLiteral("xratio")).toDouble()*10000), 0091 (int)(rect.attribute(QStringLiteral("yratio")).toDouble()*10000), //*10000 cause ratios are float 0092 (int)(rect.attribute(QStringLiteral("wratio")).toDouble()*10000), 0093 (int)(rect.attribute(QStringLiteral("hratio")).toDouble()*10000) //QPoint,QRect constructors expect ints 0094 ); 0095 } 0096 0097 QColor KHMThemeFactory::getColor(const QDomElement &element, const QString &propertyName) 0098 { 0099 const QDomElement color=element.firstChildElement(propertyName); 0100 0101 return QColor ( color.attribute(QStringLiteral("r")).toInt(), color.attribute(QStringLiteral("g")).toInt(), color.attribute(QStringLiteral("b")).toInt()); 0102 } 0103 0104 bool KHMThemeFactory::checkTheme(const QDomElement &root, const QString &themeVersion) 0105 { 0106 const QString rootName=root.tagName(); 0107 0108 return (rootName==QLatin1String("KHangManThemes")) && (themeVersion==root.attribute(QStringLiteral("version"))); 0109 } 0110 0111 void KHMThemeFactory::doTheme(const QDomElement &theme, const QString &version) //fetch all the properties from .xml and stick it together 0112 //"theme" means <theme></theme> section 0113 { 0114 //Names of elements are camelCase, but 0115 //attributes are always lowercase 0116 0117 const QString name=theme.attribute(QStringLiteral("name")); 0118 const QString uiName=theme.attribute(QStringLiteral("uiname")); 0119 const QString svgFileName=theme.attribute(QStringLiteral("svgfilename")); 0120 const QString author=theme.attribute(QStringLiteral("author")); 0121 0122 const QDomElement colors=theme.firstChildElement(QStringLiteral("colors")); 0123 const QColor letterColor=getColor(colors, QStringLiteral("letterColor")); 0124 const QColor guessButtonTextColor=getColor(colors, QStringLiteral("guessButtonTextColor")); 0125 const QColor guessButtonColor=getColor(colors, QStringLiteral("guessButtonColor")); 0126 const QColor guessButtonHoverColor=getColor(colors, QStringLiteral("guessButtonHoverColor")); 0127 const QColor letterInputTextColor=getColor(colors, QStringLiteral("letterInputTextColor")); 0128 0129 const QDomElement coords=theme.firstChildElement(QStringLiteral("coords")); 0130 const QRect wordRect=makeRect(coords, QStringLiteral("wordRect")); 0131 const QRect hintRect=makeRect(coords, QStringLiteral("hintRect")); 0132 const QRect kRect=makeRect(coords, QStringLiteral("kRect")); 0133 0134 const QDomElement wordPos = coords.firstChildElement(QStringLiteral("goodWordPos")); 0135 const QPoint goodWordPos = QPoint( wordPos.attribute(QStringLiteral("wratio")).toDouble()*10000, wordPos.attribute(QStringLiteral("hratio")).toDouble()*10000 ); 0136 0137 const KHMTheme newTheme (name, uiName, svgFileName, author, version, wordRect, hintRect, kRect, letterColor, guessButtonTextColor, guessButtonColor, guessButtonHoverColor, letterInputTextColor, goodWordPos); 0138 themesList.append(newTheme); 0139 }