rbutil: completely rework how tts and encoders are configured. (FS#10070)
[kugel-rb.git] / rbutil / rbutilqt / encttscfggui.cpp
blob25089873fc4ec6bc6a2938f536e02d7278dd4d84
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(tr(""), tr(""), 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 QFormLayout *formlayout = new QFormLayout;
55 // setting widgets
56 for(int i = 0; i < m_settingsList.size(); i++)
58 formlayout->addRow(m_settingsList.at(i)->name(),createWidgets(m_settingsList.at(i)));
60 groupBox->setLayout(formlayout);
61 mainLayout->addWidget(groupBox);
63 // connect browse btn
64 connect(&m_browseBtnMap,SIGNAL(mapped(QObject*)),this,SLOT(browse(QObject*)));
66 // ok - cancel buttons
67 QPushButton* okBtn = new QPushButton(tr("Ok"),this);
68 okBtn->setDefault(true);
69 okBtn->setIcon(QIcon(":icons/go-next.png"));
70 QPushButton* cancelBtn = new QPushButton(tr("Cancel"),this);
71 cancelBtn->setIcon(QIcon(":icons/process-stop.png"));
72 connect(okBtn,SIGNAL(clicked()),this,SLOT(accept()));
73 connect(cancelBtn,SIGNAL(clicked()),this,SLOT(reject()));
75 QHBoxLayout *btnbox = new QHBoxLayout;
76 btnbox->addWidget(okBtn);
77 btnbox->addWidget(cancelBtn);
78 btnbox->insertStretch(0,1);
80 mainLayout->addLayout(btnbox);
82 this->setLayout(mainLayout);
85 QLayout* EncTtsCfgGui::createWidgets(EncTtsSetting* setting)
87 // value display
88 QWidget* value = NULL;
89 switch(setting->type())
91 case EncTtsSetting::eDOUBLE:
93 QDoubleSpinBox *spinBox = new QDoubleSpinBox(this);
94 spinBox->setMinimum(setting->min().toDouble());
95 spinBox->setMaximum(setting->max().toDouble());
96 spinBox->setSingleStep(0.01);
97 spinBox->setValue(setting->current().toDouble());
98 connect(spinBox,SIGNAL(valueChanged(double)),this,SLOT(updateSetting()));
99 value = spinBox;
100 break;
102 case EncTtsSetting::eINT:
104 QSpinBox *spinBox = new QSpinBox(this);
105 spinBox->setMinimum(setting->min().toInt());
106 spinBox->setMaximum(setting->max().toInt());
107 spinBox->setValue(setting->current().toInt());
108 connect(spinBox,SIGNAL(valueChanged(int)),this,SLOT(updateSetting()));
109 value = spinBox;
110 break;
112 case EncTtsSetting::eSTRING:
114 QLineEdit *lineEdit = new QLineEdit(this);
115 lineEdit->setText(setting->current().toString());
116 connect(lineEdit,SIGNAL(textChanged(QString)),this,SLOT(updateSetting()));
117 value = lineEdit;
118 break;
120 case EncTtsSetting::eREADONLYSTRING:
122 value = new QLabel(setting->current().toString(),this);
123 break;
125 case EncTtsSetting::eSTRINGLIST:
127 QComboBox *comboBox = new QComboBox(this);
128 comboBox->addItems(setting->list());
129 int index = comboBox->findText(setting->current().toString());
130 comboBox->setCurrentIndex(index);
131 connect(comboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(updateSetting()));
132 value = comboBox;
133 break;
135 case EncTtsSetting::eBOOL:
137 QCheckBox *checkbox = new QCheckBox(this);
138 checkbox->setCheckState(setting->current().toBool() == true ? Qt::Checked : Qt::Unchecked);
139 connect(checkbox,SIGNAL(stateChanged(int)),this,SLOT(updateSetting()));
140 value = checkbox;
141 break;
143 default:
145 qDebug() << "Warning: unknown EncTTsSetting type" << setting->type();
146 break;
150 // remeber widget
151 if(value != NULL)
153 m_settingsWidgetsMap.insert(setting,value);
154 connect(setting,SIGNAL(updateGui()),this,SLOT(updateWidget()));
157 // buttons ?
158 QWidget* btn = createButton(setting);
160 // add to layout
161 QHBoxLayout *hbox = new QHBoxLayout;
162 if(value != NULL)hbox->addWidget(value);
163 if(btn != NULL) hbox->addWidget(btn);
165 return hbox;
169 QWidget* EncTtsCfgGui::createButton(EncTtsSetting* setting)
171 if(setting->button() == EncTtsSetting::eBROWSEBTN)
173 QPushButton* browsebtn = new QPushButton(tr("Browse"),this);
174 browsebtn->setFixedWidth(50); //all buttons the same size, or it looks ugly
175 m_browseBtnMap.setMapping(browsebtn,setting);
176 connect(browsebtn,SIGNAL(clicked()),&m_browseBtnMap,SLOT(map()));
177 return browsebtn;
179 else if(setting->button() == EncTtsSetting::eREFRESHBTN)
181 QPushButton* refreshbtn = new QPushButton(tr("Refresh"),this);
182 refreshbtn->setFixedWidth(50); //all buttons the same size, or it looks ugly
183 connect(refreshbtn,SIGNAL(clicked()),setting,SIGNAL(refresh()));
184 return refreshbtn;
186 else
187 return NULL;
190 void EncTtsCfgGui::updateSetting()
192 //cast and get the sender widget
193 QWidget* widget = qobject_cast<QWidget*>(QObject::sender());
194 if(widget == NULL) return;
195 // get the corresponding setting
196 EncTtsSetting* setting = m_settingsWidgetsMap.key(widget);
198 // update widget based on setting type
199 switch(setting->type())
201 case EncTtsSetting::eDOUBLE:
203 setting->setCurrent(((QDoubleSpinBox*)widget)->value(),false);
204 break;
206 case EncTtsSetting::eINT:
208 setting->setCurrent(((QSpinBox*)widget)->value(),false);
209 break;
211 case EncTtsSetting::eSTRING:
213 setting->setCurrent(((QLineEdit*)widget)->text(),false);
214 break;
216 case EncTtsSetting::eREADONLYSTRING:
218 setting->setCurrent(((QLabel*)widget)->text(),false);
219 break;
221 case EncTtsSetting::eSTRINGLIST:
223 setting->setCurrent(((QComboBox*)widget)->currentText(),false);
224 break;
226 case EncTtsSetting::eBOOL:
228 setting->setCurrent(((QCheckBox*)widget)->isChecked(),false);
230 default:
232 qDebug() << "unknown Settingtype !!";
233 break;
238 void EncTtsCfgGui::updateWidget()
240 // get sender setting
241 EncTtsSetting* setting = qobject_cast<EncTtsSetting*>(QObject::sender());
242 if(setting == NULL) return;
243 // get corresponding widget
244 QWidget* widget = m_settingsWidgetsMap.value(setting);
246 // update Widget based on setting type
247 switch(setting->type())
249 case EncTtsSetting::eDOUBLE:
251 QDoubleSpinBox* spinbox = (QDoubleSpinBox*) widget;
252 spinbox->setMinimum(setting->min().toDouble());
253 spinbox->setMaximum(setting->max().toDouble());
254 spinbox->blockSignals(true);
255 spinbox->setValue(setting->current().toDouble());
256 spinbox->blockSignals(false);
257 break;
259 case EncTtsSetting::eINT:
261 QSpinBox* spinbox = (QSpinBox*) widget;
262 spinbox->setMinimum(setting->min().toInt());
263 spinbox->setMaximum(setting->max().toInt());
264 spinbox->blockSignals(true);
265 spinbox->setValue(setting->current().toInt());
266 spinbox->blockSignals(false);
267 break;
269 case EncTtsSetting::eSTRING:
271 QLineEdit* lineedit = (QLineEdit*) widget;
273 lineedit->blockSignals(true);
274 lineedit->setText(setting->current().toString());
275 lineedit->blockSignals(false);
276 break;
278 case EncTtsSetting::eREADONLYSTRING:
280 QLabel* label = (QLabel*) widget;
282 label->blockSignals(true);
283 label->setText(setting->current().toString());
284 label->blockSignals(false);
285 break;
287 case EncTtsSetting::eSTRINGLIST:
289 QComboBox* combobox = (QComboBox*) widget;
291 combobox->blockSignals(true);
292 combobox->clear();
293 combobox->addItems(setting->list());
294 int index = combobox->findText(setting->current().toString());
295 combobox->setCurrentIndex(index);
296 combobox->blockSignals(false);
298 break;
300 case EncTtsSetting::eBOOL:
302 QCheckBox* checkbox = (QCheckBox*) widget;
304 checkbox->blockSignals(true);
305 checkbox->setCheckState(setting->current().toBool() == true ? Qt::Checked : Qt::Unchecked);
306 checkbox->blockSignals(false);
307 break;
309 default:
311 qDebug() << "unknown EncTTsSetting";
312 break;
317 void EncTtsCfgGui::showBusy()
319 if(m_busyCnt == 0) m_busyDlg->show();
321 m_busyCnt++;
324 void EncTtsCfgGui::hideBusy()
326 m_busyCnt--;
328 if(m_busyCnt == 0) m_busyDlg->hide();
332 void EncTtsCfgGui::accept(void)
334 m_settingInterface->saveSettings();
335 this->done(0);
338 void EncTtsCfgGui::reject(void)
340 this->done(0);
343 //! takes a QObject because of QsignalMapper
344 void EncTtsCfgGui::browse(QObject* settingObj)
346 // cast top setting
347 EncTtsSetting* setting= qobject_cast<EncTtsSetting*>(settingObj);
348 if(setting == NULL) return;
350 //current path
351 QString curPath = setting->current().toString();
352 // show file dialog
353 QString exe = QFileDialog::getOpenFileName(this, tr("Select excutable"), curPath, "*");
354 if(!QFileInfo(exe).isExecutable())
355 return;
356 // set new value, gui will update automatically
357 setting->setCurrent(exe);