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] =
44 const QString FIELDS[TextElement::FIELD_COUNT] = {"TRIP", "TOTAL", "SPEED",
45 "MAXSPEED", "AVGSPEED",
46 "UNIT", "SPEEDUNIT", "TIME"};
49 TextElement::TextElement(Reader* reader): GraphicsElement(reader),
50 data_(""), format_(""), align_("left"), fontSize_(16)
52 element_ = new QGraphicsTextItem();
55 bool TextElement::setAttribute(QString const& name, QString const& value)
60 if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
62 Attribute attr = static_cast<Attribute>(attrId);
64 bool fontChanged = false;
69 element_->setX(intVal);
72 element_->setY(intVal);
81 element_->setTextWidth(intVal);
87 element_->setDefaultTextColor(QColor(value));
98 qDebug() << "Unknown attribute: " << attr;
104 if(!fontFile_.isEmpty())
108 if(getFont(fontFile_, font))
110 font.setPointSize(fontSize_);
111 element_->setFont(font);
120 element_->setFont(QFont("Default", fontSize_));
133 void TextElement::addToScene(GraphicsScene* scene)
135 specialFields_.clear();
137 for(int i = 0; i < FIELD_COUNT; i++)
139 if(data_.indexOf("{" + FIELDS[i] + "}") != -1)
141 specialFields_.push_back(static_cast<Field>(i));
145 QString replaced = data_;
146 replaceSpecialFields(replaced);
147 updateHtml(replaced);
149 scene->addItem(element_);
152 void TextElement::update()
154 if(specialFields_.isEmpty())
159 QString replaced = data_;
160 replaceSpecialFields(replaced);
162 updateHtml(replaced);
165 void TextElement::updateHtml(QString const& data)
167 element_->setHtml("<div align='"+align_+"'>"+data+"</div>");
170 void TextElement::replaceSpecialFields(QString& value)
172 for(int i = 0; i < specialFields_.size(); i++)
174 Field f = specialFields_.at(i);
175 Odometer* o = &(Odometer::instance());
180 replaceValue(value, f, formatString(o->getTrip()));
183 replaceValue(value, f, formatString(o->getTotal()));
186 replaceValue(value, f, formatString(o->getLatestFix().speed));
189 replaceValue(value, f, formatString(o->getMaxSpeed()));
192 replaceValue(value, f, formatString(o->getAverageSpeed()));
195 replaceValue(value, f, o->getUnit());
198 replaceValue(value, f, o->getSpeedUnit());
201 replaceValue(value, f, QTime::currentTime().toString("hh:mm"));
204 qDebug() << "Unknown field: " << f;
209 void TextElement::replaceValue(QString& value,
210 TextElement::Field field,
211 QString const& replace)
213 value = value.replace("{" + FIELDS[field] + "}", replace);
216 QString TextElement::formatString(double val)
218 QString format = format_;
226 result.sprintf(format.toLatin1().data(), val);
232 QGraphicsItem* TextElement::getElement() const