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