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/QDebug>
21 #include <QtCore/QTime>
22 #include <QtGui/QGraphicsScene>
23 #include <QtGui/QGraphicsTextItem>
24 #include "textelement.h"
27 #include "graphicsscene.h"
31 const GraphicsElement::AttributeDetails ATTRIBUTES[TextElement::ATTRIBUTE_COUNT] =
46 const QString FIELDS[TextElement::FIELD_COUNT] = {"TRIP", "TOTAL", "SPEED",
47 "MAXSPEED", "AVGSPEED",
48 "UNIT", "SPEEDUNIT", "TIME"};
51 TextElement::TextElement(Reader* reader): GraphicsElement(reader),
52 data_(""), format_(""), align_("left"), fontSize_(16), bold_(false), italic_(false)
54 element_ = new QGraphicsTextItem();
57 bool TextElement::setAttribute(QString const& name, QString const& value)
62 if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
64 Attribute attr = static_cast<Attribute>(attrId);
66 bool fontChanged = false;
71 element_->setX(intVal);
74 element_->setY(intVal);
83 element_->setTextWidth(intVal);
89 element_->setDefaultTextColor(QColor(value));
100 if(value != "false" && value != "0")
107 if(value != "false" && value != "0")
114 qDebug() << "Unknown attribute: " << attr;
120 if(!fontFile_.isEmpty())
124 if(getFont(fontFile_, font))
126 font.setPointSize(fontSize_);
134 font.setItalic(true);
136 element_->setFont(font);
145 QFont font("Default");
153 font.setItalic(true);
156 font.setPointSize(fontSize_);
157 element_->setFont(font);
170 void TextElement::addToScene(GraphicsScene* scene)
172 specialFields_.clear();
174 for(int i = 0; i < FIELD_COUNT; i++)
176 if(data_.indexOf("{" + FIELDS[i] + "}") != -1)
178 specialFields_.push_back(static_cast<Field>(i));
183 QString replaced = data_;
184 replaceSpecialFields(replaced);
185 updateHtml(replaced);
187 scene->addItem(element_);
190 void TextElement::update()
192 if(specialFields_.isEmpty())
197 QString replaced = data_;
198 replaceSpecialFields(replaced);
200 updateHtml(replaced);
203 void TextElement::updateHtml(QString const& data)
205 element_->setHtml("<div align='"+align_+"'>"+data+"</div>");
208 void TextElement::replaceSpecialFields(QString& value)
210 for(int i = 0; i < specialFields_.size(); i++)
212 Field f = specialFields_.at(i);
213 Odometer* o = &(Odometer::instance());
218 replaceValue(value, f, formatString(o->getTrip()));
221 replaceValue(value, f, formatString(o->getTotal()));
224 replaceValue(value, f, formatString(o->getLatestFix().speed));
227 replaceValue(value, f, formatString(o->getMaxSpeed()));
230 replaceValue(value, f, formatString(o->getAverageSpeed()));
233 replaceValue(value, f, o->getUnit());
236 replaceValue(value, f, o->getSpeedUnit());
239 replaceValue(value, f, QTime::currentTime().toString("hh:mm"));
242 qDebug() << "Unknown field: " << f;
247 void TextElement::replaceValue(QString& value,
248 TextElement::Field field,
249 QString const& replace)
251 value = value.replace("{" + FIELDS[field] + "}", replace);
254 QString TextElement::formatString(double val)
256 QString format = format_;
264 result.sprintf(format.toLatin1().data(), val);
270 QGraphicsItem* TextElement::getElement() const