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] =
46 {"letterspacing", true},
47 {"timeformat", false},
51 const QString FIELDS[TextElement::FIELD_COUNT] = {"TRIP", "TOTAL", "SPEED",
52 "MAXSPEED", "AVGSPEED",
53 "UNIT", "SPEEDUNIT", "TIME", "DATE"};
56 TextElement::TextElement(Reader* reader): GraphicsElement(reader),
57 data_(""), format_(""), align_("left"), timeFormat_("hh:mm"), dateFormat_("dd.MM.yyyy"), fontSize_(16), bold_(false), italic_(false),
58 uppercase_(false), letterSpacing_(0)
60 element_ = new QGraphicsTextItem();
63 bool TextElement::setAttribute(QString const& name, QString const& value)
68 if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
70 Attribute attr = static_cast<Attribute>(attrId);
72 bool fontChanged = false;
77 element_->setX(intVal);
80 element_->setY(intVal);
83 element_->setZValue(intVal);
92 element_->setTextWidth(intVal);
98 element_->setDefaultTextColor(QColor(value));
109 if(value != "false" && value != "0")
116 if(value != "false" && value != "0")
123 if(value != "false" && value != "0")
129 letterSpacing_ = intVal;
139 qDebug() << "Unknown attribute: " << attr;
157 bool TextElement::applyFont()
161 if(!fontFile_.isEmpty())
163 if(!getFont(fontFile_, font))
169 font.setPointSize(fontSize_);
178 font.setItalic(true);
183 font.setLetterSpacing(QFont::PercentageSpacing, letterSpacing_);
186 element_->setFont(font);
191 void TextElement::addToScene(GraphicsScene* scene)
193 specialFields_.clear();
195 for(int i = 0; i < FIELD_COUNT; i++)
197 if(data_.indexOf("{" + FIELDS[i] + "}") != -1)
199 specialFields_.push_back(static_cast<Field>(i));
204 QString replaced = data_;
205 replaceSpecialFields(replaced);
206 updateHtml(replaced);
208 scene->addItem(element_);
211 void TextElement::update()
213 if(specialFields_.isEmpty())
218 QString replaced = data_;
219 replaceSpecialFields(replaced);
221 updateHtml(replaced);
224 void TextElement::updateHtml(QString data)
228 data = data.toUpper();
231 element_->setHtml("<div align='"+align_+"'>"+data+"</div>");
234 void TextElement::replaceSpecialFields(QString& value)
236 for(int i = 0; i < specialFields_.size(); i++)
238 Field f = specialFields_.at(i);
239 Odometer* o = &(Odometer::instance());
244 replaceValue(value, f, formatString(o->getTrip()));
247 replaceValue(value, f, formatString(o->getTotal()));
250 replaceValue(value, f, formatString(o->getLatestFix().speed));
253 replaceValue(value, f, formatString(o->getMaxSpeed()));
256 replaceValue(value, f, formatString(o->getAverageSpeed()));
259 replaceValue(value, f, o->getUnit());
262 replaceValue(value, f, o->getSpeedUnit());
265 replaceValue(value, f, QTime::currentTime().toString(timeFormat_));
268 replaceValue(value, f, QDate::currentDate().toString(dateFormat_));
271 qDebug() << "Unknown field: " << f;
276 void TextElement::replaceValue(QString& value,
277 TextElement::Field field,
278 QString const& replace)
280 value = value.replace("{" + FIELDS[field] + "}", replace);
283 QString TextElement::formatString(double val)
285 QString format = format_;
293 result.sprintf(format.toLatin1().data(), val);
299 QGraphicsItem* TextElement::getElement() const