/
AutomationEditor.cpp
2329 lines (1880 loc) · 60.1 KB
/
AutomationEditor.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
/*
* AutomationEditor.cpp - implementation of AutomationEditor which is used for
* actual setting of dynamic values
*
* Copyright (c) 2008-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2008-2013 Paul Giblock <pgib/at/users.sourceforge.net>
* Copyright (c) 2006-2008 Javier Serrano Polo <jasp00/at/users.sourceforge.net>
*
* 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 "AutomationEditor.h"
#include <QApplication>
#include <QInputDialog>
#include <QKeyEvent>
#include <QLabel>
#include <QPainter>
#include <QPainterPath>
#include <QScrollBar>
#include <QStyleOption>
#include <QToolTip>
#include <cmath>
#include "SampleClip.h"
#include "SampleWaveform.h"
#ifndef __USE_XOPEN
#define __USE_XOPEN
#endif
#include "ActionGroup.h"
#include "AutomationNode.h"
#include "ComboBox.h"
#include "DeprecationHelper.h"
#include "DetuningHelper.h"
#include "Engine.h"
#include "GuiApplication.h"
#include "Knob.h"
#include "MainWindow.h"
#include "MidiClip.h"
#include "PatternStore.h"
#include "PianoRoll.h"
#include "ProjectJournal.h"
#include "SampleBuffer.h"
#include "StringPairDrag.h"
#include "TextFloat.h"
#include "TimeLineWidget.h"
#include "debug.h"
#include "embed.h"
#include "gui_templates.h"
namespace lmms::gui
{
const std::array<float, 7> AutomationEditor::m_zoomXLevels =
{ 0.125f, 0.25f, 0.5f, 1.0f, 2.0f, 4.0f, 8.0f };
AutomationEditor::AutomationEditor() :
QWidget(),
m_zoomingXModel(),
m_zoomingYModel(),
m_quantizeModel(),
m_clip(nullptr),
m_minLevel( 0 ),
m_maxLevel( 0 ),
m_step( 1 ),
m_scrollLevel( 0 ),
m_bottomLevel( 0 ),
m_topLevel( 0 ),
m_currentPosition(),
m_action( Action::None ),
m_drawLastLevel( 0.0f ),
m_drawLastTick( 0 ),
m_ppb( DEFAULT_PPB ),
m_y_delta( DEFAULT_Y_DELTA ),
m_y_auto( true ),
m_editMode( EditMode::Draw ),
m_mouseDownLeft(false),
m_mouseDownRight( false ),
m_scrollBack( false ),
m_barLineColor(0, 0, 0),
m_beatLineColor(0, 0, 0),
m_lineColor(0, 0, 0),
m_graphColor(Qt::SolidPattern),
m_nodeInValueColor(0, 0, 0),
m_nodeOutValueColor(0, 0, 0),
m_nodeTangentLineColor(0, 0, 0),
m_scaleColor(Qt::SolidPattern),
m_crossColor(0, 0, 0),
m_backgroundShade(0, 0, 0),
m_ghostNoteColor(0, 0, 0)
{
connect( this, SIGNAL(currentClipChanged()),
this, SLOT(updateAfterClipChange()),
Qt::QueuedConnection );
connect( Engine::getSong(), SIGNAL(timeSignatureChanged(int,int)),
this, SLOT(update()));
setAttribute( Qt::WA_OpaquePaintEvent, true );
//keeps the direction of the widget, undepended on the locale
setLayoutDirection( Qt::LeftToRight );
m_tensionModel = new FloatModel(1.0, 0.0, 1.0, 0.01);
connect( m_tensionModel, SIGNAL(dataChanged()),
this, SLOT(setTension()));
for (auto q : Quantizations) {
m_quantizeModel.addItem(QString("1/%1").arg(q));
}
connect( &m_quantizeModel, SIGNAL(dataChanged()),
this, SLOT(setQuantization()));
m_quantizeModel.setValue( m_quantizeModel.findText( "1/8" ) );
// add time-line
m_timeLine = new TimeLineWidget(VALUES_WIDTH, 0, m_ppb,
Engine::getSong()->getPlayPos(Song::PlayMode::AutomationClip),
Engine::getSong()->getTimeline(Song::PlayMode::AutomationClip),
m_currentPosition, Song::PlayMode::AutomationClip, this
);
connect(this, &AutomationEditor::positionChanged, m_timeLine, &TimeLineWidget::updatePosition);
connect( m_timeLine, SIGNAL( positionChanged( const lmms::TimePos& ) ),
this, SLOT( updatePosition( const lmms::TimePos& ) ) );
// 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)));
setCurrentClip(nullptr);
setMouseTracking( true );
setFocusPolicy( Qt::StrongFocus );
setFocus();
}
AutomationEditor::~AutomationEditor()
{
m_zoomingXModel.disconnect();
m_zoomingYModel.disconnect();
m_quantizeModel.disconnect();
m_tensionModel->disconnect();
delete m_tensionModel;
}
void AutomationEditor::setCurrentClip(AutomationClip * new_clip )
{
if (m_clip)
{
m_clip->disconnect(this);
}
m_clip = new_clip;
if (m_clip != nullptr)
{
connect(m_clip, SIGNAL(dataChanged()), this, SLOT(update()));
}
emit currentClipChanged();
}
void AutomationEditor::saveSettings(QDomDocument & doc, QDomElement & dom_parent)
{
MainWindow::saveWidgetState( parentWidget(), dom_parent );
}
void AutomationEditor::loadSettings( const QDomElement & dom_parent)
{
MainWindow::restoreWidgetState(parentWidget(), dom_parent);
}
void AutomationEditor::updateAfterClipChange()
{
m_currentPosition = 0;
if( !validClip() )
{
m_minLevel = m_maxLevel = m_scrollLevel = 0;
m_step = 1;
resizeEvent(nullptr);
return;
}
m_minLevel = m_clip->firstObject()->minValue<float>();
m_maxLevel = m_clip->firstObject()->maxValue<float>();
m_step = m_clip->firstObject()->step<float>();
centerTopBottomScroll();
m_tensionModel->setValue( m_clip->getTension() );
// resizeEvent() does the rest for us (scrolling, range-checking
// of levels and so on...)
resizeEvent(nullptr);
update();
}
void AutomationEditor::update()
{
QWidget::update();
// Note detuning?
if( m_clip && !m_clip->getTrack() )
{
getGUI()->pianoRoll()->update();
}
}
void AutomationEditor::keyPressEvent(QKeyEvent * ke )
{
switch( ke->key() )
{
case Qt::Key_Up:
m_topBottomScroll->setValue(
m_topBottomScroll->value() - 1 );
ke->accept();
break;
case Qt::Key_Down:
m_topBottomScroll->setValue(
m_topBottomScroll->value() + 1 );
ke->accept();
break;
case Qt::Key_Left:
if( ( m_timeLine->pos() -= 16 ) < 0 )
{
m_timeLine->pos().setTicks( 0 );
}
m_timeLine->updatePosition();
ke->accept();
break;
case Qt::Key_Right:
m_timeLine->pos() += 16;
m_timeLine->updatePosition();
ke->accept();
break;
case Qt::Key_Home:
m_timeLine->pos().setTicks( 0 );
m_timeLine->updatePosition();
ke->accept();
break;
default:
break;
}
}
void AutomationEditor::leaveEvent(QEvent * e )
{
while (QApplication::overrideCursor() != nullptr)
{
QApplication::restoreOverrideCursor();
}
QWidget::leaveEvent( e );
update();
}
void AutomationEditor::drawLine( int x0In, float y0, int x1In, float y1 )
{
int x0 = Note::quantized( x0In, AutomationClip::quantization() );
int x1 = Note::quantized( x1In, AutomationClip::quantization() );
int deltax = qAbs( x1 - x0 );
auto deltay = qAbs<float>(y1 - y0);
int x = x0;
float y = y0;
if( deltax < AutomationClip::quantization() )
{
return;
}
deltax /= AutomationClip::quantization();
float yscale = deltay / ( deltax );
int xstep = (x0 < x1 ? 1 : -1) * AutomationClip::quantization();
int ystep = y0 < y1 ? 1 : -1;
float lineAdjust = ystep * yscale;
for (int i = 0; i < deltax; ++i)
{
y = y0 + ( ystep * yscale * i ) + lineAdjust;
x += xstep;
m_clip->removeNode(TimePos(x));
m_clip->putValue( TimePos( x ), y );
}
}
bool AutomationEditor::fineTuneValue(timeMap::iterator node, bool editingOutValue)
{
if (node == m_clip->getTimeMap().end()) { return false; }
// Display dialog to edit the value
bool ok;
double value = QInputDialog::getDouble(
this,
tr("Edit Value"),
editingOutValue
? tr("New outValue")
: tr("New inValue"),
editingOutValue
? OUTVAL(node)
: INVAL(node),
m_clip->firstObject()->minValue<float>(),
m_clip->firstObject()->maxValue<float>(),
3,
&ok
);
// If dialog failed return false
if (!ok) { return false; }
// Set the new inValue/outValue
if (editingOutValue)
{
node.value().setOutValue(value);
}
else
{
// If the outValue is equal to the inValue we
// set both to the given value
if (OFFSET(node) == 0)
{
node.value().setOutValue(value);
}
node.value().setInValue(value);
}
Engine::getSong()->setModified();
return true;
}
void AutomationEditor::mousePressEvent( QMouseEvent* mouseEvent )
{
if( !validClip() )
{
return;
}
// Some helper lambda functions to avoid repetition of code
auto eraseNode = [this](timeMap::iterator node)
{
if (node != m_clip->getTimeMap().end())
{
m_clip->removeNode(POS(node));
Engine::getSong()->setModified();
}
};
auto resetNode = [this](timeMap::iterator node)
{
if (node != m_clip->getTimeMap().end())
{
node.value().resetOutValue();
Engine::getSong()->setModified();
}
};
auto resetTangent = [this](timeMap::iterator node)
{
if (node != m_clip->getTimeMap().end())
{
// Unlock the tangents from that node
node.value().setLockedTangents(false);
// Recalculate the tangents
m_clip->generateTangents(node, 1);
Engine::getSong()->setModified();
}
};
// If we clicked inside the AutomationEditor viewport (where the nodes are represented)
if (mouseEvent->y() > TOP_MARGIN && mouseEvent->x() >= VALUES_WIDTH)
{
float level = getLevel( mouseEvent->y() );
// Get the viewport X
int x = mouseEvent->x() - VALUES_WIDTH;
// Get tick in which the user clicked
int posTicks = (x * TimePos::ticksPerBar() / m_ppb) + m_currentPosition;
// Get the time map of current clip
timeMap & tm = m_clip->getTimeMap();
m_mouseDownLeft = (mouseEvent->button() == Qt::LeftButton);
m_mouseDownRight = (mouseEvent->button() == Qt::RightButton);
// Some actions require that we know if we clicked the inValue of
// a node, while others require that we know if we clicked the outValue
// of a node.
bool editingOutValue = (
m_editMode == EditMode::DrawOutValues
|| (m_editMode == EditMode::Erase && m_mouseDownRight)
);
timeMap::iterator clickedNode = getNodeAt(mouseEvent->x(), mouseEvent->y(), editingOutValue);
switch (m_editMode)
{
case EditMode::Draw:
{
m_clip->addJournalCheckPoint();
if (m_mouseDownLeft)
{
// If we are pressing shift, make a line of nodes from the last
// clicked position to this position
if (mouseEvent->modifiers() & Qt::ShiftModifier)
{
drawLine(m_drawLastTick, m_drawLastLevel,
posTicks, level);
// Update the last clicked position for the next time
m_drawLastTick = posTicks;
m_drawLastLevel = level;
// Changes the action to drawing a line of nodes
m_action = Action::DrawLine;
}
else // No shift, we are just creating/moving nodes
{
// Starts actually moving/draging the node
TimePos newTime = m_clip->setDragValue(
// The TimePos of either the clicked node or a new one
TimePos(
clickedNode == tm.end()
? posTicks
: POS(clickedNode)
),
level,
clickedNode == tm.end(),
mouseEvent->modifiers() & Qt::ControlModifier
);
// We need to update our iterator because we are either creating a new node
// or dragging an existing one. In the latter, setDragValue removes the node that
// is being dragged, so if we don't update it we have a bogus iterator
clickedNode = tm.find(newTime);
// Set the action to Action::MoveValue so moveMouseEvent() knows we are moving a node
m_action = Action::MoveValue;
// Calculate the offset from the place the mouse click happened in comparison
// to the center of the node
int alignedX = (POS(clickedNode) - m_currentPosition) * m_ppb / TimePos::ticksPerBar();
m_moveXOffset = x - alignedX - 1;
// Set move-cursor
QCursor c(Qt::SizeAllCursor);
QApplication::setOverrideCursor(c);
// Update the last clicked position so it can be used if we draw a line later
m_drawLastTick = posTicks;
m_drawLastLevel = level;
}
Engine::getSong()->setModified();
}
else if (m_mouseDownRight) // Right click on EditMode::Draw mode erases values
{
// Update the last clicked position so we remove all nodes from
// that point up to the point we release the mouse button
m_drawLastTick = posTicks;
// If we right-clicked a node, remove it
eraseNode(clickedNode);
m_action = Action::EraseValues;
}
break;
}
case EditMode::Erase:
{
m_clip->addJournalCheckPoint();
// On erase mode, left click removes nodes
if (m_mouseDownLeft)
{
// Update the last clicked position so we remove all nodes from
// that point up to the point we release the mouse button
m_drawLastTick = posTicks;
// If we right-clicked a node, remove it
eraseNode(clickedNode);
m_action = Action::EraseValues;
}
else if (m_mouseDownRight) // And right click resets outValues
{
// If we clicked an outValue reset it
resetNode(clickedNode);
// Update the last clicked position so we reset all outValues from
// that point up to the point we release the mouse button
m_drawLastTick = posTicks;
m_action = Action::ResetOutValues;
}
break;
}
case EditMode::DrawOutValues:
{
m_clip->addJournalCheckPoint();
// On this mode, left click sets the outValue
if (m_mouseDownLeft)
{
// If we clicked an outValue
if (clickedNode != tm.end())
{
m_draggedOutValueKey = POS(clickedNode);
clickedNode.value().setOutValue(level);
m_action = Action::MoveOutValue;
Engine::getSong()->setModified();
}
else // If we didn't click an outValue
{
// We check if the quantized position of the time we clicked has a
// node and set its outValue
TimePos quantizedPos = Note::quantized(
TimePos(posTicks),
m_clip->quantization()
);
clickedNode = tm.find(quantizedPos);
if (clickedNode != tm.end())
{
m_draggedOutValueKey = POS(clickedNode);
clickedNode.value().setOutValue(level);
m_action = Action::MoveOutValue;
Engine::getSong()->setModified();
}
}
}
else if (m_mouseDownRight) // Right click resets outValues
{
// If we clicked an outValue reset it
resetNode(clickedNode);
// Update the last clicked position so we reset all outValues from
// that point up to the point we release the mouse button
m_drawLastTick = posTicks;
m_action = Action::ResetOutValues;
}
break;
}
case EditMode::EditTangents:
{
if (!m_clip->canEditTangents())
{
update();
return;
}
m_clip->addJournalCheckPoint();
// Gets the closest node to the mouse click
timeMap::iterator node = getClosestNode(mouseEvent->x());
// Starts dragging a tangent
if (m_mouseDownLeft && node != tm.end())
{
// Lock the tangents from that node, so it can only be
// manually edited
node.value().setLockedTangents(true);
m_draggedTangentTick = POS(node);
// Are we dragging the out or in tangent?
m_draggedOutTangent = posTicks >= m_draggedTangentTick;
m_action = Action::MoveTangent;
}
// Resets node's tangent
else if (m_mouseDownRight)
{
// Resets tangent from node
resetTangent(node);
// Update the last clicked position so we reset all tangents from
// that point up to the point we release the mouse button
m_drawLastTick = posTicks;
m_action = Action::ResetTangents;
}
break;
}
}
update();
}
}
void AutomationEditor::mouseDoubleClickEvent(QMouseEvent * mouseEvent)
{
if (!validClip()) { return; }
// If we double clicked outside the AutomationEditor viewport return
if (mouseEvent->y() <= TOP_MARGIN || mouseEvent->x() < VALUES_WIDTH) { return; }
// Are we fine tuning the inValue or outValue?
const bool isOutVal = (m_editMode == EditMode::DrawOutValues);
timeMap::iterator clickedNode = getNodeAt(mouseEvent->x(), mouseEvent->y(), isOutVal);
switch (m_editMode)
{
case EditMode::Draw:
case EditMode::DrawOutValues:
if (fineTuneValue(clickedNode, isOutVal)) { update(); }
break;
default:
break;
}
}
void AutomationEditor::mouseReleaseEvent(QMouseEvent * mouseEvent )
{
bool mustRepaint = false;
if (mouseEvent->button() == Qt::LeftButton)
{
m_mouseDownLeft = false;
mustRepaint = true;
}
if (mouseEvent->button() == Qt::RightButton)
{
m_mouseDownRight = false;
mustRepaint = true;
}
if (m_editMode == EditMode::Draw)
{
if (m_action == Action::MoveValue)
{
// Actually apply the value of the node being dragged
m_clip->applyDragValue();
}
QApplication::restoreOverrideCursor();
}
m_action = Action::None;
if (mustRepaint) { repaint(); }
}
void AutomationEditor::mouseMoveEvent(QMouseEvent * mouseEvent )
{
if( !validClip() )
{
update();
return;
}
// If the mouse y position is inside the Automation Editor viewport
if (mouseEvent->y() > TOP_MARGIN)
{
float level = getLevel(mouseEvent->y());
// Get the viewport X position where the mouse is at
int x = mouseEvent->x() - VALUES_WIDTH;
// Get the X position in ticks
int posTicks = (x * TimePos::ticksPerBar() / m_ppb) + m_currentPosition;
switch (m_editMode)
{
case EditMode::Draw:
{
// We are dragging a node
if (m_mouseDownLeft)
{
if (m_action == Action::MoveValue)
{
// When we clicked the node, we might have clicked slightly off
// so we account for that offset for a smooth drag
x -= m_moveXOffset;
posTicks = (x * TimePos::ticksPerBar() / m_ppb) + m_currentPosition;
// If we moved the mouse past the beginning correct the position in ticks
posTicks = qMax(posTicks, 0);
m_drawLastTick = posTicks;
m_drawLastLevel = level;
// Updates the drag value of the moved node
m_clip->setDragValue(
TimePos(posTicks),
level,
true,
mouseEvent->modifiers() & Qt::ControlModifier
);
Engine::getSong()->setModified();
}
/* else if (m_action == Action::DrawLine)
{
// We are drawing a line. For now do nothing (as before), but later logic
// could be added here so the line is updated according to the new mouse position
// until the button is released
}*/
}
else if (m_mouseDownRight) // We are removing nodes
{
if (m_action == Action::EraseValues)
{
// If we moved the mouse past the beginning correct the position in ticks
posTicks = qMax(posTicks, 0);
// Removing automation nodes
// Removes all values from the last clicked tick up to the current position tick
m_clip->removeNodes(m_drawLastTick, posTicks);
Engine::getSong()->setModified();
}
}
break;
}
case EditMode::Erase:
{
// If we moved the mouse past the beginning correct the position in ticks
posTicks = qMax(posTicks, 0);
// Left button removes nodes
if (m_mouseDownLeft)
{
if (m_action == Action::EraseValues)
{
// Removing automation nodes
// Removes all values from the last clicked tick up to the current position tick
m_clip->removeNodes(m_drawLastTick, posTicks);
Engine::getSong()->setModified();
}
}
else if (m_mouseDownRight) // Right button resets outValues
{
if (m_action == Action::ResetOutValues)
{
// Reseting outValues
// Resets all values from the last clicked tick up to the current position tick
m_clip->resetNodes(m_drawLastTick, posTicks);
Engine::getSong()->setModified();
}
}
break;
}
case EditMode::DrawOutValues:
{
// If we moved the mouse past the beginning correct the position in ticks
posTicks = qMax(posTicks, 0);
// Left button moves outValues
if (m_mouseDownLeft)
{
if (m_action == Action::MoveOutValue)
{
// We are moving the outValue of the node
timeMap & tm = m_clip->getTimeMap();
timeMap::iterator it = tm.find(m_draggedOutValueKey);
// Safety check
if (it != tm.end())
{
it.value().setOutValue(level);
Engine::getSong()->setModified();
}
}
}
else if (m_mouseDownRight) // Right button resets them
{
if (m_action == Action::ResetOutValues)
{
// Reseting outValues
// Resets all values from the last clicked tick up to the current position tick
m_clip->resetNodes(m_drawLastTick, posTicks);
Engine::getSong()->setModified();
}
}
break;
}
case EditMode::EditTangents:
{
// If we moved the mouse past the beginning correct the position in ticks
posTicks = std::max(posTicks, 0);
if (m_mouseDownLeft && m_action == Action::MoveTangent)
{
timeMap& tm = m_clip->getTimeMap();
auto it = tm.find(m_draggedTangentTick);
// Safety check
if (it == tm.end())
{
update();
return;
}
// Calculate new tangent
float y = m_draggedOutTangent
? yCoordOfLevel(OUTVAL(it))
: yCoordOfLevel(INVAL(it));
float dy = m_draggedOutTangent
? y - mouseEvent->y()
: mouseEvent->y() - y;
float dx = std::abs(posTicks - POS(it));
float newTangent = dy / std::max(dx, 1.0f);
if (m_draggedOutTangent)
{
it.value().setOutTangent(newTangent);
}
else
{
it.value().setInTangent(newTangent);
}
}
else if (m_mouseDownRight && m_action == Action::ResetTangents)
{
// Resets all tangents from the last clicked tick up to the current position tick
m_clip->resetTangents(m_drawLastTick, posTicks);
Engine::getSong()->setModified();
}
break;
}
}
}
else // If the mouse Y position is above the AutomationEditor viewport
{
QApplication::restoreOverrideCursor();
}
update();
}
inline void AutomationEditor::drawCross( QPainter & p )
{
QPoint mouse_pos = mapFromGlobal( QCursor::pos() );
int grid_bottom = height() - SCROLLBAR_SIZE - 1;
float level = getLevel( mouse_pos.y() );
float cross_y = m_y_auto ?
grid_bottom - ( ( grid_bottom - TOP_MARGIN )
* ( level - m_minLevel )
/ (float)( m_maxLevel - m_minLevel ) ) :
grid_bottom - ( level - m_bottomLevel ) * m_y_delta;
p.setPen(m_crossColor);
p.drawLine( VALUES_WIDTH, (int) cross_y, width(), (int) cross_y );
p.drawLine( mouse_pos.x(), TOP_MARGIN, mouse_pos.x(), height() - SCROLLBAR_SIZE );
QPoint tt_pos = QCursor::pos();
tt_pos.ry() -= 51;
tt_pos.rx() += 26;
float scaledLevel = m_clip->firstObject()->scaledValue( level );
// Limit the scaled-level tooltip to the grid
if( mouse_pos.x() >= 0 &&
mouse_pos.x() <= width() - SCROLLBAR_SIZE &&
mouse_pos.y() >= 0 &&
mouse_pos.y() <= height() - SCROLLBAR_SIZE )
{
QToolTip::showText( tt_pos, QString::number( scaledLevel ), this );
}
}
inline void AutomationEditor::drawAutomationPoint(QPainter & p, timeMap::iterator it)
{
int x = xCoordOfTick(POS(it));
// Below (m_ppb * AutomationClip::quantization() / 576) is used because:
// 1 bar equals to 192/quantization() notes. Hence, to calculate the number of pixels
// per note we would have (m_ppb * 1 bar / (192/quantization()) notes per bar), or
// (m_ppb * quantization / 192). If we want 1/3 of the number of pixels per note we
// get (m_ppb * quantization() / 192*3) or (m_ppb * quantization() / 576)
const int outerRadius = qBound(3, (m_ppb * AutomationClip::quantization()) / 576, 5);
// Draw a circle for the outValue
int y = yCoordOfLevel(OUTVAL(it));
p.setPen(QPen(m_nodeOutValueColor.lighter(200)));
p.setBrush(QBrush(m_nodeOutValueColor));
p.drawEllipse(x - outerRadius, y - outerRadius, outerRadius * 2, outerRadius * 2);
// Draw a circle for the inValue
y = yCoordOfLevel(INVAL(it));
p.setPen(QPen(m_nodeInValueColor.lighter(200)));
p.setBrush(QBrush(m_nodeInValueColor));
p.drawEllipse(x - outerRadius, y - outerRadius, outerRadius * 2, outerRadius * 2);
}
inline void AutomationEditor::drawAutomationTangents(QPainter& p, timeMap::iterator it)
{
int x = xCoordOfTick(POS(it));
// The tangent value correlates the variation in the node value related to the increase
// in ticks. So to have a proportionate drawing of the tangent line, we need to find the
// relation between the number of pixels per tick and the number of pixels per value level.
float viewportHeight = (height() - SCROLLBAR_SIZE - 1) - TOP_MARGIN;
float pixelsPerTick = m_ppb / TimePos::ticksPerBar();
// std::abs just in case the topLevel is smaller than the bottomLevel for some reason
float pixelsPerLevel = std::abs(viewportHeight / (m_topLevel - m_bottomLevel));
float proportion = pixelsPerLevel / pixelsPerTick;
p.setPen(QPen(m_nodeTangentLineColor));
p.setBrush(QBrush(m_nodeTangentLineColor));
int y = yCoordOfLevel(INVAL(it));
int tx = x - 20;