Theme Editor: Fixed some compiler warnings and a segfault. Got some basic text rende...
[kugel-rb.git] / utils / themeeditor / gui / devicestate.cpp
blob3933926a482e0f82ac4cc691b4834c594d95fa6e
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>
34 DeviceState::DeviceState(QWidget *parent) :
35 QWidget(parent), tabs(this)
37 /* UI stuff */
38 resize(500,400);
39 setWindowIcon(QIcon(":/resources/windowicon.png"));
40 setWindowTitle(tr("Device Settings"));
42 QVBoxLayout* layout = new QVBoxLayout(this);
43 layout->addWidget(&tabs);
44 this->setLayout(layout);
46 /* Loading the tabs */
47 QScrollArea* currentArea = 0;
48 QHBoxLayout* subLayout;
49 QWidget* panel;
51 QFile fin(":/resources/deviceoptions");
52 fin.open(QFile::Text | QFile::ReadOnly);
53 while(!fin.atEnd())
55 QString line = QString(fin.readLine());
56 line = line.trimmed();
58 /* Continue on a comment or an empty line */
59 if(line[0] == '#' || line.length() == 0)
60 continue;
62 if(line[0] == '[')
64 QString buffer;
65 for(int i = 1; line[i] != ']'; i++)
66 buffer.append(line[i]);
67 buffer = buffer.trimmed();
69 panel = new QWidget();
70 currentArea = new QScrollArea();
71 layout = new QVBoxLayout(panel);
72 currentArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
73 currentArea->setWidget(panel);
74 currentArea->setWidgetResizable(true);
76 tabs.addTab(currentArea, buffer);
78 continue;
81 QStringList elements = line.split(";");
82 QString tag = elements[0].trimmed();
83 QString title = elements[1].trimmed();
84 QString type = elements[2].trimmed();
85 QString defVal = elements[3].trimmed();
87 subLayout = new QHBoxLayout();
88 if(type != "check")
89 subLayout->addWidget(new QLabel(elements[1].trimmed(),
90 currentArea));
91 layout->addLayout(subLayout);
94 elements = type.split("(");
95 if(elements[0].trimmed() == "text")
97 QLineEdit* temp = new QLineEdit(defVal, currentArea);
98 subLayout->addWidget(temp);
99 inputs.insert(tag, QPair<InputType, QWidget*>(Text, temp));
101 temp->setSizePolicy(QSizePolicy(QSizePolicy::Preferred,
102 QSizePolicy::Fixed));
104 QObject::connect(temp, SIGNAL(textChanged(QString)),
105 this, SLOT(input()));
107 else if(elements[0].trimmed() == "check")
109 QCheckBox* temp = new QCheckBox(title, currentArea);
110 subLayout->addWidget(temp);
111 if(defVal.toLower() == "true")
112 temp->setChecked(true);
113 else
114 temp->setChecked(false);
115 inputs.insert(tag, QPair<InputType, QWidget*>(Check, temp));
117 QObject::connect(temp, SIGNAL(toggled(bool)),
118 this, SLOT(input()));
120 else if(elements[0].trimmed() == "slider")
122 elements = elements[1].trimmed().split(",");
123 int min = elements[0].trimmed().toInt();
124 QString maxS = elements[1].trimmed();
125 maxS.chop(1);
126 int max = maxS.toInt();
128 QSlider* temp = new QSlider(Qt::Horizontal, currentArea);
129 temp->setMinimum(min);
130 temp->setMaximum(max);
131 temp->setValue(defVal.toInt());
132 subLayout->addWidget(temp);
133 inputs.insert(tag, QPair<InputType, QWidget*>(Slide, temp));
135 QObject::connect(temp, SIGNAL(valueChanged(int)),
136 this, SLOT(input()));
138 else if(elements[0].trimmed() == "spin")
140 elements = elements[1].trimmed().split(",");
141 int min = elements[0].trimmed().toInt();
142 QString maxS = elements[1].trimmed();
143 maxS.chop(1);
144 int max = maxS.toInt();
146 QSpinBox* temp = new QSpinBox(currentArea);
147 temp->setMinimum(min);
148 temp->setMaximum(max);
149 temp->setValue(defVal.toInt());
150 subLayout->addWidget(temp);
151 inputs.insert(tag, QPair<InputType, QWidget*>(Spin, temp));
153 QObject::connect(temp, SIGNAL(valueChanged(int)),
154 this, SLOT(input()));
156 else if(elements[0].trimmed() == "fspin")
158 elements = elements[1].trimmed().split(",");
159 int min = elements[0].trimmed().toDouble();
160 QString maxS = elements[1].trimmed();
161 maxS.chop(1);
162 int max = maxS.toDouble();
164 QDoubleSpinBox* temp = new QDoubleSpinBox(currentArea);
165 temp->setMinimum(min);
166 temp->setMaximum(max);
167 temp->setValue(defVal.toDouble());
168 temp->setSingleStep(0.1);
169 subLayout->addWidget(temp);
170 inputs.insert(tag, QPair<InputType, QWidget*>(DSpin, temp));
172 QObject::connect(temp, SIGNAL(valueChanged(double)),
173 this, SLOT(input()));
175 else if(elements[0].trimmed() == "combo")
177 elements = elements[1].trimmed().split(",");
179 int defIndex = 0;
180 QComboBox* temp = new QComboBox(currentArea);
181 for(int i = 0; i < elements.count(); i++)
183 QString current = elements[i].trimmed();
184 if(i == elements.count() - 1)
185 current.chop(1);
186 temp->addItem(current, i);
187 if(current == defVal)
188 defIndex = i;
190 temp->setCurrentIndex(defIndex);
191 subLayout->addWidget(temp);
192 inputs.insert(tag, QPair<InputType, QWidget*>(Combo, temp));
194 QObject::connect(temp, SIGNAL(currentIndexChanged(int)),
195 this, SLOT(input()));
201 DeviceState::~DeviceState()
205 QVariant DeviceState::data(QString tag)
207 QPair<InputType, QWidget*> found =
208 inputs.value(tag, QPair<InputType, QWidget*>(Slide, 0));
210 if(found.second == 0)
211 return QVariant();
213 switch(found.first)
215 case Text:
216 return dynamic_cast<QLineEdit*>(found.second)->text();
218 case Slide:
219 return dynamic_cast<QSlider*>(found.second)->value();
221 case Spin:
222 return dynamic_cast<QSpinBox*>(found.second)->value();
224 case DSpin:
225 return dynamic_cast<QDoubleSpinBox*>(found.second)->value();
227 case Combo:
228 return dynamic_cast<QComboBox*>(found.second)->currentIndex();
230 case Check:
231 return dynamic_cast<QCheckBox*>(found.second)->isChecked();
234 return QVariant();
237 void DeviceState::input()
239 emit settingsChanged();