Theme Editor: Committed FS#11477 to add a DECIMAL parameter type in the parser and...
[maemo-rb.git] / utils / themeeditor / gui / devicestate.cpp
blobe766a64835fad149e50e3ce5316656f96059f60b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Robert Bieber
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "devicestate.h"
24 #include <QScrollArea>
25 #include <QFile>
26 #include <QCheckBox>
27 #include <QSpinBox>
28 #include <QComboBox>
29 #include <QVBoxLayout>
30 #include <QLabel>
31 #include <QLineEdit>
32 #include <QFormLayout>
33 #include <QTime>
35 #include <cstdlib>
37 DeviceState::DeviceState(QWidget *parent) :
38 QWidget(parent), tabs(this)
40 /* UI stuff */
41 resize(500,400);
42 setWindowIcon(QIcon(":/resources/windowicon.png"));
43 setWindowTitle(tr("Device Settings"));
45 QFormLayout* layout = new QFormLayout(this);
46 layout->addWidget(&tabs);
47 this->setLayout(layout);
49 /* Loading the tabs */
50 QScrollArea* currentArea = 0;
51 QWidget* panel;
53 QFile fin(":/resources/deviceoptions");
54 fin.open(QFile::Text | QFile::ReadOnly);
55 while(!fin.atEnd())
57 QString line = QString(fin.readLine());
58 line = line.trimmed();
60 /* Continue on a comment or an empty line */
61 if(line[0] == '#' || line.length() == 0)
62 continue;
64 if(line[0] == '[')
66 QString buffer;
67 for(int i = 1; line[i] != ']'; i++)
68 buffer.append(line[i]);
69 buffer = buffer.trimmed();
71 panel = new QWidget();
72 currentArea = new QScrollArea();
73 layout = new QFormLayout(panel);
74 currentArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
75 currentArea->setWidget(panel);
76 currentArea->setWidgetResizable(true);
78 tabs.addTab(currentArea, buffer);
80 continue;
83 QStringList elements = line.split(";");
84 QString tag = elements[0].trimmed();
85 QString title = elements[1].trimmed();
86 QString type = elements[2].trimmed();
87 QString defVal = elements[3].trimmed();
89 elements = type.split("(");
90 if(elements[0].trimmed() == "text")
92 QLineEdit* temp = new QLineEdit(defVal, currentArea);
93 layout->addRow(title, temp);
94 inputs.insert(tag, QPair<InputType, QWidget*>(Text, temp));
96 temp->setSizePolicy(QSizePolicy(QSizePolicy::Preferred,
97 QSizePolicy::Fixed));
99 QObject::connect(temp, SIGNAL(textChanged(QString)),
100 this, SLOT(input()));
102 else if(elements[0].trimmed() == "check")
104 QCheckBox* temp = new QCheckBox(title, currentArea);
105 layout->addRow(temp);
106 if(defVal.toLower() == "true")
107 temp->setChecked(true);
108 else
109 temp->setChecked(false);
110 inputs.insert(tag, QPair<InputType, QWidget*>(Check, temp));
112 QObject::connect(temp, SIGNAL(toggled(bool)),
113 this, SLOT(input()));
115 else if(elements[0].trimmed() == "slider")
117 elements = elements[1].trimmed().split(",");
118 int min = elements[0].trimmed().toInt();
119 QString maxS = elements[1].trimmed();
120 maxS.chop(1);
121 int max = maxS.toInt();
123 QSlider* temp = new QSlider(Qt::Horizontal, currentArea);
124 temp->setMinimum(min);
125 temp->setMaximum(max);
126 temp->setValue(defVal.toInt());
127 layout->addRow(title, temp);
128 inputs.insert(tag, QPair<InputType, QWidget*>(Slide, temp));
130 QObject::connect(temp, SIGNAL(valueChanged(int)),
131 this, SLOT(input()));
133 else if(elements[0].trimmed() == "spin")
135 elements = elements[1].trimmed().split(",");
136 int min = elements[0].trimmed().toInt();
137 QString maxS = elements[1].trimmed();
138 maxS.chop(1);
139 int max = maxS.toInt();
141 QSpinBox* temp = new QSpinBox(currentArea);
142 temp->setMinimum(min);
143 temp->setMaximum(max);
144 temp->setValue(defVal.toInt());
145 layout->addRow(title, temp);
146 inputs.insert(tag, QPair<InputType, QWidget*>(Spin, temp));
148 QObject::connect(temp, SIGNAL(valueChanged(int)),
149 this, SLOT(input()));
151 else if(elements[0].trimmed() == "fspin")
153 elements = elements[1].trimmed().split(",");
154 int min = elements[0].trimmed().toDouble();
155 QString maxS = elements[1].trimmed();
156 maxS.chop(1);
157 int max = maxS.toDouble();
159 QDoubleSpinBox* temp = new QDoubleSpinBox(currentArea);
160 temp->setMinimum(min);
161 temp->setMaximum(max);
162 temp->setValue(defVal.toDouble());
163 temp->setSingleStep(0.1);
164 layout->addRow(title, temp);
165 inputs.insert(tag, QPair<InputType, QWidget*>(DSpin, temp));
167 QObject::connect(temp, SIGNAL(valueChanged(double)),
168 this, SLOT(input()));
170 else if(elements[0].trimmed() == "combo")
172 elements = elements[1].trimmed().split(",");
174 int defIndex = 0;
175 QComboBox* temp = new QComboBox(currentArea);
176 for(int i = 0; i < elements.count(); i++)
178 QString current = elements[i].trimmed();
179 if(i == elements.count() - 1)
180 current.chop(1);
181 temp->addItem(current, i);
182 if(current == defVal)
183 defIndex = i;
185 temp->setCurrentIndex(defIndex);
186 layout->addRow(title, temp);
187 inputs.insert(tag, QPair<InputType, QWidget*>(Combo, temp));
189 QObject::connect(temp, SIGNAL(currentIndexChanged(int)),
190 this, SLOT(input()));
196 DeviceState::~DeviceState()
200 QVariant DeviceState::data(QString tag, int paramCount,
201 skin_tag_parameter *params)
203 /* Handling special cases */
204 if(tag.toLower() == "fm")
206 QString path = tag[0].isLower()
207 ? data("file").toString() : data("nextfile").toString();
208 return fileName(path, true);
210 else if(tag.toLower() == "fn")
212 QString path = tag[0].isLower()
213 ? data("file").toString() : data("nextfile").toString();
214 return fileName(path, false);
216 else if(tag.toLower() == "fp")
218 if(tag[0].isLower())
219 return data("file").toString();
220 else
221 return data("nextfile").toString();
223 else if(tag.toLower() == "d")
225 QString path = tag[0].isLower()
226 ? data("file").toString() : data("nextfile").toString();
227 if(paramCount > 0)
228 return directory(path, params[0].data.number);
229 else
230 return QVariant();
232 else if(tag == "pc")
234 int secs = data("?pc").toInt();
235 return secsToString(secs);
237 else if(tag == "pr")
239 int secs = data("?pt").toInt() - data("?pc").toInt();
240 if(secs < 0)
241 secs = 0;
242 return secsToString(secs);
244 else if(tag == "pt")
246 int secs = data("?pt").toInt();
247 return secsToString(secs);
249 else if(tag == "px")
251 int totalTime = data("?pt").toInt();
252 int currentTime = data("?pc").toInt();
253 return currentTime * 100 / totalTime;
255 else if(tag == "pS")
257 double threshhold = paramCount > 0
258 ? params[0].data.number / 10. : 10;
259 if(data("?pc").toDouble() <= threshhold)
260 return true;
261 else
262 return false;
264 else if(tag == "pE")
266 double threshhold = paramCount > 0
267 ? params[0].data.number / 10. : 10;
268 if(data("?pt").toDouble() - data("?pc").toDouble() <= threshhold)
269 return true;
270 else
271 return false;
273 else if(tag == "ce")
275 return data("month");
277 else if(tag == "cH")
279 int hour = data("hour").toInt();
280 if(hour < 10)
281 return "0" + QString::number(hour);
282 else
283 return hour;
285 else if(tag == "cK")
287 return data("hour");
289 else if(tag == "cI")
291 int hour = data("hour").toInt();
292 if(hour > 12)
293 hour -= 12;
294 if(hour == 0)
295 hour = 12;
297 if(hour < 10)
298 return "0" + QString::number(hour);
299 else
300 return hour;
302 else if(tag == "cl")
304 int hour = data("hour").toInt();
305 if(hour > 12)
306 hour -= 12;
307 if(hour == 0)
308 hour = 12;
310 return hour;
312 else if(tag == "cm")
314 int month = data("?cm").toInt() + 1;
315 if(month < 10)
316 return "0" + QString::number(month);
317 else
318 return month;
320 else if(tag == "cd")
322 int day = data("day").toInt();
323 if(day < 10)
324 return "0" + QString::number(day);
325 else
326 return day;
328 else if(tag == "cM")
330 int minute = data("minute").toInt();
331 if(minute < 10)
332 return "0" + QString::number(minute);
333 else
334 return minute;
336 else if(tag == "cS")
338 int second = data("second").toInt();
339 if(second < 10)
340 return "0" + QString::number(second);
341 else
342 return second;
344 else if(tag == "cy")
346 QString year = data("cY").toString();
347 return year.right(2);
349 else if(tag == "cP")
351 if(data("hour").toInt() >= 12)
352 return "PM";
353 else
354 return "AM";
356 else if(tag == "cp")
358 if(data("hour").toInt() >= 12)
359 return "pm";
360 else
361 return "am";
363 else if(tag == "ca")
365 QString day = data("cw").toString();
366 return day.left(3);
368 else if(tag == "cb")
370 int month = data("cm").toInt();
371 switch(month)
373 case 1: return "Jan";
374 case 2: return "Feb";
375 case 3: return "Mar";
376 case 4: return "Apr";
377 case 5: return "May";
378 case 6: return "Jun";
379 case 7: return "Jul";
380 case 8: return "Aug";
381 case 9: return "Sep";
382 case 10: return "Oct";
383 case 11: return "Nov";
384 case 12: return "Dec";
387 else if(tag == "cu")
389 int day = data("?cw").toInt();
390 if(day == 0)
391 day = 7;
392 return day;
394 else if(tag == "?cu")
396 int day = data("?cw").toInt() - 1;
397 if(day == -1)
398 day = 6;
399 return day;
401 else if(tag == "cw")
403 return data("?cw");
405 else if(tag == "cs")
407 int seconds = data("seconds").toInt();
408 if(seconds < 10)
409 return "0" + QString::number(seconds);
410 else
411 return seconds;
414 QPair<InputType, QWidget*> found =
415 inputs.value(tag, QPair<InputType, QWidget*>(Slide, 0));
417 if(found.second == 0 && tag[0] == '?')
418 found = inputs.value(tag.right(2), QPair<InputType, QWidget*>(Slide,0));
420 if(found.second == 0)
421 return QVariant();
423 switch(found.first)
425 case Text:
426 return dynamic_cast<QLineEdit*>(found.second)->text();
428 case Slide:
429 return dynamic_cast<QSlider*>(found.second)->value();
431 case Spin:
432 return dynamic_cast<QSpinBox*>(found.second)->value();
434 case DSpin:
435 return dynamic_cast<QDoubleSpinBox*>(found.second)->value();
437 case Combo:
438 if(tag[0] == '?')
439 return dynamic_cast<QComboBox*>(found.second)->currentIndex();
440 else
441 return dynamic_cast<QComboBox*>(found.second)->currentText();
443 case Check:
444 return dynamic_cast<QCheckBox*>(found.second)->isChecked();
447 return QVariant();
450 void DeviceState::setData(QString tag, QVariant data)
452 QPair<InputType, QWidget*> found =
453 inputs.value(tag, QPair<InputType, QWidget*>(Slide, 0));
455 if(found.second == 0)
456 return;
458 switch(found.first)
460 case Text:
461 dynamic_cast<QLineEdit*>(found.second)->setText(data.toString());
462 break;
464 case Slide:
465 dynamic_cast<QSlider*>(found.second)->setValue(data.toInt());
466 break;
468 case Spin:
469 dynamic_cast<QSpinBox*>(found.second)->setValue(data.toInt());
470 break;
472 case DSpin:
473 dynamic_cast<QDoubleSpinBox*>(found.second)->setValue(data.toDouble());
474 break;
476 case Combo:
477 if(data.type() == QVariant::String)
478 dynamic_cast<QComboBox*>
479 (found.second)->
480 setCurrentIndex(dynamic_cast<QComboBox*>
481 (found.second)->findText(data.toString()));
482 else
483 dynamic_cast<QComboBox*>(found.second)->
484 setCurrentIndex(data.toInt());
485 break;
487 case Check:
488 dynamic_cast<QCheckBox*>(found.second)->setChecked(data.toBool());
489 break;
492 emit settingsChanged();
495 void DeviceState::input()
497 emit settingsChanged();
500 QString DeviceState::fileName(QString path, bool extension)
502 path = path.split("/").last();
503 if(!extension)
505 QString sum;
506 QStringList name = path.split(".");
507 for(int i = 0; i < name.count() - 1; i++)
508 sum.append(name[i]);
509 return sum;
511 else
513 return path;
517 QString DeviceState::directory(QString path, int level)
519 QStringList dirs = path.split("/");
520 int index = dirs.count() - 1 - level;
521 if(index < 0)
522 index = 0;
523 return dirs[index];
526 QString DeviceState::secsToString(int secs)
528 int hours = 0;
529 int minutes = 0;
530 while(secs >= 60)
532 minutes++;
533 secs -= 60;
536 while(minutes >= 60)
538 hours++;
539 minutes -= 60;
542 QString retval;
544 if(hours > 0)
546 retval += QString::number(hours);
547 if(minutes < 10)
548 retval += ":0";
549 else
550 retval += ":";
553 retval += QString::number(minutes);
554 if(secs < 10)
555 retval += ":0";
556 else
557 retval += ":";
559 retval += QString::number(secs);
560 return retval;