[3e4acc]: plugins / newsignal / NewSignalDialog.cpp Maximize Restore History

Download this file

NewSignalDialog.cpp    381 lines (323 with data), 11.4 kB

  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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/***************************************************************************
NewSignalDialog.cpp - dialog for the "newsignal" plugin
-------------------
begin : Wed Jul 18 2001
copyright : (C) 2001 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "config.h"
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <QComboBox>
#include <QLabel>
#include <QLayout>
#include <QPushButton>
#include <QRadioButton>
#include <QSlider>
#include <QSpinBox>
#include <QString>
#include <QTimer>
#include <klocale.h>
#include <knuminput.h>
#include <ktoolinvocation.h>
#include "NewSignalDialog.h"
//***************************************************************************
NewSignalDialog::NewSignalDialog(QWidget *parent, unsigned int samples,
unsigned int rate, unsigned int bits, unsigned int tracks,
bool by_time)
:QDialog(parent), Ui::NewSigDlg(), m_timer(this), m_recursive(false)
{
setupUi(this);
setModal(true);
edSamples->setRange(0, INT_MAX, 1);
edSamples->setSliderEnabled(false);
// connect the timer for the sample edit
connect(&m_timer, SIGNAL(timeout()), this, SLOT(checkNewSampleEdit()));
connect(rbTime, SIGNAL(toggled(bool)),
this, SLOT(rbTimeToggled(bool)));
// connect the file format controls
connect(cbSampleRate, SIGNAL(editTextChanged(const QString&)),
this, SLOT(sampleRateChanged(const QString&)));
connect(sbChannels, SIGNAL(valueChanged(int)),
this, SLOT(tracksChanged(int)));
connect(sbResolution, SIGNAL(valueChanged(int)),
this, SLOT(checkTimeAndLengthInfo(int)));
// connect the time controls
connect(sbSeconds, SIGNAL(valueChanged(int)),
this, SLOT(timeChanged(int)));
connect(sbMinutes, SIGNAL(valueChanged(int)),
this, SLOT(timeChanged(int)));
connect(sbHours, SIGNAL(valueChanged(int)),
this, SLOT(timeChanged(int)));
// selection by number of samples
connect(slideLength,SIGNAL(valueChanged(int)),
this, SLOT(setLengthPercentage(int)));
// selection by percentage of maximum possible length
connect(edSamples, SIGNAL(valueChanged(int)),
this, SLOT(samplesChanged(int)));
// help button
connect(btHelp, SIGNAL(clicked()),
this, SLOT(invokeHelp()));
// pre-initialize the size
setMaximumHeight(sizeHint().height());
setMaximumWidth(sizeHint().width());
// initialize the controls
cbSampleRate->setEditText(QString::number(rate));
sbResolution->setValue(bits);
sbChannels->setValue(tracks);
if (by_time) {
rbSamples->setChecked(false);
rbTime->setChecked(true);
setHMS(samples);
edSamples->setEnabled(false);
sbHours->setEnabled(true);
sbMinutes->setEnabled(true);
sbSeconds->setEnabled(true);
} else {
// by samples
rbTime->setChecked(false);
rbSamples->setChecked(true);
edSamples->setValue(samples);
edSamples->setEnabled(true);
sbHours->setEnabled(false);
sbMinutes->setEnabled(false);
sbSeconds->setEnabled(false);
}
tracksChanged(0);
checkTimeAndLengthInfo(0);
// that dialog is big enough, limit it to it's optimal size
setFixedHeight(sizeHint().height());
setFixedWidth(sizeHint().width());
}
//***************************************************************************
unsigned int NewSignalDialog::samples()
{
return static_cast<unsigned int>(edSamples->value());
}
//***************************************************************************
double NewSignalDialog::rate()
{
bool ok;
double r = cbSampleRate->currentText().toDouble(&ok);
if (!ok) r = 0;
return r;
}
//***************************************************************************
void NewSignalDialog::checkNewSampleEdit()
{
static int last_samples = -1;
if (edSamples->value() != last_samples) {
last_samples = edSamples->value();
samplesChanged(last_samples);
}
}
//***************************************************************************
unsigned int NewSignalDialog::tracks()
{
return sbChannels->value();
}
//***************************************************************************
unsigned int NewSignalDialog::bitsPerSample()
{
int res = sbResolution->value();
if (res < 8) res = 8;
return res;
}
//***************************************************************************
bool NewSignalDialog::byTime()
{
return rbTime->isChecked();
}
//***************************************************************************
unsigned int NewSignalDialog::maxSamples()
{
unsigned int bytes_per_sample = bitsPerSample() >> 3;
/*
* NOTE: this limitation to INT_MAX instead of UINT_MAX is
* only needed because some gui elements like
* KIntNumInput cannot handle more :-(
*/
unsigned int max_file_size = INT_MAX;
return (max_file_size / tracks() / bytes_per_sample);
}
//***************************************************************************
void NewSignalDialog::rbTimeToggled(bool)
{
if (rbTime->isChecked()) {
m_timer.stop();
} else {
// activate the sample edit timer
m_timer.setSingleShot(false);
m_timer.start(100);
}
}
//***************************************************************************
void NewSignalDialog::checkTimeAndLengthInfo(int)
{
(rbTime->isChecked()) ? timeChanged(0) : samplesChanged(0);
}
//***************************************************************************
void NewSignalDialog::timeChanged(int)
{
if (m_recursive) return; // don't do recursive processing
if (!rbTime->isChecked()) return;
if (!rate() || !tracks() || (bitsPerSample() < 8)) return;
m_recursive = true;
// get current time and correct wrap-overs
int seconds = sbSeconds->value();
int minutes = sbMinutes->value();
int hours = sbHours->value();
if ((seconds < 0) && ((minutes > 0) || (hours > 0)) ) {
sbSeconds->setValue(59);
sbMinutes->stepDown();
minutes--;
} else if (seconds < 0) {
sbSeconds->setValue(0);
} else if (seconds > 59) {
sbSeconds->setValue(0);
sbMinutes->stepUp();
minutes++;
}
if ((minutes < 0) && (hours > 0)) {
sbMinutes->setValue(59);
sbHours->stepDown();
hours--;
} else if (minutes < 0) {
sbMinutes->setValue(0);
} else if (minutes > 59) {
sbMinutes->setValue(0);
sbHours->stepUp();
hours++;
}
seconds = sbSeconds->value();
minutes = sbMinutes->value();
hours = sbHours->value();
minutes += 60 * hours;
seconds += 60 * minutes;
// limit the current number of samples
unsigned int max_samples = maxSamples();
unsigned int samples = static_cast<unsigned int>(ceil(
static_cast<double>(seconds) * rate()));
if (samples > max_samples) {
// wrap down to the maximum allowed number of samples
samples = max_samples;
setHMS(samples);
}
// update the other controls
Q_ASSERT(samples <= INT_MAX);
if (samples > INT_MAX) samples = INT_MAX;
edSamples->setValue(samples);
slideLength->setValue(static_cast<int>(100.0 * samples / max_samples));
updateFileSize();
btOK->setEnabled(samples > 0.0);
m_recursive = false;
}
//***************************************************************************
void NewSignalDialog::samplesChanged(int)
{
if (m_recursive) return; // don't do recursive processing
if (!rbSamples->isChecked()) return;
m_recursive = true;
unsigned int samples = edSamples->value();
unsigned int max_samples = maxSamples();
if (samples > max_samples) {
samples = max_samples;
Q_ASSERT(samples <= INT_MAX);
if (samples > INT_MAX) samples = INT_MAX;
edSamples->setValue(samples);
}
// update the other controls
setHMS(samples);
slideLength->setValue(static_cast<int>(100.0 * samples / max_samples));
updateFileSize();
btOK->setEnabled(samples > 0.0);
m_recursive = false;
}
//***************************************************************************
void NewSignalDialog::sampleRateChanged(const QString&)
{
checkTimeAndLengthInfo(0);
}
//***************************************************************************
void NewSignalDialog::tracksChanged(int)
{
switch (tracks()) {
case 1:
lblTracksVerbose->setText(i18n("(Mono)"));
break;
case 2:
lblTracksVerbose->setText(i18n("(Stereo)"));
break;
case 4:
lblTracksVerbose->setText(i18n("(Quadro)"));
break;
default:
lblTracksVerbose->setText("");
break;
}
checkTimeAndLengthInfo(0);
}
//***************************************************************************
void NewSignalDialog::updateFileSize()
{
double samples = static_cast<double>(edSamples->value());
double mbytes = samples * static_cast<double>(tracks()) *
static_cast<double>(bitsPerSample() >> 3);
mbytes /= 1024.0; // to kilobytes
mbytes /= 1024.0; // to megabytes
QString str_bytes = "";
if (mbytes >= 10.0) {
str_bytes.sprintf("%0.1f",mbytes);
} else {
str_bytes.sprintf("%0.3f",mbytes);
}
lblFileSize->setText(i18n("(Resulting file size: %1 MB)", str_bytes));
}
//***************************************************************************
void NewSignalDialog::setLengthPercentage(int percent)
{
if (m_recursive) return; // don't do recursive processing
if (rate() <= 0) return;
m_recursive = true;
unsigned int samples = static_cast<unsigned int>(maxSamples() *
static_cast<double>(percent) / 100.0);
Q_ASSERT(samples <= INT_MAX);
if (samples > INT_MAX) samples = INT_MAX;
// update the other controls
edSamples->setValue(samples);
setHMS(samples);
updateFileSize();
btOK->setEnabled(samples > 0.0);
m_recursive = false;
}
//***************************************************************************
void NewSignalDialog::setHMS(const double &samples)
{
double rate = this->rate();
if (rate <= 0.0) return;
int total_sec = static_cast<int>(ceil(samples / rate));
int hours = total_sec / (60*60);
int minutes = (total_sec / 60) % 60;
int seconds = total_sec % 60;
sbHours->setValue(hours);
sbMinutes->setValue(minutes);
sbSeconds->setValue(seconds);
}
//***************************************************************************
void NewSignalDialog::invokeHelp()
{
KToolInvocation::invokeHelp("memory-setup");
}
//***************************************************************************
#include "NewSignalDialog.moc"
//***************************************************************************
//***************************************************************************