-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathPianoRoll.cpp
5009 lines (4162 loc) · 127 KB
/
PianoRoll.cpp
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PianoRoll.cpp - implementation of piano roll which is used for actual
* writing of melodies
*
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2008 Andrew Kelley <superjoe30/at/gmail/dot/com>
*
* This file is part of LMMS - https://lmms.io
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include "PianoRoll.h"
#include <QApplication>
#include <QClipboard>
#include <QKeyEvent>
#include <QLabel>
#include <QLayout>
#include <QMdiArea>
#include <QPainter>
#include <QPointer>
#include <QScrollBar>
#include <QStyleOption>
#include <QtMath>
#include <QToolButton>
#ifndef __USE_XOPEN
#define __USE_XOPEN
#endif
#include <math.h>
#include <utility>
#include "AutomationEditor.h"
#include "ActionGroup.h"
#include "BBTrackContainer.h"
#include "Clipboard.h"
#include "ComboBox.h"
#include "ConfigManager.h"
#include "DataFile.h"
#include "debug.h"
#include "DeprecationHelper.h"
#include "DetuningHelper.h"
#include "embed.h"
#include "GuiApplication.h"
#include "gui_templates.h"
#include "InstrumentTrack.h"
#include "MainWindow.h"
#include "Pattern.h"
#include "SongEditor.h"
#include "StepRecorderWidget.h"
#include "TextFloat.h"
#include "TimeLineWidget.h"
using std::move;
typedef AutomationPattern::timeMap timeMap;
// some constants...
const int INITIAL_PIANOROLL_WIDTH = 860;
const int INITIAL_PIANOROLL_HEIGHT = 485;
const int SCROLLBAR_SIZE = 12;
const int PIANO_X = 0;
const int WHITE_KEY_WIDTH = 64;
const int BLACK_KEY_WIDTH = 41;
const int DEFAULT_KEY_LINE_HEIGHT = 12;
const int DEFAULT_CELL_WIDTH = 12;
const int NOTE_EDIT_RESIZE_BAR = 6;
const int NOTE_EDIT_MIN_HEIGHT = 50;
const int KEY_AREA_MIN_HEIGHT = DEFAULT_KEY_LINE_HEIGHT * 10;
const int PR_BOTTOM_MARGIN = SCROLLBAR_SIZE;
const int PR_TOP_MARGIN = 18;
const int PR_RIGHT_MARGIN = SCROLLBAR_SIZE;
// width of area used for resizing (the grip at the end of a note)
const int RESIZE_AREA_WIDTH = 9;
// width of line for setting volume/panning of note
const int NOTE_EDIT_LINE_WIDTH = 3;
// key where to start
const int INITIAL_START_KEY = Key_C + Octave_4 * KeysPerOctave;
// number of each note to provide in quantization and note lengths
const int NUM_EVEN_LENGTHS = 6;
const int NUM_TRIPLET_LENGTHS = 5;
QPixmap * PianoRoll::s_toolDraw = NULL;
QPixmap * PianoRoll::s_toolErase = NULL;
QPixmap * PianoRoll::s_toolSelect = NULL;
QPixmap * PianoRoll::s_toolMove = NULL;
QPixmap * PianoRoll::s_toolOpen = NULL;
QPixmap* PianoRoll::s_toolKnife = nullptr;
TextFloat * PianoRoll::s_textFloat = NULL;
static QString s_noteStrings[12] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
static QString getNoteString( int key )
{
return s_noteStrings[key % 12] + QString::number( static_cast<int>( key / KeysPerOctave ) );
}
// used for drawing of piano
PianoRoll::PianoRollKeyTypes PianoRoll::prKeyOrder[] =
{
PR_WHITE_KEY_SMALL, PR_BLACK_KEY, PR_WHITE_KEY_BIG, PR_BLACK_KEY,
PR_WHITE_KEY_SMALL, PR_WHITE_KEY_SMALL, PR_BLACK_KEY, PR_WHITE_KEY_BIG,
PR_BLACK_KEY, PR_WHITE_KEY_BIG, PR_BLACK_KEY, PR_WHITE_KEY_SMALL
} ;
const int DEFAULT_PR_PPB = DEFAULT_CELL_WIDTH * DefaultStepsPerBar;
const QVector<double> PianoRoll::m_zoomLevels =
{0.125f, 0.25f, 0.5f, 1.0f, 1.5f, 2.0f, 4.0f, 8.0f};
const QVector<double> PianoRoll::m_zoomYLevels =
{0.25f, 0.5f, 1.0f, 1.5f, 2.0f, 2.5f, 3.0f, 4.0f};
PianoRoll::PianoRoll() :
m_nemStr( QVector<QString>() ),
m_noteEditMenu( NULL ),
m_semiToneMarkerMenu( NULL ),
m_zoomingModel(),
m_zoomingYModel(),
m_quantizeModel(),
m_noteLenModel(),
m_scaleModel(),
m_chordModel(),
m_pattern( NULL ),
m_currentPosition(),
m_recording( false ),
m_currentNote( NULL ),
m_action( ActionNone ),
m_noteEditMode( NoteEditVolume ),
m_moveBoundaryLeft( 0 ),
m_moveBoundaryTop( 0 ),
m_moveBoundaryRight( 0 ),
m_moveBoundaryBottom( 0 ),
m_mouseDownKey( 0 ),
m_mouseDownTick( 0 ),
m_lastMouseX( 0 ),
m_lastMouseY( 0 ),
m_notesEditHeight( 100 ),
m_userSetNotesEditHeight(100),
m_ppb( DEFAULT_PR_PPB ),
m_keyLineHeight(DEFAULT_KEY_LINE_HEIGHT),
m_octaveHeight(m_keyLineHeight * KeysPerOctave),
m_whiteKeySmallHeight(qFloor(m_keyLineHeight * 1.5)),
m_whiteKeyBigHeight(m_keyLineHeight * 2),
m_blackKeyHeight(m_keyLineHeight),
m_lenOfNewNotes( TimePos( 0, DefaultTicksPerBar/4 ) ),
m_lastNoteVolume( DefaultVolume ),
m_lastNotePanning( DefaultPanning ),
m_minResizeLen( 0 ),
m_startKey( INITIAL_START_KEY ),
m_lastKey( 0 ),
m_editMode( ModeDraw ),
m_ctrlMode( ModeDraw ),
m_mouseDownRight( false ),
m_scrollBack( false ),
m_stepRecorderWidget(this, DEFAULT_PR_PPB, PR_TOP_MARGIN, PR_BOTTOM_MARGIN + m_notesEditHeight, WHITE_KEY_WIDTH, 0),
m_stepRecorder(*this, m_stepRecorderWidget),
m_barLineColor( 0, 0, 0 ),
m_beatLineColor( 0, 0, 0 ),
m_lineColor( 0, 0, 0 ),
m_noteModeColor( 0, 0, 0 ),
m_noteColor( 0, 0, 0 ),
m_ghostNoteColor( 0, 0, 0 ),
m_ghostNoteTextColor( 0, 0, 0 ),
m_barColor( 0, 0, 0 ),
m_selectedNoteColor( 0, 0, 0 ),
m_textColor( 0, 0, 0 ),
m_textColorLight( 0, 0, 0 ),
m_textShadow( 0, 0, 0 ),
m_markedSemitoneColor( 0, 0, 0 ),
m_knifeCutLineColor(0, 0, 0),
m_noteOpacity( 255 ),
m_ghostNoteOpacity( 255 ),
m_noteBorders( true ),
m_ghostNoteBorders( true ),
m_backgroundShade( 0, 0, 0 ),
m_whiteKeyWidth(WHITE_KEY_WIDTH),
m_blackKeyWidth(BLACK_KEY_WIDTH)
{
// gui names of edit modes
m_nemStr.push_back( tr( "Note Velocity" ) );
m_nemStr.push_back( tr( "Note Panning" ) );
m_noteEditMenu = new QMenu( this );
m_noteEditMenu->clear();
for( int i = 0; i < m_nemStr.size(); ++i )
{
QAction * act = new QAction( m_nemStr.at(i), this );
connect( act, &QAction::triggered, [this, i](){ changeNoteEditMode(i); } );
m_noteEditMenu->addAction( act );
}
m_semiToneMarkerMenu = new QMenu( this );
QAction* markSemitoneAction = new QAction( tr("Mark/unmark current semitone"), this );
QAction* markAllOctaveSemitonesAction = new QAction( tr("Mark/unmark all corresponding octave semitones"), this );
QAction* markScaleAction = new QAction( tr("Mark current scale"), this );
QAction* markChordAction = new QAction( tr("Mark current chord"), this );
QAction* unmarkAllAction = new QAction( tr("Unmark all"), this );
QAction* copyAllNotesAction = new QAction( tr("Select all notes on this key"), this);
connect( markSemitoneAction, &QAction::triggered, [this](){ markSemiTone(stmaMarkCurrentSemiTone); });
connect( markAllOctaveSemitonesAction, &QAction::triggered, [this](){ markSemiTone(stmaMarkAllOctaveSemiTones); });
connect( markScaleAction, &QAction::triggered, [this](){ markSemiTone(stmaMarkCurrentScale); });
connect( markChordAction, &QAction::triggered, [this](){ markSemiTone(stmaMarkCurrentChord); });
connect( unmarkAllAction, &QAction::triggered, [this](){ markSemiTone(stmaUnmarkAll); });
connect( copyAllNotesAction, &QAction::triggered, [this](){ markSemiTone(stmaCopyAllNotesOnKey); });
markScaleAction->setEnabled( false );
markChordAction->setEnabled( false );
connect( this, SIGNAL(semiToneMarkerMenuScaleSetEnabled(bool)), markScaleAction, SLOT(setEnabled(bool)) );
connect( this, SIGNAL(semiToneMarkerMenuChordSetEnabled(bool)), markChordAction, SLOT(setEnabled(bool)) );
m_semiToneMarkerMenu->addAction( markSemitoneAction );
m_semiToneMarkerMenu->addAction( markAllOctaveSemitonesAction );
m_semiToneMarkerMenu->addAction( markScaleAction );
m_semiToneMarkerMenu->addAction( markChordAction );
m_semiToneMarkerMenu->addAction( unmarkAllAction );
m_semiToneMarkerMenu->addAction( copyAllNotesAction );
// init pixmaps
if( s_toolDraw == NULL )
{
s_toolDraw = new QPixmap( embed::getIconPixmap( "edit_draw" ) );
}
if( s_toolErase == NULL )
{
s_toolErase= new QPixmap( embed::getIconPixmap( "edit_erase" ) );
}
if( s_toolSelect == NULL )
{
s_toolSelect = new QPixmap( embed::getIconPixmap( "edit_select" ) );
}
if( s_toolMove == NULL )
{
s_toolMove = new QPixmap( embed::getIconPixmap( "edit_move" ) );
}
if( s_toolOpen == NULL )
{
s_toolOpen = new QPixmap( embed::getIconPixmap( "automation" ) );
}
if (s_toolKnife == nullptr)
{
s_toolKnife = new QPixmap(embed::getIconPixmap("edit_knife"));
}
// init text-float
if( s_textFloat == NULL )
{
s_textFloat = new TextFloat;
}
setAttribute( Qt::WA_OpaquePaintEvent, true );
// add time-line
m_timeLine = new TimeLineWidget(m_whiteKeyWidth, 0, m_ppb,
Engine::getSong()->getPlayPos(
Song::Mode_PlayPattern ),
m_currentPosition,
Song::Mode_PlayPattern, this );
connect( this, SIGNAL( positionChanged( const TimePos & ) ),
m_timeLine, SLOT( updatePosition( const TimePos & ) ) );
connect( m_timeLine, SIGNAL( positionChanged( const TimePos & ) ),
this, SLOT( updatePosition( const TimePos & ) ) );
// white position line follows timeline marker
m_positionLine = new PositionLine(this);
//update timeline when in step-recording mode
connect( &m_stepRecorderWidget, SIGNAL( positionChanged( const TimePos & ) ),
this, SLOT( updatePositionStepRecording( const TimePos & ) ) );
// update timeline when in record-accompany mode
connect( Engine::getSong()->getPlayPos( Song::Mode_PlaySong ).m_timeLine,
SIGNAL( positionChanged( const TimePos & ) ),
this,
SLOT( updatePositionAccompany( const TimePos & ) ) );
// TODO
/* connect( engine::getSong()->getPlayPos( Song::Mode_PlayBB ).m_timeLine,
SIGNAL( positionChanged( const TimePos & ) ),
this,
SLOT( updatePositionAccompany( const TimePos & ) ) );*/
removeSelection();
// init scrollbars
m_leftRightScroll = new QScrollBar( Qt::Horizontal, this );
m_leftRightScroll->setSingleStep( 1 );
connect( m_leftRightScroll, SIGNAL( valueChanged( int ) ), this,
SLOT( horScrolled( int ) ) );
m_topBottomScroll = new QScrollBar( Qt::Vertical, this );
m_topBottomScroll->setSingleStep( 1 );
m_topBottomScroll->setPageStep( 20 );
connect( m_topBottomScroll, SIGNAL( valueChanged( int ) ), this,
SLOT( verScrolled( int ) ) );
// setup zooming-stuff
for( float const & zoomLevel : m_zoomLevels )
{
m_zoomingModel.addItem( QString( "%1\%" ).arg( zoomLevel * 100 ) );
}
m_zoomingModel.setValue( m_zoomingModel.findText( "100%" ) );
connect( &m_zoomingModel, SIGNAL( dataChanged() ),
this, SLOT( zoomingChanged() ) );
// zoom y
for (float const & zoomLevel : m_zoomYLevels)
{
m_zoomingYModel.addItem(QString( "%1\%" ).arg(zoomLevel * 100));
}
m_zoomingYModel.setValue(m_zoomingYModel.findText("100%"));
connect(&m_zoomingYModel, SIGNAL(dataChanged()),
this, SLOT(zoomingYChanged()));
// Set up quantization model
m_quantizeModel.addItem( tr( "Note lock" ) );
for (auto q : Quantizations) {
m_quantizeModel.addItem(QString("1/%1").arg(q));
}
m_quantizeModel.setValue( m_quantizeModel.findText( "1/16" ) );
connect( &m_quantizeModel, SIGNAL( dataChanged() ),
this, SLOT( quantizeChanged() ) );
// Set up note length model
m_noteLenModel.addItem( tr( "Last note" ),
std::make_unique<PixmapLoader>( "edit_draw" ) );
const QString pixmaps[] = { "whole", "half", "quarter", "eighth",
"sixteenth", "thirtysecond", "triplethalf",
"tripletquarter", "tripleteighth",
"tripletsixteenth", "tripletthirtysecond" } ;
for( int i = 0; i < NUM_EVEN_LENGTHS; ++i )
{
auto loader = std::make_unique<PixmapLoader>( "note_" + pixmaps[i] );
m_noteLenModel.addItem( "1/" + QString::number( 1 << i ), ::move(loader) );
}
for( int i = 0; i < NUM_TRIPLET_LENGTHS; ++i )
{
auto loader = std::make_unique<PixmapLoader>( "note_" + pixmaps[i+NUM_EVEN_LENGTHS] );
m_noteLenModel.addItem( "1/" + QString::number( (1 << i) * 3 ), ::move(loader) );
}
m_noteLenModel.setValue( 0 );
// Note length change can cause a redraw if Q is set to lock
connect( &m_noteLenModel, SIGNAL( dataChanged() ),
this, SLOT( noteLengthChanged() ) );
// Set up key selection dropdown
m_keyModel.addItem(tr("No key"));
// Use piano roll note strings for key dropdown
for (int i = 0; i < 12; i++) { m_keyModel.addItem(s_noteStrings[i]); }
m_keyModel.setValue(0); // start with "No key"
connect(&m_keyModel, &ComboBoxModel::dataChanged, this, &PianoRoll::keyChanged);
// Set up scale model
const InstrumentFunctionNoteStacking::ChordTable& chord_table =
InstrumentFunctionNoteStacking::ChordTable::getInstance();
m_scaleModel.addItem( tr("No scale") );
for( const InstrumentFunctionNoteStacking::Chord& chord : chord_table )
{
if( chord.isScale() )
{
m_scaleModel.addItem( chord.getName() );
}
}
m_scaleModel.setValue( 0 );
// connect scale change to key change so it auto-highlights with scale as well
connect(&m_scaleModel, &ComboBoxModel::dataChanged, this, &PianoRoll::keyChanged);
// change can update m_semiToneMarkerMenu
connect( &m_scaleModel, SIGNAL( dataChanged() ),
this, SLOT( updateSemiToneMarkerMenu() ) );
// Set up chord model
m_chordModel.addItem( tr("No chord") );
for( const InstrumentFunctionNoteStacking::Chord& chord : chord_table )
{
if( ! chord.isScale() )
{
m_chordModel.addItem( chord.getName() );
}
}
m_chordModel.setValue( 0 );
// change can update m_semiToneMarkerMenu
connect( &m_chordModel, SIGNAL( dataChanged() ),
this, SLOT( updateSemiToneMarkerMenu() ) );
setFocusPolicy( Qt::StrongFocus );
setFocus();
setMouseTracking( true );
connect( &m_scaleModel, SIGNAL( dataChanged() ),
this, SLOT( updateSemiToneMarkerMenu() ) );
connect( Engine::getSong(), SIGNAL( timeSignatureChanged( int, int ) ),
this, SLOT( update() ) );
//connection for selecion from timeline
connect( m_timeLine, SIGNAL( regionSelectedFromPixels( int, int ) ),
this, SLOT( selectRegionFromPixels( int, int ) ) );
m_stepRecorder.initialize();
}
void PianoRoll::reset()
{
m_lastNoteVolume = DefaultVolume;
m_lastNotePanning = DefaultPanning;
clearGhostPattern();
}
void PianoRoll::showTextFloat(const QString &text, const QPoint &pos, int timeout)
{
s_textFloat->setText( text );
// show the float, offset slightly so as to not obscure anything
s_textFloat->moveGlobal( this, pos + QPoint(4, 16) );
if (timeout == -1)
{
s_textFloat->show();
}
else
{
s_textFloat->setVisibilityTimeOut( timeout );
}
}
void PianoRoll::showVolTextFloat(volume_t vol, const QPoint &pos, int timeout)
{
//! \todo display velocity for MIDI-based instruments
// possibly dBFS values too? not sure if it makes sense for note volumes...
showTextFloat( tr("Velocity: %1%").arg( vol ), pos, timeout );
}
void PianoRoll::showPanTextFloat(panning_t pan, const QPoint &pos, int timeout)
{
QString text;
if( pan < 0 )
{
text = tr("Panning: %1% left").arg( qAbs( pan ) );
}
else if( pan > 0 )
{
text = tr("Panning: %1% right").arg( qAbs( pan ) );
}
else
{
text = tr("Panning: center");
}
showTextFloat( text, pos, timeout );
}
void PianoRoll::changeNoteEditMode( int i )
{
m_noteEditMode = (NoteEditMode) i;
repaint();
}
void PianoRoll::markSemiTone(int i, bool fromMenu)
{
const int key = fromMenu
? getKey(mapFromGlobal(m_semiToneMarkerMenu->pos()).y())
: m_keyModel.value() - 1;
const InstrumentFunctionNoteStacking::Chord * chord = nullptr;
// if "No key" is selected, key is -1, unmark all semitones
// or if scale changed from toolbar to "No scale", unmark all semitones
if (!fromMenu && (key < 0 || m_scaleModel.value() == 0)) { i = stmaUnmarkAll; }
switch( static_cast<SemiToneMarkerAction>( i ) )
{
case stmaUnmarkAll:
m_markedSemiTones.clear();
break;
case stmaMarkCurrentSemiTone:
{
QList<int>::iterator it = std::find( m_markedSemiTones.begin(), m_markedSemiTones.end(), key );
if( it != m_markedSemiTones.end() )
{
m_markedSemiTones.erase( it );
}
else
{
m_markedSemiTones.push_back( key );
}
break;
}
case stmaMarkAllOctaveSemiTones:
{
QList<int> aok = getAllOctavesForKey(key);
if ( m_markedSemiTones.contains(key) )
{
// lets erase all of the ones that match this by octave
QList<int>::iterator i;
for (int ix = 0; ix < aok.size(); ++ix)
{
i = std::find(m_markedSemiTones.begin(), m_markedSemiTones.end(), aok.at(ix));
if (i != m_markedSemiTones.end())
{
m_markedSemiTones.erase(i);
}
}
}
else
{
// we should add all of the ones that match this by octave
m_markedSemiTones.append(aok);
}
break;
}
case stmaMarkCurrentScale:
chord = & InstrumentFunctionNoteStacking::ChordTable::getInstance()
.getScaleByName( m_scaleModel.currentText() );
case stmaMarkCurrentChord:
{
if( ! chord )
{
chord = & InstrumentFunctionNoteStacking::ChordTable::getInstance()
.getChordByName( m_chordModel.currentText() );
}
if( chord->isEmpty() )
{
break;
}
else if( chord->isScale() )
{
m_markedSemiTones.clear();
}
const int first = chord->isScale() ? 0 : key;
const int last = chord->isScale() ? NumKeys : key + chord->last();
const int cap = ( chord->isScale() || chord->last() == 0 ) ? KeysPerOctave : chord->last();
for( int i = first; i <= last; i++ )
{
if( chord->hasSemiTone( ( i + cap - ( key % cap ) ) % cap ) )
{
m_markedSemiTones.push_back( i );
}
}
break;
}
case stmaCopyAllNotesOnKey:
{
selectNotesOnKey();
break;
}
default:
;
}
std::sort( m_markedSemiTones.begin(), m_markedSemiTones.end(), std::greater<int>() );
QList<int>::iterator new_end = std::unique( m_markedSemiTones.begin(), m_markedSemiTones.end() );
m_markedSemiTones.erase( new_end, m_markedSemiTones.end() );
// until we move the mouse the window won't update, force redraw
update();
}
PianoRoll::~PianoRoll()
{
}
void PianoRoll::setGhostPattern( Pattern* newPattern )
{
// Expects a pointer to a pattern or nullptr.
m_ghostNotes.clear();
if( newPattern != nullptr )
{
for( Note *note : newPattern->notes() )
{
Note * new_note = new Note( note->length(), note->pos(), note->key() );
m_ghostNotes.push_back( new_note );
}
emit ghostPatternSet( true );
}
}
void PianoRoll::loadGhostNotes( const QDomElement & de )
{
// Load ghost notes from DOM element.
if( de.isElement() )
{
QDomNode node = de.firstChild();
while( !node.isNull() )
{
Note * n = new Note;
n->restoreState( node.toElement() );
m_ghostNotes.push_back( n );
node = node.nextSibling();
}
emit ghostPatternSet( true );
}
}
void PianoRoll::clearGhostPattern()
{
setGhostPattern( nullptr );
emit ghostPatternSet( false );
update();
}
void PianoRoll::glueNotes()
{
if (hasValidPattern())
{
NoteVector selectedNotes = getSelectedNotes();
if (selectedNotes.empty())
{
TextFloat::displayMessage( tr( "Glue notes failed" ),
tr( "Please select notes to glue first." ),
embed::getIconPixmap( "glue", 24, 24 ),
3000 );
return;
}
// Make undo possible
m_pattern->addJournalCheckPoint();
// Sort notes on key and then pos.
std::sort(selectedNotes.begin(), selectedNotes.end(),
[](const Note * note, const Note * compareNote) -> bool
{
if (note->key() == compareNote->key())
{
return note->pos() < compareNote->pos();
}
return note->key() < compareNote->key();
});
QList<Note *> noteToRemove;
NoteVector::iterator note = selectedNotes.begin();
auto nextNote = note+1;
NoteVector::iterator end = selectedNotes.end();
while (note != end && nextNote != end)
{
// key and position match for glue. The notes are already
// sorted so we don't need to test that nextNote is the same
// position or next in sequence.
if ((*note)->key() == (*nextNote)->key()
&& (*nextNote)->pos() <= (*note)->pos()
+ qMax(TimePos(0), (*note)->length()))
{
(*note)->setLength(qMax((*note)->length(),
TimePos((*nextNote)->endPos() - (*note)->pos())));
noteToRemove.push_back(*nextNote);
++nextNote;
}
// key or position doesn't match
else
{
note = nextNote;
nextNote = note+1;
}
}
// Remove old notes
for (int i = 0; i < noteToRemove.count(); ++i)
{
m_pattern->removeNote(noteToRemove[i]);
}
update();
}
}
void PianoRoll::loadMarkedSemiTones(const QDomElement & de)
{
// clear marked semitones to prevent leftover marks
m_markedSemiTones.clear();
if (de.isElement())
{
QDomNode node = de.firstChild();
while (!node.isNull())
{
bool ok;
int key = node.toElement().attribute(
QString("key"), QString("-1")).toInt(&ok, 10);
if (ok && key >= 0)
{
m_markedSemiTones.append(key);
}
node = node.nextSibling();
}
}
// from markSemiTone, required otherwise marks will not show
std::sort(m_markedSemiTones.begin(), m_markedSemiTones.end(), std::greater<int>());
QList<int>::iterator new_end = std::unique(m_markedSemiTones.begin(), m_markedSemiTones.end());
m_markedSemiTones.erase(new_end, m_markedSemiTones.end());
}
void PianoRoll::setCurrentPattern( Pattern* newPattern )
{
if( hasValidPattern() )
{
m_pattern->instrumentTrack()->disconnect( this );
}
// force the song-editor to stop playing if it played pattern before
if( Engine::getSong()->isPlaying() &&
Engine::getSong()->playMode() == Song::Mode_PlayPattern )
{
Engine::getSong()->playPattern( NULL );
}
if(m_stepRecorder.isRecording())
{
m_stepRecorder.stop();
}
// set new data
m_pattern = newPattern;
m_currentPosition = 0;
m_currentNote = NULL;
m_startKey = INITIAL_START_KEY;
m_stepRecorder.setCurrentPattern(newPattern);
if( ! hasValidPattern() )
{
//resizeEvent( NULL );
update();
emit currentPatternChanged();
return;
}
m_leftRightScroll->setValue( 0 );
// determine the central key so that we can scroll to it
int central_key = 0;
int total_notes = 0;
for( const Note *note : m_pattern->notes() )
{
if( note->length() > 0 )
{
central_key += note->key();
++total_notes;
}
}
if( total_notes > 0 )
{
central_key = central_key / total_notes -
( KeysPerOctave * NumOctaves - m_totalKeysToScroll ) / 2;
m_startKey = qBound( 0, central_key, NumOctaves * KeysPerOctave );
}
// resizeEvent() does the rest for us (scrolling, range-checking
// of start-notes and so on...)
resizeEvent( NULL );
// make sure to always get informed about the pattern being destroyed
connect( m_pattern, SIGNAL( destroyedPattern( Pattern* ) ), this, SLOT( hidePattern( Pattern* ) ) );
connect( m_pattern->instrumentTrack(), SIGNAL( midiNoteOn( const Note& ) ), this, SLOT( startRecordNote( const Note& ) ) );
connect( m_pattern->instrumentTrack(), SIGNAL( midiNoteOff( const Note& ) ), this, SLOT( finishRecordNote( const Note& ) ) );
connect( m_pattern->instrumentTrack()->pianoModel(), SIGNAL( dataChanged() ), this, SLOT( update() ) );
update();
emit currentPatternChanged();
}
void PianoRoll::hidePattern( Pattern* pattern )
{
if( m_pattern == pattern )
{
setCurrentPattern( NULL );
}
}
void PianoRoll::selectRegionFromPixels( int xStart, int xEnd )
{
xStart -= m_whiteKeyWidth;
xEnd -= m_whiteKeyWidth;
// select an area of notes
int posTicks = xStart * TimePos::ticksPerBar() / m_ppb +
m_currentPosition;
int keyNum = 0;
m_selectStartTick = posTicks;
m_selectedTick = 0;
m_selectStartKey = keyNum;
m_selectedKeys = 1;
// change size of selection
// get tick in which the cursor is posated
posTicks = xEnd * TimePos::ticksPerBar() / m_ppb +
m_currentPosition;
keyNum = 120;
m_selectedTick = posTicks - m_selectStartTick;
if( (int) m_selectStartTick + m_selectedTick < 0 )
{
m_selectedTick = -static_cast<int>(
m_selectStartTick );
}
m_selectedKeys = keyNum - m_selectStartKey;
if( keyNum <= m_selectStartKey )
{
--m_selectedKeys;
}
computeSelectedNotes( false );
}
void PianoRoll::drawNoteRect( QPainter & p, int x, int y,
int width, const Note * n, const QColor & noteCol, const QColor & noteTextColor,
const QColor & selCol, const int noteOpc, const bool borders, bool drawNoteName )
{
++x;
++y;
width -= 2;
if( width <= 0 )
{
width = 2;
}
// Volume
float const volumeRange = static_cast<float>(MaxVolume - MinVolume);
float const volumeSpan = static_cast<float>(n->getVolume() - MinVolume);
float const volumeRatio = volumeSpan / volumeRange;
int volVal = qMin( 255, 100 + static_cast<int>( volumeRatio * 155.0f) );
// Panning
float const panningRange = static_cast<float>(PanningRight - PanningLeft);
float const leftPanSpan = static_cast<float>(PanningRight - n->getPanning());
float const rightPanSpan = static_cast<float>(n->getPanning() - PanningLeft);
float leftPercent = qMin<float>( 1.0f, leftPanSpan / panningRange * 2.0f );
float rightPercent = qMin<float>( 1.0f, rightPanSpan / panningRange * 2.0f );
QColor col = QColor( noteCol );
QPen pen;
if( n->selected() )
{
col = QColor( selCol );
}
const int borderWidth = borders ? 1 : 0;
const int noteHeight = m_keyLineHeight - 1 - borderWidth;
int noteWidth = width + 1 - borderWidth;
// adjust note to make it a bit faded if it has a lower volume
// in stereo using gradients
QColor lcol = QColor::fromHsv( col.hue(), col.saturation(),
static_cast<int>(volVal * leftPercent), noteOpc );
QColor rcol = QColor::fromHsv( col.hue(), col.saturation(),
static_cast<int>(volVal * rightPercent), noteOpc );
QLinearGradient gradient( x, y, x, y + noteHeight );
gradient.setColorAt( 0, rcol );
gradient.setColorAt( 1, lcol );
p.setBrush( gradient );
if ( borders )
{
p.setPen( col );
}
else
{
p.setPen( Qt::NoPen );
}
p.drawRect( x, y, noteWidth, noteHeight );
// Draw note key text
if (drawNoteName)
{
p.save();
int const noteTextHeight = static_cast<int>(noteHeight * 0.8);
if (noteTextHeight > 6)
{
QString noteKeyString = getNoteString(n->key());
QFont noteFont(p.font());
noteFont.setPixelSize(noteTextHeight);
QFontMetrics fontMetrics(noteFont);
QSize textSize = fontMetrics.size(Qt::TextSingleLine, noteKeyString);
int const distanceToBorder = 2;
int const xOffset = borderWidth + distanceToBorder;
// noteTextHeight, textSize are not suitable for determining vertical spacing,
// capHeight() can be used for this, but requires Qt 5.8.
// We use boundingRect() with QChar (the QString version returns wrong value).
QRect const boundingRect = fontMetrics.boundingRect(QChar::fromLatin1('H'));
int const yOffset = (noteHeight - boundingRect.top() - boundingRect.bottom()) / 2;
if (textSize.width() < noteWidth - xOffset)
{
p.setPen(noteTextColor);
p.setFont(noteFont);
QPoint textStart(x + xOffset, y + yOffset);
p.drawText(textStart, noteKeyString);
}
}
p.restore();
}
// draw the note endmark, to hint the user to resize
p.setBrush( col );
if( width > 2 )
{
const int endmarkWidth = 3 - borderWidth;
p.drawRect( x + noteWidth - endmarkWidth, y, endmarkWidth, noteHeight );
}
}
void PianoRoll::drawDetuningInfo( QPainter & _p, const Note * _n, int _x,
int _y ) const
{
int middle_y = _y + m_keyLineHeight / 2;
_p.setPen(m_noteColor);
_p.setClipRect(
m_whiteKeyWidth,
PR_TOP_MARGIN,
width() - m_whiteKeyWidth,
keyAreaBottom() - PR_TOP_MARGIN);
// Draw lines for the detuning automation, treating cubic hermit curves
// as straight lines for now. Also draw discrete jumps.
int old_x = 0;
int old_y = 0;
timeMap & map = _n->detuning()->automationPattern()->getTimeMap();
for (timeMap::const_iterator it = map.begin(); it != map.end(); ++it)
{
// Current node values
int cur_ticks = POS(it);
int cur_x = _x + cur_ticks * m_ppb / TimePos::ticksPerBar();