File: tracktab.cpp

package info (click to toggle)
tiatracker 1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,664 kB
  • sloc: cpp: 8,875; asm: 664; makefile: 8
file content (655 lines) | stat: -rw-r--r-- 27,185 bytes parent folder | download | duplicates (2)
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
/* TIATracker, (c) 2016 Andre "Kylearan" Wichmann.
 * Website: https://bitbucket.org/kylearan/tiatracker
 * Email: andre.wichmann@gmx.de
 * See the file "license.txt" for information on usage and redistribution
 * of this file.
 */

#include "tracktab.h"
#include <QPushButton>
#include <QLayout>
#include <QButtonGroup>
#include <iostream>
#include "instrumentselector.h"
#include "timeline.h"
#include "patterneditor.h"
#include <QLabel>
#include <QSpinBox>
#include "setslidedialog.h"
#include <QMessageBox>
#include "setfrequencydialog.h"
#include "instrumentstab.h"
#include "renamepatterndialog.h"
#include "setgotodialog.h"
#include "mainwindow.h"
#include "insertpatterndialog.h"
#include "createpatterndialog.h"
#include <qcheckbox.h>


TrackTab::TrackTab(QWidget *parent) : QWidget(parent)
{
    // Global
    addShortcut(&actionMoveUp, "CursorUp");
    addShortcut(&actionMoveDown, "CursorDown");
    addShortcut(&actionLeftChannel, "CursorLeftChannel");
    addShortcut(&actionRightChannel, "CursorRightChannel");
    addShortcut(&actionSwitchChannel, "CursorSwitchChannel");
    addShortcut(&actionFirstRow, "CursorFirstRow");
    addShortcut(&actionLastRow, "CursorLastRow");
    addShortcut(&actionNextPattern, "CursorNextPattern");
    addShortcut(&actionPreviousPattern, "CursorPreviousPattern");

    // Pattern
    addShortcut(&actionInsertPatternBefore, "PatternInsertBefore");
    addShortcut(&actionInsertPatternAfter, "PatternInsertAfter");
    patternContextMenu.addAction(&actionInsertPatternBefore);
    patternContextMenu.addAction(&actionInsertPatternAfter);
    addShortcut(&actionMovePatternUp, "PatternMoveUp");
    patternContextMenu.addAction(&actionMovePatternUp);
    addShortcut(&actionMovePatternDown, "PatternMoveDown");
    patternContextMenu.addAction(&actionMovePatternDown);
    addShortcut(&actionDuplicatePattern, "PatternDuplicate");
    patternContextMenu.addAction(&actionDuplicatePattern);
    addShortcut(&actionRemovePattern, "PatternRemove");
    patternContextMenu.addAction(&actionRemovePattern);
    addShortcut(&actionRenamePattern, "PatternRename");
    patternContextMenu.addAction(&actionRenamePattern);
    patternContextMenu.addSeparator();
    addShortcut(&actionSetGoto, "PatternSetGoto");
    patternContextMenu.addAction(&actionSetGoto);
    addShortcut(&actionRemoveGoto, "PatternRemoveGoto");
    patternContextMenu.addAction(&actionRemoveGoto);
    patternContextMenu.addAction(&actionSetStartPattern);
    // Channel
    addShortcut(&actionPause, "NotePause");
    channelContextMenu.addAction(&actionPause);
    addShortcut(&actionHold, "NoteHold");
    channelContextMenu.addAction(&actionHold);
    addShortcut(&actionSlide, "NoteSlide");
    channelContextMenu.addAction(&actionSlide);
    addShortcut(&actionSetFrequency, "NoteFrequency");
    channelContextMenu.addAction(&actionSetFrequency);
    channelContextMenu.addSeparator();
    addShortcut(&actionInsertRowBefore, "RowInsert");
    channelContextMenu.addAction(&actionInsertRowBefore);
    channelContextMenu.addAction(&actionInsertRowAfter);
    addShortcut(&actionDeleteRow, "RowDelete");
    channelContextMenu.addAction(&actionDeleteRow);
    channelContextMenu.addSeparator();
    actionMuteChannel.setCheckable(true);
    addShortcut(&actionMuteChannel, "ChannelToggleMute");
    channelContextMenu.addAction(&actionMuteChannel);
}

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

void TrackTab::registerTrack(Track::Track *newTrack) {
    pTrack = newTrack;
}

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

void TrackTab::registerPitchGuide(TiaSound::PitchGuide *newGuide) {
    pPitchGuide = newGuide;
}

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

void TrackTab::registerPlayer(Emulation::Player *newPlayer) {
    pPlayer = newPlayer;
}

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

void TrackTab::initTrackTab() {
    pTrack->updateFirstNoteNumbers();

    InstrumentSelector *insSel = findChild<InstrumentSelector *>("trackInstrumentSelector");
    insSel->registerTrack(pTrack);
    insSel->initSelector();
    Timeline *timeline = findChild<Timeline *>("trackTimeline");
    timeline->registerTrack(pTrack);
    PatternEditor *editor = findChild<PatternEditor *>("trackEditor");
    editor->registerTrack(pTrack);
    editor->registerPitchGuide(pPitchGuide);
    editor->registerMuteAction(&actionMuteChannel);
    editor->registerInstrumentSelector(insSel);

    // Gloabl actions
    QObject::connect(&actionMoveUp, SIGNAL(triggered(bool)), editor, SLOT(moveUp(bool)));
    QObject::connect(&actionMoveDown, SIGNAL(triggered(bool)), editor, SLOT(moveDown(bool)));
    QObject::connect(&actionLeftChannel, SIGNAL(triggered(bool)), editor, SLOT(moveLeft(bool)));
    QObject::connect(&actionRightChannel, SIGNAL(triggered(bool)), editor, SLOT(moveRight(bool)));
    QObject::connect(&actionSwitchChannel, SIGNAL(triggered(bool)), editor, SLOT(switchChannel(bool)));
    QObject::connect(&actionFirstRow, SIGNAL(triggered(bool)), editor, SLOT(gotoFirstRow(bool)));
    QObject::connect(&actionLastRow, SIGNAL(triggered(bool)), editor, SLOT(gotoLastRow(bool)));
    QObject::connect(&actionNextPattern, SIGNAL(triggered(bool)), editor, SLOT(gotoNextPattern(bool)));
    QObject::connect(&actionPreviousPattern, SIGNAL(triggered(bool)), editor, SLOT(gotoPreviousPattern(bool)));

    // Pattern context menu
    QObject::connect(&actionSetStartPattern, SIGNAL(triggered(bool)), this, SLOT(setStartPattern(bool)));
    QObject::connect(&actionRenamePattern, SIGNAL(triggered(bool)), this, SLOT(renamePattern(bool)));
    QObject::connect(&actionSetGoto, SIGNAL(triggered(bool)), this, SLOT(setGoto(bool)));
    QObject::connect(&actionRemoveGoto, SIGNAL(triggered(bool)), this, SLOT(removeGoto(bool)));
    QObject::connect(&actionMovePatternUp, SIGNAL(triggered(bool)), this, SLOT(movePatternUp(bool)));
    QObject::connect(&actionMovePatternDown, SIGNAL(triggered(bool)), this, SLOT(movePatternDown(bool)));
    QObject::connect(&actionInsertPatternBefore, SIGNAL(triggered(bool)), this, SLOT(insertPatternBefore(bool)));
    QObject::connect(&actionInsertPatternAfter, SIGNAL(triggered(bool)), this, SLOT(insertPatternAfter(bool)));
    QObject::connect(&actionRemovePattern, SIGNAL(triggered(bool)), this, SLOT(removePattern(bool)));
    QObject::connect(&actionDuplicatePattern, SIGNAL(triggered(bool)), this, SLOT(duplicatePattern(bool)));

    // Channel context menu
    QObject::connect(&actionSlide, SIGNAL(triggered(bool)), this, SLOT(setSlideValue(bool)));
    QObject::connect(&actionSetFrequency, SIGNAL(triggered(bool)), this, SLOT(setFrequency(bool)));
    QObject::connect(&actionHold, SIGNAL(triggered(bool)), this, SLOT(setHold(bool)));
    QObject::connect(&actionPause, SIGNAL(triggered(bool)), this, SLOT(setPause(bool)));
    QObject::connect(&actionDeleteRow, SIGNAL(triggered(bool)), this, SLOT(deleteRow(bool)));
    QObject::connect(&actionInsertRowBefore, SIGNAL(triggered(bool)), this, SLOT(insertRowBefore(bool)));
    QObject::connect(&actionInsertRowAfter, SIGNAL(triggered(bool)), this, SLOT(insertRowAfter(bool)));
    QObject::connect(&actionMuteChannel, SIGNAL(triggered(bool)), this, SLOT(toggleMute(bool)));

    editor->registerPatternMenu(&patternContextMenu);
    editor->registerChannelMenu(&channelContextMenu);
    timeline->registerPatternMenu(&patternContextMenu);
}

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

void TrackTab::updateTrackTab() {
    // Set GUI elements
    QCheckBox *cbGlobal = findChild<QCheckBox *>("checkBoxGlobalTempo");
    cbGlobal->setChecked(pTrack->globalSpeed);
    QSpinBox *spEven = findChild<QSpinBox *>("spinBoxEvenTempo");
    QSpinBox *spOdd = findChild<QSpinBox *>("spinBoxOddTempo");
    if (pTrack->globalSpeed) {
        spEven->setValue(pTrack->evenSpeed);
        spOdd->setValue(pTrack->oddSpeed);
    } else {
        PatternEditor *pe = findChild<PatternEditor *>("trackEditor");
        int editPos = pe->getEditPos();
        if (editPos < pTrack->getChannelNumRows(0)) {
            int patternIndex = pTrack->getPatternIndex(0, editPos);
            spEven->setValue(pTrack->patterns[patternIndex].evenSpeed);
            spOdd->setValue(pTrack->patterns[patternIndex].oddSpeed);
        }
    }
    QSpinBox *spRowsPerBeat = findChild<QSpinBox *>("spinBoxRowsPerBeat");
    spRowsPerBeat->setValue(pTrack->rowsPerBeat);

    // Update individual sub-widgets
    updateTrackStats();
}

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

void TrackTab::toggleGlobalTempo(bool toggled) {
    pTrack->globalSpeed = toggled;
    updateTrackTab();
    updateTrackStats();
    updatePatternEditor();
}

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

void TrackTab::setEvenSpeed(int value) {
    if (pTrack->globalSpeed) {
        pTrack->evenSpeed = value;
    } else {
        PatternEditor *pe = findChild<PatternEditor *>("trackEditor");
        int editPos = pe->getEditPos();
        // Only the left channel is used for local tempo
        int patternIndex = pTrack->getPatternIndex(0, editPos);
        pTrack->patterns[patternIndex].evenSpeed = value;
    }
    updateTrackStats();
    updatePatternEditor();
}

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

void TrackTab::setOddSpeed(int value) {
    if (pTrack->globalSpeed) {
        pTrack->oddSpeed = value;
    } else {
        PatternEditor *pe = findChild<PatternEditor *>("trackEditor");
        int editPos = pe->getEditPos();
        int patternIndex = pTrack->getPatternIndex(0, editPos);
        pTrack->patterns[patternIndex].oddSpeed = value;
    }
    updateTrackStats();
    updatePatternEditor();
}

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

void TrackTab::channelContextEvent(int channel, int noteIndex) {
    contextEventChannel = channel;
    contextEventNoteIndex = noteIndex;
}

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

void TrackTab::toggleFollow(bool) {
    QCheckBox *cb = findChild<QCheckBox *>("checkBoxFollow");
    cb->toggle();
}

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

void TrackTab::toggleLoop(bool) {
    QCheckBox *cb = findChild<QCheckBox *>("checkBoxLoop");
    cb->toggle();
}

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

void TrackTab::setStartPattern(bool) {
    pTrack->lock();
    pTrack->startPatterns[contextEventChannel] = pTrack->getSequenceEntryIndex(contextEventChannel, contextEventNoteIndex);
    pTrack->unlock();
    update();
}

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

void TrackTab::renamePattern(bool) {
    RenamePatternDialog dialog(this);
    int entryIndex = pTrack->getSequenceEntryIndex(contextEventChannel, contextEventNoteIndex);
    int patternIndex = pTrack->channelSequences[contextEventChannel].sequence[entryIndex].patternIndex;
    dialog.setPatternName(pTrack->patterns[patternIndex].name);
    if (dialog.exec() == QDialog::Accepted) {
        pTrack->patterns[patternIndex].name = dialog.getPatternName();
        update();
    }
}

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

void TrackTab::setGoto(bool) {
    int maxValue = pTrack->channelSequences[contextEventChannel].sequence.size();
    SetGotoDialog dialog(this);
    dialog.setMaxValue(maxValue);
    int entryIndex = pTrack->getSequenceEntryIndex(contextEventChannel, contextEventNoteIndex);
    Track::SequenceEntry *entry = &(pTrack->channelSequences[contextEventChannel].sequence[entryIndex]);
    dialog.setGotoValue(std::max(1, entry->gotoTarget + 1));
    if (dialog.exec() == QDialog::Accepted) {
        pTrack->lock();
        entry->gotoTarget = dialog.getGotoValue() - 1;
        pTrack->unlock();
        update();
    }

}

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

void TrackTab::removeGoto(bool) {
    pTrack->lock();
    int entryIndex = pTrack->getSequenceEntryIndex(contextEventChannel, contextEventNoteIndex);
    Track::SequenceEntry *entry = &(pTrack->channelSequences[contextEventChannel].sequence[entryIndex]);
    entry->gotoTarget = -1;
    pTrack->unlock();
    update();
}

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

void TrackTab::movePatternUp(bool) {
    int entryIndex = pTrack->getSequenceEntryIndex(contextEventChannel, contextEventNoteIndex);
    if (entryIndex > 0) {
        emit stopTrack();
        pTrack->channelSequences[contextEventChannel].sequence.swap(entryIndex, entryIndex - 1);
        pTrack->updateFirstNoteNumbers();
        update();
    }
}

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

void TrackTab::movePatternDown(bool) {
    int entryIndex = pTrack->getSequenceEntryIndex(contextEventChannel, contextEventNoteIndex);
    if (entryIndex != pTrack->channelSequences[contextEventChannel].sequence.size() - 1) {
        emit stopTrack();
        pTrack->channelSequences[contextEventChannel].sequence.swap(entryIndex, entryIndex + 1);
        pTrack->updateFirstNoteNumbers();
        update();
    }
}

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

void TrackTab::insertPatternBefore(bool) {
    emit stopTrack();
    int patternIndex = choosePatternToInsert(true);
    if (patternIndex == -1) {
        return;
    }
    int entryIndex = pTrack->getSequenceEntryIndex(contextEventChannel, contextEventNoteIndex);
    Track::SequenceEntry newEntry(patternIndex);
    pTrack->channelSequences[contextEventChannel].sequence.insert(entryIndex, newEntry);
    pTrack->updateFirstNoteNumbers();
    update();
}

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

void TrackTab::insertPatternAfter(bool) {
    emit stopTrack();
    int patternIndex = choosePatternToInsert(false);
    if (patternIndex == -1) {
        return;
    }
    int entryIndex = pTrack->getSequenceEntryIndex(contextEventChannel, contextEventNoteIndex);
    Track::SequenceEntry newEntry(patternIndex);
    pTrack->channelSequences[contextEventChannel].sequence.insert(entryIndex + 1, newEntry);
    pTrack->updateFirstNoteNumbers();
    update();
}

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

void TrackTab::removePattern(bool) {
    emit stopTrack();
    if (pTrack->channelSequences[contextEventChannel].sequence.size() == 1) {
        MainWindow::displayMessage("A channel must contain at least one pattern!");
        return;
    }

    int entryIndex = pTrack->getSequenceEntryIndex(contextEventChannel, contextEventNoteIndex);
    int patternIndex = pTrack->channelSequences[contextEventChannel].sequence[entryIndex].patternIndex;
    pTrack->channelSequences[contextEventChannel].sequence.removeAt(entryIndex);
    pTrack->updateFirstNoteNumbers();
    // Check if pattern is no longer used anywhere
    bool wasLast = true;
    for (int channel = 0; channel < 2 && wasLast; ++channel) {
        for (int i = 0; i < pTrack->channelSequences[channel].sequence.size(); ++i) {
            if (pTrack->channelSequences[channel].sequence[i].patternIndex == patternIndex) {
                wasLast = false;
                break;
            }
        }
    }
    if (wasLast) {
        QMessageBox msgBox(QMessageBox::NoIcon,
                           "Delete Pattern?",
                           "This pattern is no longer used in the track. Do you want to delete it?",
                           QMessageBox::Yes | QMessageBox::No, this,
                           Qt::FramelessWindowHint);
        if (msgBox.exec() == QMessageBox::Yes) {
            // Correct SequenceEntries
            for (int channel = 0; channel < 2; ++channel) {
                for (int i = 0; i < pTrack->channelSequences[channel].sequence.size(); ++i) {
                    if (pTrack->channelSequences[channel].sequence[i].patternIndex > patternIndex) {
                        pTrack->channelSequences[channel].sequence[i].patternIndex--;
                    }
                }
            }
            pTrack->patterns.removeAt(patternIndex);
            pTrack->updateFirstNoteNumbers();
        }
    }
    // Validate gotos
    for (int i = 0; i < pTrack->channelSequences[contextEventChannel].sequence.size(); ++i) {
        if (pTrack->channelSequences[contextEventChannel].sequence[i].gotoTarget >= entryIndex) {
            pTrack->channelSequences[contextEventChannel].sequence[i].gotoTarget--;
        }
    }

    emit validateEditPos();
    update();
}

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

void TrackTab::duplicatePattern(bool) {
    emit stopTrack();
    RenamePatternDialog dialog(this);
    int entryIndex = pTrack->getSequenceEntryIndex(contextEventChannel, contextEventNoteIndex);
    int patternIndex = pTrack->channelSequences[contextEventChannel].sequence[entryIndex].patternIndex;
    dialog.setPatternName(pTrack->patterns[patternIndex].name);
    if (dialog.exec() == QDialog::Accepted) {
        pTrack->patterns.append(pTrack->patterns[patternIndex]);
        pTrack->patterns.last().name = dialog.getPatternName();
        int entryIndex = pTrack->getSequenceEntryIndex(contextEventChannel, contextEventNoteIndex);
        Track::SequenceEntry newEntry(pTrack->patterns.size() - 1);
        pTrack->channelSequences[contextEventChannel].sequence.insert(entryIndex + 1, newEntry);
        pTrack->updateFirstNoteNumbers();
        update();
    }
}

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

void TrackTab::setSlideValue(bool) {
    emit stopTrack();
    if (!pTrack->checkSlideValidity(contextEventChannel, contextEventNoteIndex)) {
        MainWindow::displayMessage("A SLIDE can only follow a melodic instrument or another slide!");
        return;
    }

    SetSlideDialog dialog(this);
    // If selected row already contains a SLIDE, pre-set dialog to that value
    Track::Note *selectedNote = pTrack->getNote(contextEventChannel, contextEventNoteIndex);
    if (selectedNote->type == Track::Note::instrumentType::Slide) {
        dialog.setSlideValue(selectedNote->value);
    }
    if (dialog.exec() == QDialog::Accepted) {
        selectedNote->type = Track::Note::instrumentType::Slide;
        selectedNote->value = dialog.getSlideValue();
        emit advanceEditPos();
        updatePatternEditor();
    }
}

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

void TrackTab::setFrequency(bool) {
    emit stopTrack();
    Track::Note *selectedNote = pTrack->getNote(contextEventChannel, contextEventNoteIndex);
    if (selectedNote->type != Track::Note::instrumentType::Instrument) {
        MainWindow::displayMessage("Only a melodic instrument can have a frequency value!");
        return;
    }
    SetFrequencyDialog dialog(this);
    dialog.setFrequencyValue(selectedNote->value);
    Track::Instrument *ins = &(pTrack->instruments[selectedNote->instrumentNumber]);
    int maxFreq = ins->baseDistortion == TiaSound::Distortion::PURE_COMBINED ? 63 : 31;
    dialog.setMaxFrequencyValue(maxFreq);
    if (dialog.exec() == QDialog::Accepted) {
        selectedNote->value = dialog.getFrequencyValue();
        emit advanceEditPos();
        updatePatternEditor();
    }
}

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

void TrackTab::setHold(bool) {
    pTrack->lock();
    pTrack->getNote(contextEventChannel, contextEventNoteIndex)->type = Track::Note::instrumentType::Hold;
    pTrack->unlock();
    emit advanceEditPos();
    update();
}

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

void TrackTab::setPause(bool) {
    pTrack->lock();
    pTrack->getNote(contextEventChannel, contextEventNoteIndex)->type = Track::Note::instrumentType::Pause;
    pTrack->unlock();
    emit advanceEditPos();
    update();
}

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

void TrackTab::deleteRow(bool) {
    emit stopTrack();
    int patternIndex = pTrack->getPatternIndex(contextEventChannel, contextEventNoteIndex);
    Track::Pattern *pattern = &(pTrack->patterns[patternIndex]);
    if (pattern->notes.size() == Track::Pattern::minSize) {
        MainWindow::displayMessage("Pattern is already at minimum size!");
        return;
    }

    int noteInPattern = pTrack->getNoteIndexInPattern(contextEventChannel, contextEventNoteIndex);
    pattern->notes.removeAt(noteInPattern);
    pTrack->updateFirstNoteNumbers();
    emit validateEditPos();
    update();
}

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

void TrackTab::insertRowBefore(bool) {
    emit stopTrack();
    int patternIndex = pTrack->getPatternIndex(contextEventChannel, contextEventNoteIndex);
    Track::Pattern *pattern = &(pTrack->patterns[patternIndex]);
    if (pattern->notes.size() == Track::Pattern::maxSize) {
        MainWindow::displayMessage("Pattern is already at maximum size!");
        return;
    }

    int noteInPattern = pTrack->getNoteIndexInPattern(contextEventChannel, contextEventNoteIndex);
    Track::Note newNote(Track::Note::instrumentType::Hold, 0, 0);
    pattern->notes.insert(noteInPattern, newNote);
    pTrack->updateFirstNoteNumbers();
    update();
}

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

void TrackTab::insertRowAfter(bool) {
    emit stopTrack();
    int patternIndex = pTrack->getPatternIndex(contextEventChannel, contextEventNoteIndex);
    Track::Pattern *pattern = &(pTrack->patterns[patternIndex]);
    if (pattern->notes.size() == Track::Pattern::maxSize) {
        MainWindow::displayMessage("Pattern is already at maximum size!");
        return;
    }

    int noteInPattern = pTrack->getNoteIndexInPattern(contextEventChannel, contextEventNoteIndex);
    Track::Note newNote(Track::Note::instrumentType::Hold, 0, 0);
    pattern->notes.insert(noteInPattern + 1, newNote);
    pTrack->updateFirstNoteNumbers();
    update();
}

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

void TrackTab::toggleMute(bool) {
    pPlayer->channelMuted[contextEventChannel] = !pPlayer->channelMuted[contextEventChannel];
}

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

void TrackTab::invalidNoteFound(int, int, int, QString reason) {
    MainWindow::displayMessage(reason);
}

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

void TrackTab::updateTrackStats() {
    // Patterns
    QLabel *statsLabel = findChild<QLabel *>("labelPatternsUsed");
    int sequenceLength = pTrack->channelSequences[0].sequence.size() + pTrack->channelSequences[1].sequence.size();
    int distinct = pTrack->patterns.size();
    statsLabel->setText("Patterns used: " + QString::number(sequenceLength) + " of 255 ("
                        + QString::number(distinct) + " distinct)");

    // Start patterns
    QLabel *startLabel = findChild<QLabel *>("labelStartPatterns");
    startLabel->setText("Start patterns: " + QString::number(pTrack->startPatterns[0] + 1)
                        + "/" + QString::number(pTrack->startPatterns[1] + 1));

    // Time
    int numRows = pTrack->getTrackNumRows();
    long numOddTicks = int((numRows + 1)/2)*pTrack->oddSpeed;
    long numEvenTicks = int(numRows/2)*pTrack->evenSpeed;
    long numTicks = numOddTicks + numEvenTicks;
    int ticksPerSecond = pTrack->getTvMode() == TiaSound::TvStandard::PAL ? 50 : 60;
    int minutes = numTicks/(ticksPerSecond*60);
    int seconds = (numTicks%(ticksPerSecond*60))/ticksPerSecond;
    QString timeText = QString::number(minutes);
    if (seconds < 10) {
        timeText.append("m 0");
    } else {
        timeText.append("m ");
    }
    timeText.append(QString::number(seconds) + "s");
    QLabel *timeLabel = findChild<QLabel *>("labelTotalLength");
    timeLabel->setText("Total length: " + timeText);
    // BPM
    QLabel *bpmLabel = findChild<QLabel *>("labelBPM");
    if (pTrack->globalSpeed) {
        int numTicksSum = pTrack->oddSpeed + pTrack->evenSpeed;
        double bpm = (60.0*ticksPerSecond)/((numTicksSum/2.0)*pTrack->rowsPerBeat);
        bpmLabel->setText("(~" + QString::number(bpm, 'f', 2) + " bpm)");
    } else {
        bpmLabel->setText("n/a (local tempo)");
    }
}

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

void TrackTab::updatePatternEditor() {
    PatternEditor *editor = findChild<PatternEditor *>("trackEditor");
    editor->update();
}

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

int TrackTab::choosePatternToInsert(bool doBefore) {
    InsertPatternDialog dialog(this);
    dialog.prepare(pTrack);
    if (dialog.exec() == QDialog::Accepted) {
        int result = dialog.getSelectedPattern();
        // Check for "create new pattern"
        if (result == pTrack->patterns.size()) {
            // Calc index of first note of new pattern, for "align" button
            int entryIndex = pTrack->getSequenceEntryIndex(contextEventChannel, contextEventNoteIndex);
            int patternIndex = pTrack->channelSequences[contextEventChannel].sequence[entryIndex].patternIndex;
            int newRow = pTrack->channelSequences[contextEventChannel].sequence[entryIndex].firstNoteNumber;
            if (!doBefore) {
                newRow += pTrack->patterns[patternIndex].notes.size();
            }
            CreatePatternDialog newDialog(this);
            newDialog.prepare(pTrack, lastNewPatternLength, contextEventChannel, newRow);
            if (newDialog.exec() == QDialog::Accepted) {
                QString newName = newDialog.getName();
                int newLength = newDialog.getLength();
                Track::Pattern newPattern(newName);
                QSpinBox *spEven = findChild<QSpinBox *>("spinBoxEvenTempo");
                // Get even/odd speeds from GUI
                newPattern.evenSpeed = spEven->value();
                QSpinBox *spOdd = findChild<QSpinBox *>("spinBoxOddTempo");
                newPattern.oddSpeed = spOdd->value();
                // Create notes
                for (int i = 0; i < newLength; ++i) {
                    Track::Note newNote(Track::Note::instrumentType::Hold, 0, 0);
                    newPattern.notes.append(newNote);
                }
                // Add new pattern
                pTrack->patterns.append(newPattern);
                lastNewPatternLength = newLength;
            } else {
                return -1;
            }
        }
        return result;
    } else {
        return -1;
    }
}

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

void TrackTab::addShortcut(QAction *action, QString actionName) {
    if (MainWindow::keymap.contains(actionName)) {
        QString shortcut = MainWindow::keymap[actionName].toString();
        action->setShortcut(QKeySequence(shortcut));
        addAction(action);
    }
}