Theme Editor: Added New Project feature
[kugel-rb.git] / rbutil / rbutilqt / encttscfggui.cpp
blobb063b658bfd946aee3db25b1b741cdeb616e5d7e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id: encoders.h 17902 2008-06-30 22:09:45Z bluebrother $
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 "encttscfggui.h"
23 #include "browsedirtree.h"
25 EncTtsCfgGui::EncTtsCfgGui(QDialog* parent,EncTtsSettingInterface* interface,QString name) : QDialog(parent)
27 m_settingInterface = interface;
29 m_busyCnt=0;
30 // create a busy Dialog
31 m_busyDlg= new QProgressDialog("", "", 0, 0,this);
32 m_busyDlg->setWindowTitle(tr("Waiting for engine..."));
33 m_busyDlg->setModal(true);
34 m_busyDlg->setLabel(0);
35 m_busyDlg->setCancelButton(0);
36 m_busyDlg->hide();
37 connect(interface,SIGNAL(busy()),this,SLOT(showBusy()));
38 connect(interface,SIGNAL(busyEnd()),this,SLOT(hideBusy()));
40 //setup the window
41 setWindowTitle(name);
42 setUpWindow();
45 void EncTtsCfgGui::setUpWindow()
47 m_settingsList = m_settingInterface->getSettings();
49 // layout
50 QVBoxLayout *mainLayout = new QVBoxLayout;
52 // groupbox
53 QGroupBox *groupBox = new QGroupBox(this);
54 QGridLayout *gridLayout = new QGridLayout(groupBox);
55 // setting widgets
56 for(int i = 0; i < m_settingsList.size(); i++)
58 QLabel *label = new QLabel(m_settingsList.at(i)->name());
59 gridLayout->addWidget(label, i, 0);
60 QWidget *widget = createWidgets(m_settingsList.at(i));
61 gridLayout->addWidget(widget, i, 1);
62 widget->setLayoutDirection(Qt::LeftToRight);
63 QWidget *btn = createButton(m_settingsList.at(i));
64 if(btn != NULL)
66 gridLayout->addWidget(btn, i, 2);
69 // add hidden spacers to make the dialog scale properly
70 QSpacerItem* spacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
71 gridLayout->addItem(spacer, m_settingsList.size(), 0);
72 spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
73 gridLayout->addItem(spacer, m_settingsList.size(), 1);
75 groupBox->setLayout(gridLayout);
76 mainLayout->addWidget(groupBox);
78 // connect browse btn
79 connect(&m_browseBtnMap,SIGNAL(mapped(QObject*)),this,SLOT(browse(QObject*)));
81 // ok - cancel buttons
82 QPushButton* okBtn = new QPushButton(tr("Ok"),this);
83 okBtn->setDefault(true);
84 okBtn->setIcon(QIcon(":icons/go-next.png"));
85 QPushButton* cancelBtn = new QPushButton(tr("Cancel"),this);
86 cancelBtn->setIcon(QIcon(":icons/process-stop.png"));
87 connect(okBtn,SIGNAL(clicked()),this,SLOT(accept()));
88 connect(cancelBtn,SIGNAL(clicked()),this,SLOT(reject()));
90 QHBoxLayout *btnbox = new QHBoxLayout;
91 btnbox->addWidget(okBtn);
92 btnbox->addWidget(cancelBtn);
93 btnbox->insertStretch(0,1);
95 mainLayout->addLayout(btnbox);
97 this->setLayout(mainLayout);
100 QWidget* EncTtsCfgGui::createWidgets(EncTtsSetting* setting)
102 // value display
103 QWidget* value = NULL;
104 switch(setting->type())
106 case EncTtsSetting::eDOUBLE:
108 QDoubleSpinBox *spinBox = new QDoubleSpinBox(this);
109 spinBox->setAccessibleName(setting->name());
110 spinBox->setMinimum(setting->min().toDouble());
111 spinBox->setMaximum(setting->max().toDouble());
112 spinBox->setSingleStep(0.01);
113 spinBox->setValue(setting->current().toDouble());
114 connect(spinBox,SIGNAL(valueChanged(double)),this,SLOT(updateSetting()));
115 value = spinBox;
116 break;
118 case EncTtsSetting::eINT:
120 QSpinBox *spinBox = new QSpinBox(this);
121 spinBox->setAccessibleName(setting->name());
122 spinBox->setMinimum(setting->min().toInt());
123 spinBox->setMaximum(setting->max().toInt());
124 spinBox->setValue(setting->current().toInt());
125 connect(spinBox,SIGNAL(valueChanged(int)),this,SLOT(updateSetting()));
126 value = spinBox;
127 break;
129 case EncTtsSetting::eSTRING:
131 QLineEdit *lineEdit = new QLineEdit(this);
132 lineEdit->setAccessibleName(setting->name());
133 lineEdit->setText(setting->current().toString());
134 connect(lineEdit,SIGNAL(textChanged(QString)),this,SLOT(updateSetting()));
135 value = lineEdit;
136 break;
138 case EncTtsSetting::eREADONLYSTRING:
140 value = new QLabel(setting->current().toString(),this);
141 break;
143 case EncTtsSetting::eSTRINGLIST:
145 QComboBox *comboBox = new QComboBox(this);
146 comboBox->setAccessibleName(setting->name());
147 comboBox->addItems(setting->list());
148 int index = comboBox->findText(setting->current().toString());
149 comboBox->setCurrentIndex(index);
150 connect(comboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(updateSetting()));
151 value = comboBox;
152 break;
154 case EncTtsSetting::eBOOL:
156 QCheckBox *checkbox = new QCheckBox(this);
157 checkbox->setAccessibleName(setting->name());
158 checkbox->setCheckState(setting->current().toBool() == true ? Qt::Checked : Qt::Unchecked);
159 connect(checkbox,SIGNAL(stateChanged(int)),this,SLOT(updateSetting()));
160 value = checkbox;
161 break;
163 default:
165 qDebug() << "Warning: unknown EncTTsSetting type" << setting->type();
166 break;
170 // remember widget
171 if(value != NULL)
173 m_settingsWidgetsMap.insert(setting,value);
174 connect(setting,SIGNAL(updateGui()),this,SLOT(updateWidget()));
177 return value;
180 QWidget* EncTtsCfgGui::createButton(EncTtsSetting* setting)
182 if(setting->button() == EncTtsSetting::eBROWSEBTN)
184 QPushButton* browsebtn = new QPushButton(tr("Browse"),this);
185 browsebtn->setIcon(QIcon(":/icons/system-search.png"));
186 m_browseBtnMap.setMapping(browsebtn,setting);
187 connect(browsebtn,SIGNAL(clicked()),&m_browseBtnMap,SLOT(map()));
188 return browsebtn;
190 else if(setting->button() == EncTtsSetting::eREFRESHBTN)
192 QPushButton* refreshbtn = new QPushButton(tr("Refresh"),this);
193 refreshbtn->setIcon(QIcon(":/icons/view-refresh.png"));
194 connect(refreshbtn,SIGNAL(clicked()),setting,SIGNAL(refresh()));
195 return refreshbtn;
197 else
198 return NULL;
201 void EncTtsCfgGui::updateSetting()
203 //cast and get the sender widget
204 QWidget* widget = qobject_cast<QWidget*>(QObject::sender());
205 if(widget == NULL) return;
206 // get the corresponding setting
207 EncTtsSetting* setting = m_settingsWidgetsMap.key(widget);
209 // update widget based on setting type
210 switch(setting->type())
212 case EncTtsSetting::eDOUBLE:
214 setting->setCurrent(((QDoubleSpinBox*)widget)->value(),false);
215 break;
217 case EncTtsSetting::eINT:
219 setting->setCurrent(((QSpinBox*)widget)->value(),false);
220 break;
222 case EncTtsSetting::eSTRING:
224 setting->setCurrent(((QLineEdit*)widget)->text(),false);
225 break;
227 case EncTtsSetting::eREADONLYSTRING:
229 setting->setCurrent(((QLabel*)widget)->text(),false);
230 break;
232 case EncTtsSetting::eSTRINGLIST:
234 setting->setCurrent(((QComboBox*)widget)->currentText(),false);
235 break;
237 case EncTtsSetting::eBOOL:
239 setting->setCurrent(((QCheckBox*)widget)->isChecked(),false);
241 default:
243 qDebug() << "unknown Settingtype !!";
244 break;
249 void EncTtsCfgGui::updateWidget()
251 // get sender setting
252 EncTtsSetting* setting = qobject_cast<EncTtsSetting*>(QObject::sender());
253 if(setting == NULL) return;
254 // get corresponding widget
255 QWidget* widget = m_settingsWidgetsMap.value(setting);
257 // update Widget based on setting type
258 switch(setting->type())
260 case EncTtsSetting::eDOUBLE:
262 QDoubleSpinBox* spinbox = (QDoubleSpinBox*) widget;
263 spinbox->setMinimum(setting->min().toDouble());
264 spinbox->setMaximum(setting->max().toDouble());
265 spinbox->blockSignals(true);
266 spinbox->setValue(setting->current().toDouble());
267 spinbox->blockSignals(false);
268 break;
270 case EncTtsSetting::eINT:
272 QSpinBox* spinbox = (QSpinBox*) widget;
273 spinbox->setMinimum(setting->min().toInt());
274 spinbox->setMaximum(setting->max().toInt());
275 spinbox->blockSignals(true);
276 spinbox->setValue(setting->current().toInt());
277 spinbox->blockSignals(false);
278 break;
280 case EncTtsSetting::eSTRING:
282 QLineEdit* lineedit = (QLineEdit*) widget;
284 lineedit->blockSignals(true);
285 lineedit->setText(setting->current().toString());
286 lineedit->blockSignals(false);
287 break;
289 case EncTtsSetting::eREADONLYSTRING:
291 QLabel* label = (QLabel*) widget;
293 label->blockSignals(true);
294 label->setText(setting->current().toString());
295 label->blockSignals(false);
296 break;
298 case EncTtsSetting::eSTRINGLIST:
300 QComboBox* combobox = (QComboBox*) widget;
302 combobox->blockSignals(true);
303 combobox->clear();
304 combobox->addItems(setting->list());
305 int index = combobox->findText(setting->current().toString());
306 combobox->setCurrentIndex(index);
307 combobox->blockSignals(false);
309 break;
311 case EncTtsSetting::eBOOL:
313 QCheckBox* checkbox = (QCheckBox*) widget;
315 checkbox->blockSignals(true);
316 checkbox->setCheckState(setting->current().toBool() == true ? Qt::Checked : Qt::Unchecked);
317 checkbox->blockSignals(false);
318 break;
320 default:
322 qDebug() << "unknown EncTTsSetting";
323 break;
328 void EncTtsCfgGui::showBusy()
330 if(m_busyCnt == 0) m_busyDlg->show();
332 m_busyCnt++;
335 void EncTtsCfgGui::hideBusy()
337 m_busyCnt--;
339 if(m_busyCnt == 0) m_busyDlg->hide();
343 void EncTtsCfgGui::accept(void)
345 m_settingInterface->saveSettings();
346 this->done(0);
349 void EncTtsCfgGui::reject(void)
351 this->done(0);
354 //! takes a QObject because of QsignalMapper
355 void EncTtsCfgGui::browse(QObject* settingObj)
357 // cast top setting
358 EncTtsSetting* setting= qobject_cast<EncTtsSetting*>(settingObj);
359 if(setting == NULL) return;
361 //current path
362 QString curPath = setting->current().toString();
363 // show file dialog
364 QString exe = QFileDialog::getOpenFileName(this, tr("Select excutable"), curPath, "*");
365 if(!QFileInfo(exe).isExecutable())
366 return;
367 // set new value, gui will update automatically
368 setting->setCurrent(exe);