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] =
45 {"letterspacing", true}
48 const QString FIELDS[TextElement::FIELD_COUNT] = {"TRIP", "TOTAL", "SPEED",
49 "MAXSPEED", "AVGSPEED",
50 "UNIT", "SPEEDUNIT", "TIME"};
53 TextElement::TextElement(Reader* reader): GraphicsElement(reader),
54 data_(""), format_(""), align_("left"), fontSize_(16), bold_(false), italic_(false),
55 uppercase_(false), letterSpacing_(0)
57 element_ = new QGraphicsTextItem();
60 bool TextElement::setAttribute(QString const& name, QString const& value)
65 if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
67 Attribute attr = static_cast<Attribute>(attrId);
69 bool fontChanged = false;
74 element_->setX(intVal);
77 element_->setY(intVal);
86 element_->setTextWidth(intVal);
92 element_->setDefaultTextColor(QColor(value));
103 if(value != "false" && value != "0")
110 if(value != "false" && value != "0")
117 if(value != "false" && value != "0")
123 letterSpacing_ = intVal;
127 qDebug() << "Unknown attribute: " << attr;
145 bool TextElement::applyFont()
149 if(!fontFile_.isEmpty())
151 if(!getFont(fontFile_, font))
157 font.setPointSize(fontSize_);
166 font.setItalic(true);
171 font.setLetterSpacing(QFont::PercentageSpacing, letterSpacing_);
174 element_->setFont(font);
179 void TextElement::addToScene(GraphicsScene* scene)
181 specialFields_.clear();
183 for(int i = 0; i < FIELD_COUNT; i++)
185 if(data_.indexOf("{" + FIELDS[i] + "}") != -1)
187 specialFields_.push_back(static_cast<Field>(i));
192 QString replaced = data_;
193 replaceSpecialFields(replaced);
194 updateHtml(replaced);
196 scene->addItem(element_);
199 void TextElement::update()
201 if(specialFields_.isEmpty())
206 QString replaced = data_;
207 replaceSpecialFields(replaced);
209 updateHtml(replaced);
212 void TextElement::updateHtml(QString data)
216 data = data.toUpper();
219 element_->setHtml("<div align='"+align_+"'>"+data+"</div>");
222 void TextElement::replaceSpecialFields(QString& value)
224 for(int i = 0; i < specialFields_.size(); i++)
226 Field f = specialFields_.at(i);
227 Odometer* o = &(Odometer::instance());
232 replaceValue(value, f, formatString(o->getTrip()));
235 replaceValue(value, f, formatString(o->getTotal()));
238 replaceValue(value, f, formatString(o->getLatestFix().speed));
241 replaceValue(value, f, formatString(o->getMaxSpeed()));
244 replaceValue(value, f, formatString(o->getAverageSpeed()));
247 replaceValue(value, f, o->getUnit());
250 replaceValue(value, f, o->getSpeedUnit());
253 replaceValue(value, f, QTime::currentTime().toString("hh:mm"));
256 qDebug() << "Unknown field: " << f;
261 void TextElement::replaceValue(QString& value,
262 TextElement::Field field,
263 QString const& replace)
265 value = value.replace("{" + FIELDS[field] + "}", replace);
268 QString TextElement::formatString(double val)
270 QString format = format_;
278 result.sprintf(format.toLatin1().data(), val);
284 QGraphicsItem* TextElement::getElement() const