annotate view/Overview.cpp @ 1324:13d9b422f7fe zoom

Merge from default branch
author Chris Cannam
date Mon, 17 Sep 2018 13:51:31 +0100
parents 57d192e26331 a34a2a25907c
children bc2cb82050a0
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@854 30 m_clickedInRange(false),
Chris@854 31 m_dragCentreFrame(0)
Chris@173 32 {
Chris@173 33 setObjectName(tr("Overview"));
Chris@173 34 m_followPan = false;
Chris@173 35 m_followZoom = false;
Chris@211 36 setPlaybackFollow(PlaybackIgnore);
Chris@274 37 m_modelTestTime.start();
Chris@965 38
Chris@965 39 bool light = hasLightBackground();
Chris@965 40 if (light) m_boxColour = Qt::darkGray;
Chris@965 41 else m_boxColour = Qt::lightGray;
Chris@173 42 }
Chris@173 43
Chris@173 44 void
Chris@908 45 Overview::modelChangedWithin(sv_frame_t startFrame, sv_frame_t endFrame)
Chris@173 46 {
Chris@253 47 bool zoomChanged = false;
Chris@253 48
Chris@908 49 sv_frame_t frameCount = getModelsEndFrame() - getModelsStartFrame();
Chris@1183 50 ZoomLevel zoomLevel { ZoomLevel::FramesPerPixel, int(frameCount / width()) };
Chris@1183 51 if (zoomLevel.level < 1) zoomLevel.level = 1;
Chris@1183 52 zoomLevel = getZoomConstraintBlockSize(zoomLevel, ZoomConstraint::RoundUp);
Chris@253 53 if (zoomLevel != m_zoomLevel) {
Chris@253 54 zoomChanged = true;
Chris@253 55 }
Chris@253 56
Chris@253 57 if (!zoomChanged) {
Chris@274 58 if (m_modelTestTime.elapsed() < 1000) {
Chris@835 59 for (LayerList::const_iterator i = m_layerStack.begin();
Chris@835 60 i != m_layerStack.end(); ++i) {
Chris@274 61 if ((*i)->getModel() &&
Chris@389 62 (!(*i)->getModel()->isOK() ||
Chris@389 63 !(*i)->getModel()->isReady())) {
Chris@274 64 return;
Chris@274 65 }
Chris@253 66 }
Chris@274 67 } else {
Chris@274 68 m_modelTestTime.restart();
Chris@253 69 }
Chris@253 70 }
Chris@253 71
Chris@806 72 View::modelChangedWithin(startFrame, endFrame);
Chris@173 73 }
Chris@173 74
Chris@173 75 void
Chris@173 76 Overview::modelReplaced()
Chris@173 77 {
Chris@339 78 m_playPointerFrame = getAlignedPlaybackFrame();
Chris@173 79 View::modelReplaced();
Chris@173 80 }
Chris@173 81
Chris@173 82 void
Chris@211 83 Overview::registerView(View *view)
Chris@173 84 {
Chris@211 85 m_views.insert(view);
Chris@173 86 update();
Chris@173 87 }
Chris@173 88
Chris@173 89 void
Chris@211 90 Overview::unregisterView(View *view)
Chris@173 91 {
Chris@211 92 m_views.erase(view);
Chris@173 93 update();
Chris@173 94 }
Chris@173 95
Chris@173 96 void
Chris@908 97 Overview::globalCentreFrameChanged(sv_frame_t
Chris@806 98 #ifdef DEBUG_OVERVIEW
Chris@806 99 f
Chris@806 100 #endif
Chris@806 101 )
Chris@173 102 {
Chris@642 103 #ifdef DEBUG_OVERVIEW
Chris@682 104 cerr << "Overview::globalCentreFrameChanged: " << f << endl;
Chris@642 105 #endif
Chris@211 106 update();
Chris@211 107 }
Chris@173 108
Chris@211 109 void
Chris@908 110 Overview::viewCentreFrameChanged(View *v, sv_frame_t
Chris@806 111 #ifdef DEBUG_OVERVIEW
Chris@806 112 f
Chris@806 113 #endif
Chris@806 114 )
Chris@211 115 {
Chris@642 116 #ifdef DEBUG_OVERVIEW
Chris@682 117 cerr << "Overview[" << this << "]::viewCentreFrameChanged(" << v << "): " << f << endl;
Chris@642 118 #endif
Chris@211 119 if (m_views.find(v) != m_views.end()) {
Chris@1266 120 update();
Chris@173 121 }
Chris@211 122 }
Chris@173 123
Chris@173 124 void
Chris@806 125 Overview::viewZoomLevelChanged(View *v, int, bool)
Chris@173 126 {
Chris@222 127 if (v == this) return;
Chris@211 128 if (m_views.find(v) != m_views.end()) {
Chris@1266 129 update();
Chris@173 130 }
Chris@173 131 }
Chris@173 132
Chris@173 133 void
Chris@908 134 Overview::viewManagerPlaybackFrameChanged(sv_frame_t f)
Chris@173 135 {
Chris@642 136 #ifdef DEBUG_OVERVIEW
Chris@682 137 cerr << "Overview[" << this << "]::viewManagerPlaybackFrameChanged(" << f << "): " << f << endl;
Chris@642 138 #endif
Chris@642 139
Chris@173 140 bool changed = false;
Chris@173 141
Chris@339 142 f = getAlignedPlaybackFrame();
Chris@339 143
Chris@173 144 if (getXForFrame(m_playPointerFrame) != getXForFrame(f)) changed = true;
Chris@173 145 m_playPointerFrame = f;
Chris@173 146
Chris@173 147 if (changed) update();
Chris@173 148 }
Chris@173 149
Chris@871 150 QColor
Chris@871 151 Overview::getFillWithin() const
Chris@871 152 {
Chris@871 153 return Qt::transparent;
Chris@871 154 }
Chris@871 155
Chris@871 156 QColor
Chris@871 157 Overview::getFillWithout() const
Chris@871 158 {
Chris@871 159 QColor c = palette().window().color();
Chris@871 160 c.setAlpha(100);
Chris@871 161 return c;
Chris@871 162 }
Chris@871 163
Chris@173 164 void
Chris@965 165 Overview::setBoxColour(QColor c)
Chris@965 166 {
Chris@965 167 m_boxColour = c;
Chris@965 168 }
Chris@965 169
Chris@965 170 void
Chris@173 171 Overview::paintEvent(QPaintEvent *e)
Chris@173 172 {
Chris@173 173 // Recalculate zoom in case the size of the widget has changed.
Chris@173 174
Chris@642 175 #ifdef DEBUG_OVERVIEW
Chris@682 176 cerr << "Overview::paintEvent: width is " << width() << ", centre frame " << m_centreFrame << endl;
Chris@642 177 #endif
Chris@214 178
Chris@908 179 sv_frame_t startFrame = getModelsStartFrame();
Chris@908 180 sv_frame_t frameCount = getModelsEndFrame() - getModelsStartFrame();
Chris@908 181 int zoomLevel = int(frameCount / width());
Chris@173 182 if (zoomLevel < 1) zoomLevel = 1;
Chris@173 183 zoomLevel = getZoomConstraintBlockSize(zoomLevel,
Chris@1266 184 ZoomConstraint::RoundUp);
Chris@173 185 if (zoomLevel != m_zoomLevel) {
Chris@1266 186 m_zoomLevel = zoomLevel;
Chris@1266 187 emit zoomLevelChanged(m_zoomLevel, m_followZoom);
Chris@173 188 }
Chris@253 189
Chris@908 190 sv_frame_t centreFrame = startFrame + m_zoomLevel * (width() / 2);
Chris@173 191 if (centreFrame > (startFrame + getModelsEndFrame())/2) {
Chris@1266 192 centreFrame = (startFrame + getModelsEndFrame())/2;
Chris@173 193 }
Chris@173 194 if (centreFrame != m_centreFrame) {
Chris@642 195 #ifdef DEBUG_OVERVIEW
Chris@682 196 cerr << "Overview::paintEvent: Centre frame changed from "
Chris@642 197 << m_centreFrame << " to " << centreFrame << " and thus start frame from " << getStartFrame();
Chris@642 198 #endif
Chris@1266 199 m_centreFrame = centreFrame;
Chris@642 200 #ifdef DEBUG_OVERVIEW
Chris@682 201 cerr << " to " << getStartFrame() << endl;
Chris@642 202 #endif
Chris@1266 203 emit centreFrameChanged(m_centreFrame, false, PlaybackIgnore);
Chris@173 204 }
Chris@173 205
Chris@173 206 View::paintEvent(e);
Chris@173 207
Chris@173 208 QPainter paint;
Chris@173 209 paint.begin(this);
Chris@871 210 paint.setClipRegion(e->region());
Chris@871 211 paint.setRenderHints(QPainter::Antialiasing);
Chris@871 212
Chris@871 213 // We paint a rounded rect for each distinct set of view extents,
Chris@871 214 // and we colour in the inside and outside of the rect that
Chris@871 215 // corresponds to the current view. (One small caveat -- we don't
Chris@871 216 // know which rect that is yet. We'll have to figure it out
Chris@871 217 // somehow...)
Chris@173 218
Chris@871 219 std::set<std::pair<int, int> > extents;
Chris@871 220 std::vector<QRect> rects;
Chris@871 221 QRect primary;
Chris@173 222
Chris@173 223 int y = 0;
Chris@173 224
Chris@211 225 for (ViewSet::iterator i = m_views.begin(); i != m_views.end(); ++i) {
Chris@1266 226 if (!*i) continue;
Chris@173 227
Chris@1266 228 View *w = (View *)*i;
Chris@173 229
Chris@1266 230 sv_frame_t f0 = w->getFrameForX(0);
Chris@1266 231 sv_frame_t f1 = w->getFrameForX(w->width());
Chris@173 232
Chris@339 233 if (f0 >= 0) {
Chris@908 234 sv_frame_t rf0 = w->alignToReference(f0);
Chris@339 235 f0 = alignFromReference(rf0);
Chris@339 236 }
Chris@339 237 if (f1 >= 0) {
Chris@908 238 sv_frame_t rf1 = w->alignToReference(f1);
Chris@339 239 f1 = alignFromReference(rf1);
Chris@339 240 }
Chris@339 241
Chris@1266 242 int x0 = getXForFrame(f0);
Chris@1266 243 int x1 = getXForFrame(f1);
Chris@173 244
Chris@1266 245 if (x1 <= x0) x1 = x0 + 1;
Chris@871 246
Chris@871 247 std::pair<int, int> extent(x0, x1);
Chris@871 248
Chris@871 249 if (extents.find(extent) == extents.end()) {
Chris@871 250
Chris@1266 251 y += height() / 10 + 1;
Chris@871 252 extents.insert(extent);
Chris@173 253
Chris@871 254 QRect vr(x0, y, x1 - x0, height() - 2 * y);
Chris@871 255 rects.push_back(vr);
Chris@871 256 primary = vr; //!!! for now
Chris@871 257 }
Chris@871 258 }
Chris@871 259
Chris@871 260 QPainterPath without;
matthiasm@935 261 without.addRoundedRect(primary, 4, 4);
Chris@871 262 without.addRect(rect());
Chris@871 263 paint.setPen(Qt::NoPen);
Chris@871 264 paint.setBrush(getFillWithout());
Chris@871 265 paint.drawPath(without);
Chris@871 266
Chris@871 267 paint.setBrush(getFillWithin());
matthiasm@935 268 paint.drawRoundedRect(primary, 4, 4);
Chris@871 269
Chris@871 270 foreach (QRect vr, rects) {
Chris@871 271 paint.setBrush(Qt::NoBrush);
Chris@965 272 paint.setPen(QPen(m_boxColour, 2));
matthiasm@935 273 paint.drawRoundedRect(vr, 4, 4);
Chris@173 274 }
Chris@173 275
Chris@173 276 paint.end();
Chris@173 277 }
Chris@173 278
Chris@173 279 void
Chris@173 280 Overview::mousePressEvent(QMouseEvent *e)
Chris@173 281 {
Chris@173 282 m_clickPos = e->pos();
Chris@908 283 sv_frame_t clickFrame = getFrameForX(m_clickPos.x());
Chris@339 284 if (clickFrame > 0) m_dragCentreFrame = clickFrame;
Chris@339 285 else m_dragCentreFrame = 0;
Chris@339 286 m_clickedInRange = true;
Chris@339 287
Chris@211 288 for (ViewSet::iterator i = m_views.begin(); i != m_views.end(); ++i) {
Chris@1266 289 if (*i && (*i)->getAligningModel() == getAligningModel()) {
Chris@339 290 m_dragCentreFrame = (*i)->getCentreFrame();
Chris@339 291 break;
Chris@339 292 }
Chris@173 293 }
Chris@173 294 }
Chris@173 295
Chris@173 296 void
Chris@173 297 Overview::mouseReleaseEvent(QMouseEvent *e)
Chris@173 298 {
Chris@173 299 if (m_clickedInRange) {
Chris@1266 300 mouseMoveEvent(e);
Chris@173 301 }
Chris@173 302 m_clickedInRange = false;
Chris@173 303 }
Chris@173 304
Chris@173 305 void
Chris@173 306 Overview::mouseMoveEvent(QMouseEvent *e)
Chris@173 307 {
Chris@173 308 if (!m_clickedInRange) return;
Chris@173 309
Chris@806 310 int xoff = int(e->x()) - int(m_clickPos.x());
Chris@908 311 sv_frame_t frameOff = xoff * m_zoomLevel;
Chris@173 312
Chris@908 313 sv_frame_t newCentreFrame = m_dragCentreFrame;
Chris@173 314 if (frameOff > 0) {
Chris@1266 315 newCentreFrame += frameOff;
Chris@908 316 } else if (newCentreFrame >= -frameOff) {
Chris@1266 317 newCentreFrame += frameOff;
Chris@173 318 } else {
Chris@1266 319 newCentreFrame = 0;
Chris@173 320 }
Chris@173 321
Chris@173 322 if (newCentreFrame >= getModelsEndFrame()) {
Chris@1266 323 newCentreFrame = getModelsEndFrame();
Chris@1266 324 if (newCentreFrame > 0) --newCentreFrame;
Chris@173 325 }
Chris@173 326
Chris@173 327 if (std::max(m_centreFrame, newCentreFrame) -
Chris@1266 328 std::min(m_centreFrame, newCentreFrame) > m_zoomLevel) {
Chris@908 329 sv_frame_t rf = alignToReference(newCentreFrame);
Chris@643 330 #ifdef DEBUG_OVERVIEW
Chris@682 331 cerr << "Overview::mouseMoveEvent: x " << e->x() << " and click x " << m_clickPos.x() << " -> frame " << newCentreFrame << " -> rf " << rf << endl;
Chris@643 332 #endif
Chris@817 333 if (m_followPlay == PlaybackScrollContinuous ||
Chris@817 334 m_followPlay == PlaybackScrollPageWithCentre) {
Chris@817 335 emit centreFrameChanged(rf, true, PlaybackScrollContinuous);
Chris@817 336 } else {
Chris@817 337 emit centreFrameChanged(rf, true, PlaybackIgnore);
Chris@817 338 }
Chris@173 339 }
Chris@173 340 }
Chris@173 341
Chris@189 342 void
Chris@189 343 Overview::mouseDoubleClickEvent(QMouseEvent *e)
Chris@189 344 {
Chris@908 345 sv_frame_t frame = getFrameForX(e->x());
Chris@908 346 sv_frame_t rf = 0;
Chris@339 347 if (frame > 0) rf = alignToReference(frame);
Chris@643 348 #ifdef DEBUG_OVERVIEW
Chris@682 349 cerr << "Overview::mouseDoubleClickEvent: frame " << frame << " -> rf " << rf << endl;
Chris@643 350 #endif
Chris@643 351 m_clickedInRange = false; // we're not starting a drag with the second click
Chris@339 352 emit centreFrameChanged(rf, true, PlaybackScrollContinuous);
Chris@189 353 }
Chris@173 354
Chris@189 355 void
Chris@189 356 Overview::enterEvent(QEvent *)
Chris@189 357 {
Chris@189 358 emit contextHelpChanged(tr("Click and drag to navigate; double-click to jump"));
Chris@189 359 }
Chris@189 360
Chris@189 361 void
Chris@189 362 Overview::leaveEvent(QEvent *)
Chris@189 363 {
Chris@189 364 emit contextHelpChanged("");
Chris@189 365 }
Chris@189 366
Chris@189 367