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"
28 #include "poialerts.h"
32 const GraphicsElement::AttributeDetails ATTRIBUTES[TextElement::ATTRIBUTE_COUNT] =
37 {"visiblewhen", false},
48 {"letterspacing", true},
49 {"timeformat", false},
53 const QString FIELDS[TextElement::FIELD_COUNT] = {"TRIP", "TOTAL", "SPEED",
54 "MAXSPEED", "AVGSPEED",
55 "UNIT", "SPEEDUNIT", "TIME", "DATE",
56 "POIDISTANCE", "ANGLE", "LATITUDE",
57 "LONGITUDE", "ALTITUDE", "DISTANCEUNIT"};
60 TextElement::TextElement(Reader* reader): GraphicsElement(reader),
61 data_(""), format_(""), align_("left"), timeFormat_("hh:mm"),
62 dateFormat_("dd.MM.yyyy"), fontSize_(16), bold_(false), italic_(false),
63 uppercase_(false), letterSpacing_(0)
65 element_ = new QGraphicsTextItem();
68 bool TextElement::setAttribute(QString const& name, QString const& value)
73 if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
75 Attribute attr = static_cast<Attribute>(attrId);
77 bool fontChanged = false;
82 element_->setX(intVal);
85 element_->setY(intVal);
88 element_->setZValue(intVal);
91 setVisibleWhen(strToVisibleWhen(value));
100 element_->setTextWidth(intVal);
106 element_->setDefaultTextColor(QColor(value));
117 if(value != "false" && value != "0")
124 if(value != "false" && value != "0")
131 if(value != "false" && value != "0")
137 letterSpacing_ = intVal;
147 qDebug() << "Unknown attribute: " << attr;
165 bool TextElement::applyFont()
169 if(!fontFile_.isEmpty())
171 if(!getFont(fontFile_, font))
177 font.setPointSize(fontSize_);
186 font.setItalic(true);
191 font.setLetterSpacing(QFont::PercentageSpacing, letterSpacing_);
194 element_->setFont(font);
199 void TextElement::addToScene(GraphicsScene* scene)
201 specialFields_.clear();
203 for(int i = 0; i < FIELD_COUNT; i++)
205 if(data_.indexOf("{" + FIELDS[i] + "}") != -1)
207 specialFields_.push_back(static_cast<Field>(i));
212 QString replaced = data_;
213 replaceSpecialFields(replaced);
214 updateHtml(replaced);
216 scene->addItem(element_);
219 void TextElement::update()
221 if(specialFields_.isEmpty())
226 QString replaced = data_;
227 replaceSpecialFields(replaced);
229 updateHtml(replaced);
232 void TextElement::updateHtml(QString data)
236 data = data.toUpper();
239 element_->setHtml("<div align='"+align_+"'>"+data+"</div>");
242 void TextElement::replaceSpecialFields(QString& value)
244 for(int i = 0; i < specialFields_.size(); i++)
246 Field f = specialFields_.at(i);
247 Odometer* o = &(Odometer::instance());
252 replaceValue(value, f, formatString(o->getTrip()));
255 replaceValue(value, f, formatString(o->getTotal()));
258 replaceValue(value, f, formatString(o->getLatestFix().speed));
261 replaceValue(value, f, formatString(o->getMaxSpeed()));
264 replaceValue(value, f, formatString(o->getAverageSpeed()));
267 replaceValue(value, f, o->getUnit());
270 replaceValue(value, f, o->getSpeedUnit());
273 replaceValue(value, f, QTime::currentTime().toString(timeFormat_));
276 replaceValue(value, f, QDate::currentDate().toString(dateFormat_));
279 replaceValue(value, f, handleEmpty(PoiAlerts::instance().getCurrentDistance()));
282 replaceValue(value, f, formatString(o->getLatestFix().track));
285 replaceValue(value, f, handleEmpty(o->getLatestFix().latitude));
288 replaceValue(value, f, handleEmpty(o->getLatestFix().longitude));
291 replaceValue(value, f, handleEmpty(o->getLatestFix().altitude));
294 replaceValue(value, f, o->getMeterUnit());
297 qDebug() << "Unknown field: " << f;
302 QString TextElement::handleEmpty(double value)
309 return formatString(value);
312 void TextElement::replaceValue(QString& value,
313 TextElement::Field field,
314 QString const& replace)
316 value = value.replace("{" + FIELDS[field] + "}", replace);
319 QString TextElement::formatString(double val)
321 QString format = format_;
329 result.sprintf(format.toLatin1().data(), val);
335 QGraphicsItem* TextElement::getElement() const