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",
60 TextElement::TextElement(Reader* reader): GraphicsElement(reader),
61 data_(""), format_(""), align_("left"), timeFormat_("hh:mm"), dateFormat_("dd.MM.yyyy"), fontSize_(16), bold_(false), italic_(false),
62 uppercase_(false), letterSpacing_(0)
64 element_ = new QGraphicsTextItem();
67 bool TextElement::setAttribute(QString const& name, QString const& value)
72 if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
74 Attribute attr = static_cast<Attribute>(attrId);
76 bool fontChanged = false;
81 element_->setX(intVal);
84 element_->setY(intVal);
87 element_->setZValue(intVal);
90 setVisibleWhen(strToVisibleWhen(value));
99 element_->setTextWidth(intVal);
105 element_->setDefaultTextColor(QColor(value));
116 if(value != "false" && value != "0")
123 if(value != "false" && value != "0")
130 if(value != "false" && value != "0")
136 letterSpacing_ = intVal;
146 qDebug() << "Unknown attribute: " << attr;
164 bool TextElement::applyFont()
168 if(!fontFile_.isEmpty())
170 if(!getFont(fontFile_, font))
176 font.setPointSize(fontSize_);
185 font.setItalic(true);
190 font.setLetterSpacing(QFont::PercentageSpacing, letterSpacing_);
193 element_->setFont(font);
198 void TextElement::addToScene(GraphicsScene* scene)
200 specialFields_.clear();
202 for(int i = 0; i < FIELD_COUNT; i++)
204 if(data_.indexOf("{" + FIELDS[i] + "}") != -1)
206 specialFields_.push_back(static_cast<Field>(i));
211 QString replaced = data_;
212 replaceSpecialFields(replaced);
213 updateHtml(replaced);
215 scene->addItem(element_);
218 void TextElement::update()
220 if(specialFields_.isEmpty())
225 QString replaced = data_;
226 replaceSpecialFields(replaced);
228 updateHtml(replaced);
231 void TextElement::updateHtml(QString data)
235 data = data.toUpper();
238 element_->setHtml("<div align='"+align_+"'>"+data+"</div>");
241 void TextElement::replaceSpecialFields(QString& value)
243 for(int i = 0; i < specialFields_.size(); i++)
245 Field f = specialFields_.at(i);
246 Odometer* o = &(Odometer::instance());
251 replaceValue(value, f, formatString(o->getTrip()));
254 replaceValue(value, f, formatString(o->getTotal()));
257 replaceValue(value, f, formatString(o->getLatestFix().speed));
260 replaceValue(value, f, formatString(o->getMaxSpeed()));
263 replaceValue(value, f, formatString(o->getAverageSpeed()));
266 replaceValue(value, f, o->getUnit());
269 replaceValue(value, f, o->getSpeedUnit());
272 replaceValue(value, f, QTime::currentTime().toString(timeFormat_));
275 replaceValue(value, f, QDate::currentDate().toString(dateFormat_));
278 replaceValue(value, f, handleEmpty(PoiAlerts::instance().getCurrentDistance()));
281 replaceValue(value, f, formatString(o->getLatestFix().track));
284 replaceValue(value, f, handleEmpty(o->getLatestFix().latitude));
287 replaceValue(value, f, handleEmpty(o->getLatestFix().longitude));
290 qDebug() << "Unknown field: " << f;
295 QString TextElement::handleEmpty(double value)
302 return formatString(value);
305 void TextElement::replaceValue(QString& value,
306 TextElement::Field field,
307 QString const& replace)
309 value = value.replace("{" + FIELDS[field] + "}", replace);
312 QString TextElement::formatString(double val)
314 QString format = format_;
322 result.sprintf(format.toLatin1().data(), val);
328 QGraphicsItem* TextElement::getElement() const