Chris@867: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@867: Chris@867: /* Chris@867: Sonic Visualiser Chris@867: An audio file viewer and annotation editor. Chris@867: Centre for Digital Music, Queen Mary, University of London. Chris@867: This file copyright 2006-2014 Chris Cannam and QMUL. Chris@867: Chris@867: This program is free software; you can redistribute it and/or Chris@867: modify it under the terms of the GNU General Public License as Chris@867: published by the Free Software Foundation; either version 2 of the Chris@867: License, or (at your option) any later version. See the file Chris@867: COPYING included with this distribution for more information. Chris@867: */ Chris@867: Chris@867: #include "AlignmentView.h" Chris@867: Chris@867: #include Chris@867: Chris@868: #include "data/model/SparseOneDimensionalModel.h" Chris@868: Chris@868: #include "layer/TimeInstantLayer.h" Chris@868: Chris@867: using std::vector; Chris@867: Chris@867: AlignmentView::AlignmentView(QWidget *w) : Chris@867: View(w, false), Chris@867: m_above(0), Chris@867: m_below(0) Chris@867: { Chris@867: setObjectName(tr("AlignmentView")); Chris@867: } Chris@867: Chris@867: void Chris@976: AlignmentView::globalCentreFrameChanged(sv_frame_t f) Chris@867: { Chris@867: View::globalCentreFrameChanged(f); Chris@867: update(); Chris@867: } Chris@867: Chris@867: void Chris@976: AlignmentView::viewCentreFrameChanged(View *v, sv_frame_t f) Chris@867: { Chris@867: View::viewCentreFrameChanged(v, f); Chris@867: if (v == m_above) { Chris@867: m_centreFrame = f; Chris@867: update(); Chris@867: } else if (v == m_below) { Chris@867: update(); Chris@867: } Chris@867: } Chris@867: Chris@867: void Chris@976: AlignmentView::viewManagerPlaybackFrameChanged(sv_frame_t) Chris@867: { Chris@867: update(); Chris@867: } Chris@867: Chris@867: void Chris@867: AlignmentView::viewAboveZoomLevelChanged(int level, bool) Chris@867: { Chris@867: m_zoomLevel = level; Chris@867: update(); Chris@867: } Chris@867: Chris@867: void Chris@867: AlignmentView::viewBelowZoomLevelChanged(int, bool) Chris@867: { Chris@867: update(); Chris@867: } Chris@867: Chris@867: void Chris@867: AlignmentView::setViewAbove(View *v) Chris@867: { Chris@867: if (m_above) { Chris@867: disconnect(m_above, 0, this, 0); Chris@867: } Chris@867: Chris@867: m_above = v; Chris@867: Chris@867: if (m_above) { Chris@867: connect(m_above, Chris@867: SIGNAL(zoomLevelChanged(int, bool)), Chris@867: this, Chris@867: SLOT(viewAboveZoomLevelChanged(int, bool))); Chris@867: } Chris@867: } Chris@867: Chris@867: void Chris@867: AlignmentView::setViewBelow(View *v) Chris@867: { Chris@867: if (m_below) { Chris@867: disconnect(m_below, 0, this, 0); Chris@867: } Chris@867: Chris@867: m_below = v; Chris@867: Chris@867: if (m_below) { Chris@867: connect(m_below, Chris@867: SIGNAL(zoomLevelChanged(int, bool)), Chris@867: this, Chris@867: SLOT(viewBelowZoomLevelChanged(int, bool))); Chris@867: } Chris@867: } Chris@867: Chris@867: void Chris@867: AlignmentView::paintEvent(QPaintEvent *) Chris@867: { Chris@867: if (m_above == 0 || m_below == 0 || !m_manager) return; Chris@867: Chris@867: bool darkPalette = false; Chris@867: if (m_manager) darkPalette = m_manager->getGlobalDarkBackground(); Chris@867: Chris@881: QColor fg, bg; Chris@881: if (darkPalette) { Chris@881: fg = Qt::gray; Chris@881: bg = Qt::black; Chris@881: } else { Chris@881: fg = Qt::black; Chris@881: bg = Qt::gray; Chris@881: } Chris@867: Chris@867: QPainter paint(this); Chris@867: paint.setPen(QPen(fg, 2)); Chris@867: paint.setBrush(Qt::NoBrush); Chris@867: paint.setRenderHint(QPainter::Antialiasing, true); Chris@867: Chris@867: paint.fillRect(rect(), bg); Chris@867: Chris@976: vector keyFrames = getKeyFrames(); Chris@867: Chris@976: foreach (sv_frame_t f, keyFrames) { Chris@882: int ax = m_above->getXForFrame(f); Chris@976: sv_frame_t rf = m_above->alignToReference(f); Chris@976: sv_frame_t bf = m_below->alignFromReference(rf); Chris@867: int bx = m_below->getXForFrame(bf); Chris@867: paint.drawLine(ax, 0, bx, height()); Chris@867: } Chris@867: Chris@867: paint.end(); Chris@867: } Chris@867: Chris@976: vector Chris@868: AlignmentView::getKeyFrames() Chris@868: { Chris@868: if (!m_above) { Chris@868: return getDefaultKeyFrames(); Chris@868: } Chris@868: Chris@868: SparseOneDimensionalModel *m = 0; Chris@868: Chris@868: // get the topmost such Chris@868: for (int i = 0; i < m_above->getLayerCount(); ++i) { Chris@868: if (qobject_cast(m_above->getLayer(i))) { Chris@868: SparseOneDimensionalModel *mm = Chris@868: qobject_cast Chris@868: (m_above->getLayer(i)->getModel()); Chris@868: if (mm) m = mm; Chris@868: } Chris@868: } Chris@868: Chris@868: if (!m) { Chris@868: return getDefaultKeyFrames(); Chris@868: } Chris@868: Chris@976: vector keyFrames; Chris@868: Chris@868: const SparseOneDimensionalModel::PointList pp = m->getPoints(); Chris@868: for (SparseOneDimensionalModel::PointList::const_iterator pi = pp.begin(); Chris@868: pi != pp.end(); ++pi) { Chris@868: keyFrames.push_back(pi->frame); Chris@868: } Chris@868: Chris@868: return keyFrames; Chris@868: } Chris@868: Chris@976: vector Chris@868: AlignmentView::getDefaultKeyFrames() Chris@868: { Chris@976: vector keyFrames; Chris@868: Chris@868: if (!m_above || !m_manager) return keyFrames; Chris@868: Chris@976: sv_samplerate_t rate = m_manager->getMainModelSampleRate(); Chris@868: if (rate == 0) return keyFrames; Chris@868: Chris@976: for (sv_frame_t f = m_above->getModelsStartFrame(); Chris@868: f <= m_above->getModelsEndFrame(); Chris@976: f += sv_frame_t(rate * 5 + 0.5)) { Chris@868: keyFrames.push_back(f); Chris@868: } Chris@868: Chris@868: return keyFrames; Chris@868: } Chris@868: Chris@868: Chris@868: Chris@868: Chris@868: Chris@868: