Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[3e4acc]: libgui / SelectTimeWidget.cpp Maximize Restore History

Download this file

SelectTimeWidget.cpp    517 lines (438 with data), 14.9 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/***************************************************************************
SelectTimeWidget.cpp - widget for selecting a time or range
-------------------
begin : Thu Jan 16 2003
copyright : (C) 2002 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 <math.h>
#include <limits.h>
#include <QRadioButton>
#include <QSlider>
#include <QWidget>
#include <klocale.h>
#include <knuminput.h>
#include "SelectTimeWidget.h"
//***************************************************************************
SelectTimeWidget::SelectTimeWidget(QWidget *widget)
:QGroupBox(widget), Ui::SelectTimeWidgetBase(),
m_mode(bySamples), m_range(0), m_rate(1.0), m_offset(0), m_length(0),
m_timer(this)
{
setupUi(this);
modeChanged(true);
}
//***************************************************************************
void SelectTimeWidget::init(Mode mode, unsigned int range, double sample_rate,
sample_index_t offset, sample_index_t signal_length)
{
m_mode = mode;
m_range = range;
m_rate = sample_rate;
m_offset = offset;
m_length = signal_length;
Q_ASSERT(m_rate);
Q_ASSERT(m_length);
Q_ASSERT(rbTime);
Q_ASSERT(rbSamples);
Q_ASSERT(rbPercents);
Q_ASSERT(m_offset < m_length);
if (!m_rate) m_rate = 1.0;
if (!m_length) m_length = 1;
// set range of selection by sample
edSamples->setSliderEnabled(false);
edSamples->setRange(0, m_length-m_offset, 1);
// set range of time controls
int t = static_cast<int>(rint((m_length * 1E3) / m_rate));
sbMilliseconds->setMaximum(t);
t /= 1000;
sbSeconds->setMaximum(t);
t /= 60;
sbMinutes->setMaximum(t);
t /= 60;
sbHours->setMaximum(t);
// activate the current mode
setMode(mode);
m_range = range;
// set initial values
switch (m_mode) {
case byTime: {
unsigned int t = m_range;
sbMilliseconds->setValue(t % 1000);
t /= 1000;
sbSeconds->setValue(t % 60);
t /= 60;
sbMinutes->setValue(t % 60);
t /= 60;
sbHours->setValue(t);
break;
}
case bySamples: {
unsigned int samples = m_range;
Q_ASSERT(samples <= INT_MAX);
if (samples > INT_MAX) samples = INT_MAX;
edSamples->setValue(static_cast<int>(samples));
break;
}
case byPercents: {
sbPercents->setValue(static_cast<int>(m_range));
break;
}
}
// connect mode controls
QObject::connect(rbTime, SIGNAL(toggled(bool)),
this, SLOT(modeChanged(bool)));
QObject::connect(rbSamples,SIGNAL(toggled(bool)),
this, SLOT(modeChanged(bool)));
QObject::connect(rbPercents,SIGNAL(toggled(bool)),
this, SLOT(modeChanged(bool)));
connect();
// connect percentage control
QObject::connect(sbPercents, SIGNAL(valueChanged(int)),
this, SLOT(percentsChanged(int)));
// update all controls
switch (m_mode) {
case byTime:
timeChanged(0);
break;
case bySamples:
samplesChanged(0);
break;
case byPercents:
percentsChanged(static_cast<int>(m_range));
break;
}
adjustSize();
setMinimumSize(sizeHint());
modeChanged(true);
}
//***************************************************************************
SelectTimeWidget::~SelectTimeWidget()
{
}
//***************************************************************************
void SelectTimeWidget::connect()
{
// connect the time controls
QObject::connect(sbMilliseconds, SIGNAL(valueChanged(int)),
this, SLOT(timeChanged(int)));
QObject::connect(sbSeconds, SIGNAL(valueChanged(int)),
this, SLOT(timeChanged(int)));
QObject::connect(sbMinutes, SIGNAL(valueChanged(int)),
this, SLOT(timeChanged(int)));
QObject::connect(sbHours, SIGNAL(valueChanged(int)),
this, SLOT(timeChanged(int)));
// connect sample count control
QObject::connect(edSamples, SIGNAL(valueChanged(int)),
this, SLOT(samplesChanged(int)));
// connect the timer for the sample edit
QObject::connect(&m_timer, SIGNAL(timeout()),
this, SLOT(checkNewSampleEdit()));
}
//***************************************************************************
void SelectTimeWidget::disconnect()
{
// disconnect the time controls
QObject::disconnect(sbMilliseconds, SIGNAL(valueChanged(int)),
this, SLOT(timeChanged(int)));
QObject::disconnect(sbSeconds, SIGNAL(valueChanged(int)),
this, SLOT(timeChanged(int)));
QObject::disconnect(sbMinutes, SIGNAL(valueChanged(int)),
this, SLOT(timeChanged(int)));
QObject::disconnect(sbHours, SIGNAL(valueChanged(int)),
this, SLOT(timeChanged(int)));
// disconnect sample count control
QObject::disconnect(edSamples, SIGNAL(valueChanged(int)),
this, SLOT(samplesChanged(int)));
// disconnect the timer for the sample edit
QObject::disconnect(&m_timer, SIGNAL(timeout()),
this, SLOT(checkNewSampleEdit()));
}
//***************************************************************************
void SelectTimeWidget::setMode(Mode new_mode)
{
// enable the selected mode
switch (new_mode) {
case byTime:
rbTime->setChecked(true);
Q_ASSERT(rbTime->isChecked());
Q_ASSERT(!rbSamples->isChecked());
Q_ASSERT(!rbPercents->isChecked());
break;
case bySamples:
rbSamples->setChecked(true);
Q_ASSERT(!rbTime->isChecked());
Q_ASSERT(rbSamples->isChecked());
Q_ASSERT(!rbPercents->isChecked());
break;
case byPercents:
rbPercents->setChecked(true);
Q_ASSERT(!rbTime->isChecked());
Q_ASSERT(!rbSamples->isChecked());
Q_ASSERT(rbPercents->isChecked());
break;
}
m_mode = new_mode;
}
//***************************************************************************
void SelectTimeWidget::modeChanged(bool checked)
{
if (!checked) return; // ignore disabling of radio buttons
if (rbTime->isChecked()) {
m_mode = byTime;
rbSamples->setChecked(false);
rbPercents->setChecked(false);
timeChanged(0); // (sets m_range)
}
if (rbSamples->isChecked()) {
m_mode = bySamples;
rbTime->setChecked(false);
rbPercents->setChecked(false);
samplesChanged(0); // (sets m_range)
if (rbTime->isChecked()) {
m_timer.stop();
} else {
// activate the sample edit timer
m_timer.setSingleShot(false);
m_timer.start(100);
}
}
if (rbPercents->isChecked()) {
m_mode = byPercents;
rbTime->setChecked(false);
rbSamples->setChecked(false);
percentsChanged(sbPercents->value()); // (sets m_range)
}
}
//***************************************************************************
void SelectTimeWidget::timeChanged(int)
{
if (m_mode != byTime) return;
disconnect();
// get current time and correct wrap-overs
int milliseconds = sbMilliseconds->value();
int seconds = sbSeconds->value();
int minutes = sbMinutes->value();
int hours = sbHours->value();
if (milliseconds < 0) {
milliseconds = 999;
seconds--;
}
if (seconds < 0) {
seconds = 59;
minutes--;
}
if (minutes < 0) {
minutes = 59;
hours--;
}
if (hours < 0) {
hours = 0;
minutes = 0;
seconds = 0;
milliseconds = 0;
}
Q_ASSERT((hours>=0) && (minutes>=0) && (seconds>=0) && (milliseconds>=0));
int ms =
milliseconds +
(seconds + (minutes +
(hours * 60)) * 60) * 1000;
if (ms < 0) ms = 0;
// limit time
long int max_ms = static_cast<long int>(
ceil(((m_length - m_offset) * 1E3) / m_rate));
if (ms > max_ms) ms = max_ms;
long int t = ms;
milliseconds = t % 1000L;
t /= 1000L;
seconds = t % 60L;
t /= 60L;
minutes = t % 60L;
hours = t / 60L;
sbMilliseconds->setValue(milliseconds);
sbSeconds->setValue(seconds);
sbMinutes->setValue(minutes);
sbHours->setValue(hours);
// update the other widgets
int samples = timeToSamples(byTime, ms, m_rate, m_length);
edSamples->setValue(samples);
int percents = samplesToTime(byPercents, samples, m_rate, m_length);
sbPercents->setValue(percents);
// set range in byTime mode [ms]
m_range = ms;
emit valueChanged(samples); // emit the change
connect();
}
//***************************************************************************
void SelectTimeWidget::checkNewSampleEdit()
{
static int last_samples = -1;
if (edSamples->value() != last_samples) {
last_samples = edSamples->value();
samplesChanged(last_samples);
}
}
//***************************************************************************
void SelectTimeWidget::samplesChanged(int)
{
if (m_mode != bySamples) return;
disconnect();
unsigned int max_samples = m_length - m_offset;
unsigned int samples = edSamples->value();
// limit the current value
if (samples > max_samples) samples = max_samples;
// update the other widgets
unsigned int t = samplesToTime(byTime, samples, m_rate, m_length);
sbMilliseconds->setValue(t % 1000);
t /= 1000;
sbSeconds->setValue(t % 60);
t /= 60;
sbMinutes->setValue(t % 60);
t /= 60;
sbHours->setValue(t);
int percents = samplesToTime(byPercents, samples, m_rate, m_length);
sbPercents->setValue(percents);
// update in samples mode
m_range = samples;
// re-activate the sample edit timer
m_timer.stop();
m_timer.setSingleShot(false);
m_timer.start(100);
emit valueChanged(samples); // emit the change
connect();
}
//***************************************************************************
void SelectTimeWidget::percentsChanged(int p)
{
if (m_mode != byPercents) return;
disconnect();
// limit to rest of signal
int max_percents = 100;
if (m_length)
max_percents = (100 * static_cast<quint64>(m_length - m_offset)) /
static_cast<quint64>(m_length);
if (p > max_percents) {
p = max_percents;
}
// update in byPercents mode [0...100]
m_range = p;
if (slidePercents->value() != p) slidePercents->setValue(p);
if (sbPercents->value() != p) sbPercents->setValue(p);
// update the other widgets
int samples = timeToSamples(byPercents, p, m_rate, m_length);
edSamples->setValue(samples);
unsigned int t = samplesToTime(byTime, samples, m_rate, m_length);
sbMilliseconds->setValue(t % 1000);
t /= 1000;
sbSeconds->setValue(t % 60);
t /= 60;
sbMinutes->setValue(t % 60);
t /= 60;
sbHours->setValue(t);
emit valueChanged(samples); // emit the change
connect();
}
//***************************************************************************
void SelectTimeWidget::setTitle(const QString title)
{
QGroupBox::setTitle(title);
}
//***************************************************************************
void SelectTimeWidget::setOffset(sample_index_t offset)
{
m_offset = offset;
sample_index_t max_samples = m_length - m_offset;
sample_index_t samples = edSamples->value();
// the range of the sample edit should always get updated
edSamples->setSliderEnabled(false);
edSamples->setRange(0, m_length-m_offset, 1);
// no range conflict -> nothing to do
if (samples <= max_samples) return;
// limit the length to the rest
samples = max_samples;
// update all widgets
disconnect();
int t = static_cast<int>(ceil((static_cast<double>(samples) * 1E3) /
m_rate));
sbMilliseconds->setValue(t);
t /= 1000;
sbSeconds->setValue(t);
t /= 60;
sbMinutes->setValue(t);
t /= 60;
sbHours->setValue(t);
Q_ASSERT(samples <= INT_MAX);
if (samples > INT_MAX) samples = INT_MAX;
edSamples->setValue(samples);
double percents = 100.0 * static_cast<double>(samples) /
static_cast<double>(m_length);
sbPercents->setValue(static_cast<int>(percents));
connect();
}
//***************************************************************************
sample_index_t SelectTimeWidget::samples() const
{
return (edSamples) ? edSamples->value() : 0;
}
//***************************************************************************
sample_index_t SelectTimeWidget::timeToSamples(
SelectTimeWidget::Mode mode, unsigned int time, double rate,
sample_index_t length)
{
sample_index_t pos = 0;
switch (mode) {
case SelectTimeWidget::byTime:
// convert from ms to samples
pos = static_cast<sample_index_t>(ceil(
static_cast<double>(time) * rate * 1E-3));
break;
case SelectTimeWidget::bySamples:
// simple case -> already in samples
pos = time;
break;
case SelectTimeWidget::byPercents:
// by percentage of whole signal
pos = static_cast<unsigned int>(rint(
static_cast<double>(length * (time / 100.0))));
break;
}
if (pos > INT_MAX) pos = INT_MAX;
return pos;
}
//***************************************************************************
unsigned int SelectTimeWidget::samplesToTime(
Mode mode, sample_index_t samples, double rate, sample_index_t length)
{
unsigned int time = 0;
switch (mode) {
case SelectTimeWidget::byTime:
// convert from samples to ms
time = static_cast<unsigned int>(
rint(static_cast<double>(samples) * 1E3 / rate));
break;
case SelectTimeWidget::bySamples:
// simple case -> already in samples
time = samples;
break;
case SelectTimeWidget::byPercents:
// by percentage of whole signal
time = static_cast<unsigned int>(100.0 *
static_cast<double>(samples) /
static_cast<double>(length));
break;
}
return time;
}
//***************************************************************************
#include "SelectTimeWidget.moc"
//***************************************************************************
//***************************************************************************