File: SelectTimeWidget.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (530 lines) | stat: -rw-r--r-- 16,835 bytes parent folder | download
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/***************************************************************************
   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 <math.h>

#include <limits>

#include <QRadioButton>
#include <QSlider>
#include <QWidget>

#include <KLocalizedString>

#include "libkwave/Utils.h"

#include "libgui/SelectTimeWidget.h"

//***************************************************************************
Kwave::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 Kwave::SelectTimeWidget::init(Mode mode, quint64 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 > 0);
    Q_ASSERT(m_length);
    Q_ASSERT(rbTime);
    Q_ASSERT(rbSamples);
    Q_ASSERT(rbPercents);
    Q_ASSERT(m_offset < m_length);
    if (m_rate <= 0) m_rate = 1.0;
    if (!m_length) m_length = 1;

    // limit the length if necessary
    if ((m_length - m_offset) > SAMPLE_INDEX_MAX)
        m_length = m_offset + SAMPLE_INDEX_MAX;

    // set range of selection by sample
    edSamples->setRange(0, Kwave::toInt(m_length - m_offset));
    edSamples->setSingleStep(1);

    // set range of time controls
    {
        quint64 t = static_cast<quint64>(
            (static_cast<double>(m_length) * 1E3) / m_rate);
        sbMilliseconds->setMaximum(Kwave::toInt(qMax(t, quint64(999))));
        t /= 1000;
        sbSeconds->setMaximum(Kwave::toInt(qMax(t, quint64(59))));
        t /= 60;
        sbMinutes->setMaximum(Kwave::toInt(qMax(t, quint64(59))));
        t /= 60;
        sbHours->setMaximum(Kwave::toInt(t));
    }

    // activate the current mode
    setMode(mode);
    m_range = range;

    // set initial values
    switch (m_mode) {
        case byTime: {
            quint64 t = m_range;
            sbMilliseconds->setValue(Kwave::toInt(t % 1000));
            t /= 1000;
            sbSeconds->setValue(Kwave::toInt(t % 60));
            t /= 60;
            sbMinutes->setValue(Kwave::toInt(t % 60));
            t /= 60;
            sbHours->setValue(Kwave::toInt(t));
            break;
        }
        case bySamples: {
            quint64 samples = qMin<quint64>(m_range,
                                            std::numeric_limits<int>::max());
            edSamples->setValue(Kwave::toInt(samples));
            break;
        }
        case byPercents: {
            sbPercents->setValue(Kwave::toInt(m_range));
            break;
        }
        DEFAULT_IMPOSSIBLE;
    }

    // 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(Kwave::toInt(m_range));
            break;
        DEFAULT_IMPOSSIBLE;
    }

    adjustSize();
    setMinimumSize(sizeHint());
    modeChanged(true);
}

//***************************************************************************
Kwave::SelectTimeWidget::~SelectTimeWidget()
{
}

//***************************************************************************
void Kwave::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 Kwave::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 Kwave::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;
        DEFAULT_IMPOSSIBLE;
    }
    m_mode = new_mode;
}

//***************************************************************************
void Kwave::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 Kwave::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));

    quint64 ms = milliseconds +
        (seconds + (minutes + (hours * 60)) * 60) * 1000;

    // limit time
    quint64 max_ms = static_cast<quint64>(
        ceil((static_cast<double>(m_length - m_offset) * 1E3) / m_rate));
    if (ms > max_ms) ms = max_ms;
    quint64 t = ms;

    milliseconds = Kwave::toInt(t % 1000L);
    t /= 1000L;
    seconds = Kwave::toInt(t % 60L);
    t /= 60L;
    minutes = Kwave::toInt(t % 60L);
    hours   = Kwave::toInt(t / 60L);

    sbMilliseconds->setValue(milliseconds);
    sbSeconds->setValue(seconds);
    sbMinutes->setValue(minutes);
    sbHours->setValue(hours);

    // update the other widgets
    sample_index_t samples = timeToSamples(byTime, ms, m_rate, m_length);
    edSamples->setValue(Kwave::toInt(samples));
    quint64 percents = samplesToTime(byPercents, samples, m_rate, m_length);
    sbPercents->setValue(Kwave::toInt(percents));

    // set range in byTime mode [ms]
    m_range = ms;

    emit valueChanged(samples); // emit the change
    connect();
}

//***************************************************************************
void Kwave::SelectTimeWidget::checkNewSampleEdit()
{
    static int last_samples = -1;
    if (edSamples->value() != last_samples) {
        last_samples = edSamples->value();
        samplesChanged(last_samples);
    }
}

//***************************************************************************
void Kwave::SelectTimeWidget::samplesChanged(int)
{
    if (m_mode != bySamples) return;
    disconnect();

    sample_index_t max_samples = m_length - m_offset;
    sample_index_t samples     = edSamples->value();

    // limit the current value
    if (samples > max_samples) samples = max_samples;

    // update the other widgets
    quint64 t = samplesToTime(byTime, samples, m_rate, m_length);
    sbMilliseconds->setValue(Kwave::toInt(t % 1000));
    t /= 1000;
    sbSeconds->setValue(Kwave::toInt(t % 60));
    t /= 60;
    sbMinutes->setValue(Kwave::toInt(t % 60));
    t /= 60;
    sbHours->setValue(Kwave::toInt(t));

    quint64 percents = samplesToTime(byPercents, samples, m_rate, m_length);
    sbPercents->setValue(Kwave::toInt(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 Kwave::SelectTimeWidget::percentsChanged(int p)
{
    if (m_mode != byPercents) return;
    disconnect();

    // limit to rest of signal
    int max_percents = 100;
    if (m_length)
        max_percents = Kwave::toInt(
            (100U * 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
    sample_index_t samples = timeToSamples(byPercents, p, m_rate, m_length);
    edSamples->setValue(Kwave::toInt(samples));

    quint64 t = samplesToTime(byTime, samples, m_rate, m_length);
    sbMilliseconds->setValue(Kwave::toInt(t % 1000));
    t /= 1000;
    sbSeconds->setValue(Kwave::toInt(t % 60));
    t /= 60;
    sbMinutes->setValue(Kwave::toInt(t % 60));
    t /= 60;
    sbHours->setValue(Kwave::toInt(t));

    emit valueChanged(samples); // emit the change
    connect();
}

//***************************************************************************
void Kwave::SelectTimeWidget::setTitle(const QString title)
{
    QGroupBox::setTitle(title);
}

//***************************************************************************
void Kwave::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
    if (max_samples > SAMPLE_INDEX_MAX)
        max_samples = SAMPLE_INDEX_MAX;
    edSamples->setRange(0, Kwave::toInt(max_samples));
    edSamples->setSingleStep(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();

    quint64 t = samplesToTime(bySamples, samples, m_rate, m_length);
    sbMilliseconds->setValue(Kwave::toInt(t % 1000));
    t /= 1000;
    sbSeconds->setValue(Kwave::toInt(t % 60));
    t /= 60;
    sbMinutes->setValue(Kwave::toInt(t % 60));
    t /= 60;
    sbHours->setValue(Kwave::toInt(t));

    Q_ASSERT(samples <= SAMPLE_INDEX_MAX);
    if (samples > SAMPLE_INDEX_MAX)
        samples = SAMPLE_INDEX_MAX;
    edSamples->setValue(Kwave::toInt(samples));

    double percents = 100.0 * static_cast<double>(samples) /
        static_cast<double>(m_length);
    sbPercents->setValue(Kwave::toInt(percents));

    connect();
}

//***************************************************************************
sample_index_t Kwave::SelectTimeWidget::samples() const
{
    return (edSamples) ? edSamples->value() : 0;
}

//***************************************************************************
sample_index_t Kwave::SelectTimeWidget::timeToSamples(
    Kwave::SelectTimeWidget::Mode mode, quint64 time, double rate,
    sample_index_t length)
{
    sample_index_t pos = 0;
    switch (mode) {
        case Kwave::SelectTimeWidget::byTime:
            // convert from ms to samples
            pos = static_cast<sample_index_t>(ceil(
                static_cast<double>(time) * (rate * 1E-3)));
            break;
        case Kwave::SelectTimeWidget::bySamples:
            // simple case -> already in samples
            pos = time;
            break;
        case Kwave::SelectTimeWidget::byPercents:
            // by percentage of whole signal
            pos = static_cast<unsigned int>(rint(
                static_cast<double>(length) *
                (static_cast<double>(time) / 100.0)));
            break;
        DEFAULT_IMPOSSIBLE;
    }

    if (pos > SAMPLE_INDEX_MAX)
        pos = SAMPLE_INDEX_MAX;
    return pos;
}

//***************************************************************************
quint64 Kwave::SelectTimeWidget::samplesToTime(
    Mode mode, sample_index_t samples, double rate, sample_index_t length)
{
    quint64 time = 0;

    switch (mode) {
        case Kwave::SelectTimeWidget::byTime:
            // convert from samples to ms
            time = static_cast<quint64>(
                rint(static_cast<double>(samples) * 1E3 / rate));
            break;
        case Kwave::SelectTimeWidget::bySamples:
            // simple case -> already in samples
            time = samples;
            break;
        case Kwave::SelectTimeWidget::byPercents:
            // by percentage of whole signal
            time = static_cast<quint64>(100.0 *
                static_cast<double>(samples) /
                static_cast<double>(length));
            break;
        DEFAULT_IMPOSSIBLE;
    }

    return time;
}

//***************************************************************************
//***************************************************************************

#include "moc_SelectTimeWidget.cpp"