annotate view/View.cpp @ 1361:2e3b3fadba27

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