Added some effects to graphics elements and changed default theme a bit.
[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     };
43
44     const QString FIELDS[TextElement::FIELD_COUNT] =  {"TRIP", "TOTAL", "SPEED",
45                                                        "MAXSPEED", "AVGSPEED",
46                                                        "UNIT", "SPEEDUNIT", "TIME"};
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     specialFields_.clear();
136
137     for(int i = 0; i < FIELD_COUNT; i++)
138     {
139         if(data_.indexOf("{" + FIELDS[i] + "}") != -1)
140         {
141             specialFields_.push_back(static_cast<Field>(i));
142         }
143
144     }
145
146     QString replaced = data_;
147     replaceSpecialFields(replaced);
148     updateHtml(replaced);
149
150     scene->addItem(element_);
151 }
152
153 void TextElement::update()
154 {
155     if(specialFields_.isEmpty())
156     {
157         return;
158     }
159
160     QString replaced = data_;
161     replaceSpecialFields(replaced);
162
163     updateHtml(replaced);
164 }
165
166 void TextElement::updateHtml(QString const& data)
167 {
168     element_->setHtml("<div align='"+align_+"'>"+data+"</div>");
169 }
170
171 void TextElement::replaceSpecialFields(QString& value)
172 {
173     for(int i = 0; i < specialFields_.size(); i++)
174     {
175         Field f = specialFields_.at(i);
176         Odometer* o = &(Odometer::instance());
177
178         switch(f)
179         {
180         case TRIP:
181             replaceValue(value, f, formatString(o->getTrip()));
182             break;
183         case TOTAL:
184             replaceValue(value, f, formatString(o->getTotal()));
185             break;
186         case SPEED:
187             replaceValue(value, f, formatString(o->getLatestFix().speed));
188             break;
189         case MAXSPEED:
190             replaceValue(value, f, formatString(o->getMaxSpeed()));
191             break;
192         case AVGSPEED:
193             replaceValue(value, f, formatString(o->getAverageSpeed()));
194             break;
195         case UNIT:
196             replaceValue(value, f, o->getUnit());
197             break;
198         case SPEEDUNIT:
199             replaceValue(value, f, o->getSpeedUnit());
200             break;
201         case TIME:
202             replaceValue(value, f, QTime::currentTime().toString("hh:mm"));
203             break;
204         default:
205             qDebug() << "Unknown field: " << f;
206         }
207     }
208 }
209
210 void TextElement::replaceValue(QString& value,
211                                TextElement::Field field,
212                                QString const& replace)
213 {
214     value = value.replace("{" + FIELDS[field] + "}", replace);
215 }
216
217 QString TextElement::formatString(double val)
218 {
219     QString format = format_;
220
221     if(format.isEmpty())
222     {
223         format = "%.1lf";
224     }
225
226     QString result;
227     result.sprintf(format.toLatin1().data(), val);
228
229     return result;
230 }
231
232
233 QGraphicsItem* TextElement::getElement() const
234 {
235     return element_;
236 }