-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathwoverview.cpp
1680 lines (1465 loc) · 63.1 KB
/
woverview.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
#include "woverview.h"
#include <QBrush>
#include <QColor>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QPen>
#include <QVBoxLayout>
#include "analyzer/analyzerprogress.h"
#include "control/controlproxy.h"
#include "engine/engine.h"
#include "mixer/playermanager.h"
#include "moc_woverview.cpp"
#include "preferences/colorpalettesettings.h"
#include "track/track.h"
#include "util/colorcomponents.h"
#include "util/dnd.h"
#include "util/duration.h"
#include "util/math.h"
#include "util/painterscope.h"
#include "util/timer.h"
#include "waveform/waveform.h"
#include "waveform/waveformwidgetfactory.h"
#include "widget/controlwidgetconnection.h"
#include "wskincolor.h"
namespace {
// Horizontal and vertical margin around the widget where we accept play pos dragging.
constexpr int kDragOutsideLimitX = 100;
constexpr int kDragOutsideLimitY = 50;
} // anonymous namespace
WOverview::WOverview(
const QString& group,
PlayerManager* pPlayerManager,
UserSettingsPointer pConfig,
QWidget* parent)
: WWidget(parent),
m_group(group),
m_pConfig(pConfig),
m_type(Type::RGB),
m_actualCompletion(0),
m_pixmapDone(false),
m_waveformPeak(-1.0),
m_diffGain(0),
m_devicePixelRatio(1.0),
m_endOfTrack(false),
m_bPassthroughEnabled(false),
m_pCueMenuPopup(make_parented<WCueMenuPopup>(pConfig, this)),
m_bShowCueTimes(true),
m_iPosSeconds(0),
m_bLeftClickDragging(false),
m_iPickupPos(0),
m_iPlayPos(0),
m_bTimeRulerActive(false),
m_orientation(Qt::Horizontal),
m_dragMarginH(kDragOutsideLimitX),
m_dragMarginV(kDragOutsideLimitY),
m_iLabelFontSize(10),
m_maxPixelPos(1.0),
m_analyzerProgress(kAnalyzerProgressUnknown),
m_trackLoaded(false),
m_pHoveredMark(nullptr),
m_scaleFactor(1.0),
m_trackSampleRateControl(
m_group,
QStringLiteral("track_samplerate")),
m_trackSamplesControl(
m_group,
QStringLiteral("track_samples")),
m_playpositionControl(
m_group,
QStringLiteral("playposition")) {
m_endOfTrackControl = make_parented<ControlProxy>(
m_group, QStringLiteral("end_of_track"), this, ControlFlag::NoAssertIfMissing);
m_endOfTrackControl->connectValueChanged(this, &WOverview::onEndOfTrackChange);
m_pRateRatioControl = make_parented<ControlProxy>(
m_group, QStringLiteral("rate_ratio"), this, ControlFlag::NoAssertIfMissing);
// Needed to recalculate range durations when rate slider is moved without the deck playing
m_pRateRatioControl->connectValueChanged(
this, &WOverview::onRateRatioChange);
m_pPassthroughControl = make_parented<ControlProxy>(
m_group, QStringLiteral("passthrough"), this, ControlFlag::NoAssertIfMissing);
m_pPassthroughControl->connectValueChanged(this, &WOverview::onPassthroughChange);
m_bPassthroughEnabled = m_pPassthroughControl->toBool();
m_pTypeControl = make_parented<ControlProxy>(
QStringLiteral("[Waveform]"),
QStringLiteral("WaveformOverviewType"),
this);
m_pTypeControl->connectValueChanged(this, &WOverview::slotTypeControlChanged);
slotTypeControlChanged(m_pTypeControl->get());
m_pMinuteMarkersControl = make_parented<ControlProxy>(
QStringLiteral("[Waveform]"),
QStringLiteral("draw_overview_minute_markers"),
this);
m_pMinuteMarkersControl->connectValueChanged(this, &WOverview::slotMinuteMarkersChanged);
slotMinuteMarkersChanged(static_cast<bool>(m_pMinuteMarkersControl->get()));
// Update immediately when the normalize option or the visual gain have been
// changed in the preferences.
WaveformWidgetFactory* widgetFactory = WaveformWidgetFactory::instance();
connect(widgetFactory,
&WaveformWidgetFactory::overviewNormalizeChanged,
this,
&WOverview::slotNormalizeOrVisualGainChanged);
connect(widgetFactory,
&WaveformWidgetFactory::overallVisualGainChanged,
this,
&WOverview::slotNormalizeOrVisualGainChanged);
m_pPassthroughLabel = make_parented<QLabel>(this);
setAcceptDrops(true);
setMouseTracking(true);
connect(pPlayerManager, &PlayerManager::trackAnalyzerProgress,
this, &WOverview::onTrackAnalyzerProgress);
connect(m_pCueMenuPopup.get(), &WCueMenuPopup::aboutToHide, this, &WOverview::slotCueMenuPopupAboutToHide);
}
void WOverview::setup(const QDomNode& node, const SkinContext& context) {
m_scaleFactor = context.getScaleFactor();
m_signalColors.setup(node, context);
m_backgroundColor = m_signalColors.getBgColor();
m_axesColor = m_signalColors.getAxesColor();
m_playPosColor = m_signalColors.getPlayPosColor();
m_passthroughOverlayColor = m_signalColors.getPassthroughOverlayColor();
m_playedOverlayColor = m_signalColors.getPlayedOverlayColor();
m_lowColor = m_signalColors.getLowColor();
m_dimBrightThreshold = m_signalColors.getDimBrightThreshold();
m_labelBackgroundColor = context.selectColor(node, "LabelBackgroundColor");
if (!m_labelBackgroundColor.isValid()) {
m_labelBackgroundColor = m_backgroundColor;
m_labelBackgroundColor.setAlpha(255 / 2); // 0 == fully transparent
}
m_labelTextColor = context.selectColor(node, "LabelTextColor");
if (!m_labelTextColor.isValid()) {
m_labelTextColor = Qt::white;
}
bool okay = false;
int labelFontSize = context.selectInt(node, "LabelFontSize", &okay);
if (okay) {
m_iLabelFontSize = labelFontSize;
}
// Clear the background pixmap, if it exists.
m_backgroundPixmap = QPixmap();
m_backgroundPixmapPath = context.selectString(node, "BgPixmap");
if (!m_backgroundPixmapPath.isEmpty()) {
auto pPixmap = WPixmapStore::getPixmapNoCache(
context.makeSkinPath(m_backgroundPixmapPath),
m_scaleFactor);
if (pPixmap) {
m_backgroundPixmap = *pPixmap;
}
}
m_endOfTrackColor = QColor(200, 25, 20);
const QString endOfTrackColorName = context.selectString(node, "EndOfTrackColor");
if (!endOfTrackColorName.isNull()) {
m_endOfTrackColor = QColor(endOfTrackColorName);
m_endOfTrackColor = WSkinColor::getCorrectColor(m_endOfTrackColor);
}
// setup hotcues and cue and loop(s)
m_marks.setup(m_group, node, context, m_signalColors);
ColorPaletteSettings colorPaletteSettings(m_pConfig);
auto colorPalette = colorPaletteSettings.getHotcueColorPalette();
m_pCueMenuPopup->setColorPalette(colorPalette);
m_marks.connectSamplePositionChanged(this, &WOverview::onMarkChanged);
m_marks.connectSampleEndPositionChanged(this, &WOverview::onMarkChanged);
m_marks.connectVisibleChanged(this, &WOverview::onMarkChanged);
QDomNode child = node.firstChild();
while (!child.isNull()) {
if (child.nodeName() == "MarkRange") {
m_markRanges.push_back(WaveformMarkRange(m_group, child, context, m_signalColors));
WaveformMarkRange& markRange = m_markRanges.back();
if (markRange.m_markEnabledControl) {
markRange.m_markEnabledControl->connectValueChanged(
this, &WOverview::onMarkRangeChange);
}
if (markRange.m_markVisibleControl) {
markRange.m_markVisibleControl->connectValueChanged(
this, &WOverview::onMarkRangeChange);
}
if (markRange.m_markStartPointControl) {
markRange.m_markStartPointControl->connectValueChanged(
this, &WOverview::onMarkRangeChange);
}
if (markRange.m_markEndPointControl) {
markRange.m_markEndPointControl->connectValueChanged(
this, &WOverview::onMarkRangeChange);
}
}
child = child.nextSibling();
}
DEBUG_ASSERT(m_pPassthroughLabel.get() != nullptr);
m_pPassthroughLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
// Shown on the overview waveform when vinyl passthrough is enabled
m_pPassthroughLabel->setText(tr("Passthrough"));
m_pPassthroughLabel->setStyleSheet(
QStringLiteral("QLabel{"
"font-family: 'Open Sans';"
"font-size: %1px;"
"font-weight: bold;"
"color: %2;"
"margin-left };")
.arg(QString::number(int(m_iLabelFontSize * 1.5)),
QColor(m_signalColors.getPassthroughLabelColor())
.name(QColor::HexRgb)));
m_pPassthroughLabel->hide();
QVBoxLayout* pPassthroughLayout = new QVBoxLayout(this);
pPassthroughLayout->setContentsMargins(m_iLabelFontSize, 0, 0, 0); // l, t, r, b
pPassthroughLayout->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
pPassthroughLayout->addWidget(m_pPassthroughLabel);
setLayout(pPassthroughLayout);
QString orientationString = context.selectString(node, "Orientation").toLower();
if (orientationString == "vertical") {
m_orientation = Qt::Vertical;
} else {
m_orientation = Qt::Horizontal;
}
m_bShowCueTimes = context.selectBool(node, "ShowCueTimes", true);
// qDebug() << "WOverview : std::as_const(m_marks)" << m_marks.size();
// qDebug() << "WOverview : m_markRanges" << m_markRanges.size();
if (!m_connections.empty()) {
auto& pDefaultConnection = m_connections.at(0);
if (pDefaultConnection) {
if (pDefaultConnection->getEmitOption() &
ControlParameterWidgetConnection::EMIT_DEFAULT) {
// ON_PRESS means here value change on mouse move during press
pDefaultConnection->setEmitOption(
ControlParameterWidgetConnection::EMIT_ON_RELEASE);
}
}
}
setFocusPolicy(Qt::NoFocus);
}
void WOverview::initWithTrack(TrackPointer pTrack) {
Init();
if (pTrack) {
// if a track already loaded (after skin change)
slotLoadingTrack(pTrack, TrackPointer());
slotTrackLoaded(pTrack);
slotWaveformSummaryUpdated();
}
}
void WOverview::onConnectedControlChanged(double dParameter, double dValue) {
// this is connected via skin to "playposition"
Q_UNUSED(dValue);
// Calculate handle position. Clamp the value within 0-1 because that's
// all we represent with this widget.
dParameter = math_clamp(dParameter, 0.0, 1.0);
bool redraw = false;
int oldPos = m_iPlayPos;
m_iPlayPos = valueToPosition(dParameter);
if (oldPos != m_iPlayPos) {
redraw = true;
}
if (!m_bLeftClickDragging) {
// if not dragged the pick-up moves with the play position
m_iPickupPos = m_iPlayPos;
}
// In case the user is hovering a cue point or holding right click, the
// calculated time between the playhead and cue/cursor should be updated at
// least once per second, regardless of m_iPos which depends on the length
// of the widget.
int oldPositionSeconds = m_iPosSeconds;
m_iPosSeconds = static_cast<int>(dParameter * getTrackSamples());
if ((m_bTimeRulerActive || m_pHoveredMark != nullptr) && oldPositionSeconds != m_iPosSeconds) {
redraw = true;
}
if (redraw) {
update();
}
}
void WOverview::slotWaveformSummaryUpdated() {
//qDebug() << "WOverview::slotWaveformSummaryUpdated()";
TrackPointer pTrack(m_pCurrentTrack);
if (!pTrack) {
return;
}
m_pWaveform = pTrack->getWaveformSummary();
if (m_pWaveform) {
// If the waveform is already complete, just draw it.
if (m_pWaveform->getCompletion() == m_pWaveform->getDataSize()) {
m_actualCompletion = 0;
if (drawNextPixmapPart()) {
update();
}
}
} else {
// Null waveform pointer means waveform was cleared.
m_waveformSourceImage = QImage();
m_analyzerProgress = kAnalyzerProgressUnknown;
m_actualCompletion = 0;
m_waveformPeak = -1.0;
m_pixmapDone = false;
update();
}
}
void WOverview::onTrackAnalyzerProgress(TrackId trackId, AnalyzerProgress analyzerProgress) {
if (!m_pCurrentTrack || (m_pCurrentTrack->getId() != trackId)) {
return;
}
bool updateNeeded = drawNextPixmapPart();
if (updateNeeded || (m_analyzerProgress != analyzerProgress)) {
m_analyzerProgress = analyzerProgress;
update();
}
}
void WOverview::slotTrackLoaded(TrackPointer pTrack) {
Q_UNUSED(pTrack); // only used in DEBUG_ASSERT
//qDebug() << "WOverview::slotTrackLoaded()" << m_pCurrentTrack.get() << pTrack.get();
DEBUG_ASSERT(m_pCurrentTrack == pTrack);
m_trackLoaded = true;
if (m_pCurrentTrack) {
updateCues(m_pCurrentTrack->getCuePoints());
}
update();
}
void WOverview::slotLoadingTrack(TrackPointer pNewTrack, TrackPointer pOldTrack) {
Q_UNUSED(pOldTrack); // only used in DEBUG_ASSERT
//qDebug() << this << "WOverview::slotLoadingTrack" << pNewTrack.get() << pOldTrack.get();
DEBUG_ASSERT(m_pCurrentTrack == pOldTrack);
if (m_pCurrentTrack != nullptr) {
disconnect(m_pCurrentTrack.get(),
&Track::waveformSummaryUpdated,
this,
&WOverview::slotWaveformSummaryUpdated);
disconnect(m_pCurrentTrack.get(),
&Track::cuesUpdated,
this,
&WOverview::receiveCuesUpdated);
}
m_waveformSourceImage = QImage();
m_analyzerProgress = kAnalyzerProgressUnknown;
m_actualCompletion = 0;
m_waveformPeak = -1.0;
m_pixmapDone = false;
// Note: Here we already have the new track, but the engine and it's
// Control Objects may still have the old one until the slotTrackLoaded()
// signal has been received.
m_trackLoaded = false;
m_endOfTrack = false;
if (pNewTrack) {
m_pCurrentTrack = pNewTrack;
m_pWaveform = pNewTrack->getWaveformSummary();
connect(pNewTrack.get(),
&Track::waveformSummaryUpdated,
this,
&WOverview::slotWaveformSummaryUpdated);
slotWaveformSummaryUpdated();
connect(pNewTrack.get(), &Track::cuesUpdated, this, &WOverview::receiveCuesUpdated);
} else {
m_pCurrentTrack.reset();
m_pWaveform.clear();
}
update();
}
void WOverview::onEndOfTrackChange(double v) {
//qDebug() << "WOverview::onEndOfTrackChange()" << v;
m_endOfTrack = v > 0.0;
update();
}
void WOverview::onMarkChanged(double v) {
Q_UNUSED(v);
//qDebug() << "WOverview::onMarkChanged()" << v;
if (m_pCurrentTrack) {
updateCues(m_pCurrentTrack->getCuePoints());
update();
}
}
void WOverview::onMarkRangeChange(double v) {
Q_UNUSED(v);
//qDebug() << "WOverview::onMarkRangeChange()" << v;
update();
}
void WOverview::onRateRatioChange(double v) {
Q_UNUSED(v);
update();
}
void WOverview::onPassthroughChange(double v) {
m_bPassthroughEnabled = static_cast<bool>(v);
if (m_bPassthroughEnabled) {
// Abort play position dragging
m_bLeftClickDragging = false;
m_bTimeRulerActive = false;
m_iPickupPos = m_iPlayPos;
} else {
slotWaveformSummaryUpdated();
}
// Always call this to trigger a repaint even if no track is loaded
update();
}
void WOverview::slotTypeControlChanged(double v) {
// Assert that v is in enum range to prevent UB.
DEBUG_ASSERT(v >= 0 && v < QMetaEnum::fromType<Type>().keyCount());
Type type = static_cast<Type>(static_cast<int>(v));
if (type == m_type) {
return;
}
m_type = type;
m_pWaveform.clear();
slotWaveformSummaryUpdated();
}
void WOverview::slotMinuteMarkersChanged(bool /*unused*/) {
update();
}
void WOverview::slotNormalizeOrVisualGainChanged() {
update();
}
void WOverview::updateCues(const QList<CuePointer> &loadedCues) {
for (const CuePointer& currentCue : loadedCues) {
const WaveformMarkPointer pMark = m_marks.getHotCueMark(currentCue->getHotCue());
if (pMark != nullptr && pMark->isValid() && pMark->isVisible()
&& pMark->getSamplePosition() != Cue::kNoPosition) {
QColor newColor = mixxx::RgbColor::toQColor(currentCue->getColor());
if (newColor != pMark->fillColor() || newColor != pMark->m_textColor) {
pMark->setBaseColor(newColor, m_dimBrightThreshold);
}
int hotcueNumber = currentCue->getHotCue();
if ((currentCue->getType() == mixxx::CueType::HotCue ||
currentCue->getType() == mixxx::CueType::Loop) &&
hotcueNumber != Cue::kNoHotCue) {
// Prepend the hotcue number to hotcues' labels
QString newLabel = currentCue->getLabel();
if (newLabel.isEmpty()) {
newLabel = QString::number(hotcueNumber + 1);
} else {
newLabel = QString("%1: %2").arg(hotcueNumber + 1).arg(newLabel);
}
if (pMark->m_text != newLabel) {
pMark->m_text = newLabel;
}
}
}
}
m_marks.update();
}
// connecting the tracks cuesUpdated and onMarkChanged is not possible
// due to the incompatible signatures. This is a "wrapper" workaround
void WOverview::receiveCuesUpdated() {
onMarkChanged(0);
}
void WOverview::mouseMoveEvent(QMouseEvent* e) {
if (m_bLeftClickDragging) {
if (isPosInAllowedPosDragZone(e->pos())) {
m_bTimeRulerActive = true;
m_timeRulerPos = e->pos();
unsetCursor();
} else {
// Remove the time ruler to indicate dragging position is invalid,
// don't abort dragging!
m_iPickupPos = m_iPlayPos;
m_bTimeRulerActive = false;
setCursor(Qt::ForbiddenCursor);
// Remember to restore cursor everywhere where we cancel dragging.
// Update immediately.
update();
return;
}
if (m_orientation == Qt::Horizontal) {
m_iPickupPos = math_clamp(e->pos().x(), 0, width() - 1);
} else {
m_iPickupPos = math_clamp(e->pos().y(), 0, height() - 1);
}
}
// Do not activate cue hovering while right click is held down and the
// button down event was not on a cue.
if (m_bTimeRulerActive) {
// Prevent showing times beyond the boundaries of the track when the
// cursor is dragged outside this widget before releasing right click.
m_timeRulerPos.setX(math_clamp(e->pos().x(), 0, width()));
m_timeRulerPos.setY(math_clamp(e->pos().y(), 0, height()));
update();
return;
}
m_pHoveredMark = m_marks.findHoveredMark(e->pos(), m_orientation);
// qDebug() << "WOverview::mouseMoveEvent" << e->pos();
update();
}
void WOverview::mouseReleaseEvent(QMouseEvent* e) {
mouseMoveEvent(e);
if (m_bPassthroughEnabled) {
m_bLeftClickDragging = false;
// We may be dragging, and we may be outside the valid dragging area.
// If so, we've set the 'invalid drag' cursor. Restore the cursor now.
unsetCursor();
return;
}
//qDebug() << "WOverview::mouseReleaseEvent" << e->pos() << m_iPos << ">>" << dValue;
if (e->button() == Qt::LeftButton) {
if (m_bLeftClickDragging) {
unsetCursor();
if (isPosInAllowedPosDragZone(e->pos())) {
m_iPlayPos = m_iPickupPos;
double dValue = positionToValue(m_iPickupPos);
setControlParameterUp(dValue);
m_bLeftClickDragging = false;
} else {
// Abort dragging if we are way outside the widget.
m_iPickupPos = m_iPlayPos;
m_bLeftClickDragging = false;
m_bTimeRulerActive = false;
return;
}
}
m_bTimeRulerActive = false;
} else if (e->button() == Qt::RightButton) {
// Do not seek when releasing a right click. This is important to
// prevent accidental seeking when trying to right click a hotcue.
m_bTimeRulerActive = false;
}
}
void WOverview::mousePressEvent(QMouseEvent* e) {
//qDebug() << "WOverview::mousePressEvent" << e->pos();
mouseMoveEvent(e);
if (m_bPassthroughEnabled) {
m_bLeftClickDragging = false;
unsetCursor();
return;
}
double trackSamples = getTrackSamples();
if (m_pCurrentTrack == nullptr || trackSamples <= 0) {
return;
}
if (e->button() == Qt::LeftButton) {
if (m_orientation == Qt::Horizontal) {
m_iPickupPos = math_clamp(e->pos().x(), 0, width() - 1);
} else {
m_iPickupPos = math_clamp(e->pos().y(), 0, height() - 1);
}
if (m_pHoveredMark != nullptr) {
double dValue = m_pHoveredMark->getSamplePosition() / trackSamples;
m_iPickupPos = valueToPosition(dValue);
m_iPlayPos = m_iPickupPos;
setControlParameterUp(dValue);
m_bLeftClickDragging = false;
} else {
m_bLeftClickDragging = true;
m_bTimeRulerActive = true;
m_timeRulerPos = e->pos();
}
} else if (e->button() == Qt::RightButton) {
if (m_bLeftClickDragging) {
// Abort dragging
m_iPickupPos = m_iPlayPos;
m_bLeftClickDragging = false;
m_bTimeRulerActive = false;
unsetCursor();
} else if (m_pHoveredMark == nullptr) {
m_bTimeRulerActive = true;
m_timeRulerPos = e->pos();
} else if (m_pHoveredMark->getHotCue() != Cue::kNoHotCue) {
// Currently the only way WaveformMarks can be associated
// with their respective Cue objects is by using the hotcue
// number. If cues without assigned hotcue are drawn on
// WOverview in the future, another way to associate
// WaveformMarks with Cues will need to be implemented.
CuePointer pHoveredCue;
const QList<CuePointer> cueList = m_pCurrentTrack->getCuePoints();
for (const auto& pCue : cueList) {
if (pCue->getHotCue() == m_pHoveredMark->getHotCue()) {
pHoveredCue = pCue;
break;
}
}
if (pHoveredCue != nullptr) {
if (e->modifiers().testFlag(Qt::ShiftModifier)) {
m_pCurrentTrack->removeCue(pHoveredCue);
return;
} else {
// Clear the pickup position display, we have all cue info in the menu.
leaveEvent(nullptr);
m_pCueMenuPopup->setTrackCueGroup(m_pCurrentTrack, pHoveredCue, m_group);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
m_pCueMenuPopup->popup(e->globalPosition().toPoint());
#else
m_pCueMenuPopup->popup(e->globalPos());
#endif
}
}
}
}
}
void WOverview::slotCueMenuPopupAboutToHide() {
m_pHoveredMark.clear();
update();
}
void WOverview::leaveEvent(QEvent* pEvent) {
Q_UNUSED(pEvent);
if (!m_pCueMenuPopup->isVisible()) {
m_pHoveredMark.clear();
}
m_bLeftClickDragging = false;
m_bTimeRulerActive = false;
update();
}
void WOverview::paintEvent(QPaintEvent* pEvent) {
Q_UNUSED(pEvent);
ScopedTimer t(QStringLiteral("WOverview::paintEvent"));
QPainter painter(this);
painter.fillRect(rect(), m_backgroundColor);
if (!m_backgroundPixmap.isNull()) {
painter.drawPixmap(rect(), m_backgroundPixmap);
}
if (m_pCurrentTrack) {
// Refer to util/ScopePainter.h to understand the semantics of
// ScopePainter.
drawEndOfTrackBackground(&painter);
drawAxis(&painter);
drawWaveformPixmap(&painter);
drawPlayedOverlay(&painter);
drawMinuteMarkers(&painter);
drawPlayPosition(&painter);
drawEndOfTrackFrame(&painter);
drawAnalyzerProgress(&painter);
double trackSamples = getTrackSamples();
if (trackSamples > 0) {
const float offset = 1.0f;
const auto gain = static_cast<CSAMPLE_GAIN>(length() - 2) /
static_cast<CSAMPLE_GAIN>(trackSamples);
drawRangeMarks(&painter, offset, gain);
drawMarks(&painter, offset, gain);
drawPickupPosition(&painter);
drawTimeRuler(&painter);
drawMarkLabels(&painter, offset, gain);
}
}
if (m_bPassthroughEnabled) {
drawPassthroughOverlay(&painter);
m_pPassthroughLabel->show();
unsetCursor();
} else {
m_pPassthroughLabel->hide();
}
}
void WOverview::drawEndOfTrackBackground(QPainter* pPainter) {
if (m_endOfTrack) {
PainterScope painterScope(pPainter);
pPainter->setOpacity(0.3);
pPainter->setBrush(m_endOfTrackColor);
pPainter->drawRect(rect().adjusted(1, 1, -2, -2));
}
}
void WOverview::drawAxis(QPainter* pPainter) {
PainterScope painterScope(pPainter);
pPainter->setPen(QPen(m_axesColor, m_scaleFactor));
if (m_orientation == Qt::Horizontal) {
pPainter->drawLine(0, height() / 2, width(), height() / 2);
} else {
pPainter->drawLine(width() / 2, 0, width() / 2, height());
}
}
void WOverview::drawWaveformPixmap(QPainter* pPainter) {
WaveformWidgetFactory* widgetFactory = WaveformWidgetFactory::instance();
if (!m_waveformSourceImage.isNull()) {
PainterScope painterScope(pPainter);
float diffGain;
bool normalize = widgetFactory->isOverviewNormalized();
if (normalize && m_pixmapDone && m_waveformPeak > 1) {
diffGain = 255 - m_waveformPeak - 1;
} else {
const auto visualGain = static_cast<float>(
widgetFactory->getVisualGain(WaveformWidgetFactory::All));
diffGain = 255.0f - (255.0f / visualGain);
}
if (m_diffGain != diffGain || m_waveformImageScaled.isNull()) {
QRect sourceRect(0,
static_cast<int>(diffGain),
m_waveformSourceImage.width(),
m_waveformSourceImage.height() -
2 * static_cast<int>(diffGain));
QImage croppedImage = m_waveformSourceImage.copy(sourceRect);
if (m_orientation == Qt::Vertical) {
// Rotate pixmap
croppedImage = croppedImage.transformed(QTransform(0, 1, 1, 0, 0, 0));
}
m_waveformImageScaled = croppedImage.scaled(size() * m_devicePixelRatio,
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
m_diffGain = diffGain;
}
pPainter->drawImage(rect(), m_waveformImageScaled);
}
}
void WOverview::drawMinuteMarkers(QPainter* pPainter) {
if (!m_trackLoaded) {
return;
}
if (!static_cast<bool>(m_pMinuteMarkersControl->get())) {
return;
}
if (m_pRateRatioControl->get() == 0) {
return;
}
// Faster than track->getDuration() and already has playback speed ratio compensated for
const double trackSeconds = samplePositionToSeconds(getTrackSamples());
QLineF line;
pPainter->setPen(QPen(m_axesColor, m_scaleFactor));
pPainter->setOpacity(1.0);
const double overviewHeight = m_orientation == Qt::Horizontal ? height() : width();
const double markerHeight = overviewHeight * 0.08;
const double lowerMarkerYPos = overviewHeight * 0.92;
double currentMarkerXPos;
const int iWidth = m_orientation == Qt::Horizontal ? width() : height();
for (double currentMarkerSeconds = 60; currentMarkerSeconds < trackSeconds;
currentMarkerSeconds += 60) {
currentMarkerXPos = currentMarkerSeconds / trackSeconds * iWidth;
if (m_orientation == Qt::Horizontal) {
line.setLine(currentMarkerXPos, 0.0, currentMarkerXPos, markerHeight);
pPainter->drawLine(line);
line.setLine(currentMarkerXPos, lowerMarkerYPos, currentMarkerXPos, overviewHeight);
pPainter->drawLine(line);
} else {
// untested, best effort basis
line.setLine(0.0, currentMarkerXPos, markerHeight, currentMarkerXPos);
pPainter->drawLine(line);
line.setLine(lowerMarkerYPos, currentMarkerXPos, overviewHeight, currentMarkerXPos);
pPainter->drawLine(line);
}
}
}
void WOverview::drawPlayedOverlay(QPainter* pPainter) {
// Overlay the played part of the overview-waveform with a skin defined color
if (!m_waveformSourceImage.isNull() && m_playedOverlayColor.alpha() > 0) {
if (m_orientation == Qt::Vertical) {
pPainter->fillRect(0,
0,
m_waveformImageScaled.width(),
m_iPlayPos,
m_playedOverlayColor);
} else {
pPainter->fillRect(0,
0,
m_iPlayPos,
m_waveformImageScaled.height(),
m_playedOverlayColor);
}
}
}
void WOverview::drawPlayPosition(QPainter* pPainter) {
// When the position line is currently dragged with the left mouse button
// draw a thin line -without triangles or shadow- at the playposition.
// The new playposition is drawn by drawPickupPosition()
if (m_bLeftClickDragging) {
PainterScope painterScope(pPainter);
QLineF line;
if (m_orientation == Qt::Horizontal) {
line.setLine(m_iPlayPos, 0.0, m_iPlayPos, height());
} else {
line.setLine(0.0, m_iPlayPos, width(), m_iPlayPos);
}
pPainter->setPen(QPen(m_playPosColor, m_scaleFactor));
pPainter->setOpacity(0.5);
pPainter->drawLine(line);
}
}
void WOverview::drawEndOfTrackFrame(QPainter* pPainter) {
if (m_endOfTrack) {
PainterScope painterScope(pPainter);
pPainter->setOpacity(0.8);
pPainter->setPen(QPen(QBrush(m_endOfTrackColor), 1.5 * m_scaleFactor));
pPainter->setBrush(QColor(0, 0, 0, 0));
pPainter->drawRect(rect().adjusted(0, 0, -1, -1));
}
}
void WOverview::drawAnalyzerProgress(QPainter* pPainter) {
if ((m_analyzerProgress >= kAnalyzerProgressNone) &&
(m_analyzerProgress < kAnalyzerProgressDone)) {
PainterScope painterScope(pPainter);
pPainter->setPen(QPen(m_playPosColor, 3 * m_scaleFactor));
if (m_analyzerProgress > kAnalyzerProgressNone) {
if (m_orientation == Qt::Horizontal) {
pPainter->drawLine(QLineF(width() * m_analyzerProgress,
height() / 2,
width(),
height() / 2));
} else {
pPainter->drawLine(QLineF(width() / 2,
height() * m_analyzerProgress,
width() / 2,
height()));
}
}
if (m_analyzerProgress <= kAnalyzerProgressHalf) { // remove text after progress by wf is recognizable
if (m_trackLoaded) {
//: Text on waveform overview when file is playable but no waveform is visible
paintText(tr("Ready to play, analyzing..."), pPainter);
} else {
//: Text on waveform overview when file is cached from source
paintText(tr("Loading track..."), pPainter);
}
} else if (m_analyzerProgress >= kAnalyzerProgressFinalizing) {
//: Text on waveform overview during finalizing of waveform analysis
paintText(tr("Finalizing..."), pPainter);
}
} else if (!m_trackLoaded) {
// This happens if the track samples are not loaded, but we have
// a cached track
//: Text on waveform overview when file is cached from source
paintText(tr("Loading track..."), pPainter);
}
}
void WOverview::drawRangeMarks(QPainter* pPainter, const float& offset, const float& gain) {
for (auto&& markRange : m_markRanges) {
if (!markRange.active() || !markRange.visible()) {
continue;
}
// Active mark ranges by definition have starts/ends that are not
// disabled.
const qreal startValue = markRange.start();
const qreal endValue = markRange.end();
const qreal startPosition = offset + startValue * gain;
const qreal endPosition = offset + endValue * gain;
if (startPosition < 0.0 && endPosition < 0.0) {
continue;
}
PainterScope painterScope(pPainter);
if (markRange.enabled()) {
pPainter->setOpacity(markRange.m_enabledOpacity);
pPainter->setPen(markRange.m_activeColor);
pPainter->setBrush(markRange.m_activeColor);
} else {
pPainter->setOpacity(markRange.m_disabledOpacity);
pPainter->setPen(markRange.m_disabledColor);
pPainter->setBrush(markRange.m_disabledColor);
}
// let top and bottom of the rect out of the widget
if (m_orientation == Qt::Horizontal) {
pPainter->drawRect(QRectF(QPointF(startPosition, -2.0),
QPointF(endPosition, height() + 1.0)));
} else {
pPainter->drawRect(QRectF(QPointF(-2.0, startPosition),
QPointF(width() + 1.0, endPosition)));
}
}
}
void WOverview::drawMarks(QPainter* pPainter, const float offset, const float gain) {
QFont markerFont = pPainter->font();
markerFont.setPixelSize(static_cast<int>(m_iLabelFontSize * m_scaleFactor));
QFontMetricsF fontMetrics(markerFont);
// Text labels are rendered so they do not overlap with other WaveformMarks'
// labels. If the text would be too wide, it is elided. However, the user
// can hover the mouse cursor over a label to show the whole label text,
// temporarily hiding any following labels that would be drawn over it.
// This requires looping over the WaveformMarks twice and the marks must be
// sorted in the order they appear on the waveform.
// In the first loop, the lines are drawn and the text to render plus its
// location are calculated then stored in a WaveformMarkLabel. The text must
// be drawn in the second loop to prevent the lines of following
// WaveformMarks getting drawn over it. The second loop is in the separate
// drawMarkLabels function so it can be called after drawCurrentPosition so
// the view of labels is not obscured by the playhead.
bool markHovered = false;
for (auto it = m_marks.cbegin(); it != m_marks.cend(); ++it) {
PainterScope painterScope(pPainter);
const WaveformMarkPointer& pMark = *it;
double samplePosition = pMark->getSamplePosition();
const float markPosition = math_clamp(
offset + static_cast<float>(samplePosition) * gain,
0.0f,
static_cast<float>(width()));
pMark->m_linePosition = markPosition;
QLineF line;
QLineF bgLine;
if (m_orientation == Qt::Horizontal) {
line.setLine(markPosition, 0.0, markPosition, height());
bgLine.setLine(markPosition - 1.0, 0.0, markPosition - 1.0, height());
} else {
line.setLine(0.0, markPosition, width(), markPosition);
bgLine.setLine(0.0, markPosition - 1.0, width(), markPosition - 1.0);
}
QRectF rect;
double sampleEndPosition = pMark->getSampleEndPosition();
if (sampleEndPosition > 0) {
const float markEndPosition = math_clamp(
offset + static_cast<float>(sampleEndPosition) * gain,
0.0f,
static_cast<float>(width()));
if (m_orientation == Qt::Horizontal) {
rect.setCoords(markPosition, 0, markEndPosition, height());
} else {
rect.setCoords(0, markPosition, width(), markEndPosition);
}
}
pPainter->setPen(pMark->borderColor());
pPainter->drawLine(bgLine);
pPainter->setPen(pMark->fillColor());
pPainter->drawLine(line);
if (rect.isValid()) {
QColor loopColor = pMark->fillColor();