--- /dev/null
+/*
+ * This file is part of jSpeed.
+ *
+ * jSpeed is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * jSpeed is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with jSpeed. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <QtCore/QString>
+#include <QtCore/QDebug>
+#include <QtGui/QGraphicsScene>
+#include <QtGui/QGraphicsTextItem>
+#include "reader.h"
+#include "textelement.h"
+#include "odometer.h"
+#include "graphicsscene.h"
+#include "location.h"
+
+namespace
+{
+ const GraphicsElement::AttributeDetails ATTRIBUTES[TextElement::ATTRIBUTE_COUNT] =
+ {
+ {"xpos", true},
+ {"ypos", true},
+ {"data", false},
+ {"format", false},
+ {"width", true},
+ {"align", false},
+ {"color", false},
+ {"size", true},
+ {"font", false}
+ };
+}
+
+TextElement::TextElement(Reader* reader): GraphicsElement(reader),
+data_(""), format_(""), align_("left"), fontSize_(16), hasSpecialField_(false)
+{
+ element_ = new QGraphicsTextItem();
+}
+
+bool TextElement::setAttribute(QString const& name, QString const& value)
+{
+ int intVal = 0;
+ int attrId = -1;
+
+ if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
+ {
+ Attribute attr = static_cast<Attribute>(attrId);
+
+ bool fontChanged = false;
+
+ switch(attr)
+ {
+ case XPOS:
+ element_->setX(intVal);
+ break;
+ case YPOS:
+ element_->setY(intVal);
+ break;
+ case DATA:
+ data_ = value;
+ break;
+ case FORMAT:
+ format_ = value;
+ break;
+ case WIDTH:
+ element_->setTextWidth(intVal);
+ break;
+ case ALIGN:
+ align_ = value;
+ break;
+ case COLOR:
+ element_->setDefaultTextColor(QColor(value));
+ break;
+ case SIZE:
+ fontSize_ = intVal;
+ fontChanged = true;
+ break;
+ case FONT:
+ fontFile_ = value;
+ fontChanged = true;
+ break;
+ default:
+ qDebug() << "Unknown attribute: " << attr;
+ return false;
+ }
+
+ if(fontChanged)
+ {
+ if(!fontFile_.isEmpty())
+ {
+ QFont font;
+
+ if(getFont(fontFile_, font))
+ {
+ font.setPointSize(fontSize_);
+ element_->setFont(font);
+ }
+ else
+ {
+ return false;
+ }
+ }
+ else
+ {
+ element_->setFont(QFont("Default", fontSize_));
+ }
+ }
+
+ return true;
+
+ }
+ else
+ {
+ return false;
+ }
+}
+
+void TextElement::addToScene(GraphicsScene* scene)
+{
+ Location::Fix fix;
+
+ QString replaced = data_;
+
+ hasSpecialField_ = replaceSpecialField(replaced);
+
+ updateHtml(replaced);
+
+ scene->addItem(element_);
+}
+
+void TextElement::update()
+{
+ if(!hasSpecialField_)
+ {
+ return;
+ }
+
+ QString replaced = data_;
+ replaceSpecialField(replaced);
+
+ updateHtml(replaced);
+}
+
+void TextElement::updateHtml(QString const& data)
+{
+ element_->setHtml("<div align='"+align_+"'>"+data+"</div>");
+}
+
+bool TextElement::replaceSpecialField(QString& value)
+{
+ bool found = false;
+
+ if(value.indexOf("{TRIP}") != -1)
+ {
+ value = value.replace("{TRIP}", formatString(Odometer::instance().getTrip()));
+ found = true;
+ }
+
+ if(value.indexOf("{TOTAL}") != -1)
+ {
+ value = value.replace("{TOTAL}", formatString(Odometer::instance().getTotal()));
+ found = true;
+ }
+
+ if(value.indexOf("{SPEED}") != -1)
+ {
+ value = value.replace("{SPEED}", formatString(Odometer::instance().getLatestFix().speed));
+ found = true;
+ }
+
+ if(value.indexOf("{SPEED_UNIT}") != -1)
+ {
+ value = value.replace("{SPEED_UNIT}", Odometer::getSpeedUnit());
+ found = true;
+ }
+
+ if(value.indexOf("{UNIT}") != -1)
+ {
+ value = value.replace("{UNIT}", Odometer::getUnit());
+ found = true;
+ }
+
+ return found;
+
+}
+
+QString TextElement::formatString(double val)
+{
+ QString format = format_;
+
+ if(format.isEmpty())
+ {
+ format = "%.1lf";
+ }
+
+ QString result;
+ result.sprintf(format.toLatin1().data(), val);
+
+ return result;
+}