File: OverViewWidget.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 (465 lines) | stat: -rw-r--r-- 15,820 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
/***************************************************************************
     OverViewWidget.cpp  -  horizontal slider with overview over a signal
                             -------------------
    begin                : Tue Oct 21 2000
    copyright            : (C) 2000 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 <QApplication>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QResizeEvent>
#include <QVBoxLayout>

#include "libkwave/Label.h"
#include "libkwave/Sample.h"
#include "libkwave/SignalManager.h"
#include "libkwave/String.h"
#include "libkwave/Utils.h"

#include "libgui/OverViewWidget.h"

/**
 * interval for limiting the number of repaints per second [ms]
 * (in normal mode, no playback running)
 */
#define REPAINT_INTERVAL 100

/**
 * interval for limiting the number of repaints per second [ms]
 * (when playback is running)
 */
#define REPAINT_INTERVAL_FAST 20

#define BAR_BACKGROUND    palette().mid().color()
#define BAR_FOREGROUND    palette().light().color()

//***************************************************************************
//***************************************************************************
Kwave::OverViewWidget::WorkerThread::WorkerThread(
    Kwave::OverViewWidget *overview)
    :QThread(), m_overview(overview)
{
}

//***************************************************************************
Kwave::OverViewWidget::WorkerThread::~WorkerThread()
{
    Q_ASSERT(!isRunning());
}

//***************************************************************************
void Kwave::OverViewWidget::WorkerThread::run()
{
    if (m_overview) m_overview->calculateBitmap();
}

//***************************************************************************
//***************************************************************************
Kwave::OverViewWidget::OverViewWidget(Kwave::SignalManager &signal,
                                      QWidget *parent)
    :Kwave::ImageView(parent), m_view_offset(0), m_view_width(0),
     m_signal_length(0), m_selection_start(0), m_selection_length(0),
     m_cursor_position(SAMPLE_INDEX_MAX), m_last_offset(0),
     m_cache(signal, 0, 0, nullptr), m_repaint_timer(), m_labels(),
     m_worker_thread(this)
{
    // check: start() must be called from the GUI thread only!
    Q_ASSERT(this->thread() == QThread::currentThread());
    Q_ASSERT(this->thread() == qApp->thread());

    // update the bitmap if the cache has changed
    connect(&m_cache, SIGNAL(changed()),
            this, SLOT(overviewChanged()));

    // connect repaint timer
    connect(&m_repaint_timer, SIGNAL(timeout()),
            this, SLOT(refreshBitmap()));

    // get informed about selection changes
    connect(&(signal.selection()),
            SIGNAL(changed(sample_index_t,sample_index_t)),
            this,
            SLOT(setSelection(sample_index_t,sample_index_t)));

    // get informed about meta data changes
    connect(&signal, SIGNAL(sigMetaDataChanged(Kwave::MetaDataList)),
            this, SLOT(metaDataChanged(Kwave::MetaDataList)));

    // transport the image calculated in a background thread
    // through the signal/slot mechanism
    connect(this, SIGNAL(newImage(QImage)),
            this, SLOT(setImage(QImage)),
            Qt::AutoConnection);

    setMouseTracking(true);
}

//***************************************************************************
Kwave::OverViewWidget::~OverViewWidget()
{
    // check: start() must be called from the GUI thread only!
    Q_ASSERT(this->thread() == QThread::currentThread());
    Q_ASSERT(this->thread() == qApp->thread());

    m_repaint_timer.stop();
    m_worker_thread.wait(/* 100 * REPAINT_INTERVAL */);
}

//***************************************************************************
void Kwave::OverViewWidget::mouseMoveEvent(QMouseEvent *e)
{
    mousePressEvent(e);
}

//***************************************************************************
void Kwave::OverViewWidget::mousePressEvent(QMouseEvent *e)
{
    Q_ASSERT(e);
    if (!e) return;
    if (e->buttons() != Qt::LeftButton) {
        e->ignore();
        return;
    }

    // move the clicked position to the center of the viewport
    sample_index_t offset = pixels2offset(e->position().toPoint().x());
    if (offset != m_last_offset) {
        sample_index_t half = (m_view_width / 2);
        offset = (offset > half) ? (offset - half) : 0;
        m_last_offset = offset;
        emit valueChanged(offset);
    }
    e->accept();
}

//***************************************************************************
void Kwave::OverViewWidget::mouseDoubleClickEvent(QMouseEvent *e)
{
    Q_ASSERT(e);
    if (!e) return;
    if (e->button() != Qt::LeftButton) {
        e->ignore();
        return;
    }

    // move the clicked position to the center of the viewport
    sample_index_t offset = pixels2offset(e->position().toPoint().x());
    if (offset != m_last_offset) {
        m_last_offset = offset;
        emit valueChanged(offset);
    }

    if (e->modifiers() == Qt::NoModifier) {
        // double click without shift => zoom in
        emit sigCommand(_("view:zoom_in()"));
    } else if (e->modifiers() == Qt::ShiftModifier) {
        // double click with shift => zoom out
        emit sigCommand(_("view:zoom_out()"));
    }

    e->accept();
}

//***************************************************************************
sample_index_t Kwave::OverViewWidget::pixels2offset(int pixels)
{
    int width = this->width();
    if (!width) return 0;

    if (pixels < 0) pixels = 0;
    double zoom = static_cast<double>(m_signal_length - 1) /
                  static_cast<double>(width - 1);
    sample_index_t offset = static_cast<sample_index_t>(rint(
                            static_cast<double>(pixels) * zoom));
    return offset;
}

//***************************************************************************
void Kwave::OverViewWidget::setRange(sample_index_t offset,
                                     sample_index_t viewport,
                                     sample_index_t total)
{
    m_view_offset   = offset;
    m_view_width    = viewport;
    m_signal_length = total;

    overviewChanged();
}

//***************************************************************************
void Kwave::OverViewWidget::setSelection(sample_index_t offset,
                                         sample_index_t length)
{
    m_selection_start  = offset;
    m_selection_length = length;

    overviewChanged();
}

//***************************************************************************
void Kwave::OverViewWidget::resizeEvent(QResizeEvent *)
{
    refreshBitmap();
}

//***************************************************************************
QSize Kwave::OverViewWidget::minimumSize() const
{
    return QSize(30, 30);
}

//***************************************************************************
QSize Kwave::OverViewWidget::sizeHint() const
{
    return minimumSize();
}

//***************************************************************************
void Kwave::OverViewWidget::overviewChanged()
{
    // check: start() must be called from the GUI thread only!
    Q_ASSERT(this->thread() == QThread::currentThread());
    Q_ASSERT(this->thread() == qApp->thread());

    if (m_repaint_timer.isActive()) {
        // repainting is inhibited -> wait until the
        // repaint timer is elapsed
        return;
    } else {
        // repaint once and once later...
        refreshBitmap();

        // start the repaint timer
        m_repaint_timer.setSingleShot(true);
        m_repaint_timer.start(REPAINT_INTERVAL);
    }
}

//***************************************************************************
void Kwave::OverViewWidget::metaDataChanged(Kwave::MetaDataList meta)
{
    // check: start() must be called from the GUI thread only!
    Q_ASSERT(this->thread() == QThread::currentThread());
    Q_ASSERT(this->thread() == qApp->thread());

    m_labels = Kwave::LabelList(meta);

    // only re-start the repaint timer, this hides some GUI update artifacts
    if (!m_repaint_timer.isActive()) {
        m_repaint_timer.stop();
        m_repaint_timer.setSingleShot(true);
        m_repaint_timer.start(REPAINT_INTERVAL);
    }
}

//***************************************************************************
void Kwave::OverViewWidget::showCursor(sample_index_t pos)
{
    // check: start() must be called from the GUI thread only!
    Q_ASSERT(this->thread() == QThread::currentThread());
    Q_ASSERT(this->thread() == qApp->thread());

    const sample_index_t old_pos = m_cursor_position;
    const sample_index_t new_pos = pos;

    if (new_pos == old_pos) return; // no change
    m_cursor_position = new_pos;

    if (qMax(old_pos, new_pos) != SAMPLE_INDEX_MAX) {
        // check for change in pixel units
        sample_index_t length = m_signal_length;
        if (m_view_offset + m_view_width > m_signal_length) {
            // showing deleted space after signal
            length = m_view_offset + m_view_width;
        }
        if (!length) return;
        const double scale = static_cast<double>(width()) /
                            static_cast<double>(length);
        const int old_pixel_pos = Kwave::toInt(
            static_cast<double>(old_pos) * scale);
        const int new_pixel_pos = Kwave::toInt(
            static_cast<double>(new_pos) * scale);
        if (old_pixel_pos == new_pixel_pos) return;
    }

    // some update is required, start the repaint timer in quick mode
    if (!m_repaint_timer.isActive() ||
        (m_repaint_timer.interval() != REPAINT_INTERVAL_FAST))
    {
        m_repaint_timer.stop();
        m_repaint_timer.setSingleShot(true);
        m_repaint_timer.start(REPAINT_INTERVAL_FAST);
    }
}

//***************************************************************************
void Kwave::OverViewWidget::drawMark(QPainter &p, int x, int height,
                                     QColor color)
{
    QPolygon mark;
    const int w = 5;
    const int y = (height - 1);

    p.setCompositionMode(QPainter::CompositionMode_SourceOver);
    color.setAlpha(100);
    p.setBrush(QBrush(color));
    p.setPen(QPen(Qt::black));

    mark.setPoints(3, x - w, 0, x + w, 0, x, w);     // upper
    p.drawPolygon(mark);
    mark.setPoints(3, x - w, y, x + w, y, x, y - w); // lower
    p.drawPolygon(mark);
}

//***************************************************************************
void Kwave::OverViewWidget::refreshBitmap()
{
    // check: start() must be called from the GUI thread only!
    Q_ASSERT(this->thread() == QThread::currentThread());
    Q_ASSERT(this->thread() == qApp->thread());

    if (m_worker_thread.isRunning()) {
        // (re)start the repaint timer if the worker thread is still
        // running, try again later...
        m_repaint_timer.stop();
        m_repaint_timer.setSingleShot(true);
        m_repaint_timer.start(REPAINT_INTERVAL);
    } else {
        // start the calculation in a background thread
        m_worker_thread.start(QThread::IdlePriority);
    }
}

//***************************************************************************
void Kwave::OverViewWidget::calculateBitmap()
{
    sample_index_t length = m_signal_length;
    if (m_view_offset + m_view_width > m_signal_length) {
        // showing deleted space after signal
        length = m_view_offset + m_view_width;
    }

    int width  = this->width();
    int height = this->height();
    if (!width || !height || !m_view_width || !length)
        return;

    const double scale = static_cast<double>(width) /
                         static_cast<double>(length);
    const int bitmap_width = Kwave::toInt(
        static_cast<double>(m_signal_length) * scale);

    // let the bitmap be updated from the cache
    QImage bitmap = m_cache.getOverView(bitmap_width, height,
        BAR_FOREGROUND ,BAR_BACKGROUND);

    // draw the bitmap (converted to QImage)
    QImage image(width, height, QImage::Format_ARGB32_Premultiplied);
    QPainter p;
    p.begin(&image);
    p.fillRect(rect(), BAR_BACKGROUND);
    p.drawImage(0, 0, bitmap);

    // highlight the selection
    if ((m_selection_length > 1) && m_signal_length)
    {
        int first = Kwave::toInt(
            static_cast<double>(m_selection_start) * scale);
        int len   = Kwave::toInt(
            static_cast<double>(m_selection_length) * scale);
        if (len < 1) len = 1;

        // draw the selection as rectangle
        QBrush hilight(Qt::yellow);
        hilight.setStyle(Qt::SolidPattern);
        p.setBrush(hilight);
        p.setPen(QPen(Qt::yellow));
        p.setCompositionMode(QPainter::CompositionMode_Exclusion);
        p.drawRect(first, 0, len, height);

        // marks at start and end of selection
        drawMark(p, first, height, Qt::blue);
        drawMark(p, first + len, height, Qt::blue);
    }

    // draw labels
    int last_label_pos = width + 1;
    foreach (const Kwave::Label &label, m_labels) {
        sample_index_t pos = label.pos();
        int x = Kwave::toInt(static_cast<double>(pos) * scale);

        // position must differ from the last one, otherwise we
        // would wipe out the last one with XOR mode
        if (x == last_label_pos) continue;

        // draw a line for each label
        p.setPen(QPen(Qt::cyan));
        p.setCompositionMode(QPainter::CompositionMode_Exclusion);
        p.drawLine(x, 0, x, height);
        drawMark(p, x, height, Qt::cyan);

        last_label_pos = x;
    }

    // draw playback position
    if (m_cursor_position != SAMPLE_INDEX_MAX) {
        const sample_index_t pos = m_cursor_position;
        int x = Kwave::toInt(static_cast<double>(pos) * scale);

        // draw a line for the playback position
        QPen pen(Qt::yellow);
        pen.setWidth(5);
        p.setPen(pen);
        p.setCompositionMode(QPainter::CompositionMode_Exclusion);
        p.drawLine(x, 0, x, height);
        drawMark(p, x, height, Qt::cyan);
    }

    // dim the currently invisible parts
    if ((m_view_offset > 0) || (m_view_offset + m_view_width < m_signal_length))
    {
        QColor color = BAR_BACKGROUND;
        color.setAlpha(128);
        QBrush out_of_view(color);
        out_of_view.setStyle(Qt::SolidPattern);
        p.setBrush(out_of_view);
        p.setPen(QPen(color));
        p.setCompositionMode(QPainter::CompositionMode_SourceOver);

        if (m_view_offset > 0) {
            int x = Kwave::toInt(static_cast<double>(m_view_offset) * scale);
            p.drawRect(0, 0, x, height);
        }

        if (m_view_offset + m_view_width < m_signal_length) {
            int x = Kwave::toInt(
                static_cast<double>(m_view_offset + m_view_width) * scale);
            p.drawRect(x, 0, width - x, height);
        }
    }

    p.end();

    // update the widget with the overview
    emit newImage(image);
}

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

#include "moc_OverViewWidget.cpp"