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
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This file copyright 2006-2014 Chris Cannam and QMUL.
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. See the file
COPYING included with this distribution for more information.
*/
#include "AlignmentView.h"
#include <QPainter>
#include "data/model/SparseOneDimensionalModel.h"
#include "layer/TimeInstantLayer.h"
//#define DEBUG_ALIGNMENT_VIEW 1
namespace sv {
using std::vector;
using std::set;
AlignmentView::AlignmentView(QWidget *w) :
View(w, false),
m_above(nullptr),
m_below(nullptr),
m_reference(nullptr),
m_leftmostAbove(-1),
m_rightmostAbove(-1)
{
setObjectName(tr("AlignmentView"));
}
void
AlignmentView::keyFramesChanged()
{
#ifdef DEBUG_ALIGNMENT_VIEW
SVCERR << "AlignmentView " << getId() << "::keyFramesChanged" << endl;
#endif
// This is just a notification that we need to rebuild - so all we
// do here is clear, and rebuild on demand later
QMutexLocker locker(&m_mapsMutex);
m_fromAboveMap.clear();
m_fromReferenceMap.clear();
}
void
AlignmentView::globalCentreFrameChanged(sv_frame_t f)
{
View::globalCentreFrameChanged(f);
update();
}
void
AlignmentView::viewCentreFrameChanged(View *v, sv_frame_t f)
{
View::viewCentreFrameChanged(v, f);
if (v == m_above) {
m_centreFrame = f;
update();
} else if (v == m_below) {
update();
}
}
void
AlignmentView::viewManagerPlaybackFrameChanged(sv_frame_t)
{
update();
}
void
AlignmentView::viewAboveZoomLevelChanged(ZoomLevel level, bool)
{
m_zoomLevel = level;
update();
}
void
AlignmentView::viewBelowZoomLevelChanged(ZoomLevel, bool)
{
update();
}
void
AlignmentView::setAboveView(View *v)
{
if (m_above) {
disconnect(m_above, nullptr, this, nullptr);
}
m_above = v;
if (m_above) {
connect(m_above,
SIGNAL(zoomLevelChanged(ZoomLevel, bool)),
this,
SLOT(viewAboveZoomLevelChanged(ZoomLevel, bool)));
connect(m_above,
SIGNAL(propertyContainerAdded(PropertyContainer *)),
this,
SLOT(keyFramesChanged()));
connect(m_above,
SIGNAL(layerModelChanged()),
this,
SLOT(keyFramesChanged()));
}
keyFramesChanged();
}
void
AlignmentView::setBelowView(View *v)
{
if (m_below) {
disconnect(m_below, nullptr, this, nullptr);
}
m_below = v;
if (m_below) {
connect(m_below,
SIGNAL(zoomLevelChanged(ZoomLevel, bool)),
this,
SLOT(viewBelowZoomLevelChanged(ZoomLevel, bool)));
connect(m_below,
SIGNAL(propertyContainerAdded(PropertyContainer *)),
this,
SLOT(keyFramesChanged()));
connect(m_below,
SIGNAL(layerModelChanged()),
this,
SLOT(keyFramesChanged()));
}
keyFramesChanged();
}
void
AlignmentView::setReferenceView(View *view)
{
m_reference = view;
}
void
AlignmentView::paintEvent(QPaintEvent *)
{
if (m_above == nullptr || m_below == nullptr || !m_manager) return;
#ifdef DEBUG_ALIGNMENT_VIEW
SVCERR << "AlignmentView " << getId() << "::paintEvent" << endl;
#endif
bool darkPalette = false;
if (m_manager) darkPalette = m_manager->getGlobalDarkBackground();
QColor fg, bg;
if (darkPalette) {
fg = Qt::gray;
bg = Qt::black;
} else {
fg = Qt::black;
bg = Qt::gray;
}
QPainter paint(this);
paint.setPen(QPen(fg, 2));
paint.setBrush(Qt::NoBrush);
paint.setRenderHint(QPainter::Antialiasing, true);
paint.fillRect(rect(), bg);
QMutexLocker locker(&m_mapsMutex);
if (m_fromAboveMap.empty()) {
reconnectModels();
buildMaps();
}
#ifdef DEBUG_ALIGNMENT_VIEW
SVCERR << "AlignmentView " << getId() << "::paintEvent: painting "
<< m_fromAboveMap.size() << " mappings" << endl;
#endif
int w = width();
int h = height();
if (m_leftmostAbove >= 0) {
#ifdef DEBUG_ALIGNMENT_VIEW
SVCERR << "AlignmentView: m_leftmostAbove = " << m_leftmostAbove
<< ", we have a relationship with the pane above us: showing "
<< "mappings in relation to that" << endl;
#endif
for (const auto &km: m_fromAboveMap) {
sv_frame_t af = km.first;
sv_frame_t bf = km.second;
if (af < m_leftmostAbove) {
#ifdef DEBUG_ALIGNMENT_VIEW
SVCERR << "AlignmentView: af " << af << " < m_leftmostAbove " << m_leftmostAbove << endl;
#endif
continue;
}
if (af > m_rightmostAbove) {
#ifdef DEBUG_ALIGNMENT_VIEW
SVCERR << "AlignmentView: af " << af << " > m_rightmostAbove " << m_rightmostAbove << endl;
#endif
continue;
}
int ax = m_above->getXForFrame(af);
int bx = m_below->getXForFrame(bf);
if (ax >= 0 || ax < w || bx >= 0 || bx < w) {
paint.drawLine(ax, 0, bx, h);
}
}
} else if (m_reference != nullptr) {
// the below has nothing in common with the above: show things
// in common with the reference instead
#ifdef DEBUG_ALIGNMENT_VIEW
SVCERR << "AlignmentView: m_leftmostAbove = " << m_leftmostAbove
<< ", we have no relationship with the pane above us: showing "
<< "mappings in relation to the reference instead" << endl;
#endif
for (const auto &km: m_fromReferenceMap) {
sv_frame_t af = km.first;
sv_frame_t bf = km.second;
int ax = m_reference->getXForFrame(af);
int bx = m_below->getXForFrame(bf);
if (ax >= 0 || ax < w || bx >= 0 || bx < w) {
paint.drawLine(ax, 0, bx, h);
}
}
}
paint.end();
}
void
AlignmentView::reconnectModels()
{
vector<ModelId> toConnect {
getSalientModel(m_above),
getSalientModel(m_below)
};
for (auto modelId: toConnect) {
if (auto model = ModelById::get(modelId)) {
auto referenceId = model->getAlignmentReference();
if (!referenceId.isNone()) {
toConnect.push_back(referenceId);
}
}
}
for (auto modelId: toConnect) {
if (auto model = ModelById::get(modelId)) {
auto ptr = model.get();
disconnect(ptr, 0, this, 0);
connect(ptr, SIGNAL(modelChanged(ModelId)),
this, SLOT(keyFramesChanged()));
connect(ptr, SIGNAL(completionChanged(ModelId)),
this, SLOT(keyFramesChanged()));
connect(ptr, SIGNAL(alignmentCompletionChanged(ModelId)),
this, SLOT(keyFramesChanged()));
}
}
}
void
AlignmentView::buildMaps()
{
#ifdef DEBUG_ALIGNMENT_VIEW
SVCERR << "AlignmentView " << getId() << "::buildMaps" << endl;
#endif
sv_frame_t resolution = 1;
set<sv_frame_t> keyFramesBelow;
for (auto f: getKeyFrames(m_below, resolution)) {
keyFramesBelow.insert(f);
}
for (sv_frame_t f : keyFramesBelow) {
sv_frame_t rf = m_below->alignToReference(f);
m_fromReferenceMap.insert({ rf, f });
}
vector<sv_frame_t> keyFrames = getKeyFrames(m_above, resolution);
// These are the most extreme leftward and rightward frames in
// "above" that have distinct corresponding frames in
// "below". Anything left of m_leftmostAbove or right of
// m_rightmostAbove maps effectively off one end or the other of
// the below view. (They don't actually map off the ends, they
// just all map to the same first/last destination frame. But we
// don't want to display their mappings, as they're just noise.)
m_leftmostAbove = -1;
m_rightmostAbove = -1;
sv_frame_t prevAf = -1;
sv_frame_t prevBf = -1;
for (sv_frame_t af : keyFrames) {
sv_frame_t rf = m_above->alignToReference(af);
sv_frame_t bf = m_below->alignFromReference(rf);
if (prevBf > 0 && bf > prevBf) {
if (m_leftmostAbove < 0) {
m_leftmostAbove = prevAf;
}
m_rightmostAbove = af;
}
prevAf = af;
prevBf = bf;
bool mappedSomething = false;
if (resolution > 1) {
if (keyFramesBelow.find(bf) == keyFramesBelow.end()) {
sv_frame_t af1 = af + resolution;
sv_frame_t rf1 = m_above->alignToReference(af1);
sv_frame_t bf1 = m_below->alignFromReference(rf1);
for (sv_frame_t probe = bf + 1; probe <= bf1; ++probe) {
if (keyFramesBelow.find(probe) != keyFramesBelow.end()) {
m_fromAboveMap.insert({ af, probe });
mappedSomething = true;
}
}
}
}
if (!mappedSomething) {
m_fromAboveMap.insert({ af, bf });
}
}
#ifdef DEBUG_ALIGNMENT_VIEW
SVCERR << "AlignmentView " << getId() << "::buildMaps: have "
<< m_fromAboveMap.size() << " mappings" << endl;
#endif
}
vector<sv_frame_t>
AlignmentView::getKeyFrames(View *view, sv_frame_t &resolution)
{
resolution = 1;
if (!view) {
return getDefaultKeyFrames();
}
ModelId m = getSalientModel(view);
auto model = ModelById::getAs<SparseOneDimensionalModel>(m);
if (!model) {
return getDefaultKeyFrames();
}
resolution = model->getResolution();
vector<sv_frame_t> keyFrames;
EventVector pp = model->getAllEvents();
for (EventVector::const_iterator pi = pp.begin(); pi != pp.end(); ++pi) {
keyFrames.push_back(pi->getFrame());
}
return keyFrames;
}
vector<sv_frame_t>
AlignmentView::getDefaultKeyFrames()
{
vector<sv_frame_t> keyFrames;
return keyFrames;
#ifdef NOT_REALLY
if (!m_above || !m_manager) return keyFrames;
sv_samplerate_t rate = m_manager->getMainModelSampleRate();
if (rate == 0) return keyFrames;
for (sv_frame_t f = m_above->getModelsStartFrame();
f <= m_above->getModelsEndFrame();
f += sv_frame_t(rate * 5 + 0.5)) {
keyFrames.push_back(f);
}
return keyFrames;
#endif
}
ModelId
AlignmentView::getSalientModel(View *view)
{
ModelId m;
// get the topmost such
for (int i = 0; i < view->getLayerCount(); ++i) {
if (qobject_cast<TimeInstantLayer *>(view->getLayer(i))) {
ModelId mm = view->getLayer(i)->getModel();
if (ModelById::isa<SparseOneDimensionalModel>(mm)) {
m = mm;
}
}
}
return m;
}
} // end namespace sv
|