1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 <QSpacerItem>
23 #include <QPushButton>
24 #include <QHBoxLayout>
25 #include <QDoubleSpinBox>
28 #include <QFileDialog>
33 #include <QProgressDialog>
34 #include "encttscfggui.h"
36 EncTtsCfgGui::EncTtsCfgGui(QDialog
* parent
,EncTtsSettingInterface
* interface
,QString name
) : QDialog(parent
)
38 m_settingInterface
= interface
;
41 // create a busy Dialog
42 m_busyDlg
= new QProgressDialog("", "", 0, 0,this);
43 m_busyDlg
->setWindowTitle(tr("Waiting for engine..."));
44 m_busyDlg
->setModal(true);
45 m_busyDlg
->setLabel(0);
46 m_busyDlg
->setCancelButton(0);
48 connect(interface
,SIGNAL(busy()),this,SLOT(showBusy()));
49 connect(interface
,SIGNAL(busyEnd()),this,SLOT(hideBusy()));
56 void EncTtsCfgGui::setUpWindow()
58 m_settingsList
= m_settingInterface
->getSettings();
61 QVBoxLayout
*mainLayout
= new QVBoxLayout
;
64 QGroupBox
*groupBox
= new QGroupBox(this);
65 QGridLayout
*gridLayout
= new QGridLayout(groupBox
);
67 for(int i
= 0; i
< m_settingsList
.size(); i
++)
69 QLabel
*label
= new QLabel(m_settingsList
.at(i
)->name());
70 gridLayout
->addWidget(label
, i
, 0);
71 QWidget
*widget
= createWidgets(m_settingsList
.at(i
));
72 gridLayout
->addWidget(widget
, i
, 1);
73 widget
->setLayoutDirection(Qt::LeftToRight
);
74 QWidget
*btn
= createButton(m_settingsList
.at(i
));
77 gridLayout
->addWidget(btn
, i
, 2);
80 // add hidden spacers to make the dialog scale properly
81 QSpacerItem
* spacer
= new QSpacerItem(0, 0, QSizePolicy::Minimum
, QSizePolicy::Expanding
);
82 gridLayout
->addItem(spacer
, m_settingsList
.size(), 0);
83 spacer
= new QSpacerItem(0, 0, QSizePolicy::Expanding
, QSizePolicy::Minimum
);
84 gridLayout
->addItem(spacer
, m_settingsList
.size(), 1);
86 groupBox
->setLayout(gridLayout
);
87 mainLayout
->addWidget(groupBox
);
90 connect(&m_browseBtnMap
,SIGNAL(mapped(QObject
*)),this,SLOT(browse(QObject
*)));
92 // ok - cancel buttons
93 QPushButton
* okBtn
= new QPushButton(tr("Ok"),this);
94 okBtn
->setDefault(true);
95 okBtn
->setIcon(QIcon(":icons/go-next.png"));
96 QPushButton
* cancelBtn
= new QPushButton(tr("Cancel"),this);
97 cancelBtn
->setIcon(QIcon(":icons/process-stop.png"));
98 connect(okBtn
,SIGNAL(clicked()),this,SLOT(accept()));
99 connect(cancelBtn
,SIGNAL(clicked()),this,SLOT(reject()));
101 QHBoxLayout
*btnbox
= new QHBoxLayout
;
102 btnbox
->addWidget(okBtn
);
103 btnbox
->addWidget(cancelBtn
);
104 btnbox
->insertStretch(0,1);
106 mainLayout
->addLayout(btnbox
);
108 this->setLayout(mainLayout
);
111 QWidget
* EncTtsCfgGui::createWidgets(EncTtsSetting
* setting
)
114 QWidget
* value
= NULL
;
115 switch(setting
->type())
117 case EncTtsSetting::eDOUBLE
:
119 QDoubleSpinBox
*spinBox
= new QDoubleSpinBox(this);
120 spinBox
->setAccessibleName(setting
->name());
121 spinBox
->setMinimum(setting
->min().toDouble());
122 spinBox
->setMaximum(setting
->max().toDouble());
123 spinBox
->setSingleStep(0.01);
124 spinBox
->setValue(setting
->current().toDouble());
125 connect(spinBox
,SIGNAL(valueChanged(double)),this,SLOT(updateSetting()));
129 case EncTtsSetting::eINT
:
131 QSpinBox
*spinBox
= new QSpinBox(this);
132 spinBox
->setAccessibleName(setting
->name());
133 spinBox
->setMinimum(setting
->min().toInt());
134 spinBox
->setMaximum(setting
->max().toInt());
135 spinBox
->setValue(setting
->current().toInt());
136 connect(spinBox
,SIGNAL(valueChanged(int)),this,SLOT(updateSetting()));
140 case EncTtsSetting::eSTRING
:
142 QLineEdit
*lineEdit
= new QLineEdit(this);
143 lineEdit
->setAccessibleName(setting
->name());
144 lineEdit
->setText(setting
->current().toString());
145 connect(lineEdit
,SIGNAL(textChanged(QString
)),this,SLOT(updateSetting()));
149 case EncTtsSetting::eREADONLYSTRING
:
151 value
= new QLabel(setting
->current().toString(),this);
154 case EncTtsSetting::eSTRINGLIST
:
156 QComboBox
*comboBox
= new QComboBox(this);
157 comboBox
->setAccessibleName(setting
->name());
158 comboBox
->addItems(setting
->list());
159 int index
= comboBox
->findText(setting
->current().toString());
160 comboBox
->setCurrentIndex(index
);
161 connect(comboBox
,SIGNAL(currentIndexChanged(QString
)),this,SLOT(updateSetting()));
165 case EncTtsSetting::eBOOL
:
167 QCheckBox
*checkbox
= new QCheckBox(this);
168 checkbox
->setAccessibleName(setting
->name());
169 checkbox
->setCheckState(setting
->current().toBool() == true ? Qt::Checked
: Qt::Unchecked
);
170 connect(checkbox
,SIGNAL(stateChanged(int)),this,SLOT(updateSetting()));
176 qDebug() << "Warning: unknown EncTTsSetting type" << setting
->type();
184 m_settingsWidgetsMap
.insert(setting
,value
);
185 connect(setting
,SIGNAL(updateGui()),this,SLOT(updateWidget()));
191 QWidget
* EncTtsCfgGui::createButton(EncTtsSetting
* setting
)
193 if(setting
->button() == EncTtsSetting::eBROWSEBTN
)
195 QPushButton
* browsebtn
= new QPushButton(tr("Browse"),this);
196 browsebtn
->setIcon(QIcon(":/icons/system-search.png"));
197 m_browseBtnMap
.setMapping(browsebtn
,setting
);
198 connect(browsebtn
,SIGNAL(clicked()),&m_browseBtnMap
,SLOT(map()));
201 else if(setting
->button() == EncTtsSetting::eREFRESHBTN
)
203 QPushButton
* refreshbtn
= new QPushButton(tr("Refresh"),this);
204 refreshbtn
->setIcon(QIcon(":/icons/view-refresh.png"));
205 connect(refreshbtn
,SIGNAL(clicked()),setting
,SIGNAL(refresh()));
212 void EncTtsCfgGui::updateSetting()
214 //cast and get the sender widget
215 QWidget
* widget
= qobject_cast
<QWidget
*>(QObject::sender());
216 if(widget
== NULL
) return;
217 // get the corresponding setting
218 EncTtsSetting
* setting
= m_settingsWidgetsMap
.key(widget
);
220 // update widget based on setting type
221 switch(setting
->type())
223 case EncTtsSetting::eDOUBLE
:
225 setting
->setCurrent(((QDoubleSpinBox
*)widget
)->value(),false);
228 case EncTtsSetting::eINT
:
230 setting
->setCurrent(((QSpinBox
*)widget
)->value(),false);
233 case EncTtsSetting::eSTRING
:
235 setting
->setCurrent(((QLineEdit
*)widget
)->text(),false);
238 case EncTtsSetting::eREADONLYSTRING
:
240 setting
->setCurrent(((QLabel
*)widget
)->text(),false);
243 case EncTtsSetting::eSTRINGLIST
:
245 setting
->setCurrent(((QComboBox
*)widget
)->currentText(),false);
248 case EncTtsSetting::eBOOL
:
250 setting
->setCurrent(((QCheckBox
*)widget
)->isChecked(),false);
254 qDebug() << "unknown Settingtype !!";
260 void EncTtsCfgGui::updateWidget()
262 // get sender setting
263 EncTtsSetting
* setting
= qobject_cast
<EncTtsSetting
*>(QObject::sender());
264 if(setting
== NULL
) return;
265 // get corresponding widget
266 QWidget
* widget
= m_settingsWidgetsMap
.value(setting
);
268 // update Widget based on setting type
269 switch(setting
->type())
271 case EncTtsSetting::eDOUBLE
:
273 QDoubleSpinBox
* spinbox
= (QDoubleSpinBox
*) widget
;
274 spinbox
->setMinimum(setting
->min().toDouble());
275 spinbox
->setMaximum(setting
->max().toDouble());
276 spinbox
->blockSignals(true);
277 spinbox
->setValue(setting
->current().toDouble());
278 spinbox
->blockSignals(false);
281 case EncTtsSetting::eINT
:
283 QSpinBox
* spinbox
= (QSpinBox
*) widget
;
284 spinbox
->setMinimum(setting
->min().toInt());
285 spinbox
->setMaximum(setting
->max().toInt());
286 spinbox
->blockSignals(true);
287 spinbox
->setValue(setting
->current().toInt());
288 spinbox
->blockSignals(false);
291 case EncTtsSetting::eSTRING
:
293 QLineEdit
* lineedit
= (QLineEdit
*) widget
;
295 lineedit
->blockSignals(true);
296 lineedit
->setText(setting
->current().toString());
297 lineedit
->blockSignals(false);
300 case EncTtsSetting::eREADONLYSTRING
:
302 QLabel
* label
= (QLabel
*) widget
;
304 label
->blockSignals(true);
305 label
->setText(setting
->current().toString());
306 label
->blockSignals(false);
309 case EncTtsSetting::eSTRINGLIST
:
311 QComboBox
* combobox
= (QComboBox
*) widget
;
313 combobox
->blockSignals(true);
315 combobox
->addItems(setting
->list());
316 int index
= combobox
->findText(setting
->current().toString());
317 combobox
->setCurrentIndex(index
);
318 combobox
->blockSignals(false);
322 case EncTtsSetting::eBOOL
:
324 QCheckBox
* checkbox
= (QCheckBox
*) widget
;
326 checkbox
->blockSignals(true);
327 checkbox
->setCheckState(setting
->current().toBool() == true ? Qt::Checked
: Qt::Unchecked
);
328 checkbox
->blockSignals(false);
333 qDebug() << "unknown EncTTsSetting";
339 void EncTtsCfgGui::showBusy()
341 if(m_busyCnt
== 0) m_busyDlg
->show();
346 void EncTtsCfgGui::hideBusy()
350 if(m_busyCnt
== 0) m_busyDlg
->hide();
354 void EncTtsCfgGui::accept(void)
356 m_settingInterface
->saveSettings();
360 void EncTtsCfgGui::reject(void)
365 //! takes a QObject because of QsignalMapper
366 void EncTtsCfgGui::browse(QObject
* settingObj
)
369 EncTtsSetting
* setting
= qobject_cast
<EncTtsSetting
*>(settingObj
);
370 if(setting
== NULL
) return;
373 QString curPath
= setting
->current().toString();
375 QString exe
= QFileDialog::getOpenFileName(this, tr("Select executable"), curPath
, "*");
376 if(!QFileInfo(exe
).isExecutable())
378 // set new value, gui will update automatically
379 setting
->setCurrent(exe
);