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