92bf7fa14b7f1bec472d2b01ac4b4a6c6fa6335d
[jspeed] / src / textelement.cpp
1 /*
2  * This file is part of jSpeed.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  */
18
19 #include <QtCore/QString>
20 #include <QtCore/QDebug>
21 #include <QtGui/QGraphicsScene>
22 #include <QtGui/QGraphicsTextItem>
23 #include "reader.h"
24 #include "textelement.h"
25 #include "odometer.h"
26 #include "graphicsscene.h"
27 #include "location.h"
28
29 namespace
30 {
31     const GraphicsElement::AttributeDetails ATTRIBUTES[TextElement::ATTRIBUTE_COUNT] =
32     {
33      {"xpos", true},
34      {"ypos", true},
35      {"data", false},
36      {"format", false},
37      {"width", true},
38      {"align", false},
39      {"color", false},
40      {"size", true},
41      {"font", false}
42     };
43 }
44
45 TextElement::TextElement(Reader* reader): GraphicsElement(reader),
46 data_(""), format_(""), align_("left"), fontSize_(16), hasSpecialField_(false)
47 {
48     element_ = new QGraphicsTextItem();
49 }
50
51 bool TextElement::setAttribute(QString const& name, QString const& value)
52 {
53     int intVal = 0;
54     int attrId = -1;
55
56     if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
57     {
58         Attribute attr = static_cast<Attribute>(attrId);
59
60         bool fontChanged = false;
61
62         switch(attr)
63         {
64         case XPOS:
65             element_->setX(intVal);
66             break;
67         case YPOS:
68             element_->setY(intVal);
69             break;
70         case DATA:
71             data_ = value;
72             break;
73         case FORMAT:
74             format_ = value;
75             break;
76         case WIDTH:
77             element_->setTextWidth(intVal);
78             break;
79         case ALIGN:
80             align_ = value;
81             break;
82         case COLOR:
83             element_->setDefaultTextColor(QColor(value));
84             break;
85         case SIZE:
86             fontSize_ = intVal;
87             fontChanged = true;
88             break;
89         case FONT:
90             fontFile_ = value;
91             fontChanged = true;
92             break;
93         default:
94             qDebug() << "Unknown attribute: " << attr;
95             return false;
96         }
97
98         if(fontChanged)
99         {
100             if(!fontFile_.isEmpty())
101             {
102                 QFont font;
103
104                 if(getFont(fontFile_, font))
105                 {
106                     font.setPointSize(fontSize_);
107                     element_->setFont(font);
108                 }
109                 else
110                 {
111                     return false;
112                 }
113             }
114             else
115             {
116                 element_->setFont(QFont("Default", fontSize_));
117             }
118         }
119
120         return true;
121
122     }
123     else
124     {
125         return false;
126     }
127 }
128
129 void TextElement::addToScene(GraphicsScene* scene)
130 {
131     Location::Fix fix;
132
133     QString replaced = data_;
134
135     hasSpecialField_ = replaceSpecialField(replaced);
136
137     updateHtml(replaced);
138
139     scene->addItem(element_);
140 }
141
142 void TextElement::update()
143 {
144     if(!hasSpecialField_)
145     {
146         return;
147     }
148
149     QString replaced = data_;
150     replaceSpecialField(replaced);
151
152     updateHtml(replaced);
153 }
154
155 void TextElement::updateHtml(QString const& data)
156 {
157     element_->setHtml("<div align='"+align_+"'>"+data+"</div>");
158 }
159
160 bool TextElement::replaceSpecialField(QString& value)
161 {
162     bool found = false;
163
164     if(value.indexOf("{TRIP}") != -1)
165     {
166         value = value.replace("{TRIP}", formatString(Odometer::instance().getTrip()));
167         found = true;
168     }
169
170     if(value.indexOf("{TOTAL}") != -1)
171     {
172         value = value.replace("{TOTAL}", formatString(Odometer::instance().getTotal()));
173         found = true;
174     }
175
176     if(value.indexOf("{SPEED}") != -1)
177     {
178         value = value.replace("{SPEED}", formatString(Odometer::instance().getLatestFix().speed));
179         found = true;
180     }
181
182     if(value.indexOf("{SPEED_UNIT}") != -1)
183     {
184         value = value.replace("{SPEED_UNIT}", Odometer::getSpeedUnit());
185         found = true;
186     }
187
188     if(value.indexOf("{UNIT}") != -1)
189     {
190         value = value.replace("{UNIT}", Odometer::getUnit());
191         found = true;
192     }
193
194     return found;
195
196 }
197
198 QString TextElement::formatString(double val)
199 {
200     QString format = format_;
201
202     if(format.isEmpty())
203     {
204         format = "%.1lf";
205     }
206
207     QString result;
208     result.sprintf(format.toLatin1().data(), val);
209
210     return result;
211 }