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 <QtGui/QGraphicsScene>
22 #include <QtGui/QGraphicsTextItem>
23 #include "textelement.h"
26 #include "graphicsscene.h"
30 const GraphicsElement::AttributeDetails ATTRIBUTES[TextElement::ATTRIBUTE_COUNT] =
43 const QString FIELDS[TextElement::FIELD_COUNT] = {"TRIP", "TOTAL", "SPEED",
44 "MAXSPEED", "AVGSPEED",
48 TextElement::TextElement(Reader* reader): GraphicsElement(reader),
49 data_(""), format_(""), align_("left"), fontSize_(16)
51 element_ = new QGraphicsTextItem();
54 bool TextElement::setAttribute(QString const& name, QString const& value)
59 if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
61 Attribute attr = static_cast<Attribute>(attrId);
63 bool fontChanged = false;
68 element_->setX(intVal);
71 element_->setY(intVal);
80 element_->setTextWidth(intVal);
86 element_->setDefaultTextColor(QColor(value));
97 qDebug() << "Unknown attribute: " << attr;
103 if(!fontFile_.isEmpty())
107 if(getFont(fontFile_, font))
109 font.setPointSize(fontSize_);
110 element_->setFont(font);
119 element_->setFont(QFont("Default", fontSize_));
132 void TextElement::addToScene(GraphicsScene* scene)
134 specialFields_.clear();
136 for(int i = 0; i < FIELD_COUNT; i++)
138 if(data_.indexOf("{" + FIELDS[i] + "}") != -1)
140 specialFields_.push_back(static_cast<Field>(i));
144 QString replaced = data_;
145 replaceSpecialFields(replaced);
146 updateHtml(replaced);
148 scene->addItem(element_);
151 void TextElement::update()
153 if(specialFields_.isEmpty())
158 QString replaced = data_;
159 replaceSpecialFields(replaced);
161 updateHtml(replaced);
164 void TextElement::updateHtml(QString const& data)
166 element_->setHtml("<div align='"+align_+"'>"+data+"</div>");
169 void TextElement::replaceSpecialFields(QString& value)
171 for(int i = 0; i < specialFields_.size(); i++)
173 Field f = specialFields_.at(i);
174 Odometer* o = &(Odometer::instance());
179 replaceValue(value, f, formatString(o->getTrip()));
182 replaceValue(value, f, formatString(o->getTotal()));
185 replaceValue(value, f, formatString(o->getLatestFix().speed));
188 replaceValue(value, f, formatString(o->getMaxSpeed()));
191 replaceValue(value, f, formatString(o->getAverageSpeed()));
194 replaceValue(value, f, o->getUnit());
197 replaceValue(value, f, o->getSpeedUnit());
200 qDebug() << "Unknown field: " << f;
205 void TextElement::replaceValue(QString& value,
206 TextElement::Field field,
207 QString const& replace)
209 value = value.replace("{" + FIELDS[field] + "}", replace);
212 QString TextElement::formatString(double val)
214 QString format = format_;
222 result.sprintf(format.toLatin1().data(), val);