1c49b7c30d880903f290cd760f5ec99272612172
[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 <QtCore/QTime>
22 #include <QtGui/QGraphicsScene>
23 #include <QtGui/QGraphicsTextItem>
24 #include "textelement.h"
25 #include "reader.h"
26 #include "odometer.h"
27 #include "graphicsscene.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      {"bold", false},
43      {"italic", false}
44     };
45
46     const QString FIELDS[TextElement::FIELD_COUNT] =  {"TRIP", "TOTAL", "SPEED",
47                                                        "MAXSPEED", "AVGSPEED",
48                                                        "UNIT", "SPEEDUNIT", "TIME"};
49 }
50
51 TextElement::TextElement(Reader* reader): GraphicsElement(reader),
52 data_(""), format_(""), align_("left"), fontSize_(16), bold_(false), italic_(false)
53 {
54     element_ = new QGraphicsTextItem();
55 }
56
57 bool TextElement::setAttribute(QString const& name, QString const& value)
58 {
59     int intVal = 0;
60     int attrId = -1;
61
62     if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
63     {
64         Attribute attr = static_cast<Attribute>(attrId);
65
66         bool fontChanged = false;
67
68         switch(attr)
69         {
70         case XPOS:
71             element_->setX(intVal);
72             break;
73         case YPOS:
74             element_->setY(intVal);
75             break;
76         case DATA:
77             data_ = value;
78             break;
79         case FORMAT:
80             format_ = value;
81             break;
82         case WIDTH:
83             element_->setTextWidth(intVal);
84             break;
85         case ALIGN:
86             align_ = value;
87             break;
88         case COLOR:
89             element_->setDefaultTextColor(QColor(value));
90             break;
91         case SIZE:
92             fontSize_ = intVal;
93             fontChanged = true;
94             break;
95         case FONT:
96             fontFile_ = value;
97             fontChanged = true;
98             break;
99         case BOLD:
100             if(value != "false" && value != "0")
101             {
102                 bold_ = true;
103                 fontChanged = true;
104             }
105             break;
106         case ITALIC:
107             if(value != "false" && value != "0")
108             {
109                 italic_ = true;
110                 fontChanged = true;
111             }
112             break;
113         default:
114             qDebug() << "Unknown attribute: " << attr;
115             return false;
116         }
117
118         if(fontChanged)
119         {
120             if(!fontFile_.isEmpty())
121             {
122                 QFont font;
123
124                 if(getFont(fontFile_, font))
125                 {
126                     font.setPointSize(fontSize_);
127
128                     if(bold_)
129                     {
130                         font.setBold(true);
131                     }
132                     if(italic_)
133                     {
134                         font.setItalic(true);
135                     }
136                     element_->setFont(font);
137                 }
138                 else
139                 {
140                     return false;
141                 }
142             }
143             else
144             {
145                 QFont font("Default");
146
147                 if(bold_)
148                 {
149                     font.setBold(true);
150                 }
151                 if(italic_)
152                 {
153                     font.setItalic(true);
154                 }
155
156                 font.setPointSize(fontSize_);
157                 element_->setFont(font);
158             }
159         }
160
161         return true;
162
163     }
164     else
165     {
166         return false;
167     }
168 }
169
170 void TextElement::addToScene(GraphicsScene* scene)
171 {
172     specialFields_.clear();
173
174     for(int i = 0; i < FIELD_COUNT; i++)
175     {
176         if(data_.indexOf("{" + FIELDS[i] + "}") != -1)
177         {
178             specialFields_.push_back(static_cast<Field>(i));
179         }
180
181     }
182
183     QString replaced = data_;
184     replaceSpecialFields(replaced);
185     updateHtml(replaced);
186
187     scene->addItem(element_);
188 }
189
190 void TextElement::update()
191 {
192     if(specialFields_.isEmpty())
193     {
194         return;
195     }
196
197     QString replaced = data_;
198     replaceSpecialFields(replaced);
199
200     updateHtml(replaced);
201 }
202
203 void TextElement::updateHtml(QString const& data)
204 {
205     element_->setHtml("<div align='"+align_+"'>"+data+"</div>");
206 }
207
208 void TextElement::replaceSpecialFields(QString& value)
209 {
210     for(int i = 0; i < specialFields_.size(); i++)
211     {
212         Field f = specialFields_.at(i);
213         Odometer* o = &(Odometer::instance());
214
215         switch(f)
216         {
217         case TRIP:
218             replaceValue(value, f, formatString(o->getTrip()));
219             break;
220         case TOTAL:
221             replaceValue(value, f, formatString(o->getTotal()));
222             break;
223         case SPEED:
224             replaceValue(value, f, formatString(o->getLatestFix().speed));
225             break;
226         case MAXSPEED:
227             replaceValue(value, f, formatString(o->getMaxSpeed()));
228             break;
229         case AVGSPEED:
230             replaceValue(value, f, formatString(o->getAverageSpeed()));
231             break;
232         case UNIT:
233             replaceValue(value, f, o->getUnit());
234             break;
235         case SPEEDUNIT:
236             replaceValue(value, f, o->getSpeedUnit());
237             break;
238         case TIME:
239             replaceValue(value, f, QTime::currentTime().toString("hh:mm"));
240             break;
241         default:
242             qDebug() << "Unknown field: " << f;
243         }
244     }
245 }
246
247 void TextElement::replaceValue(QString& value,
248                                TextElement::Field field,
249                                QString const& replace)
250 {
251     value = value.replace("{" + FIELDS[field] + "}", replace);
252 }
253
254 QString TextElement::formatString(double val)
255 {
256     QString format = format_;
257
258     if(format.isEmpty())
259     {
260         format = "%.1lf";
261     }
262
263     QString result;
264     result.sprintf(format.toLatin1().data(), val);
265
266     return result;
267 }
268
269
270 QGraphicsItem* TextElement::getElement() const
271 {
272     return element_;
273 }