2 * This file is part of jSpeed.
4 * jSpeed is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * jSpeed is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with jSpeed. If not, see <http://www.gnu.org/licenses/>.
19 #include <QtCore/QString>
20 #include <QtCore/QByteArray>
21 #include <QtCore/QDebug>
22 #include <QtCore/QMap>
23 #include <QtGui/QGraphicsScene>
24 #include <QtGui/QFont>
25 #include <QtGui/QFontDatabase>
26 #include "graphicselement.h"
28 #include "textelement.h"
29 #include "imageelement.h"
30 #include "rectangle.h"
34 GraphicsElement::GraphicsElement(Reader* reader): reader_(reader), error_("")
38 GraphicsElement* GraphicsElement::getElement(QString const& name, Reader* reader)
42 return new ImageElement(reader);
47 return new TextElement(reader);
50 if(name == "rectangle")
52 return new Rectangle(reader);
57 bool animate = Settings::instance().value("animate_pointer", true).toBool();
58 return new Pointer(reader, animate);
61 qDebug() << "Element not found: " << name;
65 QString const& GraphicsElement::getError() const
70 void GraphicsElement::setError(QString const& error)
75 bool GraphicsElement::readFile(QString const& filename, QByteArray& data)
78 if(!reader_->readFile(filename, data))
80 setError(reader_->errorString());
87 bool GraphicsElement::getFont(QString const& name, QFont& font)
91 QMap<QString, QString>::const_iterator it = loadedFonts_.find(name);
93 if(it != loadedFonts_.end())
95 font = QFont(it.value());
99 if(!reader_->fileExists(name))
105 if(!readFile(name, data))
110 int id = QFontDatabase::addApplicationFontFromData(data);
114 setError("Unable to load font: " + name);
115 loadedFonts_[name] = "";
119 QStringList families = QFontDatabase::applicationFontFamilies(id);
121 if(families.size() == 0)
123 setError("No fonts found in " + name);
127 loadedFonts_[name] = families.at(0);
128 font = QFont(families.at(0));
133 int GraphicsElement::getAttribute(QString const& name,
134 QString const& value,
135 const GraphicsElement::AttributeDetails details[],
139 QString lower = name.toLower();
141 for(int i = 0; i < count; i++)
143 if(details[i].name == lower)
145 if(!details[i].isInt)
152 int tmp = value.toInt(&ok);
161 setError("Value for " + name + " is not integer ("+value+")");
168 setError("Unknown attribute: " + name);