summaryrefslogtreecommitdiff
path: root/src/ui/soundconf.cpp
blob: d42456c07a09711d078acc3ea9a153dd09d80015 (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
/***************************************************************************
 *   fqterm, a terminal emulator for both BBS and *nix.                    *
 *   Copyright (C) 2008 fqterm development group.                          *
 *                                                                         *
 *   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 program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.               *
 ***************************************************************************/

#include <QMessageBox>
#include <QFileDialog>

#include "fqterm.h"
#include "fqterm_config.h"
#include "fqterm_path.h"
#include "fqterm_sound.h"

#include "soundconf.h"

namespace FQTerm {
/*
 *  Constructs a fSoundConf which is a child of 'parent', with the
 *  name 'name' and widget flags set to 'f'
 *
 *  The dialog will by default be modeless, unless you set 'modal' to
 *  TRUE to construct a modal dialog.
 */

soundConf::soundConf(FQTermConfig * config, QWidget *parent, Qt::WindowFlags fl)
    : QDialog(parent, fl),
    buttonGroup_(this),
    config_(config){
  ui_.setupUi(this);
  buttonGroup_.addButton(ui_.radioButton1, 0);
  buttonGroup_.addButton(ui_.radioButton2, 1);
  sound_ = NULL;
  loadSetting();
  FQ_VERIFY(connect(ui_.bfSelect, SIGNAL(clicked()),
    this, SLOT(onSelectFile())));
  FQ_VERIFY(connect(ui_.bpSelect, SIGNAL(clicked()),
    this, SLOT(onSelectProg())));
  FQ_VERIFY(connect(ui_.bpTest, SIGNAL(clicked()),
    this, SLOT(onTestPlay())));
  FQ_VERIFY(connect(ui_.bOK, SIGNAL(clicked()),
    this, SLOT(accept())));
  FQ_VERIFY(connect(ui_.bCancel, SIGNAL(clicked()),
    this, SLOT(reject())));
}

/*
 *  Destroys the object and frees any allocated resources
 */
soundConf::~soundConf() {
  // no need to delete child widgets, Qt does it all for us
}

/*
 * public slot
 */
void soundConf::onSelectFile() {
  FQTermFileDialog fileDialog(config_);
  QString soundfile = fileDialog.getOpenName("Choose a WAVE file", "WAVE Audio Files (*.wav *.WAV)");
  if (!soundfile.isEmpty()) {
    ui_.leFile->setText(soundfile);
  }
}

/*
 * public slot
 */
void soundConf::onSelectProg() {
  FQTermFileDialog fileDialog(config_);
  QString progfile = fileDialog.getOpenName("Choose a program", "");
  if (!progfile.isEmpty()) {
    ui_.leProg->setText(progfile);
  }
}

/*
 * public slot
 */
void soundConf::onPlayMethod(int id) {
FQ_TRACE("sconf", 0) << id << ": " << buttonGroup_.checkedId();
  ui_.bpSelect->setEnabled(id == 1 || buttonGroup_.checkedId() == 1);
}

void soundConf::onTestPlay() {
  if (ui_.leFile->text().isEmpty()) {
    QMessageBox::critical(this, tr("No sound file"),
      tr("You have to select a file to test the sound"), tr("&Ok"));

    return;
  }

  sound_ = NULL;

  switch (buttonGroup_.checkedId()) {
    case 0:
      sound_ = new FQTermSystemSound(ui_.leFile->text());
      break;
    case 1:
      if (ui_.leProg->text().isEmpty()) {
        QMessageBox::critical(this, tr("No player"),
          tr("You have to specify an external player"), tr("&Ok"));

        break;
      }

      sound_ = new FQTermExternalSound(ui_.leProg->text(), ui_.leFile->text());
      break;
  }

  if (sound_) {
    sound_->start();
  }
}

void soundConf::loadSetting() {

  QString strTmp;

  strTmp = config_->getItemValue("preference", "wavefile");
  if (!strTmp.isEmpty()) {
    ui_.leFile->setText(strTmp);
  }

  strTmp = config_->getItemValue("preference", "playmethod");

  int valTmp = !strTmp.isEmpty()? strTmp.toInt(): -1;

  if (valTmp >= 0 && valTmp <= 1) {
    buttonGroup_.button(valTmp)->setChecked(true);
    if (valTmp == 1) {
      strTmp = config_->getItemValue("preference", "externalplayer");
      if (!strTmp.isEmpty()) {
        ui_.leProg->setText(strTmp);
      }
    }
  }
}

void soundConf::saveSetting() {

  QString strTmp;

  config_->setItemValue("preference", "beep", "2");

  config_->setItemValue("preference", "wavefile", ui_.leFile->text());

  strTmp.setNum(buttonGroup_.checkedId());
  config_->setItemValue("preference", "playmethod", strTmp);

  if (strTmp == "1") {
    config_->setItemValue("preference", "externalplayer", ui_.leProg->text());
  }

  config_->save(getPath(USER_CONFIG) + "fqterm.cfg");
}

void soundConf::accept() {
  saveSetting();
  QDialog::accept();
}

}  // namespace FQTerm

#include "soundconf.moc"