Warning, file /education/khangman/src/khmthemefactory.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /*************************************************************************** 0002 * Copyright (C) 2010 Adam Rakowski <foo-script@o2.pl> (GSoC 2010) * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU General Public License as published by * 0006 * the Free Software Foundation; either version 2 of the License, or * 0007 * (at your option) any later version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, * 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0012 * GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License * 0015 * along with this program; if not, write to the * 0016 * Free Software Foundation, Inc., * 0017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 0018 ***************************************************************************/ 0019 0020 #include "khmthemefactory.h" 0021 #include <QDebug> 0022 0023 bool KHMThemeFactory::addTheme(const QString &themeFile) 0024 { 0025 QFile file (themeFile); 0026 if (!file.exists()) { 0027 qDebug() << "Themes file doesn't exist"; 0028 return false; 0029 } 0030 0031 if (!file.open(QIODevice::ReadOnly)) { 0032 qDebug()<<"Can't open themes file"; 0033 return false; 0034 } 0035 0036 QDomDocument tree(QStringLiteral("KHangManTheme")); //do we need it? 0037 if (!tree.setContent(&file)) { 0038 file.close(); 0039 qDebug()<<"Can't set content for theme file"; 0040 return false; 0041 } 0042 0043 file.close(); 0044 const QDomElement root=tree.documentElement(); 0045 0046 if (!checkTheme(root, QStringLiteral("1"))) { 0047 qDebug()<<"Incompatible version of theme loaded"; 0048 return false; 0049 } 0050 0051 const QString themeVersion=root.attribute(QStringLiteral("version")); 0052 for (QDomNode themeNode=root.firstChild(); !themeNode.isNull(); themeNode=themeNode.nextSibling()) { 0053 doTheme(themeNode.toElement(), themeVersion); 0054 } 0055 return true; 0056 } 0057 0058 void KHMThemeFactory::walkDirectory(const QDir &dir) //unused! (but works) 0059 { 0060 if (dir.exists()) { 0061 const QStringList allowedExtenstion(QStringLiteral("*.xml")); 0062 const QFileInfoList xmlFilesList=dir.entryInfoList(allowedExtenstion, QDir::Files); 0063 0064 for (const auto &file : xmlFilesList) { 0065 addTheme(file.absoluteFilePath()); 0066 } 0067 } 0068 } 0069 0070 int KHMThemeFactory::getQty() const 0071 { 0072 return themesList.size(); 0073 } 0074 0075 QStringList KHMThemeFactory::getNames() const 0076 { 0077 QStringList list; 0078 for (const auto &theme : themesList) { 0079 list.append(theme.name()); 0080 } 0081 return list; 0082 } 0083 0084 QStringList KHMThemeFactory::themeList() const 0085 { 0086 QStringList list; 0087 for (const auto &theme : themesList) { 0088 list.append(theme.uiName()); 0089 } 0090 return list; 0091 } 0092 0093 const KHMTheme * KHMThemeFactory::getTheme(int id) const 0094 { 0095 if (id >= 0 && id < themesList.size()) { 0096 return &themesList[id]; 0097 } 0098 return nullptr; 0099 } 0100 0101 QRect KHMThemeFactory::makeRect(const QDomElement &element, const QString &propertyName) 0102 { 0103 const QDomElement rect=element.firstChildElement(propertyName); 0104 0105 return QRect( 0106 (int)(rect.attribute(QStringLiteral("xratio")).toDouble()*10000), 0107 (int)(rect.attribute(QStringLiteral("yratio")).toDouble()*10000), //*10000 cause ratios are float 0108 (int)(rect.attribute(QStringLiteral("wratio")).toDouble()*10000), 0109 (int)(rect.attribute(QStringLiteral("hratio")).toDouble()*10000) //QPoint,QRect constructors expect ints 0110 ); 0111 } 0112 0113 QColor KHMThemeFactory::getColor(const QDomElement &element, const QString &propertyName) 0114 { 0115 const QDomElement color=element.firstChildElement(propertyName); 0116 0117 return QColor ( color.attribute(QStringLiteral("r")).toInt(), color.attribute(QStringLiteral("g")).toInt(), color.attribute(QStringLiteral("b")).toInt()); 0118 } 0119 0120 bool KHMThemeFactory::checkTheme(const QDomElement &root, const QString &themeVersion) 0121 { 0122 const QString rootName=root.tagName(); 0123 0124 return (rootName==QLatin1String("KHangManThemes")) && (themeVersion==root.attribute(QStringLiteral("version"))); 0125 } 0126 0127 void KHMThemeFactory::doTheme(const QDomElement &theme, const QString &version) //fetch all the properties from .xml and stick it together 0128 //"theme" means <theme></theme> section 0129 { 0130 //Names of elements are camelCase, but 0131 //attributes are always lowercase 0132 0133 const QString name=theme.attribute(QStringLiteral("name")); 0134 const QString uiName=theme.attribute(QStringLiteral("uiname")); 0135 const QString svgFileName=theme.attribute(QStringLiteral("svgfilename")); 0136 const QString author=theme.attribute(QStringLiteral("author")); 0137 0138 const QDomElement colors=theme.firstChildElement(QStringLiteral("colors")); 0139 const QColor letterColor=getColor(colors, QStringLiteral("letterColor")); 0140 const QColor guessButtonTextColor=getColor(colors, QStringLiteral("guessButtonTextColor")); 0141 const QColor guessButtonColor=getColor(colors, QStringLiteral("guessButtonColor")); 0142 const QColor guessButtonHoverColor=getColor(colors, QStringLiteral("guessButtonHoverColor")); 0143 const QColor letterInputTextColor=getColor(colors, QStringLiteral("letterInputTextColor")); 0144 0145 const QDomElement coords=theme.firstChildElement(QStringLiteral("coords")); 0146 const QRect wordRect=makeRect(coords, QStringLiteral("wordRect")); 0147 const QRect hintRect=makeRect(coords, QStringLiteral("hintRect")); 0148 const QRect kRect=makeRect(coords, QStringLiteral("kRect")); 0149 0150 const QDomElement wordPos = coords.firstChildElement(QStringLiteral("goodWordPos")); 0151 const QPoint goodWordPos = QPoint( wordPos.attribute(QStringLiteral("wratio")).toDouble()*10000, wordPos.attribute(QStringLiteral("hratio")).toDouble()*10000 ); 0152 0153 const KHMTheme newTheme (name, uiName, svgFileName, author, version, wordRect, hintRect, kRect, letterColor, guessButtonTextColor, guessButtonColor, guessButtonHoverColor, letterInputTextColor, goodWordPos); 0154 themesList.append(newTheme); 0155 }