Theme Editor: Added settingsChanged() signal to DeviceState class
[kugel-rb.git] / utils / themeeditor / gui / devicestate.cpp
blobb17dcafa5745ffb15b522e0168ed86af25f21626
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"
23 #include "ui_devicestate.h"
25 #include <QScrollArea>
26 #include <QFile>
27 #include <QCheckBox>
28 #include <QSpinBox>
29 #include <QComboBox>
31 DeviceState::DeviceState(QWidget *parent) :
32 QWidget(parent), tabs(this)
34 /* UI stuff */
35 resize(500,400);
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;
46 QWidget* panel;
48 QFile fin(":/resources/deviceoptions");
49 fin.open(QFile::Text | QFile::ReadOnly);
50 while(!fin.atEnd())
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)
57 continue;
59 if(line[0] == '[')
61 QString buffer;
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);
75 continue;
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();
85 if(type != "check")
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)),
98 this, SLOT(input()));
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);
106 else
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();
118 maxS.chop(1);
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();
136 maxS.chop(1);
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();
154 maxS.chop(1);
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(",");
172 int defIndex;
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)
178 current.chop(1);
179 temp->addItem(current, i);
180 if(current == defVal)
181 defIndex = i;
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)
204 return QVariant();
206 switch(found.first)
208 case Text:
209 return dynamic_cast<QLineEdit*>(found.second)->text();
211 case Slide:
212 return dynamic_cast<QSlider*>(found.second)->value();
214 case Spin:
215 return dynamic_cast<QSpinBox*>(found.second)->value();
217 case DSpin:
218 return dynamic_cast<QDoubleSpinBox*>(found.second)->value();
220 case Combo:
221 return dynamic_cast<QComboBox*>(found.second)->currentIndex();
223 case Check:
224 return dynamic_cast<QCheckBox*>(found.second)->isChecked();
227 return QVariant();
230 void DeviceState::input()
232 emit settingsChanged();