annotate view/View.cpp @ 1537:4f8c72adbf43

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