File: SignalWidget.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 (706 lines) | stat: -rw-r--r-- 24,570 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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/***************************************************************************
             SignalWidget.cpp  -  Widget for displaying the signal
                             -------------------
    begin                : 1999
    copyright            : (C) 1999 by Martin Wilz
    email                : Martin Wilz <mwilz@ernie.mi.uni-koeln.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 <errno.h>
#include <math.h>
#include <stdlib.h>

#include <new>

#include <QApplication>
#include <QBitmap>
#include <QContextMenuEvent>
#include <QDragEnterEvent>
#include <QDragLeaveEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QEvent>
#include <QFrame>
#include <QIcon>
#include <QMenu>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QResizeEvent>
#include <QTime>
#include <QToolTip>

#include <KCursor>
#include <KLocalizedString>

#include "libkwave/ClipBoard.h"
#include "libkwave/LabelList.h"
#include "libkwave/Track.h"
#include "libkwave/Utils.h"

#include "libgui/SignalWidget.h"
#include "libgui/TrackPixmap.h"
#include "libgui/TrackView.h"
// #include "libgui/ShortcutWrapper.h"

// /** table of keyboard shortcuts 0...9 */
// static const int tbl_keys[10] = {
//     Qt::Key_1,
//     Qt::Key_2,
//     Qt::Key_3,
//     Qt::Key_4,
//     Qt::Key_5,
//     Qt::Key_6,
//     Qt::Key_7,
//     Qt::Key_8,
//     Qt::Key_9,
//     Qt::Key_0
// };

/** vertical zoom factor: minimum value */
#define VERTICAL_ZOOM_MIN 1.0

/** vertical zoom factor: maximum value */
#define VERTICAL_ZOOM_MAX 100.0

/** vertical zoom factor: increment/decrement factor */
#define VERTICAL_ZOOM_STEP_FACTOR 1.5

/** interval for limiting the number of repaints per second [ms] */
#define REPAINT_INTERVAL 50

//***************************************************************************
Kwave::SignalWidget::SignalWidget(QWidget *parent,
                                  Kwave::SignalManager *signal_manager,
                                  QVBoxLayout *upper_dock,
                                  QVBoxLayout *lower_dock)
    :QWidget(parent),
     m_signal_manager(signal_manager),
     m_views(),
     m_layout(this),
     m_upper_dock(upper_dock),
     m_lower_dock(lower_dock),
     m_offset(0),
     m_zoom(0.0),
     m_vertical_zoom(1.0),
     m_repaint_timer()
{
//    qDebug("SignalWidget::SignalWidget()");

    // connect to the signal manager's signals
    Kwave::SignalManager *sig = m_signal_manager;

    connect(sig,  SIGNAL(sigTrackInserted(uint,Kwave::Track*)),
            this, SLOT(slotTrackInserted(uint,Kwave::Track*)));
    connect(sig,  SIGNAL(sigTrackDeleted(uint,Kwave::Track*)),
            this, SLOT(slotTrackDeleted(uint,Kwave::Track*)));

    // use a timer for limiting the repaint rate
    m_repaint_timer.setSingleShot(true);
    connect(&m_repaint_timer, SIGNAL(timeout()),
            this,             SLOT(repaintTimerElapsed()));

//     // -- accelerator keys for 1...9 --
//     for (int i = 0; i < 10; i++) {
//      Kwave::ShortcutWrapper *shortcut =
//          new(std::nothrow) Kwave::ShortcutWrapper(this, tbl_keys[i], i);
//      connect(shortcut, SIGNAL(activated(int)),
//              this, SLOT(parseKey(int)));
//     }

    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    m_layout.setColumnStretch(0,   0);
    m_layout.setColumnStretch(1, 100);
    m_layout.setContentsMargins(0, 0, 0, 0);
    m_layout.setVerticalSpacing(3);
    setLayout(&m_layout);
    m_layout.activate();
}

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

//***************************************************************************
void Kwave::SignalWidget::setZoomAndOffset(double zoom, sample_index_t offset)
{
    Q_ASSERT(zoom >= 0.0);
    foreach (QPointer<Kwave::SignalView> view, m_views)
        view->setZoomAndOffset(zoom, offset);
}

//***************************************************************************
void Kwave::SignalWidget::forwardCommand(const QString &command)
{
    emit sigCommand(command);
}

//***************************************************************************
void Kwave::SignalWidget::requestRepaint(Kwave::SignalView *view)
{
    // add the view to the repaint queue (if not already there)
    if (!m_repaint_queue.contains(view))
        m_repaint_queue.enqueue(view);

    if (!m_repaint_timer.isActive()) {
        // start the repaint timer
        m_repaint_timer.start(REPAINT_INTERVAL);
    }
    // else: repainting is inhibited -> wait until the
    // repaint timer is elapsed
}

//***************************************************************************
void Kwave::SignalWidget::repaintTimerElapsed()
{
    while (!m_repaint_queue.isEmpty())
    {
        Kwave::SignalView *view = m_repaint_queue.dequeue();
        if (!view) continue;
        view->refresh();
    }
}

//***************************************************************************
void Kwave::SignalWidget::contextMenuEvent(QContextMenuEvent *e)
{
    Q_ASSERT(e);

    bool have_signal = m_signal_manager && !m_signal_manager->isEmpty();
    if (!have_signal)return;
    bool have_selection = (m_signal_manager->selection().length() > 1);
    bool have_labels =
        !(Kwave::LabelList(m_signal_manager->metaData()).isEmpty());

    QMenu *context_menu = new(std::nothrow) QMenu(this);
    Q_ASSERT(context_menu);
    if (!context_menu) return;

    /* menu items common to all cases */

    // undo
    QAction *action;
    action = context_menu->addAction(
        QIcon::fromTheme(_("edit-undo")),
        i18n("&Undo"),
        QKeySequence::StandardKey::Undo,
        this,
        SLOT(contextMenuEditUndo()));
    Q_ASSERT(action);
    if (!action) return;
    if (!m_signal_manager->canUndo())
        action->setEnabled(false);

    // redo
    action = context_menu->addAction(
        QIcon::fromTheme(_("edit-redo")),
        i18n("&Redo"),
        QKeySequence::StandardKey::Redo,
        this,
        SLOT(contextMenuEditRedo()));
    Q_ASSERT(action);
    if (!action) return;
    if (!m_signal_manager->canRedo())
        action->setEnabled(false);
    context_menu->addSeparator();

    // cut/copy/paste
    QAction *action_cut = context_menu->addAction(
        QIcon::fromTheme(_("edit-cut")),
        i18n("Cu&t"),
        QKeySequence::StandardKey::Cut,
        this,
        SLOT(contextMenuEditCut()));
    QAction *action_copy = context_menu->addAction(
        QIcon::fromTheme(_("edit-copy")),
        i18n("&Copy"),
        QKeySequence::StandardKey::Copy,
        this,
        SLOT(contextMenuEditCopy()));
    QAction *action_paste = context_menu->addAction(
        QIcon::fromTheme(_("edit-paste")),
        i18n("&Paste"),
        QKeySequence::StandardKey::Paste,
        this,
        SLOT(contextMenuEditPaste()));
    context_menu->addSeparator();
    if (action_cut)   action_cut->setEnabled(have_selection);
    if (action_copy)  action_copy->setEnabled(have_selection);
    if (action_paste)
        action_paste->setEnabled(!Kwave::ClipBoard::instance().isEmpty());

    int mouse_x = mapFromGlobal(e->globalPos()).x();
    int mouse_y = mapFromGlobal(e->globalPos()).y();
    if (mouse_x < 0) mouse_x = 0;
    if (mouse_y < 0) mouse_y = 0;
    if (mouse_x >= width())   mouse_x = width()   - 1;
    if (mouse_y >= height())  mouse_y = height()  - 1;


    QMenu *submenu_select = context_menu->addMenu(i18n("Selection"));
    Q_ASSERT(submenu_select);
    if (!submenu_select) return;

    // Selection / &Save
    QAction *action_select_save = submenu_select->addAction(
        QIcon::fromTheme(_("document-save")),
        i18n("&Save..."), this, SLOT(contextMenuSaveSelection()));
    Q_ASSERT(action_select_save);
    if (!action_select_save) return;
    action_select_save->setEnabled(have_selection);

    // Selection / &Expand to labels
    QAction *action_select_expand_to_labels = submenu_select->addAction(
        i18n("&Expand to Labels"),
        Qt::Key_E,
        this,
        SLOT(contextMenuSelectionExpandToLabels()));
    Q_ASSERT(action_select_expand_to_labels);
    if (!action_select_expand_to_labels) return;
    action_select_expand_to_labels->setEnabled(have_labels);

    // Selection / to next labels
    QAction *action_select_next_labels = submenu_select->addAction(
        i18n("To Next Labels"),
        Qt::SHIFT | Qt::CTRL | Qt::Key_N,
        this,
        SLOT(contextMenuSelectionNextLabels()));
    Q_ASSERT(action_select_next_labels);
    if (!action_select_next_labels) return;
    action_select_next_labels->setEnabled(have_labels);

    // Selection / to previous labels
    QAction *action_select_prev_labels = submenu_select->addAction(
        i18n("To Previous Labels"),
        Qt::SHIFT | Qt::CTRL | Qt::Key_P,
        this,
        SLOT(contextMenuSelectionPrevLabels()));
    Q_ASSERT(action_select_prev_labels);
    if (!action_select_prev_labels) return;
    action_select_prev_labels->setEnabled(have_labels);

    // find out whether there was a click within a signal view
    QSharedPointer<Kwave::ViewItem> item(nullptr);
    foreach (QPointer<Kwave::SignalView> view, m_views) {
        // map the rect of the view to our coordinate system
        const QRect view_rect = QRect(
            view->mapToParent(view->rect().topLeft()),
            view->mapToParent(view->rect().bottomRight()));

        // check: mouse click was into that view?
        if (view_rect.contains(mouse_x, mouse_y)) {
            // map mouse click position to coordinate system of the view
            QPoint pos = view->mapFromParent(QPoint(mouse_x, mouse_y));

            // give the view the chance to extend the context menu
            view->handleContextMenu(pos, context_menu);

            // try to find a view item at these coordinates
            item = view->findItem(pos);

            // if found, give the item the chance to extend the context menu
            if (!item.isNull()) {
                connect(item.data(), SIGNAL(sigCommand(QString)),
                        this,        SLOT(forwardCommand(QString)));
                item->appendContextMenu(context_menu);
            }

            // we process only one view, views cannot overlap!
            break;
        }
    }

    context_menu->exec(QCursor::pos());
    delete context_menu;
}

//***************************************************************************
void Kwave::SignalWidget::wheelEvent(QWheelEvent *event)
{
    if (!event) return;

    // we currently are only interested in <Alt> + <WheelUp/Down>
    if (event->modifiers() != Qt::AltModifier) {
        event->ignore();
        return;
    }

    if (event->angleDelta().ry() > 0) {
        // zoom in
        setVerticalZoom(m_vertical_zoom  * VERTICAL_ZOOM_STEP_FACTOR);
        event->accept();
    } else if (event->angleDelta().ry() < 0) {
        // zoom out
        setVerticalZoom(m_vertical_zoom  / VERTICAL_ZOOM_STEP_FACTOR);
        event->accept();
    } else {
        // no change
        event->ignore();
    }
}

//***************************************************************************
void Kwave::SignalWidget::setVerticalZoom(double zoom)
{
    if (zoom > VERTICAL_ZOOM_MAX) zoom = VERTICAL_ZOOM_MAX;
    if (zoom < VERTICAL_ZOOM_MIN) zoom = VERTICAL_ZOOM_MIN;
    if (qFuzzyCompare(zoom, m_vertical_zoom)) return; // no change

    // take over the zoom factor
    m_vertical_zoom = zoom;

    // propagate the zoom to all views
    foreach (QPointer<Kwave::SignalView> view, m_views)
        if (view) view->setVerticalZoom(m_vertical_zoom);

    // get back the maximum zoom set by the views
    foreach (QPointer<Kwave::SignalView> view, m_views)
        if (view && view->verticalZoom() > m_vertical_zoom)
            m_vertical_zoom = view->verticalZoom();
}

//***************************************************************************
int Kwave::SignalWidget::mapToViewPort(const QPoint &pos) const
{
    if (m_views.isEmpty()) return mapFromGlobal(pos).x(); // if empty
    return mapFromGlobal(pos).x() - m_layout.cellRect(0, 1).left();
}

//***************************************************************************
int Kwave::SignalWidget::visibleWidth() const
{
    if (m_views.isEmpty()) return width(); // if empty
    return m_layout.cellRect(0, 1).width();
}

//***************************************************************************
void Kwave::SignalWidget::insertRow(int index, Kwave::SignalView *view,
                                    QWidget *controls)
{
    const int rows = m_layout.rowCount();
    const int cols = m_layout.columnCount();

    // update the layout: move all items from the index on to the next row
    for (int row = rows; row > index; row--) {
        for (int col = 0; col < cols; col++) {
            QLayoutItem *item = m_layout.itemAtPosition(row - 1, col);
            if (item) {
                m_layout.removeItem(item);
                m_layout.addItem(item, row, col);
            }
        }
    }

    // add the widget to the layout
    m_layout.addWidget(view, index, 1);

    if (controls) {
        // add the controls to the layout
        m_layout.addWidget(controls, index, 0);

        // associate the controls to the view, so that when the view
        // gets removed/deleted, the controls get removed as well
        view->addSibling(controls);

        m_layout.activate();
        // NOTE: QLayout::activate() does not really activate the layout
        //       synchronously as expected. Instead it posts an event to
        //       invalidate the layout of it's parent widget.
        //       Unfortunately we need reliable geometry information
        //       already *now*, which forces us to do the event loop
        //       right here.
        qApp->processEvents(QEventLoop::ExcludeUserInputEvents |
                            QEventLoop::ExcludeSocketNotifiers);
    }
}

//***************************************************************************
void Kwave::SignalWidget::deleteRow(int index)
{
    const int rows = m_layout.rowCount();
    const int cols = m_layout.columnCount();

    if (index >= rows)
        return;

    // update the layout: move all items from this row to the previous
    for (int row = index; row < (rows - 1); row++) {
        for (int col = 0; col < cols; col++) {
            QLayoutItem *item = m_layout.itemAtPosition(row + 1, col);
            if (item) {
                m_layout.removeItem(item);
                m_layout.addItem(item, row, col);
            }
        }
    }
}

//***************************************************************************
void Kwave::SignalWidget::insertView(Kwave::SignalView *view,
                                     QWidget *controls)
{
    Q_ASSERT(m_upper_dock);
    Q_ASSERT(m_lower_dock);
    Q_ASSERT(view);
    if (!m_upper_dock || !m_lower_dock) return;
    if (!view) return;

    // set initial vertical zoom
    view->setVerticalZoom(m_vertical_zoom);

    // find the proper row to insert the track view
    int index = 0;
    int track = (view) ? view->track() : -1;
    const Kwave::SignalView::Location where = view->preferredLocation();
    switch (where) {
        case Kwave::SignalView::UpperDockTop: {
            // upper dock area, top
            index = 0;
            m_upper_dock->insertWidget(0, view);
            Q_ASSERT(!controls);
            break;
        }
        case Kwave::SignalView::UpperDockBottom: {
            // upper dock area, bottom
            index = m_upper_dock->count();
            m_upper_dock->addWidget(view);
            Q_ASSERT(!controls);
            break;
        }
        case Kwave::SignalView::Top: {
            // central layout, above all others
            index = m_upper_dock->count();
            int row = 0;
            insertRow(row, view, controls);
            break;
        }
        case Kwave::SignalView::AboveTrackTop: {
            // above the corresponding track, start of group
            index = m_upper_dock->count();
            int row = 0;
            for (;index < m_views.count(); ++row, ++index) {
                if (m_views[index]->track() >= track) break; // reached top
                if (m_views[index]->preferredLocation() >=
                        Kwave::SignalView::Bottom) break;
            }
            insertRow(row, view, controls);
            break;
        }
        case Kwave::SignalView::AboveTrackBottom: {
            // above the corresponding track, end of group
            index = m_upper_dock->count();
            int row = 0;
            for (;index < m_views.count(); ++row, ++index) {
                if (m_views[index]->track() < track) continue; // too early
                if (m_views[index]->track() != track) break; // next track
                if (m_views[index]->preferredLocation() !=
                        Kwave::SignalView::AboveTrackTop) break;
            }
            insertRow(row, view, controls);
            break;
        }
        case Kwave::SignalView::BelowTrackTop: {
            // below the corresponding track, start of group
            index = m_upper_dock->count();
            int row = 0;
            for (;index < m_views.count(); ++row, ++index) {
                if (m_views[index]->track() < track) continue; // too early
                if (m_views[index]->track() != track) break; // next track
                if (m_views[index]->preferredLocation() >=
                        Kwave::SignalView::BelowTrackTop) break;
            }
            insertRow(row, view, controls);
            break;
        }
        case Kwave::SignalView::BelowTrackBottom: {
            // below the corresponding track, end of group
            index = m_upper_dock->count();
            int row = 0;
            for (;index < m_views.count(); ++row, ++index) {
                if (m_views[index]->track() < track) continue; // too early
                if (m_views[index]->track() != track) break; // next track
                if (m_views[index]->preferredLocation() >=
                        Kwave::SignalView::Bottom) break;
            }
            insertRow(row, view, controls);
            break;
        }
        case Kwave::SignalView::Bottom: {
            // below all others
            int row = m_layout.rowCount();
            index = m_upper_dock->count() + row;
            insertRow(row, view, controls);
            break;
        }
        case Kwave::SignalView::LowerDockTop: {
            // lower dock area, top
            index = m_upper_dock->count() + m_layout.rowCount();
            m_lower_dock->insertWidget(0, view);
            Q_ASSERT(!controls);
            break;
        }
        case Kwave::SignalView::LowerDockBottom:
            // lower dock area, bottom
            index = m_upper_dock->count() + m_layout.rowCount() +
                    m_lower_dock->count();
            m_lower_dock->addWidget(view);
            Q_ASSERT(!controls);
            break;
        DEFAULT_IMPOSSIBLE;
    }

    // insert the view into the list of views
    Q_ASSERT(index >= 0);
    Q_ASSERT(index < m_upper_dock->count() + m_layout.rowCount() +
             m_lower_dock->count());
    m_views.insert(index, view);

    // initially set the current view info
    view->setZoomAndOffset(m_zoom, m_offset);

    // connect all signals
    connect(view,       SIGNAL(sigCommand(QString)),
            this,       SIGNAL(sigCommand(QString)),
            Qt::QueuedConnection);

    connect(view,       SIGNAL(sigNeedRepaint(Kwave::SignalView*)),
            this,       SLOT(requestRepaint(Kwave::SignalView*)));

    connect(view,       SIGNAL(contentSizeChanged()),
            this,       SLOT(updateMinimumHeight()));
    connect(view,       SIGNAL(destroyed(QObject*)),
            this,       SLOT(updateMinimumHeight()));
    connect(view,       SIGNAL(sigCursorChanged(sample_index_t)),
            this,       SIGNAL(sigCursorChanged(sample_index_t)));
    connect(this,       SIGNAL(sigCursorChanged(sample_index_t)),
            view,       SLOT(showCursor(sample_index_t)));

    updateMinimumHeight();
}

//***************************************************************************
void Kwave::SignalWidget::slotTrackInserted(unsigned int index,
                                            Kwave::Track *track)
{
    Q_ASSERT(track);
    if (!track) return;

    // create a container widget for the track controls
    QWidget *controls = new(std::nothrow) QWidget(nullptr);
    Q_ASSERT(controls);
    if (!controls) return;

    // create a new view for the track's signal
    Kwave::SignalView *new_view = new(std::nothrow) Kwave::TrackView(
        this, controls, m_signal_manager, track);
    Q_ASSERT(new_view);
    if (!new_view) {
        delete controls;
        return;
    }

    // loop over all views and adjust the track index of the following ones
    foreach (QPointer<Kwave::SignalView> view, m_views) {
        if (view->track() >= Kwave::toInt(index))
            view->setTrack(view->track() + 1);
    }

    // assign the view to the new track
    new_view->setTrack(index);

    insertView(new_view, controls);
}

//***************************************************************************
void Kwave::SignalWidget::slotTrackDeleted(unsigned int index,
                                           Kwave::Track *track)
{
    Q_UNUSED(track)

    // loop over all views, delete those that are bound to this track
    // and adjust the index of the following ones
    bool empty = true;
    QMutableListIterator<QPointer<Kwave::SignalView> > it(m_views);
    while (it.hasNext()) {
        Kwave::SignalView *view = it.next();
        if (view->track() == Kwave::toInt(index)) {
            it.remove();
            delete view;
        } else if (view->track() > Kwave::toInt(index)) {
            view->setTrack(view->track() - 1);
            empty = false;
        } else if (view->track() != -1) {
            empty = false;
        }
    }

    // find out if there are any empty rows in the grid now
    const int rows = m_layout.rowCount();
    const int cols = m_layout.columnCount();
    for (int row = 0; row < rows; row++) {
        bool row_is_empty = true;
        for (int col = 0; col < cols; col++) {
            QLayoutItem *item = m_layout.itemAtPosition(row, col);
            if (item) {
                row_is_empty = false;
                break;
            }
        }
        if (row_is_empty) deleteRow(row);
    }

    // if there are only views with track() == -1, we are empty,
    // in that case delete the rest (all views)
    if (empty) {
        while (!m_views.isEmpty())
            delete m_views.takeFirst();
    }
}

//***************************************************************************
void Kwave::SignalWidget::updateMinimumHeight()
{
    int height = 0;

    const int rows = m_layout.rowCount();
    const int cols = m_layout.columnCount();
    for (int row = 0; row < rows; row++) {
        int h = 0;
        for (int col = 0; col < cols; col++) {
            QLayoutItem *item = m_layout.itemAtPosition(row, col);
            QWidget *widget = (item) ? item->widget() : nullptr;
            if (widget) {
                int min_height = widget->minimumSize().height();
                if (min_height > h) h = min_height;
            }
        }
        height += h;
    }
    if (rows > 1) height += (rows - 1) * m_layout.verticalSpacing();

    setMinimumHeight(height);
}

//***************************************************************************
// void Kwave::SignalWidget::parseKey(int key)
// {
//     if ((key < 0) || (key >= m_lamps.count()))
//      return;
//     if (m_lamps.at(key)) m_lamps.at(key)->nextState();
// }

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

#include "moc_SignalWidget.cpp"