Changed media player to initialize in boot.
[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 <QtGui/QGraphicsEffect>
26 #include <QtGui/QGraphicsDropShadowEffect>
27 #include <QtGui/QGraphicsItem>
28 #include "graphicselement.h"
29 #include "reader.h"
30 #include "textelement.h"
31 #include "imageelement.h"
32 #include "rectangle.h"
33 #include "pointer.h"
34 #include "compass.h"
35 #include "settings.h"
36 #include "effect.h"
37 #include "poialerts.h"
38 #include "speedalarm.h"
39
40 GraphicsElement::GraphicsElement(Reader* reader): QObject(0),
41 reader_(reader), error_(""), effect_(0), visibleWhen_(ALWAYS), canBeVisible_(true)
42 {
43 }
44
45 GraphicsElement* GraphicsElement::getElement(QString const& name, Reader* reader)
46 {
47     if(name == "image")
48     {
49         return new ImageElement(reader);
50     }
51
52     if(name == "text")
53     {
54         return new TextElement(reader);
55     }
56
57     if(name == "rectangle")
58     {
59         return new Rectangle(reader);
60     }
61
62     if(name == "pointer")
63     {
64         bool animate = Settings::instance().value("animate_pointer", true).toBool();
65         return new Pointer(reader, animate);
66     }
67
68     if(name == "compass")
69     {
70         bool animate = Settings::instance().value("animate_pointer", true).toBool();
71         return new Compass(reader, animate);
72     }
73
74     qDebug() << "Element not found: " << name;
75     return 0;
76 }
77
78 QString const& GraphicsElement::getError() const
79 {
80     return error_;
81 }
82
83 void GraphicsElement::setError(QString const& error)
84 {
85     error_ = error;
86 }
87
88 bool GraphicsElement::readFile(QString const& filename, QByteArray& data)
89 {
90     if(!reader_->readFile(filename, data))
91     {
92         setError(reader_->errorString());
93         return false;
94     }
95
96     return true;
97 }
98
99 bool GraphicsElement::getFont(QString const& name, QFont& font)
100 {
101     QByteArray data;
102
103     QMap<QString, QString>::const_iterator it = loadedFonts_.find(name);
104
105     if(it != loadedFonts_.end())
106     {
107         font = QFont(it.value());
108         return true;
109     }
110
111     if(!reader_->fileExists(name))
112     {
113         font = QFont(name);
114         return true;
115     }
116
117     if(!readFile(name, data))
118     {
119         return false;
120     }
121
122     int id = QFontDatabase::addApplicationFontFromData(data);
123
124     if(id == -1)
125     {
126         setError("Unable to load font: " + name);
127         loadedFonts_[name] = "";
128         return false;
129     }
130
131     QStringList families = QFontDatabase::applicationFontFamilies(id);
132
133     if(families.size() == 0)
134     {
135         setError("No fonts found in " + name);
136         return false;
137     }
138
139     loadedFonts_[name] = families.at(0);
140     font = QFont(families.at(0));
141
142     return true;
143 }
144
145 int GraphicsElement::getAttribute(QString const& name,
146                                   QString const& value,
147                                   const GraphicsElement::AttributeDetails details[],
148                                   int count,
149                                   int& intValue)
150 {
151     QString lower = name.toLower();
152
153     for(int i = 0; i < count; i++)
154     {
155         if(details[i].name == lower)
156         {
157             if(!details[i].isInt)
158             {
159                 return i;
160             }
161             else
162             {
163                 bool ok = true;
164                 int tmp = value.toInt(&ok);
165
166                 if(ok)
167                 {
168                     intValue = tmp;
169                     return i;
170                 }
171                 else
172                 {
173                     setError("Value for " + name + " is not integer ("+value+")");
174                     return -1;
175                 }
176             }
177         }
178     }
179
180     setError("Unknown attribute: " + name);
181     return -1;
182 }
183
184 bool GraphicsElement::setEffect(QString const& name)
185 {
186     effect_ = Effect::getEffect(name);
187
188     if(!effect_)
189     {
190         return false;
191     }
192
193     return true;
194 }
195
196 bool GraphicsElement::setEffectAttribute(QString const& name, QString const& value)
197 {
198     if(!effect_)
199     {
200         qDebug() << "Effect not set";
201         return false;
202     }
203
204     return effect_->setAttribute(name, value);
205 }
206
207 void GraphicsElement::applyEffect()
208 {
209     effect_->apply(this);
210 }
211
212 void GraphicsElement::setVisibleWhen(VisibleWhen when)
213 {
214     if(when != visibleWhen_)
215     {
216         visibleWhen_ = when;
217
218         switch(when)
219         {
220         case POI_VISIBLE:
221             updateVisibility(false);
222             connect(&(PoiAlerts::instance()), SIGNAL(visibilityChanged(bool)), this, SLOT(updateVisibility(bool)));
223             break;
224         case SPEED_EXCEEDED:
225             updateVisibility(false);
226             connect(&(SpeedAlarm::instance()), SIGNAL(speedExceedChanged(bool)), this, SLOT(updateVisibility(bool)));
227             break;
228         case ALWAYS:
229             disconnect(&(PoiAlerts::instance()), SIGNAL(visibilityChanged(bool)), this, SLOT(updateVisibility(bool)));
230             disconnect(&(SpeedAlarm::instance()), SIGNAL(speedExceedChanged(bool)), this, SLOT(updateVisibility(bool)));
231             updateVisibility(true);
232             break;
233         }
234     }
235 }
236
237 GraphicsElement::VisibleWhen GraphicsElement::strToVisibleWhen(QString const& str) const
238 {
239     if(str == "poivisible")
240     {
241         return POI_VISIBLE;
242     }
243     else if(str == "speedexceeded")
244     {
245         return SPEED_EXCEEDED;
246     }
247
248     return ALWAYS;
249 }
250
251 void GraphicsElement::updateVisibility(bool visible)
252 {
253     QGraphicsItem* item = getElement();
254
255     if(item)
256     {
257         item->setVisible(visible);
258
259         if(!visible)
260         {
261             canBeVisible_ = false;
262         }
263         else
264         {
265             canBeVisible_ = true;
266         }
267     }
268 }
269
270 bool GraphicsElement::canBeVisible() const
271 {
272     return canBeVisible_;
273 }