annotate base/View.cpp @ 82:bf42d8d63885

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