annotate base/View.cpp @ 106:45175d8c5dc5

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