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>
24 #include "textelement.h"
26 #include "graphicsscene.h"
31 const GraphicsElement::AttributeDetails ATTRIBUTES[TextElement::ATTRIBUTE_COUNT] =
45 TextElement::TextElement(Reader* reader): GraphicsElement(reader),
46 data_(""), format_(""), align_("left"), fontSize_(16), hasSpecialField_(false)
48 element_ = new QGraphicsTextItem();
51 bool TextElement::setAttribute(QString const& name, QString const& value)
56 if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
58 Attribute attr = static_cast<Attribute>(attrId);
60 bool fontChanged = false;
65 element_->setX(intVal);
68 element_->setY(intVal);
77 element_->setTextWidth(intVal);
83 element_->setDefaultTextColor(QColor(value));
94 qDebug() << "Unknown attribute: " << attr;
100 if(!fontFile_.isEmpty())
104 if(getFont(fontFile_, font))
106 font.setPointSize(fontSize_);
107 element_->setFont(font);
116 element_->setFont(QFont("Default", fontSize_));
129 void TextElement::addToScene(GraphicsScene* scene)
133 QString replaced = data_;
135 hasSpecialField_ = replaceSpecialField(replaced);
137 updateHtml(replaced);
139 scene->addItem(element_);
142 void TextElement::update()
144 if(!hasSpecialField_)
149 QString replaced = data_;
150 replaceSpecialField(replaced);
152 updateHtml(replaced);
155 void TextElement::updateHtml(QString const& data)
157 element_->setHtml("<div align='"+align_+"'>"+data+"</div>");
160 bool TextElement::replaceSpecialField(QString& value)
164 if(value.indexOf("{TRIP}") != -1)
166 value = value.replace("{TRIP}", formatString(Odometer::instance().getTrip()));
170 if(value.indexOf("{TOTAL}") != -1)
172 value = value.replace("{TOTAL}", formatString(Odometer::instance().getTotal()));
176 if(value.indexOf("{SPEED}") != -1)
178 value = value.replace("{SPEED}", formatString(Odometer::instance().getLatestFix().speed));
182 if(value.indexOf("{SPEED_UNIT}") != -1)
184 value = value.replace("{SPEED_UNIT}", Odometer::getSpeedUnit());
188 if(value.indexOf("{UNIT}") != -1)
190 value = value.replace("{UNIT}", Odometer::getUnit());
198 QString TextElement::formatString(double val)
200 QString format = format_;
208 result.sprintf(format.toLatin1().data(), val);