annotate view/View.cpp @ 1354:40b9a495a0e0

Use zoom constraints more consistently, including the new RelativelyFine one
author Chris Cannam
date Wed, 10 Oct 2018 14:33:10 +0100
parents 86429ff00f05
children b9bfcb8cd5a1
rev   line source
Chris@127 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@127 2
Chris@127 3 /*
Chris@127 4 Sonic Visualiser
Chris@127 5 An audio file viewer and annotation editor.
Chris@127 6 Centre for Digital Music, Queen Mary, University of London.
Chris@127 7 This file copyright 2006 Chris Cannam.
Chris@127 8
Chris@127 9 This program is free software; you can redistribute it and/or
Chris@127 10 modify it under the terms of the GNU General Public License as
Chris@127 11 published by the Free Software Foundation; either version 2 of the
Chris@127 12 License, or (at your option) any later version. See the file
Chris@127 13 COPYING included with this distribution for more information.
Chris@127 14 */
Chris@127 15
Chris@128 16 #include "View.h"
Chris@128 17 #include "layer/Layer.h"
Chris@128 18 #include "data/model/Model.h"
Chris@127 19 #include "base/ZoomConstraint.h"
Chris@127 20 #include "base/Profiler.h"
Chris@278 21 #include "base/Pitch.h"
Chris@338 22 #include "base/Preferences.h"
Chris@919 23 #include "ViewProxy.h"
Chris@127 24
Chris@301 25 #include "layer/TimeRulerLayer.h"
Chris@287 26 #include "layer/SingleColourLayer.h"
Chris@1078 27 #include "layer/PaintAssistant.h"
Chris@1078 28
Chris@1354 29 #include "data/model/RelativelyFineZoomConstraint.h"
Chris@301 30 #include "data/model/RangeSummarisableTimeValueModel.h"
Chris@127 31
Chris@797 32 #include "widgets/IconLoader.h"
Chris@797 33
Chris@127 34 #include <QPainter>
Chris@127 35 #include <QPaintEvent>
Chris@127 36 #include <QRect>
Chris@127 37 #include <QApplication>
Chris@226 38 #include <QProgressDialog>
Chris@316 39 #include <QTextStream>
Chris@338 40 #include <QFont>
Chris@583 41 #include <QMessageBox>
Chris@797 42 #include <QPushButton>
Chris@956 43 #include <QSettings>
Chris@1202 44 #include <QSvgGenerator>
Chris@127 45
Chris@127 46 #include <iostream>
Chris@127 47 #include <cassert>
Chris@520 48 #include <cmath>
Chris@127 49
Chris@1352 50 //#define DEBUG_VIEW 1
Chris@1003 51 //#define DEBUG_VIEW_WIDGET_PAINT 1
Chris@127 52
Chris@127 53 View::View(QWidget *w, bool showProgress) :
Chris@127 54 QFrame(w),
Chris@1044 55 m_id(getNextId()),
Chris@127 56 m_centreFrame(0),
Chris@1326 57 m_zoomLevel(ZoomLevel::FramesPerPixel, 1024),
Chris@127 58 m_followPan(true),
Chris@127 59 m_followZoom(true),
Chris@815 60 m_followPlay(PlaybackScrollPageWithCentre),
Chris@789 61 m_followPlayIsDetached(false),
Chris@153 62 m_playPointerFrame(0),
Chris@127 63 m_showProgress(showProgress),
Chris@127 64 m_cache(0),
Chris@952 65 m_buffer(0),
Chris@127 66 m_cacheCentreFrame(0),
Chris@1326 67 m_cacheZoomLevel(ZoomLevel::FramesPerPixel, 1024),
Chris@127 68 m_selectionCached(false),
Chris@127 69 m_deleting(false),
Chris@127 70 m_haveSelectedLayer(false),
Chris@127 71 m_manager(0),
Chris@127 72 m_propertyContainer(new ViewPropertyContainer(this))
Chris@127 73 {
Chris@728 74 // cerr << "View::View(" << this << ")" << endl;
Chris@127 75 }
Chris@127 76
Chris@127 77 View::~View()
Chris@127 78 {
Chris@728 79 // cerr << "View::~View(" << this << ")" << endl;
Chris@127 80
Chris@127 81 m_deleting = true;
Chris@127 82 delete m_propertyContainer;
Chris@1215 83 delete m_cache;
Chris@1215 84 delete m_buffer;
Chris@127 85 }
Chris@127 86
Chris@127 87 PropertyContainer::PropertyList
Chris@127 88 View::getProperties() const
Chris@127 89 {
Chris@127 90 PropertyContainer::PropertyList list;
Chris@127 91 list.push_back("Global Scroll");
Chris@127 92 list.push_back("Global Zoom");
Chris@127 93 list.push_back("Follow Playback");
Chris@127 94 return list;
Chris@127 95 }
Chris@127 96
Chris@127 97 QString
Chris@127 98 View::getPropertyLabel(const PropertyName &pn) const
Chris@127 99 {
Chris@127 100 if (pn == "Global Scroll") return tr("Global Scroll");
Chris@127 101 if (pn == "Global Zoom") return tr("Global Zoom");
Chris@127 102 if (pn == "Follow Playback") return tr("Follow Playback");
Chris@127 103 return "";
Chris@127 104 }
Chris@127 105
Chris@127 106 PropertyContainer::PropertyType
Chris@127 107 View::getPropertyType(const PropertyContainer::PropertyName &name) const
Chris@127 108 {
Chris@127 109 if (name == "Global Scroll") return PropertyContainer::ToggleProperty;
Chris@127 110 if (name == "Global Zoom") return PropertyContainer::ToggleProperty;
Chris@127 111 if (name == "Follow Playback") return PropertyContainer::ValueProperty;
Chris@127 112 return PropertyContainer::InvalidProperty;
Chris@127 113 }
Chris@127 114
Chris@127 115 int
Chris@127 116 View::getPropertyRangeAndValue(const PropertyContainer::PropertyName &name,
Chris@1266 117 int *min, int *max, int *deflt) const
Chris@127 118 {
Chris@216 119 if (deflt) *deflt = 1;
Chris@127 120 if (name == "Global Scroll") return m_followPan;
Chris@127 121 if (name == "Global Zoom") return m_followZoom;
Chris@127 122 if (name == "Follow Playback") {
Chris@1266 123 if (min) *min = 0;
Chris@1266 124 if (max) *max = 2;
Chris@815 125 if (deflt) *deflt = int(PlaybackScrollPageWithCentre);
Chris@815 126 switch (m_followPlay) {
Chris@815 127 case PlaybackScrollContinuous: return 0;
Chris@815 128 case PlaybackScrollPageWithCentre: case PlaybackScrollPage: return 1;
Chris@815 129 case PlaybackIgnore: return 2;
Chris@815 130 }
Chris@127 131 }
Chris@127 132 if (min) *min = 0;
Chris@127 133 if (max) *max = 0;
Chris@216 134 if (deflt) *deflt = 0;
Chris@127 135 return 0;
Chris@127 136 }
Chris@127 137
Chris@127 138 QString
Chris@127 139 View::getPropertyValueLabel(const PropertyContainer::PropertyName &name,
Chris@1266 140 int value) const
Chris@127 141 {
Chris@127 142 if (name == "Follow Playback") {
Chris@1266 143 switch (value) {
Chris@1266 144 default:
Chris@1266 145 case 0: return tr("Scroll");
Chris@1266 146 case 1: return tr("Page");
Chris@1266 147 case 2: return tr("Off");
Chris@1266 148 }
Chris@127 149 }
Chris@127 150 return tr("<unknown>");
Chris@127 151 }
Chris@127 152
Chris@127 153 void
Chris@127 154 View::setProperty(const PropertyContainer::PropertyName &name, int value)
Chris@127 155 {
Chris@127 156 if (name == "Global Scroll") {
Chris@1266 157 setFollowGlobalPan(value != 0);
Chris@127 158 } else if (name == "Global Zoom") {
Chris@1266 159 setFollowGlobalZoom(value != 0);
Chris@127 160 } else if (name == "Follow Playback") {
Chris@1266 161 switch (value) {
Chris@1266 162 default:
Chris@1266 163 case 0: setPlaybackFollow(PlaybackScrollContinuous); break;
Chris@1266 164 case 1: setPlaybackFollow(PlaybackScrollPageWithCentre); break;
Chris@1266 165 case 2: setPlaybackFollow(PlaybackIgnore); break;
Chris@1266 166 }
Chris@127 167 }
Chris@127 168 }
Chris@127 169
Chris@806 170 int
Chris@127 171 View::getPropertyContainerCount() const
Chris@127 172 {
Chris@908 173 return int(m_fixedOrderLayers.size()) + 1; // the 1 is for me
Chris@127 174 }
Chris@127 175
Chris@127 176 const PropertyContainer *
Chris@806 177 View::getPropertyContainer(int i) const
Chris@127 178 {
Chris@127 179 return (const PropertyContainer *)(((View *)this)->
Chris@1266 180 getPropertyContainer(i));
Chris@127 181 }
Chris@127 182
Chris@127 183 PropertyContainer *
Chris@806 184 View::getPropertyContainer(int i)
Chris@127 185 {
Chris@127 186 if (i == 0) return m_propertyContainer;
Chris@837 187 return m_fixedOrderLayers[i-1];
Chris@127 188 }
Chris@127 189
Chris@127 190 bool
Chris@904 191 View::getValueExtents(QString unit, double &min, double &max, bool &log) const
Chris@127 192 {
Chris@127 193 bool have = false;
Chris@127 194
Chris@835 195 for (LayerList::const_iterator i = m_layerStack.begin();
Chris@835 196 i != m_layerStack.end(); ++i) {
Chris@127 197
Chris@127 198 QString layerUnit;
Chris@904 199 double layerMin = 0.0, layerMax = 0.0;
Chris@904 200 double displayMin = 0.0, displayMax = 0.0;
Chris@127 201 bool layerLog = false;
Chris@127 202
Chris@127 203 if ((*i)->getValueExtents(layerMin, layerMax, layerLog, layerUnit) &&
Chris@127 204 layerUnit.toLower() == unit.toLower()) {
Chris@127 205
Chris@127 206 if ((*i)->getDisplayExtents(displayMin, displayMax)) {
Chris@127 207
Chris@127 208 min = displayMin;
Chris@127 209 max = displayMax;
Chris@127 210 log = layerLog;
Chris@127 211 have = true;
Chris@127 212 break;
Chris@127 213
Chris@127 214 } else {
Chris@127 215
Chris@127 216 if (!have || layerMin < min) min = layerMin;
Chris@127 217 if (!have || layerMax > max) max = layerMax;
Chris@127 218 if (layerLog) log = true;
Chris@127 219 have = true;
Chris@127 220 }
Chris@127 221 }
Chris@127 222 }
Chris@127 223
Chris@127 224 return have;
Chris@127 225 }
Chris@127 226
Chris@127 227 int
Chris@127 228 View::getTextLabelHeight(const Layer *layer, QPainter &paint) const
Chris@127 229 {
Chris@127 230 std::map<int, Layer *> sortedLayers;
Chris@127 231
Chris@835 232 for (LayerList::const_iterator i = m_layerStack.begin();
Chris@835 233 i != m_layerStack.end(); ++i) {
Chris@127 234 if ((*i)->needsTextLabelHeight()) {
Chris@127 235 sortedLayers[getObjectExportId(*i)] = *i;
Chris@127 236 }
Chris@127 237 }
Chris@127 238
Chris@1273 239 int y = ViewManager::scalePixelSize(15) + paint.fontMetrics().ascent();
Chris@127 240
Chris@127 241 for (std::map<int, Layer *>::const_iterator i = sortedLayers.begin();
Chris@127 242 i != sortedLayers.end(); ++i) {
Chris@1273 243 if (i->second == layer) break;
Chris@127 244 y += paint.fontMetrics().height();
Chris@127 245 }
Chris@127 246
Chris@127 247 return y;
Chris@127 248 }
Chris@127 249
Chris@127 250 void
Chris@127 251 View::propertyContainerSelected(View *client, PropertyContainer *pc)
Chris@127 252 {
Chris@127 253 if (client != this) return;
Chris@127 254
Chris@127 255 if (pc == m_propertyContainer) {
Chris@1266 256 if (m_haveSelectedLayer) {
Chris@1266 257 m_haveSelectedLayer = false;
Chris@1266 258 update();
Chris@1266 259 }
Chris@1266 260 return;
Chris@127 261 }
Chris@127 262
Chris@127 263 delete m_cache;
Chris@127 264 m_cache = 0;
Chris@127 265
Chris@127 266 Layer *selectedLayer = 0;
Chris@127 267
Chris@835 268 for (LayerList::iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@1266 269 if (*i == pc) {
Chris@1266 270 selectedLayer = *i;
Chris@1266 271 m_layerStack.erase(i);
Chris@1266 272 break;
Chris@1266 273 }
Chris@127 274 }
Chris@127 275
Chris@127 276 if (selectedLayer) {
Chris@1266 277 m_haveSelectedLayer = true;
Chris@1266 278 m_layerStack.push_back(selectedLayer);
Chris@1266 279 update();
Chris@127 280 } else {
Chris@1266 281 m_haveSelectedLayer = false;
Chris@127 282 }
Chris@298 283
Chris@298 284 emit propertyContainerSelected(pc);
Chris@127 285 }
Chris@127 286
Chris@127 287 void
Chris@127 288 View::toolModeChanged()
Chris@127 289 {
Chris@587 290 // SVDEBUG << "View::toolModeChanged(" << m_manager->getToolMode() << ")" << endl;
Chris@127 291 }
Chris@127 292
Chris@133 293 void
Chris@133 294 View::overlayModeChanged()
Chris@133 295 {
Chris@206 296 delete m_cache;
Chris@206 297 m_cache = 0;
Chris@133 298 update();
Chris@133 299 }
Chris@133 300
Chris@133 301 void
Chris@133 302 View::zoomWheelsEnabledChanged()
Chris@133 303 {
Chris@133 304 // subclass might override this
Chris@133 305 }
Chris@133 306
Chris@908 307 sv_frame_t
Chris@127 308 View::getStartFrame() const
Chris@127 309 {
Chris@313 310 return getFrameForX(0);
Chris@127 311 }
Chris@127 312
Chris@908 313 sv_frame_t
Chris@127 314 View::getEndFrame() const
Chris@127 315 {
Chris@127 316 return getFrameForX(width()) - 1;
Chris@127 317 }
Chris@127 318
Chris@127 319 void
Chris@908 320 View::setStartFrame(sv_frame_t f)
Chris@127 321 {
Chris@1326 322 setCentreFrame(f + sv_frame_t(round
Chris@1326 323 (m_zoomLevel.pixelsToFrames(width() / 2))));
Chris@127 324 }
Chris@127 325
Chris@127 326 bool
Chris@908 327 View::setCentreFrame(sv_frame_t f, bool e)
Chris@127 328 {
Chris@127 329 bool changeVisible = false;
Chris@127 330
Chris@1329 331 #ifdef DEBUG_VIEW
Chris@1329 332 SVCERR << "View::setCentreFrame: from " << m_centreFrame
Chris@1329 333 << " to " << f << endl;
Chris@1329 334 #endif
Chris@1329 335
Chris@127 336 if (m_centreFrame != f) {
Chris@127 337
Chris@1327 338 sv_frame_t formerCentre = m_centreFrame;
Chris@1266 339 m_centreFrame = f;
Chris@1266 340
Chris@1327 341 if (m_zoomLevel.zone == ZoomLevel::PixelsPerFrame) {
Chris@127 342
Chris@127 343 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1327 344 SVCERR << "View(" << this << ")::setCentreFrame: in PixelsPerFrame zone, so change must be visible" << endl;
Chris@127 345 #endif
Chris@1266 346 update();
Chris@1266 347 changeVisible = true;
Chris@1327 348
Chris@1327 349 } else {
Chris@1327 350
Chris@1327 351 int formerPixel = int(formerCentre / m_zoomLevel.level);
Chris@1327 352 int newPixel = int(m_centreFrame / m_zoomLevel.level);
Chris@1327 353
Chris@1327 354 if (newPixel != formerPixel) {
Chris@1327 355
Chris@1327 356 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1327 357 SVCERR << "View(" << this << ")::setCentreFrame: newPixel " << newPixel << ", formerPixel " << formerPixel << endl;
Chris@1327 358 #endif
Chris@1329 359 // ensure the centre frame is a multiple of the zoom level
Chris@1329 360 m_centreFrame = sv_frame_t(newPixel) * m_zoomLevel.level;
Chris@1329 361
Chris@1327 362 update();
Chris@1327 363 changeVisible = true;
Chris@1327 364 }
Chris@1266 365 }
Chris@1266 366
Chris@1266 367 if (e) {
Chris@908 368 sv_frame_t rf = alignToReference(f);
Chris@830 369 #ifdef DEBUG_VIEW
Chris@682 370 cerr << "View[" << this << "]::setCentreFrame(" << f
Chris@333 371 << "): emitting centreFrameChanged("
Chris@682 372 << rf << ")" << endl;
Chris@355 373 #endif
Chris@333 374 emit centreFrameChanged(rf, m_followPan, m_followPlay);
Chris@333 375 }
Chris@127 376 }
Chris@127 377
Chris@127 378 return changeVisible;
Chris@127 379 }
Chris@127 380
Chris@127 381 int
Chris@908 382 View::getXForFrame(sv_frame_t frame) const
Chris@127 383 {
Chris@1341 384 // In FramesPerPixel mode, the pixel should be the one "covering"
Chris@1341 385 // the given frame, i.e. to the "left" of it - not necessarily the
Chris@1341 386 // nearest boundary.
Chris@1341 387
Chris@1341 388 sv_frame_t level = m_zoomLevel.level;
Chris@1327 389 sv_frame_t fdiff = frame - getCentreFrame();
Chris@1341 390 int diff, result;
Chris@1327 391
Chris@1327 392 if (m_zoomLevel.zone == ZoomLevel::FramesPerPixel) {
Chris@1343 393 diff = int(fdiff / level);
Chris@1341 394 if ((fdiff < 0) && ((fdiff % level) != 0)) {
Chris@1341 395 --diff; // round to the left
Chris@1341 396 }
Chris@1327 397 } else {
Chris@1343 398 diff = int(fdiff * level);
Chris@1327 399 }
Chris@1327 400
Chris@1341 401 result = int(diff + (width()/2));
Chris@1341 402 return result;
Chris@127 403 }
Chris@127 404
Chris@908 405 sv_frame_t
Chris@127 406 View::getFrameForX(int x) const
Chris@127 407 {
Chris@1330 408 // Note, this must always return a value that is on a zoom-level
Chris@1330 409 // boundary - regardless of whether the nominal centre frame is on
Chris@1341 410 // such a boundary or not.
Chris@1341 411
Chris@1341 412 // In PixelsPerFrame mode, the frame should be the one immediately
Chris@1341 413 // left of the given pixel, not necessarily the nearest.
Chris@1330 414
Chris@1327 415 int diff = x - (width()/2);
Chris@1330 416 sv_frame_t level = m_zoomLevel.level;
Chris@1330 417 sv_frame_t fdiff, result;
Chris@1330 418
Chris@1327 419 if (m_zoomLevel.zone == ZoomLevel::FramesPerPixel) {
Chris@1330 420 fdiff = diff * level;
Chris@1330 421 result = ((fdiff + m_centreFrame) / level) * level;
Chris@1327 422 } else {
Chris@1330 423 fdiff = diff / level;
Chris@1341 424 if ((diff < 0) && ((diff % level) != 0)) {
Chris@1341 425 --fdiff; // round to the left
Chris@1341 426 }
Chris@1330 427 result = fdiff + m_centreFrame;
Chris@1327 428 }
Chris@1341 429
Chris@1352 430 #ifdef DEBUG_VIEW
Chris@1330 431 if (x == 0) {
Chris@1330 432 SVCERR << "getFrameForX(" << x << "): diff = " << diff << ", fdiff = "
Chris@1330 433 << fdiff << ", m_centreFrame = " << m_centreFrame
Chris@1330 434 << ", level = " << m_zoomLevel.level
Chris@1330 435 << ", diff % level = " << (diff % m_zoomLevel.level)
Chris@1330 436 << ", nominal " << fdiff + m_centreFrame
Chris@1330 437 << ", will return " << result
Chris@1330 438 << endl;
Chris@1330 439 }
Chris@1352 440 #endif
Chris@1352 441
Chris@1330 442 return result;
Chris@127 443 }
Chris@127 444
Chris@904 445 double
Chris@904 446 View::getYForFrequency(double frequency,
Chris@1266 447 double minf,
Chris@1266 448 double maxf,
Chris@1266 449 bool logarithmic) const
Chris@127 450 {
Chris@382 451 Profiler profiler("View::getYForFrequency");
Chris@382 452
Chris@127 453 int h = height();
Chris@127 454
Chris@127 455 if (logarithmic) {
Chris@127 456
Chris@1266 457 static double lastminf = 0.0, lastmaxf = 0.0;
Chris@1266 458 static double logminf = 0.0, logmaxf = 0.0;
Chris@1266 459
Chris@1266 460 if (lastminf != minf) {
Chris@1266 461 lastminf = (minf == 0.0 ? 1.0 : minf);
Chris@1266 462 logminf = log10(minf);
Chris@1266 463 }
Chris@1266 464 if (lastmaxf != maxf) {
Chris@1266 465 lastmaxf = (maxf < lastminf ? lastminf : maxf);
Chris@1266 466 logmaxf = log10(maxf);
Chris@1266 467 }
Chris@1266 468
Chris@1266 469 if (logminf == logmaxf) return 0;
Chris@1266 470 return h - (h * (log10(frequency) - logminf)) / (logmaxf - logminf);
Chris@127 471
Chris@127 472 } else {
Chris@1266 473
Chris@1266 474 if (minf == maxf) return 0;
Chris@1266 475 return h - (h * (frequency - minf)) / (maxf - minf);
Chris@127 476 }
Chris@127 477 }
Chris@127 478
Chris@904 479 double
Chris@1085 480 View::getFrequencyForY(double y,
Chris@1266 481 double minf,
Chris@1266 482 double maxf,
Chris@1266 483 bool logarithmic) const
Chris@127 484 {
Chris@1085 485 double h = height();
Chris@127 486
Chris@127 487 if (logarithmic) {
Chris@127 488
Chris@1266 489 static double lastminf = 0.0, lastmaxf = 0.0;
Chris@1266 490 static double logminf = 0.0, logmaxf = 0.0;
Chris@1266 491
Chris@1266 492 if (lastminf != minf) {
Chris@1266 493 lastminf = (minf == 0.0 ? 1.0 : minf);
Chris@1266 494 logminf = log10(minf);
Chris@1266 495 }
Chris@1266 496 if (lastmaxf != maxf) {
Chris@1266 497 lastmaxf = (maxf < lastminf ? lastminf : maxf);
Chris@1266 498 logmaxf = log10(maxf);
Chris@1266 499 }
Chris@1266 500
Chris@1266 501 if (logminf == logmaxf) return 0;
Chris@1266 502 return pow(10.0, logminf + ((logmaxf - logminf) * (h - y)) / h);
Chris@127 503
Chris@127 504 } else {
Chris@127 505
Chris@1266 506 if (minf == maxf) return 0;
Chris@1266 507 return minf + ((h - y) * (maxf - minf)) / h;
Chris@127 508 }
Chris@127 509 }
Chris@127 510
Chris@1183 511 ZoomLevel
Chris@127 512 View::getZoomLevel() const
Chris@127 513 {
Chris@127 514 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266 515 // cout << "zoom level: " << m_zoomLevel << endl;
Chris@127 516 #endif
Chris@127 517 return m_zoomLevel;
Chris@127 518 }
Chris@127 519
Chris@956 520 int
Chris@956 521 View::effectiveDevicePixelRatio() const
Chris@956 522 {
Chris@956 523 #ifdef Q_OS_MAC
Chris@956 524 int dpratio = devicePixelRatio();
Chris@956 525 if (dpratio > 1) {
Chris@956 526 QSettings settings;
Chris@956 527 settings.beginGroup("Preferences");
Chris@956 528 if (!settings.value("scaledHiDpi", true).toBool()) {
Chris@956 529 dpratio = 1;
Chris@956 530 }
Chris@956 531 settings.endGroup();
Chris@956 532 }
Chris@956 533 return dpratio;
Chris@956 534 #else
Chris@956 535 return 1;
Chris@956 536 #endif
Chris@956 537 }
Chris@956 538
Chris@127 539 void
Chris@1183 540 View::setZoomLevel(ZoomLevel z)
Chris@127 541 {
Chris@1327 542 //!!! int dpratio = effectiveDevicePixelRatio();
Chris@1327 543 // if (z < dpratio) return;
Chris@1327 544 // if (z < 1) z = 1;
Chris@1327 545 if (m_zoomLevel == z) {
Chris@1327 546 return;
Chris@127 547 }
Chris@1327 548 m_zoomLevel = z;
Chris@1327 549 emit zoomLevelChanged(z, m_followZoom);
Chris@1327 550 update();
Chris@127 551 }
Chris@127 552
Chris@224 553 bool
Chris@224 554 View::hasLightBackground() const
Chris@224 555 {
Chris@287 556 bool darkPalette = false;
Chris@292 557 if (m_manager) darkPalette = m_manager->getGlobalDarkBackground();
Chris@287 558
Chris@287 559 Layer::ColourSignificance maxSignificance = Layer::ColourAbsent;
Chris@287 560 bool mostSignificantHasDarkBackground = false;
Chris@287 561
Chris@835 562 for (LayerList::const_iterator i = m_layerStack.begin();
Chris@835 563 i != m_layerStack.end(); ++i) {
Chris@287 564
Chris@287 565 Layer::ColourSignificance s = (*i)->getLayerColourSignificance();
Chris@287 566 bool light = (*i)->hasLightBackground();
Chris@287 567
Chris@287 568 if (int(s) > int(maxSignificance)) {
Chris@287 569 maxSignificance = s;
Chris@287 570 mostSignificantHasDarkBackground = !light;
Chris@287 571 } else if (s == maxSignificance && !light) {
Chris@287 572 mostSignificantHasDarkBackground = true;
Chris@287 573 }
Chris@224 574 }
Chris@287 575
Chris@1314 576 if (int(maxSignificance) >= int(Layer::ColourDistinguishes)) {
Chris@287 577 return !mostSignificantHasDarkBackground;
Chris@287 578 } else {
Chris@287 579 return !darkPalette;
Chris@287 580 }
Chris@287 581 }
Chris@287 582
Chris@287 583 QColor
Chris@287 584 View::getBackground() const
Chris@287 585 {
Chris@287 586 bool light = hasLightBackground();
Chris@287 587
Chris@287 588 QColor widgetbg = palette().window().color();
Chris@287 589 bool widgetLight =
Chris@287 590 (widgetbg.red() + widgetbg.green() + widgetbg.blue()) > 384;
Chris@287 591
Chris@296 592 if (widgetLight == light) {
Chris@296 593 if (widgetLight) {
Chris@296 594 return widgetbg.light();
Chris@296 595 } else {
Chris@296 596 return widgetbg.dark();
Chris@296 597 }
Chris@296 598 }
Chris@287 599 else if (light) return Qt::white;
Chris@287 600 else return Qt::black;
Chris@287 601 }
Chris@287 602
Chris@287 603 QColor
Chris@287 604 View::getForeground() const
Chris@287 605 {
Chris@287 606 bool light = hasLightBackground();
Chris@287 607
Chris@287 608 QColor widgetfg = palette().text().color();
Chris@287 609 bool widgetLight =
Chris@287 610 (widgetfg.red() + widgetfg.green() + widgetfg.blue()) > 384;
Chris@287 611
Chris@287 612 if (widgetLight != light) return widgetfg;
Chris@287 613 else if (light) return Qt::black;
Chris@287 614 else return Qt::white;
Chris@224 615 }
Chris@224 616
Chris@127 617 void
Chris@127 618 View::addLayer(Layer *layer)
Chris@127 619 {
Chris@127 620 delete m_cache;
Chris@127 621 m_cache = 0;
Chris@127 622
Chris@287 623 SingleColourLayer *scl = dynamic_cast<SingleColourLayer *>(layer);
Chris@287 624 if (scl) scl->setDefaultColourFor(this);
Chris@287 625
Chris@836 626 m_fixedOrderLayers.push_back(layer);
Chris@835 627 m_layerStack.push_back(layer);
Chris@127 628
Chris@555 629 QProgressBar *pb = new QProgressBar(this);
Chris@555 630 pb->setMinimum(0);
Chris@555 631 pb->setMaximum(0);
Chris@555 632 pb->setFixedWidth(80);
Chris@555 633 pb->setTextVisible(false);
Chris@555 634
Chris@797 635 QPushButton *cancel = new QPushButton(this);
Chris@1350 636 cancel->setIcon(IconLoader().load("cancel"));
Chris@797 637 cancel->setFlat(true);
Chris@1351 638 int scaled20 = ViewManager::scalePixelSize(20);
Chris@1351 639 cancel->setFixedSize(QSize(scaled20, scaled20));
Chris@797 640 connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
Chris@797 641
Chris@555 642 ProgressBarRec pbr;
Chris@797 643 pbr.cancel = cancel;
Chris@555 644 pbr.bar = pb;
Chris@555 645 pbr.lastCheck = 0;
Chris@555 646 pbr.checkTimer = new QTimer();
Chris@555 647 connect(pbr.checkTimer, SIGNAL(timeout()), this,
Chris@555 648 SLOT(progressCheckStalledTimerElapsed()));
Chris@555 649
Chris@555 650 m_progressBars[layer] = pbr;
Chris@555 651
Chris@555 652 QFont f(pb->font());
Chris@339 653 int fs = Preferences::getInstance()->getViewFontSize();
Chris@339 654 f.setPointSize(std::min(fs, int(ceil(fs * 0.85))));
Chris@339 655
Chris@797 656 cancel->hide();
Chris@797 657
Chris@555 658 pb->setFont(f);
Chris@555 659 pb->hide();
Chris@127 660
Chris@127 661 connect(layer, SIGNAL(layerParametersChanged()),
Chris@1266 662 this, SLOT(layerParametersChanged()));
Chris@197 663 connect(layer, SIGNAL(layerParameterRangesChanged()),
Chris@1266 664 this, SLOT(layerParameterRangesChanged()));
Chris@268 665 connect(layer, SIGNAL(layerMeasurementRectsChanged()),
Chris@1266 666 this, SLOT(layerMeasurementRectsChanged()));
Chris@127 667 connect(layer, SIGNAL(layerNameChanged()),
Chris@1266 668 this, SLOT(layerNameChanged()));
Chris@127 669 connect(layer, SIGNAL(modelChanged()),
Chris@1266 670 this, SLOT(modelChanged()));
Chris@127 671 connect(layer, SIGNAL(modelCompletionChanged()),
Chris@1266 672 this, SLOT(modelCompletionChanged()));
Chris@320 673 connect(layer, SIGNAL(modelAlignmentCompletionChanged()),
Chris@1266 674 this, SLOT(modelAlignmentCompletionChanged()));
Chris@908 675 connect(layer, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@1266 676 this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t)));
Chris@127 677 connect(layer, SIGNAL(modelReplaced()),
Chris@1266 678 this, SLOT(modelReplaced()));
Chris@127 679
Chris@127 680 update();
Chris@127 681
Chris@127 682 emit propertyContainerAdded(layer);
Chris@127 683 }
Chris@127 684
Chris@127 685 void
Chris@127 686 View::removeLayer(Layer *layer)
Chris@127 687 {
Chris@127 688 if (m_deleting) {
Chris@1266 689 return;
Chris@127 690 }
Chris@127 691
Chris@127 692 delete m_cache;
Chris@127 693 m_cache = 0;
Chris@127 694
Chris@836 695 for (LayerList::iterator i = m_fixedOrderLayers.begin();
Chris@836 696 i != m_fixedOrderLayers.end();
Chris@836 697 ++i) {
Chris@1266 698 if (*i == layer) {
Chris@1266 699 m_fixedOrderLayers.erase(i);
Chris@836 700 break;
Chris@836 701 }
Chris@836 702 }
Chris@836 703
Chris@836 704 for (LayerList::iterator i = m_layerStack.begin();
Chris@836 705 i != m_layerStack.end();
Chris@836 706 ++i) {
Chris@1266 707 if (*i == layer) {
Chris@1266 708 m_layerStack.erase(i);
Chris@1266 709 if (m_progressBars.find(layer) != m_progressBars.end()) {
Chris@1266 710 delete m_progressBars[layer].bar;
Chris@797 711 delete m_progressBars[layer].cancel;
Chris@1266 712 delete m_progressBars[layer].checkTimer;
Chris@1266 713 m_progressBars.erase(layer);
Chris@1266 714 }
Chris@1266 715 break;
Chris@1266 716 }
Chris@127 717 }
Chris@127 718
Chris@197 719 disconnect(layer, SIGNAL(layerParametersChanged()),
Chris@197 720 this, SLOT(layerParametersChanged()));
Chris@197 721 disconnect(layer, SIGNAL(layerParameterRangesChanged()),
Chris@197 722 this, SLOT(layerParameterRangesChanged()));
Chris@197 723 disconnect(layer, SIGNAL(layerNameChanged()),
Chris@197 724 this, SLOT(layerNameChanged()));
Chris@197 725 disconnect(layer, SIGNAL(modelChanged()),
Chris@197 726 this, SLOT(modelChanged()));
Chris@197 727 disconnect(layer, SIGNAL(modelCompletionChanged()),
Chris@197 728 this, SLOT(modelCompletionChanged()));
Chris@320 729 disconnect(layer, SIGNAL(modelAlignmentCompletionChanged()),
Chris@320 730 this, SLOT(modelAlignmentCompletionChanged()));
Chris@908 731 disconnect(layer, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@908 732 this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t)));
Chris@197 733 disconnect(layer, SIGNAL(modelReplaced()),
Chris@197 734 this, SLOT(modelReplaced()));
Chris@197 735
Chris@127 736 update();
Chris@127 737
Chris@127 738 emit propertyContainerRemoved(layer);
Chris@127 739 }
Chris@127 740
Chris@127 741 Layer *
Chris@834 742 View::getInteractionLayer()
Chris@834 743 {
Chris@834 744 Layer *sl = getSelectedLayer();
Chris@834 745 if (sl && !(sl->isLayerDormant(this))) {
Chris@834 746 return sl;
Chris@834 747 }
Chris@835 748 if (!m_layerStack.empty()) {
Chris@834 749 int n = getLayerCount();
Chris@834 750 while (n > 0) {
Chris@834 751 --n;
Chris@834 752 Layer *layer = getLayer(n);
Chris@834 753 if (!(layer->isLayerDormant(this))) {
Chris@834 754 return layer;
Chris@834 755 }
Chris@834 756 }
Chris@834 757 }
Chris@834 758 return 0;
Chris@834 759 }
Chris@834 760
Chris@841 761 const Layer *
Chris@841 762 View::getInteractionLayer() const
Chris@841 763 {
Chris@841 764 return const_cast<const Layer *>(const_cast<View *>(this)->getInteractionLayer());
Chris@841 765 }
Chris@841 766
Chris@834 767 Layer *
Chris@127 768 View::getSelectedLayer()
Chris@127 769 {
Chris@835 770 if (m_haveSelectedLayer && !m_layerStack.empty()) {
Chris@839 771 return getLayer(getLayerCount() - 1);
Chris@127 772 } else {
Chris@1266 773 return 0;
Chris@127 774 }
Chris@127 775 }
Chris@127 776
Chris@127 777 const Layer *
Chris@127 778 View::getSelectedLayer() const
Chris@127 779 {
Chris@127 780 return const_cast<const Layer *>(const_cast<View *>(this)->getSelectedLayer());
Chris@127 781 }
Chris@127 782
Chris@127 783 void
Chris@127 784 View::setViewManager(ViewManager *manager)
Chris@127 785 {
Chris@127 786 if (m_manager) {
Chris@1266 787 m_manager->disconnect(this, SLOT(globalCentreFrameChanged(sv_frame_t)));
Chris@1266 788 m_manager->disconnect(this, SLOT(viewCentreFrameChanged(View *, sv_frame_t)));
Chris@1266 789 m_manager->disconnect(this, SLOT(viewManagerPlaybackFrameChanged(sv_frame_t)));
Chris@1324 790 m_manager->disconnect(this, SLOT(viewZoomLevelChanged(View *, ZoomLevel, bool)));
Chris@211 791 m_manager->disconnect(this, SLOT(toolModeChanged()));
Chris@211 792 m_manager->disconnect(this, SLOT(selectionChanged()));
Chris@211 793 m_manager->disconnect(this, SLOT(overlayModeChanged()));
Chris@211 794 m_manager->disconnect(this, SLOT(zoomWheelsEnabledChanged()));
Chris@908 795 disconnect(m_manager, SLOT(viewCentreFrameChanged(sv_frame_t, bool, PlaybackFollowMode)));
Chris@1324 796 disconnect(m_manager, SLOT(zoomLevelChanged(ZoomLevel, bool)));
Chris@127 797 }
Chris@127 798
Chris@127 799 m_manager = manager;
Chris@127 800
Chris@908 801 connect(m_manager, SIGNAL(globalCentreFrameChanged(sv_frame_t)),
Chris@1266 802 this, SLOT(globalCentreFrameChanged(sv_frame_t)));
Chris@908 803 connect(m_manager, SIGNAL(viewCentreFrameChanged(View *, sv_frame_t)),
Chris@1266 804 this, SLOT(viewCentreFrameChanged(View *, sv_frame_t)));
Chris@908 805 connect(m_manager, SIGNAL(playbackFrameChanged(sv_frame_t)),
Chris@1266 806 this, SLOT(viewManagerPlaybackFrameChanged(sv_frame_t)));
Chris@806 807
Chris@1183 808 connect(m_manager, SIGNAL(viewZoomLevelChanged(View *, ZoomLevel, bool)),
Chris@1324 809 this, SLOT(viewZoomLevelChanged(View *, ZoomLevel, bool)));
Chris@211 810
Chris@127 811 connect(m_manager, SIGNAL(toolModeChanged()),
Chris@1266 812 this, SLOT(toolModeChanged()));
Chris@127 813 connect(m_manager, SIGNAL(selectionChanged()),
Chris@1266 814 this, SLOT(selectionChanged()));
Chris@127 815 connect(m_manager, SIGNAL(inProgressSelectionChanged()),
Chris@1266 816 this, SLOT(selectionChanged()));
Chris@127 817 connect(m_manager, SIGNAL(overlayModeChanged()),
Chris@133 818 this, SLOT(overlayModeChanged()));
Chris@607 819 connect(m_manager, SIGNAL(showCentreLineChanged()),
Chris@607 820 this, SLOT(overlayModeChanged()));
Chris@133 821 connect(m_manager, SIGNAL(zoomWheelsEnabledChanged()),
Chris@133 822 this, SLOT(zoomWheelsEnabledChanged()));
Chris@127 823
Chris@908 824 connect(this, SIGNAL(centreFrameChanged(sv_frame_t, bool,
Chris@211 825 PlaybackFollowMode)),
Chris@908 826 m_manager, SLOT(viewCentreFrameChanged(sv_frame_t, bool,
Chris@211 827 PlaybackFollowMode)));
Chris@211 828
Chris@1183 829 connect(this, SIGNAL(zoomLevelChanged(ZoomLevel, bool)),
Chris@1324 830 m_manager, SLOT(viewZoomLevelChanged(ZoomLevel, bool)));
Chris@127 831
Chris@815 832 switch (m_followPlay) {
Chris@815 833
Chris@815 834 case PlaybackScrollPage:
Chris@815 835 case PlaybackScrollPageWithCentre:
Chris@364 836 setCentreFrame(m_manager->getGlobalCentreFrame(), false);
Chris@815 837 break;
Chris@815 838
Chris@815 839 case PlaybackScrollContinuous:
Chris@236 840 setCentreFrame(m_manager->getPlaybackFrame(), false);
Chris@815 841 break;
Chris@815 842
Chris@815 843 case PlaybackIgnore:
Chris@815 844 if (m_followPan) {
Chris@815 845 setCentreFrame(m_manager->getGlobalCentreFrame(), false);
Chris@815 846 }
Chris@815 847 break;
Chris@236 848 }
Chris@516 849
Chris@236 850 if (m_followZoom) setZoomLevel(m_manager->getGlobalZoom());
Chris@236 851
Chris@511 852 movePlayPointer(getAlignedPlaybackFrame());
Chris@511 853
Chris@127 854 toolModeChanged();
Chris@127 855 }
Chris@127 856
Chris@127 857 void
Chris@908 858 View::setViewManager(ViewManager *vm, sv_frame_t initialCentreFrame)
Chris@516 859 {
Chris@516 860 setViewManager(vm);
Chris@516 861 setCentreFrame(initialCentreFrame, false);
Chris@516 862 }
Chris@516 863
Chris@516 864 void
Chris@127 865 View::setFollowGlobalPan(bool f)
Chris@127 866 {
Chris@127 867 m_followPan = f;
Chris@127 868 emit propertyContainerPropertyChanged(m_propertyContainer);
Chris@127 869 }
Chris@127 870
Chris@127 871 void
Chris@127 872 View::setFollowGlobalZoom(bool f)
Chris@127 873 {
Chris@127 874 m_followZoom = f;
Chris@127 875 emit propertyContainerPropertyChanged(m_propertyContainer);
Chris@127 876 }
Chris@127 877
Chris@127 878 void
Chris@127 879 View::setPlaybackFollow(PlaybackFollowMode m)
Chris@127 880 {
Chris@127 881 m_followPlay = m;
Chris@127 882 emit propertyContainerPropertyChanged(m_propertyContainer);
Chris@127 883 }
Chris@127 884
Chris@127 885 void
Chris@127 886 View::modelChanged()
Chris@127 887 {
Chris@127 888 QObject *obj = sender();
Chris@127 889
Chris@127 890 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@682 891 cerr << "View(" << this << ")::modelChanged()" << endl;
Chris@127 892 #endif
Chris@127 893
Chris@127 894 // If the model that has changed is not used by any of the cached
Chris@127 895 // layers, we won't need to recreate the cache
Chris@127 896
Chris@127 897 bool recreate = false;
Chris@127 898
Chris@127 899 bool discard;
Chris@127 900 LayerList scrollables = getScrollableBackLayers(false, discard);
Chris@127 901 for (LayerList::const_iterator i = scrollables.begin();
Chris@1266 902 i != scrollables.end(); ++i) {
Chris@1266 903 if (*i == obj || (*i)->getModel() == obj) {
Chris@1266 904 recreate = true;
Chris@1266 905 break;
Chris@1266 906 }
Chris@127 907 }
Chris@127 908
Chris@127 909 if (recreate) {
Chris@1266 910 delete m_cache;
Chris@1266 911 m_cache = 0;
Chris@127 912 }
Chris@127 913
Chris@336 914 emit layerModelChanged();
Chris@336 915
Chris@127 916 checkProgress(obj);
Chris@127 917
Chris@127 918 update();
Chris@127 919 }
Chris@127 920
Chris@127 921 void
Chris@908 922 View::modelChangedWithin(sv_frame_t startFrame, sv_frame_t endFrame)
Chris@127 923 {
Chris@127 924 QObject *obj = sender();
Chris@127 925
Chris@908 926 sv_frame_t myStartFrame = getStartFrame();
Chris@908 927 sv_frame_t myEndFrame = getEndFrame();
Chris@127 928
Chris@127 929 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@806 930 cerr << "View(" << this << ")::modelChangedWithin(" << startFrame << "," << endFrame << ") [me " << myStartFrame << "," << myEndFrame << "]" << endl;
Chris@127 931 #endif
Chris@127 932
Chris@908 933 if (myStartFrame > 0 && endFrame < myStartFrame) {
Chris@1266 934 checkProgress(obj);
Chris@1266 935 return;
Chris@127 936 }
Chris@127 937 if (startFrame > myEndFrame) {
Chris@1266 938 checkProgress(obj);
Chris@1266 939 return;
Chris@127 940 }
Chris@127 941
Chris@127 942 // If the model that has changed is not used by any of the cached
Chris@127 943 // layers, we won't need to recreate the cache
Chris@127 944
Chris@127 945 bool recreate = false;
Chris@127 946
Chris@127 947 bool discard;
Chris@127 948 LayerList scrollables = getScrollableBackLayers(false, discard);
Chris@127 949 for (LayerList::const_iterator i = scrollables.begin();
Chris@1266 950 i != scrollables.end(); ++i) {
Chris@1266 951 if (*i == obj || (*i)->getModel() == obj) {
Chris@1266 952 recreate = true;
Chris@1266 953 break;
Chris@1266 954 }
Chris@127 955 }
Chris@127 956
Chris@127 957 if (recreate) {
Chris@1266 958 delete m_cache;
Chris@1266 959 m_cache = 0;
Chris@127 960 }
Chris@127 961
Chris@806 962 if (startFrame < myStartFrame) startFrame = myStartFrame;
Chris@127 963 if (endFrame > myEndFrame) endFrame = myEndFrame;
Chris@127 964
Chris@127 965 checkProgress(obj);
Chris@127 966
Chris@127 967 update();
Chris@127 968 }
Chris@127 969
Chris@127 970 void
Chris@127 971 View::modelCompletionChanged()
Chris@127 972 {
Chris@682 973 // cerr << "View(" << this << ")::modelCompletionChanged()" << endl;
Chris@301 974
Chris@127 975 QObject *obj = sender();
Chris@127 976 checkProgress(obj);
Chris@127 977 }
Chris@127 978
Chris@127 979 void
Chris@320 980 View::modelAlignmentCompletionChanged()
Chris@320 981 {
Chris@682 982 // cerr << "View(" << this << ")::modelAlignmentCompletionChanged()" << endl;
Chris@320 983
Chris@320 984 QObject *obj = sender();
Chris@320 985 checkProgress(obj);
Chris@320 986 }
Chris@320 987
Chris@320 988 void
Chris@127 989 View::modelReplaced()
Chris@127 990 {
Chris@127 991 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@682 992 cerr << "View(" << this << ")::modelReplaced()" << endl;
Chris@127 993 #endif
Chris@127 994 delete m_cache;
Chris@127 995 m_cache = 0;
Chris@127 996
Chris@127 997 update();
Chris@127 998 }
Chris@127 999
Chris@127 1000 void
Chris@127 1001 View::layerParametersChanged()
Chris@127 1002 {
Chris@127 1003 Layer *layer = dynamic_cast<Layer *>(sender());
Chris@127 1004
Chris@127 1005 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@587 1006 SVDEBUG << "View::layerParametersChanged()" << endl;
Chris@127 1007 #endif
Chris@127 1008
Chris@127 1009 delete m_cache;
Chris@127 1010 m_cache = 0;
Chris@127 1011 update();
Chris@127 1012
Chris@127 1013 if (layer) {
Chris@1266 1014 emit propertyContainerPropertyChanged(layer);
Chris@127 1015 }
Chris@127 1016 }
Chris@127 1017
Chris@127 1018 void
Chris@197 1019 View::layerParameterRangesChanged()
Chris@197 1020 {
Chris@197 1021 Layer *layer = dynamic_cast<Layer *>(sender());
Chris@197 1022 if (layer) emit propertyContainerPropertyRangeChanged(layer);
Chris@197 1023 }
Chris@197 1024
Chris@197 1025 void
Chris@268 1026 View::layerMeasurementRectsChanged()
Chris@268 1027 {
Chris@268 1028 Layer *layer = dynamic_cast<Layer *>(sender());
Chris@268 1029 if (layer) update();
Chris@268 1030 }
Chris@268 1031
Chris@268 1032 void
Chris@127 1033 View::layerNameChanged()
Chris@127 1034 {
Chris@127 1035 Layer *layer = dynamic_cast<Layer *>(sender());
Chris@127 1036 if (layer) emit propertyContainerNameChanged(layer);
Chris@127 1037 }
Chris@127 1038
Chris@127 1039 void
Chris@908 1040 View::globalCentreFrameChanged(sv_frame_t rf)
Chris@127 1041 {
Chris@211 1042 if (m_followPan) {
Chris@908 1043 sv_frame_t f = alignFromReference(rf);
Chris@830 1044 #ifdef DEBUG_VIEW
Chris@682 1045 cerr << "View[" << this << "]::globalCentreFrameChanged(" << rf
Chris@682 1046 << "): setting centre frame to " << f << endl;
Chris@363 1047 #endif
Chris@333 1048 setCentreFrame(f, false);
Chris@127 1049 }
Chris@127 1050 }
Chris@127 1051
Chris@127 1052 void
Chris@908 1053 View::viewCentreFrameChanged(View *, sv_frame_t )
Chris@211 1054 {
Chris@211 1055 // We do nothing with this, but a subclass might
Chris@211 1056 }
Chris@211 1057
Chris@211 1058 void
Chris@908 1059 View::viewManagerPlaybackFrameChanged(sv_frame_t f)
Chris@127 1060 {
Chris@127 1061 if (m_manager) {
Chris@1266 1062 if (sender() != m_manager) return;
Chris@127 1063 }
Chris@127 1064
Chris@830 1065 #ifdef DEBUG_VIEW
Chris@830 1066 cerr << "View::viewManagerPlaybackFrameChanged(" << f << ")" << endl;
Chris@830 1067 #endif
Chris@830 1068
Chris@301 1069 f = getAlignedPlaybackFrame();
Chris@301 1070
Chris@830 1071 #ifdef DEBUG_VIEW
Chris@951 1072 cerr << " -> aligned frame = " << f << endl;
Chris@830 1073 #endif
Chris@830 1074
Chris@511 1075 movePlayPointer(f);
Chris@511 1076 }
Chris@511 1077
Chris@511 1078 void
Chris@908 1079 View::movePlayPointer(sv_frame_t newFrame)
Chris@511 1080 {
Chris@830 1081 #ifdef DEBUG_VIEW
Chris@830 1082 cerr << "View(" << this << ")::movePlayPointer(" << newFrame << ")" << endl;
Chris@830 1083 #endif
Chris@830 1084
Chris@511 1085 if (m_playPointerFrame == newFrame) return;
Chris@511 1086 bool visibleChange =
Chris@511 1087 (getXForFrame(m_playPointerFrame) != getXForFrame(newFrame));
Chris@908 1088 sv_frame_t oldPlayPointerFrame = m_playPointerFrame;
Chris@511 1089 m_playPointerFrame = newFrame;
Chris@511 1090 if (!visibleChange) return;
Chris@127 1091
Chris@513 1092 bool somethingGoingOn =
Chris@513 1093 ((QApplication::mouseButtons() != Qt::NoButton) ||
Chris@513 1094 (QApplication::keyboardModifiers() & Qt::AltModifier));
Chris@513 1095
Chris@789 1096 bool pointerInVisibleArea =
Chris@1266 1097 long(m_playPointerFrame) >= getStartFrame() &&
Chris@789 1098 (m_playPointerFrame < getEndFrame() ||
Chris@789 1099 // include old pointer location so we know to refresh when moving out
Chris@789 1100 oldPlayPointerFrame < getEndFrame());
Chris@789 1101
Chris@127 1102 switch (m_followPlay) {
Chris@127 1103
Chris@127 1104 case PlaybackScrollContinuous:
Chris@1266 1105 if (!somethingGoingOn) {
Chris@1266 1106 setCentreFrame(m_playPointerFrame, false);
Chris@1266 1107 }
Chris@1266 1108 break;
Chris@127 1109
Chris@127 1110 case PlaybackScrollPage:
Chris@815 1111 case PlaybackScrollPageWithCentre:
Chris@789 1112
Chris@789 1113 if (!pointerInVisibleArea && somethingGoingOn) {
Chris@789 1114
Chris@789 1115 m_followPlayIsDetached = true;
Chris@789 1116
Chris@789 1117 } else if (!pointerInVisibleArea && m_followPlayIsDetached) {
Chris@789 1118
Chris@789 1119 // do nothing; we aren't tracking until the pointer comes back in
Chris@789 1120
Chris@789 1121 } else {
Chris@789 1122
Chris@789 1123 int xold = getXForFrame(oldPlayPointerFrame);
Chris@789 1124 update(xold - 4, 0, 9, height());
Chris@789 1125
Chris@908 1126 sv_frame_t w = getEndFrame() - getStartFrame();
Chris@789 1127 w -= w/5;
Chris@908 1128 sv_frame_t sf = (m_playPointerFrame / w) * w - w/8;
Chris@789 1129
Chris@789 1130 if (m_manager &&
Chris@789 1131 m_manager->isPlaying() &&
Chris@789 1132 m_manager->getPlaySelectionMode()) {
Chris@789 1133 MultiSelection::SelectionList selections = m_manager->getSelections();
Chris@789 1134 if (!selections.empty()) {
Chris@908 1135 sv_frame_t selectionStart = selections.begin()->getStartFrame();
Chris@808 1136 if (sf < selectionStart - w / 10) {
Chris@808 1137 sf = selectionStart - w / 10;
Chris@789 1138 }
Chris@789 1139 }
Chris@789 1140 }
Chris@127 1141
Chris@127 1142 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@789 1143 cerr << "PlaybackScrollPage: f = " << m_playPointerFrame << ", sf = " << sf << ", start frame "
Chris@789 1144 << getStartFrame() << endl;
Chris@127 1145 #endif
Chris@127 1146
Chris@789 1147 // We don't consider scrolling unless the pointer is outside
Chris@789 1148 // the central visible range already
Chris@789 1149
Chris@789 1150 int xnew = getXForFrame(m_playPointerFrame);
Chris@127 1151
Chris@127 1152 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@789 1153 cerr << "xnew = " << xnew << ", width = " << width() << endl;
Chris@127 1154 #endif
Chris@127 1155
Chris@789 1156 bool shouldScroll = (xnew > (width() * 7) / 8);
Chris@789 1157
Chris@789 1158 if (!m_followPlayIsDetached && (xnew < width() / 8)) {
Chris@789 1159 shouldScroll = true;
Chris@789 1160 }
Chris@789 1161
Chris@789 1162 if (xnew > width() / 8) {
Chris@789 1163 m_followPlayIsDetached = false;
Chris@791 1164 } else if (somethingGoingOn) {
Chris@791 1165 m_followPlayIsDetached = true;
Chris@789 1166 }
Chris@789 1167
Chris@789 1168 if (!somethingGoingOn && shouldScroll) {
Chris@908 1169 sv_frame_t offset = getFrameForX(width()/2) - getStartFrame();
Chris@908 1170 sv_frame_t newCentre = sf + offset;
Chris@789 1171 bool changed = setCentreFrame(newCentre, false);
Chris@789 1172 if (changed) {
Chris@789 1173 xold = getXForFrame(oldPlayPointerFrame);
Chris@789 1174 update(xold - 4, 0, 9, height());
Chris@789 1175 }
Chris@789 1176 }
Chris@789 1177
Chris@789 1178 update(xnew - 4, 0, 9, height());
Chris@789 1179 }
Chris@789 1180 break;
Chris@127 1181
Chris@127 1182 case PlaybackIgnore:
Chris@1266 1183 if (m_playPointerFrame >= getStartFrame() &&
Chris@511 1184 m_playPointerFrame < getEndFrame()) {
Chris@1266 1185 update();
Chris@1266 1186 }
Chris@1266 1187 break;
Chris@127 1188 }
Chris@127 1189 }
Chris@127 1190
Chris@127 1191 void
Chris@1183 1192 View::viewZoomLevelChanged(View *p, ZoomLevel z, bool locked)
Chris@127 1193 {
Chris@244 1194 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@682 1195 cerr << "View[" << this << "]: viewZoomLevelChanged(" << p << ", " << z << ", " << locked << ")" << endl;
Chris@244 1196 #endif
Chris@127 1197 if (m_followZoom && p != this && locked) {
Chris@222 1198 setZoomLevel(z);
Chris@127 1199 }
Chris@127 1200 }
Chris@127 1201
Chris@127 1202 void
Chris@127 1203 View::selectionChanged()
Chris@127 1204 {
Chris@127 1205 if (m_selectionCached) {
Chris@1266 1206 delete m_cache;
Chris@1266 1207 m_cache = 0;
Chris@1266 1208 m_selectionCached = false;
Chris@127 1209 }
Chris@127 1210 update();
Chris@127 1211 }
Chris@127 1212
Chris@908 1213 sv_frame_t
Chris@222 1214 View::getFirstVisibleFrame() const
Chris@222 1215 {
Chris@908 1216 sv_frame_t f0 = getStartFrame();
Chris@908 1217 sv_frame_t f = getModelsStartFrame();
Chris@806 1218 if (f0 < 0 || f0 < f) return f;
Chris@222 1219 return f0;
Chris@222 1220 }
Chris@222 1221
Chris@908 1222 sv_frame_t
Chris@222 1223 View::getLastVisibleFrame() const
Chris@222 1224 {
Chris@908 1225 sv_frame_t f0 = getEndFrame();
Chris@908 1226 sv_frame_t f = getModelsEndFrame();
Chris@222 1227 if (f0 > f) return f;
Chris@222 1228 return f0;
Chris@222 1229 }
Chris@222 1230
Chris@908 1231 sv_frame_t
Chris@127 1232 View::getModelsStartFrame() const
Chris@127 1233 {
Chris@127 1234 bool first = true;
Chris@908 1235 sv_frame_t startFrame = 0;
Chris@127 1236
Chris@835 1237 for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@127 1238
Chris@1266 1239 if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@1266 1240
Chris@1266 1241 sv_frame_t thisStartFrame = (*i)->getModel()->getStartFrame();
Chris@1266 1242
Chris@1266 1243 if (first || thisStartFrame < startFrame) {
Chris@1266 1244 startFrame = thisStartFrame;
Chris@1266 1245 }
Chris@1266 1246 first = false;
Chris@1266 1247 }
Chris@127 1248 }
Chris@127 1249 return startFrame;
Chris@127 1250 }
Chris@127 1251
Chris@908 1252 sv_frame_t
Chris@127 1253 View::getModelsEndFrame() const
Chris@127 1254 {
Chris@127 1255 bool first = true;
Chris@908 1256 sv_frame_t endFrame = 0;
Chris@127 1257
Chris@835 1258 for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@127 1259
Chris@1266 1260 if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@1266 1261
Chris@1266 1262 sv_frame_t thisEndFrame = (*i)->getModel()->getEndFrame();
Chris@1266 1263
Chris@1266 1264 if (first || thisEndFrame > endFrame) {
Chris@1266 1265 endFrame = thisEndFrame;
Chris@1266 1266 }
Chris@1266 1267 first = false;
Chris@1266 1268 }
Chris@127 1269 }
Chris@127 1270
Chris@127 1271 if (first) return getModelsStartFrame();
Chris@127 1272 return endFrame;
Chris@127 1273 }
Chris@127 1274
Chris@908 1275 sv_samplerate_t
Chris@127 1276 View::getModelsSampleRate() const
Chris@127 1277 {
Chris@127 1278 //!!! Just go for the first, for now. If we were supporting
Chris@127 1279 // multiple samplerates, we'd probably want to do frame/time
Chris@127 1280 // conversion in the model
Chris@127 1281
Chris@159 1282 //!!! nah, this wants to always return the sr of the main model!
Chris@159 1283
Chris@835 1284 for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@1266 1285 if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@1266 1286 return (*i)->getModel()->getSampleRate();
Chris@1266 1287 }
Chris@127 1288 }
Chris@127 1289 return 0;
Chris@127 1290 }
Chris@127 1291
Chris@315 1292 View::ModelSet
Chris@315 1293 View::getModels()
Chris@315 1294 {
Chris@315 1295 ModelSet models;
Chris@315 1296
Chris@315 1297 for (int i = 0; i < getLayerCount(); ++i) {
Chris@315 1298
Chris@315 1299 Layer *layer = getLayer(i);
Chris@315 1300
Chris@315 1301 if (dynamic_cast<TimeRulerLayer *>(layer)) {
Chris@315 1302 continue;
Chris@315 1303 }
Chris@315 1304
Chris@315 1305 if (layer && layer->getModel()) {
Chris@315 1306 Model *model = layer->getModel();
Chris@315 1307 models.insert(model);
Chris@315 1308 }
Chris@315 1309 }
Chris@315 1310
Chris@315 1311 return models;
Chris@315 1312 }
Chris@315 1313
Chris@320 1314 Model *
Chris@320 1315 View::getAligningModel() const
Chris@301 1316 {
Chris@320 1317 if (!m_manager ||
Chris@320 1318 !m_manager->getAlignMode() ||
Chris@314 1319 !m_manager->getPlaybackModel()) {
Chris@320 1320 return 0;
Chris@314 1321 }
Chris@301 1322
Chris@320 1323 Model *anyModel = 0;
Chris@359 1324 Model *alignedModel = 0;
Chris@320 1325 Model *goodModel = 0;
Chris@301 1326
Chris@835 1327 for (LayerList::const_iterator i = m_layerStack.begin();
Chris@835 1328 i != m_layerStack.end(); ++i) {
Chris@320 1329
Chris@320 1330 Layer *layer = *i;
Chris@320 1331
Chris@320 1332 if (!layer) continue;
Chris@320 1333 if (dynamic_cast<TimeRulerLayer *>(layer)) continue;
Chris@301 1334
Chris@301 1335 Model *model = (*i)->getModel();
Chris@301 1336 if (!model) continue;
Chris@301 1337
Chris@359 1338 anyModel = model;
Chris@359 1339
Chris@320 1340 if (model->getAlignmentReference()) {
Chris@359 1341 alignedModel = model;
Chris@320 1342 if (layer->isLayerOpaque() ||
Chris@320 1343 dynamic_cast<RangeSummarisableTimeValueModel *>(model)) {
Chris@320 1344 goodModel = model;
Chris@320 1345 }
Chris@301 1346 }
Chris@320 1347 }
Chris@301 1348
Chris@320 1349 if (goodModel) return goodModel;
Chris@359 1350 else if (alignedModel) return alignedModel;
Chris@320 1351 else return anyModel;
Chris@320 1352 }
Chris@320 1353
Chris@908 1354 sv_frame_t
Chris@908 1355 View::alignFromReference(sv_frame_t f) const
Chris@320 1356 {
Chris@856 1357 if (!m_manager || !m_manager->getAlignMode()) return f;
Chris@320 1358 Model *aligningModel = getAligningModel();
Chris@320 1359 if (!aligningModel) return f;
Chris@320 1360 return aligningModel->alignFromReference(f);
Chris@320 1361 }
Chris@320 1362
Chris@908 1363 sv_frame_t
Chris@908 1364 View::alignToReference(sv_frame_t f) const
Chris@320 1365 {
Chris@321 1366 if (!m_manager->getAlignMode()) return f;
Chris@320 1367 Model *aligningModel = getAligningModel();
Chris@320 1368 if (!aligningModel) return f;
Chris@320 1369 return aligningModel->alignToReference(f);
Chris@320 1370 }
Chris@320 1371
Chris@908 1372 sv_frame_t
Chris@320 1373 View::getAlignedPlaybackFrame() const
Chris@320 1374 {
Chris@856 1375 if (!m_manager) return 0;
Chris@908 1376 sv_frame_t pf = m_manager->getPlaybackFrame();
Chris@321 1377 if (!m_manager->getAlignMode()) return pf;
Chris@321 1378
Chris@320 1379 Model *aligningModel = getAligningModel();
Chris@320 1380 if (!aligningModel) return pf;
Chris@830 1381
Chris@908 1382 sv_frame_t af = aligningModel->alignFromReference(pf);
Chris@301 1383
Chris@301 1384 return af;
Chris@301 1385 }
Chris@301 1386
Chris@127 1387 bool
Chris@127 1388 View::areLayersScrollable() const
Chris@127 1389 {
Chris@127 1390 // True iff all views are scrollable
Chris@835 1391 for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@1266 1392 if (!(*i)->isLayerScrollable(this)) return false;
Chris@127 1393 }
Chris@127 1394 return true;
Chris@127 1395 }
Chris@127 1396
Chris@127 1397 View::LayerList
Chris@127 1398 View::getScrollableBackLayers(bool testChanged, bool &changed) const
Chris@127 1399 {
Chris@127 1400 changed = false;
Chris@127 1401
Chris@127 1402 // We want a list of all the scrollable layers that are behind the
Chris@127 1403 // backmost non-scrollable layer.
Chris@127 1404
Chris@127 1405 LayerList scrollables;
Chris@127 1406 bool metUnscrollable = false;
Chris@127 1407
Chris@835 1408 for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@587 1409 // SVDEBUG << "View::getScrollableBackLayers: calling isLayerDormant on layer " << *i << endl;
Chris@682 1410 // cerr << "(name is " << (*i)->objectName() << ")"
Chris@682 1411 // << endl;
Chris@587 1412 // SVDEBUG << "View::getScrollableBackLayers: I am " << this << endl;
Chris@1266 1413 if ((*i)->isLayerDormant(this)) continue;
Chris@1266 1414 if ((*i)->isLayerOpaque()) {
Chris@1266 1415 // You can't see anything behind an opaque layer!
Chris@1266 1416 scrollables.clear();
Chris@127 1417 if (metUnscrollable) break;
Chris@1266 1418 }
Chris@1266 1419 if (!metUnscrollable && (*i)->isLayerScrollable(this)) {
Chris@127 1420 scrollables.push_back(*i);
Chris@127 1421 } else {
Chris@127 1422 metUnscrollable = true;
Chris@127 1423 }
Chris@127 1424 }
Chris@127 1425
Chris@127 1426 if (testChanged && scrollables != m_lastScrollableBackLayers) {
Chris@1266 1427 m_lastScrollableBackLayers = scrollables;
Chris@1266 1428 changed = true;
Chris@127 1429 }
Chris@127 1430 return scrollables;
Chris@127 1431 }
Chris@127 1432
Chris@127 1433 View::LayerList
Chris@127 1434 View::getNonScrollableFrontLayers(bool testChanged, bool &changed) const
Chris@127 1435 {
Chris@127 1436 changed = false;
Chris@127 1437 LayerList nonScrollables;
Chris@127 1438
Chris@127 1439 // Everything in front of the first non-scrollable from the back
Chris@127 1440 // should also be considered non-scrollable
Chris@127 1441
Chris@127 1442 bool started = false;
Chris@127 1443
Chris@835 1444 for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@1266 1445 if ((*i)->isLayerDormant(this)) continue;
Chris@1266 1446 if (!started && (*i)->isLayerScrollable(this)) {
Chris@1266 1447 continue;
Chris@1266 1448 }
Chris@1266 1449 started = true;
Chris@1266 1450 if ((*i)->isLayerOpaque()) {
Chris@1266 1451 // You can't see anything behind an opaque layer!
Chris@1266 1452 nonScrollables.clear();
Chris@1266 1453 }
Chris@1266 1454 nonScrollables.push_back(*i);
Chris@127 1455 }
Chris@127 1456
Chris@127 1457 if (testChanged && nonScrollables != m_lastNonScrollableBackLayers) {
Chris@1266 1458 m_lastNonScrollableBackLayers = nonScrollables;
Chris@1266 1459 changed = true;
Chris@127 1460 }
Chris@127 1461
Chris@127 1462 return nonScrollables;
Chris@127 1463 }
Chris@127 1464
Chris@1327 1465 ZoomLevel
Chris@1327 1466 View::getZoomConstraintLevel(ZoomLevel zoomLevel,
Chris@1327 1467 ZoomConstraint::RoundingDirection dir)
Chris@127 1468 const
Chris@127 1469 {
Chris@1327 1470 using namespace std::rel_ops;
Chris@1327 1471
Chris@1354 1472 ZoomLevel candidate =
Chris@1354 1473 RelativelyFineZoomConstraint().getNearestZoomLevel(zoomLevel, dir);
Chris@1354 1474
Chris@1354 1475 for (auto i : m_layerStack) {
Chris@1354 1476
Chris@1354 1477 if (i->supportsOtherZoomLevels() || !(i->getZoomConstraint())) {
Chris@1354 1478 continue;
Chris@1354 1479 }
Chris@1354 1480
Chris@1327 1481 ZoomLevel thisLevel =
Chris@1354 1482 i->getZoomConstraint()->getNearestZoomLevel(zoomLevel, dir);
Chris@1266 1483
Chris@1266 1484 // Go for the block size that's furthest from the one
Chris@1266 1485 // passed in. Most of the time, that's what we want.
Chris@1354 1486 if ((thisLevel > zoomLevel && thisLevel > candidate) ||
Chris@1327 1487 (thisLevel < zoomLevel && thisLevel < candidate)) {
Chris@1327 1488 candidate = thisLevel;
Chris@1266 1489 }
Chris@127 1490 }
Chris@127 1491
Chris@127 1492 return candidate;
Chris@127 1493 }
Chris@127 1494
Chris@1354 1495 int
Chris@1354 1496 View::countZoomLevels() const
Chris@1354 1497 {
Chris@1354 1498 int n = 0;
Chris@1354 1499 ZoomLevel min = ZoomConstraint().getMinZoomLevel();
Chris@1354 1500 ZoomLevel max = ZoomConstraint().getMaxZoomLevel();
Chris@1354 1501 ZoomLevel level = min;
Chris@1354 1502 while (true) {
Chris@1354 1503 ++n;
Chris@1354 1504 if (level == max) {
Chris@1354 1505 break;
Chris@1354 1506 }
Chris@1354 1507 level = getZoomConstraintLevel
Chris@1354 1508 (level.incremented(), ZoomConstraint::RoundUp);
Chris@1354 1509 }
Chris@1354 1510 cerr << "View::countZoomLevels: " << n << endl;
Chris@1354 1511 return n;
Chris@1354 1512 }
Chris@1354 1513
Chris@1354 1514 ZoomLevel
Chris@1354 1515 View::getZoomLevelByIndex(int ix) const
Chris@1354 1516 {
Chris@1354 1517 int n = 0;
Chris@1354 1518 ZoomLevel min = ZoomConstraint().getMinZoomLevel();
Chris@1354 1519 ZoomLevel max = ZoomConstraint().getMaxZoomLevel();
Chris@1354 1520 ZoomLevel level = min;
Chris@1354 1521 while (true) {
Chris@1354 1522 if (n == ix) {
Chris@1354 1523 cerr << "View::getZoomLevelByIndex: " << ix << " -> " << level
Chris@1354 1524 << endl;
Chris@1354 1525 return level;
Chris@1354 1526 }
Chris@1354 1527 ++n;
Chris@1354 1528 if (level == max) {
Chris@1354 1529 break;
Chris@1354 1530 }
Chris@1354 1531 level = getZoomConstraintLevel
Chris@1354 1532 (level.incremented(), ZoomConstraint::RoundUp);
Chris@1354 1533 }
Chris@1354 1534 cerr << "View::getZoomLevelByIndex: " << ix << " -> " << max << " (max)"
Chris@1354 1535 << endl;
Chris@1354 1536 return max;
Chris@1354 1537 }
Chris@1354 1538
Chris@1354 1539 int
Chris@1354 1540 View::getZoomLevelIndex(ZoomLevel z) const
Chris@1354 1541 {
Chris@1354 1542 int n = 0;
Chris@1354 1543 ZoomLevel min = ZoomConstraint().getMinZoomLevel();
Chris@1354 1544 ZoomLevel max = ZoomConstraint().getMaxZoomLevel();
Chris@1354 1545 ZoomLevel level = min;
Chris@1354 1546 while (true) {
Chris@1354 1547 if (z == level) {
Chris@1354 1548 cerr << "View::getZoomLevelIndex: " << z << " -> " << n
Chris@1354 1549 << endl;
Chris@1354 1550 return n;
Chris@1354 1551 }
Chris@1354 1552 ++n;
Chris@1354 1553 if (level == max) {
Chris@1354 1554 break;
Chris@1354 1555 }
Chris@1354 1556 level = getZoomConstraintLevel
Chris@1354 1557 (level.incremented(), ZoomConstraint::RoundUp);
Chris@1354 1558 }
Chris@1354 1559 cerr << "View::getZoomLevelIndex: " << z << " -> " << n << " (max)"
Chris@1354 1560 << endl;
Chris@1354 1561 return n;
Chris@1354 1562 }
Chris@1354 1563
Chris@183 1564 bool
Chris@183 1565 View::areLayerColoursSignificant() const
Chris@183 1566 {
Chris@835 1567 for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@1266 1568 if ((*i)->getLayerColourSignificance() ==
Chris@287 1569 Layer::ColourHasMeaningfulValue) return true;
Chris@183 1570 if ((*i)->isLayerOpaque()) break;
Chris@183 1571 }
Chris@183 1572 return false;
Chris@183 1573 }
Chris@183 1574
Chris@217 1575 bool
Chris@217 1576 View::hasTopLayerTimeXAxis() const
Chris@217 1577 {
Chris@835 1578 LayerList::const_iterator i = m_layerStack.end();
Chris@835 1579 if (i == m_layerStack.begin()) return false;
Chris@217 1580 --i;
Chris@217 1581 return (*i)->hasTimeXAxis();
Chris@217 1582 }
Chris@217 1583
Chris@127 1584 void
Chris@127 1585 View::zoom(bool in)
Chris@127 1586 {
Chris@1183 1587 ZoomLevel newZoomLevel = m_zoomLevel;
Chris@127 1588
Chris@127 1589 if (in) {
Chris@1324 1590 newZoomLevel = getZoomConstraintLevel(m_zoomLevel.decremented(),
Chris@1183 1591 ZoomConstraint::RoundDown);
Chris@127 1592 } else {
Chris@1324 1593 newZoomLevel = getZoomConstraintLevel(m_zoomLevel.incremented(),
Chris@1183 1594 ZoomConstraint::RoundUp);
Chris@127 1595 }
Chris@127 1596
Chris@1327 1597 using namespace std::rel_ops;
Chris@1327 1598
Chris@127 1599 if (newZoomLevel != m_zoomLevel) {
Chris@1266 1600 setZoomLevel(newZoomLevel);
Chris@127 1601 }
Chris@127 1602 }
Chris@127 1603
Chris@127 1604 void
Chris@510 1605 View::scroll(bool right, bool lots, bool e)
Chris@127 1606 {
Chris@908 1607 sv_frame_t delta;
Chris@127 1608 if (lots) {
Chris@1266 1609 delta = (getEndFrame() - getStartFrame()) / 2;
Chris@127 1610 } else {
Chris@1266 1611 delta = (getEndFrame() - getStartFrame()) / 20;
Chris@127 1612 }
Chris@127 1613 if (right) delta = -delta;
Chris@127 1614
Chris@908 1615 if (m_centreFrame < delta) {
Chris@1266 1616 setCentreFrame(0, e);
Chris@908 1617 } else if (m_centreFrame - delta >= getModelsEndFrame()) {
Chris@1266 1618 setCentreFrame(getModelsEndFrame(), e);
Chris@127 1619 } else {
Chris@1266 1620 setCentreFrame(m_centreFrame - delta, e);
Chris@127 1621 }
Chris@127 1622 }
Chris@127 1623
Chris@127 1624 void
Chris@797 1625 View::cancelClicked()
Chris@797 1626 {
Chris@797 1627 QPushButton *cancel = qobject_cast<QPushButton *>(sender());
Chris@797 1628 if (!cancel) return;
Chris@797 1629
Chris@797 1630 for (ProgressMap::iterator i = m_progressBars.begin();
Chris@1266 1631 i != m_progressBars.end(); ++i) {
Chris@797 1632
Chris@797 1633 if (i->second.cancel == cancel) {
Chris@797 1634
Chris@797 1635 Layer *layer = i->first;
Chris@797 1636 Model *model = layer->getModel();
Chris@797 1637
Chris@797 1638 if (model) model->abandon();
Chris@797 1639 }
Chris@797 1640 }
Chris@797 1641 }
Chris@797 1642
Chris@797 1643 void
Chris@127 1644 View::checkProgress(void *object)
Chris@127 1645 {
Chris@127 1646 if (!m_showProgress) return;
Chris@127 1647
Chris@127 1648 int ph = height();
Chris@127 1649
Chris@555 1650 for (ProgressMap::iterator i = m_progressBars.begin();
Chris@1266 1651 i != m_progressBars.end(); ++i) {
Chris@127 1652
Chris@555 1653 QProgressBar *pb = i->second.bar;
Chris@797 1654 QPushButton *cancel = i->second.cancel;
Chris@555 1655
Chris@1266 1656 if (i->first == object) {
Chris@127 1657
Chris@555 1658 // The timer is used to test for stalls. If the progress
Chris@555 1659 // bar does not get updated for some length of time, the
Chris@555 1660 // timer prompts it to go back into "indeterminate" mode
Chris@555 1661 QTimer *timer = i->second.checkTimer;
Chris@555 1662
Chris@1266 1663 int completion = i->first->getCompletion(this);
Chris@301 1664 QString text = i->first->getPropertyContainerName();
Chris@583 1665 QString error = i->first->getError(this);
Chris@583 1666
Chris@583 1667 if (error != "" && error != m_lastError) {
Chris@583 1668 QMessageBox::critical(this, tr("Layer rendering error"), error);
Chris@583 1669 m_lastError = error;
Chris@583 1670 }
Chris@301 1671
Chris@387 1672 Model *model = i->first->getModel();
Chris@387 1673 RangeSummarisableTimeValueModel *wfm =
Chris@387 1674 dynamic_cast<RangeSummarisableTimeValueModel *>(model);
Chris@387 1675
Chris@388 1676 if (completion > 0) {
Chris@555 1677 pb->setMaximum(100); // was 0, for indeterminate start
Chris@388 1678 }
Chris@388 1679
Chris@301 1680 if (completion >= 100) {
Chris@301 1681
Chris@301 1682 //!!!
Chris@326 1683 if (wfm ||
Chris@776 1684 (model &&
Chris@776 1685 (wfm = dynamic_cast<RangeSummarisableTimeValueModel *>
Chris@776 1686 (model->getSourceModel())))) {
Chris@301 1687 completion = wfm->getAlignmentCompletion();
Chris@587 1688 // SVDEBUG << "View::checkProgress: Alignment completion = " << completion << endl;
Chris@301 1689 if (completion < 100) {
Chris@301 1690 text = tr("Alignment");
Chris@301 1691 }
Chris@301 1692 }
Chris@387 1693
Chris@387 1694 } else if (wfm) {
Chris@387 1695 update(); // ensure duration &c gets updated
Chris@301 1696 }
Chris@127 1697
Chris@1266 1698 if (completion >= 100) {
Chris@1266 1699
Chris@1266 1700 pb->hide();
Chris@797 1701 cancel->hide();
Chris@555 1702 timer->stop();
Chris@127 1703
Chris@1266 1704 } else {
Chris@127 1705
Chris@682 1706 // cerr << "progress = " << completion << endl;
Chris@555 1707
Chris@555 1708 if (!pb->isVisible()) {
Chris@555 1709 i->second.lastCheck = 0;
Chris@555 1710 timer->setInterval(2000);
Chris@555 1711 timer->start();
Chris@555 1712 }
Chris@555 1713
Chris@1351 1714 int scaled20 = ViewManager::scalePixelSize(20);
Chris@1351 1715
Chris@1351 1716 cancel->move(0, ph - pb->height()/2 - scaled20/2);
Chris@797 1717 cancel->show();
Chris@797 1718
Chris@1266 1719 pb->setValue(completion);
Chris@1351 1720 pb->move(scaled20, ph - pb->height());
Chris@1266 1721
Chris@1266 1722 pb->show();
Chris@1266 1723 pb->update();
Chris@1266 1724
Chris@1266 1725 ph -= pb->height();
Chris@1266 1726 }
Chris@1266 1727 } else {
Chris@1266 1728 if (pb->isVisible()) {
Chris@1266 1729 ph -= pb->height();
Chris@1266 1730 }
Chris@1266 1731 }
Chris@127 1732 }
Chris@127 1733 }
Chris@127 1734
Chris@555 1735 void
Chris@555 1736 View::progressCheckStalledTimerElapsed()
Chris@555 1737 {
Chris@555 1738 QObject *s = sender();
Chris@555 1739 QTimer *t = qobject_cast<QTimer *>(s);
Chris@555 1740 if (!t) return;
Chris@555 1741 for (ProgressMap::iterator i = m_progressBars.begin();
Chris@555 1742 i != m_progressBars.end(); ++i) {
Chris@555 1743 if (i->second.checkTimer == t) {
Chris@555 1744 int value = i->second.bar->value();
Chris@555 1745 if (value > 0 && value == i->second.lastCheck) {
Chris@555 1746 i->second.bar->setMaximum(0); // indeterminate
Chris@555 1747 }
Chris@555 1748 i->second.lastCheck = value;
Chris@555 1749 return;
Chris@555 1750 }
Chris@555 1751 }
Chris@555 1752 }
Chris@555 1753
Chris@384 1754 int
Chris@384 1755 View::getProgressBarWidth() const
Chris@384 1756 {
Chris@384 1757 for (ProgressMap::const_iterator i = m_progressBars.begin();
Chris@1266 1758 i != m_progressBars.end(); ++i) {
Chris@555 1759 if (i->second.bar && i->second.bar->isVisible()) {
Chris@555 1760 return i->second.bar->width();
Chris@555 1761 }
Chris@384 1762 }
Chris@384 1763
Chris@384 1764 return 0;
Chris@384 1765 }
Chris@384 1766
Chris@127 1767 void
Chris@339 1768 View::setPaintFont(QPainter &paint)
Chris@339 1769 {
Chris@955 1770 int scaleFactor = 1;
Chris@956 1771 int dpratio = effectiveDevicePixelRatio();
Chris@955 1772 if (dpratio > 1) {
Chris@955 1773 QPaintDevice *dev = paint.device();
Chris@955 1774 if (dynamic_cast<QPixmap *>(dev) || dynamic_cast<QImage *>(dev)) {
Chris@955 1775 scaleFactor = dpratio;
Chris@955 1776 }
Chris@955 1777 }
Chris@955 1778
Chris@339 1779 QFont font(paint.font());
Chris@955 1780 font.setPointSize(Preferences::getInstance()->getViewFontSize()
Chris@955 1781 * scaleFactor);
Chris@339 1782 paint.setFont(font);
Chris@339 1783 }
Chris@339 1784
Chris@915 1785 QRect
Chris@915 1786 View::getPaintRect() const
Chris@915 1787 {
Chris@919 1788 return rect();
Chris@915 1789 }
Chris@915 1790
Chris@339 1791 void
Chris@127 1792 View::paintEvent(QPaintEvent *e)
Chris@127 1793 {
Chris@127 1794 // Profiler prof("View::paintEvent", false);
Chris@800 1795 // cerr << "View::paintEvent: centre frame is " << m_centreFrame << endl;
matthiasm@785 1796
Chris@835 1797 if (m_layerStack.empty()) {
Chris@1266 1798 QFrame::paintEvent(e);
Chris@1266 1799 return;
Chris@127 1800 }
Chris@127 1801
Chris@127 1802 // ensure our constraints are met
Chris@1354 1803 m_zoomLevel = getZoomConstraintLevel
Chris@1354 1804 (m_zoomLevel, ZoomConstraint::RoundNearest);
Chris@127 1805
Chris@127 1806 QPainter paint;
Chris@127 1807 bool repaintCache = false;
Chris@127 1808 bool paintedCacheRect = false;
Chris@127 1809
Chris@127 1810 QRect cacheRect(rect());
Chris@127 1811
Chris@127 1812 if (e) {
Chris@1266 1813 cacheRect &= e->rect();
Chris@127 1814 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266 1815 cerr << "paint rect " << cacheRect.width() << "x" << cacheRect.height()
Chris@1266 1816 << ", my rect " << width() << "x" << height() << endl;
Chris@127 1817 #endif
Chris@127 1818 }
Chris@127 1819
Chris@127 1820 QRect nonCacheRect(cacheRect);
Chris@127 1821
Chris@956 1822 int dpratio = effectiveDevicePixelRatio();
Chris@953 1823
Chris@127 1824 // If not all layers are scrollable, but some of the back layers
Chris@127 1825 // are, we should store only those in the cache.
Chris@127 1826
Chris@127 1827 bool layersChanged = false;
Chris@127 1828 LayerList scrollables = getScrollableBackLayers(true, layersChanged);
Chris@127 1829 LayerList nonScrollables = getNonScrollableFrontLayers(true, layersChanged);
Chris@127 1830 bool selectionCacheable = nonScrollables.empty();
Chris@127 1831 bool haveSelections = m_manager && !m_manager->getSelections().empty();
Chris@127 1832
Chris@127 1833 // If all the non-scrollable layers are non-opaque, then we draw
Chris@127 1834 // the selection rectangle behind them and cache it. If any are
Chris@953 1835 // opaque, however, or if our device-pixel ratio is not 1 (so we
Chris@953 1836 // need to paint direct to the widget), then we can't cache.
Chris@127 1837 //
Chris@953 1838 if (dpratio == 1) {
Chris@953 1839
Chris@953 1840 if (!selectionCacheable) {
Chris@953 1841 selectionCacheable = true;
Chris@953 1842 for (LayerList::const_iterator i = nonScrollables.begin();
Chris@953 1843 i != nonScrollables.end(); ++i) {
Chris@953 1844 if ((*i)->isLayerOpaque()) {
Chris@953 1845 selectionCacheable = false;
Chris@953 1846 break;
Chris@953 1847 }
Chris@953 1848 }
Chris@953 1849 }
Chris@953 1850
Chris@953 1851 if (selectionCacheable) {
Chris@953 1852 QPoint localPos;
Chris@953 1853 bool closeToLeft, closeToRight;
Chris@953 1854 if (shouldIlluminateLocalSelection
Chris@953 1855 (localPos, closeToLeft, closeToRight)) {
Chris@953 1856 selectionCacheable = false;
Chris@953 1857 }
Chris@953 1858 }
Chris@953 1859
Chris@953 1860 } else {
Chris@953 1861
Chris@953 1862 selectionCacheable = false;
Chris@127 1863 }
Chris@127 1864
Chris@127 1865 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@682 1866 cerr << "View(" << this << ")::paintEvent: have " << scrollables.size()
Chris@1266 1867 << " scrollable back layers and " << nonScrollables.size()
Chris@1266 1868 << " non-scrollable front layers" << endl;
Chris@682 1869 cerr << "haveSelections " << haveSelections << ", selectionCacheable "
Chris@1266 1870 << selectionCacheable << ", m_selectionCached " << m_selectionCached << endl;
Chris@127 1871 #endif
Chris@127 1872
Chris@127 1873 if (layersChanged || scrollables.empty() ||
Chris@1266 1874 (haveSelections && (selectionCacheable != m_selectionCached))) {
Chris@1266 1875 delete m_cache;
Chris@1266 1876 m_cache = 0;
Chris@1266 1877 m_selectionCached = false;
Chris@127 1878 }
Chris@127 1879
Chris@952 1880 QSize scaledCacheSize(scaledSize(size(), dpratio));
Chris@952 1881 QRect scaledCacheRect(scaledRect(cacheRect, dpratio));
Chris@952 1882
Chris@952 1883 if (!m_buffer || scaledCacheSize != m_buffer->size()) {
Chris@952 1884 delete m_buffer;
Chris@952 1885 m_buffer = new QPixmap(scaledCacheSize);
Chris@952 1886 }
Chris@952 1887
Chris@127 1888 if (!scrollables.empty()) {
Chris@244 1889
Chris@244 1890 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@682 1891 cerr << "View(" << this << "): cache " << m_cache << ", cache zoom "
Chris@682 1892 << m_cacheZoomLevel << ", zoom " << m_zoomLevel << endl;
Chris@244 1893 #endif
Chris@244 1894
Chris@1327 1895 using namespace std::rel_ops;
Chris@1327 1896
Chris@1266 1897 if (!m_cache ||
Chris@1266 1898 m_cacheZoomLevel != m_zoomLevel ||
Chris@914 1899 scaledCacheSize != m_cache->size()) {
Chris@127 1900
Chris@1266 1901 // cache is not valid
Chris@1266 1902
Chris@1266 1903 if (cacheRect.width() < width()/10) {
Chris@1266 1904 delete m_cache;
Chris@244 1905 m_cache = 0;
Chris@127 1906 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266 1907 cerr << "View(" << this << ")::paintEvent: small repaint, not bothering to recreate cache" << endl;
Chris@127 1908 #endif
Chris@1266 1909 } else {
Chris@1266 1910 delete m_cache;
Chris@1266 1911 m_cache = new QPixmap(scaledCacheSize);
Chris@127 1912 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266 1913 cerr << "View(" << this << ")::paintEvent: recreated cache" << endl;
Chris@127 1914 #endif
Chris@1266 1915 cacheRect = rect();
Chris@1266 1916 repaintCache = true;
Chris@1266 1917 }
Chris@1266 1918
Chris@1266 1919 } else if (m_cacheCentreFrame != m_centreFrame) {
Chris@1266 1920
Chris@1266 1921 int dx =
Chris@1266 1922 getXForFrame(m_cacheCentreFrame) -
Chris@1266 1923 getXForFrame(m_centreFrame);
Chris@1266 1924
Chris@1266 1925 if (dx > -width() && dx < width()) {
Chris@1266 1926 static QPixmap *tmpPixmap = 0;
Chris@1266 1927 if (!tmpPixmap || tmpPixmap->size() != scaledCacheSize) {
Chris@1266 1928 delete tmpPixmap;
Chris@1266 1929 tmpPixmap = new QPixmap(scaledCacheSize);
Chris@1266 1930 }
Chris@1266 1931 paint.begin(tmpPixmap);
Chris@1266 1932 paint.drawPixmap(0, 0, *m_cache);
Chris@1266 1933 paint.end();
Chris@1266 1934 paint.begin(m_cache);
Chris@1266 1935 paint.drawPixmap(dx, 0, *tmpPixmap);
Chris@1266 1936 paint.end();
Chris@1266 1937 if (dx < 0) {
Chris@1266 1938 cacheRect = QRect(width() + dx, 0, -dx, height());
Chris@1266 1939 } else {
Chris@1266 1940 cacheRect = QRect(0, 0, dx, height());
Chris@1266 1941 }
Chris@127 1942 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266 1943 cerr << "View(" << this << ")::paintEvent: scrolled cache by " << dx << endl;
Chris@127 1944 #endif
Chris@1266 1945 } else {
Chris@1266 1946 cacheRect = rect();
Chris@127 1947 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266 1948 cerr << "View(" << this << ")::paintEvent: scrolling too far" << endl;
Chris@127 1949 #endif
Chris@1266 1950 }
Chris@1266 1951 repaintCache = true;
Chris@1266 1952
Chris@1266 1953 } else {
Chris@127 1954 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266 1955 cerr << "View(" << this << ")::paintEvent: cache is good" << endl;
Chris@127 1956 #endif
Chris@1266 1957 paint.begin(m_buffer);
Chris@1266 1958 paint.drawPixmap(scaledCacheRect, *m_cache, scaledCacheRect);
Chris@1266 1959 paint.end();
Chris@1266 1960 QFrame::paintEvent(e);
Chris@1266 1961 paintedCacheRect = true;
Chris@1266 1962 }
Chris@1266 1963
Chris@1266 1964 m_cacheCentreFrame = m_centreFrame;
Chris@1266 1965 m_cacheZoomLevel = m_zoomLevel;
Chris@127 1966 }
Chris@127 1967
Chris@127 1968 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@682 1969 // cerr << "View(" << this << ")::paintEvent: cacheRect " << cacheRect << ", nonCacheRect " << (nonCacheRect | cacheRect) << ", repaintCache " << repaintCache << ", paintedCacheRect " << paintedCacheRect << endl;
Chris@127 1970 #endif
Chris@127 1971
Chris@127 1972 // Scrollable (cacheable) items first
Chris@127 1973
Chris@919 1974 ViewProxy proxy(this, dpratio);
Chris@919 1975
Chris@127 1976 if (!paintedCacheRect) {
Chris@127 1977
Chris@914 1978 QRect rectToPaint;
Chris@914 1979
Chris@1266 1980 if (repaintCache) {
Chris@914 1981 paint.begin(m_cache);
Chris@914 1982 rectToPaint = scaledCacheRect;
Chris@914 1983 } else {
Chris@952 1984 paint.begin(m_buffer);
Chris@952 1985 rectToPaint = scaledCacheRect;
Chris@914 1986 }
Chris@914 1987
Chris@339 1988 setPaintFont(paint);
Chris@1266 1989 paint.setClipRect(rectToPaint);
Chris@287 1990
Chris@287 1991 paint.setPen(getBackground());
Chris@287 1992 paint.setBrush(getBackground());
Chris@1266 1993 paint.drawRect(rectToPaint);
Chris@1266 1994
Chris@1266 1995 paint.setPen(getForeground());
Chris@1266 1996 paint.setBrush(Qt::NoBrush);
Chris@1266 1997
Chris@1266 1998 for (LayerList::iterator i = scrollables.begin(); i != scrollables.end(); ++i) {
Chris@1266 1999 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@1266 2000 paint.save();
Chris@951 2001 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@951 2002 cerr << "Painting scrollable layer " << *i << " using proxy with repaintCache = " << repaintCache << ", dpratio = " << dpratio << ", rectToPaint = " << rectToPaint.x() << "," << rectToPaint.y() << " " << rectToPaint.width() << "x" << rectToPaint.height() << endl;
Chris@951 2003 #endif
Chris@919 2004 (*i)->paint(&proxy, paint, rectToPaint);
Chris@1266 2005 paint.restore();
Chris@1266 2006 }
Chris@1266 2007
Chris@1266 2008 if (haveSelections && selectionCacheable) {
Chris@1266 2009 drawSelections(paint);
Chris@1266 2010 m_selectionCached = repaintCache;
Chris@1266 2011 }
Chris@1266 2012
Chris@1266 2013 paint.end();
Chris@1266 2014
Chris@1266 2015 if (repaintCache) {
Chris@1266 2016 cacheRect |= (e ? e->rect() : rect());
Chris@952 2017 scaledCacheRect = scaledRect(cacheRect, dpratio);
Chris@1266 2018 paint.begin(m_buffer);
Chris@1266 2019 paint.drawPixmap(scaledCacheRect, *m_cache, scaledCacheRect);
Chris@1266 2020 paint.end();
Chris@1266 2021 }
Chris@127 2022 }
Chris@127 2023
Chris@127 2024 // Now non-cacheable items. We always need to redraw the
Chris@127 2025 // non-cacheable items across at least the area we drew of the
Chris@127 2026 // cacheable items.
Chris@127 2027
Chris@127 2028 nonCacheRect |= cacheRect;
Chris@127 2029
Chris@952 2030 QRect scaledNonCacheRect = scaledRect(nonCacheRect, dpratio);
Chris@952 2031
Chris@952 2032 paint.begin(m_buffer);
Chris@952 2033 paint.setClipRect(scaledNonCacheRect);
Chris@339 2034 setPaintFont(paint);
Chris@127 2035 if (scrollables.empty()) {
Chris@287 2036 paint.setPen(getBackground());
Chris@287 2037 paint.setBrush(getBackground());
Chris@1266 2038 paint.drawRect(scaledNonCacheRect);
Chris@127 2039 }
Chris@1266 2040
Chris@287 2041 paint.setPen(getForeground());
Chris@127 2042 paint.setBrush(Qt::NoBrush);
Chris@1266 2043
Chris@127 2044 for (LayerList::iterator i = nonScrollables.begin(); i != nonScrollables.end(); ++i) {
Chris@127 2045 // Profiler profiler2("View::paintEvent non-cacheable");
Chris@951 2046 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@951 2047 cerr << "Painting non-scrollable layer " << *i << " without proxy with repaintCache = " << repaintCache << ", dpratio = " << dpratio << ", rectToPaint = " << nonCacheRect.x() << "," << nonCacheRect.y() << " " << nonCacheRect.width() << "x" << nonCacheRect.height() << endl;
Chris@951 2048 #endif
Chris@1266 2049 (*i)->paint(&proxy, paint, scaledNonCacheRect);
Chris@127 2050 }
Chris@1266 2051
Chris@127 2052 paint.end();
Chris@953 2053
Chris@953 2054 paint.begin(this);
Chris@953 2055 QRect finalPaintRect = e ? e->rect() : rect();
Chris@953 2056 paint.drawPixmap(finalPaintRect, *m_buffer, scaledRect(finalPaintRect, dpratio));
Chris@953 2057 paint.end();
Chris@953 2058
Chris@953 2059 paint.begin(this);
Chris@339 2060 setPaintFont(paint);
Chris@953 2061 if (e) paint.setClipRect(e->rect());
Chris@127 2062 if (!m_selectionCached) {
Chris@1266 2063 drawSelections(paint);
Chris@127 2064 }
Chris@127 2065 paint.end();
Chris@127 2066
Chris@211 2067 bool showPlayPointer = true;
Chris@211 2068 if (m_followPlay == PlaybackScrollContinuous) {
Chris@211 2069 showPlayPointer = false;
Chris@806 2070 } else if (m_playPointerFrame <= getStartFrame() ||
Chris@211 2071 m_playPointerFrame >= getEndFrame()) {
Chris@211 2072 showPlayPointer = false;
Chris@211 2073 } else if (m_manager && !m_manager->isPlaying()) {
Chris@211 2074 if (m_playPointerFrame == getCentreFrame() &&
Chris@787 2075 m_manager->shouldShowCentreLine() &&
Chris@211 2076 m_followPlay != PlaybackIgnore) {
Chris@787 2077 // Don't show the play pointer when it is redundant with
Chris@787 2078 // the centre line
Chris@211 2079 showPlayPointer = false;
Chris@211 2080 }
Chris@211 2081 }
Chris@952 2082
Chris@211 2083 if (showPlayPointer) {
Chris@127 2084
Chris@1266 2085 paint.begin(this);
Chris@127 2086
Chris@211 2087 int playx = getXForFrame(m_playPointerFrame);
Chris@211 2088
Chris@287 2089 paint.setPen(getForeground());
Chris@211 2090 paint.drawLine(playx - 1, 0, playx - 1, height() - 1);
Chris@211 2091 paint.drawLine(playx + 1, 0, playx + 1, height() - 1);
Chris@211 2092 paint.drawPoint(playx, 0);
Chris@211 2093 paint.drawPoint(playx, height() - 1);
Chris@287 2094 paint.setPen(getBackground());
Chris@211 2095 paint.drawLine(playx, 1, playx, height() - 2);
Chris@127 2096
Chris@1266 2097 paint.end();
Chris@127 2098 }
Chris@127 2099
Chris@127 2100 QFrame::paintEvent(e);
Chris@127 2101 }
Chris@127 2102
Chris@127 2103 void
Chris@127 2104 View::drawSelections(QPainter &paint)
Chris@127 2105 {
Chris@217 2106 if (!hasTopLayerTimeXAxis()) return;
Chris@217 2107
Chris@127 2108 MultiSelection::SelectionList selections;
Chris@127 2109
Chris@127 2110 if (m_manager) {
Chris@1266 2111 selections = m_manager->getSelections();
Chris@1266 2112 if (m_manager->haveInProgressSelection()) {
Chris@1266 2113 bool exclusive;
Chris@1266 2114 Selection inProgressSelection =
Chris@1266 2115 m_manager->getInProgressSelection(exclusive);
Chris@1266 2116 if (exclusive) selections.clear();
Chris@1266 2117 selections.insert(inProgressSelection);
Chris@1266 2118 }
Chris@127 2119 }
Chris@127 2120
Chris@127 2121 paint.save();
Chris@183 2122
Chris@183 2123 bool translucent = !areLayerColoursSignificant();
Chris@183 2124
Chris@183 2125 if (translucent) {
Chris@183 2126 paint.setBrush(QColor(150, 150, 255, 80));
Chris@183 2127 } else {
Chris@183 2128 paint.setBrush(Qt::NoBrush);
Chris@183 2129 }
Chris@127 2130
Chris@908 2131 sv_samplerate_t sampleRate = getModelsSampleRate();
Chris@127 2132
Chris@127 2133 QPoint localPos;
Chris@908 2134 sv_frame_t illuminateFrame = -1;
Chris@127 2135 bool closeToLeft, closeToRight;
Chris@127 2136
Chris@127 2137 if (shouldIlluminateLocalSelection(localPos, closeToLeft, closeToRight)) {
Chris@1266 2138 illuminateFrame = getFrameForX(localPos.x());
Chris@127 2139 }
Chris@127 2140
Chris@127 2141 const QFontMetrics &metrics = paint.fontMetrics();
Chris@127 2142
Chris@127 2143 for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@1266 2144 i != selections.end(); ++i) {
Chris@1266 2145
Chris@1266 2146 int p0 = getXForFrame(alignFromReference(i->getStartFrame()));
Chris@1266 2147 int p1 = getXForFrame(alignFromReference(i->getEndFrame()));
Chris@1266 2148
Chris@1266 2149 if (p1 < 0 || p0 > width()) continue;
Chris@127 2150
Chris@127 2151 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266 2152 SVDEBUG << "View::drawSelections: " << p0 << ",-1 [" << (p1-p0) << "x" << (height()+1) << "]" << endl;
Chris@127 2153 #endif
Chris@127 2154
Chris@1266 2155 bool illuminateThis =
Chris@1266 2156 (illuminateFrame >= 0 && i->contains(illuminateFrame));
Chris@1266 2157
Chris@1270 2158 double h = height();
Chris@1270 2159 double penWidth = PaintAssistant::scalePenWidth(1.0);
Chris@1270 2160 double half = penWidth/2.0;
Chris@1270 2161
Chris@1270 2162 paint.setPen(QPen(QColor(150, 150, 255), penWidth));
Chris@183 2163
Chris@183 2164 if (translucent && shouldLabelSelections()) {
Chris@1270 2165 paint.drawRect(QRectF(p0, -penWidth, p1 - p0, h + 2*penWidth));
Chris@183 2166 } else {
Chris@183 2167 // Make the top & bottom lines of the box visible if we
Chris@183 2168 // are lacking some of the other visual cues. There's no
Chris@183 2169 // particular logic to this, it's just a question of what
Chris@183 2170 // I happen to think looks nice.
Chris@1270 2171 paint.drawRect(QRectF(p0, half, p1 - p0, h - penWidth));
Chris@183 2172 }
Chris@127 2173
Chris@1266 2174 if (illuminateThis) {
Chris@1266 2175 paint.save();
Chris@1270 2176 penWidth = PaintAssistant::scalePenWidth(2.0);
Chris@1270 2177 half = penWidth/2.0;
Chris@1270 2178 paint.setPen(QPen(getForeground(), penWidth));
Chris@1266 2179 if (closeToLeft) {
Chris@1270 2180 paint.drawLine(QLineF(p0, half, p1, half));
Chris@1270 2181 paint.drawLine(QLineF(p0, half, p0, h - half));
Chris@1270 2182 paint.drawLine(QLineF(p0, h - half, p1, h - half));
Chris@1266 2183 } else if (closeToRight) {
Chris@1270 2184 paint.drawLine(QLineF(p0, half, p1, half));
Chris@1270 2185 paint.drawLine(QLineF(p1, half, p1, h - half));
Chris@1270 2186 paint.drawLine(QLineF(p0, h - half, p1, h - half));
Chris@1266 2187 } else {
Chris@1266 2188 paint.setBrush(Qt::NoBrush);
Chris@1270 2189 paint.drawRect(QRectF(p0, half, p1 - p0, h - penWidth));
Chris@1266 2190 }
Chris@1266 2191 paint.restore();
Chris@1266 2192 }
Chris@1266 2193
Chris@1266 2194 if (sampleRate && shouldLabelSelections() && m_manager &&
Chris@189 2195 m_manager->shouldShowSelectionExtents()) {
Chris@1266 2196
Chris@1266 2197 QString startText = QString("%1 / %2")
Chris@1266 2198 .arg(QString::fromStdString
Chris@1266 2199 (RealTime::frame2RealTime
Chris@1266 2200 (i->getStartFrame(), sampleRate).toText(true)))
Chris@1266 2201 .arg(i->getStartFrame());
Chris@1266 2202
Chris@1266 2203 QString endText = QString(" %1 / %2")
Chris@1266 2204 .arg(QString::fromStdString
Chris@1266 2205 (RealTime::frame2RealTime
Chris@1266 2206 (i->getEndFrame(), sampleRate).toText(true)))
Chris@1266 2207 .arg(i->getEndFrame());
Chris@1266 2208
Chris@1266 2209 QString durationText = QString("(%1 / %2) ")
Chris@1266 2210 .arg(QString::fromStdString
Chris@1266 2211 (RealTime::frame2RealTime
Chris@1266 2212 (i->getEndFrame() - i->getStartFrame(), sampleRate)
Chris@1266 2213 .toText(true)))
Chris@1266 2214 .arg(i->getEndFrame() - i->getStartFrame());
Chris@1266 2215
Chris@1266 2216 int sw = metrics.width(startText),
Chris@1266 2217 ew = metrics.width(endText),
Chris@1266 2218 dw = metrics.width(durationText);
Chris@1266 2219
Chris@1266 2220 int sy = metrics.ascent() + metrics.height() + 4;
Chris@1266 2221 int ey = sy;
Chris@1266 2222 int dy = sy + metrics.height();
Chris@1266 2223
Chris@1266 2224 int sx = p0 + 2;
Chris@1266 2225 int ex = sx;
Chris@1266 2226 int dx = sx;
Chris@127 2227
Chris@501 2228 bool durationBothEnds = true;
Chris@501 2229
Chris@1266 2230 if (sw + ew > (p1 - p0)) {
Chris@1266 2231 ey += metrics.height();
Chris@1266 2232 dy += metrics.height();
Chris@501 2233 durationBothEnds = false;
Chris@1266 2234 }
Chris@1266 2235
Chris@1266 2236 if (ew < (p1 - p0)) {
Chris@1266 2237 ex = p1 - 2 - ew;
Chris@1266 2238 }
Chris@1266 2239
Chris@1266 2240 if (dw < (p1 - p0)) {
Chris@1266 2241 dx = p1 - 2 - dw;
Chris@1266 2242 }
Chris@127 2243
Chris@1193 2244 PaintAssistant::drawVisibleText(this, paint, sx, sy, startText,
Chris@1193 2245 PaintAssistant::OutlinedText);
Chris@1193 2246 PaintAssistant::drawVisibleText(this, paint, ex, ey, endText,
Chris@1193 2247 PaintAssistant::OutlinedText);
Chris@1193 2248 PaintAssistant::drawVisibleText(this, paint, dx, dy, durationText,
Chris@1193 2249 PaintAssistant::OutlinedText);
Chris@501 2250 if (durationBothEnds) {
Chris@1193 2251 PaintAssistant::drawVisibleText(this, paint, sx, dy, durationText,
Chris@1193 2252 PaintAssistant::OutlinedText);
Chris@501 2253 }
Chris@1266 2254 }
Chris@127 2255 }
Chris@127 2256
Chris@127 2257 paint.restore();
Chris@127 2258 }
Chris@127 2259
Chris@267 2260 void
Chris@270 2261 View::drawMeasurementRect(QPainter &paint, const Layer *topLayer, QRect r,
Chris@270 2262 bool focus) const
Chris@267 2263 {
Chris@587 2264 // SVDEBUG << "View::drawMeasurementRect(" << r.x() << "," << r.y() << " "
Chris@585 2265 // << r.width() << "x" << r.height() << ")" << endl;
Chris@268 2266
Chris@267 2267 if (r.x() + r.width() < 0 || r.x() >= width()) return;
Chris@267 2268
Chris@270 2269 if (r.width() != 0 || r.height() != 0) {
Chris@270 2270 paint.save();
Chris@270 2271 if (focus) {
Chris@270 2272 paint.setPen(Qt::NoPen);
Chris@272 2273 QColor brushColour(Qt::black);
Chris@272 2274 brushColour.setAlpha(hasLightBackground() ? 15 : 40);
Chris@270 2275 paint.setBrush(brushColour);
Chris@270 2276 if (r.x() > 0) {
Chris@270 2277 paint.drawRect(0, 0, r.x(), height());
Chris@270 2278 }
Chris@270 2279 if (r.x() + r.width() < width()) {
Chris@270 2280 paint.drawRect(r.x() + r.width(), 0, width()-r.x()-r.width(), height());
Chris@270 2281 }
Chris@270 2282 if (r.y() > 0) {
Chris@270 2283 paint.drawRect(r.x(), 0, r.width(), r.y());
Chris@270 2284 }
Chris@270 2285 if (r.y() + r.height() < height()) {
Chris@270 2286 paint.drawRect(r.x(), r.y() + r.height(), r.width(), height()-r.y()-r.height());
Chris@270 2287 }
Chris@270 2288 paint.setBrush(Qt::NoBrush);
Chris@270 2289 }
Chris@270 2290 paint.setPen(Qt::green);
Chris@270 2291 paint.drawRect(r);
Chris@270 2292 paint.restore();
Chris@270 2293 } else {
Chris@270 2294 paint.save();
Chris@270 2295 paint.setPen(Qt::green);
Chris@270 2296 paint.drawPoint(r.x(), r.y());
Chris@270 2297 paint.restore();
Chris@270 2298 }
Chris@270 2299
Chris@270 2300 if (!focus) return;
Chris@270 2301
Chris@278 2302 paint.save();
Chris@278 2303 QFont fn = paint.font();
Chris@278 2304 if (fn.pointSize() > 8) {
Chris@278 2305 fn.setPointSize(fn.pointSize() - 1);
Chris@278 2306 paint.setFont(fn);
Chris@278 2307 }
Chris@278 2308
Chris@267 2309 int fontHeight = paint.fontMetrics().height();
Chris@267 2310 int fontAscent = paint.fontMetrics().ascent();
Chris@267 2311
Chris@904 2312 double v0, v1;
Chris@267 2313 QString u0, u1;
Chris@267 2314 bool b0 = false, b1 = false;
Chris@267 2315
Chris@267 2316 QString axs, ays, bxs, bys, dxs, dys;
Chris@267 2317
Chris@267 2318 int axx, axy, bxx, bxy, dxx, dxy;
Chris@267 2319 int aw = 0, bw = 0, dw = 0;
Chris@267 2320
Chris@267 2321 int labelCount = 0;
Chris@267 2322
Chris@362 2323 // top-left point, x-coord
Chris@362 2324
Chris@267 2325 if ((b0 = topLayer->getXScaleValue(this, r.x(), v0, u0))) {
Chris@267 2326 axs = QString("%1 %2").arg(v0).arg(u0);
Chris@278 2327 if (u0 == "Hz" && Pitch::isFrequencyInMidiRange(v0)) {
Chris@278 2328 axs = QString("%1 (%2)").arg(axs)
Chris@278 2329 .arg(Pitch::getPitchLabelForFrequency(v0));
Chris@278 2330 }
Chris@267 2331 aw = paint.fontMetrics().width(axs);
Chris@267 2332 ++labelCount;
Chris@267 2333 }
Chris@362 2334
Chris@362 2335 // bottom-right point, x-coord
Chris@267 2336
Chris@267 2337 if (r.width() > 0) {
Chris@267 2338 if ((b1 = topLayer->getXScaleValue(this, r.x() + r.width(), v1, u1))) {
Chris@267 2339 bxs = QString("%1 %2").arg(v1).arg(u1);
Chris@278 2340 if (u1 == "Hz" && Pitch::isFrequencyInMidiRange(v1)) {
Chris@278 2341 bxs = QString("%1 (%2)").arg(bxs)
Chris@278 2342 .arg(Pitch::getPitchLabelForFrequency(v1));
Chris@278 2343 }
Chris@267 2344 bw = paint.fontMetrics().width(bxs);
Chris@267 2345 }
Chris@267 2346 }
Chris@362 2347
Chris@362 2348 // dimension, width
Chris@267 2349
Chris@283 2350 if (b0 && b1 && v1 != v0 && u0 == u1) {
Chris@362 2351 dxs = QString("[%1 %2]").arg(fabs(v1 - v0)).arg(u1);
Chris@267 2352 dw = paint.fontMetrics().width(dxs);
Chris@267 2353 }
Chris@267 2354
Chris@267 2355 b0 = false;
Chris@267 2356 b1 = false;
Chris@267 2357
Chris@362 2358 // top-left point, y-coord
Chris@362 2359
Chris@267 2360 if ((b0 = topLayer->getYScaleValue(this, r.y(), v0, u0))) {
Chris@267 2361 ays = QString("%1 %2").arg(v0).arg(u0);
Chris@278 2362 if (u0 == "Hz" && Pitch::isFrequencyInMidiRange(v0)) {
Chris@278 2363 ays = QString("%1 (%2)").arg(ays)
Chris@278 2364 .arg(Pitch::getPitchLabelForFrequency(v0));
Chris@278 2365 }
Chris@267 2366 aw = std::max(aw, paint.fontMetrics().width(ays));
Chris@267 2367 ++labelCount;
Chris@267 2368 }
Chris@267 2369
Chris@362 2370 // bottom-right point, y-coord
Chris@362 2371
Chris@267 2372 if (r.height() > 0) {
Chris@267 2373 if ((b1 = topLayer->getYScaleValue(this, r.y() + r.height(), v1, u1))) {
Chris@267 2374 bys = QString("%1 %2").arg(v1).arg(u1);
Chris@278 2375 if (u1 == "Hz" && Pitch::isFrequencyInMidiRange(v1)) {
Chris@278 2376 bys = QString("%1 (%2)").arg(bys)
Chris@278 2377 .arg(Pitch::getPitchLabelForFrequency(v1));
Chris@278 2378 }
Chris@267 2379 bw = std::max(bw, paint.fontMetrics().width(bys));
Chris@267 2380 }
Chris@267 2381 }
Chris@274 2382
Chris@274 2383 bool bd = false;
Chris@904 2384 double dy = 0.f;
Chris@274 2385 QString du;
Chris@274 2386
Chris@362 2387 // dimension, height
Chris@362 2388
Chris@274 2389 if ((bd = topLayer->getYScaleDifference(this, r.y(), r.y() + r.height(),
Chris@283 2390 dy, du)) &&
Chris@283 2391 dy != 0) {
Chris@274 2392 if (du != "") {
Chris@362 2393 if (du == "Hz") {
Chris@362 2394 int semis;
Chris@904 2395 double cents;
Chris@362 2396 semis = Pitch::getPitchForFrequencyDifference(v0, v1, &cents);
Chris@362 2397 dys = QString("[%1 %2 (%3)]")
Chris@362 2398 .arg(dy).arg(du)
Chris@362 2399 .arg(Pitch::getLabelForPitchRange(semis, cents));
Chris@362 2400 } else {
Chris@362 2401 dys = QString("[%1 %2]").arg(dy).arg(du);
Chris@362 2402 }
Chris@274 2403 } else {
Chris@362 2404 dys = QString("[%1]").arg(dy);
Chris@274 2405 }
Chris@267 2406 dw = std::max(dw, paint.fontMetrics().width(dys));
Chris@267 2407 }
Chris@267 2408
Chris@267 2409 int mw = r.width();
Chris@267 2410 int mh = r.height();
Chris@267 2411
Chris@267 2412 bool edgeLabelsInside = false;
Chris@267 2413 bool sizeLabelsInside = false;
Chris@267 2414
Chris@267 2415 if (mw < std::max(aw, std::max(bw, dw)) + 4) {
Chris@267 2416 // defaults stand
Chris@267 2417 } else if (mw < aw + bw + 4) {
Chris@267 2418 if (mh > fontHeight * labelCount * 3 + 4) {
Chris@267 2419 edgeLabelsInside = true;
Chris@267 2420 sizeLabelsInside = true;
Chris@267 2421 } else if (mh > fontHeight * labelCount * 2 + 4) {
Chris@267 2422 edgeLabelsInside = true;
Chris@267 2423 }
Chris@267 2424 } else if (mw < aw + bw + dw + 4) {
Chris@267 2425 if (mh > fontHeight * labelCount * 3 + 4) {
Chris@267 2426 edgeLabelsInside = true;
Chris@267 2427 sizeLabelsInside = true;
Chris@267 2428 } else if (mh > fontHeight * labelCount + 4) {
Chris@267 2429 edgeLabelsInside = true;
Chris@267 2430 }
Chris@267 2431 } else {
Chris@267 2432 if (mh > fontHeight * labelCount + 4) {
Chris@267 2433 edgeLabelsInside = true;
Chris@267 2434 sizeLabelsInside = true;
Chris@267 2435 }
Chris@267 2436 }
Chris@267 2437
Chris@267 2438 if (edgeLabelsInside) {
Chris@267 2439
Chris@267 2440 axx = r.x() + 2;
Chris@267 2441 axy = r.y() + fontAscent + 2;
Chris@267 2442
Chris@267 2443 bxx = r.x() + r.width() - bw - 2;
Chris@267 2444 bxy = r.y() + r.height() - (labelCount-1) * fontHeight - 2;
Chris@267 2445
Chris@267 2446 } else {
Chris@267 2447
Chris@267 2448 axx = r.x() - aw - 2;
Chris@267 2449 axy = r.y() + fontAscent;
Chris@267 2450
Chris@267 2451 bxx = r.x() + r.width() + 2;
Chris@267 2452 bxy = r.y() + r.height() - (labelCount-1) * fontHeight;
Chris@267 2453 }
Chris@267 2454
Chris@267 2455 dxx = r.width()/2 + r.x() - dw/2;
Chris@267 2456
Chris@267 2457 if (sizeLabelsInside) {
Chris@267 2458
Chris@267 2459 dxy = r.height()/2 + r.y() - (labelCount * fontHeight)/2 + fontAscent;
Chris@267 2460
Chris@267 2461 } else {
Chris@267 2462
Chris@267 2463 dxy = r.y() + r.height() + fontAscent + 2;
Chris@267 2464 }
Chris@267 2465
Chris@267 2466 if (axs != "") {
Chris@1078 2467 PaintAssistant::drawVisibleText(this, paint, axx, axy, axs, PaintAssistant::OutlinedText);
Chris@267 2468 axy += fontHeight;
Chris@267 2469 }
Chris@267 2470
Chris@267 2471 if (ays != "") {
Chris@1078 2472 PaintAssistant::drawVisibleText(this, paint, axx, axy, ays, PaintAssistant::OutlinedText);
Chris@267 2473 axy += fontHeight;
Chris@267 2474 }
Chris@267 2475
Chris@267 2476 if (bxs != "") {
Chris@1078 2477 PaintAssistant::drawVisibleText(this, paint, bxx, bxy, bxs, PaintAssistant::OutlinedText);
Chris@267 2478 bxy += fontHeight;
Chris@267 2479 }
Chris@267 2480
Chris@267 2481 if (bys != "") {
Chris@1078 2482 PaintAssistant::drawVisibleText(this, paint, bxx, bxy, bys, PaintAssistant::OutlinedText);
Chris@267 2483 bxy += fontHeight;
Chris@267 2484 }
Chris@267 2485
Chris@267 2486 if (dxs != "") {
Chris@1078 2487 PaintAssistant::drawVisibleText(this, paint, dxx, dxy, dxs, PaintAssistant::OutlinedText);
Chris@267 2488 dxy += fontHeight;
Chris@267 2489 }
Chris@267 2490
Chris@267 2491 if (dys != "") {
Chris@1078 2492 PaintAssistant::drawVisibleText(this, paint, dxx, dxy, dys, PaintAssistant::OutlinedText);
Chris@267 2493 dxy += fontHeight;
Chris@267 2494 }
Chris@278 2495
Chris@278 2496 paint.restore();
Chris@267 2497 }
Chris@267 2498
Chris@227 2499 bool
Chris@908 2500 View::render(QPainter &paint, int xorigin, sv_frame_t f0, sv_frame_t f1)
Chris@227 2501 {
Chris@1327 2502 int x0 = int(round(m_zoomLevel.framesToPixels(double(f0))));
Chris@1327 2503 int x1 = int(round(m_zoomLevel.framesToPixels(double(f1))));
Chris@806 2504
Chris@806 2505 int w = x1 - x0;
Chris@806 2506
Chris@908 2507 sv_frame_t origCentreFrame = m_centreFrame;
Chris@227 2508
Chris@227 2509 bool someLayersIncomplete = false;
Chris@227 2510
Chris@835 2511 for (LayerList::iterator i = m_layerStack.begin();
Chris@835 2512 i != m_layerStack.end(); ++i) {
Chris@227 2513
Chris@227 2514 int c = (*i)->getCompletion(this);
Chris@227 2515 if (c < 100) {
Chris@227 2516 someLayersIncomplete = true;
Chris@227 2517 break;
Chris@227 2518 }
Chris@227 2519 }
Chris@227 2520
Chris@227 2521 if (someLayersIncomplete) {
Chris@227 2522
Chris@227 2523 QProgressDialog progress(tr("Waiting for layers to be ready..."),
Chris@227 2524 tr("Cancel"), 0, 100, this);
Chris@227 2525
Chris@227 2526 int layerCompletion = 0;
Chris@227 2527
Chris@227 2528 while (layerCompletion < 100) {
Chris@227 2529
Chris@835 2530 for (LayerList::iterator i = m_layerStack.begin();
Chris@835 2531 i != m_layerStack.end(); ++i) {
Chris@227 2532
Chris@227 2533 int c = (*i)->getCompletion(this);
Chris@835 2534 if (i == m_layerStack.begin() || c < layerCompletion) {
Chris@227 2535 layerCompletion = c;
Chris@227 2536 }
Chris@227 2537 }
Chris@227 2538
Chris@227 2539 if (layerCompletion >= 100) break;
Chris@227 2540
Chris@227 2541 progress.setValue(layerCompletion);
Chris@227 2542 qApp->processEvents();
Chris@227 2543 if (progress.wasCanceled()) {
Chris@227 2544 update();
Chris@227 2545 return false;
Chris@227 2546 }
Chris@227 2547
Chris@227 2548 usleep(50000);
Chris@227 2549 }
Chris@227 2550 }
Chris@227 2551
Chris@227 2552 QProgressDialog progress(tr("Rendering image..."),
Chris@227 2553 tr("Cancel"), 0, w / width(), this);
Chris@227 2554
Chris@806 2555 for (int x = 0; x < w; x += width()) {
Chris@227 2556
Chris@227 2557 progress.setValue(x / width());
Chris@227 2558 qApp->processEvents();
Chris@227 2559 if (progress.wasCanceled()) {
Chris@227 2560 m_centreFrame = origCentreFrame;
Chris@227 2561 update();
Chris@227 2562 return false;
Chris@227 2563 }
Chris@227 2564
Chris@1327 2565 m_centreFrame = f0 + sv_frame_t(round(m_zoomLevel.pixelsToFrames
Chris@1327 2566 (x + width()/2)));
Chris@227 2567
Chris@227 2568 QRect chunk(0, 0, width(), height());
Chris@227 2569
Chris@287 2570 paint.setPen(getBackground());
Chris@287 2571 paint.setBrush(getBackground());
Chris@227 2572
Chris@1266 2573 paint.drawRect(QRect(xorigin + x, 0, width(), height()));
Chris@1266 2574
Chris@1266 2575 paint.setPen(getForeground());
Chris@1266 2576 paint.setBrush(Qt::NoBrush);
Chris@1266 2577
Chris@1266 2578 for (LayerList::iterator i = m_layerStack.begin();
Chris@835 2579 i != m_layerStack.end(); ++i) {
Chris@919 2580 if (!((*i)->isLayerDormant(this))){
Chris@919 2581
Chris@919 2582 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@919 2583
Chris@919 2584 paint.save();
Chris@919 2585 paint.translate(xorigin + x, 0);
Chris@919 2586
Chris@919 2587 cerr << "Centre frame now: " << m_centreFrame << " drawing to " << chunk.x() + x + xorigin << ", " << chunk.width() << endl;
Chris@919 2588
Chris@919 2589 (*i)->setSynchronousPainting(true);
Chris@919 2590
Chris@919 2591 (*i)->paint(this, paint, chunk);
Chris@919 2592
Chris@919 2593 (*i)->setSynchronousPainting(false);
Chris@919 2594
Chris@919 2595 paint.restore();
Chris@919 2596 }
Chris@1266 2597 }
Chris@227 2598 }
Chris@227 2599
Chris@227 2600 m_centreFrame = origCentreFrame;
Chris@227 2601 update();
Chris@227 2602 return true;
Chris@227 2603 }
Chris@227 2604
Chris@227 2605 QImage *
Chris@1202 2606 View::renderToNewImage()
Chris@227 2607 {
Chris@908 2608 sv_frame_t f0 = getModelsStartFrame();
Chris@908 2609 sv_frame_t f1 = getModelsEndFrame();
Chris@227 2610
Chris@1202 2611 return renderPartToNewImage(f0, f1);
Chris@229 2612 }
Chris@229 2613
Chris@229 2614 QImage *
Chris@1202 2615 View::renderPartToNewImage(sv_frame_t f0, sv_frame_t f1)
Chris@229 2616 {
Chris@1327 2617 int x0 = int(round(getZoomLevel().framesToPixels(double(f0))));
Chris@1327 2618 int x1 = int(round(getZoomLevel().framesToPixels(double(f1))));
Chris@227 2619
Chris@227 2620 QImage *image = new QImage(x1 - x0, height(), QImage::Format_RGB32);
Chris@227 2621
Chris@227 2622 QPainter *paint = new QPainter(image);
Chris@229 2623 if (!render(*paint, 0, f0, f1)) {
Chris@227 2624 delete paint;
Chris@227 2625 delete image;
Chris@227 2626 return 0;
Chris@227 2627 } else {
Chris@227 2628 delete paint;
Chris@227 2629 return image;
Chris@227 2630 }
Chris@227 2631 }
Chris@227 2632
Chris@229 2633 QSize
Chris@1202 2634 View::getRenderedImageSize()
Chris@229 2635 {
Chris@908 2636 sv_frame_t f0 = getModelsStartFrame();
Chris@908 2637 sv_frame_t f1 = getModelsEndFrame();
Chris@229 2638
Chris@1202 2639 return getRenderedPartImageSize(f0, f1);
Chris@229 2640 }
Chris@229 2641
Chris@229 2642 QSize
Chris@1202 2643 View::getRenderedPartImageSize(sv_frame_t f0, sv_frame_t f1)
Chris@229 2644 {
Chris@1327 2645 int x0 = int(round(getZoomLevel().framesToPixels(double(f0))));
Chris@1327 2646 int x1 = int(round(getZoomLevel().framesToPixels(double(f1))));
Chris@229 2647
Chris@229 2648 return QSize(x1 - x0, height());
Chris@229 2649 }
Chris@229 2650
Chris@1202 2651 bool
Chris@1202 2652 View::renderToSvgFile(QString filename)
Chris@1202 2653 {
Chris@1202 2654 sv_frame_t f0 = getModelsStartFrame();
Chris@1202 2655 sv_frame_t f1 = getModelsEndFrame();
Chris@1202 2656
Chris@1202 2657 return renderPartToSvgFile(filename, f0, f1);
Chris@1202 2658 }
Chris@1202 2659
Chris@1202 2660 bool
Chris@1202 2661 View::renderPartToSvgFile(QString filename, sv_frame_t f0, sv_frame_t f1)
Chris@1202 2662 {
Chris@1327 2663 int x0 = int(round(getZoomLevel().framesToPixels(double(f0))));
Chris@1327 2664 int x1 = int(round(getZoomLevel().framesToPixels(double(f1))));
Chris@1202 2665
Chris@1202 2666 QSvgGenerator generator;
Chris@1202 2667 generator.setFileName(filename);
Chris@1202 2668 generator.setSize(QSize(x1 - x0, height()));
Chris@1202 2669 generator.setViewBox(QRect(0, 0, x1 - x0, height()));
Chris@1202 2670 generator.setTitle(tr("Exported image from %1")
Chris@1202 2671 .arg(QApplication::applicationName()));
Chris@1202 2672
Chris@1202 2673 QPainter paint;
Chris@1202 2674 paint.begin(&generator);
Chris@1202 2675 bool result = render(paint, 0, f0, f1);
Chris@1202 2676 paint.end();
Chris@1202 2677 return result;
Chris@1202 2678 }
Chris@1202 2679
Chris@316 2680 void
Chris@316 2681 View::toXml(QTextStream &stream,
Chris@316 2682 QString indent, QString extraAttributes) const
Chris@127 2683 {
Chris@316 2684 stream << indent;
Chris@127 2685
Chris@1327 2686 int classicZoomValue, deepZoomValue;
Chris@1327 2687
Chris@1327 2688 if (m_zoomLevel.zone == ZoomLevel::FramesPerPixel) {
Chris@1327 2689 classicZoomValue = m_zoomLevel.level;
Chris@1327 2690 deepZoomValue = 1;
Chris@1327 2691 } else {
Chris@1327 2692 classicZoomValue = 1;
Chris@1327 2693 deepZoomValue = m_zoomLevel.level;
Chris@1327 2694 }
Chris@1327 2695
Chris@316 2696 stream << QString("<view "
Chris@316 2697 "centre=\"%1\" "
Chris@316 2698 "zoom=\"%2\" "
Chris@1327 2699 "deepZoom=\"%3\" "
Chris@1327 2700 "followPan=\"%4\" "
Chris@1327 2701 "followZoom=\"%5\" "
Chris@1327 2702 "tracking=\"%6\" "
Chris@1331 2703 " %7>\n")
Chris@1266 2704 .arg(m_centreFrame)
Chris@1327 2705 .arg(classicZoomValue)
Chris@1327 2706 .arg(deepZoomValue)
Chris@1266 2707 .arg(m_followPan)
Chris@1266 2708 .arg(m_followZoom)
Chris@1266 2709 .arg(m_followPlay == PlaybackScrollContinuous ? "scroll" :
Chris@1266 2710 m_followPlay == PlaybackScrollPageWithCentre ? "page" :
Chris@1266 2711 m_followPlay == PlaybackScrollPage ? "daw" :
Chris@815 2712 "ignore")
Chris@1266 2713 .arg(extraAttributes);
Chris@127 2714
Chris@838 2715 for (int i = 0; i < (int)m_fixedOrderLayers.size(); ++i) {
Chris@838 2716 bool visible = !m_fixedOrderLayers[i]->isLayerDormant(this);
Chris@838 2717 m_fixedOrderLayers[i]->toBriefXml(stream, indent + " ",
Chris@838 2718 QString("visible=\"%1\"")
Chris@838 2719 .arg(visible ? "true" : "false"));
Chris@127 2720 }
Chris@127 2721
Chris@316 2722 stream << indent + "</view>\n";
Chris@127 2723 }
Chris@127 2724
Chris@127 2725 ViewPropertyContainer::ViewPropertyContainer(View *v) :
Chris@127 2726 m_v(v)
Chris@127 2727 {
Chris@728 2728 // cerr << "ViewPropertyContainer: " << this << " is owned by View " << v << endl;
Chris@127 2729 connect(m_v, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@1266 2730 this, SIGNAL(propertyChanged(PropertyContainer::PropertyName)));
Chris@127 2731 }
Chris@127 2732
Chris@728 2733 ViewPropertyContainer::~ViewPropertyContainer()
Chris@728 2734 {
Chris@728 2735 }