summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/encttscfggui.cpp
blob: 57e58961482887e63bdb0e515b756a267736732c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 *   Copyright (C) 2007 by Dominik Wenger
 *   $Id: encoders.h 17902 2008-06-30 22:09:45Z bluebrother $
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
 
#include "encttscfggui.h"
#include "browsedirtree.h"

EncTtsCfgGui::EncTtsCfgGui(QDialog* parent,EncTtsSettingInterface* interface,QString name) : QDialog(parent)
{
    m_settingInterface = interface;
    
    m_busyCnt=0; 
    // create a busy Dialog
    m_busyDlg= new QProgressDialog("", "", 0, 0,this);
    m_busyDlg->setWindowTitle(tr("Waiting for engine..."));
    m_busyDlg->setModal(true);
    m_busyDlg->setLabel(0);
    m_busyDlg->setCancelButton(0);
    m_busyDlg->hide();
    connect(interface,SIGNAL(busy()),this,SLOT(showBusy()));
    connect(interface,SIGNAL(busyEnd()),this,SLOT(hideBusy()));

    //setup the window
    setWindowTitle(name);
    setUpWindow();
}

void EncTtsCfgGui::setUpWindow()
{
    m_settingsList = m_settingInterface->getSettings();
    
    //layout
    QVBoxLayout *mainLayout = new QVBoxLayout;
    
      // groupbox
    QGroupBox *groupBox = new QGroupBox(this);
    QFormLayout *formlayout = new QFormLayout;
    // setting widgets
    for(int i = 0; i < m_settingsList.size(); i++)
    {
        formlayout->addRow(m_settingsList.at(i)->name(),createWidgets(m_settingsList.at(i)));
    }
    groupBox->setLayout(formlayout);
    mainLayout->addWidget(groupBox);
    
    // connect browse btn
    connect(&m_browseBtnMap,SIGNAL(mapped(QObject*)),this,SLOT(browse(QObject*)));
    
    // ok - cancel buttons
    QPushButton* okBtn = new QPushButton(tr("Ok"),this);
    okBtn->setDefault(true);
    okBtn->setIcon(QIcon(":icons/go-next.png"));
    QPushButton* cancelBtn = new QPushButton(tr("Cancel"),this);
    cancelBtn->setIcon(QIcon(":icons/process-stop.png"));
    connect(okBtn,SIGNAL(clicked()),this,SLOT(accept()));
    connect(cancelBtn,SIGNAL(clicked()),this,SLOT(reject()));
    
    QHBoxLayout *btnbox = new QHBoxLayout;
    btnbox->addWidget(okBtn);
    btnbox->addWidget(cancelBtn);
    btnbox->insertStretch(0,1);
    
    mainLayout->addLayout(btnbox);
    
    this->setLayout(mainLayout);
}

QLayout* EncTtsCfgGui::createWidgets(EncTtsSetting* setting)
{
    // value display
    QWidget* value = NULL;
    switch(setting->type())
    {
        case EncTtsSetting::eDOUBLE:
        {
            QDoubleSpinBox *spinBox = new QDoubleSpinBox(this);
            spinBox->setAccessibleName(setting->name());
            spinBox->setMinimum(setting->min().toDouble());
            spinBox->setMaximum(setting->max().toDouble());
            spinBox->setSingleStep(0.01);
            spinBox->setValue(setting->current().toDouble());
            connect(spinBox,SIGNAL(valueChanged(double)),this,SLOT(updateSetting()));
            value = spinBox;
            break;
        }
        case EncTtsSetting::eINT:
        {
            QSpinBox *spinBox = new QSpinBox(this);
            spinBox->setAccessibleName(setting->name());
            spinBox->setMinimum(setting->min().toInt());
            spinBox->setMaximum(setting->max().toInt());
            spinBox->setValue(setting->current().toInt());
            connect(spinBox,SIGNAL(valueChanged(int)),this,SLOT(updateSetting()));
            value = spinBox;
            break;
        }
        case EncTtsSetting::eSTRING:
        {
            QLineEdit *lineEdit = new QLineEdit(this);
            lineEdit->setAccessibleName(setting->name());
            lineEdit->setText(setting->current().toString());
            connect(lineEdit,SIGNAL(textChanged(QString)),this,SLOT(updateSetting()));
            value = lineEdit;
            break;
        }
        case EncTtsSetting::eREADONLYSTRING:
        {
            value = new QLabel(setting->current().toString(),this);
            break;
        }
        case EncTtsSetting::eSTRINGLIST:
        {
            QComboBox *comboBox = new QComboBox(this);
            comboBox->setAccessibleName(setting->name());
            comboBox->addItems(setting->list());
            int index = comboBox->findText(setting->current().toString());
            comboBox->setCurrentIndex(index);
            connect(comboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(updateSetting()));
            value = comboBox;
            break;
        }
        case EncTtsSetting::eBOOL:
        {
            QCheckBox *checkbox = new QCheckBox(this);
            checkbox->setAccessibleName(setting->name());
            checkbox->setCheckState(setting->current().toBool() == true ? Qt::Checked : Qt::Unchecked);
            connect(checkbox,SIGNAL(stateChanged(int)),this,SLOT(updateSetting()));
            value = checkbox;
            break;
        }
        default:
        {
            qDebug() << "Warning: unknown EncTTsSetting type" << setting->type();
            break;
        }
    }
    
    // remeber widget
    if(value != NULL)
    {    
        m_settingsWidgetsMap.insert(setting,value);
        connect(setting,SIGNAL(updateGui()),this,SLOT(updateWidget()));
    }
    
     // buttons ?
    QWidget* btn = createButton(setting);
    
    // add to layout
    QHBoxLayout *hbox = new QHBoxLayout;
    if(value != NULL)hbox->addWidget(value); 
    if(btn != NULL) hbox->addWidget(btn);  
  
    return hbox;
    
}

QWidget* EncTtsCfgGui::createButton(EncTtsSetting* setting)
{
    if(setting->button() == EncTtsSetting::eBROWSEBTN)
    {
        QPushButton* browsebtn = new QPushButton(tr("Browse"),this);
        browsebtn->setFixedWidth(50);  //all buttons the same size, or it looks ugly
        m_browseBtnMap.setMapping(browsebtn,setting);
        connect(browsebtn,SIGNAL(clicked()),&m_browseBtnMap,SLOT(map()));
        return browsebtn;
    }
    else if(setting->button() == EncTtsSetting::eREFRESHBTN)
    {
        QPushButton* refreshbtn = new QPushButton(tr("Refresh"),this);
        refreshbtn->setFixedWidth(50); //all buttons the same size, or it looks ugly
        connect(refreshbtn,SIGNAL(clicked()),setting,SIGNAL(refresh()));
        return refreshbtn;
    }
    else
        return NULL;
}

void EncTtsCfgGui::updateSetting()
{
    //cast and get the sender widget
    QWidget* widget = qobject_cast<QWidget*>(QObject::sender());
    if(widget == NULL) return;
    // get the corresponding setting
    EncTtsSetting* setting = m_settingsWidgetsMap.key(widget);
    
    // update widget based on setting type 
    switch(setting->type())
    {
        case EncTtsSetting::eDOUBLE:
        {
            setting->setCurrent(((QDoubleSpinBox*)widget)->value(),false);
            break;
        }
        case EncTtsSetting::eINT:
        {
            setting->setCurrent(((QSpinBox*)widget)->value(),false);
            break;
        }
        case EncTtsSetting::eSTRING:
        {
            setting->setCurrent(((QLineEdit*)widget)->text(),false);
            break;
        }
        case EncTtsSetting::eREADONLYSTRING:
        {
             setting->setCurrent(((QLabel*)widget)->text(),false);
            break;
        }
        case EncTtsSetting::eSTRINGLIST:
        {
            setting->setCurrent(((QComboBox*)widget)->currentText(),false);
            break;
        }
        case EncTtsSetting::eBOOL:
        {
            setting->setCurrent(((QCheckBox*)widget)->isChecked(),false);
        }
        default:
        {
            qDebug() << "unknown Settingtype !!";
            break;
        }  
    }
}

void EncTtsCfgGui::updateWidget()
{
    // get sender setting
    EncTtsSetting* setting = qobject_cast<EncTtsSetting*>(QObject::sender());
    if(setting == NULL) return;
    // get corresponding widget
    QWidget* widget = m_settingsWidgetsMap.value(setting);
    
    // update Widget based on setting type
    switch(setting->type())
    {
        case EncTtsSetting::eDOUBLE:
        {
            QDoubleSpinBox* spinbox = (QDoubleSpinBox*) widget;
            spinbox->setMinimum(setting->min().toDouble());
            spinbox->setMaximum(setting->max().toDouble());
            spinbox->blockSignals(true);
            spinbox->setValue(setting->current().toDouble());
            spinbox->blockSignals(false);
            break;
        }
        case EncTtsSetting::eINT:
        {
            QSpinBox* spinbox = (QSpinBox*) widget;
            spinbox->setMinimum(setting->min().toInt());
            spinbox->setMaximum(setting->max().toInt());
            spinbox->blockSignals(true);
            spinbox->setValue(setting->current().toInt());
            spinbox->blockSignals(false);
            break;
        }
        case EncTtsSetting::eSTRING:
        {
            QLineEdit* lineedit = (QLineEdit*) widget;
          
            lineedit->blockSignals(true);
            lineedit->setText(setting->current().toString());
            lineedit->blockSignals(false);
            break;
        }
        case EncTtsSetting::eREADONLYSTRING:
        {
            QLabel* label = (QLabel*) widget;
          
            label->blockSignals(true);
            label->setText(setting->current().toString());
            label->blockSignals(false);
            break;
        }
        case EncTtsSetting::eSTRINGLIST:
        {
            QComboBox* combobox = (QComboBox*) widget;
          
            combobox->blockSignals(true);
            combobox->clear();
            combobox->addItems(setting->list());
            int index = combobox->findText(setting->current().toString());
            combobox->setCurrentIndex(index);
            combobox->blockSignals(false);
            
            break;
        }
        case EncTtsSetting::eBOOL:
        {
            QCheckBox* checkbox = (QCheckBox*) widget;
          
            checkbox->blockSignals(true);
            checkbox->setCheckState(setting->current().toBool() == true ? Qt::Checked : Qt::Unchecked);
            checkbox->blockSignals(false);
            break;
        }
        default:
        {
            qDebug() << "unknown EncTTsSetting";
            break;
        }  
    }
}

void EncTtsCfgGui::showBusy()
{
    if(m_busyCnt == 0) m_busyDlg->show();
    
    m_busyCnt++;
}

void EncTtsCfgGui::hideBusy()
{
    m_busyCnt--;
    
    if(m_busyCnt == 0) m_busyDlg->hide();
}
    
    
void EncTtsCfgGui::accept(void)
{
    m_settingInterface->saveSettings();
    this->done(0);
}

void EncTtsCfgGui::reject(void)
{
    this->done(0);
}

//! takes a QObject because of QsignalMapper
void EncTtsCfgGui::browse(QObject* settingObj)
{
    // cast top setting
    EncTtsSetting* setting= qobject_cast<EncTtsSetting*>(settingObj);
    if(setting == NULL) return;
    
    //current path
    QString curPath = setting->current().toString();
    // show file dialog
    QString exe = QFileDialog::getOpenFileName(this, tr("Select excutable"), curPath, "*");
    if(!QFileInfo(exe).isExecutable())
        return;
    // set new value, gui will update automatically
    setting->setCurrent(exe);
}