Improved special field handling in text element.
[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     const QString FIELDS[TextElement::FIELD_COUNT] =  {"TRIP", "TOTAL", "SPEED",
45                                                        "MAXSPEED", "AVGSPEED",
46                                                        "UNIT", "SPEEDUNIT"};
47 }
48
49 TextElement::TextElement(Reader* reader): GraphicsElement(reader),
50 data_(""), format_(""), align_("left"), fontSize_(16)
51 {
52     element_ = new QGraphicsTextItem();
53 }
54
55 bool TextElement::setAttribute(QString const& name, QString const& value)
56 {
57     int intVal = 0;
58     int attrId = -1;
59
60     if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
61     {
62         Attribute attr = static_cast<Attribute>(attrId);
63
64         bool fontChanged = false;
65
66         switch(attr)
67         {
68         case XPOS:
69             element_->setX(intVal);
70             break;
71         case YPOS:
72             element_->setY(intVal);
73             break;
74         case DATA:
75             data_ = value;
76             break;
77         case FORMAT:
78             format_ = value;
79             break;
80         case WIDTH:
81             element_->setTextWidth(intVal);
82             break;
83         case ALIGN:
84             align_ = value;
85             break;
86         case COLOR:
87             element_->setDefaultTextColor(QColor(value));
88             break;
89         case SIZE:
90             fontSize_ = intVal;
91             fontChanged = true;
92             break;
93         case FONT:
94             fontFile_ = value;
95             fontChanged = true;
96             break;
97         default:
98             qDebug() << "Unknown attribute: " << attr;
99             return false;
100         }
101
102         if(fontChanged)
103         {
104             if(!fontFile_.isEmpty())
105             {
106                 QFont font;
107
108                 if(getFont(fontFile_, font))
109                 {
110                     font.setPointSize(fontSize_);
111                     element_->setFont(font);
112                 }
113                 else
114                 {
115                     return false;
116                 }
117             }
118             else
119             {
120                 element_->setFont(QFont("Default", fontSize_));
121             }
122         }
123
124         return true;
125
126     }
127     else
128     {
129         return false;
130     }
131 }
132
133 void TextElement::addToScene(GraphicsScene* scene)
134 {
135     Location::Fix fix;
136
137     specialFields_.clear();
138
139     for(int i = 0; i < FIELD_COUNT; i++)
140     {
141         if(data_.indexOf("{" + FIELDS[i] + "}") != -1)
142         {
143             specialFields_.push_back(static_cast<Field>(i));
144         }
145     }
146
147     QString replaced = data_;
148     replaceSpecialFields(replaced);
149     updateHtml(replaced);
150
151     scene->addItem(element_);
152 }
153
154 void TextElement::update()
155 {
156     if(specialFields_.isEmpty())
157     {
158         return;
159     }
160
161     QString replaced = data_;
162     replaceSpecialFields(replaced);
163
164     updateHtml(replaced);
165 }
166
167 void TextElement::updateHtml(QString const& data)
168 {
169     element_->setHtml("<div align='"+align_+"'>"+data+"</div>");
170 }
171
172 void TextElement::replaceSpecialFields(QString& value)
173 {
174     for(int i = 0; i < specialFields_.size(); i++)
175     {
176         Field f = specialFields_.at(i);
177         Odometer* o = &(Odometer::instance());
178
179         switch(f)
180         {
181         case TRIP:
182             replaceValue(value, f, formatString(o->getTrip()));
183             break;
184         case TOTAL:
185             replaceValue(value, f, formatString(o->getTotal()));
186             break;
187         case SPEED:
188             replaceValue(value, f, formatString(o->getLatestFix().speed));
189             break;
190         case MAXSPEED:
191             replaceValue(value, f, formatString(o->getMaxSpeed()));
192             break;
193         case AVGSPEED:
194             replaceValue(value, f, formatString(o->getAverageSpeed()));
195             break;
196         case UNIT:
197             replaceValue(value, f, o->getUnit());
198             break;
199         case SPEEDUNIT:
200             replaceValue(value, f, o->getSpeedUnit());
201             break;
202         default:
203             qDebug() << "Unknown field: " << f;
204         }
205     }
206 }
207
208 void TextElement::replaceValue(QString& value,
209                                TextElement::Field field,
210                                QString const& replace)
211 {
212     value = value.replace("{" + FIELDS[field] + "}", replace);
213 }
214
215 QString TextElement::formatString(double val)
216 {
217     QString format = format_;
218
219     if(format.isEmpty())
220     {
221         format = "%.1lf";
222     }
223
224     QString result;
225     result.sprintf(format.toLatin1().data(), val);
226
227     return result;
228 }