annotate view/Overview.cpp @ 850:0c993091a4c9

Fix uninitialised variable (from coverity scan)
author Chris Cannam
date Wed, 03 Sep 2014 11:48:18 +0100
parents 7792b7667f74
children c17719e488c9
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@643 25 //#define DEBUG_OVERVIEW 1
Chris@642 26
Chris@682 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@274 36 m_modelTestTime.start();
Chris@173 37 }
Chris@173 38
Chris@173 39 void
Chris@806 40 Overview::modelChangedWithin(int startFrame, int endFrame)
Chris@173 41 {
Chris@253 42 bool zoomChanged = false;
Chris@253 43
Chris@806 44 int frameCount = getModelsEndFrame() - getModelsStartFrame();
Chris@253 45 int zoomLevel = frameCount / width();
Chris@253 46 if (zoomLevel < 1) zoomLevel = 1;
Chris@253 47 zoomLevel = getZoomConstraintBlockSize(zoomLevel,
Chris@253 48 ZoomConstraint::RoundUp);
Chris@253 49 if (zoomLevel != m_zoomLevel) {
Chris@253 50 zoomChanged = true;
Chris@253 51 }
Chris@253 52
Chris@253 53 if (!zoomChanged) {
Chris@274 54 if (m_modelTestTime.elapsed() < 1000) {
Chris@835 55 for (LayerList::const_iterator i = m_layerStack.begin();
Chris@835 56 i != m_layerStack.end(); ++i) {
Chris@274 57 if ((*i)->getModel() &&
Chris@389 58 (!(*i)->getModel()->isOK() ||
Chris@389 59 !(*i)->getModel()->isReady())) {
Chris@274 60 return;
Chris@274 61 }
Chris@253 62 }
Chris@274 63 } else {
Chris@274 64 m_modelTestTime.restart();
Chris@253 65 }
Chris@253 66 }
Chris@253 67
Chris@806 68 View::modelChangedWithin(startFrame, endFrame);
Chris@173 69 }
Chris@173 70
Chris@173 71 void
Chris@173 72 Overview::modelReplaced()
Chris@173 73 {
Chris@339 74 m_playPointerFrame = getAlignedPlaybackFrame();
Chris@173 75 View::modelReplaced();
Chris@173 76 }
Chris@173 77
Chris@173 78 void
Chris@211 79 Overview::registerView(View *view)
Chris@173 80 {
Chris@211 81 m_views.insert(view);
Chris@173 82 update();
Chris@173 83 }
Chris@173 84
Chris@173 85 void
Chris@211 86 Overview::unregisterView(View *view)
Chris@173 87 {
Chris@211 88 m_views.erase(view);
Chris@173 89 update();
Chris@173 90 }
Chris@173 91
Chris@173 92 void
Chris@806 93 Overview::globalCentreFrameChanged(int
Chris@806 94 #ifdef DEBUG_OVERVIEW
Chris@806 95 f
Chris@806 96 #endif
Chris@806 97 )
Chris@173 98 {
Chris@642 99 #ifdef DEBUG_OVERVIEW
Chris@682 100 cerr << "Overview::globalCentreFrameChanged: " << f << endl;
Chris@642 101 #endif
Chris@211 102 update();
Chris@211 103 }
Chris@173 104
Chris@211 105 void
Chris@806 106 Overview::viewCentreFrameChanged(View *v, int
Chris@806 107 #ifdef DEBUG_OVERVIEW
Chris@806 108 f
Chris@806 109 #endif
Chris@806 110 )
Chris@211 111 {
Chris@642 112 #ifdef DEBUG_OVERVIEW
Chris@682 113 cerr << "Overview[" << this << "]::viewCentreFrameChanged(" << v << "): " << f << endl;
Chris@642 114 #endif
Chris@211 115 if (m_views.find(v) != m_views.end()) {
Chris@173 116 update();
Chris@173 117 }
Chris@211 118 }
Chris@173 119
Chris@173 120 void
Chris@806 121 Overview::viewZoomLevelChanged(View *v, int, bool)
Chris@173 122 {
Chris@222 123 if (v == this) return;
Chris@211 124 if (m_views.find(v) != m_views.end()) {
Chris@173 125 update();
Chris@173 126 }
Chris@173 127 }
Chris@173 128
Chris@173 129 void
Chris@806 130 Overview::viewManagerPlaybackFrameChanged(int f)
Chris@173 131 {
Chris@642 132 #ifdef DEBUG_OVERVIEW
Chris@682 133 cerr << "Overview[" << this << "]::viewManagerPlaybackFrameChanged(" << f << "): " << f << endl;
Chris@642 134 #endif
Chris@642 135
Chris@173 136 bool changed = false;
Chris@173 137
Chris@339 138 f = getAlignedPlaybackFrame();
Chris@339 139
Chris@173 140 if (getXForFrame(m_playPointerFrame) != getXForFrame(f)) changed = true;
Chris@173 141 m_playPointerFrame = f;
Chris@173 142
Chris@173 143 if (changed) update();
Chris@173 144 }
Chris@173 145
Chris@173 146 void
Chris@173 147 Overview::paintEvent(QPaintEvent *e)
Chris@173 148 {
Chris@173 149 // Recalculate zoom in case the size of the widget has changed.
Chris@173 150
Chris@642 151 #ifdef DEBUG_OVERVIEW
Chris@682 152 cerr << "Overview::paintEvent: width is " << width() << ", centre frame " << m_centreFrame << endl;
Chris@642 153 #endif
Chris@214 154
Chris@806 155 int startFrame = getModelsStartFrame();
Chris@806 156 int frameCount = getModelsEndFrame() - getModelsStartFrame();
Chris@173 157 int zoomLevel = frameCount / width();
Chris@173 158 if (zoomLevel < 1) zoomLevel = 1;
Chris@173 159 zoomLevel = getZoomConstraintBlockSize(zoomLevel,
Chris@173 160 ZoomConstraint::RoundUp);
Chris@173 161 if (zoomLevel != m_zoomLevel) {
Chris@173 162 m_zoomLevel = zoomLevel;
Chris@222 163 emit zoomLevelChanged(m_zoomLevel, m_followZoom);
Chris@173 164 }
Chris@253 165
Chris@806 166 int centreFrame = startFrame + m_zoomLevel * (width() / 2);
Chris@173 167 if (centreFrame > (startFrame + getModelsEndFrame())/2) {
Chris@173 168 centreFrame = (startFrame + getModelsEndFrame())/2;
Chris@173 169 }
Chris@173 170 if (centreFrame != m_centreFrame) {
Chris@642 171 #ifdef DEBUG_OVERVIEW
Chris@682 172 cerr << "Overview::paintEvent: Centre frame changed from "
Chris@642 173 << m_centreFrame << " to " << centreFrame << " and thus start frame from " << getStartFrame();
Chris@642 174 #endif
Chris@173 175 m_centreFrame = centreFrame;
Chris@642 176 #ifdef DEBUG_OVERVIEW
Chris@682 177 cerr << " to " << getStartFrame() << endl;
Chris@642 178 #endif
Chris@211 179 emit centreFrameChanged(m_centreFrame, false, PlaybackIgnore);
Chris@173 180 }
Chris@173 181
Chris@173 182 View::paintEvent(e);
Chris@173 183
Chris@173 184 QPainter paint;
Chris@173 185 paint.begin(this);
Chris@173 186
Chris@173 187 QRect r(rect());
Chris@173 188
Chris@173 189 if (e) {
Chris@173 190 r = e->rect();
Chris@173 191 paint.setClipRect(r);
Chris@173 192 }
Chris@173 193
Chris@287 194 paint.setPen(getForeground());
Chris@173 195
Chris@173 196 int y = 0;
Chris@173 197
Chris@173 198 int prevx0 = -10;
Chris@173 199 int prevx1 = -10;
Chris@173 200
Chris@211 201 for (ViewSet::iterator i = m_views.begin(); i != m_views.end(); ++i) {
Chris@173 202 if (!*i) continue;
Chris@173 203
Chris@173 204 View *w = (View *)*i;
Chris@173 205
Chris@806 206 int f0 = w->getFrameForX(0);
Chris@806 207 int f1 = w->getFrameForX(w->width());
Chris@173 208
Chris@339 209 if (f0 >= 0) {
Chris@806 210 int rf0 = w->alignToReference(f0);
Chris@339 211 f0 = alignFromReference(rf0);
Chris@339 212 }
Chris@339 213 if (f1 >= 0) {
Chris@806 214 int rf1 = w->alignToReference(f1);
Chris@339 215 f1 = alignFromReference(rf1);
Chris@339 216 }
Chris@339 217
Chris@173 218 int x0 = getXForFrame(f0);
Chris@173 219 int x1 = getXForFrame(f1);
Chris@173 220
Chris@173 221 if (x0 != prevx0 || x1 != prevx1) {
Chris@173 222 y += height() / 10 + 1;
Chris@173 223 prevx0 = x0;
Chris@173 224 prevx1 = x1;
Chris@173 225 }
Chris@173 226
Chris@173 227 if (x1 <= x0) x1 = x0 + 1;
Chris@173 228
Chris@173 229 paint.drawRect(x0, y, x1 - x0, height() - 2 * y);
Chris@173 230 }
Chris@173 231
Chris@173 232 paint.end();
Chris@173 233 }
Chris@173 234
Chris@173 235 void
Chris@173 236 Overview::mousePressEvent(QMouseEvent *e)
Chris@173 237 {
Chris@173 238 m_clickPos = e->pos();
Chris@806 239 int clickFrame = getFrameForX(m_clickPos.x());
Chris@339 240 if (clickFrame > 0) m_dragCentreFrame = clickFrame;
Chris@339 241 else m_dragCentreFrame = 0;
Chris@339 242 m_clickedInRange = true;
Chris@339 243
Chris@211 244 for (ViewSet::iterator i = m_views.begin(); i != m_views.end(); ++i) {
Chris@339 245 if (*i && (*i)->getAligningModel() == getAligningModel()) {
Chris@339 246 m_dragCentreFrame = (*i)->getCentreFrame();
Chris@339 247 break;
Chris@339 248 }
Chris@173 249 }
Chris@173 250 }
Chris@173 251
Chris@173 252 void
Chris@173 253 Overview::mouseReleaseEvent(QMouseEvent *e)
Chris@173 254 {
Chris@173 255 if (m_clickedInRange) {
Chris@173 256 mouseMoveEvent(e);
Chris@173 257 }
Chris@173 258 m_clickedInRange = false;
Chris@173 259 }
Chris@173 260
Chris@173 261 void
Chris@173 262 Overview::mouseMoveEvent(QMouseEvent *e)
Chris@173 263 {
Chris@173 264 if (!m_clickedInRange) return;
Chris@173 265
Chris@806 266 int xoff = int(e->x()) - int(m_clickPos.x());
Chris@806 267 int frameOff = xoff * m_zoomLevel;
Chris@173 268
Chris@806 269 int newCentreFrame = m_dragCentreFrame;
Chris@173 270 if (frameOff > 0) {
Chris@173 271 newCentreFrame += frameOff;
Chris@806 272 } else if (newCentreFrame >= int(-frameOff)) {
Chris@173 273 newCentreFrame += frameOff;
Chris@173 274 } else {
Chris@173 275 newCentreFrame = 0;
Chris@173 276 }
Chris@173 277
Chris@173 278 if (newCentreFrame >= getModelsEndFrame()) {
Chris@173 279 newCentreFrame = getModelsEndFrame();
Chris@173 280 if (newCentreFrame > 0) --newCentreFrame;
Chris@173 281 }
Chris@173 282
Chris@173 283 if (std::max(m_centreFrame, newCentreFrame) -
Chris@806 284 std::min(m_centreFrame, newCentreFrame) > int(m_zoomLevel)) {
Chris@806 285 int rf = alignToReference(newCentreFrame);
Chris@643 286 #ifdef DEBUG_OVERVIEW
Chris@682 287 cerr << "Overview::mouseMoveEvent: x " << e->x() << " and click x " << m_clickPos.x() << " -> frame " << newCentreFrame << " -> rf " << rf << endl;
Chris@643 288 #endif
Chris@817 289 if (m_followPlay == PlaybackScrollContinuous ||
Chris@817 290 m_followPlay == PlaybackScrollPageWithCentre) {
Chris@817 291 emit centreFrameChanged(rf, true, PlaybackScrollContinuous);
Chris@817 292 } else {
Chris@817 293 emit centreFrameChanged(rf, true, PlaybackIgnore);
Chris@817 294 }
Chris@173 295 }
Chris@173 296 }
Chris@173 297
Chris@189 298 void
Chris@189 299 Overview::mouseDoubleClickEvent(QMouseEvent *e)
Chris@189 300 {
Chris@806 301 int frame = getFrameForX(e->x());
Chris@806 302 int rf = 0;
Chris@339 303 if (frame > 0) rf = alignToReference(frame);
Chris@643 304 #ifdef DEBUG_OVERVIEW
Chris@682 305 cerr << "Overview::mouseDoubleClickEvent: frame " << frame << " -> rf " << rf << endl;
Chris@643 306 #endif
Chris@643 307 m_clickedInRange = false; // we're not starting a drag with the second click
Chris@339 308 emit centreFrameChanged(rf, true, PlaybackScrollContinuous);
Chris@189 309 }
Chris@173 310
Chris@189 311 void
Chris@189 312 Overview::enterEvent(QEvent *)
Chris@189 313 {
Chris@189 314 emit contextHelpChanged(tr("Click and drag to navigate; double-click to jump"));
Chris@189 315 }
Chris@189 316
Chris@189 317 void
Chris@189 318 Overview::leaveEvent(QEvent *)
Chris@189 319 {
Chris@189 320 emit contextHelpChanged("");
Chris@189 321 }
Chris@189 322
Chris@189 323