-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathwaveformrenderbeat.cpp
103 lines (79 loc) · 3.13 KB
/
waveformrenderbeat.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
#include <QDomNode>
#include <QPaintEvent>
#include <QPainter>
#include "waveform/renderers/waveformrenderbeat.h"
#include "control/controlobject.h"
#include "track/track.h"
#include "waveform/renderers/waveformwidgetrenderer.h"
#include "widget/wskincolor.h"
#include "widget/wwidget.h"
#include "util/painterscope.h"
WaveformRenderBeat::WaveformRenderBeat(WaveformWidgetRenderer* waveformWidgetRenderer)
: WaveformRendererAbstract(waveformWidgetRenderer) {
m_beats.resize(128);
}
WaveformRenderBeat::~WaveformRenderBeat() {
}
void WaveformRenderBeat::setup(const QDomNode& node, const SkinContext& context) {
m_beatColor.setNamedColor(context.selectString(node, "BeatColor"));
m_beatColor = WSkinColor::getCorrectColor(m_beatColor).toRgb();
}
void WaveformRenderBeat::draw(QPainter* painter, QPaintEvent* /*event*/) {
TrackPointer trackInfo = m_waveformRenderer->getTrackInfo();
if (!trackInfo) {
return;
}
mixxx::BeatsPointer trackBeats = trackInfo->getBeats();
if (!trackBeats) {
return;
}
int alpha = m_waveformRenderer->getBeatGridAlpha();
if (alpha == 0) {
return;
}
m_beatColor.setAlphaF(alpha/100.0);
const int trackSamples = m_waveformRenderer->getTrackSamples();
if (trackSamples <= 0) {
return;
}
const double firstDisplayedPosition =
m_waveformRenderer->getFirstDisplayedPosition();
const double lastDisplayedPosition =
m_waveformRenderer->getLastDisplayedPosition();
// qDebug() << "trackSamples" << trackSamples
// << "firstDisplayedPosition" << firstDisplayedPosition
// << "lastDisplayedPosition" << lastDisplayedPosition;
std::unique_ptr<mixxx::BeatIterator> it(trackBeats->findBeats(
firstDisplayedPosition * trackSamples,
lastDisplayedPosition * trackSamples));
// if no beat do not waste time saving/restoring painter
if (!it || !it->hasNext()) {
return;
}
PainterScope PainterScope(painter);
painter->setRenderHint(QPainter::Antialiasing);
QPen beatPen(m_beatColor);
beatPen.setWidthF(std::max(1.0, scaleFactor()));
painter->setPen(beatPen);
const Qt::Orientation orientation = m_waveformRenderer->getOrientation();
const float rendererWidth = m_waveformRenderer->getWidth();
const float rendererHeight = m_waveformRenderer->getHeight();
int beatCount = 0;
while (it->hasNext()) {
double beatPosition = it->next();
double xBeatPoint =
m_waveformRenderer->transformSamplePositionInRendererWorld(beatPosition);
xBeatPoint = qRound(xBeatPoint);
// If we don't have enough space, double the size.
if (beatCount >= m_beats.size()) {
m_beats.resize(m_beats.size() * 2);
}
if (orientation == Qt::Horizontal) {
m_beats[beatCount++].setLine(xBeatPoint, 0.0f, xBeatPoint, rendererHeight);
} else {
m_beats[beatCount++].setLine(0.0f, xBeatPoint, rendererWidth, xBeatPoint);
}
}
// Make sure to use constData to prevent detaches!
painter->drawLines(m_beats.constData(), beatCount);
}