annotate view/View.cpp @ 1541:b4b5b8dd5fe1

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