Chris@173: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@173: Chris@173: /* Chris@173: Sonic Visualiser Chris@173: An audio file viewer and annotation editor. Chris@173: Centre for Digital Music, Queen Mary, University of London. Chris@182: This file copyright 2006 Chris Cannam and QMUL. Chris@173: Chris@173: This program is free software; you can redistribute it and/or Chris@173: modify it under the terms of the GNU General Public License as Chris@173: published by the Free Software Foundation; either version 2 of the Chris@173: License, or (at your option) any later version. See the file Chris@173: COPYING included with this distribution for more information. Chris@173: */ Chris@173: Chris@173: #include "Overview.h" Chris@173: #include "layer/Layer.h" Chris@173: #include "data/model/Model.h" Chris@173: #include "base/ZoomConstraint.h" Chris@173: Chris@173: #include Chris@173: #include Chris@173: #include Chris@173: Chris@173: using std::cerr; Chris@173: using std::endl; Chris@173: Chris@173: Overview::Overview(QWidget *w) : Chris@173: View(w, false), Chris@173: m_clickedInRange(false) Chris@173: { Chris@173: setObjectName(tr("Overview")); Chris@173: m_followPan = false; Chris@173: m_followZoom = false; Chris@173: } Chris@173: Chris@173: void Chris@173: Overview::modelChanged(size_t startFrame, size_t endFrame) Chris@173: { Chris@173: View::modelChanged(startFrame, endFrame); Chris@173: } Chris@173: Chris@173: void Chris@173: Overview::modelReplaced() Chris@173: { Chris@173: View::modelReplaced(); Chris@173: } Chris@173: Chris@173: void Chris@173: Overview::registerView(View *widget) Chris@173: { Chris@173: m_widgets.insert(widget); Chris@173: update(); Chris@173: } Chris@173: Chris@173: void Chris@173: Overview::unregisterView(View *widget) Chris@173: { Chris@173: m_widgets.erase(widget); Chris@173: update(); Chris@173: } Chris@173: Chris@173: void Chris@173: Overview::viewManagerCentreFrameChanged(void *p, unsigned long f, bool) Chris@173: { Chris@173: // std::cerr << "Overview[" << this << "]::viewManagerCentreFrameChanged(" Chris@173: // << p << ", " << f << ")" << std::endl; Chris@173: Chris@173: if (p == this) return; Chris@173: if (m_widgets.find(p) != m_widgets.end()) { Chris@173: update(); Chris@173: } Chris@173: } Chris@173: Chris@173: void Chris@173: Overview::viewManagerZoomLevelChanged(void *p, unsigned long z, bool) Chris@173: { Chris@173: if (p == this) return; Chris@173: if (m_widgets.find(p) != m_widgets.end()) { Chris@173: update(); Chris@173: } Chris@173: } Chris@173: Chris@173: void Chris@173: Overview::viewManagerPlaybackFrameChanged(unsigned long f) Chris@173: { Chris@173: bool changed = false; Chris@173: Chris@173: if (getXForFrame(m_playPointerFrame) != getXForFrame(f)) changed = true; Chris@173: m_playPointerFrame = f; Chris@173: Chris@173: if (changed) update(); Chris@173: } Chris@173: Chris@173: void Chris@173: Overview::paintEvent(QPaintEvent *e) Chris@173: { Chris@173: // Recalculate zoom in case the size of the widget has changed. Chris@173: Chris@173: size_t startFrame = getModelsStartFrame(); Chris@173: size_t frameCount = getModelsEndFrame() - getModelsStartFrame(); Chris@173: int zoomLevel = frameCount / width(); Chris@173: if (zoomLevel < 1) zoomLevel = 1; Chris@173: zoomLevel = getZoomConstraintBlockSize(zoomLevel, Chris@173: ZoomConstraint::RoundUp); Chris@173: if (zoomLevel != m_zoomLevel) { Chris@173: m_zoomLevel = zoomLevel; Chris@173: emit zoomLevelChanged(this, m_zoomLevel, m_followZoom); Chris@173: } Chris@173: size_t centreFrame = startFrame + m_zoomLevel * (width() / 2); Chris@173: if (centreFrame > (startFrame + getModelsEndFrame())/2) { Chris@173: centreFrame = (startFrame + getModelsEndFrame())/2; Chris@173: } Chris@173: if (centreFrame != m_centreFrame) { Chris@173: m_centreFrame = centreFrame; Chris@173: emit centreFrameChanged(this, m_centreFrame, false); Chris@173: } Chris@173: Chris@173: View::paintEvent(e); Chris@173: Chris@173: QPainter paint; Chris@173: paint.begin(this); Chris@173: Chris@173: QRect r(rect()); Chris@173: Chris@173: if (e) { Chris@173: r = e->rect(); Chris@173: paint.setClipRect(r); Chris@173: } Chris@173: Chris@173: paint.setPen(Qt::black); Chris@173: Chris@173: int y = 0; Chris@173: Chris@173: int prevx0 = -10; Chris@173: int prevx1 = -10; Chris@173: Chris@173: for (WidgetSet::iterator i = m_widgets.begin(); i != m_widgets.end(); ++i) { Chris@173: if (!*i) continue; Chris@173: Chris@173: View *w = (View *)*i; Chris@173: Chris@173: long f0 = w->getFrameForX(0); Chris@173: long f1 = w->getFrameForX(w->width()); Chris@173: Chris@173: int x0 = getXForFrame(f0); Chris@173: int x1 = getXForFrame(f1); Chris@173: Chris@173: if (x0 != prevx0 || x1 != prevx1) { Chris@173: y += height() / 10 + 1; Chris@173: prevx0 = x0; Chris@173: prevx1 = x1; Chris@173: } Chris@173: Chris@173: if (x1 <= x0) x1 = x0 + 1; Chris@173: Chris@173: paint.drawRect(x0, y, x1 - x0, height() - 2 * y); Chris@173: } Chris@173: Chris@173: paint.end(); Chris@173: } Chris@173: Chris@173: void Chris@173: Overview::mousePressEvent(QMouseEvent *e) Chris@173: { Chris@173: m_clickPos = e->pos(); Chris@173: for (WidgetSet::iterator i = m_widgets.begin(); i != m_widgets.end(); ++i) { Chris@173: if (*i) { Chris@173: m_clickedInRange = true; Chris@173: m_dragCentreFrame = ((View *)*i)->getCentreFrame(); Chris@173: break; Chris@173: } Chris@173: } Chris@173: } Chris@173: Chris@173: void Chris@173: Overview::mouseReleaseEvent(QMouseEvent *e) Chris@173: { Chris@173: if (m_clickedInRange) { Chris@173: mouseMoveEvent(e); Chris@173: } Chris@173: m_clickedInRange = false; Chris@173: } Chris@173: Chris@173: void Chris@173: Overview::mouseMoveEvent(QMouseEvent *e) Chris@173: { Chris@173: if (!m_clickedInRange) return; Chris@173: Chris@173: long xoff = int(e->x()) - int(m_clickPos.x()); Chris@173: long frameOff = xoff * m_zoomLevel; Chris@173: Chris@173: size_t newCentreFrame = m_dragCentreFrame; Chris@173: if (frameOff > 0) { Chris@173: newCentreFrame += frameOff; Chris@173: } else if (newCentreFrame >= size_t(-frameOff)) { Chris@173: newCentreFrame += frameOff; Chris@173: } else { Chris@173: newCentreFrame = 0; Chris@173: } Chris@173: Chris@173: if (newCentreFrame >= getModelsEndFrame()) { Chris@173: newCentreFrame = getModelsEndFrame(); Chris@173: if (newCentreFrame > 0) --newCentreFrame; Chris@173: } Chris@173: Chris@173: if (std::max(m_centreFrame, newCentreFrame) - Chris@173: std::min(m_centreFrame, newCentreFrame) > size_t(m_zoomLevel)) { Chris@173: emit centreFrameChanged(this, newCentreFrame, true); Chris@173: } Chris@173: } Chris@173: Chris@173: