annotate base/View.cpp @ 31:4afaf0df4d51

* Add TextModel and TextLayer types * Make View refresh work better when editing a model (previously edits might not be refreshed if their visible changed area extended beyond the strict frame range that was being modified in the model) * Add phase-adjusted instantaneous frequency display to spectrogram layer (still a work in progress) * Pull maths aliases out into a separate header in dsp/maths so MathUtilities can be included without introducing them
author Chris Cannam
date Mon, 20 Feb 2006 13:33:36 +0000
parents 8460b3bf8f04
children 51e158b505da
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@31 417
Chris@31 418 // If the model that has changed is not used by any of the cached
Chris@31 419 // layers, we won't need to recreate the cache
Chris@31 420
Chris@31 421 bool recreate = false;
Chris@31 422
Chris@31 423 bool discard;
Chris@31 424 LayerList scrollables = getScrollableBackLayers(false, discard);
Chris@31 425 for (LayerList::const_iterator i = scrollables.begin();
Chris@31 426 i != scrollables.end(); ++i) {
Chris@31 427 if (*i == obj || (*i)->getModel() == obj) {
Chris@31 428 recreate = true;
Chris@31 429 break;
Chris@31 430 }
Chris@31 431 }
Chris@31 432
Chris@31 433 if (recreate) {
Chris@31 434 delete m_cache;
Chris@31 435 m_cache = 0;
Chris@31 436 }
Chris@0 437
Chris@0 438 checkProgress(obj);
Chris@0 439
Chris@0 440 update();
Chris@0 441 }
Chris@0 442
Chris@0 443 void
Chris@0 444 View::modelChanged(size_t startFrame, size_t endFrame)
Chris@0 445 {
Chris@0 446 QObject *obj = sender();
Chris@0 447
Chris@0 448 long myStartFrame = getStartFrame();
Chris@0 449 size_t myEndFrame = getEndFrame();
Chris@0 450
Chris@1 451 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1 452 std::cerr << "View(" << this << ")::modelChanged(" << startFrame << "," << endFrame << ") [me " << myStartFrame << "," << myEndFrame << "]" << std::endl;
Chris@1 453 #endif
Chris@1 454
Chris@0 455 if (myStartFrame > 0 && endFrame < size_t(myStartFrame)) {
Chris@0 456 checkProgress(obj);
Chris@0 457 return;
Chris@0 458 }
Chris@0 459 if (startFrame > myEndFrame) {
Chris@0 460 checkProgress(obj);
Chris@0 461 return;
Chris@0 462 }
Chris@0 463
Chris@31 464 // If the model that has changed is not used by any of the cached
Chris@31 465 // layers, we won't need to recreate the cache
Chris@31 466
Chris@31 467 bool recreate = false;
Chris@31 468
Chris@31 469 bool discard;
Chris@31 470 LayerList scrollables = getScrollableBackLayers(false, discard);
Chris@31 471 for (LayerList::const_iterator i = scrollables.begin();
Chris@31 472 i != scrollables.end(); ++i) {
Chris@31 473 if (*i == obj || (*i)->getModel() == obj) {
Chris@31 474 recreate = true;
Chris@31 475 break;
Chris@31 476 }
Chris@31 477 }
Chris@31 478
Chris@31 479 if (recreate) {
Chris@31 480 delete m_cache;
Chris@31 481 m_cache = 0;
Chris@31 482 }
Chris@0 483
Chris@0 484 if (long(startFrame) < myStartFrame) startFrame = myStartFrame;
Chris@0 485 if (endFrame > myEndFrame) endFrame = myEndFrame;
Chris@0 486
Chris@15 487 int x0 = getXForFrame(startFrame);
Chris@15 488 int x1 = getXForFrame(endFrame + 1);
Chris@15 489 if (x1 < x0) x1 = x0;
Chris@0 490
Chris@0 491 checkProgress(obj);
Chris@0 492
Chris@31 493 update();
Chris@31 494 //!! update(x0, 0, x1 - x0 + 1, height());
Chris@0 495 }
Chris@0 496
Chris@0 497 void
Chris@0 498 View::modelCompletionChanged()
Chris@0 499 {
Chris@0 500 QObject *obj = sender();
Chris@0 501 checkProgress(obj);
Chris@0 502 }
Chris@0 503
Chris@0 504 void
Chris@0 505 View::modelReplaced()
Chris@0 506 {
Chris@0 507 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1 508 std::cerr << "View(" << this << ")::modelReplaced()" << std::endl;
Chris@0 509 #endif
Chris@1 510 delete m_cache;
Chris@1 511 m_cache = 0;
Chris@1 512
Chris@0 513 update();
Chris@0 514 }
Chris@0 515
Chris@0 516 void
Chris@0 517 View::layerParametersChanged()
Chris@0 518 {
Chris@0 519 Layer *layer = dynamic_cast<Layer *>(sender());
Chris@0 520
Chris@0 521 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 522 std::cerr << "View::layerParametersChanged()" << std::endl;
Chris@0 523 #endif
Chris@0 524
Chris@0 525 delete m_cache;
Chris@0 526 m_cache = 0;
Chris@0 527 update();
Chris@0 528
Chris@0 529 if (layer) {
Chris@0 530 emit propertyContainerPropertyChanged(layer);
Chris@0 531 }
Chris@0 532 }
Chris@0 533
Chris@0 534 void
Chris@0 535 View::layerNameChanged()
Chris@0 536 {
Chris@0 537 Layer *layer = dynamic_cast<Layer *>(sender());
Chris@0 538 if (layer) emit propertyContainerNameChanged(layer);
Chris@0 539 }
Chris@0 540
Chris@0 541 void
Chris@0 542 View::viewManagerCentreFrameChanged(void *p, unsigned long f, bool locked)
Chris@0 543 {
Chris@0 544 if (m_followPan && p != this && locked) {
Chris@0 545 if (m_manager && (sender() == m_manager)) {
Chris@0 546 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 547 std::cerr << this << ": manager frame changed " << f << " from " << p << std::endl;
Chris@0 548 #endif
Chris@0 549 setCentreFrame(f);
Chris@0 550 if (p == this) repaint();
Chris@0 551 }
Chris@0 552 }
Chris@0 553 }
Chris@0 554
Chris@0 555 void
Chris@0 556 View::viewManagerPlaybackFrameChanged(unsigned long f)
Chris@0 557 {
Chris@0 558 if (m_manager) {
Chris@0 559 if (sender() != m_manager) return;
Chris@0 560 }
Chris@0 561
Chris@0 562 if (m_playPointerFrame == f) return;
Chris@15 563 bool visible = (getXForFrame(m_playPointerFrame) != getXForFrame(f));
Chris@0 564 size_t oldPlayPointerFrame = m_playPointerFrame;
Chris@0 565 m_playPointerFrame = f;
Chris@0 566 if (!visible) return;
Chris@0 567
Chris@21 568 bool modifierPressed = // we only care about these ones
Chris@21 569 ((QApplication::keyboardModifiers() & Qt::ShiftModifier) ||
Chris@21 570 (QApplication::keyboardModifiers() & Qt::ControlModifier));
Chris@20 571
Chris@0 572 switch (m_followPlay) {
Chris@0 573
Chris@0 574 case PlaybackScrollContinuous:
Chris@10 575 if (QApplication::mouseButtons() == Qt::NoButton &&
Chris@21 576 !modifierPressed) {
Chris@0 577 setCentreFrame(f, false);
Chris@0 578 }
Chris@0 579 break;
Chris@0 580
Chris@0 581 case PlaybackScrollPage:
Chris@0 582 {
Chris@15 583 int xold = getXForFrame(oldPlayPointerFrame);
Chris@10 584 repaint(xold - 1, 0, 3, height());
Chris@10 585
Chris@15 586 long w = getEndFrame() - getStartFrame();
Chris@0 587 w -= w/5;
Chris@0 588 long sf = (f / w) * w - w/8;
Chris@10 589
Chris@10 590 if (m_manager &&
Chris@10 591 m_manager->isPlaying() &&
Chris@10 592 m_manager->getPlaySelectionMode()) {
Chris@24 593 MultiSelection::SelectionList selections = m_manager->getSelections();
Chris@10 594 if (!selections.empty()) {
Chris@10 595 size_t selectionStart = selections.begin()->getStartFrame();
Chris@10 596 if (sf < long(selectionStart) - w / 10) {
Chris@10 597 sf = long(selectionStart) - w / 10;
Chris@10 598 }
Chris@10 599 }
Chris@10 600 }
Chris@10 601
Chris@0 602 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 603 std::cerr << "PlaybackScrollPage: f = " << f << ", sf = " << sf << ", start frame "
Chris@0 604 << getStartFrame() << std::endl;
Chris@0 605 #endif
Chris@10 606
Chris@15 607 // We don't consider scrolling unless the pointer is outside
Chris@15 608 // the clearly visible range already
Chris@15 609
Chris@15 610 int xnew = getXForFrame(m_playPointerFrame);
Chris@15 611
Chris@18 612 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@18 613 std::cerr << "xnew = " << xnew << ", width = " << width() << std::endl;
Chris@18 614 #endif
Chris@18 615
Chris@15 616 if (xnew < width()/8 || xnew > (width()*7)/8) {
Chris@15 617 if (QApplication::mouseButtons() == Qt::NoButton &&
Chris@21 618 !modifierPressed) {
Chris@15 619 long offset = getFrameForX(width()/2) - getStartFrame();
Chris@15 620 long newCentre = sf + offset;
Chris@15 621 bool changed = setCentreFrame(newCentre, false);
Chris@15 622 if (changed) {
Chris@15 623 xold = getXForFrame(oldPlayPointerFrame);
Chris@15 624 update(xold - 1, 0, 3, height());
Chris@15 625 }
Chris@10 626 }
Chris@10 627 }
Chris@10 628
Chris@0 629 update(xnew - 1, 0, 3, height());
Chris@10 630
Chris@0 631 break;
Chris@0 632 }
Chris@0 633
Chris@0 634 case PlaybackIgnore:
Chris@0 635 if (long(f) >= getStartFrame() && f < getEndFrame()) {
Chris@0 636 update();
Chris@0 637 }
Chris@0 638 break;
Chris@0 639 }
Chris@0 640 }
Chris@0 641
Chris@0 642 void
Chris@0 643 View::viewManagerZoomLevelChanged(void *p, unsigned long z, bool locked)
Chris@0 644 {
Chris@0 645 if (m_followZoom && p != this && locked) {
Chris@0 646 if (m_manager && (sender() == m_manager)) {
Chris@0 647 setZoomLevel(z);
Chris@0 648 if (p == this) repaint();
Chris@0 649 }
Chris@0 650 }
Chris@0 651 }
Chris@0 652
Chris@9 653 void
Chris@9 654 View::selectionChanged()
Chris@9 655 {
Chris@9 656 if (m_selectionCached) {
Chris@9 657 delete m_cache;
Chris@9 658 m_cache = 0;
Chris@9 659 m_selectionCached = false;
Chris@9 660 }
Chris@9 661 update();
Chris@9 662 }
Chris@9 663
Chris@0 664 size_t
Chris@0 665 View::getModelsStartFrame() const
Chris@0 666 {
Chris@0 667 bool first = true;
Chris@0 668 size_t startFrame = 0;
Chris@0 669
Chris@0 670 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@0 671
Chris@0 672 if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@0 673
Chris@0 674 size_t thisStartFrame = (*i)->getModel()->getStartFrame();
Chris@0 675
Chris@0 676 if (first || thisStartFrame < startFrame) {
Chris@0 677 startFrame = thisStartFrame;
Chris@0 678 }
Chris@0 679 first = false;
Chris@0 680 }
Chris@0 681 }
Chris@0 682 return startFrame;
Chris@0 683 }
Chris@0 684
Chris@0 685 size_t
Chris@0 686 View::getModelsEndFrame() const
Chris@0 687 {
Chris@0 688 bool first = true;
Chris@0 689 size_t endFrame = 0;
Chris@0 690
Chris@0 691 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@0 692
Chris@0 693 if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@0 694
Chris@0 695 size_t thisEndFrame = (*i)->getModel()->getEndFrame();
Chris@0 696
Chris@0 697 if (first || thisEndFrame > endFrame) {
Chris@0 698 endFrame = thisEndFrame;
Chris@0 699 }
Chris@0 700 first = false;
Chris@0 701 }
Chris@0 702 }
Chris@0 703
Chris@0 704 if (first) return getModelsStartFrame();
Chris@0 705 return endFrame;
Chris@0 706 }
Chris@0 707
Chris@0 708 int
Chris@0 709 View::getModelsSampleRate() const
Chris@0 710 {
Chris@0 711 //!!! Just go for the first, for now. If we were supporting
Chris@0 712 // multiple samplerates, we'd probably want to do frame/time
Chris@0 713 // conversion in the model
Chris@0 714
Chris@0 715 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@0 716 if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@0 717 return (*i)->getModel()->getSampleRate();
Chris@0 718 }
Chris@0 719 }
Chris@0 720 return 0;
Chris@0 721 }
Chris@0 722
Chris@0 723 bool
Chris@0 724 View::areLayersScrollable() const
Chris@0 725 {
Chris@0 726 // True iff all views are scrollable
Chris@0 727 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@0 728 if (!(*i)->isLayerScrollable()) return false;
Chris@0 729 }
Chris@0 730 return true;
Chris@0 731 }
Chris@0 732
Chris@0 733 View::LayerList
Chris@31 734 View::getScrollableBackLayers(bool testChanged, bool &changed) const
Chris@0 735 {
Chris@0 736 changed = false;
Chris@0 737
Chris@0 738 LayerList scrollables;
Chris@0 739 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@29 740 if ((*i)->isLayerDormant()) continue;
Chris@0 741 if ((*i)->isLayerScrollable()) scrollables.push_back(*i);
Chris@0 742 else {
Chris@31 743 if (testChanged && scrollables != m_lastScrollableBackLayers) {
Chris@0 744 m_lastScrollableBackLayers = scrollables;
Chris@0 745 changed = true;
Chris@0 746 }
Chris@0 747 return scrollables;
Chris@0 748 }
Chris@0 749 }
Chris@0 750
Chris@0 751 if (scrollables.size() == 1 &&
Chris@0 752 dynamic_cast<TimeRulerLayer *>(*scrollables.begin())) {
Chris@0 753
Chris@0 754 // If only the ruler is scrollable, it's not worth the bother
Chris@0 755 // -- it probably redraws as quickly as it refreshes from
Chris@0 756 // cache
Chris@0 757 scrollables.clear();
Chris@0 758 }
Chris@0 759
Chris@31 760 if (testChanged && scrollables != m_lastScrollableBackLayers) {
Chris@0 761 m_lastScrollableBackLayers = scrollables;
Chris@0 762 changed = true;
Chris@0 763 }
Chris@0 764 return scrollables;
Chris@0 765 }
Chris@0 766
Chris@0 767 View::LayerList
Chris@31 768 View::getNonScrollableFrontLayers(bool testChanged, bool &changed) const
Chris@0 769 {
Chris@0 770 changed = false;
Chris@31 771 LayerList scrollables = getScrollableBackLayers(testChanged, changed);
Chris@0 772 LayerList nonScrollables;
Chris@0 773
Chris@0 774 // Everything in front of the first non-scrollable from the back
Chris@0 775 // should also be considered non-scrollable
Chris@0 776
Chris@0 777 size_t count = 0;
Chris@0 778 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@29 779 if ((*i)->isLayerDormant()) continue;
Chris@0 780 if (count < scrollables.size()) {
Chris@0 781 ++count;
Chris@0 782 continue;
Chris@0 783 }
Chris@0 784 nonScrollables.push_back(*i);
Chris@0 785 }
Chris@0 786
Chris@31 787 if (testChanged && nonScrollables != m_lastNonScrollableBackLayers) {
Chris@0 788 m_lastNonScrollableBackLayers = nonScrollables;
Chris@0 789 changed = true;
Chris@0 790 }
Chris@0 791
Chris@0 792 return nonScrollables;
Chris@0 793 }
Chris@0 794
Chris@0 795 size_t
Chris@0 796 View::getZoomConstraintBlockSize(size_t blockSize,
Chris@0 797 ZoomConstraint::RoundingDirection dir)
Chris@0 798 const
Chris@0 799 {
Chris@0 800 size_t candidate = blockSize;
Chris@0 801 bool haveCandidate = false;
Chris@0 802
Chris@25 803 PowerOfSqrtTwoZoomConstraint defaultZoomConstraint;
Chris@25 804
Chris@0 805 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@0 806
Chris@25 807 const ZoomConstraint *zoomConstraint = (*i)->getZoomConstraint();
Chris@25 808 if (!zoomConstraint) zoomConstraint = &defaultZoomConstraint;
Chris@0 809
Chris@25 810 size_t thisBlockSize =
Chris@25 811 zoomConstraint->getNearestBlockSize(blockSize, dir);
Chris@0 812
Chris@25 813 // Go for the block size that's furthest from the one
Chris@25 814 // passed in. Most of the time, that's what we want.
Chris@25 815 if (!haveCandidate ||
Chris@25 816 (thisBlockSize > blockSize && thisBlockSize > candidate) ||
Chris@25 817 (thisBlockSize < blockSize && thisBlockSize < candidate)) {
Chris@25 818 candidate = thisBlockSize;
Chris@25 819 haveCandidate = true;
Chris@0 820 }
Chris@0 821 }
Chris@0 822
Chris@0 823 return candidate;
Chris@0 824 }
Chris@0 825
Chris@0 826 void
Chris@0 827 View::zoom(bool in)
Chris@0 828 {
Chris@0 829 int newZoomLevel = m_zoomLevel;
Chris@0 830
Chris@0 831 if (in) {
Chris@0 832 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1,
Chris@0 833 ZoomConstraint::RoundDown);
Chris@0 834 } else {
Chris@0 835 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1,
Chris@0 836 ZoomConstraint::RoundUp);
Chris@0 837 }
Chris@0 838
Chris@0 839 if (newZoomLevel != m_zoomLevel) {
Chris@0 840 setZoomLevel(newZoomLevel);
Chris@0 841 }
Chris@0 842 }
Chris@0 843
Chris@0 844 void
Chris@12 845 View::scroll(bool right, bool lots)
Chris@12 846 {
Chris@12 847 long delta;
Chris@12 848 if (lots) {
Chris@15 849 delta = (getEndFrame() - getStartFrame()) / 2;
Chris@12 850 } else {
Chris@15 851 delta = (getEndFrame() - getStartFrame()) / 20;
Chris@12 852 }
Chris@12 853 if (right) delta = -delta;
Chris@12 854
Chris@12 855 if (int(m_centreFrame) < delta) {
Chris@12 856 setCentreFrame(0);
Chris@12 857 } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) {
Chris@12 858 setCentreFrame(getModelsEndFrame());
Chris@12 859 } else {
Chris@12 860 setCentreFrame(m_centreFrame - delta);
Chris@12 861 }
Chris@12 862 }
Chris@12 863
Chris@12 864 void
Chris@0 865 View::checkProgress(void *object)
Chris@0 866 {
Chris@0 867 if (!m_showProgress) return;
Chris@0 868
Chris@0 869 int ph = height();
Chris@0 870
Chris@0 871 for (ProgressMap::const_iterator i = m_progressBars.begin();
Chris@0 872 i != m_progressBars.end(); ++i) {
Chris@0 873
Chris@0 874 if (i->first == object) {
Chris@0 875
Chris@0 876 int completion = i->first->getCompletion();
Chris@0 877
Chris@0 878 if (completion >= 100) {
Chris@0 879
Chris@0 880 i->second->hide();
Chris@0 881
Chris@0 882 } else {
Chris@0 883
Chris@0 884 i->second->setText(i->first->getPropertyContainerName());
Chris@0 885 i->second->setValue(completion);
Chris@0 886 i->second->move(0, ph - i->second->height());
Chris@0 887
Chris@0 888 i->second->show();
Chris@0 889 i->second->update();
Chris@0 890
Chris@0 891 ph -= i->second->height();
Chris@0 892 }
Chris@0 893 } else {
Chris@0 894 if (i->second->isVisible()) {
Chris@0 895 ph -= i->second->height();
Chris@0 896 }
Chris@0 897 }
Chris@0 898 }
Chris@0 899 }
Chris@0 900
Chris@0 901 void
Chris@0 902 View::paintEvent(QPaintEvent *e)
Chris@0 903 {
Chris@0 904 // Profiler prof("View::paintEvent", true);
Chris@0 905 // std::cerr << "View::paintEvent" << std::endl;
Chris@0 906
Chris@0 907 if (m_layers.empty()) {
Chris@0 908 QFrame::paintEvent(e);
Chris@0 909 return;
Chris@0 910 }
Chris@0 911
Chris@0 912 // ensure our constraints are met
Chris@0 913 m_zoomLevel = getZoomConstraintBlockSize(m_zoomLevel,
Chris@0 914 ZoomConstraint::RoundUp);
Chris@0 915
Chris@0 916 QPainter paint;
Chris@0 917 bool repaintCache = false;
Chris@0 918 bool paintedCacheRect = false;
Chris@0 919
Chris@0 920 QRect cacheRect(rect());
Chris@0 921
Chris@0 922 if (e) {
Chris@0 923 cacheRect &= e->rect();
Chris@0 924 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 925 std::cerr << "paint rect " << cacheRect.width() << "x" << cacheRect.height()
Chris@0 926 << ", my rect " << width() << "x" << height() << std::endl;
Chris@0 927 #endif
Chris@0 928 }
Chris@0 929
Chris@0 930 QRect nonCacheRect(cacheRect);
Chris@0 931
Chris@0 932 // If not all layers are scrollable, but some of the back layers
Chris@0 933 // are, we should store only those in the cache
Chris@0 934
Chris@0 935 bool layersChanged = false;
Chris@31 936 LayerList scrollables = getScrollableBackLayers(true, layersChanged);
Chris@31 937 LayerList nonScrollables = getNonScrollableFrontLayers(true, layersChanged);
Chris@9 938 bool selectionCacheable = nonScrollables.empty();
Chris@9 939 bool haveSelections = m_manager && !m_manager->getSelections().empty();
Chris@9 940 bool selectionDrawn = false;
Chris@0 941
Chris@10 942 if (!selectionCacheable) {
Chris@10 943 selectionCacheable = true;
Chris@10 944 for (LayerList::const_iterator i = nonScrollables.begin();
Chris@10 945 i != nonScrollables.end(); ++i) {
Chris@10 946 if ((*i)->isLayerOpaque()) {
Chris@10 947 selectionCacheable = false;
Chris@10 948 break;
Chris@10 949 }
Chris@10 950 }
Chris@10 951 }
Chris@10 952
Chris@0 953 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 954 std::cerr << "View(" << this << ")::paintEvent: have " << scrollables.size()
Chris@0 955 << " scrollable back layers and " << nonScrollables.size()
Chris@0 956 << " non-scrollable front layers" << std::endl;
Chris@9 957 std::cerr << "haveSelections " << haveSelections << ", selectionCacheable "
Chris@9 958 << selectionCacheable << ", m_selectionCached " << m_selectionCached << std::endl;
Chris@0 959 #endif
Chris@0 960
Chris@9 961 if (layersChanged || scrollables.empty() ||
Chris@9 962 (haveSelections && (selectionCacheable != m_selectionCached))) {
Chris@0 963 delete m_cache;
Chris@0 964 m_cache = 0;
Chris@9 965 m_selectionCached = false;
Chris@0 966 }
Chris@0 967
Chris@0 968 if (!scrollables.empty()) {
Chris@0 969 if (!m_cache ||
Chris@0 970 m_cacheZoomLevel != m_zoomLevel ||
Chris@0 971 width() != m_cache->width() ||
Chris@0 972 height() != m_cache->height()) {
Chris@0 973
Chris@0 974 // cache is not valid
Chris@0 975
Chris@0 976 if (cacheRect.width() < width()/10) {
Chris@0 977 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 978 std::cerr << "View(" << this << ")::paintEvent: small repaint, not bothering to recreate cache" << std::endl;
Chris@0 979 #endif
Chris@0 980 } else {
Chris@0 981 delete m_cache;
Chris@0 982 m_cache = new QPixmap(width(), height());
Chris@0 983 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 984 std::cerr << "View(" << this << ")::paintEvent: recreated cache" << std::endl;
Chris@0 985 #endif
Chris@0 986 cacheRect = rect();
Chris@0 987 repaintCache = true;
Chris@0 988 }
Chris@0 989
Chris@0 990 } else if (m_cacheCentreFrame != m_centreFrame) {
Chris@0 991
Chris@15 992 long dx =
Chris@15 993 getXForFrame(m_cacheCentreFrame) -
Chris@15 994 getXForFrame(m_centreFrame);
Chris@0 995
Chris@0 996 if (dx > -width() && dx < width()) {
Chris@0 997 #if defined(Q_WS_WIN32) || defined(Q_WS_MAC)
Chris@0 998 // Copying a pixmap to itself doesn't work properly on Windows
Chris@0 999 // or Mac (it only works when moving in one direction)
Chris@0 1000 static QPixmap *tmpPixmap = 0;
Chris@0 1001 if (!tmpPixmap ||
Chris@0 1002 tmpPixmap->width() != width() ||
Chris@0 1003 tmpPixmap->height() != height()) {
Chris@0 1004 delete tmpPixmap;
Chris@0 1005 tmpPixmap = new QPixmap(width(), height());
Chris@0 1006 }
Chris@0 1007 paint.begin(tmpPixmap);
Chris@0 1008 paint.drawPixmap(0, 0, *m_cache);
Chris@0 1009 paint.end();
Chris@0 1010 paint.begin(m_cache);
Chris@0 1011 paint.drawPixmap(dx, 0, *tmpPixmap);
Chris@0 1012 paint.end();
Chris@0 1013 #else
Chris@0 1014 // But it seems to be fine on X11
Chris@0 1015 paint.begin(m_cache);
Chris@0 1016 paint.drawPixmap(dx, 0, *m_cache);
Chris@0 1017 paint.end();
Chris@0 1018 #endif
Chris@0 1019
Chris@0 1020 if (dx < 0) {
Chris@0 1021 cacheRect = QRect(width() + dx, 0, -dx, height());
Chris@0 1022 } else {
Chris@0 1023 cacheRect = QRect(0, 0, dx, height());
Chris@0 1024 }
Chris@0 1025 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 1026 std::cerr << "View(" << this << ")::paintEvent: scrolled cache by " << dx << std::endl;
Chris@0 1027 #endif
Chris@0 1028 } else {
Chris@0 1029 cacheRect = rect();
Chris@0 1030 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 1031 std::cerr << "View(" << this << ")::paintEvent: scrolling too far" << std::endl;
Chris@0 1032 #endif
Chris@0 1033 }
Chris@0 1034 repaintCache = true;
Chris@0 1035
Chris@0 1036 } else {
Chris@0 1037 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 1038 std::cerr << "View(" << this << ")::paintEvent: cache is good" << std::endl;
Chris@0 1039 #endif
Chris@0 1040 paint.begin(this);
Chris@0 1041 paint.drawPixmap(cacheRect, *m_cache, cacheRect);
Chris@0 1042 paint.end();
Chris@0 1043 QFrame::paintEvent(e);
Chris@0 1044 paintedCacheRect = true;
Chris@0 1045 }
Chris@0 1046
Chris@0 1047 m_cacheCentreFrame = m_centreFrame;
Chris@0 1048 m_cacheZoomLevel = m_zoomLevel;
Chris@0 1049 }
Chris@0 1050
Chris@0 1051 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@0 1052 // std::cerr << "View(" << this << ")::paintEvent: cacheRect " << cacheRect << ", nonCacheRect " << (nonCacheRect | cacheRect) << ", repaintCache " << repaintCache << ", paintedCacheRect " << paintedCacheRect << std::endl;
Chris@0 1053 #endif
Chris@0 1054
Chris@0 1055 // Scrollable (cacheable) items first
Chris@0 1056
Chris@0 1057 if (!paintedCacheRect) {
Chris@0 1058
Chris@0 1059 if (repaintCache) paint.begin(m_cache);
Chris@0 1060 else paint.begin(this);
Chris@0 1061
Chris@0 1062 paint.setClipRect(cacheRect);
Chris@0 1063
Chris@0 1064 if (hasLightBackground()) {
Chris@0 1065 paint.setPen(Qt::white);
Chris@0 1066 paint.setBrush(Qt::white);
Chris@0 1067 } else {
Chris@0 1068 paint.setPen(Qt::black);
Chris@0 1069 paint.setBrush(Qt::black);
Chris@0 1070 }
Chris@0 1071 paint.drawRect(cacheRect);
Chris@0 1072
Chris@0 1073 paint.setPen(Qt::black);
Chris@0 1074 paint.setBrush(Qt::NoBrush);
Chris@0 1075
Chris@0 1076 for (LayerList::iterator i = scrollables.begin(); i != scrollables.end(); ++i) {
Chris@8 1077 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@8 1078 paint.save();
Chris@0 1079 (*i)->paint(paint, cacheRect);
Chris@8 1080 paint.restore();
Chris@0 1081 }
Chris@9 1082
Chris@9 1083 if (haveSelections && selectionCacheable) {
Chris@9 1084 drawSelections(paint);
Chris@9 1085 m_selectionCached = repaintCache;
Chris@9 1086 selectionDrawn = true;
Chris@9 1087 }
Chris@0 1088
Chris@0 1089 paint.end();
Chris@0 1090
Chris@0 1091 if (repaintCache) {
Chris@0 1092 cacheRect |= (e ? e->rect() : rect());
Chris@0 1093 paint.begin(this);
Chris@0 1094 paint.drawPixmap(cacheRect, *m_cache, cacheRect);
Chris@0 1095 paint.end();
Chris@0 1096 }
Chris@0 1097 }
Chris@0 1098
Chris@0 1099 // Now non-cacheable items. We always need to redraw the
Chris@0 1100 // non-cacheable items across at least the area we drew of the
Chris@0 1101 // cacheable items.
Chris@0 1102
Chris@0 1103 nonCacheRect |= cacheRect;
Chris@0 1104
Chris@0 1105 paint.begin(this);
Chris@0 1106 paint.setClipRect(nonCacheRect);
Chris@0 1107
Chris@0 1108 if (scrollables.empty()) {
Chris@0 1109 if (hasLightBackground()) {
Chris@0 1110 paint.setPen(Qt::white);
Chris@0 1111 paint.setBrush(Qt::white);
Chris@0 1112 } else {
Chris@0 1113 paint.setPen(Qt::black);
Chris@0 1114 paint.setBrush(Qt::black);
Chris@0 1115 }
Chris@0 1116 paint.drawRect(nonCacheRect);
Chris@0 1117 }
Chris@0 1118
Chris@0 1119 paint.setPen(Qt::black);
Chris@0 1120 paint.setBrush(Qt::NoBrush);
Chris@0 1121
Chris@0 1122 for (LayerList::iterator i = nonScrollables.begin(); i != nonScrollables.end(); ++i) {
Chris@0 1123 (*i)->paint(paint, nonCacheRect);
Chris@0 1124 }
Chris@0 1125
Chris@9 1126 paint.end();
Chris@8 1127
Chris@9 1128 paint.begin(this);
Chris@9 1129 if (e) paint.setClipRect(e->rect());
Chris@9 1130 if (!m_selectionCached) {
Chris@9 1131 drawSelections(paint);
Chris@8 1132 }
Chris@0 1133 paint.end();
Chris@0 1134
Chris@0 1135 if (m_followPlay != PlaybackScrollContinuous) {
Chris@0 1136
Chris@0 1137 paint.begin(this);
Chris@0 1138
Chris@0 1139 if (long(m_playPointerFrame) > getStartFrame() &&
Chris@0 1140 m_playPointerFrame < getEndFrame()) {
Chris@0 1141
Chris@15 1142 int playx = getXForFrame(m_playPointerFrame);
Chris@0 1143
Chris@0 1144 paint.setPen(Qt::black);
Chris@0 1145 paint.drawLine(playx - 1, 0, playx - 1, height() - 1);
Chris@0 1146 paint.drawLine(playx + 1, 0, playx + 1, height() - 1);
Chris@0 1147 paint.drawPoint(playx, 0);
Chris@0 1148 paint.drawPoint(playx, height() - 1);
Chris@0 1149 paint.setPen(Qt::white);
Chris@0 1150 paint.drawLine(playx, 1, playx, height() - 2);
Chris@0 1151 }
Chris@0 1152
Chris@0 1153 paint.end();
Chris@0 1154 }
Chris@0 1155
Chris@0 1156 QFrame::paintEvent(e);
Chris@0 1157 }
Chris@0 1158
Chris@9 1159 void
Chris@9 1160 View::drawSelections(QPainter &paint)
Chris@9 1161 {
Chris@24 1162 MultiSelection::SelectionList selections;
Chris@9 1163
Chris@9 1164 if (m_manager) {
Chris@9 1165 selections = m_manager->getSelections();
Chris@9 1166 if (m_manager->haveInProgressSelection()) {
Chris@9 1167 bool exclusive;
Chris@9 1168 Selection inProgressSelection =
Chris@9 1169 m_manager->getInProgressSelection(exclusive);
Chris@9 1170 if (exclusive) selections.clear();
Chris@9 1171 selections.insert(inProgressSelection);
Chris@9 1172 }
Chris@9 1173 }
Chris@9 1174
Chris@9 1175 paint.save();
Chris@9 1176 paint.setPen(QColor(150, 150, 255));
Chris@9 1177 paint.setBrush(QColor(150, 150, 255, 80));
Chris@9 1178
Chris@10 1179 int sampleRate = getModelsSampleRate();
Chris@10 1180
Chris@10 1181 const QFontMetrics &metrics = paint.fontMetrics();
Chris@10 1182
Chris@24 1183 for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@9 1184 i != selections.end(); ++i) {
Chris@9 1185
Chris@15 1186 int p0 = getXForFrame(i->getStartFrame());
Chris@15 1187 int p1 = getXForFrame(i->getEndFrame());
Chris@9 1188
Chris@10 1189 if (p1 < 0 || p0 > width()) continue;
Chris@9 1190
Chris@9 1191 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@9 1192 std::cerr << "View::drawSelections: " << p0 << ",-1 [" << (p1-p0) << "x" << (height()+1) << "]" << std::endl;
Chris@9 1193 #endif
Chris@9 1194
Chris@9 1195 paint.drawRect(p0, -1, p1 - p0, height() + 1);
Chris@10 1196
Chris@10 1197 if (sampleRate && shouldLabelSelections()) {
Chris@10 1198
Chris@10 1199 QString startText = QString("%1 / %2")
Chris@10 1200 .arg(QString::fromStdString
Chris@10 1201 (RealTime::frame2RealTime
Chris@10 1202 (i->getStartFrame(), sampleRate).toText(true)))
Chris@10 1203 .arg(i->getStartFrame());
Chris@10 1204
Chris@10 1205 QString endText = QString(" %1 / %2")
Chris@10 1206 .arg(QString::fromStdString
Chris@10 1207 (RealTime::frame2RealTime
Chris@10 1208 (i->getEndFrame(), sampleRate).toText(true)))
Chris@10 1209 .arg(i->getEndFrame());
Chris@10 1210
Chris@10 1211 QString durationText = QString("(%1 / %2) ")
Chris@10 1212 .arg(QString::fromStdString
Chris@10 1213 (RealTime::frame2RealTime
Chris@10 1214 (i->getEndFrame() - i->getStartFrame(), sampleRate)
Chris@10 1215 .toText(true)))
Chris@10 1216 .arg(i->getEndFrame() - i->getStartFrame());
Chris@10 1217
Chris@10 1218 int sw = metrics.width(startText),
Chris@10 1219 ew = metrics.width(endText),
Chris@10 1220 dw = metrics.width(durationText);
Chris@10 1221
Chris@10 1222 int sy = metrics.ascent() + metrics.height() + 4;
Chris@10 1223 int ey = sy;
Chris@10 1224 int dy = sy + metrics.height();
Chris@10 1225
Chris@10 1226 int sx = p0 + 2;
Chris@10 1227 int ex = sx;
Chris@10 1228 int dx = sx;
Chris@10 1229
Chris@10 1230 if (sw + ew > (p1 - p0)) {
Chris@10 1231 ey += metrics.height();
Chris@10 1232 dy += metrics.height();
Chris@10 1233 }
Chris@10 1234
Chris@10 1235 if (ew < (p1 - p0)) {
Chris@10 1236 ex = p1 - 2 - ew;
Chris@10 1237 }
Chris@10 1238
Chris@10 1239 if (dw < (p1 - p0)) {
Chris@10 1240 dx = p1 - 2 - dw;
Chris@10 1241 }
Chris@10 1242
Chris@10 1243 paint.drawText(sx, sy, startText);
Chris@10 1244 paint.drawText(ex, ey, endText);
Chris@10 1245 paint.drawText(dx, dy, durationText);
Chris@10 1246 }
Chris@9 1247 }
Chris@9 1248
Chris@9 1249 paint.restore();
Chris@9 1250 }
Chris@9 1251
Chris@3 1252 QString
Chris@3 1253 View::toXmlString(QString indent, QString extraAttributes) const
Chris@3 1254 {
Chris@3 1255 QString s;
Chris@3 1256
Chris@3 1257 s += indent;
Chris@3 1258
Chris@3 1259 s += QString("<view "
Chris@3 1260 "centre=\"%1\" "
Chris@3 1261 "zoom=\"%2\" "
Chris@3 1262 "followPan=\"%3\" "
Chris@3 1263 "followZoom=\"%4\" "
Chris@3 1264 "tracking=\"%5\" "
Chris@3 1265 "light=\"%6\" %7>\n")
Chris@3 1266 .arg(m_centreFrame)
Chris@3 1267 .arg(m_zoomLevel)
Chris@3 1268 .arg(m_followPan)
Chris@3 1269 .arg(m_followZoom)
Chris@3 1270 .arg(m_followPlay == PlaybackScrollContinuous ? "scroll" :
Chris@3 1271 m_followPlay == PlaybackScrollPage ? "page" : "ignore")
Chris@3 1272 .arg(m_lightBackground)
Chris@3 1273 .arg(extraAttributes);
Chris@3 1274
Chris@3 1275 for (size_t i = 0; i < m_layers.size(); ++i) {
Chris@3 1276 s += m_layers[i]->toXmlString(indent + " ");
Chris@3 1277 }
Chris@3 1278
Chris@4 1279 s += indent + "</view>\n";
Chris@3 1280
Chris@3 1281 return s;
Chris@3 1282 }
Chris@3 1283
Chris@29 1284 ViewPropertyContainer::ViewPropertyContainer(View *v) :
Chris@29 1285 m_v(v)
Chris@29 1286 {
Chris@29 1287 connect(m_v, SIGNAL(propertyChanged(PropertyName)),
Chris@29 1288 this, SIGNAL(propertyChanged(PropertyName)));
Chris@29 1289 }
Chris@3 1290
Chris@0 1291 #ifdef INCLUDE_MOCFILES
Chris@0 1292 #include "View.moc.cpp"
Chris@0 1293 #endif
Chris@0 1294