annotate view/View.cpp @ 131:eaae73b6bd28

* Suspend/resume fft data server write activity while reading from a server to repaint the spectrogram display. Makes a significant improvement to the otherwise dreadful responsiveness of spectrogram display.
author Chris Cannam
date Thu, 03 Aug 2006 12:42:15 +0000
parents 33929e0c3c6b
children 5d3a483856ff
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@127 21
Chris@127 22 #include "layer/TimeRulerLayer.h" //!!! damn, shouldn't be including that here
Chris@128 23 #include "data/model/PowerOfSqrtTwoZoomConstraint.h" //!!! likewise
Chris@127 24
Chris@127 25 #include <QPainter>
Chris@127 26 #include <QPaintEvent>
Chris@127 27 #include <QRect>
Chris@127 28 #include <QApplication>
Chris@127 29
Chris@127 30 #include <iostream>
Chris@127 31 #include <cassert>
Chris@127 32 #include <math.h>
Chris@127 33
Chris@127 34 //#define DEBUG_VIEW_WIDGET_PAINT 1
Chris@127 35
Chris@127 36 using std::cerr;
Chris@127 37 using std::endl;
Chris@127 38
Chris@127 39 View::View(QWidget *w, bool showProgress) :
Chris@127 40 QFrame(w),
Chris@127 41 m_centreFrame(0),
Chris@127 42 m_zoomLevel(1024),
Chris@127 43 m_followPan(true),
Chris@127 44 m_followZoom(true),
Chris@127 45 m_followPlay(PlaybackScrollPage),
Chris@127 46 m_lightBackground(true),
Chris@127 47 m_showProgress(showProgress),
Chris@127 48 m_cache(0),
Chris@127 49 m_cacheCentreFrame(0),
Chris@127 50 m_cacheZoomLevel(1024),
Chris@127 51 m_selectionCached(false),
Chris@127 52 m_deleting(false),
Chris@127 53 m_haveSelectedLayer(false),
Chris@127 54 m_manager(0),
Chris@127 55 m_propertyContainer(new ViewPropertyContainer(this))
Chris@127 56 {
Chris@127 57 // QWidget::setAttribute(Qt::WA_PaintOnScreen);
Chris@127 58 }
Chris@127 59
Chris@127 60 View::~View()
Chris@127 61 {
Chris@127 62 // std::cerr << "View::~View(" << this << ")" << std::endl;
Chris@127 63
Chris@127 64 m_deleting = true;
Chris@127 65 delete m_propertyContainer;
Chris@127 66 }
Chris@127 67
Chris@127 68 PropertyContainer::PropertyList
Chris@127 69 View::getProperties() const
Chris@127 70 {
Chris@127 71 PropertyContainer::PropertyList list;
Chris@127 72 list.push_back("Global Scroll");
Chris@127 73 list.push_back("Global Zoom");
Chris@127 74 list.push_back("Follow Playback");
Chris@127 75 return list;
Chris@127 76 }
Chris@127 77
Chris@127 78 QString
Chris@127 79 View::getPropertyLabel(const PropertyName &pn) const
Chris@127 80 {
Chris@127 81 if (pn == "Global Scroll") return tr("Global Scroll");
Chris@127 82 if (pn == "Global Zoom") return tr("Global Zoom");
Chris@127 83 if (pn == "Follow Playback") return tr("Follow Playback");
Chris@127 84 return "";
Chris@127 85 }
Chris@127 86
Chris@127 87 PropertyContainer::PropertyType
Chris@127 88 View::getPropertyType(const PropertyContainer::PropertyName &name) const
Chris@127 89 {
Chris@127 90 if (name == "Global Scroll") return PropertyContainer::ToggleProperty;
Chris@127 91 if (name == "Global Zoom") return PropertyContainer::ToggleProperty;
Chris@127 92 if (name == "Follow Playback") return PropertyContainer::ValueProperty;
Chris@127 93 return PropertyContainer::InvalidProperty;
Chris@127 94 }
Chris@127 95
Chris@127 96 int
Chris@127 97 View::getPropertyRangeAndValue(const PropertyContainer::PropertyName &name,
Chris@127 98 int *min, int *max) const
Chris@127 99 {
Chris@127 100 if (name == "Global Scroll") return m_followPan;
Chris@127 101 if (name == "Global Zoom") return m_followZoom;
Chris@127 102 if (name == "Follow Playback") {
Chris@127 103 if (min) *min = 0;
Chris@127 104 if (max) *max = 2;
Chris@127 105 return int(m_followPlay);
Chris@127 106 }
Chris@127 107 if (min) *min = 0;
Chris@127 108 if (max) *max = 0;
Chris@127 109 return 0;
Chris@127 110 }
Chris@127 111
Chris@127 112 QString
Chris@127 113 View::getPropertyValueLabel(const PropertyContainer::PropertyName &name,
Chris@127 114 int value) const
Chris@127 115 {
Chris@127 116 if (name == "Follow Playback") {
Chris@127 117 switch (value) {
Chris@127 118 default:
Chris@127 119 case 0: return tr("Scroll");
Chris@127 120 case 1: return tr("Page");
Chris@127 121 case 2: return tr("Off");
Chris@127 122 }
Chris@127 123 }
Chris@127 124 return tr("<unknown>");
Chris@127 125 }
Chris@127 126
Chris@127 127 void
Chris@127 128 View::setProperty(const PropertyContainer::PropertyName &name, int value)
Chris@127 129 {
Chris@127 130 if (name == "Global Scroll") {
Chris@127 131 setFollowGlobalPan(value != 0);
Chris@127 132 } else if (name == "Global Zoom") {
Chris@127 133 setFollowGlobalZoom(value != 0);
Chris@127 134 } else if (name == "Follow Playback") {
Chris@127 135 switch (value) {
Chris@127 136 default:
Chris@127 137 case 0: setPlaybackFollow(PlaybackScrollContinuous); break;
Chris@127 138 case 1: setPlaybackFollow(PlaybackScrollPage); break;
Chris@127 139 case 2: setPlaybackFollow(PlaybackIgnore); break;
Chris@127 140 }
Chris@127 141 }
Chris@127 142 }
Chris@127 143
Chris@127 144 size_t
Chris@127 145 View::getPropertyContainerCount() const
Chris@127 146 {
Chris@127 147 return m_layers.size() + 1; // the 1 is for me
Chris@127 148 }
Chris@127 149
Chris@127 150 const PropertyContainer *
Chris@127 151 View::getPropertyContainer(size_t i) const
Chris@127 152 {
Chris@127 153 return (const PropertyContainer *)(((View *)this)->
Chris@127 154 getPropertyContainer(i));
Chris@127 155 }
Chris@127 156
Chris@127 157 PropertyContainer *
Chris@127 158 View::getPropertyContainer(size_t i)
Chris@127 159 {
Chris@127 160 if (i == 0) return m_propertyContainer;
Chris@127 161 return m_layers[i-1];
Chris@127 162 }
Chris@127 163
Chris@127 164 bool
Chris@127 165 View::getValueExtents(QString unit, float &min, float &max, bool &log) const
Chris@127 166 {
Chris@127 167 bool have = false;
Chris@127 168
Chris@127 169 for (LayerList::const_iterator i = m_layers.begin();
Chris@127 170 i != m_layers.end(); ++i) {
Chris@127 171
Chris@127 172 QString layerUnit;
Chris@127 173 float layerMin = 0.0, layerMax = 0.0;
Chris@127 174 float displayMin = 0.0, displayMax = 0.0;
Chris@127 175 bool layerLog = false;
Chris@127 176
Chris@127 177 if ((*i)->getValueExtents(layerMin, layerMax, layerLog, layerUnit) &&
Chris@127 178 layerUnit.toLower() == unit.toLower()) {
Chris@127 179
Chris@127 180 if ((*i)->getDisplayExtents(displayMin, displayMax)) {
Chris@127 181
Chris@127 182 min = displayMin;
Chris@127 183 max = displayMax;
Chris@127 184 log = layerLog;
Chris@127 185 have = true;
Chris@127 186 break;
Chris@127 187
Chris@127 188 } else {
Chris@127 189
Chris@127 190 if (!have || layerMin < min) min = layerMin;
Chris@127 191 if (!have || layerMax > max) max = layerMax;
Chris@127 192 if (layerLog) log = true;
Chris@127 193 have = true;
Chris@127 194 }
Chris@127 195 }
Chris@127 196 }
Chris@127 197
Chris@127 198 return have;
Chris@127 199 }
Chris@127 200
Chris@127 201 int
Chris@127 202 View::getTextLabelHeight(const Layer *layer, QPainter &paint) const
Chris@127 203 {
Chris@127 204 std::map<int, Layer *> sortedLayers;
Chris@127 205
Chris@127 206 for (LayerList::const_iterator i = m_layers.begin();
Chris@127 207 i != m_layers.end(); ++i) {
Chris@127 208 if ((*i)->needsTextLabelHeight()) {
Chris@127 209 sortedLayers[getObjectExportId(*i)] = *i;
Chris@127 210 }
Chris@127 211 }
Chris@127 212
Chris@127 213 int y = 15 + paint.fontMetrics().ascent();
Chris@127 214
Chris@127 215 for (std::map<int, Layer *>::const_iterator i = sortedLayers.begin();
Chris@127 216 i != sortedLayers.end(); ++i) {
Chris@127 217 if (i->second == layer) return y;
Chris@127 218 y += paint.fontMetrics().height();
Chris@127 219 }
Chris@127 220
Chris@127 221 return y;
Chris@127 222 }
Chris@127 223
Chris@127 224 void
Chris@127 225 View::propertyContainerSelected(View *client, PropertyContainer *pc)
Chris@127 226 {
Chris@127 227 if (client != this) return;
Chris@127 228
Chris@127 229 if (pc == m_propertyContainer) {
Chris@127 230 if (m_haveSelectedLayer) {
Chris@127 231 m_haveSelectedLayer = false;
Chris@127 232 update();
Chris@127 233 }
Chris@127 234 return;
Chris@127 235 }
Chris@127 236
Chris@127 237 delete m_cache;
Chris@127 238 m_cache = 0;
Chris@127 239
Chris@127 240 Layer *selectedLayer = 0;
Chris@127 241
Chris@127 242 for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@127 243 if (*i == pc) {
Chris@127 244 selectedLayer = *i;
Chris@127 245 m_layers.erase(i);
Chris@127 246 break;
Chris@127 247 }
Chris@127 248 }
Chris@127 249
Chris@127 250 if (selectedLayer) {
Chris@127 251 m_haveSelectedLayer = true;
Chris@127 252 m_layers.push_back(selectedLayer);
Chris@127 253 update();
Chris@127 254 } else {
Chris@127 255 m_haveSelectedLayer = false;
Chris@127 256 }
Chris@127 257 }
Chris@127 258
Chris@127 259 void
Chris@127 260 View::toolModeChanged()
Chris@127 261 {
Chris@127 262 // std::cerr << "View::toolModeChanged(" << m_manager->getToolMode() << ")" << std::endl;
Chris@127 263 }
Chris@127 264
Chris@127 265 long
Chris@127 266 View::getStartFrame() const
Chris@127 267 {
Chris@127 268 size_t w2 = (width() / 2) * m_zoomLevel;
Chris@127 269 size_t frame = m_centreFrame;
Chris@127 270 if (frame >= w2) {
Chris@127 271 frame -= w2;
Chris@127 272 return (frame / m_zoomLevel * m_zoomLevel);
Chris@127 273 } else {
Chris@127 274 frame = w2 - frame;
Chris@127 275 frame = frame / m_zoomLevel * m_zoomLevel;
Chris@127 276 return -(long)frame - m_zoomLevel;
Chris@127 277 }
Chris@127 278 }
Chris@127 279
Chris@127 280 size_t
Chris@127 281 View::getEndFrame() const
Chris@127 282 {
Chris@127 283 return getFrameForX(width()) - 1;
Chris@127 284 }
Chris@127 285
Chris@127 286 void
Chris@127 287 View::setStartFrame(long f)
Chris@127 288 {
Chris@127 289 setCentreFrame(f + m_zoomLevel * (width() / 2));
Chris@127 290 }
Chris@127 291
Chris@127 292 bool
Chris@127 293 View::setCentreFrame(size_t f, bool e)
Chris@127 294 {
Chris@127 295 bool changeVisible = false;
Chris@127 296
Chris@127 297 if (m_centreFrame != f) {
Chris@127 298
Chris@127 299 int formerPixel = m_centreFrame / m_zoomLevel;
Chris@127 300
Chris@127 301 m_centreFrame = f;
Chris@127 302
Chris@127 303 int newPixel = m_centreFrame / m_zoomLevel;
Chris@127 304
Chris@127 305 if (newPixel != formerPixel) {
Chris@127 306
Chris@127 307 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 308 std::cout << "View(" << this << ")::setCentreFrame: newPixel " << newPixel << ", formerPixel " << formerPixel << std::endl;
Chris@127 309 #endif
Chris@127 310 update();
Chris@127 311
Chris@127 312 changeVisible = true;
Chris@127 313 }
Chris@127 314
Chris@127 315 if (e) emit centreFrameChanged(this, f, m_followPan);
Chris@127 316 }
Chris@127 317
Chris@127 318 return changeVisible;
Chris@127 319 }
Chris@127 320
Chris@127 321 int
Chris@127 322 View::getXForFrame(long frame) const
Chris@127 323 {
Chris@127 324 return (frame - getStartFrame()) / m_zoomLevel;
Chris@127 325 }
Chris@127 326
Chris@127 327 long
Chris@127 328 View::getFrameForX(int x) const
Chris@127 329 {
Chris@127 330 return (long(x) * long(m_zoomLevel)) + getStartFrame();
Chris@127 331 }
Chris@127 332
Chris@127 333 float
Chris@127 334 View::getYForFrequency(float frequency,
Chris@127 335 float minf,
Chris@127 336 float maxf,
Chris@127 337 bool logarithmic) const
Chris@127 338 {
Chris@127 339 int h = height();
Chris@127 340
Chris@127 341 if (logarithmic) {
Chris@127 342
Chris@127 343 static float lastminf = 0.0, lastmaxf = 0.0;
Chris@127 344 static float logminf = 0.0, logmaxf = 0.0;
Chris@127 345
Chris@127 346 if (lastminf != minf) {
Chris@127 347 lastminf = (minf == 0.0 ? 1.0 : minf);
Chris@127 348 logminf = log10f(minf);
Chris@127 349 }
Chris@127 350 if (lastmaxf != maxf) {
Chris@127 351 lastmaxf = (maxf < lastminf ? lastminf : maxf);
Chris@127 352 logmaxf = log10f(maxf);
Chris@127 353 }
Chris@127 354
Chris@127 355 if (logminf == logmaxf) return 0;
Chris@127 356 return h - (h * (log10f(frequency) - logminf)) / (logmaxf - logminf);
Chris@127 357
Chris@127 358 } else {
Chris@127 359
Chris@127 360 if (minf == maxf) return 0;
Chris@127 361 return h - (h * (frequency - minf)) / (maxf - minf);
Chris@127 362 }
Chris@127 363 }
Chris@127 364
Chris@127 365 float
Chris@127 366 View::getFrequencyForY(int y,
Chris@127 367 float minf,
Chris@127 368 float maxf,
Chris@127 369 bool logarithmic) const
Chris@127 370 {
Chris@127 371 int h = height();
Chris@127 372
Chris@127 373 if (logarithmic) {
Chris@127 374
Chris@127 375 static float lastminf = 0.0, lastmaxf = 0.0;
Chris@127 376 static float logminf = 0.0, logmaxf = 0.0;
Chris@127 377
Chris@127 378 if (lastminf != minf) {
Chris@127 379 lastminf = (minf == 0.0 ? 1.0 : minf);
Chris@127 380 logminf = log10f(minf);
Chris@127 381 }
Chris@127 382 if (lastmaxf != maxf) {
Chris@127 383 lastmaxf = (maxf < lastminf ? lastminf : maxf);
Chris@127 384 logmaxf = log10f(maxf);
Chris@127 385 }
Chris@127 386
Chris@127 387 if (logminf == logmaxf) return 0;
Chris@127 388 return pow(10.f, logminf + ((logmaxf - logminf) * (h - y)) / h);
Chris@127 389
Chris@127 390 } else {
Chris@127 391
Chris@127 392 if (minf == maxf) return 0;
Chris@127 393 return minf + ((h - y) * (maxf - minf)) / h;
Chris@127 394 }
Chris@127 395 }
Chris@127 396
Chris@127 397 int
Chris@127 398 View::getZoomLevel() const
Chris@127 399 {
Chris@127 400 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 401 std::cout << "zoom level: " << m_zoomLevel << std::endl;
Chris@127 402 #endif
Chris@127 403 return m_zoomLevel;
Chris@127 404 }
Chris@127 405
Chris@127 406 void
Chris@127 407 View::setZoomLevel(size_t z)
Chris@127 408 {
Chris@127 409 if (m_zoomLevel != int(z)) {
Chris@127 410 m_zoomLevel = z;
Chris@127 411 emit zoomLevelChanged(this, z, m_followZoom);
Chris@127 412 update();
Chris@127 413 }
Chris@127 414 }
Chris@127 415
Chris@127 416 View::LayerProgressBar::LayerProgressBar(QWidget *parent) :
Chris@127 417 QProgressBar(parent)
Chris@127 418 {
Chris@127 419 QFont f(font());
Chris@127 420 f.setPointSize(f.pointSize() * 8 / 10);
Chris@127 421 setFont(f);
Chris@127 422 }
Chris@127 423
Chris@127 424 void
Chris@127 425 View::addLayer(Layer *layer)
Chris@127 426 {
Chris@127 427 delete m_cache;
Chris@127 428 m_cache = 0;
Chris@127 429
Chris@127 430 m_layers.push_back(layer);
Chris@127 431
Chris@127 432 m_progressBars[layer] = new LayerProgressBar(this);
Chris@127 433 m_progressBars[layer]->setMinimum(0);
Chris@127 434 m_progressBars[layer]->setMaximum(100);
Chris@127 435 m_progressBars[layer]->setMinimumWidth(80);
Chris@127 436 m_progressBars[layer]->hide();
Chris@127 437
Chris@127 438 connect(layer, SIGNAL(layerParametersChanged()),
Chris@127 439 this, SLOT(layerParametersChanged()));
Chris@127 440 connect(layer, SIGNAL(layerNameChanged()),
Chris@127 441 this, SLOT(layerNameChanged()));
Chris@127 442 connect(layer, SIGNAL(modelChanged()),
Chris@127 443 this, SLOT(modelChanged()));
Chris@127 444 connect(layer, SIGNAL(modelCompletionChanged()),
Chris@127 445 this, SLOT(modelCompletionChanged()));
Chris@127 446 connect(layer, SIGNAL(modelChanged(size_t, size_t)),
Chris@127 447 this, SLOT(modelChanged(size_t, size_t)));
Chris@127 448 connect(layer, SIGNAL(modelReplaced()),
Chris@127 449 this, SLOT(modelReplaced()));
Chris@127 450
Chris@127 451 update();
Chris@127 452
Chris@127 453 emit propertyContainerAdded(layer);
Chris@127 454 }
Chris@127 455
Chris@127 456 void
Chris@127 457 View::removeLayer(Layer *layer)
Chris@127 458 {
Chris@127 459 if (m_deleting) {
Chris@127 460 return;
Chris@127 461 }
Chris@127 462
Chris@127 463 delete m_cache;
Chris@127 464 m_cache = 0;
Chris@127 465
Chris@127 466 for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@127 467 if (*i == layer) {
Chris@127 468 m_layers.erase(i);
Chris@127 469 if (m_progressBars.find(layer) != m_progressBars.end()) {
Chris@127 470 delete m_progressBars[layer];
Chris@127 471 m_progressBars.erase(layer);
Chris@127 472 }
Chris@127 473 break;
Chris@127 474 }
Chris@127 475 }
Chris@127 476
Chris@127 477 update();
Chris@127 478
Chris@127 479 emit propertyContainerRemoved(layer);
Chris@127 480 }
Chris@127 481
Chris@127 482 Layer *
Chris@127 483 View::getSelectedLayer()
Chris@127 484 {
Chris@127 485 if (m_haveSelectedLayer && !m_layers.empty()) {
Chris@127 486 return getLayer(getLayerCount() - 1);
Chris@127 487 } else {
Chris@127 488 return 0;
Chris@127 489 }
Chris@127 490 }
Chris@127 491
Chris@127 492 const Layer *
Chris@127 493 View::getSelectedLayer() const
Chris@127 494 {
Chris@127 495 return const_cast<const Layer *>(const_cast<View *>(this)->getSelectedLayer());
Chris@127 496 }
Chris@127 497
Chris@127 498 void
Chris@127 499 View::setViewManager(ViewManager *manager)
Chris@127 500 {
Chris@127 501 if (m_manager) {
Chris@127 502 m_manager->disconnect(this, SLOT(viewManagerCentreFrameChanged(void *, unsigned long, bool)));
Chris@127 503 m_manager->disconnect(this, SLOT(viewManagerZoomLevelChanged(void *, unsigned long, bool)));
Chris@127 504 disconnect(m_manager, SIGNAL(centreFrameChanged(void *, unsigned long, bool)));
Chris@127 505 disconnect(m_manager, SIGNAL(zoomLevelChanged(void *, unsigned long, bool)));
Chris@127 506 disconnect(m_manager, SIGNAL(toolModeChanged()));
Chris@127 507 disconnect(m_manager, SIGNAL(selectionChanged()));
Chris@127 508 disconnect(m_manager, SIGNAL(inProgressSelectionChanged()));
Chris@127 509 }
Chris@127 510
Chris@127 511 m_manager = manager;
Chris@127 512 if (m_followPan) setCentreFrame(m_manager->getGlobalCentreFrame(), false);
Chris@127 513 if (m_followZoom) setZoomLevel(m_manager->getGlobalZoom());
Chris@127 514
Chris@127 515 connect(m_manager, SIGNAL(centreFrameChanged(void *, unsigned long, bool)),
Chris@127 516 this, SLOT(viewManagerCentreFrameChanged(void *, unsigned long, bool)));
Chris@127 517 connect(m_manager, SIGNAL(playbackFrameChanged(unsigned long)),
Chris@127 518 this, SLOT(viewManagerPlaybackFrameChanged(unsigned long)));
Chris@127 519 connect(m_manager, SIGNAL(zoomLevelChanged(void *, unsigned long, bool)),
Chris@127 520 this, SLOT(viewManagerZoomLevelChanged(void *, unsigned long, bool)));
Chris@127 521 connect(m_manager, SIGNAL(toolModeChanged()),
Chris@127 522 this, SLOT(toolModeChanged()));
Chris@127 523 connect(m_manager, SIGNAL(selectionChanged()),
Chris@127 524 this, SLOT(selectionChanged()));
Chris@127 525 connect(m_manager, SIGNAL(inProgressSelectionChanged()),
Chris@127 526 this, SLOT(selectionChanged()));
Chris@127 527 connect(m_manager, SIGNAL(overlayModeChanged()),
Chris@127 528 this, SLOT(update()));
Chris@127 529
Chris@127 530 connect(this, SIGNAL(centreFrameChanged(void *, unsigned long, bool)),
Chris@127 531 m_manager, SIGNAL(centreFrameChanged(void *, unsigned long, bool)));
Chris@127 532 connect(this, SIGNAL(zoomLevelChanged(void *, unsigned long, bool)),
Chris@127 533 m_manager, SIGNAL(zoomLevelChanged(void *, unsigned long, bool)));
Chris@127 534
Chris@127 535 toolModeChanged();
Chris@127 536 }
Chris@127 537
Chris@127 538 void
Chris@127 539 View::setFollowGlobalPan(bool f)
Chris@127 540 {
Chris@127 541 m_followPan = f;
Chris@127 542 emit propertyContainerPropertyChanged(m_propertyContainer);
Chris@127 543 }
Chris@127 544
Chris@127 545 void
Chris@127 546 View::setFollowGlobalZoom(bool f)
Chris@127 547 {
Chris@127 548 m_followZoom = f;
Chris@127 549 emit propertyContainerPropertyChanged(m_propertyContainer);
Chris@127 550 }
Chris@127 551
Chris@127 552 void
Chris@127 553 View::drawVisibleText(QPainter &paint, int x, int y, QString text, TextStyle style)
Chris@127 554 {
Chris@127 555 if (style == OutlinedText) {
Chris@127 556
Chris@127 557 QColor origPenColour = paint.pen().color();
Chris@127 558 QColor penColour = origPenColour;
Chris@127 559 QColor surroundColour = Qt::white; //palette().background().color();
Chris@127 560
Chris@127 561 if (!hasLightBackground()) {
Chris@127 562 int h, s, v;
Chris@127 563 penColour.getHsv(&h, &s, &v);
Chris@127 564 penColour = QColor::fromHsv(h, s, 255 - v);
Chris@127 565 surroundColour = Qt::black;
Chris@127 566 }
Chris@127 567
Chris@127 568 paint.setPen(surroundColour);
Chris@127 569
Chris@127 570 for (int dx = -1; dx <= 1; ++dx) {
Chris@127 571 for (int dy = -1; dy <= 1; ++dy) {
Chris@127 572 if (!(dx || dy)) continue;
Chris@127 573 paint.drawText(x + dx, y + dy, text);
Chris@127 574 }
Chris@127 575 }
Chris@127 576
Chris@127 577 paint.setPen(penColour);
Chris@127 578
Chris@127 579 paint.drawText(x, y, text);
Chris@127 580
Chris@127 581 paint.setPen(origPenColour);
Chris@127 582
Chris@127 583 } else {
Chris@127 584
Chris@127 585 std::cerr << "ERROR: View::drawVisibleText: Boxed style not yet implemented!" << std::endl;
Chris@127 586 }
Chris@127 587 }
Chris@127 588
Chris@127 589 void
Chris@127 590 View::setPlaybackFollow(PlaybackFollowMode m)
Chris@127 591 {
Chris@127 592 m_followPlay = m;
Chris@127 593 emit propertyContainerPropertyChanged(m_propertyContainer);
Chris@127 594 }
Chris@127 595
Chris@127 596 void
Chris@127 597 View::modelChanged()
Chris@127 598 {
Chris@127 599 QObject *obj = sender();
Chris@127 600
Chris@127 601 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 602 std::cerr << "View(" << this << ")::modelChanged()" << std::endl;
Chris@127 603 #endif
Chris@127 604
Chris@127 605 // If the model that has changed is not used by any of the cached
Chris@127 606 // layers, we won't need to recreate the cache
Chris@127 607
Chris@127 608 bool recreate = false;
Chris@127 609
Chris@127 610 bool discard;
Chris@127 611 LayerList scrollables = getScrollableBackLayers(false, discard);
Chris@127 612 for (LayerList::const_iterator i = scrollables.begin();
Chris@127 613 i != scrollables.end(); ++i) {
Chris@127 614 if (*i == obj || (*i)->getModel() == obj) {
Chris@127 615 recreate = true;
Chris@127 616 break;
Chris@127 617 }
Chris@127 618 }
Chris@127 619
Chris@127 620 if (recreate) {
Chris@127 621 delete m_cache;
Chris@127 622 m_cache = 0;
Chris@127 623 }
Chris@127 624
Chris@127 625 checkProgress(obj);
Chris@127 626
Chris@127 627 update();
Chris@127 628 }
Chris@127 629
Chris@127 630 void
Chris@127 631 View::modelChanged(size_t startFrame, size_t endFrame)
Chris@127 632 {
Chris@127 633 QObject *obj = sender();
Chris@127 634
Chris@127 635 long myStartFrame = getStartFrame();
Chris@127 636 size_t myEndFrame = getEndFrame();
Chris@127 637
Chris@127 638 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 639 std::cerr << "View(" << this << ")::modelChanged(" << startFrame << "," << endFrame << ") [me " << myStartFrame << "," << myEndFrame << "]" << std::endl;
Chris@127 640 #endif
Chris@127 641
Chris@127 642 if (myStartFrame > 0 && endFrame < size_t(myStartFrame)) {
Chris@127 643 checkProgress(obj);
Chris@127 644 return;
Chris@127 645 }
Chris@127 646 if (startFrame > myEndFrame) {
Chris@127 647 checkProgress(obj);
Chris@127 648 return;
Chris@127 649 }
Chris@127 650
Chris@127 651 // If the model that has changed is not used by any of the cached
Chris@127 652 // layers, we won't need to recreate the cache
Chris@127 653
Chris@127 654 bool recreate = false;
Chris@127 655
Chris@127 656 bool discard;
Chris@127 657 LayerList scrollables = getScrollableBackLayers(false, discard);
Chris@127 658 for (LayerList::const_iterator i = scrollables.begin();
Chris@127 659 i != scrollables.end(); ++i) {
Chris@127 660 if (*i == obj || (*i)->getModel() == obj) {
Chris@127 661 recreate = true;
Chris@127 662 break;
Chris@127 663 }
Chris@127 664 }
Chris@127 665
Chris@127 666 if (recreate) {
Chris@127 667 delete m_cache;
Chris@127 668 m_cache = 0;
Chris@127 669 }
Chris@127 670
Chris@127 671 if (long(startFrame) < myStartFrame) startFrame = myStartFrame;
Chris@127 672 if (endFrame > myEndFrame) endFrame = myEndFrame;
Chris@127 673
Chris@127 674 int x0 = getXForFrame(startFrame);
Chris@127 675 int x1 = getXForFrame(endFrame + 1);
Chris@127 676 if (x1 < x0) x1 = x0;
Chris@127 677
Chris@127 678 checkProgress(obj);
Chris@127 679
Chris@127 680 update();
Chris@127 681 //!!! update(x0, 0, x1 - x0 + 1, height());
Chris@127 682 }
Chris@127 683
Chris@127 684 void
Chris@127 685 View::modelCompletionChanged()
Chris@127 686 {
Chris@127 687 QObject *obj = sender();
Chris@127 688 checkProgress(obj);
Chris@127 689 }
Chris@127 690
Chris@127 691 void
Chris@127 692 View::modelReplaced()
Chris@127 693 {
Chris@127 694 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 695 std::cerr << "View(" << this << ")::modelReplaced()" << std::endl;
Chris@127 696 #endif
Chris@127 697 delete m_cache;
Chris@127 698 m_cache = 0;
Chris@127 699
Chris@127 700 update();
Chris@127 701 }
Chris@127 702
Chris@127 703 void
Chris@127 704 View::layerParametersChanged()
Chris@127 705 {
Chris@127 706 Layer *layer = dynamic_cast<Layer *>(sender());
Chris@127 707
Chris@127 708 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 709 std::cerr << "View::layerParametersChanged()" << std::endl;
Chris@127 710 #endif
Chris@127 711
Chris@127 712 delete m_cache;
Chris@127 713 m_cache = 0;
Chris@127 714 update();
Chris@127 715
Chris@127 716 if (layer) {
Chris@127 717 emit propertyContainerPropertyChanged(layer);
Chris@127 718 }
Chris@127 719 }
Chris@127 720
Chris@127 721 void
Chris@127 722 View::layerNameChanged()
Chris@127 723 {
Chris@127 724 Layer *layer = dynamic_cast<Layer *>(sender());
Chris@127 725 if (layer) emit propertyContainerNameChanged(layer);
Chris@127 726 }
Chris@127 727
Chris@127 728 void
Chris@127 729 View::viewManagerCentreFrameChanged(void *p, unsigned long f, bool locked)
Chris@127 730 {
Chris@127 731 if (m_followPan && p != this && locked) {
Chris@127 732 if (m_manager && (sender() == m_manager)) {
Chris@127 733 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 734 std::cerr << this << ": manager frame changed " << f << " from " << p << std::endl;
Chris@127 735 #endif
Chris@127 736 setCentreFrame(f);
Chris@127 737 if (p == this) repaint();
Chris@127 738 }
Chris@127 739 }
Chris@127 740 }
Chris@127 741
Chris@127 742 void
Chris@127 743 View::viewManagerPlaybackFrameChanged(unsigned long f)
Chris@127 744 {
Chris@127 745 if (m_manager) {
Chris@127 746 if (sender() != m_manager) return;
Chris@127 747 }
Chris@127 748
Chris@127 749 if (m_playPointerFrame == f) return;
Chris@127 750 bool visible = (getXForFrame(m_playPointerFrame) != getXForFrame(f));
Chris@127 751 size_t oldPlayPointerFrame = m_playPointerFrame;
Chris@127 752 m_playPointerFrame = f;
Chris@127 753 if (!visible) return;
Chris@127 754
Chris@127 755 switch (m_followPlay) {
Chris@127 756
Chris@127 757 case PlaybackScrollContinuous:
Chris@127 758 if (QApplication::mouseButtons() == Qt::NoButton) {
Chris@127 759 setCentreFrame(f, false);
Chris@127 760 }
Chris@127 761 break;
Chris@127 762
Chris@127 763 case PlaybackScrollPage:
Chris@127 764 {
Chris@127 765 int xold = getXForFrame(oldPlayPointerFrame);
Chris@127 766 update(xold - 1, 0, 3, height());
Chris@127 767
Chris@127 768 long w = getEndFrame() - getStartFrame();
Chris@127 769 w -= w/5;
Chris@127 770 long sf = (f / w) * w - w/8;
Chris@127 771
Chris@127 772 if (m_manager &&
Chris@127 773 m_manager->isPlaying() &&
Chris@127 774 m_manager->getPlaySelectionMode()) {
Chris@127 775 MultiSelection::SelectionList selections = m_manager->getSelections();
Chris@127 776 if (!selections.empty()) {
Chris@127 777 size_t selectionStart = selections.begin()->getStartFrame();
Chris@127 778 if (sf < long(selectionStart) - w / 10) {
Chris@127 779 sf = long(selectionStart) - w / 10;
Chris@127 780 }
Chris@127 781 }
Chris@127 782 }
Chris@127 783
Chris@127 784 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 785 std::cerr << "PlaybackScrollPage: f = " << f << ", sf = " << sf << ", start frame "
Chris@127 786 << getStartFrame() << std::endl;
Chris@127 787 #endif
Chris@127 788
Chris@127 789 // We don't consider scrolling unless the pointer is outside
Chris@127 790 // the clearly visible range already
Chris@127 791
Chris@127 792 int xnew = getXForFrame(m_playPointerFrame);
Chris@127 793
Chris@127 794 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 795 std::cerr << "xnew = " << xnew << ", width = " << width() << std::endl;
Chris@127 796 #endif
Chris@127 797
Chris@127 798 if (xnew < width()/8 || xnew > (width()*7)/8) {
Chris@127 799 if (QApplication::mouseButtons() == Qt::NoButton) {
Chris@127 800 long offset = getFrameForX(width()/2) - getStartFrame();
Chris@127 801 long newCentre = sf + offset;
Chris@127 802 bool changed = setCentreFrame(newCentre, false);
Chris@127 803 if (changed) {
Chris@127 804 xold = getXForFrame(oldPlayPointerFrame);
Chris@127 805 update(xold - 1, 0, 3, height());
Chris@127 806 }
Chris@127 807 }
Chris@127 808 }
Chris@127 809
Chris@127 810 update(xnew - 1, 0, 3, height());
Chris@127 811
Chris@127 812 break;
Chris@127 813 }
Chris@127 814
Chris@127 815 case PlaybackIgnore:
Chris@127 816 if (long(f) >= getStartFrame() && f < getEndFrame()) {
Chris@127 817 update();
Chris@127 818 }
Chris@127 819 break;
Chris@127 820 }
Chris@127 821 }
Chris@127 822
Chris@127 823 void
Chris@127 824 View::viewManagerZoomLevelChanged(void *p, unsigned long z, bool locked)
Chris@127 825 {
Chris@127 826 if (m_followZoom && p != this && locked) {
Chris@127 827 if (m_manager && (sender() == m_manager)) {
Chris@127 828 setZoomLevel(z);
Chris@127 829 if (p == this) repaint();
Chris@127 830 }
Chris@127 831 }
Chris@127 832 }
Chris@127 833
Chris@127 834 void
Chris@127 835 View::selectionChanged()
Chris@127 836 {
Chris@127 837 if (m_selectionCached) {
Chris@127 838 delete m_cache;
Chris@127 839 m_cache = 0;
Chris@127 840 m_selectionCached = false;
Chris@127 841 }
Chris@127 842 update();
Chris@127 843 }
Chris@127 844
Chris@127 845 size_t
Chris@127 846 View::getModelsStartFrame() const
Chris@127 847 {
Chris@127 848 bool first = true;
Chris@127 849 size_t startFrame = 0;
Chris@127 850
Chris@127 851 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@127 852
Chris@127 853 if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@127 854
Chris@127 855 size_t thisStartFrame = (*i)->getModel()->getStartFrame();
Chris@127 856
Chris@127 857 if (first || thisStartFrame < startFrame) {
Chris@127 858 startFrame = thisStartFrame;
Chris@127 859 }
Chris@127 860 first = false;
Chris@127 861 }
Chris@127 862 }
Chris@127 863 return startFrame;
Chris@127 864 }
Chris@127 865
Chris@127 866 size_t
Chris@127 867 View::getModelsEndFrame() const
Chris@127 868 {
Chris@127 869 bool first = true;
Chris@127 870 size_t endFrame = 0;
Chris@127 871
Chris@127 872 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@127 873
Chris@127 874 if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@127 875
Chris@127 876 size_t thisEndFrame = (*i)->getModel()->getEndFrame();
Chris@127 877
Chris@127 878 if (first || thisEndFrame > endFrame) {
Chris@127 879 endFrame = thisEndFrame;
Chris@127 880 }
Chris@127 881 first = false;
Chris@127 882 }
Chris@127 883 }
Chris@127 884
Chris@127 885 if (first) return getModelsStartFrame();
Chris@127 886 return endFrame;
Chris@127 887 }
Chris@127 888
Chris@127 889 int
Chris@127 890 View::getModelsSampleRate() const
Chris@127 891 {
Chris@127 892 //!!! Just go for the first, for now. If we were supporting
Chris@127 893 // multiple samplerates, we'd probably want to do frame/time
Chris@127 894 // conversion in the model
Chris@127 895
Chris@127 896 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@127 897 if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@127 898 return (*i)->getModel()->getSampleRate();
Chris@127 899 }
Chris@127 900 }
Chris@127 901 return 0;
Chris@127 902 }
Chris@127 903
Chris@127 904 bool
Chris@127 905 View::areLayersScrollable() const
Chris@127 906 {
Chris@127 907 // True iff all views are scrollable
Chris@127 908 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@127 909 if (!(*i)->isLayerScrollable(this)) return false;
Chris@127 910 }
Chris@127 911 return true;
Chris@127 912 }
Chris@127 913
Chris@127 914 View::LayerList
Chris@127 915 View::getScrollableBackLayers(bool testChanged, bool &changed) const
Chris@127 916 {
Chris@127 917 changed = false;
Chris@127 918
Chris@127 919 // We want a list of all the scrollable layers that are behind the
Chris@127 920 // backmost non-scrollable layer.
Chris@127 921
Chris@127 922 LayerList scrollables;
Chris@127 923 bool metUnscrollable = false;
Chris@127 924
Chris@127 925 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@131 926 std::cerr << "View::getScrollableBackLayers: calling isLayerDormant on layer " << *i << std::endl;
Chris@131 927 std::cerr << "(name is " << (*i)->objectName().toStdString() << ")"
Chris@131 928 << std::endl;
Chris@131 929 std::cerr << "View::getScrollableBackLayers: I am " << this << std::endl;
Chris@127 930 if ((*i)->isLayerDormant(this)) continue;
Chris@127 931 if ((*i)->isLayerOpaque()) {
Chris@127 932 // You can't see anything behind an opaque layer!
Chris@127 933 scrollables.clear();
Chris@127 934 if (metUnscrollable) break;
Chris@127 935 }
Chris@127 936 if (!metUnscrollable && (*i)->isLayerScrollable(this)) {
Chris@127 937 scrollables.push_back(*i);
Chris@127 938 } else {
Chris@127 939 metUnscrollable = true;
Chris@127 940 }
Chris@127 941 }
Chris@127 942
Chris@127 943 if (testChanged && scrollables != m_lastScrollableBackLayers) {
Chris@127 944 m_lastScrollableBackLayers = scrollables;
Chris@127 945 changed = true;
Chris@127 946 }
Chris@127 947 return scrollables;
Chris@127 948 }
Chris@127 949
Chris@127 950 View::LayerList
Chris@127 951 View::getNonScrollableFrontLayers(bool testChanged, bool &changed) const
Chris@127 952 {
Chris@127 953 changed = false;
Chris@127 954 LayerList nonScrollables;
Chris@127 955
Chris@127 956 // Everything in front of the first non-scrollable from the back
Chris@127 957 // should also be considered non-scrollable
Chris@127 958
Chris@127 959 bool started = false;
Chris@127 960
Chris@127 961 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@127 962 if ((*i)->isLayerDormant(this)) continue;
Chris@127 963 if (!started && (*i)->isLayerScrollable(this)) {
Chris@127 964 continue;
Chris@127 965 }
Chris@127 966 started = true;
Chris@127 967 if ((*i)->isLayerOpaque()) {
Chris@127 968 // You can't see anything behind an opaque layer!
Chris@127 969 nonScrollables.clear();
Chris@127 970 }
Chris@127 971 nonScrollables.push_back(*i);
Chris@127 972 }
Chris@127 973
Chris@127 974 if (testChanged && nonScrollables != m_lastNonScrollableBackLayers) {
Chris@127 975 m_lastNonScrollableBackLayers = nonScrollables;
Chris@127 976 changed = true;
Chris@127 977 }
Chris@127 978
Chris@127 979 return nonScrollables;
Chris@127 980 }
Chris@127 981
Chris@127 982 size_t
Chris@127 983 View::getZoomConstraintBlockSize(size_t blockSize,
Chris@127 984 ZoomConstraint::RoundingDirection dir)
Chris@127 985 const
Chris@127 986 {
Chris@127 987 size_t candidate = blockSize;
Chris@127 988 bool haveCandidate = false;
Chris@127 989
Chris@127 990 PowerOfSqrtTwoZoomConstraint defaultZoomConstraint;
Chris@127 991
Chris@127 992 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@127 993
Chris@127 994 const ZoomConstraint *zoomConstraint = (*i)->getZoomConstraint();
Chris@127 995 if (!zoomConstraint) zoomConstraint = &defaultZoomConstraint;
Chris@127 996
Chris@127 997 size_t thisBlockSize =
Chris@127 998 zoomConstraint->getNearestBlockSize(blockSize, dir);
Chris@127 999
Chris@127 1000 // Go for the block size that's furthest from the one
Chris@127 1001 // passed in. Most of the time, that's what we want.
Chris@127 1002 if (!haveCandidate ||
Chris@127 1003 (thisBlockSize > blockSize && thisBlockSize > candidate) ||
Chris@127 1004 (thisBlockSize < blockSize && thisBlockSize < candidate)) {
Chris@127 1005 candidate = thisBlockSize;
Chris@127 1006 haveCandidate = true;
Chris@127 1007 }
Chris@127 1008 }
Chris@127 1009
Chris@127 1010 return candidate;
Chris@127 1011 }
Chris@127 1012
Chris@127 1013 void
Chris@127 1014 View::zoom(bool in)
Chris@127 1015 {
Chris@127 1016 int newZoomLevel = m_zoomLevel;
Chris@127 1017
Chris@127 1018 if (in) {
Chris@127 1019 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1,
Chris@127 1020 ZoomConstraint::RoundDown);
Chris@127 1021 } else {
Chris@127 1022 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1,
Chris@127 1023 ZoomConstraint::RoundUp);
Chris@127 1024 }
Chris@127 1025
Chris@127 1026 if (newZoomLevel != m_zoomLevel) {
Chris@127 1027 setZoomLevel(newZoomLevel);
Chris@127 1028 }
Chris@127 1029 }
Chris@127 1030
Chris@127 1031 void
Chris@127 1032 View::scroll(bool right, bool lots)
Chris@127 1033 {
Chris@127 1034 long delta;
Chris@127 1035 if (lots) {
Chris@127 1036 delta = (getEndFrame() - getStartFrame()) / 2;
Chris@127 1037 } else {
Chris@127 1038 delta = (getEndFrame() - getStartFrame()) / 20;
Chris@127 1039 }
Chris@127 1040 if (right) delta = -delta;
Chris@127 1041
Chris@127 1042 if (int(m_centreFrame) < delta) {
Chris@127 1043 setCentreFrame(0);
Chris@127 1044 } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) {
Chris@127 1045 setCentreFrame(getModelsEndFrame());
Chris@127 1046 } else {
Chris@127 1047 setCentreFrame(m_centreFrame - delta);
Chris@127 1048 }
Chris@127 1049 }
Chris@127 1050
Chris@127 1051 void
Chris@127 1052 View::checkProgress(void *object)
Chris@127 1053 {
Chris@127 1054 if (!m_showProgress) return;
Chris@127 1055
Chris@127 1056 int ph = height();
Chris@127 1057
Chris@127 1058 for (ProgressMap::const_iterator i = m_progressBars.begin();
Chris@127 1059 i != m_progressBars.end(); ++i) {
Chris@127 1060
Chris@127 1061 if (i->first == object) {
Chris@127 1062
Chris@127 1063 int completion = i->first->getCompletion(this);
Chris@127 1064
Chris@127 1065 if (completion >= 100) {
Chris@127 1066
Chris@127 1067 i->second->hide();
Chris@127 1068
Chris@127 1069 } else {
Chris@127 1070
Chris@127 1071 i->second->setText(i->first->getPropertyContainerName());
Chris@127 1072 i->second->setValue(completion);
Chris@127 1073 i->second->move(0, ph - i->second->height());
Chris@127 1074
Chris@127 1075 i->second->show();
Chris@127 1076 i->second->update();
Chris@127 1077
Chris@127 1078 ph -= i->second->height();
Chris@127 1079 }
Chris@127 1080 } else {
Chris@127 1081 if (i->second->isVisible()) {
Chris@127 1082 ph -= i->second->height();
Chris@127 1083 }
Chris@127 1084 }
Chris@127 1085 }
Chris@127 1086 }
Chris@127 1087
Chris@127 1088 void
Chris@127 1089 View::paintEvent(QPaintEvent *e)
Chris@127 1090 {
Chris@127 1091 // Profiler prof("View::paintEvent", false);
Chris@127 1092 // std::cerr << "View::paintEvent" << std::endl;
Chris@127 1093
Chris@127 1094 if (m_layers.empty()) {
Chris@127 1095 QFrame::paintEvent(e);
Chris@127 1096 return;
Chris@127 1097 }
Chris@127 1098
Chris@127 1099 // ensure our constraints are met
Chris@127 1100 m_zoomLevel = getZoomConstraintBlockSize(m_zoomLevel,
Chris@127 1101 ZoomConstraint::RoundUp);
Chris@127 1102
Chris@127 1103 QPainter paint;
Chris@127 1104 bool repaintCache = false;
Chris@127 1105 bool paintedCacheRect = false;
Chris@127 1106
Chris@127 1107 QRect cacheRect(rect());
Chris@127 1108
Chris@127 1109 if (e) {
Chris@127 1110 cacheRect &= e->rect();
Chris@127 1111 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 1112 std::cerr << "paint rect " << cacheRect.width() << "x" << cacheRect.height()
Chris@127 1113 << ", my rect " << width() << "x" << height() << std::endl;
Chris@127 1114 #endif
Chris@127 1115 }
Chris@127 1116
Chris@127 1117 QRect nonCacheRect(cacheRect);
Chris@127 1118
Chris@127 1119 // If not all layers are scrollable, but some of the back layers
Chris@127 1120 // are, we should store only those in the cache.
Chris@127 1121
Chris@127 1122 bool layersChanged = false;
Chris@127 1123 LayerList scrollables = getScrollableBackLayers(true, layersChanged);
Chris@127 1124 LayerList nonScrollables = getNonScrollableFrontLayers(true, layersChanged);
Chris@127 1125 bool selectionCacheable = nonScrollables.empty();
Chris@127 1126 bool haveSelections = m_manager && !m_manager->getSelections().empty();
Chris@127 1127 bool selectionDrawn = false;
Chris@127 1128
Chris@127 1129 // If all the non-scrollable layers are non-opaque, then we draw
Chris@127 1130 // the selection rectangle behind them and cache it. If any are
Chris@127 1131 // opaque, however, we can't cache.
Chris@127 1132 //
Chris@127 1133 if (!selectionCacheable) {
Chris@127 1134 selectionCacheable = true;
Chris@127 1135 for (LayerList::const_iterator i = nonScrollables.begin();
Chris@127 1136 i != nonScrollables.end(); ++i) {
Chris@127 1137 if ((*i)->isLayerOpaque()) {
Chris@127 1138 selectionCacheable = false;
Chris@127 1139 break;
Chris@127 1140 }
Chris@127 1141 }
Chris@127 1142 }
Chris@127 1143
Chris@127 1144 if (selectionCacheable) {
Chris@127 1145 QPoint localPos;
Chris@127 1146 bool closeToLeft, closeToRight;
Chris@127 1147 if (shouldIlluminateLocalSelection(localPos, closeToLeft, closeToRight)) {
Chris@127 1148 selectionCacheable = false;
Chris@127 1149 }
Chris@127 1150 }
Chris@127 1151
Chris@127 1152 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 1153 std::cerr << "View(" << this << ")::paintEvent: have " << scrollables.size()
Chris@127 1154 << " scrollable back layers and " << nonScrollables.size()
Chris@127 1155 << " non-scrollable front layers" << std::endl;
Chris@127 1156 std::cerr << "haveSelections " << haveSelections << ", selectionCacheable "
Chris@127 1157 << selectionCacheable << ", m_selectionCached " << m_selectionCached << std::endl;
Chris@127 1158 #endif
Chris@127 1159
Chris@127 1160 if (layersChanged || scrollables.empty() ||
Chris@127 1161 (haveSelections && (selectionCacheable != m_selectionCached))) {
Chris@127 1162 delete m_cache;
Chris@127 1163 m_cache = 0;
Chris@127 1164 m_selectionCached = false;
Chris@127 1165 }
Chris@127 1166
Chris@127 1167 if (!scrollables.empty()) {
Chris@127 1168 if (!m_cache ||
Chris@127 1169 m_cacheZoomLevel != m_zoomLevel ||
Chris@127 1170 width() != m_cache->width() ||
Chris@127 1171 height() != m_cache->height()) {
Chris@127 1172
Chris@127 1173 // cache is not valid
Chris@127 1174
Chris@127 1175 if (cacheRect.width() < width()/10) {
Chris@127 1176 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 1177 std::cerr << "View(" << this << ")::paintEvent: small repaint, not bothering to recreate cache" << std::endl;
Chris@127 1178 #endif
Chris@127 1179 } else {
Chris@127 1180 delete m_cache;
Chris@127 1181 m_cache = new QPixmap(width(), height());
Chris@127 1182 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 1183 std::cerr << "View(" << this << ")::paintEvent: recreated cache" << std::endl;
Chris@127 1184 #endif
Chris@127 1185 cacheRect = rect();
Chris@127 1186 repaintCache = true;
Chris@127 1187 }
Chris@127 1188
Chris@127 1189 } else if (m_cacheCentreFrame != m_centreFrame) {
Chris@127 1190
Chris@127 1191 long dx =
Chris@127 1192 getXForFrame(m_cacheCentreFrame) -
Chris@127 1193 getXForFrame(m_centreFrame);
Chris@127 1194
Chris@127 1195 if (dx > -width() && dx < width()) {
Chris@127 1196 #if defined(Q_WS_WIN32) || defined(Q_WS_MAC)
Chris@127 1197 // Copying a pixmap to itself doesn't work properly on Windows
Chris@127 1198 // or Mac (it only works when moving in one direction)
Chris@127 1199 static QPixmap *tmpPixmap = 0;
Chris@127 1200 if (!tmpPixmap ||
Chris@127 1201 tmpPixmap->width() != width() ||
Chris@127 1202 tmpPixmap->height() != height()) {
Chris@127 1203 delete tmpPixmap;
Chris@127 1204 tmpPixmap = new QPixmap(width(), height());
Chris@127 1205 }
Chris@127 1206 paint.begin(tmpPixmap);
Chris@127 1207 paint.drawPixmap(0, 0, *m_cache);
Chris@127 1208 paint.end();
Chris@127 1209 paint.begin(m_cache);
Chris@127 1210 paint.drawPixmap(dx, 0, *tmpPixmap);
Chris@127 1211 paint.end();
Chris@127 1212 #else
Chris@127 1213 // But it seems to be fine on X11
Chris@127 1214 paint.begin(m_cache);
Chris@127 1215 paint.drawPixmap(dx, 0, *m_cache);
Chris@127 1216 paint.end();
Chris@127 1217 #endif
Chris@127 1218
Chris@127 1219 if (dx < 0) {
Chris@127 1220 cacheRect = QRect(width() + dx, 0, -dx, height());
Chris@127 1221 } else {
Chris@127 1222 cacheRect = QRect(0, 0, dx, height());
Chris@127 1223 }
Chris@127 1224 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 1225 std::cerr << "View(" << this << ")::paintEvent: scrolled cache by " << dx << std::endl;
Chris@127 1226 #endif
Chris@127 1227 } else {
Chris@127 1228 cacheRect = rect();
Chris@127 1229 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 1230 std::cerr << "View(" << this << ")::paintEvent: scrolling too far" << std::endl;
Chris@127 1231 #endif
Chris@127 1232 }
Chris@127 1233 repaintCache = true;
Chris@127 1234
Chris@127 1235 } else {
Chris@127 1236 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 1237 std::cerr << "View(" << this << ")::paintEvent: cache is good" << std::endl;
Chris@127 1238 #endif
Chris@127 1239 paint.begin(this);
Chris@127 1240 paint.drawPixmap(cacheRect, *m_cache, cacheRect);
Chris@127 1241 paint.end();
Chris@127 1242 QFrame::paintEvent(e);
Chris@127 1243 paintedCacheRect = true;
Chris@127 1244 }
Chris@127 1245
Chris@127 1246 m_cacheCentreFrame = m_centreFrame;
Chris@127 1247 m_cacheZoomLevel = m_zoomLevel;
Chris@127 1248 }
Chris@127 1249
Chris@127 1250 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 1251 // std::cerr << "View(" << this << ")::paintEvent: cacheRect " << cacheRect << ", nonCacheRect " << (nonCacheRect | cacheRect) << ", repaintCache " << repaintCache << ", paintedCacheRect " << paintedCacheRect << std::endl;
Chris@127 1252 #endif
Chris@127 1253
Chris@127 1254 // Scrollable (cacheable) items first
Chris@127 1255
Chris@127 1256 if (!paintedCacheRect) {
Chris@127 1257
Chris@127 1258 if (repaintCache) paint.begin(m_cache);
Chris@127 1259 else paint.begin(this);
Chris@127 1260
Chris@127 1261 paint.setClipRect(cacheRect);
Chris@127 1262
Chris@127 1263 if (hasLightBackground()) {
Chris@127 1264 paint.setPen(Qt::white);
Chris@127 1265 paint.setBrush(Qt::white);
Chris@127 1266 } else {
Chris@127 1267 paint.setPen(Qt::black);
Chris@127 1268 paint.setBrush(Qt::black);
Chris@127 1269 }
Chris@127 1270 paint.drawRect(cacheRect);
Chris@127 1271
Chris@127 1272 paint.setPen(Qt::black);
Chris@127 1273 paint.setBrush(Qt::NoBrush);
Chris@127 1274
Chris@127 1275 for (LayerList::iterator i = scrollables.begin(); i != scrollables.end(); ++i) {
Chris@127 1276 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@127 1277 paint.save();
Chris@127 1278 (*i)->paint(this, paint, cacheRect);
Chris@127 1279 paint.restore();
Chris@127 1280 }
Chris@127 1281
Chris@127 1282 if (haveSelections && selectionCacheable) {
Chris@127 1283 drawSelections(paint);
Chris@127 1284 m_selectionCached = repaintCache;
Chris@127 1285 selectionDrawn = true;
Chris@127 1286 }
Chris@127 1287
Chris@127 1288 paint.end();
Chris@127 1289
Chris@127 1290 if (repaintCache) {
Chris@127 1291 cacheRect |= (e ? e->rect() : rect());
Chris@127 1292 paint.begin(this);
Chris@127 1293 paint.drawPixmap(cacheRect, *m_cache, cacheRect);
Chris@127 1294 paint.end();
Chris@127 1295 }
Chris@127 1296 }
Chris@127 1297
Chris@127 1298 // Now non-cacheable items. We always need to redraw the
Chris@127 1299 // non-cacheable items across at least the area we drew of the
Chris@127 1300 // cacheable items.
Chris@127 1301
Chris@127 1302 nonCacheRect |= cacheRect;
Chris@127 1303
Chris@127 1304 paint.begin(this);
Chris@127 1305 paint.setClipRect(nonCacheRect);
Chris@127 1306
Chris@127 1307 if (scrollables.empty()) {
Chris@127 1308 if (hasLightBackground()) {
Chris@127 1309 paint.setPen(Qt::white);
Chris@127 1310 paint.setBrush(Qt::white);
Chris@127 1311 } else {
Chris@127 1312 paint.setPen(Qt::black);
Chris@127 1313 paint.setBrush(Qt::black);
Chris@127 1314 }
Chris@127 1315 paint.drawRect(nonCacheRect);
Chris@127 1316 }
Chris@127 1317
Chris@127 1318 paint.setPen(Qt::black);
Chris@127 1319 paint.setBrush(Qt::NoBrush);
Chris@127 1320
Chris@127 1321 for (LayerList::iterator i = nonScrollables.begin(); i != nonScrollables.end(); ++i) {
Chris@127 1322 // Profiler profiler2("View::paintEvent non-cacheable");
Chris@127 1323 (*i)->paint(this, paint, nonCacheRect);
Chris@127 1324 }
Chris@127 1325
Chris@127 1326 paint.end();
Chris@127 1327
Chris@127 1328 paint.begin(this);
Chris@127 1329 if (e) paint.setClipRect(e->rect());
Chris@127 1330 if (!m_selectionCached) {
Chris@127 1331 drawSelections(paint);
Chris@127 1332 }
Chris@127 1333 paint.end();
Chris@127 1334
Chris@127 1335 if (m_followPlay != PlaybackScrollContinuous) {
Chris@127 1336
Chris@127 1337 paint.begin(this);
Chris@127 1338
Chris@127 1339 if (long(m_playPointerFrame) > getStartFrame() &&
Chris@127 1340 m_playPointerFrame < getEndFrame()) {
Chris@127 1341
Chris@127 1342 int playx = getXForFrame(m_playPointerFrame);
Chris@127 1343
Chris@127 1344 paint.setPen(Qt::black);
Chris@127 1345 paint.drawLine(playx - 1, 0, playx - 1, height() - 1);
Chris@127 1346 paint.drawLine(playx + 1, 0, playx + 1, height() - 1);
Chris@127 1347 paint.drawPoint(playx, 0);
Chris@127 1348 paint.drawPoint(playx, height() - 1);
Chris@127 1349 paint.setPen(Qt::white);
Chris@127 1350 paint.drawLine(playx, 1, playx, height() - 2);
Chris@127 1351 }
Chris@127 1352
Chris@127 1353 paint.end();
Chris@127 1354 }
Chris@127 1355
Chris@127 1356 QFrame::paintEvent(e);
Chris@127 1357 }
Chris@127 1358
Chris@127 1359 void
Chris@127 1360 View::drawSelections(QPainter &paint)
Chris@127 1361 {
Chris@127 1362 MultiSelection::SelectionList selections;
Chris@127 1363
Chris@127 1364 if (m_manager) {
Chris@127 1365 selections = m_manager->getSelections();
Chris@127 1366 if (m_manager->haveInProgressSelection()) {
Chris@127 1367 bool exclusive;
Chris@127 1368 Selection inProgressSelection =
Chris@127 1369 m_manager->getInProgressSelection(exclusive);
Chris@127 1370 if (exclusive) selections.clear();
Chris@127 1371 selections.insert(inProgressSelection);
Chris@127 1372 }
Chris@127 1373 }
Chris@127 1374
Chris@127 1375 paint.save();
Chris@127 1376 paint.setBrush(QColor(150, 150, 255, 80));
Chris@127 1377
Chris@127 1378 int sampleRate = getModelsSampleRate();
Chris@127 1379
Chris@127 1380 QPoint localPos;
Chris@127 1381 long illuminateFrame = -1;
Chris@127 1382 bool closeToLeft, closeToRight;
Chris@127 1383
Chris@127 1384 if (shouldIlluminateLocalSelection(localPos, closeToLeft, closeToRight)) {
Chris@127 1385 illuminateFrame = getFrameForX(localPos.x());
Chris@127 1386 }
Chris@127 1387
Chris@127 1388 const QFontMetrics &metrics = paint.fontMetrics();
Chris@127 1389
Chris@127 1390 for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@127 1391 i != selections.end(); ++i) {
Chris@127 1392
Chris@127 1393 int p0 = getXForFrame(i->getStartFrame());
Chris@127 1394 int p1 = getXForFrame(i->getEndFrame());
Chris@127 1395
Chris@127 1396 if (p1 < 0 || p0 > width()) continue;
Chris@127 1397
Chris@127 1398 #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@127 1399 std::cerr << "View::drawSelections: " << p0 << ",-1 [" << (p1-p0) << "x" << (height()+1) << "]" << std::endl;
Chris@127 1400 #endif
Chris@127 1401
Chris@127 1402 bool illuminateThis =
Chris@127 1403 (illuminateFrame >= 0 && i->contains(illuminateFrame));
Chris@127 1404
Chris@127 1405 paint.setPen(QColor(150, 150, 255));
Chris@127 1406 paint.drawRect(p0, -1, p1 - p0, height() + 1);
Chris@127 1407
Chris@127 1408 if (illuminateThis) {
Chris@127 1409 paint.save();
Chris@127 1410 if (hasLightBackground()) {
Chris@127 1411 paint.setPen(QPen(Qt::black, 2));
Chris@127 1412 } else {
Chris@127 1413 paint.setPen(QPen(Qt::white, 2));
Chris@127 1414 }
Chris@127 1415 if (closeToLeft) {
Chris@127 1416 paint.drawLine(p0, 1, p1, 1);
Chris@127 1417 paint.drawLine(p0, 0, p0, height());
Chris@127 1418 paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@127 1419 } else if (closeToRight) {
Chris@127 1420 paint.drawLine(p0, 1, p1, 1);
Chris@127 1421 paint.drawLine(p1, 0, p1, height());
Chris@127 1422 paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@127 1423 } else {
Chris@127 1424 paint.setBrush(Qt::NoBrush);
Chris@127 1425 paint.drawRect(p0, 1, p1 - p0, height() - 2);
Chris@127 1426 }
Chris@127 1427 paint.restore();
Chris@127 1428 }
Chris@127 1429
Chris@127 1430 if (sampleRate && shouldLabelSelections() && m_manager &&
Chris@127 1431 m_manager->getOverlayMode() != ViewManager::NoOverlays) {
Chris@127 1432
Chris@127 1433 QString startText = QString("%1 / %2")
Chris@127 1434 .arg(QString::fromStdString
Chris@127 1435 (RealTime::frame2RealTime
Chris@127 1436 (i->getStartFrame(), sampleRate).toText(true)))
Chris@127 1437 .arg(i->getStartFrame());
Chris@127 1438
Chris@127 1439 QString endText = QString(" %1 / %2")
Chris@127 1440 .arg(QString::fromStdString
Chris@127 1441 (RealTime::frame2RealTime
Chris@127 1442 (i->getEndFrame(), sampleRate).toText(true)))
Chris@127 1443 .arg(i->getEndFrame());
Chris@127 1444
Chris@127 1445 QString durationText = QString("(%1 / %2) ")
Chris@127 1446 .arg(QString::fromStdString
Chris@127 1447 (RealTime::frame2RealTime
Chris@127 1448 (i->getEndFrame() - i->getStartFrame(), sampleRate)
Chris@127 1449 .toText(true)))
Chris@127 1450 .arg(i->getEndFrame() - i->getStartFrame());
Chris@127 1451
Chris@127 1452 int sw = metrics.width(startText),
Chris@127 1453 ew = metrics.width(endText),
Chris@127 1454 dw = metrics.width(durationText);
Chris@127 1455
Chris@127 1456 int sy = metrics.ascent() + metrics.height() + 4;
Chris@127 1457 int ey = sy;
Chris@127 1458 int dy = sy + metrics.height();
Chris@127 1459
Chris@127 1460 int sx = p0 + 2;
Chris@127 1461 int ex = sx;
Chris@127 1462 int dx = sx;
Chris@127 1463
Chris@127 1464 if (sw + ew > (p1 - p0)) {
Chris@127 1465 ey += metrics.height();
Chris@127 1466 dy += metrics.height();
Chris@127 1467 }
Chris@127 1468
Chris@127 1469 if (ew < (p1 - p0)) {
Chris@127 1470 ex = p1 - 2 - ew;
Chris@127 1471 }
Chris@127 1472
Chris@127 1473 if (dw < (p1 - p0)) {
Chris@127 1474 dx = p1 - 2 - dw;
Chris@127 1475 }
Chris@127 1476
Chris@127 1477 paint.drawText(sx, sy, startText);
Chris@127 1478 paint.drawText(ex, ey, endText);
Chris@127 1479 paint.drawText(dx, dy, durationText);
Chris@127 1480 }
Chris@127 1481 }
Chris@127 1482
Chris@127 1483 paint.restore();
Chris@127 1484 }
Chris@127 1485
Chris@127 1486 QString
Chris@127 1487 View::toXmlString(QString indent, QString extraAttributes) const
Chris@127 1488 {
Chris@127 1489 QString s;
Chris@127 1490
Chris@127 1491 s += indent;
Chris@127 1492
Chris@127 1493 s += QString("<view "
Chris@127 1494 "centre=\"%1\" "
Chris@127 1495 "zoom=\"%2\" "
Chris@127 1496 "followPan=\"%3\" "
Chris@127 1497 "followZoom=\"%4\" "
Chris@127 1498 "tracking=\"%5\" "
Chris@127 1499 "light=\"%6\" %7>\n")
Chris@127 1500 .arg(m_centreFrame)
Chris@127 1501 .arg(m_zoomLevel)
Chris@127 1502 .arg(m_followPan)
Chris@127 1503 .arg(m_followZoom)
Chris@127 1504 .arg(m_followPlay == PlaybackScrollContinuous ? "scroll" :
Chris@127 1505 m_followPlay == PlaybackScrollPage ? "page" : "ignore")
Chris@127 1506 .arg(m_lightBackground)
Chris@127 1507 .arg(extraAttributes);
Chris@127 1508
Chris@127 1509 for (size_t i = 0; i < m_layers.size(); ++i) {
Chris@127 1510 s += m_layers[i]->toXmlString(indent + " ");
Chris@127 1511 }
Chris@127 1512
Chris@127 1513 s += indent + "</view>\n";
Chris@127 1514
Chris@127 1515 return s;
Chris@127 1516 }
Chris@127 1517
Chris@127 1518 ViewPropertyContainer::ViewPropertyContainer(View *v) :
Chris@127 1519 m_v(v)
Chris@127 1520 {
Chris@127 1521 connect(m_v, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@127 1522 this, SIGNAL(propertyChanged(PropertyContainer::PropertyName)));
Chris@127 1523 }
Chris@127 1524
Chris@127 1525 #ifdef INCLUDE_MOCFILES
Chris@127 1526 #include "View.moc.cpp"
Chris@127 1527 #endif
Chris@127 1528