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