annotate view/View.cpp @ 1539:bfacecf7ea7e

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