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