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 <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"
30 #include "textelement.h"
31 #include "imageelement.h"
32 #include "rectangle.h"
37 #include "poialerts.h"
38 #include "speedalarm.h"
40 GraphicsElement::GraphicsElement(Reader* reader): QObject(0),
41 reader_(reader), error_(""), effect_(0), visibleWhen_(ALWAYS), canBeVisible_(true)
45 GraphicsElement* GraphicsElement::getElement(QString const& name, Reader* reader)
49 return new ImageElement(reader);
54 return new TextElement(reader);
57 if(name == "rectangle")
59 return new Rectangle(reader);
64 bool animate = Settings::instance().value("animate_pointer", true).toBool();
65 return new Pointer(reader, animate);
70 bool animate = Settings::instance().value("animate_pointer", true).toBool();
71 return new Compass(reader, animate);
74 qDebug() << "Element not found: " << name;
78 QString const& GraphicsElement::getError() const
83 void GraphicsElement::setError(QString const& error)
88 bool GraphicsElement::readFile(QString const& filename, QByteArray& data)
90 if(!reader_->readFile(filename, data))
92 setError(reader_->errorString());
99 bool GraphicsElement::getFont(QString const& name, QFont& font)
103 QMap<QString, QString>::const_iterator it = loadedFonts_.find(name);
105 if(it != loadedFonts_.end())
107 font = QFont(it.value());
111 if(!reader_->fileExists(name))
117 if(!readFile(name, data))
122 int id = QFontDatabase::addApplicationFontFromData(data);
126 setError("Unable to load font: " + name);
127 loadedFonts_[name] = "";
131 QStringList families = QFontDatabase::applicationFontFamilies(id);
133 if(families.size() == 0)
135 setError("No fonts found in " + name);
139 loadedFonts_[name] = families.at(0);
140 font = QFont(families.at(0));
145 int GraphicsElement::getAttribute(QString const& name,
146 QString const& value,
147 const GraphicsElement::AttributeDetails details[],
151 QString lower = name.toLower();
153 for(int i = 0; i < count; i++)
155 if(details[i].name == lower)
157 if(!details[i].isInt)
164 int tmp = value.toInt(&ok);
173 setError("Value for " + name + " is not integer ("+value+")");
180 setError("Unknown attribute: " + name);
184 bool GraphicsElement::setEffect(QString const& name)
186 effect_ = Effect::getEffect(name);
196 bool GraphicsElement::setEffectAttribute(QString const& name, QString const& value)
200 qDebug() << "Effect not set";
204 return effect_->setAttribute(name, value);
207 void GraphicsElement::applyEffect()
209 effect_->apply(this);
212 void GraphicsElement::setVisibleWhen(VisibleWhen when)
214 if(when != visibleWhen_)
221 updateVisibility(false);
222 connect(&(PoiAlerts::instance()), SIGNAL(visibilityChanged(bool)), this, SLOT(updateVisibility(bool)));
225 updateVisibility(false);
226 connect(&(SpeedAlarm::instance()), SIGNAL(speedExceedChanged(bool)), this, SLOT(updateVisibility(bool)));
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);
237 GraphicsElement::VisibleWhen GraphicsElement::strToVisibleWhen(QString const& str) const
239 if(str == "poivisible")
243 else if(str == "speedexceeded")
245 return SPEED_EXCEEDED;
251 void GraphicsElement::updateVisibility(bool visible)
253 QGraphicsItem* item = getElement();
257 item->setVisible(visible);
261 canBeVisible_ = false;
265 canBeVisible_ = true;
270 bool GraphicsElement::canBeVisible() const
272 return canBeVisible_;