annotate base/View.cpp @ 29:8460b3bf8f04

* Implement play mute, level and pan controls and a layer visibility control * Handle swapping the buffers in AudioCallbackPlaySource more gracefully, so that in many cases it can be done inaudibly. Still gets it wrong when playing in a noncontiguous selection. * Fix to SV file save for non-2d sparse models * Fixes to LED button drawing and AudioDial mouse functionality * Add progress bar for Ogg file import * Reshuffle PropertyContainer and its subclasses so it can be a QObject * Add layer dormancy (invisible layer permitted to free its cache space) * Optimisations to SpectrogramLayer, removing locks when reading/writing individual pixels in the cache (should be unnecessary there) -- there's still an issue here as we need a lock when reading from the model in case the model is replaced, and we don't currently have one * Several munlock() calls to make it harder to exhaust real memory if running in an RT mode with mlockall() active
author Chris Cannam
date Fri, 17 Feb 2006 18:04:26 +0000
parents 7dad8a310963
children 4afaf0df4d51
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 A waveform viewer and audio annotation editor.
Chris@1 5 Chris Cannam, Queen Mary University of London, 2005-2006
Chris@0 6
Chris@0 7 This is experimental software. Not for distribution.
Chris@0 8 */
Chris@0 9
Chris@0 10 #include "base/View.h"
Chris@0 11 #include "base/Layer.h"
Chris@0 12 #include "base/Model.h"
Chris@0 13 #include "base/ZoomConstraint.h"
Chris@0 14 #include "base/Profiler.h"
Chris@0 15
Chris@0 16 #include "layer/TimeRulerLayer.h" //!!! damn, shouldn't be including that here
Chris@25 17 #include "model/PowerOfSqrtTwoZoomConstraint.h" //!!! likewise
Chris@0 18
Chris@0 19 #include <QPainter>
Chris@0 20 #include <QPaintEvent>
Chris@0 21 #include <QRect>
Chris@0 22 #include <QApplication>
Chris@0 23
Chris@0 24 #include <iostream>
Chris@16 25 #include <cassert>
Chris@0 26
Chris@18 27 //#define DEBUG_VIEW_WIDGET_PAINT 1
Chris@0 28
Chris@0 29 using std::cerr;
Chris@0 30 using std::endl;
Chris@0 31
Chris@0 32 View::View(QWidget *w, bool showProgress) :
Chris@0 33 QFrame(w),
Chris@0 34 m_centreFrame(0),
Chris@0 35 m_zoomLevel(1024),
Chris@0 36 m_followPan(true),
Chris@0 37 m_followZoom(true),
Chris@0 38 m_followPlay(PlaybackScrollPage),
Chris@0 39 m_lightBackground(true),
Chris@0 40 m_showProgress(showProgress),
Chris@0 41 m_cache(0),
Chris@0 42 m_cacheCentreFrame(0),
Chris@0 43 m_cacheZoomLevel(1024),
Chris@9 44 m_selectionCached(false),
Chris@0 45 m_deleting(false),
Chris@8 46 m_haveSelectedLayer(false),
Chris@29 47 m_manager(0),
Chris@29 48 m_propertyContainer(new ViewPropertyContainer(this))
Chris@0 49 {
Chris@0 50 // QWidget::setAttribute(Qt::WA_PaintOnScreen);
Chris@0 51 }
Chris@0 52
Chris@0 53 View::~View()
Chris@0 54 {
Chris@0 55 m_deleting = true;
Chris@0 56
Chris@0 57 for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@0 58 delete *i;
Chris@0 59 }
Chris@29 60
Chris@29 61 delete m_propertyContainer;
Chris@0 62 }
Chris@0 63
Chris@0 64 PropertyContainer::PropertyList
Chris@0 65 View::getProperties() const
Chris@0 66 {
Chris@29 67 PropertyContainer::PropertyList list;
Chris@0 68 list.push_back(tr("Global Scroll"));
Chris@0 69 list.push_back(tr("Global Zoom"));
Chris@0 70 list.push_back(tr("Follow Playback"));
Chris@0 71 return list;
Chris@0 72 }
Chris@0 73
Chris@0 74 PropertyContainer::PropertyType
Chris@29 75 View::getPropertyType(const PropertyContainer::PropertyName &name) const
Chris@0 76 {
Chris@29 77 if (name == tr("Global Scroll")) return PropertyContainer::ToggleProperty;
Chris@29 78 if (name == tr("Global Zoom")) return PropertyContainer::ToggleProperty;
Chris@29 79 if (name == tr("Follow Playback")) return PropertyContainer::ValueProperty;
Chris@29 80 return PropertyContainer::InvalidProperty;
Chris@0 81 }
Chris@0 82
Chris@0 83 int
Chris@29 84 View::getPropertyRangeAndValue(const PropertyContainer::PropertyName &name,
Chris@29 85 int *min, int *max) const
Chris@0 86 {
Chris@0 87 if (name == tr("Global Scroll")) return m_followPan;
Chris@0 88 if (name == tr("Global Zoom")) return m_followZoom;
Chris@29 89 if (name == tr("Follow Playback")) {
Chris@29 90 if (min) *min = 0;
Chris@29 91 if (max) *max = 2;
Chris@29 92 return int(m_followPlay);
Chris@29 93 }
Chris@29 94 if (min) *min = 0;
Chris@29 95 if (max) *max = 0;
Chris@29 96 return 0;
Chris@0 97 }
Chris@0 98
Chris@0 99 QString
Chris@29 100 View::getPropertyValueLabel(const PropertyContainer::PropertyName &name,
Chris@29 101 int value) const
Chris@0 102 {
Chris@0 103 if (name == tr("Follow Playback")) {
Chris@0 104 switch (value) {
Chris@0 105 default:
Chris@0 106 case 0: return tr("Scroll");
Chris@0 107 case 1: return tr("Page");
Chris@0 108 case 2: return tr("Off");
Chris@0 109 }
Chris@0 110 }
Chris@0 111 return tr("<unknown>");
Chris@0 112 }
Chris@0 113
Chris@0 114 void
Chris@29 115 View::setProperty(const PropertyContainer::PropertyName &name, int value)
Chris@0 116 {
Chris@0 117 if (name == tr("Global Scroll")) {
Chris@0 118 setFollowGlobalPan(value != 0);
Chris@0 119 } else if (name == tr("Global Zoom")) {
Chris@0 120 setFollowGlobalZoom(value != 0);
Chris@0 121 } else if (name == tr("Follow Playback")) {
Chris@0 122 switch (value) {
Chris@0 123 default:
Chris@0 124 case 0: setPlaybackFollow(PlaybackScrollContinuous); break;
Chris@0 125 case 1: setPlaybackFollow(PlaybackScrollPage); break;
Chris@0 126 case 2: setPlaybackFollow(PlaybackIgnore); break;
Chris@0 127 }
Chris@0 128 }
Chris@0 129 }
Chris@0 130
Chris@0 131 size_t
Chris@0 132 View::getPropertyContainerCount() const
Chris@0 133 {
Chris@0 134 return m_layers.size() + 1; // the 1 is for me
Chris@0 135 }
Chris@0 136
Chris@0 137 const PropertyContainer *
Chris@0 138 View::getPropertyContainer(size_t i) const
Chris@0 139 {
Chris@0 140 return (const PropertyContainer *)(((View *)this)->
Chris@0 141 getPropertyContainer(i));
Chris@0 142 }
Chris@0 143
Chris@0 144 PropertyContainer *
Chris@0 145 View::getPropertyContainer(size_t i)
Chris@0 146 {
Chris@29 147 if (i == 0) return m_propertyContainer;
Chris@0 148 return m_layers[i-1];
Chris@0 149 }
Chris@0 150
Chris@0 151 void
Chris@0 152 View::propertyContainerSelected(PropertyContainer *pc)
Chris@0 153 {
Chris@29 154 if (pc == m_propertyContainer) {
Chris@8 155 if (m_haveSelectedLayer) {
Chris@8 156 m_haveSelectedLayer = false;
Chris@8 157 update();
Chris@8 158 }
Chris@8 159 return;
Chris@8 160 }
Chris@0 161
Chris@0 162 delete m_cache;
Chris@0 163 m_cache = 0;
Chris@0 164
Chris@0 165 Layer *selectedLayer = 0;
Chris@0 166
Chris@0 167 for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@0 168 if (*i == pc) {
Chris@0 169 selectedLayer = *i;
Chris@0 170 m_layers.erase(i);
Chris@0 171 break;
Chris@0 172 }
Chris@0 173 }
Chris@0 174
Chris@0 175 if (selectedLayer) {
Chris@8 176 m_haveSelectedLayer = true;
Chris@0 177 m_layers.push_back(selectedLayer);
Chris@0 178 update();
Chris@8 179 } else {
Chris@8 180 m_haveSelectedLayer = false;
Chris@0 181 }
Chris@0 182 }
Chris@0 183
Chris@8 184 void
Chris@8 185 View::toolModeChanged()
Chris@8 186 {
Chris@8 187 std::cerr << "View::toolModeChanged(" << m_manager->getToolMode() << ")" << std::endl;
Chris@8 188 }
Chris@8 189
Chris@0 190 long
Chris@0 191 View::getStartFrame() const
Chris@0 192 {
Chris@0 193 size_t w2 = (width() / 2) * m_zoomLevel;
Chris@0 194 size_t frame = m_centreFrame;
Chris@0 195 if (frame >= w2) {
Chris@0 196 frame -= w2;
Chris@0 197 return (frame / m_zoomLevel * m_zoomLevel);
Chris@0 198 } else {
Chris@0 199 frame = w2 - frame;
Chris@0 200 frame = frame / m_zoomLevel * m_zoomLevel;
Chris@0 201 return -(long)frame - m_zoomLevel;
Chris@0 202 }
Chris@0 203 }
Chris@0 204
Chris@0 205 size_t
Chris@0 206 View::getEndFrame() const
Chris@0 207 {
Chris@16 208 return getFrameForX(width()) - 1;
Chris@0 209 }
Chris@0 210
Chris@0 211 void
Chris@0 212 View::setStartFrame(long f)
Chris@0 213 {
Chris@0 214 setCentreFrame(f + m_zoomLevel * (width() / 2));
Chris@0 215 }
Chris@0 216
Chris@10 217 bool
Chris@0 218 View::setCentreFrame(size_t f, bool e)
Chris@0 219 {
Chris@10 220 bool changeVisible = false;
Chris@10 221
Chris@0 222 if (m_centreFrame != f) {
Chris@0 223
Chris@0 224 int formerPixel = m_centreFrame / m_zoomLevel;
Chris@0 225
Chris@0 226 m_centreFrame = f;
Chris@0 227
Chris@0 228 int newPixel = m_centreFrame / m_zoomLevel;
Chris@0 229
Chris@0 230 if (newPixel != formerPixel) {
Chris@0 231
Chris@0 232 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 233 std::cout << "View(" << this << ")::setCentreFrame: newPixel " << newPixel << ", formerPixel " << formerPixel << std::endl;
Chris@0 234 #endif
Chris@0 235 update();
Chris@10 236
Chris@10 237 changeVisible = true;
Chris@0 238 }
Chris@0 239
Chris@0 240 if (e) emit centreFrameChanged(this, f, m_followPan);
Chris@0 241 }
Chris@10 242
Chris@10 243 return changeVisible;
Chris@0 244 }
Chris@0 245
Chris@15 246 int
Chris@15 247 View::getXForFrame(long frame) const
Chris@15 248 {
Chris@15 249 return (frame - getStartFrame()) / m_zoomLevel;
Chris@15 250 }
Chris@15 251
Chris@15 252 long
Chris@15 253 View::getFrameForX(int x) const
Chris@15 254 {
Chris@15 255 return (long(x) * long(m_zoomLevel)) + getStartFrame();
Chris@15 256 }
Chris@15 257
Chris@22 258 int
Chris@22 259 View::getZoomLevel() const
Chris@22 260 {
Chris@22 261 return m_zoomLevel;
Chris@22 262 }
Chris@22 263
Chris@0 264 void
Chris@0 265 View::setZoomLevel(size_t z)
Chris@0 266 {
Chris@0 267 if (m_zoomLevel != int(z)) {
Chris@0 268 m_zoomLevel = z;
Chris@0 269 emit zoomLevelChanged(this, z, m_followZoom);
Chris@0 270 update();
Chris@0 271 }
Chris@0 272 }
Chris@0 273
Chris@16 274 View::LayerProgressBar::LayerProgressBar(QWidget *parent) :
Chris@16 275 QProgressBar(parent)
Chris@16 276 {
Chris@16 277 QFont f(font());
Chris@16 278 f.setPointSize(f.pointSize() * 8 / 10);
Chris@16 279 setFont(f);
Chris@16 280 }
Chris@16 281
Chris@0 282 void
Chris@0 283 View::addLayer(Layer *layer)
Chris@0 284 {
Chris@0 285 delete m_cache;
Chris@0 286 m_cache = 0;
Chris@0 287
Chris@0 288 m_layers.push_back(layer);
Chris@0 289
Chris@0 290 m_progressBars[layer] = new LayerProgressBar(this);
Chris@0 291 m_progressBars[layer]->setMinimum(0);
Chris@0 292 m_progressBars[layer]->setMaximum(100);
Chris@0 293 m_progressBars[layer]->setMinimumWidth(80);
Chris@0 294 m_progressBars[layer]->hide();
Chris@16 295
Chris@0 296 connect(layer, SIGNAL(layerParametersChanged()),
Chris@0 297 this, SLOT(layerParametersChanged()));
Chris@0 298 connect(layer, SIGNAL(layerNameChanged()),
Chris@0 299 this, SLOT(layerNameChanged()));
Chris@0 300 connect(layer, SIGNAL(modelChanged()),
Chris@0 301 this, SLOT(modelChanged()));
Chris@0 302 connect(layer, SIGNAL(modelCompletionChanged()),
Chris@0 303 this, SLOT(modelCompletionChanged()));
Chris@0 304 connect(layer, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 305 this, SLOT(modelChanged(size_t, size_t)));
Chris@0 306 connect(layer, SIGNAL(modelReplaced()),
Chris@0 307 this, SLOT(modelReplaced()));
Chris@0 308
Chris@0 309 update();
Chris@0 310
Chris@0 311 emit propertyContainerAdded(layer);
Chris@0 312 }
Chris@0 313
Chris@0 314 void
Chris@0 315 View::removeLayer(Layer *layer)
Chris@0 316 {
Chris@0 317 if (m_deleting) {
Chris@0 318 return;
Chris@0 319 }
Chris@0 320
Chris@0 321 delete m_cache;
Chris@0 322 m_cache = 0;
Chris@0 323
Chris@0 324 for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@0 325 if (*i == layer) {
Chris@0 326 m_layers.erase(i);
Chris@0 327 if (m_progressBars.find(layer) != m_progressBars.end()) {
Chris@0 328 delete m_progressBars[layer];
Chris@0 329 m_progressBars.erase(layer);
Chris@0 330 }
Chris@0 331 break;
Chris@0 332 }
Chris@0 333 }
Chris@0 334
Chris@0 335 update();
Chris@0 336
Chris@0 337 emit propertyContainerRemoved(layer);
Chris@0 338 }
Chris@0 339
Chris@8 340 Layer *
Chris@8 341 View::getSelectedLayer()
Chris@8 342 {
Chris@8 343 if (m_haveSelectedLayer && !m_layers.empty()) {
Chris@8 344 return getLayer(getLayerCount() - 1);
Chris@8 345 } else {
Chris@8 346 return 0;
Chris@8 347 }
Chris@8 348 }
Chris@8 349
Chris@0 350 void
Chris@0 351 View::setViewManager(ViewManager *manager)
Chris@0 352 {
Chris@0 353 if (m_manager) {
Chris@0 354 m_manager->disconnect(this, SLOT(viewManagerCentreFrameChanged(void *, unsigned long, bool)));
Chris@0 355 m_manager->disconnect(this, SLOT(viewManagerZoomLevelChanged(void *, unsigned long, bool)));
Chris@0 356 disconnect(m_manager, SIGNAL(centreFrameChanged(void *, unsigned long, bool)));
Chris@0 357 disconnect(m_manager, SIGNAL(zoomLevelChanged(void *, unsigned long, bool)));
Chris@8 358 disconnect(m_manager, SIGNAL(toolModeChanged()));
Chris@8 359 disconnect(m_manager, SIGNAL(selectionChanged()));
Chris@9 360 disconnect(m_manager, SIGNAL(inProgressSelectionChanged()));
Chris@0 361 }
Chris@0 362
Chris@0 363 m_manager = manager;
Chris@0 364 if (m_followPan) setCentreFrame(m_manager->getGlobalCentreFrame(), false);
Chris@0 365 if (m_followZoom) setZoomLevel(m_manager->getGlobalZoom());
Chris@0 366
Chris@0 367 connect(m_manager, SIGNAL(centreFrameChanged(void *, unsigned long, bool)),
Chris@0 368 this, SLOT(viewManagerCentreFrameChanged(void *, unsigned long, bool)));
Chris@0 369 connect(m_manager, SIGNAL(playbackFrameChanged(unsigned long)),
Chris@0 370 this, SLOT(viewManagerPlaybackFrameChanged(unsigned long)));
Chris@0 371 connect(m_manager, SIGNAL(zoomLevelChanged(void *, unsigned long, bool)),
Chris@0 372 this, SLOT(viewManagerZoomLevelChanged(void *, unsigned long, bool)));
Chris@8 373 connect(m_manager, SIGNAL(toolModeChanged()),
Chris@8 374 this, SLOT(toolModeChanged()));
Chris@8 375 connect(m_manager, SIGNAL(selectionChanged()),
Chris@9 376 this, SLOT(selectionChanged()));
Chris@9 377 connect(m_manager, SIGNAL(inProgressSelectionChanged()),
Chris@9 378 this, SLOT(selectionChanged()));
Chris@0 379
Chris@0 380 connect(this, SIGNAL(centreFrameChanged(void *, unsigned long, bool)),
Chris@0 381 m_manager, SIGNAL(centreFrameChanged(void *, unsigned long, bool)));
Chris@0 382 connect(this, SIGNAL(zoomLevelChanged(void *, unsigned long, bool)),
Chris@0 383 m_manager, SIGNAL(zoomLevelChanged(void *, unsigned long, bool)));
Chris@8 384
Chris@8 385 toolModeChanged();
Chris@0 386 }
Chris@0 387
Chris@0 388 void
Chris@0 389 View::setFollowGlobalPan(bool f)
Chris@0 390 {
Chris@0 391 m_followPan = f;
Chris@29 392 emit propertyContainerPropertyChanged(m_propertyContainer);
Chris@0 393 }
Chris@0 394
Chris@0 395 void
Chris@0 396 View::setFollowGlobalZoom(bool f)
Chris@0 397 {
Chris@0 398 m_followZoom = f;
Chris@29 399 emit propertyContainerPropertyChanged(m_propertyContainer);
Chris@0 400 }
Chris@0 401
Chris@0 402 void
Chris@0 403 View::setPlaybackFollow(PlaybackFollowMode m)
Chris@0 404 {
Chris@0 405 m_followPlay = m;
Chris@29 406 emit propertyContainerPropertyChanged(m_propertyContainer);
Chris@0 407 }
Chris@0 408
Chris@0 409 void
Chris@0 410 View::modelChanged()
Chris@0 411 {
Chris@0 412 QObject *obj = sender();
Chris@0 413
Chris@0 414 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1 415 std::cerr << "View(" << this << ")::modelChanged()" << std::endl;
Chris@0 416 #endif
Chris@0 417 delete m_cache;
Chris@0 418 m_cache = 0;
Chris@0 419
Chris@0 420 checkProgress(obj);
Chris@0 421
Chris@0 422 update();
Chris@0 423 }
Chris@0 424
Chris@0 425 void
Chris@0 426 View::modelChanged(size_t startFrame, size_t endFrame)
Chris@0 427 {
Chris@0 428 QObject *obj = sender();
Chris@0 429
Chris@0 430 long myStartFrame = getStartFrame();
Chris@0 431 size_t myEndFrame = getEndFrame();
Chris@0 432
Chris@1 433 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1 434 std::cerr << "View(" << this << ")::modelChanged(" << startFrame << "," << endFrame << ") [me " << myStartFrame << "," << myEndFrame << "]" << std::endl;
Chris@1 435 #endif
Chris@1 436
Chris@0 437 if (myStartFrame > 0 && endFrame < size_t(myStartFrame)) {
Chris@0 438 checkProgress(obj);
Chris@0 439 return;
Chris@0 440 }
Chris@0 441 if (startFrame > myEndFrame) {
Chris@0 442 checkProgress(obj);
Chris@0 443 return;
Chris@0 444 }
Chris@0 445
Chris@0 446 delete m_cache;
Chris@0 447 m_cache = 0;
Chris@0 448
Chris@0 449 if (long(startFrame) < myStartFrame) startFrame = myStartFrame;
Chris@0 450 if (endFrame > myEndFrame) endFrame = myEndFrame;
Chris@0 451
Chris@15 452 int x0 = getXForFrame(startFrame);
Chris@15 453 int x1 = getXForFrame(endFrame + 1);
Chris@15 454 if (x1 < x0) x1 = x0;
Chris@0 455
Chris@0 456 checkProgress(obj);
Chris@0 457
Chris@0 458 update(x0, 0, x1 - x0 + 1, height());
Chris@0 459 }
Chris@0 460
Chris@0 461 void
Chris@0 462 View::modelCompletionChanged()
Chris@0 463 {
Chris@0 464 QObject *obj = sender();
Chris@0 465 checkProgress(obj);
Chris@0 466 }
Chris@0 467
Chris@0 468 void
Chris@0 469 View::modelReplaced()
Chris@0 470 {
Chris@0 471 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1 472 std::cerr << "View(" << this << ")::modelReplaced()" << std::endl;
Chris@0 473 #endif
Chris@1 474 delete m_cache;
Chris@1 475 m_cache = 0;
Chris@1 476
Chris@0 477 update();
Chris@0 478 }
Chris@0 479
Chris@0 480 void
Chris@0 481 View::layerParametersChanged()
Chris@0 482 {
Chris@0 483 Layer *layer = dynamic_cast<Layer *>(sender());
Chris@0 484
Chris@0 485 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 486 std::cerr << "View::layerParametersChanged()" << std::endl;
Chris@0 487 #endif
Chris@0 488
Chris@0 489 delete m_cache;
Chris@0 490 m_cache = 0;
Chris@0 491 update();
Chris@0 492
Chris@0 493 if (layer) {
Chris@0 494 emit propertyContainerPropertyChanged(layer);
Chris@0 495 }
Chris@0 496 }
Chris@0 497
Chris@0 498 void
Chris@0 499 View::layerNameChanged()
Chris@0 500 {
Chris@0 501 Layer *layer = dynamic_cast<Layer *>(sender());
Chris@0 502 if (layer) emit propertyContainerNameChanged(layer);
Chris@0 503 }
Chris@0 504
Chris@0 505 void
Chris@0 506 View::viewManagerCentreFrameChanged(void *p, unsigned long f, bool locked)
Chris@0 507 {
Chris@0 508 if (m_followPan && p != this && locked) {
Chris@0 509 if (m_manager && (sender() == m_manager)) {
Chris@0 510 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 511 std::cerr << this << ": manager frame changed " << f << " from " << p << std::endl;
Chris@0 512 #endif
Chris@0 513 setCentreFrame(f);
Chris@0 514 if (p == this) repaint();
Chris@0 515 }
Chris@0 516 }
Chris@0 517 }
Chris@0 518
Chris@0 519 void
Chris@0 520 View::viewManagerPlaybackFrameChanged(unsigned long f)
Chris@0 521 {
Chris@0 522 if (m_manager) {
Chris@0 523 if (sender() != m_manager) return;
Chris@0 524 }
Chris@0 525
Chris@0 526 if (m_playPointerFrame == f) return;
Chris@15 527 bool visible = (getXForFrame(m_playPointerFrame) != getXForFrame(f));
Chris@0 528 size_t oldPlayPointerFrame = m_playPointerFrame;
Chris@0 529 m_playPointerFrame = f;
Chris@0 530 if (!visible) return;
Chris@0 531
Chris@21 532 bool modifierPressed = // we only care about these ones
Chris@21 533 ((QApplication::keyboardModifiers() & Qt::ShiftModifier) ||
Chris@21 534 (QApplication::keyboardModifiers() & Qt::ControlModifier));
Chris@20 535
Chris@0 536 switch (m_followPlay) {
Chris@0 537
Chris@0 538 case PlaybackScrollContinuous:
Chris@10 539 if (QApplication::mouseButtons() == Qt::NoButton &&
Chris@21 540 !modifierPressed) {
Chris@0 541 setCentreFrame(f, false);
Chris@0 542 }
Chris@0 543 break;
Chris@0 544
Chris@0 545 case PlaybackScrollPage:
Chris@0 546 {
Chris@15 547 int xold = getXForFrame(oldPlayPointerFrame);
Chris@10 548 repaint(xold - 1, 0, 3, height());
Chris@10 549
Chris@15 550 long w = getEndFrame() - getStartFrame();
Chris@0 551 w -= w/5;
Chris@0 552 long sf = (f / w) * w - w/8;
Chris@10 553
Chris@10 554 if (m_manager &&
Chris@10 555 m_manager->isPlaying() &&
Chris@10 556 m_manager->getPlaySelectionMode()) {
Chris@24 557 MultiSelection::SelectionList selections = m_manager->getSelections();
Chris@10 558 if (!selections.empty()) {
Chris@10 559 size_t selectionStart = selections.begin()->getStartFrame();
Chris@10 560 if (sf < long(selectionStart) - w / 10) {
Chris@10 561 sf = long(selectionStart) - w / 10;
Chris@10 562 }
Chris@10 563 }
Chris@10 564 }
Chris@10 565
Chris@0 566 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 567 std::cerr << "PlaybackScrollPage: f = " << f << ", sf = " << sf << ", start frame "
Chris@0 568 << getStartFrame() << std::endl;
Chris@0 569 #endif
Chris@10 570
Chris@15 571 // We don't consider scrolling unless the pointer is outside
Chris@15 572 // the clearly visible range already
Chris@15 573
Chris@15 574 int xnew = getXForFrame(m_playPointerFrame);
Chris@15 575
Chris@18 576 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@18 577 std::cerr << "xnew = " << xnew << ", width = " << width() << std::endl;
Chris@18 578 #endif
Chris@18 579
Chris@15 580 if (xnew < width()/8 || xnew > (width()*7)/8) {
Chris@15 581 if (QApplication::mouseButtons() == Qt::NoButton &&
Chris@21 582 !modifierPressed) {
Chris@15 583 long offset = getFrameForX(width()/2) - getStartFrame();
Chris@15 584 long newCentre = sf + offset;
Chris@15 585 bool changed = setCentreFrame(newCentre, false);
Chris@15 586 if (changed) {
Chris@15 587 xold = getXForFrame(oldPlayPointerFrame);
Chris@15 588 update(xold - 1, 0, 3, height());
Chris@15 589 }
Chris@10 590 }
Chris@10 591 }
Chris@10 592
Chris@0 593 update(xnew - 1, 0, 3, height());
Chris@10 594
Chris@0 595 break;
Chris@0 596 }
Chris@0 597
Chris@0 598 case PlaybackIgnore:
Chris@0 599 if (long(f) >= getStartFrame() && f < getEndFrame()) {
Chris@0 600 update();
Chris@0 601 }
Chris@0 602 break;
Chris@0 603 }
Chris@0 604 }
Chris@0 605
Chris@0 606 void
Chris@0 607 View::viewManagerZoomLevelChanged(void *p, unsigned long z, bool locked)
Chris@0 608 {
Chris@0 609 if (m_followZoom && p != this && locked) {
Chris@0 610 if (m_manager && (sender() == m_manager)) {
Chris@0 611 setZoomLevel(z);
Chris@0 612 if (p == this) repaint();
Chris@0 613 }
Chris@0 614 }
Chris@0 615 }
Chris@0 616
Chris@9 617 void
Chris@9 618 View::selectionChanged()
Chris@9 619 {
Chris@9 620 if (m_selectionCached) {
Chris@9 621 delete m_cache;
Chris@9 622 m_cache = 0;
Chris@9 623 m_selectionCached = false;
Chris@9 624 }
Chris@9 625 update();
Chris@9 626 }
Chris@9 627
Chris@0 628 size_t
Chris@0 629 View::getModelsStartFrame() const
Chris@0 630 {
Chris@0 631 bool first = true;
Chris@0 632 size_t startFrame = 0;
Chris@0 633
Chris@0 634 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@0 635
Chris@0 636 if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@0 637
Chris@0 638 size_t thisStartFrame = (*i)->getModel()->getStartFrame();
Chris@0 639
Chris@0 640 if (first || thisStartFrame < startFrame) {
Chris@0 641 startFrame = thisStartFrame;
Chris@0 642 }
Chris@0 643 first = false;
Chris@0 644 }
Chris@0 645 }
Chris@0 646 return startFrame;
Chris@0 647 }
Chris@0 648
Chris@0 649 size_t
Chris@0 650 View::getModelsEndFrame() const
Chris@0 651 {
Chris@0 652 bool first = true;
Chris@0 653 size_t endFrame = 0;
Chris@0 654
Chris@0 655 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@0 656
Chris@0 657 if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@0 658
Chris@0 659 size_t thisEndFrame = (*i)->getModel()->getEndFrame();
Chris@0 660
Chris@0 661 if (first || thisEndFrame > endFrame) {
Chris@0 662 endFrame = thisEndFrame;
Chris@0 663 }
Chris@0 664 first = false;
Chris@0 665 }
Chris@0 666 }
Chris@0 667
Chris@0 668 if (first) return getModelsStartFrame();
Chris@0 669 return endFrame;
Chris@0 670 }
Chris@0 671
Chris@0 672 int
Chris@0 673 View::getModelsSampleRate() const
Chris@0 674 {
Chris@0 675 //!!! Just go for the first, for now. If we were supporting
Chris@0 676 // multiple samplerates, we'd probably want to do frame/time
Chris@0 677 // conversion in the model
Chris@0 678
Chris@0 679 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@0 680 if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@0 681 return (*i)->getModel()->getSampleRate();
Chris@0 682 }
Chris@0 683 }
Chris@0 684 return 0;
Chris@0 685 }
Chris@0 686
Chris@0 687 bool
Chris@0 688 View::areLayersScrollable() const
Chris@0 689 {
Chris@0 690 // True iff all views are scrollable
Chris@0 691 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@0 692 if (!(*i)->isLayerScrollable()) return false;
Chris@0 693 }
Chris@0 694 return true;
Chris@0 695 }
Chris@0 696
Chris@0 697 View::LayerList
Chris@0 698 View::getScrollableBackLayers(bool &changed) const
Chris@0 699 {
Chris@0 700 changed = false;
Chris@0 701
Chris@0 702 LayerList scrollables;
Chris@0 703 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@29 704 if ((*i)->isLayerDormant()) continue;
Chris@0 705 if ((*i)->isLayerScrollable()) scrollables.push_back(*i);
Chris@0 706 else {
Chris@0 707 if (scrollables != m_lastScrollableBackLayers) {
Chris@0 708 m_lastScrollableBackLayers = scrollables;
Chris@0 709 changed = true;
Chris@0 710 }
Chris@0 711 return scrollables;
Chris@0 712 }
Chris@0 713 }
Chris@0 714
Chris@0 715 if (scrollables.size() == 1 &&
Chris@0 716 dynamic_cast<TimeRulerLayer *>(*scrollables.begin())) {
Chris@0 717
Chris@0 718 // If only the ruler is scrollable, it's not worth the bother
Chris@0 719 // -- it probably redraws as quickly as it refreshes from
Chris@0 720 // cache
Chris@0 721 scrollables.clear();
Chris@0 722 }
Chris@0 723
Chris@0 724 if (scrollables != m_lastScrollableBackLayers) {
Chris@0 725 m_lastScrollableBackLayers = scrollables;
Chris@0 726 changed = true;
Chris@0 727 }
Chris@0 728 return scrollables;
Chris@0 729 }
Chris@0 730
Chris@0 731 View::LayerList
Chris@0 732 View::getNonScrollableFrontLayers(bool &changed) const
Chris@0 733 {
Chris@0 734 changed = false;
Chris@0 735 LayerList scrollables = getScrollableBackLayers(changed);
Chris@0 736 LayerList nonScrollables;
Chris@0 737
Chris@0 738 // Everything in front of the first non-scrollable from the back
Chris@0 739 // should also be considered non-scrollable
Chris@0 740
Chris@0 741 size_t count = 0;
Chris@0 742 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@29 743 if ((*i)->isLayerDormant()) continue;
Chris@0 744 if (count < scrollables.size()) {
Chris@0 745 ++count;
Chris@0 746 continue;
Chris@0 747 }
Chris@0 748 nonScrollables.push_back(*i);
Chris@0 749 }
Chris@0 750
Chris@0 751 if (nonScrollables != m_lastNonScrollableBackLayers) {
Chris@0 752 m_lastNonScrollableBackLayers = nonScrollables;
Chris@0 753 changed = true;
Chris@0 754 }
Chris@0 755
Chris@0 756 return nonScrollables;
Chris@0 757 }
Chris@0 758
Chris@0 759 size_t
Chris@0 760 View::getZoomConstraintBlockSize(size_t blockSize,
Chris@0 761 ZoomConstraint::RoundingDirection dir)
Chris@0 762 const
Chris@0 763 {
Chris@0 764 size_t candidate = blockSize;
Chris@0 765 bool haveCandidate = false;
Chris@0 766
Chris@25 767 PowerOfSqrtTwoZoomConstraint defaultZoomConstraint;
Chris@25 768
Chris@0 769 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@0 770
Chris@25 771 const ZoomConstraint *zoomConstraint = (*i)->getZoomConstraint();
Chris@25 772 if (!zoomConstraint) zoomConstraint = &defaultZoomConstraint;
Chris@0 773
Chris@25 774 size_t thisBlockSize =
Chris@25 775 zoomConstraint->getNearestBlockSize(blockSize, dir);
Chris@0 776
Chris@25 777 // Go for the block size that's furthest from the one
Chris@25 778 // passed in. Most of the time, that's what we want.
Chris@25 779 if (!haveCandidate ||
Chris@25 780 (thisBlockSize > blockSize && thisBlockSize > candidate) ||
Chris@25 781 (thisBlockSize < blockSize && thisBlockSize < candidate)) {
Chris@25 782 candidate = thisBlockSize;
Chris@25 783 haveCandidate = true;
Chris@0 784 }
Chris@0 785 }
Chris@0 786
Chris@0 787 return candidate;
Chris@0 788 }
Chris@0 789
Chris@0 790 void
Chris@0 791 View::zoom(bool in)
Chris@0 792 {
Chris@0 793 int newZoomLevel = m_zoomLevel;
Chris@0 794
Chris@0 795 if (in) {
Chris@0 796 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1,
Chris@0 797 ZoomConstraint::RoundDown);
Chris@0 798 } else {
Chris@0 799 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1,
Chris@0 800 ZoomConstraint::RoundUp);
Chris@0 801 }
Chris@0 802
Chris@0 803 if (newZoomLevel != m_zoomLevel) {
Chris@0 804 setZoomLevel(newZoomLevel);
Chris@0 805 }
Chris@0 806 }
Chris@0 807
Chris@0 808 void
Chris@12 809 View::scroll(bool right, bool lots)
Chris@12 810 {
Chris@12 811 long delta;
Chris@12 812 if (lots) {
Chris@15 813 delta = (getEndFrame() - getStartFrame()) / 2;
Chris@12 814 } else {
Chris@15 815 delta = (getEndFrame() - getStartFrame()) / 20;
Chris@12 816 }
Chris@12 817 if (right) delta = -delta;
Chris@12 818
Chris@12 819 if (int(m_centreFrame) < delta) {
Chris@12 820 setCentreFrame(0);
Chris@12 821 } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) {
Chris@12 822 setCentreFrame(getModelsEndFrame());
Chris@12 823 } else {
Chris@12 824 setCentreFrame(m_centreFrame - delta);
Chris@12 825 }
Chris@12 826 }
Chris@12 827
Chris@12 828 void
Chris@0 829 View::checkProgress(void *object)
Chris@0 830 {
Chris@0 831 if (!m_showProgress) return;
Chris@0 832
Chris@0 833 int ph = height();
Chris@0 834
Chris@0 835 for (ProgressMap::const_iterator i = m_progressBars.begin();
Chris@0 836 i != m_progressBars.end(); ++i) {
Chris@0 837
Chris@0 838 if (i->first == object) {
Chris@0 839
Chris@0 840 int completion = i->first->getCompletion();
Chris@0 841
Chris@0 842 if (completion >= 100) {
Chris@0 843
Chris@0 844 i->second->hide();
Chris@0 845
Chris@0 846 } else {
Chris@0 847
Chris@0 848 i->second->setText(i->first->getPropertyContainerName());
Chris@0 849 i->second->setValue(completion);
Chris@0 850 i->second->move(0, ph - i->second->height());
Chris@0 851
Chris@0 852 i->second->show();
Chris@0 853 i->second->update();
Chris@0 854
Chris@0 855 ph -= i->second->height();
Chris@0 856 }
Chris@0 857 } else {
Chris@0 858 if (i->second->isVisible()) {
Chris@0 859 ph -= i->second->height();
Chris@0 860 }
Chris@0 861 }
Chris@0 862 }
Chris@0 863 }
Chris@0 864
Chris@0 865 void
Chris@0 866 View::paintEvent(QPaintEvent *e)
Chris@0 867 {
Chris@0 868 // Profiler prof("View::paintEvent", true);
Chris@0 869 // std::cerr << "View::paintEvent" << std::endl;
Chris@0 870
Chris@0 871 if (m_layers.empty()) {
Chris@0 872 QFrame::paintEvent(e);
Chris@0 873 return;
Chris@0 874 }
Chris@0 875
Chris@0 876 // ensure our constraints are met
Chris@0 877 m_zoomLevel = getZoomConstraintBlockSize(m_zoomLevel,
Chris@0 878 ZoomConstraint::RoundUp);
Chris@0 879
Chris@0 880 QPainter paint;
Chris@0 881 bool repaintCache = false;
Chris@0 882 bool paintedCacheRect = false;
Chris@0 883
Chris@0 884 QRect cacheRect(rect());
Chris@0 885
Chris@0 886 if (e) {
Chris@0 887 cacheRect &= e->rect();
Chris@0 888 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 889 std::cerr << "paint rect " << cacheRect.width() << "x" << cacheRect.height()
Chris@0 890 << ", my rect " << width() << "x" << height() << std::endl;
Chris@0 891 #endif
Chris@0 892 }
Chris@0 893
Chris@0 894 QRect nonCacheRect(cacheRect);
Chris@0 895
Chris@0 896 // If not all layers are scrollable, but some of the back layers
Chris@0 897 // are, we should store only those in the cache
Chris@0 898
Chris@0 899 bool layersChanged = false;
Chris@0 900 LayerList scrollables = getScrollableBackLayers(layersChanged);
Chris@0 901 LayerList nonScrollables = getNonScrollableFrontLayers(layersChanged);
Chris@9 902 bool selectionCacheable = nonScrollables.empty();
Chris@9 903 bool haveSelections = m_manager && !m_manager->getSelections().empty();
Chris@9 904 bool selectionDrawn = false;
Chris@0 905
Chris@10 906 if (!selectionCacheable) {
Chris@10 907 selectionCacheable = true;
Chris@10 908 for (LayerList::const_iterator i = nonScrollables.begin();
Chris@10 909 i != nonScrollables.end(); ++i) {
Chris@10 910 if ((*i)->isLayerOpaque()) {
Chris@10 911 selectionCacheable = false;
Chris@10 912 break;
Chris@10 913 }
Chris@10 914 }
Chris@10 915 }
Chris@10 916
Chris@0 917 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 918 std::cerr << "View(" << this << ")::paintEvent: have " << scrollables.size()
Chris@0 919 << " scrollable back layers and " << nonScrollables.size()
Chris@0 920 << " non-scrollable front layers" << std::endl;
Chris@9 921 std::cerr << "haveSelections " << haveSelections << ", selectionCacheable "
Chris@9 922 << selectionCacheable << ", m_selectionCached " << m_selectionCached << std::endl;
Chris@0 923 #endif
Chris@0 924
Chris@9 925 if (layersChanged || scrollables.empty() ||
Chris@9 926 (haveSelections && (selectionCacheable != m_selectionCached))) {
Chris@0 927 delete m_cache;
Chris@0 928 m_cache = 0;
Chris@9 929 m_selectionCached = false;
Chris@0 930 }
Chris@0 931
Chris@0 932 if (!scrollables.empty()) {
Chris@0 933 if (!m_cache ||
Chris@0 934 m_cacheZoomLevel != m_zoomLevel ||
Chris@0 935 width() != m_cache->width() ||
Chris@0 936 height() != m_cache->height()) {
Chris@0 937
Chris@0 938 // cache is not valid
Chris@0 939
Chris@0 940 if (cacheRect.width() < width()/10) {
Chris@0 941 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 942 std::cerr << "View(" << this << ")::paintEvent: small repaint, not bothering to recreate cache" << std::endl;
Chris@0 943 #endif
Chris@0 944 } else {
Chris@0 945 delete m_cache;
Chris@0 946 m_cache = new QPixmap(width(), height());
Chris@0 947 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 948 std::cerr << "View(" << this << ")::paintEvent: recreated cache" << std::endl;
Chris@0 949 #endif
Chris@0 950 cacheRect = rect();
Chris@0 951 repaintCache = true;
Chris@0 952 }
Chris@0 953
Chris@0 954 } else if (m_cacheCentreFrame != m_centreFrame) {
Chris@0 955
Chris@15 956 long dx =
Chris@15 957 getXForFrame(m_cacheCentreFrame) -
Chris@15 958 getXForFrame(m_centreFrame);
Chris@0 959
Chris@0 960 if (dx > -width() && dx < width()) {
Chris@0 961 #if defined(Q_WS_WIN32) || defined(Q_WS_MAC)
Chris@0 962 // Copying a pixmap to itself doesn't work properly on Windows
Chris@0 963 // or Mac (it only works when moving in one direction)
Chris@0 964 static QPixmap *tmpPixmap = 0;
Chris@0 965 if (!tmpPixmap ||
Chris@0 966 tmpPixmap->width() != width() ||
Chris@0 967 tmpPixmap->height() != height()) {
Chris@0 968 delete tmpPixmap;
Chris@0 969 tmpPixmap = new QPixmap(width(), height());
Chris@0 970 }
Chris@0 971 paint.begin(tmpPixmap);
Chris@0 972 paint.drawPixmap(0, 0, *m_cache);
Chris@0 973 paint.end();
Chris@0 974 paint.begin(m_cache);
Chris@0 975 paint.drawPixmap(dx, 0, *tmpPixmap);
Chris@0 976 paint.end();
Chris@0 977 #else
Chris@0 978 // But it seems to be fine on X11
Chris@0 979 paint.begin(m_cache);
Chris@0 980 paint.drawPixmap(dx, 0, *m_cache);
Chris@0 981 paint.end();
Chris@0 982 #endif
Chris@0 983
Chris@0 984 if (dx < 0) {
Chris@0 985 cacheRect = QRect(width() + dx, 0, -dx, height());
Chris@0 986 } else {
Chris@0 987 cacheRect = QRect(0, 0, dx, height());
Chris@0 988 }
Chris@0 989 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 990 std::cerr << "View(" << this << ")::paintEvent: scrolled cache by " << dx << std::endl;
Chris@0 991 #endif
Chris@0 992 } else {
Chris@0 993 cacheRect = rect();
Chris@0 994 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 995 std::cerr << "View(" << this << ")::paintEvent: scrolling too far" << std::endl;
Chris@0 996 #endif
Chris@0 997 }
Chris@0 998 repaintCache = true;
Chris@0 999
Chris@0 1000 } else {
Chris@0 1001 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 1002 std::cerr << "View(" << this << ")::paintEvent: cache is good" << std::endl;
Chris@0 1003 #endif
Chris@0 1004 paint.begin(this);
Chris@0 1005 paint.drawPixmap(cacheRect, *m_cache, cacheRect);
Chris@0 1006 paint.end();
Chris@0 1007 QFrame::paintEvent(e);
Chris@0 1008 paintedCacheRect = true;
Chris@0 1009 }
Chris@0 1010
Chris@0 1011 m_cacheCentreFrame = m_centreFrame;
Chris@0 1012 m_cacheZoomLevel = m_zoomLevel;
Chris@0 1013 }
Chris@0 1014
Chris@0 1015 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 1016 // std::cerr << "View(" << this << ")::paintEvent: cacheRect " << cacheRect << ", nonCacheRect " << (nonCacheRect | cacheRect) << ", repaintCache " << repaintCache << ", paintedCacheRect " << paintedCacheRect << std::endl;
Chris@0 1017 #endif
Chris@0 1018
Chris@0 1019 // Scrollable (cacheable) items first
Chris@0 1020
Chris@0 1021 if (!paintedCacheRect) {
Chris@0 1022
Chris@0 1023 if (repaintCache) paint.begin(m_cache);
Chris@0 1024 else paint.begin(this);
Chris@0 1025
Chris@0 1026 paint.setClipRect(cacheRect);
Chris@0 1027
Chris@0 1028 if (hasLightBackground()) {
Chris@0 1029 paint.setPen(Qt::white);
Chris@0 1030 paint.setBrush(Qt::white);
Chris@0 1031 } else {
Chris@0 1032 paint.setPen(Qt::black);
Chris@0 1033 paint.setBrush(Qt::black);
Chris@0 1034 }
Chris@0 1035 paint.drawRect(cacheRect);
Chris@0 1036
Chris@0 1037 paint.setPen(Qt::black);
Chris@0 1038 paint.setBrush(Qt::NoBrush);
Chris@0 1039
Chris@0 1040 for (LayerList::iterator i = scrollables.begin(); i != scrollables.end(); ++i) {
Chris@8 1041 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@8 1042 paint.save();
Chris@0 1043 (*i)->paint(paint, cacheRect);
Chris@8 1044 paint.restore();
Chris@0 1045 }
Chris@9 1046
Chris@9 1047 if (haveSelections && selectionCacheable) {
Chris@9 1048 drawSelections(paint);
Chris@9 1049 m_selectionCached = repaintCache;
Chris@9 1050 selectionDrawn = true;
Chris@9 1051 }
Chris@0 1052
Chris@0 1053 paint.end();
Chris@0 1054
Chris@0 1055 if (repaintCache) {
Chris@0 1056 cacheRect |= (e ? e->rect() : rect());
Chris@0 1057 paint.begin(this);
Chris@0 1058 paint.drawPixmap(cacheRect, *m_cache, cacheRect);
Chris@0 1059 paint.end();
Chris@0 1060 }
Chris@0 1061 }
Chris@0 1062
Chris@0 1063 // Now non-cacheable items. We always need to redraw the
Chris@0 1064 // non-cacheable items across at least the area we drew of the
Chris@0 1065 // cacheable items.
Chris@0 1066
Chris@0 1067 nonCacheRect |= cacheRect;
Chris@0 1068
Chris@0 1069 paint.begin(this);
Chris@0 1070 paint.setClipRect(nonCacheRect);
Chris@0 1071
Chris@0 1072 if (scrollables.empty()) {
Chris@0 1073 if (hasLightBackground()) {
Chris@0 1074 paint.setPen(Qt::white);
Chris@0 1075 paint.setBrush(Qt::white);
Chris@0 1076 } else {
Chris@0 1077 paint.setPen(Qt::black);
Chris@0 1078 paint.setBrush(Qt::black);
Chris@0 1079 }
Chris@0 1080 paint.drawRect(nonCacheRect);
Chris@0 1081 }
Chris@0 1082
Chris@0 1083 paint.setPen(Qt::black);
Chris@0 1084 paint.setBrush(Qt::NoBrush);
Chris@0 1085
Chris@0 1086 for (LayerList::iterator i = nonScrollables.begin(); i != nonScrollables.end(); ++i) {
Chris@0 1087 (*i)->paint(paint, nonCacheRect);
Chris@0 1088 }
Chris@0 1089
Chris@9 1090 paint.end();
Chris@8 1091
Chris@9 1092 paint.begin(this);
Chris@9 1093 if (e) paint.setClipRect(e->rect());
Chris@9 1094 if (!m_selectionCached) {
Chris@9 1095 drawSelections(paint);
Chris@8 1096 }
Chris@0 1097 paint.end();
Chris@0 1098
Chris@0 1099 if (m_followPlay != PlaybackScrollContinuous) {
Chris@0 1100
Chris@0 1101 paint.begin(this);
Chris@0 1102
Chris@0 1103 if (long(m_playPointerFrame) > getStartFrame() &&
Chris@0 1104 m_playPointerFrame < getEndFrame()) {
Chris@0 1105
Chris@15 1106 int playx = getXForFrame(m_playPointerFrame);
Chris@0 1107
Chris@0 1108 paint.setPen(Qt::black);
Chris@0 1109 paint.drawLine(playx - 1, 0, playx - 1, height() - 1);
Chris@0 1110 paint.drawLine(playx + 1, 0, playx + 1, height() - 1);
Chris@0 1111 paint.drawPoint(playx, 0);
Chris@0 1112 paint.drawPoint(playx, height() - 1);
Chris@0 1113 paint.setPen(Qt::white);
Chris@0 1114 paint.drawLine(playx, 1, playx, height() - 2);
Chris@0 1115 }
Chris@0 1116
Chris@0 1117 paint.end();
Chris@0 1118 }
Chris@0 1119
Chris@0 1120 QFrame::paintEvent(e);
Chris@0 1121 }
Chris@0 1122
Chris@9 1123 void
Chris@9 1124 View::drawSelections(QPainter &paint)
Chris@9 1125 {
Chris@24 1126 MultiSelection::SelectionList selections;
Chris@9 1127
Chris@9 1128 if (m_manager) {
Chris@9 1129 selections = m_manager->getSelections();
Chris@9 1130 if (m_manager->haveInProgressSelection()) {
Chris@9 1131 bool exclusive;
Chris@9 1132 Selection inProgressSelection =
Chris@9 1133 m_manager->getInProgressSelection(exclusive);
Chris@9 1134 if (exclusive) selections.clear();
Chris@9 1135 selections.insert(inProgressSelection);
Chris@9 1136 }
Chris@9 1137 }
Chris@9 1138
Chris@9 1139 paint.save();
Chris@9 1140 paint.setPen(QColor(150, 150, 255));
Chris@9 1141 paint.setBrush(QColor(150, 150, 255, 80));
Chris@9 1142
Chris@10 1143 int sampleRate = getModelsSampleRate();
Chris@10 1144
Chris@10 1145 const QFontMetrics &metrics = paint.fontMetrics();
Chris@10 1146
Chris@24 1147 for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@9 1148 i != selections.end(); ++i) {
Chris@9 1149
Chris@15 1150 int p0 = getXForFrame(i->getStartFrame());
Chris@15 1151 int p1 = getXForFrame(i->getEndFrame());
Chris@9 1152
Chris@10 1153 if (p1 < 0 || p0 > width()) continue;
Chris@9 1154
Chris@9 1155 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@9 1156 std::cerr << "View::drawSelections: " << p0 << ",-1 [" << (p1-p0) << "x" << (height()+1) << "]" << std::endl;
Chris@9 1157 #endif
Chris@9 1158
Chris@9 1159 paint.drawRect(p0, -1, p1 - p0, height() + 1);
Chris@10 1160
Chris@10 1161 if (sampleRate && shouldLabelSelections()) {
Chris@10 1162
Chris@10 1163 QString startText = QString("%1 / %2")
Chris@10 1164 .arg(QString::fromStdString
Chris@10 1165 (RealTime::frame2RealTime
Chris@10 1166 (i->getStartFrame(), sampleRate).toText(true)))
Chris@10 1167 .arg(i->getStartFrame());
Chris@10 1168
Chris@10 1169 QString endText = QString(" %1 / %2")
Chris@10 1170 .arg(QString::fromStdString
Chris@10 1171 (RealTime::frame2RealTime
Chris@10 1172 (i->getEndFrame(), sampleRate).toText(true)))
Chris@10 1173 .arg(i->getEndFrame());
Chris@10 1174
Chris@10 1175 QString durationText = QString("(%1 / %2) ")
Chris@10 1176 .arg(QString::fromStdString
Chris@10 1177 (RealTime::frame2RealTime
Chris@10 1178 (i->getEndFrame() - i->getStartFrame(), sampleRate)
Chris@10 1179 .toText(true)))
Chris@10 1180 .arg(i->getEndFrame() - i->getStartFrame());
Chris@10 1181
Chris@10 1182 int sw = metrics.width(startText),
Chris@10 1183 ew = metrics.width(endText),
Chris@10 1184 dw = metrics.width(durationText);
Chris@10 1185
Chris@10 1186 int sy = metrics.ascent() + metrics.height() + 4;
Chris@10 1187 int ey = sy;
Chris@10 1188 int dy = sy + metrics.height();
Chris@10 1189
Chris@10 1190 int sx = p0 + 2;
Chris@10 1191 int ex = sx;
Chris@10 1192 int dx = sx;
Chris@10 1193
Chris@10 1194 if (sw + ew > (p1 - p0)) {
Chris@10 1195 ey += metrics.height();
Chris@10 1196 dy += metrics.height();
Chris@10 1197 }
Chris@10 1198
Chris@10 1199 if (ew < (p1 - p0)) {
Chris@10 1200 ex = p1 - 2 - ew;
Chris@10 1201 }
Chris@10 1202
Chris@10 1203 if (dw < (p1 - p0)) {
Chris@10 1204 dx = p1 - 2 - dw;
Chris@10 1205 }
Chris@10 1206
Chris@10 1207 paint.drawText(sx, sy, startText);
Chris@10 1208 paint.drawText(ex, ey, endText);
Chris@10 1209 paint.drawText(dx, dy, durationText);
Chris@10 1210 }
Chris@9 1211 }
Chris@9 1212
Chris@9 1213 paint.restore();
Chris@9 1214 }
Chris@9 1215
Chris@3 1216 QString
Chris@3 1217 View::toXmlString(QString indent, QString extraAttributes) const
Chris@3 1218 {
Chris@3 1219 QString s;
Chris@3 1220
Chris@3 1221 s += indent;
Chris@3 1222
Chris@3 1223 s += QString("<view "
Chris@3 1224 "centre=\"%1\" "
Chris@3 1225 "zoom=\"%2\" "
Chris@3 1226 "followPan=\"%3\" "
Chris@3 1227 "followZoom=\"%4\" "
Chris@3 1228 "tracking=\"%5\" "
Chris@3 1229 "light=\"%6\" %7>\n")
Chris@3 1230 .arg(m_centreFrame)
Chris@3 1231 .arg(m_zoomLevel)
Chris@3 1232 .arg(m_followPan)
Chris@3 1233 .arg(m_followZoom)
Chris@3 1234 .arg(m_followPlay == PlaybackScrollContinuous ? "scroll" :
Chris@3 1235 m_followPlay == PlaybackScrollPage ? "page" : "ignore")
Chris@3 1236 .arg(m_lightBackground)
Chris@3 1237 .arg(extraAttributes);
Chris@3 1238
Chris@3 1239 for (size_t i = 0; i < m_layers.size(); ++i) {
Chris@3 1240 s += m_layers[i]->toXmlString(indent + " ");
Chris@3 1241 }
Chris@3 1242
Chris@4 1243 s += indent + "</view>\n";
Chris@3 1244
Chris@3 1245 return s;
Chris@3 1246 }
Chris@3 1247
Chris@29 1248 ViewPropertyContainer::ViewPropertyContainer(View *v) :
Chris@29 1249 m_v(v)
Chris@29 1250 {
Chris@29 1251 connect(m_v, SIGNAL(propertyChanged(PropertyName)),
Chris@29 1252 this, SIGNAL(propertyChanged(PropertyName)));
Chris@29 1253 }
Chris@3 1254
Chris@0 1255 #ifdef INCLUDE_MOCFILES
Chris@0 1256 #include "View.moc.cpp"
Chris@0 1257 #endif
Chris@0 1258