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"
23 #include "ui_devicestate.h"
25 #include <QScrollArea>
31 DeviceState::DeviceState(QWidget
*parent
) :
32 QWidget(parent
), tabs(this)
36 setWindowIcon(QIcon(":/resources/windowicon.png"));
37 setWindowTitle(tr("Device Settings"));
39 QVBoxLayout
* layout
= new QVBoxLayout(this);
40 layout
->addWidget(&tabs
);
41 this->setLayout(layout
);
43 /* Loading the tabs */
44 QScrollArea
* currentArea
;
45 QHBoxLayout
* subLayout
;
49 QFile
fin(":/resources/deviceoptions");
50 fin
.open(QFile::Text
| QFile::ReadOnly
);
53 QString line
= QString(fin
.readLine());
54 line
= line
.trimmed();
56 /* Continue on a comment or an empty line */
57 if(line
[0] == '#' || line
.length() == 0)
63 for(int i
= 1; line
[i
] != ']'; i
++)
64 buffer
.append(line
[i
]);
65 buffer
= buffer
.trimmed();
67 panel
= new QWidget();
68 currentArea
= new QScrollArea();
69 layout
= new QVBoxLayout(panel
);
70 currentArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
71 currentArea
->setWidget(panel
);
72 currentArea
->setWidgetResizable(true);
74 tabs
.addTab(currentArea
, buffer
);
79 QStringList elements
= line
.split(";");
80 QString tag
= elements
[0].trimmed();
81 QString title
= elements
[1].trimmed();
82 QString type
= elements
[2].trimmed();
83 QString defVal
= elements
[3].trimmed();
85 subLayout
= new QHBoxLayout();
87 subLayout
->addWidget(new QLabel(elements
[1].trimmed(), currentArea
));
88 layout
->addLayout(subLayout
);
91 elements
= type
.split("(");
92 if(elements
[0].trimmed() == "text")
94 temp
= new QLineEdit(defVal
, currentArea
);
95 subLayout
->addWidget(temp
);
96 inputs
.insert(tag
, QPair
<InputType
, QWidget
*>(Text
, temp
));
98 else if(elements
[0].trimmed() == "check")
100 temp
= new QCheckBox(title
, currentArea
);
101 subLayout
->addWidget(temp
);
102 if(defVal
.toLower() == "true")
103 dynamic_cast<QCheckBox
*>(temp
)->setChecked(true);
105 dynamic_cast<QCheckBox
*>(temp
)->setChecked(false);
106 inputs
.insert(tag
, QPair
<InputType
, QWidget
*>(Check
, temp
));
108 else if(elements
[0].trimmed() == "slider")
110 elements
= elements
[1].trimmed().split(",");
111 int min
= elements
[0].trimmed().toInt();
112 QString maxS
= elements
[1].trimmed();
114 int max
= maxS
.toInt();
116 temp
= new QSlider(Qt::Horizontal
, currentArea
);
117 dynamic_cast<QSlider
*>(temp
)->setMinimum(min
);
118 dynamic_cast<QSlider
*>(temp
)->setMaximum(max
);
119 dynamic_cast<QSlider
*>(temp
)->setValue(defVal
.toInt());
120 subLayout
->addWidget(temp
);
121 inputs
.insert(tag
, QPair
<InputType
, QWidget
*>(Slide
, temp
));
123 else if(elements
[0].trimmed() == "spin")
125 elements
= elements
[1].trimmed().split(",");
126 int min
= elements
[0].trimmed().toInt();
127 QString maxS
= elements
[1].trimmed();
129 int max
= maxS
.toInt();
131 temp
= new QSpinBox(currentArea
);
132 dynamic_cast<QSpinBox
*>(temp
)->setMinimum(min
);
133 dynamic_cast<QSpinBox
*>(temp
)->setMaximum(max
);
134 dynamic_cast<QSpinBox
*>(temp
)->setValue(defVal
.toInt());
135 subLayout
->addWidget(temp
);
136 inputs
.insert(tag
, QPair
<InputType
, QWidget
*>(Spin
, temp
));
138 else if(elements
[0].trimmed() == "fspin")
140 elements
= elements
[1].trimmed().split(",");
141 int min
= elements
[0].trimmed().toDouble();
142 QString maxS
= elements
[1].trimmed();
144 int max
= maxS
.toDouble();
146 temp
= new QDoubleSpinBox(currentArea
);
147 dynamic_cast<QDoubleSpinBox
*>(temp
)->setMinimum(min
);
148 dynamic_cast<QDoubleSpinBox
*>(temp
)->setMaximum(max
);
149 dynamic_cast<QDoubleSpinBox
*>(temp
)->setValue(defVal
.toDouble());
150 dynamic_cast<QDoubleSpinBox
*>(temp
)->setSingleStep(0.1);
151 subLayout
->addWidget(temp
);
152 inputs
.insert(tag
, QPair
<InputType
, QWidget
*>(DSpin
, temp
));
154 else if(elements
[0].trimmed() == "combo")
156 elements
= elements
[1].trimmed().split(",");
159 temp
= new QComboBox(currentArea
);
160 for(int i
= 0; i
< elements
.count(); i
++)
162 QString current
= elements
[i
].trimmed();
163 if(i
== elements
.count() - 1)
165 dynamic_cast<QComboBox
*>(temp
)->addItem(current
, i
);
166 if(current
== defVal
)
169 dynamic_cast<QComboBox
*>(temp
)->setCurrentIndex(defIndex
);
170 subLayout
->addWidget(temp
);
171 inputs
.insert(tag
, QPair
<InputType
, QWidget
*>(Combo
, temp
));
177 DeviceState::~DeviceState()
181 QVariant
DeviceState::data(QString tag
)
183 QPair
<InputType
, QWidget
*> found
=
184 inputs
.value(tag
, QPair
<InputType
, QWidget
*>(Slide
, 0));
186 if(found
.second
== 0)
192 return dynamic_cast<QLineEdit
*>(found
.second
)->text();
195 return dynamic_cast<QSlider
*>(found
.second
)->value();
198 return dynamic_cast<QSpinBox
*>(found
.second
)->value();
201 return dynamic_cast<QDoubleSpinBox
*>(found
.second
)->value();
204 return dynamic_cast<QComboBox
*>(found
.second
)->currentIndex();
207 return dynamic_cast<QCheckBox
*>(found
.second
)->isChecked();