annotate view/Overview.cpp @ 214:8520b7918104

* Fix overzealous cacheing in waveform layer that was leading to inaccurate positioning of view rects & playback pointer on overview widget sometimes
author Chris Cannam
date Thu, 01 Mar 2007 17:12:50 +0000
parents df791d8c8f58
children cd81066ac7ad
rev   line source
Chris@173 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@173 2
Chris@173 3 /*
Chris@173 4 Sonic Visualiser
Chris@173 5 An audio file viewer and annotation editor.
Chris@173 6 Centre for Digital Music, Queen Mary, University of London.
Chris@182 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@173 8
Chris@173 9 This program is free software; you can redistribute it and/or
Chris@173 10 modify it under the terms of the GNU General Public License as
Chris@173 11 published by the Free Software Foundation; either version 2 of the
Chris@173 12 License, or (at your option) any later version. See the file
Chris@173 13 COPYING included with this distribution for more information.
Chris@173 14 */
Chris@173 15
Chris@173 16 #include "Overview.h"
Chris@173 17 #include "layer/Layer.h"
Chris@173 18 #include "data/model/Model.h"
Chris@173 19 #include "base/ZoomConstraint.h"
Chris@173 20
Chris@173 21 #include <QPaintEvent>
Chris@173 22 #include <QPainter>
Chris@173 23 #include <iostream>
Chris@173 24
Chris@173 25 using std::cerr;
Chris@173 26 using std::endl;
Chris@173 27
Chris@173 28 Overview::Overview(QWidget *w) :
Chris@173 29 View(w, false),
Chris@173 30 m_clickedInRange(false)
Chris@173 31 {
Chris@173 32 setObjectName(tr("Overview"));
Chris@173 33 m_followPan = false;
Chris@173 34 m_followZoom = false;
Chris@211 35 setPlaybackFollow(PlaybackIgnore);
Chris@173 36 }
Chris@173 37
Chris@173 38 void
Chris@173 39 Overview::modelChanged(size_t startFrame, size_t endFrame)
Chris@173 40 {
Chris@173 41 View::modelChanged(startFrame, endFrame);
Chris@173 42 }
Chris@173 43
Chris@173 44 void
Chris@173 45 Overview::modelReplaced()
Chris@173 46 {
Chris@173 47 View::modelReplaced();
Chris@173 48 }
Chris@173 49
Chris@173 50 void
Chris@211 51 Overview::registerView(View *view)
Chris@173 52 {
Chris@211 53 m_views.insert(view);
Chris@173 54 update();
Chris@173 55 }
Chris@173 56
Chris@173 57 void
Chris@211 58 Overview::unregisterView(View *view)
Chris@173 59 {
Chris@211 60 m_views.erase(view);
Chris@173 61 update();
Chris@173 62 }
Chris@173 63
Chris@173 64 void
Chris@211 65 Overview::globalCentreFrameChanged(unsigned long f)
Chris@173 66 {
Chris@211 67 update();
Chris@211 68 }
Chris@173 69
Chris@211 70 void
Chris@211 71 Overview::viewCentreFrameChanged(View *v, unsigned long f)
Chris@211 72 {
Chris@211 73 if (m_views.find(v) != m_views.end()) {
Chris@173 74 update();
Chris@173 75 }
Chris@211 76 }
Chris@173 77
Chris@173 78 void
Chris@173 79 Overview::viewManagerZoomLevelChanged(void *p, unsigned long z, bool)
Chris@173 80 {
Chris@173 81 if (p == this) return;
Chris@211 82 View *v = (View *)p;
Chris@211 83 if (m_views.find(v) != m_views.end()) {
Chris@173 84 update();
Chris@173 85 }
Chris@173 86 }
Chris@173 87
Chris@173 88 void
Chris@173 89 Overview::viewManagerPlaybackFrameChanged(unsigned long f)
Chris@173 90 {
Chris@173 91 bool changed = false;
Chris@173 92
Chris@173 93 if (getXForFrame(m_playPointerFrame) != getXForFrame(f)) changed = true;
Chris@173 94 m_playPointerFrame = f;
Chris@173 95
Chris@173 96 if (changed) update();
Chris@173 97 }
Chris@173 98
Chris@173 99 void
Chris@173 100 Overview::paintEvent(QPaintEvent *e)
Chris@173 101 {
Chris@173 102 // Recalculate zoom in case the size of the widget has changed.
Chris@173 103
Chris@214 104 // std::cerr << "Overview::paintEvent: width is " << width() << ", centre frame " << m_centreFrame << std::endl;
Chris@214 105
Chris@173 106 size_t startFrame = getModelsStartFrame();
Chris@173 107 size_t frameCount = getModelsEndFrame() - getModelsStartFrame();
Chris@173 108 int zoomLevel = frameCount / width();
Chris@173 109 if (zoomLevel < 1) zoomLevel = 1;
Chris@173 110 zoomLevel = getZoomConstraintBlockSize(zoomLevel,
Chris@173 111 ZoomConstraint::RoundUp);
Chris@173 112 if (zoomLevel != m_zoomLevel) {
Chris@173 113 m_zoomLevel = zoomLevel;
Chris@173 114 emit zoomLevelChanged(this, m_zoomLevel, m_followZoom);
Chris@173 115 }
Chris@173 116 size_t centreFrame = startFrame + m_zoomLevel * (width() / 2);
Chris@173 117 if (centreFrame > (startFrame + getModelsEndFrame())/2) {
Chris@173 118 centreFrame = (startFrame + getModelsEndFrame())/2;
Chris@173 119 }
Chris@173 120 if (centreFrame != m_centreFrame) {
Chris@214 121 // std::cerr << "Overview::paintEvent: Centre frame changed from "
Chris@214 122 // << m_centreFrame << " to " << centreFrame << " and thus start frame from " << getStartFrame();
Chris@173 123 m_centreFrame = centreFrame;
Chris@214 124 // std::cerr << " to " << getStartFrame() << std::endl;
Chris@211 125 emit centreFrameChanged(m_centreFrame, false, PlaybackIgnore);
Chris@173 126 }
Chris@173 127
Chris@173 128 View::paintEvent(e);
Chris@173 129
Chris@173 130 QPainter paint;
Chris@173 131 paint.begin(this);
Chris@173 132
Chris@173 133 QRect r(rect());
Chris@173 134
Chris@173 135 if (e) {
Chris@173 136 r = e->rect();
Chris@173 137 paint.setClipRect(r);
Chris@173 138 }
Chris@173 139
Chris@173 140 paint.setPen(Qt::black);
Chris@173 141
Chris@173 142 int y = 0;
Chris@173 143
Chris@173 144 int prevx0 = -10;
Chris@173 145 int prevx1 = -10;
Chris@173 146
Chris@211 147 for (ViewSet::iterator i = m_views.begin(); i != m_views.end(); ++i) {
Chris@173 148 if (!*i) continue;
Chris@173 149
Chris@173 150 View *w = (View *)*i;
Chris@173 151
Chris@173 152 long f0 = w->getFrameForX(0);
Chris@173 153 long f1 = w->getFrameForX(w->width());
Chris@173 154
Chris@173 155 int x0 = getXForFrame(f0);
Chris@173 156 int x1 = getXForFrame(f1);
Chris@173 157
Chris@173 158 if (x0 != prevx0 || x1 != prevx1) {
Chris@173 159 y += height() / 10 + 1;
Chris@173 160 prevx0 = x0;
Chris@173 161 prevx1 = x1;
Chris@173 162 }
Chris@173 163
Chris@173 164 if (x1 <= x0) x1 = x0 + 1;
Chris@173 165
Chris@173 166 paint.drawRect(x0, y, x1 - x0, height() - 2 * y);
Chris@173 167 }
Chris@173 168
Chris@173 169 paint.end();
Chris@173 170 }
Chris@173 171
Chris@173 172 void
Chris@173 173 Overview::mousePressEvent(QMouseEvent *e)
Chris@173 174 {
Chris@173 175 m_clickPos = e->pos();
Chris@211 176 for (ViewSet::iterator i = m_views.begin(); i != m_views.end(); ++i) {
Chris@173 177 if (*i) {
Chris@173 178 m_clickedInRange = true;
Chris@173 179 m_dragCentreFrame = ((View *)*i)->getCentreFrame();
Chris@173 180 break;
Chris@173 181 }
Chris@173 182 }
Chris@173 183 }
Chris@173 184
Chris@173 185 void
Chris@173 186 Overview::mouseReleaseEvent(QMouseEvent *e)
Chris@173 187 {
Chris@173 188 if (m_clickedInRange) {
Chris@173 189 mouseMoveEvent(e);
Chris@173 190 }
Chris@173 191 m_clickedInRange = false;
Chris@173 192 }
Chris@173 193
Chris@173 194 void
Chris@173 195 Overview::mouseMoveEvent(QMouseEvent *e)
Chris@173 196 {
Chris@173 197 if (!m_clickedInRange) return;
Chris@173 198
Chris@173 199 long xoff = int(e->x()) - int(m_clickPos.x());
Chris@173 200 long frameOff = xoff * m_zoomLevel;
Chris@173 201
Chris@173 202 size_t newCentreFrame = m_dragCentreFrame;
Chris@173 203 if (frameOff > 0) {
Chris@173 204 newCentreFrame += frameOff;
Chris@173 205 } else if (newCentreFrame >= size_t(-frameOff)) {
Chris@173 206 newCentreFrame += frameOff;
Chris@173 207 } else {
Chris@173 208 newCentreFrame = 0;
Chris@173 209 }
Chris@173 210
Chris@173 211 if (newCentreFrame >= getModelsEndFrame()) {
Chris@173 212 newCentreFrame = getModelsEndFrame();
Chris@173 213 if (newCentreFrame > 0) --newCentreFrame;
Chris@173 214 }
Chris@173 215
Chris@173 216 if (std::max(m_centreFrame, newCentreFrame) -
Chris@173 217 std::min(m_centreFrame, newCentreFrame) > size_t(m_zoomLevel)) {
Chris@213 218 emit centreFrameChanged(newCentreFrame, true, PlaybackScrollContinuous);
Chris@173 219 }
Chris@173 220 }
Chris@173 221
Chris@189 222 void
Chris@189 223 Overview::mouseDoubleClickEvent(QMouseEvent *e)
Chris@189 224 {
Chris@189 225 long frame = getFrameForX(e->x());
Chris@213 226 emit centreFrameChanged(frame, true, PlaybackScrollContinuous);
Chris@189 227 }
Chris@173 228
Chris@189 229 void
Chris@189 230 Overview::enterEvent(QEvent *)
Chris@189 231 {
Chris@189 232 emit contextHelpChanged(tr("Click and drag to navigate; double-click to jump"));
Chris@189 233 }
Chris@189 234
Chris@189 235 void
Chris@189 236 Overview::leaveEvent(QEvent *)
Chris@189 237 {
Chris@189 238 emit contextHelpChanged("");
Chris@189 239 }
Chris@189 240
Chris@189 241