3befe299b7c18e24656dec3cdf1fe82197475fb5
[jspeed] / src / graphicselement.cpp
1 /*
2  * This file is part of jSpeed.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  */
18
19 #include <QtCore/QString>
20 #include <QtCore/QByteArray>
21 #include <QtCore/QDebug>
22 #include <QtGui/QGraphicsScene>
23 #include <QtGui/QFont>
24 #include <QtGui/QFontDatabase>
25 #include "graphicselement.h"
26 #include "reader.h"
27 #include "textelement.h"
28 #include "imageelement.h"
29 #include "rectangle.h"
30 #include "pointer.h"
31 #include "settings.h"
32
33 GraphicsElement::GraphicsElement(Reader* reader): reader_(reader), error_("")
34 {
35 }
36
37 GraphicsElement* GraphicsElement::getElement(QString const& name, Reader* reader)
38 {
39     if(name == "image")
40     {
41         return new ImageElement(reader);
42     }
43
44     if(name == "text")
45     {
46         return new TextElement(reader);
47     }
48
49     if(name == "rectangle")
50     {
51         return new Rectangle(reader);
52     }
53
54     if(name == "pointer")
55     {
56         bool animate = Settings::instance().value("animate_pointer", true).toBool();
57         return new Pointer(reader, animate);
58     }
59
60     qDebug() << "Element not found: " << name;
61     return 0;
62 }
63
64 QString const& GraphicsElement::getError() const
65 {
66     return error_;
67 }
68
69 void GraphicsElement::setError(QString const& error)
70 {
71     error_ = error;
72 }
73
74 bool GraphicsElement::readFile(QString const& filename, QByteArray& data)
75 {
76
77     if(!reader_->readFile(filename, data))
78     {
79         setError(reader_->errorString());
80         return false;
81     }
82
83     return true;
84 }
85
86 bool GraphicsElement::getFont(QString const& name, QFont& font)
87 {
88     QByteArray data;
89
90     QMap<QString, QString>::const_iterator it = loadedFonts_.find(name);
91
92     if(it != loadedFonts_.end())
93     {
94         font = QFont(it.value());
95         return true;
96     }
97
98     if(!reader_->fileExists(name))
99     {
100         font = QFont(name);
101         return true;
102     }
103
104     if(!readFile(name, data))
105     {
106         return false;
107     }
108
109     int id = QFontDatabase::addApplicationFontFromData(data);
110
111     if(id == -1)
112     {
113         setError("Unable to load font: " + name);
114         loadedFonts_[name] = "";
115         return false;
116     }
117
118     QStringList families = QFontDatabase::applicationFontFamilies(id);
119
120     if(families.size() == 0)
121     {
122         setError("No fonts found in " + name);
123         return false;
124     }
125
126     loadedFonts_[name] = families.at(0);
127     font = QFont(families.at(0));
128
129     return true;
130 }
131
132 int GraphicsElement::getAttribute(QString const& name,
133                                   QString const& value,
134                                   const GraphicsElement::AttributeDetails details[],
135                                   int count,
136                                   int& intValue)
137 {
138     QString lower = name.toLower();
139
140     for(int i = 0; i < count; i++)
141     {
142         if(details[i].name == lower)
143         {
144             if(!details[i].isInt)
145             {
146                 return i;
147             }
148             else
149             {
150                 bool ok = true;
151                 int tmp = value.toInt(&ok);
152
153                 if(ok)
154                 {
155                     intValue = tmp;
156                     return i;
157                 }
158                 else
159                 {
160                     setError("Value for " + name + " is not integer ("+value+")");
161                     return -1;
162                 }
163             }
164         }
165     }
166
167     setError("Unknown attribute: " + name);
168     return -1;
169 }