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), reader_(reader), error_(""), effect_(0), visibleWhen_(ALWAYS)
44 GraphicsElement* GraphicsElement::getElement(QString const& name, Reader* reader)
48 return new ImageElement(reader);
53 return new TextElement(reader);
56 if(name == "rectangle")
58 return new Rectangle(reader);
63 bool animate = Settings::instance().value("animate_pointer", true).toBool();
64 return new Pointer(reader, animate);
69 bool animate = Settings::instance().value("animate_pointer", true).toBool();
70 return new Compass(reader, animate);
73 qDebug() << "Element not found: " << name;
77 QString const& GraphicsElement::getError() const
82 void GraphicsElement::setError(QString const& error)
87 bool GraphicsElement::readFile(QString const& filename, QByteArray& data)
89 if(!reader_->readFile(filename, data))
91 setError(reader_->errorString());
98 bool GraphicsElement::getFont(QString const& name, QFont& font)
102 QMap<QString, QString>::const_iterator it = loadedFonts_.find(name);
104 if(it != loadedFonts_.end())
106 font = QFont(it.value());
110 if(!reader_->fileExists(name))
116 if(!readFile(name, data))
121 int id = QFontDatabase::addApplicationFontFromData(data);
125 setError("Unable to load font: " + name);
126 loadedFonts_[name] = "";
130 QStringList families = QFontDatabase::applicationFontFamilies(id);
132 if(families.size() == 0)
134 setError("No fonts found in " + name);
138 loadedFonts_[name] = families.at(0);
139 font = QFont(families.at(0));
144 int GraphicsElement::getAttribute(QString const& name,
145 QString const& value,
146 const GraphicsElement::AttributeDetails details[],
150 QString lower = name.toLower();
152 for(int i = 0; i < count; i++)
154 if(details[i].name == lower)
156 if(!details[i].isInt)
163 int tmp = value.toInt(&ok);
172 setError("Value for " + name + " is not integer ("+value+")");
179 setError("Unknown attribute: " + name);
183 bool GraphicsElement::setEffect(QString const& name)
185 effect_ = Effect::getEffect(name);
195 bool GraphicsElement::setEffectAttribute(QString const& name, QString const& value)
199 qDebug() << "Effect not set";
203 return effect_->setAttribute(name, value);
206 void GraphicsElement::applyEffect()
208 effect_->apply(getElement());
211 void GraphicsElement::setVisibleWhen(VisibleWhen when)
213 if(when != visibleWhen_)
220 connect(&(PoiAlerts::instance()), SIGNAL(visibilityChanged(bool)), this, SLOT(updateVisibility(bool)));
223 connect(&(SpeedAlarm::instance()), SIGNAL(speedExceedChanged(bool)), this, SLOT(updateVisibility(bool)));
226 disconnect(&(PoiAlerts::instance()), SIGNAL(visibilityChanged(bool)), this, SLOT(updateVisibility(bool)));
227 disconnect(&(SpeedAlarm::instance()), SIGNAL(speedExceedChanged(bool)), this, SLOT(updateVisibility(bool)));
233 GraphicsElement::VisibleWhen GraphicsElement::strToVisibleWhen(QString const& str) const
235 if(str == "poivisible")
239 else if(str == "speedexceeded")
241 return SPEED_EXCEEDED;
247 void GraphicsElement::updateVisibility(bool visible)
249 QGraphicsItem* item = getElement();
253 item->setVisible(visible);