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