comparison view/AlignmentView.cpp @ 1216:dc2af6616c83

Merge from branch 3.0-integration
author Chris Cannam
date Fri, 13 Jan 2017 10:29:50 +0000
parents f2c63ec85901
children 57d192e26331 a34a2a25907c
comparison
equal deleted inserted replaced
1048:e8102ff5573b 1216:dc2af6616c83
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006-2014 Chris Cannam and QMUL.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "AlignmentView.h"
17
18 #include <QPainter>
19
20 #include "data/model/SparseOneDimensionalModel.h"
21
22 #include "layer/TimeInstantLayer.h"
23
24 using std::vector;
25
26 AlignmentView::AlignmentView(QWidget *w) :
27 View(w, false),
28 m_above(0),
29 m_below(0)
30 {
31 setObjectName(tr("AlignmentView"));
32 }
33
34 void
35 AlignmentView::globalCentreFrameChanged(sv_frame_t f)
36 {
37 View::globalCentreFrameChanged(f);
38 update();
39 }
40
41 void
42 AlignmentView::viewCentreFrameChanged(View *v, sv_frame_t f)
43 {
44 View::viewCentreFrameChanged(v, f);
45 if (v == m_above) {
46 m_centreFrame = f;
47 update();
48 } else if (v == m_below) {
49 update();
50 }
51 }
52
53 void
54 AlignmentView::viewManagerPlaybackFrameChanged(sv_frame_t)
55 {
56 update();
57 }
58
59 void
60 AlignmentView::viewAboveZoomLevelChanged(int level, bool)
61 {
62 m_zoomLevel = level;
63 update();
64 }
65
66 void
67 AlignmentView::viewBelowZoomLevelChanged(int, bool)
68 {
69 update();
70 }
71
72 void
73 AlignmentView::setViewAbove(View *v)
74 {
75 if (m_above) {
76 disconnect(m_above, 0, this, 0);
77 }
78
79 m_above = v;
80
81 if (m_above) {
82 connect(m_above,
83 SIGNAL(zoomLevelChanged(int, bool)),
84 this,
85 SLOT(viewAboveZoomLevelChanged(int, bool)));
86 }
87 }
88
89 void
90 AlignmentView::setViewBelow(View *v)
91 {
92 if (m_below) {
93 disconnect(m_below, 0, this, 0);
94 }
95
96 m_below = v;
97
98 if (m_below) {
99 connect(m_below,
100 SIGNAL(zoomLevelChanged(int, bool)),
101 this,
102 SLOT(viewBelowZoomLevelChanged(int, bool)));
103 }
104 }
105
106 void
107 AlignmentView::paintEvent(QPaintEvent *)
108 {
109 if (m_above == 0 || m_below == 0 || !m_manager) return;
110
111 bool darkPalette = false;
112 if (m_manager) darkPalette = m_manager->getGlobalDarkBackground();
113
114 QColor fg, bg;
115 if (darkPalette) {
116 fg = Qt::gray;
117 bg = Qt::black;
118 } else {
119 fg = Qt::black;
120 bg = Qt::gray;
121 }
122
123 QPainter paint(this);
124 paint.setPen(QPen(fg, 2));
125 paint.setBrush(Qt::NoBrush);
126 paint.setRenderHint(QPainter::Antialiasing, true);
127
128 paint.fillRect(rect(), bg);
129
130 vector<sv_frame_t> keyFrames = getKeyFrames();
131
132 foreach (sv_frame_t f, keyFrames) {
133 int ax = m_above->getXForFrame(f);
134 sv_frame_t rf = m_above->alignToReference(f);
135 sv_frame_t bf = m_below->alignFromReference(rf);
136 int bx = m_below->getXForFrame(bf);
137 paint.drawLine(ax, 0, bx, height());
138 }
139
140 paint.end();
141 }
142
143 vector<sv_frame_t>
144 AlignmentView::getKeyFrames()
145 {
146 if (!m_above) {
147 return getDefaultKeyFrames();
148 }
149
150 SparseOneDimensionalModel *m = 0;
151
152 // get the topmost such
153 for (int i = 0; i < m_above->getLayerCount(); ++i) {
154 if (qobject_cast<TimeInstantLayer *>(m_above->getLayer(i))) {
155 SparseOneDimensionalModel *mm =
156 qobject_cast<SparseOneDimensionalModel *>
157 (m_above->getLayer(i)->getModel());
158 if (mm) m = mm;
159 }
160 }
161
162 if (!m) {
163 return getDefaultKeyFrames();
164 }
165
166 vector<sv_frame_t> keyFrames;
167
168 const SparseOneDimensionalModel::PointList pp = m->getPoints();
169 for (SparseOneDimensionalModel::PointList::const_iterator pi = pp.begin();
170 pi != pp.end(); ++pi) {
171 keyFrames.push_back(pi->frame);
172 }
173
174 return keyFrames;
175 }
176
177 vector<sv_frame_t>
178 AlignmentView::getDefaultKeyFrames()
179 {
180 vector<sv_frame_t> keyFrames;
181
182 if (!m_above || !m_manager) return keyFrames;
183
184 sv_samplerate_t rate = m_manager->getMainModelSampleRate();
185 if (rate == 0) return keyFrames;
186
187 for (sv_frame_t f = m_above->getModelsStartFrame();
188 f <= m_above->getModelsEndFrame();
189 f += sv_frame_t(rate * 5 + 0.5)) {
190 keyFrames.push_back(f);
191 }
192
193 return keyFrames;
194 }
195
196
197
198
199
200