1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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>
29 #include <QVBoxLayout>
32 #include <QFormLayout>
37 DeviceState::DeviceState(QWidget
*parent
) :
38 QWidget(parent
), tabs(this)
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;
53 QFile
fin(":/resources/deviceoptions");
54 fin
.open(QFile::Text
| QFile::ReadOnly
);
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)
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
);
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
,
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);
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();
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();
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();
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(",");
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)
181 temp
->addItem(current
, i
);
182 if(current
== defVal
)
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")
219 return data("file").toString();
221 return data("nextfile").toString();
223 else if(tag
.toLower() == "d")
225 QString path
= tag
[0].isLower()
226 ? data("file").toString() : data("nextfile").toString();
228 return directory(path
, params
[0].data
.number
);
234 int secs
= data("?pc").toInt();
235 return secsToString(secs
);
239 int secs
= data("?pt").toInt() - data("?pc").toInt();
242 return secsToString(secs
);
246 int secs
= data("?pt").toInt();
247 return secsToString(secs
);
251 int totalTime
= data("?pt").toInt();
252 int currentTime
= data("?pc").toInt();
253 return currentTime
* 100 / totalTime
;
257 double threshhold
= paramCount
> 0
258 ? params
[0].data
.number
/ 10. : 10;
259 if(data("?pc").toDouble() <= threshhold
)
266 double threshhold
= paramCount
> 0
267 ? params
[0].data
.number
/ 10. : 10;
268 if(data("?pt").toDouble() - data("?pc").toDouble() <= threshhold
)
275 return data("month");
279 int hour
= data("hour").toInt();
281 return "0" + QString::number(hour
);
291 int hour
= data("hour").toInt();
298 return "0" + QString::number(hour
);
304 int hour
= data("hour").toInt();
314 int month
= data("?cm").toInt() + 1;
316 return "0" + QString::number(month
);
322 int day
= data("day").toInt();
324 return "0" + QString::number(day
);
330 int minute
= data("minute").toInt();
332 return "0" + QString::number(minute
);
338 int second
= data("second").toInt();
340 return "0" + QString::number(second
);
346 QString year
= data("cY").toString();
347 return year
.right(2);
351 if(data("hour").toInt() >= 12)
358 if(data("hour").toInt() >= 12)
365 QString day
= data("cw").toString();
370 int month
= data("cm").toInt();
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";
389 int day
= data("?cw").toInt();
394 else if(tag
== "?cu")
396 int day
= data("?cw").toInt() - 1;
407 int seconds
= data("seconds").toInt();
409 return "0" + QString::number(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)
426 return dynamic_cast<QLineEdit
*>(found
.second
)->text();
429 return dynamic_cast<QSlider
*>(found
.second
)->value();
432 return dynamic_cast<QSpinBox
*>(found
.second
)->value();
435 return dynamic_cast<QDoubleSpinBox
*>(found
.second
)->value();
439 return dynamic_cast<QComboBox
*>(found
.second
)->currentIndex();
441 return dynamic_cast<QComboBox
*>(found
.second
)->currentText();
444 return dynamic_cast<QCheckBox
*>(found
.second
)->isChecked();
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)
461 dynamic_cast<QLineEdit
*>(found
.second
)->setText(data
.toString());
465 dynamic_cast<QSlider
*>(found
.second
)->setValue(data
.toInt());
469 dynamic_cast<QSpinBox
*>(found
.second
)->setValue(data
.toInt());
473 dynamic_cast<QDoubleSpinBox
*>(found
.second
)->setValue(data
.toDouble());
477 if(data
.type() == QVariant::String
)
478 dynamic_cast<QComboBox
*>
480 setCurrentIndex(dynamic_cast<QComboBox
*>
481 (found
.second
)->findText(data
.toString()));
483 dynamic_cast<QComboBox
*>(found
.second
)->
484 setCurrentIndex(data
.toInt());
488 dynamic_cast<QCheckBox
*>(found
.second
)->setChecked(data
.toBool());
492 emit
settingsChanged();
495 void DeviceState::input()
497 emit
settingsChanged();
500 QString
DeviceState::fileName(QString path
, bool extension
)
502 path
= path
.split("/").last();
506 QStringList name
= path
.split(".");
507 for(int i
= 0; i
< name
.count() - 1; i
++)
517 QString
DeviceState::directory(QString path
, int level
)
519 QStringList dirs
= path
.split("/");
520 int index
= dirs
.count() - 1 - level
;
526 QString
DeviceState::secsToString(int secs
)
546 retval
+= QString::number(hours
);
553 retval
+= QString::number(minutes
);
559 retval
+= QString::number(secs
);