kwave  18.07.70
NewSignalDialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  NewSignalDialog.cpp - dialog for the "newsignal" plugin
3  -------------------
4  begin : Wed Jul 18 2001
5  copyright : (C) 2001 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <limits.h>
21 #include <math.h>
22 #include <stdlib.h>
23 
24 #include <QLabel>
25 #include <QLayout>
26 #include <QPushButton>
27 #include <QRadioButton>
28 #include <QSlider>
29 #include <QSpinBox>
30 #include <QString>
31 #include <QTimer>
32 
33 #include <KComboBox>
34 #include <KHelpClient>
35 #include <KLocalizedString>
36 
37 #include "libkwave/String.h"
38 #include "libkwave/Utils.h"
39 
40 #include "NewSignalDialog.h"
41 
42 //***************************************************************************
44  unsigned int rate, unsigned int bits, unsigned int tracks,
45  bool by_time)
46  :QDialog(parent), Ui::NewSigDlg(), m_timer(this), m_recursive(false)
47 {
48 
49  setupUi(this);
50  setModal(true);
51 
52  edSamples->setRange(0, INT_MAX);
53  edSamples->setSingleStep(1);
54 
55  // connect the timer for the sample edit
56  connect(&m_timer, SIGNAL(timeout()), this, SLOT(checkNewSampleEdit()));
57  connect(rbTime, SIGNAL(toggled(bool)),
58  this, SLOT(rbTimeToggled(bool)));
59 
60  // connect the file format controls
61  connect(cbSampleRate, SIGNAL(editTextChanged(QString)),
62  this, SLOT(sampleRateChanged(QString)));
63  connect(sbChannels, SIGNAL(valueChanged(int)),
64  this, SLOT(tracksChanged(int)));
65  connect(sbResolution, SIGNAL(valueChanged(int)),
66  this, SLOT(checkTimeAndLengthInfo(int)));
67 
68  // connect the time controls
69  connect(sbSeconds, SIGNAL(valueChanged(int)),
70  this, SLOT(timeChanged(int)));
71  connect(sbMinutes, SIGNAL(valueChanged(int)),
72  this, SLOT(timeChanged(int)));
73  connect(sbHours, SIGNAL(valueChanged(int)),
74  this, SLOT(timeChanged(int)));
75 
76  // selection by number of samples
77  connect(slideLength,SIGNAL(valueChanged(int)),
78  this, SLOT(setLengthPercentage(int)));
79 
80  // selection by percentage of maximum possible length
81  connect(edSamples, SIGNAL(valueChanged(int)),
82  this, SLOT(samplesChanged(int)));
83 
84  // help button
85  connect(buttonBox->button(QDialogButtonBox::Help), SIGNAL(clicked()),
86  this, SLOT(invokeHelp()));
87 
88  // pre-initialize the size
89  setMaximumHeight(sizeHint().height());
90  setMaximumWidth(sizeHint().width());
91 
92  // initialize the controls
93  cbSampleRate->setEditText(QString::number(rate));
94  sbResolution->setValue(bits);
95  sbChannels->setValue(tracks);
96  if (by_time) {
97  rbSamples->setChecked(false);
98  rbTime->setChecked(true);
99  setHMS(samples);
100  edSamples->setEnabled(false);
101  sbHours->setEnabled(true);
102  sbMinutes->setEnabled(true);
103  sbSeconds->setEnabled(true);
104  } else {
105  // by samples
106  rbTime->setChecked(false);
107  rbSamples->setChecked(true);
108  edSamples->setValue(Kwave::toInt(samples));
109  edSamples->setEnabled(true);
110  sbHours->setEnabled(false);
111  sbMinutes->setEnabled(false);
112  sbSeconds->setEnabled(false);
113  }
114 
115  tracksChanged(0);
117 
118  // that dialog is big enough, limit it to it's optimal size
119  setFixedHeight(sizeHint().height());
120  setFixedWidth(sizeHint().width());
121 
122  // set the focus onto the "OK" button
123  buttonBox->button(QDialogButtonBox::Ok)->setFocus();
124 }
125 
126 //***************************************************************************
128 {
129  return static_cast<sample_index_t>(edSamples->value());
130 }
131 
132 //***************************************************************************
134 {
135  bool ok;
136  double r = cbSampleRate->currentText().toDouble(&ok);
137  if (!ok) r = 0;
138  return r;
139 }
140 
141 //***************************************************************************
143 {
144  static int last_samples = -1;
145  if (edSamples->value() != last_samples) {
146  last_samples = edSamples->value();
147  samplesChanged(last_samples);
148  }
149 }
150 
151 //***************************************************************************
153 {
154  return sbChannels->value();
155 }
156 
157 //***************************************************************************
159 {
160  int res = sbResolution->value();
161  if (res < 8) res = 8;
162  return res;
163 }
164 
165 //***************************************************************************
167 {
168  return rbTime->isChecked();
169 }
170 
171 //***************************************************************************
173 {
174  unsigned int bytes_per_sample = bitsPerSample() >> 3;
175 
176  /*
177  * NOTE: this limitation to INT_MAX instead of UINT_MAX is
178  * only needed because some gui elements like
179  * QSpinBox cannot handle more :-(
180  */
181  const sample_index_t max_file_size = INT_MAX;
182 
183  return (max_file_size / tracks() / bytes_per_sample);
184 }
185 
186 //***************************************************************************
188 {
189  if (rbTime->isChecked()) {
190  m_timer.stop();
191  } else {
192  // activate the sample edit timer
193  m_timer.setSingleShot(false);
194  m_timer.start(100);
195  }
196 }
197 
198 //***************************************************************************
200 {
201  (rbTime->isChecked()) ? timeChanged(0) : samplesChanged(0);
202 }
203 
204 //***************************************************************************
206 {
207  if (m_recursive) return; // don't do recursive processing
208  if (!rbTime->isChecked()) return;
209  if ((rate() <= 0) || !tracks() || (bitsPerSample() < 8)) return;
210  m_recursive = true;
211 
212  // get current time and correct wrap-overs
213  int seconds = sbSeconds->value();
214  int minutes = sbMinutes->value();
215  int hours = sbHours->value();
216  if ((seconds < 0) && ((minutes > 0) || (hours > 0)) ) {
217  sbSeconds->setValue(59);
218  sbMinutes->stepDown();
219  minutes--;
220  } else if (seconds < 0) {
221  sbSeconds->setValue(0);
222  } else if (seconds > 59) {
223  sbSeconds->setValue(0);
224  sbMinutes->stepUp();
225  minutes++;
226  }
227 
228  if ((minutes < 0) && (hours > 0)) {
229  sbMinutes->setValue(59);
230  sbHours->stepDown();
231  hours--;
232  } else if (minutes < 0) {
233  sbMinutes->setValue(0);
234  } else if (minutes > 59) {
235  sbMinutes->setValue(0);
236  sbHours->stepUp();
237  hours++;
238  }
239  seconds = sbSeconds->value();
240  minutes = sbMinutes->value();
241  hours = sbHours->value();
242  minutes += 60 * hours;
243  seconds += 60 * minutes;
244 
245  // limit the current number of samples
246  sample_index_t max_samples = maxSamples();
247  sample_index_t samples = static_cast<sample_index_t>(ceil(
248  static_cast<double>(seconds) * rate()));
249 
250  if (samples > max_samples) {
251  // wrap down to the maximum allowed number of samples
252  samples = max_samples;
253  setHMS(samples);
254  }
255 
256  // update the other controls
257  edSamples->setValue(Kwave::toInt(samples));
258  slideLength->setValue(Kwave::toInt(100.0 * samples / max_samples));
259  updateFileSize();
260  buttonBox->button(QDialogButtonBox::Ok)->setEnabled(samples > 0.0);
261 
262  m_recursive = false;
263 }
264 
265 //***************************************************************************
267 {
268  if (m_recursive) return; // don't do recursive processing
269  if (!rbSamples->isChecked()) return;
270  m_recursive = true;
271 
272  sample_index_t samples = edSamples->value();
273  sample_index_t max_samples = maxSamples();
274 
275  if (samples > max_samples) {
276  samples = max_samples;
277  edSamples->setValue(Kwave::toInt(samples));
278  }
279 
280  // update the other controls
281  setHMS(samples);
282  slideLength->setValue(Kwave::toInt(100.0 * samples / max_samples));
283  updateFileSize();
284  buttonBox->button(QDialogButtonBox::Ok)->setEnabled(samples > 0.0);
285 
286  m_recursive = false;
287 }
288 
289 //***************************************************************************
291 {
293 }
294 
295 //***************************************************************************
297 {
298  switch (tracks()) {
299  case 1:
300  lblTracksVerbose->setText(i18n("(Mono)"));
301  break;
302  case 2:
303  lblTracksVerbose->setText(i18n("(Stereo)"));
304  break;
305  case 4:
306  lblTracksVerbose->setText(i18n("(Quadro)"));
307  break;
308  default:
309  lblTracksVerbose->setText(_(""));
310  break;
311  }
313 }
314 
315 //***************************************************************************
317 {
318  double samples = static_cast<double>(edSamples->value());
319  double mbytes = samples * static_cast<double>(tracks()) *
320  static_cast<double>(bitsPerSample() >> 3);
321  mbytes /= 1024.0; // to kilobytes
322  mbytes /= 1024.0; // to megabytes
323 
324  QString str_bytes;
325  if (mbytes >= 10.0) {
326  str_bytes.sprintf("%0.1f", mbytes);
327  } else {
328  str_bytes.sprintf("%0.3f", mbytes);
329  }
330 
331  lblFileSize->setText(i18n("(Resulting file size: %1 MB)", str_bytes));
332 }
333 
334 //***************************************************************************
336 {
337  if (m_recursive) return; // don't do recursive processing
338  if (rate() <= 0) return;
339  m_recursive = true;
340 
342  static_cast<double>(percent) / 100.0);
343 
344  // update the other controls
345  setHMS(samples);
346  edSamples->setValue(Kwave::toInt(samples));
347  updateFileSize();
348  buttonBox->button(QDialogButtonBox::Ok)->setEnabled(samples > 0);
349 
350  m_recursive = false;
351 }
352 
353 //***************************************************************************
355 {
356  double rate = this->rate();
357  if (rate <= 0.0) return;
358 
359  // TODO: support for 64 bit
360  if (samples > maxSamples()) samples = maxSamples();
361 
362  int total_sec = Kwave::toInt(ceil(samples / rate));
363  int hours = total_sec / (60*60);
364  int minutes = (total_sec / 60) % 60;
365  int seconds = total_sec % 60;
366 
367  sbHours->setValue(hours);
368  sbMinutes->setValue(minutes);
369  sbSeconds->setValue(seconds);
370 }
371 
372 //***************************************************************************
374 {
375  KHelpClient::invokeHelp(_("newsignal"));
376 }
377 
378 //***************************************************************************
379 //***************************************************************************
unsigned int bitsPerSample()
sample_index_t maxSamples()
quint64 sample_index_t
Definition: Sample.h:28
NewSignalDialog(QWidget *parent, sample_index_t samples, unsigned int rate, unsigned int bits, unsigned int tracks, bool by_time)
bool connect(Kwave::StreamObject &source, const char *output, Kwave::StreamObject &sink, const char *input)
Definition: Connect.cpp:48
void setHMS(sample_index_t &samples)
void setLengthPercentage(int percent)
int toInt(T x)
Definition: Utils.h:127
void sampleRateChanged(const QString &)
#define _(m)
Definition: memcpy.c:66
sample_index_t samples()