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
;
48 QFile
fin(":/resources/deviceoptions");
49 fin
.open(QFile::Text
| QFile::ReadOnly
);
52 QString line
= QString(fin
.readLine());
53 line
= line
.trimmed();
55 /* Continue on a comment or an empty line */
56 if(line
[0] == '#' || line
.length() == 0)
62 for(int i
= 1; line
[i
] != ']'; i
++)
63 buffer
.append(line
[i
]);
64 buffer
= buffer
.trimmed();
66 panel
= new QWidget();
67 currentArea
= new QScrollArea();
68 layout
= new QVBoxLayout(panel
);
69 currentArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
70 currentArea
->setWidget(panel
);
71 currentArea
->setWidgetResizable(true);
73 tabs
.addTab(currentArea
, buffer
);
78 QStringList elements
= line
.split(";");
79 QString tag
= elements
[0].trimmed();
80 QString title
= elements
[1].trimmed();
81 QString type
= elements
[2].trimmed();
82 QString defVal
= elements
[3].trimmed();
84 subLayout
= new QHBoxLayout();
86 subLayout
->addWidget(new QLabel(elements
[1].trimmed(), currentArea
));
87 layout
->addLayout(subLayout
);
90 elements
= type
.split("(");
91 if(elements
[0].trimmed() == "text")
93 QLineEdit
* temp
= new QLineEdit(defVal
, currentArea
);
94 subLayout
->addWidget(temp
);
95 inputs
.insert(tag
, QPair
<InputType
, QWidget
*>(Text
, temp
));
97 QObject::connect(temp
, SIGNAL(textChanged(QString
)),
100 else if(elements
[0].trimmed() == "check")
102 QCheckBox
* temp
= new QCheckBox(title
, currentArea
);
103 subLayout
->addWidget(temp
);
104 if(defVal
.toLower() == "true")
105 temp
->setChecked(true);
107 temp
->setChecked(false);
108 inputs
.insert(tag
, QPair
<InputType
, QWidget
*>(Check
, temp
));
110 QObject::connect(temp
, SIGNAL(toggled(bool)),
111 this, SLOT(input()));
113 else if(elements
[0].trimmed() == "slider")
115 elements
= elements
[1].trimmed().split(",");
116 int min
= elements
[0].trimmed().toInt();
117 QString maxS
= elements
[1].trimmed();
119 int max
= maxS
.toInt();
121 QSlider
* temp
= new QSlider(Qt::Horizontal
, currentArea
);
122 temp
->setMinimum(min
);
123 temp
->setMaximum(max
);
124 temp
->setValue(defVal
.toInt());
125 subLayout
->addWidget(temp
);
126 inputs
.insert(tag
, QPair
<InputType
, QWidget
*>(Slide
, temp
));
128 QObject::connect(temp
, SIGNAL(valueChanged(int)),
129 this, SLOT(input()));
131 else if(elements
[0].trimmed() == "spin")
133 elements
= elements
[1].trimmed().split(",");
134 int min
= elements
[0].trimmed().toInt();
135 QString maxS
= elements
[1].trimmed();
137 int max
= maxS
.toInt();
139 QSpinBox
* temp
= new QSpinBox(currentArea
);
140 temp
->setMinimum(min
);
141 temp
->setMaximum(max
);
142 temp
->setValue(defVal
.toInt());
143 subLayout
->addWidget(temp
);
144 inputs
.insert(tag
, QPair
<InputType
, QWidget
*>(Spin
, temp
));
146 QObject::connect(temp
, SIGNAL(valueChanged(int)),
147 this, SLOT(input()));
149 else if(elements
[0].trimmed() == "fspin")
151 elements
= elements
[1].trimmed().split(",");
152 int min
= elements
[0].trimmed().toDouble();
153 QString maxS
= elements
[1].trimmed();
155 int max
= maxS
.toDouble();
157 QDoubleSpinBox
* temp
= new QDoubleSpinBox(currentArea
);
158 temp
->setMinimum(min
);
159 temp
->setMaximum(max
);
160 temp
->setValue(defVal
.toDouble());
161 temp
->setSingleStep(0.1);
162 subLayout
->addWidget(temp
);
163 inputs
.insert(tag
, QPair
<InputType
, QWidget
*>(DSpin
, temp
));
165 QObject::connect(temp
, SIGNAL(valueChanged(double)),
166 this, SLOT(input()));
168 else if(elements
[0].trimmed() == "combo")
170 elements
= elements
[1].trimmed().split(",");
173 QComboBox
* temp
= new QComboBox(currentArea
);
174 for(int i
= 0; i
< elements
.count(); i
++)
176 QString current
= elements
[i
].trimmed();
177 if(i
== elements
.count() - 1)
179 temp
->addItem(current
, i
);
180 if(current
== defVal
)
183 temp
->setCurrentIndex(defIndex
);
184 subLayout
->addWidget(temp
);
185 inputs
.insert(tag
, QPair
<InputType
, QWidget
*>(Combo
, temp
));
187 QObject::connect(temp
, SIGNAL(currentIndexChanged(int)),
188 this, SLOT(input()));
194 DeviceState::~DeviceState()
198 QVariant
DeviceState::data(QString tag
)
200 QPair
<InputType
, QWidget
*> found
=
201 inputs
.value(tag
, QPair
<InputType
, QWidget
*>(Slide
, 0));
203 if(found
.second
== 0)
209 return dynamic_cast<QLineEdit
*>(found
.second
)->text();
212 return dynamic_cast<QSlider
*>(found
.second
)->value();
215 return dynamic_cast<QSpinBox
*>(found
.second
)->value();
218 return dynamic_cast<QDoubleSpinBox
*>(found
.second
)->value();
221 return dynamic_cast<QComboBox
*>(found
.second
)->currentIndex();
224 return dynamic_cast<QCheckBox
*>(found
.second
)->isChecked();
230 void DeviceState::input()
232 emit
settingsChanged();