svgui  1.9
View.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "View.h"
17 #include "layer/Layer.h"
18 #include "data/model/Model.h"
19 #include "base/ZoomConstraint.h"
20 #include "base/Profiler.h"
21 #include "base/Pitch.h"
22 #include "base/Preferences.h"
23 
24 #include "layer/TimeRulerLayer.h"
26 #include "data/model/PowerOfSqrtTwoZoomConstraint.h"
27 #include "data/model/RangeSummarisableTimeValueModel.h"
28 
29 #include "widgets/IconLoader.h"
30 
31 #include <QPainter>
32 #include <QPaintEvent>
33 #include <QRect>
34 #include <QApplication>
35 #include <QProgressDialog>
36 #include <QTextStream>
37 #include <QFont>
38 #include <QMessageBox>
39 #include <QPushButton>
40 
41 #include <iostream>
42 #include <cassert>
43 #include <cmath>
44 
45 #include <unistd.h>
46 
47 //#define DEBUG_VIEW 1
48 //#define DEBUG_VIEW_WIDGET_PAINT 1
49 
50 
51 View::View(QWidget *w, bool showProgress) :
52  QFrame(w),
53  m_centreFrame(0),
54  m_zoomLevel(1024),
55  m_followPan(true),
56  m_followZoom(true),
57  m_followPlay(PlaybackScrollPageWithCentre),
58  m_followPlayIsDetached(false),
59  m_playPointerFrame(0),
60  m_showProgress(showProgress),
61  m_cache(0),
62  m_cacheCentreFrame(0),
63  m_cacheZoomLevel(1024),
64  m_selectionCached(false),
65  m_deleting(false),
66  m_haveSelectedLayer(false),
67  m_manager(0),
68  m_propertyContainer(new ViewPropertyContainer(this))
69 {
70 // cerr << "View::View(" << this << ")" << endl;
71 }
72 
74 {
75 // cerr << "View::~View(" << this << ")" << endl;
76 
77  m_deleting = true;
78  delete m_propertyContainer;
79 }
80 
81 PropertyContainer::PropertyList
83 {
84  PropertyContainer::PropertyList list;
85  list.push_back("Global Scroll");
86  list.push_back("Global Zoom");
87  list.push_back("Follow Playback");
88  return list;
89 }
90 
91 QString
93 {
94  if (pn == "Global Scroll") return tr("Global Scroll");
95  if (pn == "Global Zoom") return tr("Global Zoom");
96  if (pn == "Follow Playback") return tr("Follow Playback");
97  return "";
98 }
99 
100 PropertyContainer::PropertyType
101 View::getPropertyType(const PropertyContainer::PropertyName &name) const
102 {
103  if (name == "Global Scroll") return PropertyContainer::ToggleProperty;
104  if (name == "Global Zoom") return PropertyContainer::ToggleProperty;
105  if (name == "Follow Playback") return PropertyContainer::ValueProperty;
106  return PropertyContainer::InvalidProperty;
107 }
108 
109 int
110 View::getPropertyRangeAndValue(const PropertyContainer::PropertyName &name,
111  int *min, int *max, int *deflt) const
112 {
113  if (deflt) *deflt = 1;
114  if (name == "Global Scroll") return m_followPan;
115  if (name == "Global Zoom") return m_followZoom;
116  if (name == "Follow Playback") {
117  if (min) *min = 0;
118  if (max) *max = 2;
119  if (deflt) *deflt = int(PlaybackScrollPageWithCentre);
120  switch (m_followPlay) {
121  case PlaybackScrollContinuous: return 0;
123  case PlaybackIgnore: return 2;
124  }
125  }
126  if (min) *min = 0;
127  if (max) *max = 0;
128  if (deflt) *deflt = 0;
129  return 0;
130 }
131 
132 QString
133 View::getPropertyValueLabel(const PropertyContainer::PropertyName &name,
134  int value) const
135 {
136  if (name == "Follow Playback") {
137  switch (value) {
138  default:
139  case 0: return tr("Scroll");
140  case 1: return tr("Page");
141  case 2: return tr("Off");
142  }
143  }
144  return tr("<unknown>");
145 }
146 
147 void
148 View::setProperty(const PropertyContainer::PropertyName &name, int value)
149 {
150  if (name == "Global Scroll") {
151  setFollowGlobalPan(value != 0);
152  } else if (name == "Global Zoom") {
153  setFollowGlobalZoom(value != 0);
154  } else if (name == "Follow Playback") {
155  switch (value) {
156  default:
159  case 2: setPlaybackFollow(PlaybackIgnore); break;
160  }
161  }
162 }
163 
164 int
166 {
167  return m_fixedOrderLayers.size() + 1; // the 1 is for me
168 }
169 
170 const PropertyContainer *
172 {
173  return (const PropertyContainer *)(((View *)this)->
175 }
176 
177 PropertyContainer *
179 {
180  if (i == 0) return m_propertyContainer;
181  return m_fixedOrderLayers[i-1];
182 }
183 
184 bool
185 View::getValueExtents(QString unit, float &min, float &max, bool &log) const
186 {
187  bool have = false;
188 
189  for (LayerList::const_iterator i = m_layerStack.begin();
190  i != m_layerStack.end(); ++i) {
191 
192  QString layerUnit;
193  float layerMin = 0.0, layerMax = 0.0;
194  float displayMin = 0.0, displayMax = 0.0;
195  bool layerLog = false;
196 
197  if ((*i)->getValueExtents(layerMin, layerMax, layerLog, layerUnit) &&
198  layerUnit.toLower() == unit.toLower()) {
199 
200  if ((*i)->getDisplayExtents(displayMin, displayMax)) {
201 
202  min = displayMin;
203  max = displayMax;
204  log = layerLog;
205  have = true;
206  break;
207 
208  } else {
209 
210  if (!have || layerMin < min) min = layerMin;
211  if (!have || layerMax > max) max = layerMax;
212  if (layerLog) log = true;
213  have = true;
214  }
215  }
216  }
217 
218  return have;
219 }
220 
221 int
222 View::getTextLabelHeight(const Layer *layer, QPainter &paint) const
223 {
224  std::map<int, Layer *> sortedLayers;
225 
226  for (LayerList::const_iterator i = m_layerStack.begin();
227  i != m_layerStack.end(); ++i) {
228  if ((*i)->needsTextLabelHeight()) {
229  sortedLayers[getObjectExportId(*i)] = *i;
230  }
231  }
232 
233  int y = 15 + paint.fontMetrics().ascent();
234 
235  for (std::map<int, Layer *>::const_iterator i = sortedLayers.begin();
236  i != sortedLayers.end(); ++i) {
237  if (i->second == layer) return y;
238  y += paint.fontMetrics().height();
239  }
240 
241  return y;
242 }
243 
244 void
245 View::propertyContainerSelected(View *client, PropertyContainer *pc)
246 {
247  if (client != this) return;
248 
249  if (pc == m_propertyContainer) {
250  if (m_haveSelectedLayer) {
251  m_haveSelectedLayer = false;
252  update();
253  }
254  return;
255  }
256 
257  delete m_cache;
258  m_cache = 0;
259 
260  Layer *selectedLayer = 0;
261 
262  for (LayerList::iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
263  if (*i == pc) {
264  selectedLayer = *i;
265  m_layerStack.erase(i);
266  break;
267  }
268  }
269 
270  if (selectedLayer) {
271  m_haveSelectedLayer = true;
272  m_layerStack.push_back(selectedLayer);
273  update();
274  } else {
275  m_haveSelectedLayer = false;
276  }
277 
278  emit propertyContainerSelected(pc);
279 }
280 
281 void
283 {
284 // SVDEBUG << "View::toolModeChanged(" << m_manager->getToolMode() << ")" << endl;
285 }
286 
287 void
289 {
290  delete m_cache;
291  m_cache = 0;
292  update();
293 }
294 
295 void
297 {
298  // subclass might override this
299 }
300 
301 int
303 {
304  return getFrameForX(0);
305 }
306 
307 int
309 {
310  return getFrameForX(width()) - 1;
311 }
312 
313 void
315 {
316  setCentreFrame(f + m_zoomLevel * (width() / 2));
317 }
318 
319 bool
320 View::setCentreFrame(int f, bool e)
321 {
322  bool changeVisible = false;
323 
324  if (m_centreFrame != f) {
325 
326  int formerPixel = m_centreFrame / m_zoomLevel;
327 
328  m_centreFrame = f;
329 
330  int newPixel = m_centreFrame / m_zoomLevel;
331 
332  if (newPixel != formerPixel) {
333 
334 #ifdef DEBUG_VIEW_WIDGET_PAINT
335  cout << "View(" << this << ")::setCentreFrame: newPixel " << newPixel << ", formerPixel " << formerPixel << endl;
336 #endif
337  update();
338 
339  changeVisible = true;
340  }
341 
342  if (e) {
343  int rf = alignToReference(f);
344 #ifdef DEBUG_VIEW
345  cerr << "View[" << this << "]::setCentreFrame(" << f
346  << "): emitting centreFrameChanged("
347  << rf << ")" << endl;
348 #endif
350  }
351  }
352 
353  return changeVisible;
354 }
355 
356 int
357 View::getXForFrame(int frame) const
358 {
359  return (frame - getStartFrame()) / m_zoomLevel;
360 }
361 
362 int
363 View::getFrameForX(int x) const
364 {
365  int z = m_zoomLevel;
366  int frame = m_centreFrame - (width()/2) * z;
367 
368 #ifdef DEBUG_VIEW_WIDGET_PAINT
369  SVDEBUG << "View::getFrameForX(" << x << "): z = " << z << ", m_centreFrame = " << m_centreFrame << ", width() = " << width() << ", frame = " << frame << endl;
370 #endif
371 
372  frame = (frame / z) * z; // this is start frame
373  return frame + x * z;
374 }
375 
376 float
377 View::getYForFrequency(float frequency,
378  float minf,
379  float maxf,
380  bool logarithmic) const
381 {
382  Profiler profiler("View::getYForFrequency");
383 
384  int h = height();
385 
386  if (logarithmic) {
387 
388  static float lastminf = 0.0, lastmaxf = 0.0;
389  static float logminf = 0.0, logmaxf = 0.0;
390 
391  if (lastminf != minf) {
392  lastminf = (minf == 0.0 ? 1.0 : minf);
393  logminf = log10f(minf);
394  }
395  if (lastmaxf != maxf) {
396  lastmaxf = (maxf < lastminf ? lastminf : maxf);
397  logmaxf = log10f(maxf);
398  }
399 
400  if (logminf == logmaxf) return 0;
401  return h - (h * (log10f(frequency) - logminf)) / (logmaxf - logminf);
402 
403  } else {
404 
405  if (minf == maxf) return 0;
406  return h - (h * (frequency - minf)) / (maxf - minf);
407  }
408 }
409 
410 float
412  float minf,
413  float maxf,
414  bool logarithmic) const
415 {
416  int h = height();
417 
418  if (logarithmic) {
419 
420  static float lastminf = 0.0, lastmaxf = 0.0;
421  static float logminf = 0.0, logmaxf = 0.0;
422 
423  if (lastminf != minf) {
424  lastminf = (minf == 0.0 ? 1.0 : minf);
425  logminf = log10f(minf);
426  }
427  if (lastmaxf != maxf) {
428  lastmaxf = (maxf < lastminf ? lastminf : maxf);
429  logmaxf = log10f(maxf);
430  }
431 
432  if (logminf == logmaxf) return 0;
433  return pow(10.f, logminf + ((logmaxf - logminf) * (h - y)) / h);
434 
435  } else {
436 
437  if (minf == maxf) return 0;
438  return minf + ((h - y) * (maxf - minf)) / h;
439  }
440 }
441 
442 int
444 {
445 #ifdef DEBUG_VIEW_WIDGET_PAINT
446 // cout << "zoom level: " << m_zoomLevel << endl;
447 #endif
448  return m_zoomLevel;
449 }
450 
451 void
453 {
454  if (z < 1) z = 1;
455  if (m_zoomLevel != int(z)) {
456  m_zoomLevel = z;
458  update();
459  }
460 }
461 
462 bool
464 {
465  bool darkPalette = false;
466  if (m_manager) darkPalette = m_manager->getGlobalDarkBackground();
467 
469  bool mostSignificantHasDarkBackground = false;
470 
471  for (LayerList::const_iterator i = m_layerStack.begin();
472  i != m_layerStack.end(); ++i) {
473 
474  Layer::ColourSignificance s = (*i)->getLayerColourSignificance();
475  bool light = (*i)->hasLightBackground();
476 
477  if (int(s) > int(maxSignificance)) {
478  maxSignificance = s;
479  mostSignificantHasDarkBackground = !light;
480  } else if (s == maxSignificance && !light) {
481  mostSignificantHasDarkBackground = true;
482  }
483  }
484 
485  if (int(maxSignificance) >= int(Layer::ColourAndBackgroundSignificant)) {
486  return !mostSignificantHasDarkBackground;
487  } else {
488  return !darkPalette;
489  }
490 }
491 
492 QColor
494 {
495  bool light = hasLightBackground();
496 
497  QColor widgetbg = palette().window().color();
498  bool widgetLight =
499  (widgetbg.red() + widgetbg.green() + widgetbg.blue()) > 384;
500 
501  if (widgetLight == light) {
502  if (widgetLight) {
503  return widgetbg.light();
504  } else {
505  return widgetbg.dark();
506  }
507  }
508  else if (light) return Qt::white;
509  else return Qt::black;
510 }
511 
512 QColor
514 {
515  bool light = hasLightBackground();
516 
517  QColor widgetfg = palette().text().color();
518  bool widgetLight =
519  (widgetfg.red() + widgetfg.green() + widgetfg.blue()) > 384;
520 
521  if (widgetLight != light) return widgetfg;
522  else if (light) return Qt::black;
523  else return Qt::white;
524 }
525 
526 void
528 {
529  delete m_cache;
530  m_cache = 0;
531 
532  SingleColourLayer *scl = dynamic_cast<SingleColourLayer *>(layer);
533  if (scl) scl->setDefaultColourFor(this);
534 
535  m_fixedOrderLayers.push_back(layer);
536  m_layerStack.push_back(layer);
537 
538  QProgressBar *pb = new QProgressBar(this);
539  pb->setMinimum(0);
540  pb->setMaximum(0);
541  pb->setFixedWidth(80);
542  pb->setTextVisible(false);
543 
544  QPushButton *cancel = new QPushButton(this);
545  cancel->setIcon(IconLoader().load("fileclose"));
546  cancel->setFlat(true);
547  cancel->setFixedSize(QSize(20, 20));
548  connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
549 
550  ProgressBarRec pbr;
551  pbr.cancel = cancel;
552  pbr.bar = pb;
553  pbr.lastCheck = 0;
554  pbr.checkTimer = new QTimer();
555  connect(pbr.checkTimer, SIGNAL(timeout()), this,
557 
558  m_progressBars[layer] = pbr;
559 
560  QFont f(pb->font());
561  int fs = Preferences::getInstance()->getViewFontSize();
562  f.setPointSize(std::min(fs, int(ceil(fs * 0.85))));
563 
564  cancel->hide();
565 
566  pb->setFont(f);
567  pb->hide();
568 
569  connect(layer, SIGNAL(layerParametersChanged()),
570  this, SLOT(layerParametersChanged()));
571  connect(layer, SIGNAL(layerParameterRangesChanged()),
572  this, SLOT(layerParameterRangesChanged()));
573  connect(layer, SIGNAL(layerMeasurementRectsChanged()),
574  this, SLOT(layerMeasurementRectsChanged()));
575  connect(layer, SIGNAL(layerNameChanged()),
576  this, SLOT(layerNameChanged()));
577  connect(layer, SIGNAL(modelChanged()),
578  this, SLOT(modelChanged()));
579  connect(layer, SIGNAL(modelCompletionChanged()),
580  this, SLOT(modelCompletionChanged()));
581  connect(layer, SIGNAL(modelAlignmentCompletionChanged()),
582  this, SLOT(modelAlignmentCompletionChanged()));
583  connect(layer, SIGNAL(modelChangedWithin(int, int)),
584  this, SLOT(modelChangedWithin(int, int)));
585  connect(layer, SIGNAL(modelReplaced()),
586  this, SLOT(modelReplaced()));
587 
588  update();
589 
590  emit propertyContainerAdded(layer);
591 }
592 
593 void
595 {
596  if (m_deleting) {
597  return;
598  }
599 
600  delete m_cache;
601  m_cache = 0;
602 
603  for (LayerList::iterator i = m_fixedOrderLayers.begin();
604  i != m_fixedOrderLayers.end();
605  ++i) {
606  if (*i == layer) {
607  m_fixedOrderLayers.erase(i);
608  break;
609  }
610  }
611 
612  for (LayerList::iterator i = m_layerStack.begin();
613  i != m_layerStack.end();
614  ++i) {
615  if (*i == layer) {
616  m_layerStack.erase(i);
617  if (m_progressBars.find(layer) != m_progressBars.end()) {
618  delete m_progressBars[layer].bar;
619  delete m_progressBars[layer].cancel;
620  delete m_progressBars[layer].checkTimer;
621  m_progressBars.erase(layer);
622  }
623  break;
624  }
625  }
626 
627  disconnect(layer, SIGNAL(layerParametersChanged()),
628  this, SLOT(layerParametersChanged()));
629  disconnect(layer, SIGNAL(layerParameterRangesChanged()),
630  this, SLOT(layerParameterRangesChanged()));
631  disconnect(layer, SIGNAL(layerNameChanged()),
632  this, SLOT(layerNameChanged()));
633  disconnect(layer, SIGNAL(modelChanged()),
634  this, SLOT(modelChanged()));
635  disconnect(layer, SIGNAL(modelCompletionChanged()),
636  this, SLOT(modelCompletionChanged()));
637  disconnect(layer, SIGNAL(modelAlignmentCompletionChanged()),
638  this, SLOT(modelAlignmentCompletionChanged()));
639  disconnect(layer, SIGNAL(modelChangedWithin(int, int)),
640  this, SLOT(modelChangedWithin(int, int)));
641  disconnect(layer, SIGNAL(modelReplaced()),
642  this, SLOT(modelReplaced()));
643 
644  update();
645 
646  emit propertyContainerRemoved(layer);
647 }
648 
649 Layer *
651 {
652  Layer *sl = getSelectedLayer();
653  if (sl && !(sl->isLayerDormant(this))) {
654  return sl;
655  }
656  if (!m_layerStack.empty()) {
657  int n = getLayerCount();
658  while (n > 0) {
659  --n;
660  Layer *layer = getLayer(n);
661  if (!(layer->isLayerDormant(this))) {
662  return layer;
663  }
664  }
665  }
666  return 0;
667 }
668 
669 const Layer *
671 {
672  return const_cast<const Layer *>(const_cast<View *>(this)->getInteractionLayer());
673 }
674 
675 Layer *
677 {
678  if (m_haveSelectedLayer && !m_layerStack.empty()) {
679  return getLayer(getLayerCount() - 1);
680  } else {
681  return 0;
682  }
683 }
684 
685 const Layer *
687 {
688  return const_cast<const Layer *>(const_cast<View *>(this)->getSelectedLayer());
689 }
690 
691 void
693 {
694  if (m_manager) {
695  m_manager->disconnect(this, SLOT(globalCentreFrameChanged(int)));
696  m_manager->disconnect(this, SLOT(viewCentreFrameChanged(View *, int)));
697  m_manager->disconnect(this, SLOT(viewManagerPlaybackFrameChanged(int)));
698  m_manager->disconnect(this, SLOT(viewZoomLevelChanged(View *, int, bool)));
699  m_manager->disconnect(this, SLOT(toolModeChanged()));
700  m_manager->disconnect(this, SLOT(selectionChanged()));
701  m_manager->disconnect(this, SLOT(overlayModeChanged()));
702  m_manager->disconnect(this, SLOT(zoomWheelsEnabledChanged()));
703  disconnect(m_manager, SLOT(viewCentreFrameChanged(int, bool, PlaybackFollowMode)));
704  disconnect(m_manager, SLOT(zoomLevelChanged(int, bool)));
705  }
706 
707  m_manager = manager;
708 
709  connect(m_manager, SIGNAL(globalCentreFrameChanged(int)),
710  this, SLOT(globalCentreFrameChanged(int)));
711  connect(m_manager, SIGNAL(viewCentreFrameChanged(View *, int)),
712  this, SLOT(viewCentreFrameChanged(View *, int)));
713  connect(m_manager, SIGNAL(playbackFrameChanged(int)),
714  this, SLOT(viewManagerPlaybackFrameChanged(int)));
715 
716  connect(m_manager, SIGNAL(viewZoomLevelChanged(View *, int, bool)),
717  this, SLOT(viewZoomLevelChanged(View *, int, bool)));
718 
719  connect(m_manager, SIGNAL(toolModeChanged()),
720  this, SLOT(toolModeChanged()));
721  connect(m_manager, SIGNAL(selectionChanged()),
722  this, SLOT(selectionChanged()));
723  connect(m_manager, SIGNAL(inProgressSelectionChanged()),
724  this, SLOT(selectionChanged()));
725  connect(m_manager, SIGNAL(overlayModeChanged()),
726  this, SLOT(overlayModeChanged()));
727  connect(m_manager, SIGNAL(showCentreLineChanged()),
728  this, SLOT(overlayModeChanged()));
729  connect(m_manager, SIGNAL(zoomWheelsEnabledChanged()),
730  this, SLOT(zoomWheelsEnabledChanged()));
731 
732  connect(this, SIGNAL(centreFrameChanged(int, bool,
734  m_manager, SLOT(viewCentreFrameChanged(int, bool,
736 
737  connect(this, SIGNAL(zoomLevelChanged(int, bool)),
738  m_manager, SLOT(viewZoomLevelChanged(int, bool)));
739 
740  switch (m_followPlay) {
741 
742  case PlaybackScrollPage:
745  break;
746 
749  break;
750 
751  case PlaybackIgnore:
752  if (m_followPan) {
754  }
755  break;
756  }
757 
759 
761 
762  toolModeChanged();
763 }
764 
765 void
766 View::setViewManager(ViewManager *vm, int initialCentreFrame)
767 {
768  setViewManager(vm);
769  setCentreFrame(initialCentreFrame, false);
770 }
771 
772 void
774 {
775  m_followPan = f;
777 }
778 
779 void
781 {
782  m_followZoom = f;
784 }
785 
786 void
787 View::drawVisibleText(QPainter &paint, int x, int y, QString text, TextStyle style) const
788 {
789  if (style == OutlinedText || style == OutlinedItalicText) {
790 
791  paint.save();
792 
793  if (style == OutlinedItalicText) {
794  QFont f(paint.font());
795  f.setItalic(true);
796  paint.setFont(f);
797  }
798 
799  QColor penColour, surroundColour, boxColour;
800 
801  penColour = getForeground();
802  surroundColour = getBackground();
803  boxColour = surroundColour;
804  boxColour.setAlpha(127);
805 
806  paint.setPen(Qt::NoPen);
807  paint.setBrush(boxColour);
808 
809  QRect r = paint.fontMetrics().boundingRect(text);
810  r.translate(QPoint(x, y));
811 // cerr << "drawVisibleText: r = " << r.x() << "," <<r.y() << " " << r.width() << "x" << r.height() << endl;
812  paint.drawRect(r);
813  paint.setBrush(Qt::NoBrush);
814 
815  paint.setPen(surroundColour);
816 
817  for (int dx = -1; dx <= 1; ++dx) {
818  for (int dy = -1; dy <= 1; ++dy) {
819  if (!(dx || dy)) continue;
820  paint.drawText(x + dx, y + dy, text);
821  }
822  }
823 
824  paint.setPen(penColour);
825 
826  paint.drawText(x, y, text);
827 
828  paint.restore();
829 
830  } else {
831 
832  cerr << "ERROR: View::drawVisibleText: Boxed style not yet implemented!" << endl;
833  }
834 }
835 
836 void
838 {
839  m_followPlay = m;
841 }
842 
843 void
845 {
846  QObject *obj = sender();
847 
848 #ifdef DEBUG_VIEW_WIDGET_PAINT
849  cerr << "View(" << this << ")::modelChanged()" << endl;
850 #endif
851 
852  // If the model that has changed is not used by any of the cached
853  // layers, we won't need to recreate the cache
854 
855  bool recreate = false;
856 
857  bool discard;
858  LayerList scrollables = getScrollableBackLayers(false, discard);
859  for (LayerList::const_iterator i = scrollables.begin();
860  i != scrollables.end(); ++i) {
861  if (*i == obj || (*i)->getModel() == obj) {
862  recreate = true;
863  break;
864  }
865  }
866 
867  if (recreate) {
868  delete m_cache;
869  m_cache = 0;
870  }
871 
872  emit layerModelChanged();
873 
874  checkProgress(obj);
875 
876  update();
877 }
878 
879 void
880 View::modelChangedWithin(int startFrame, int endFrame)
881 {
882  QObject *obj = sender();
883 
884  int myStartFrame = getStartFrame();
885  int myEndFrame = getEndFrame();
886 
887 #ifdef DEBUG_VIEW_WIDGET_PAINT
888  cerr << "View(" << this << ")::modelChangedWithin(" << startFrame << "," << endFrame << ") [me " << myStartFrame << "," << myEndFrame << "]" << endl;
889 #endif
890 
891  if (myStartFrame > 0 && endFrame < int(myStartFrame)) {
892  checkProgress(obj);
893  return;
894  }
895  if (startFrame > myEndFrame) {
896  checkProgress(obj);
897  return;
898  }
899 
900  // If the model that has changed is not used by any of the cached
901  // layers, we won't need to recreate the cache
902 
903  bool recreate = false;
904 
905  bool discard;
906  LayerList scrollables = getScrollableBackLayers(false, discard);
907  for (LayerList::const_iterator i = scrollables.begin();
908  i != scrollables.end(); ++i) {
909  if (*i == obj || (*i)->getModel() == obj) {
910  recreate = true;
911  break;
912  }
913  }
914 
915  if (recreate) {
916  delete m_cache;
917  m_cache = 0;
918  }
919 
920  if (startFrame < myStartFrame) startFrame = myStartFrame;
921  if (endFrame > myEndFrame) endFrame = myEndFrame;
922 
923  checkProgress(obj);
924 
925  update();
926 }
927 
928 void
930 {
931 // cerr << "View(" << this << ")::modelCompletionChanged()" << endl;
932 
933  QObject *obj = sender();
934  checkProgress(obj);
935 }
936 
937 void
939 {
940 // cerr << "View(" << this << ")::modelAlignmentCompletionChanged()" << endl;
941 
942  QObject *obj = sender();
943  checkProgress(obj);
944 }
945 
946 void
948 {
949 #ifdef DEBUG_VIEW_WIDGET_PAINT
950  cerr << "View(" << this << ")::modelReplaced()" << endl;
951 #endif
952  delete m_cache;
953  m_cache = 0;
954 
955  update();
956 }
957 
958 void
960 {
961  Layer *layer = dynamic_cast<Layer *>(sender());
962 
963 #ifdef DEBUG_VIEW_WIDGET_PAINT
964  SVDEBUG << "View::layerParametersChanged()" << endl;
965 #endif
966 
967  delete m_cache;
968  m_cache = 0;
969  update();
970 
971  if (layer) {
973  }
974 }
975 
976 void
978 {
979  Layer *layer = dynamic_cast<Layer *>(sender());
980  if (layer) emit propertyContainerPropertyRangeChanged(layer);
981 }
982 
983 void
985 {
986  Layer *layer = dynamic_cast<Layer *>(sender());
987  if (layer) update();
988 }
989 
990 void
992 {
993  Layer *layer = dynamic_cast<Layer *>(sender());
994  if (layer) emit propertyContainerNameChanged(layer);
995 }
996 
997 void
999 {
1000  if (m_followPan) {
1001  int f = alignFromReference(rf);
1002 #ifdef DEBUG_VIEW
1003  cerr << "View[" << this << "]::globalCentreFrameChanged(" << rf
1004  << "): setting centre frame to " << f << endl;
1005 #endif
1006  setCentreFrame(f, false);
1007  }
1008 }
1009 
1010 void
1012 {
1013  // We do nothing with this, but a subclass might
1014 }
1015 
1016 void
1018 {
1019  if (m_manager) {
1020  if (sender() != m_manager) return;
1021  }
1022 
1023 #ifdef DEBUG_VIEW
1024  cerr << "View::viewManagerPlaybackFrameChanged(" << f << ")" << endl;
1025 #endif
1026 
1028 
1029 #ifdef DEBUG_VIEW
1030  cerr << " -> aligned frame = " << af << endl;
1031 #endif
1032 
1033  movePlayPointer(f);
1034 }
1035 
1036 void
1038 {
1039 #ifdef DEBUG_VIEW
1040  cerr << "View(" << this << ")::movePlayPointer(" << newFrame << ")" << endl;
1041 #endif
1042 
1043  if (m_playPointerFrame == newFrame) return;
1044  bool visibleChange =
1046  int oldPlayPointerFrame = m_playPointerFrame;
1047  m_playPointerFrame = newFrame;
1048  if (!visibleChange) return;
1049 
1050  bool somethingGoingOn =
1051  ((QApplication::mouseButtons() != Qt::NoButton) ||
1052  (QApplication::keyboardModifiers() & Qt::AltModifier));
1053 
1054  bool pointerInVisibleArea =
1055  long(m_playPointerFrame) >= getStartFrame() &&
1057  // include old pointer location so we know to refresh when moving out
1058  oldPlayPointerFrame < getEndFrame());
1059 
1060  switch (m_followPlay) {
1061 
1063  if (!somethingGoingOn) {
1065  }
1066  break;
1067 
1068  case PlaybackScrollPage:
1070 
1071  if (!pointerInVisibleArea && somethingGoingOn) {
1072 
1073  m_followPlayIsDetached = true;
1074 
1075  } else if (!pointerInVisibleArea && m_followPlayIsDetached) {
1076 
1077  // do nothing; we aren't tracking until the pointer comes back in
1078 
1079  } else {
1080 
1081  int xold = getXForFrame(oldPlayPointerFrame);
1082  update(xold - 4, 0, 9, height());
1083 
1084  int w = getEndFrame() - getStartFrame();
1085  w -= w/5;
1086  int sf = (m_playPointerFrame / w) * w - w/8;
1087 
1088  if (m_manager &&
1089  m_manager->isPlaying() &&
1091  MultiSelection::SelectionList selections = m_manager->getSelections();
1092  if (!selections.empty()) {
1093  int selectionStart = selections.begin()->getStartFrame();
1094  if (sf < selectionStart - w / 10) {
1095  sf = selectionStart - w / 10;
1096  }
1097  }
1098  }
1099 
1100 #ifdef DEBUG_VIEW_WIDGET_PAINT
1101  cerr << "PlaybackScrollPage: f = " << m_playPointerFrame << ", sf = " << sf << ", start frame "
1102  << getStartFrame() << endl;
1103 #endif
1104 
1105  // We don't consider scrolling unless the pointer is outside
1106  // the central visible range already
1107 
1108  int xnew = getXForFrame(m_playPointerFrame);
1109 
1110 #ifdef DEBUG_VIEW_WIDGET_PAINT
1111  cerr << "xnew = " << xnew << ", width = " << width() << endl;
1112 #endif
1113 
1114  bool shouldScroll = (xnew > (width() * 7) / 8);
1115 
1116  if (!m_followPlayIsDetached && (xnew < width() / 8)) {
1117  shouldScroll = true;
1118  }
1119 
1120  if (xnew > width() / 8) {
1121  m_followPlayIsDetached = false;
1122  } else if (somethingGoingOn) {
1123  m_followPlayIsDetached = true;
1124  }
1125 
1126  if (!somethingGoingOn && shouldScroll) {
1127  int offset = getFrameForX(width()/2) - getStartFrame();
1128  int newCentre = sf + offset;
1129  bool changed = setCentreFrame(newCentre, false);
1130  if (changed) {
1131  xold = getXForFrame(oldPlayPointerFrame);
1132  update(xold - 4, 0, 9, height());
1133  }
1134  }
1135 
1136  update(xnew - 4, 0, 9, height());
1137  }
1138  break;
1139 
1140  case PlaybackIgnore:
1141  if (m_playPointerFrame >= getStartFrame() &&
1143  update();
1144  }
1145  break;
1146  }
1147 }
1148 
1149 void
1150 View::viewZoomLevelChanged(View *p, int z, bool locked)
1151 {
1152 #ifdef DEBUG_VIEW_WIDGET_PAINT
1153  cerr << "View[" << this << "]: viewZoomLevelChanged(" << p << ", " << z << ", " << locked << ")" << endl;
1154 #endif
1155  if (m_followZoom && p != this && locked) {
1156  setZoomLevel(z);
1157  }
1158 }
1159 
1160 void
1162 {
1163  if (m_selectionCached) {
1164  delete m_cache;
1165  m_cache = 0;
1166  m_selectionCached = false;
1167  }
1168  update();
1169 }
1170 
1171 int
1173 {
1174  int f0 = getStartFrame();
1175  int f = getModelsStartFrame();
1176  if (f0 < 0 || f0 < f) return f;
1177  return f0;
1178 }
1179 
1180 int
1182 {
1183  int f0 = getEndFrame();
1184  int f = getModelsEndFrame();
1185  if (f0 > f) return f;
1186  return f0;
1187 }
1188 
1189 int
1191 {
1192  bool first = true;
1193  int startFrame = 0;
1194 
1195  for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
1196 
1197  if ((*i)->getModel() && (*i)->getModel()->isOK()) {
1198 
1199  int thisStartFrame = (*i)->getModel()->getStartFrame();
1200 
1201  if (first || thisStartFrame < startFrame) {
1202  startFrame = thisStartFrame;
1203  }
1204  first = false;
1205  }
1206  }
1207  return startFrame;
1208 }
1209 
1210 int
1212 {
1213  bool first = true;
1214  int endFrame = 0;
1215 
1216  for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
1217 
1218  if ((*i)->getModel() && (*i)->getModel()->isOK()) {
1219 
1220  int thisEndFrame = (*i)->getModel()->getEndFrame();
1221 
1222  if (first || thisEndFrame > endFrame) {
1223  endFrame = thisEndFrame;
1224  }
1225  first = false;
1226  }
1227  }
1228 
1229  if (first) return getModelsStartFrame();
1230  return endFrame;
1231 }
1232 
1233 int
1235 {
1237  // multiple samplerates, we'd probably want to do frame/time
1238  // conversion in the model
1239 
1241 
1242  for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
1243  if ((*i)->getModel() && (*i)->getModel()->isOK()) {
1244  return (*i)->getModel()->getSampleRate();
1245  }
1246  }
1247  return 0;
1248 }
1249 
1252 {
1253  ModelSet models;
1254 
1255  for (int i = 0; i < getLayerCount(); ++i) {
1256 
1257  Layer *layer = getLayer(i);
1258 
1259  if (dynamic_cast<TimeRulerLayer *>(layer)) {
1260  continue;
1261  }
1262 
1263  if (layer && layer->getModel()) {
1264  Model *model = layer->getModel();
1265  models.insert(model);
1266  }
1267  }
1268 
1269  return models;
1270 }
1271 
1272 Model *
1274 {
1275  if (!m_manager ||
1276  !m_manager->getAlignMode() ||
1278  return 0;
1279  }
1280 
1281  Model *anyModel = 0;
1282  Model *alignedModel = 0;
1283  Model *goodModel = 0;
1284 
1285  for (LayerList::const_iterator i = m_layerStack.begin();
1286  i != m_layerStack.end(); ++i) {
1287 
1288  Layer *layer = *i;
1289 
1290  if (!layer) continue;
1291  if (dynamic_cast<TimeRulerLayer *>(layer)) continue;
1292 
1293  Model *model = (*i)->getModel();
1294  if (!model) continue;
1295 
1296  anyModel = model;
1297 
1298  if (model->getAlignmentReference()) {
1299  alignedModel = model;
1300  if (layer->isLayerOpaque() ||
1301  dynamic_cast<RangeSummarisableTimeValueModel *>(model)) {
1302  goodModel = model;
1303  }
1304  }
1305  }
1306 
1307  if (goodModel) return goodModel;
1308  else if (alignedModel) return alignedModel;
1309  else return anyModel;
1310 }
1311 
1312 int
1314 {
1315  if (!m_manager || !m_manager->getAlignMode()) return f;
1316  Model *aligningModel = getAligningModel();
1317  if (!aligningModel) return f;
1318  return aligningModel->alignFromReference(f);
1319 }
1320 
1321 int
1323 {
1324  if (!m_manager->getAlignMode()) return f;
1325  Model *aligningModel = getAligningModel();
1326  if (!aligningModel) return f;
1327  return aligningModel->alignToReference(f);
1328 }
1329 
1330 int
1332 {
1333  if (!m_manager) return 0;
1334  int pf = m_manager->getPlaybackFrame();
1335  if (!m_manager->getAlignMode()) return pf;
1336 
1337  Model *aligningModel = getAligningModel();
1338  if (!aligningModel) return pf;
1339 
1340  int af = aligningModel->alignFromReference(pf);
1341 
1342  return af;
1343 }
1344 
1345 bool
1347 {
1348  // True iff all views are scrollable
1349  for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
1350  if (!(*i)->isLayerScrollable(this)) return false;
1351  }
1352  return true;
1353 }
1354 
1356 View::getScrollableBackLayers(bool testChanged, bool &changed) const
1357 {
1358  changed = false;
1359 
1360  // We want a list of all the scrollable layers that are behind the
1361  // backmost non-scrollable layer.
1362 
1363  LayerList scrollables;
1364  bool metUnscrollable = false;
1365 
1366  for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
1367 // SVDEBUG << "View::getScrollableBackLayers: calling isLayerDormant on layer " << *i << endl;
1368 // cerr << "(name is " << (*i)->objectName() << ")"
1369 // << endl;
1370 // SVDEBUG << "View::getScrollableBackLayers: I am " << this << endl;
1371  if ((*i)->isLayerDormant(this)) continue;
1372  if ((*i)->isLayerOpaque()) {
1373  // You can't see anything behind an opaque layer!
1374  scrollables.clear();
1375  if (metUnscrollable) break;
1376  }
1377  if (!metUnscrollable && (*i)->isLayerScrollable(this)) {
1378  scrollables.push_back(*i);
1379  } else {
1380  metUnscrollable = true;
1381  }
1382  }
1383 
1384  if (testChanged && scrollables != m_lastScrollableBackLayers) {
1385  m_lastScrollableBackLayers = scrollables;
1386  changed = true;
1387  }
1388  return scrollables;
1389 }
1390 
1392 View::getNonScrollableFrontLayers(bool testChanged, bool &changed) const
1393 {
1394  changed = false;
1395  LayerList nonScrollables;
1396 
1397  // Everything in front of the first non-scrollable from the back
1398  // should also be considered non-scrollable
1399 
1400  bool started = false;
1401 
1402  for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
1403  if ((*i)->isLayerDormant(this)) continue;
1404  if (!started && (*i)->isLayerScrollable(this)) {
1405  continue;
1406  }
1407  started = true;
1408  if ((*i)->isLayerOpaque()) {
1409  // You can't see anything behind an opaque layer!
1410  nonScrollables.clear();
1411  }
1412  nonScrollables.push_back(*i);
1413  }
1414 
1415  if (testChanged && nonScrollables != m_lastNonScrollableBackLayers) {
1416  m_lastNonScrollableBackLayers = nonScrollables;
1417  changed = true;
1418  }
1419 
1420  return nonScrollables;
1421 }
1422 
1423 int
1425  ZoomConstraint::RoundingDirection dir)
1426  const
1427 {
1428  int candidate = blockSize;
1429  bool haveCandidate = false;
1430 
1431  PowerOfSqrtTwoZoomConstraint defaultZoomConstraint;
1432 
1433  for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
1434 
1435  const ZoomConstraint *zoomConstraint = (*i)->getZoomConstraint();
1436  if (!zoomConstraint) zoomConstraint = &defaultZoomConstraint;
1437 
1438  int thisBlockSize =
1439  zoomConstraint->getNearestBlockSize(blockSize, dir);
1440 
1441  // Go for the block size that's furthest from the one
1442  // passed in. Most of the time, that's what we want.
1443  if (!haveCandidate ||
1444  (thisBlockSize > blockSize && thisBlockSize > candidate) ||
1445  (thisBlockSize < blockSize && thisBlockSize < candidate)) {
1446  candidate = thisBlockSize;
1447  haveCandidate = true;
1448  }
1449  }
1450 
1451  return candidate;
1452 }
1453 
1454 bool
1456 {
1457  for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
1458  if ((*i)->getLayerColourSignificance() ==
1459  Layer::ColourHasMeaningfulValue) return true;
1460  if ((*i)->isLayerOpaque()) break;
1461  }
1462  return false;
1463 }
1464 
1465 bool
1467 {
1468  LayerList::const_iterator i = m_layerStack.end();
1469  if (i == m_layerStack.begin()) return false;
1470  --i;
1471  return (*i)->hasTimeXAxis();
1472 }
1473 
1474 void
1475 View::zoom(bool in)
1476 {
1477  int newZoomLevel = m_zoomLevel;
1478 
1479  if (in) {
1480  newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1,
1481  ZoomConstraint::RoundDown);
1482  } else {
1483  newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1,
1484  ZoomConstraint::RoundUp);
1485  }
1486 
1487  if (newZoomLevel != m_zoomLevel) {
1488  setZoomLevel(newZoomLevel);
1489  }
1490 }
1491 
1492 void
1493 View::scroll(bool right, bool lots, bool e)
1494 {
1495  int delta;
1496  if (lots) {
1497  delta = (getEndFrame() - getStartFrame()) / 2;
1498  } else {
1499  delta = (getEndFrame() - getStartFrame()) / 20;
1500  }
1501  if (right) delta = -delta;
1502 
1503  if (int(m_centreFrame) < delta) {
1504  setCentreFrame(0, e);
1505  } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) {
1507  } else {
1508  setCentreFrame(m_centreFrame - delta, e);
1509  }
1510 }
1511 
1512 void
1514 {
1515  QPushButton *cancel = qobject_cast<QPushButton *>(sender());
1516  if (!cancel) return;
1517 
1518  for (ProgressMap::iterator i = m_progressBars.begin();
1519  i != m_progressBars.end(); ++i) {
1520 
1521  if (i->second.cancel == cancel) {
1522 
1523  Layer *layer = i->first;
1524  Model *model = layer->getModel();
1525 
1526  if (model) model->abandon();
1527  }
1528  }
1529 }
1530 
1531 void
1532 View::checkProgress(void *object)
1533 {
1534  if (!m_showProgress) return;
1535 
1536  int ph = height();
1537 
1538  for (ProgressMap::iterator i = m_progressBars.begin();
1539  i != m_progressBars.end(); ++i) {
1540 
1541  QProgressBar *pb = i->second.bar;
1542  QPushButton *cancel = i->second.cancel;
1543 
1544  if (i->first == object) {
1545 
1546  // The timer is used to test for stalls. If the progress
1547  // bar does not get updated for some length of time, the
1548  // timer prompts it to go back into "indeterminate" mode
1549  QTimer *timer = i->second.checkTimer;
1550 
1551  int completion = i->first->getCompletion(this);
1552  QString text = i->first->getPropertyContainerName();
1553  QString error = i->first->getError(this);
1554 
1555  if (error != "" && error != m_lastError) {
1556  QMessageBox::critical(this, tr("Layer rendering error"), error);
1557  m_lastError = error;
1558  }
1559 
1560  Model *model = i->first->getModel();
1561  RangeSummarisableTimeValueModel *wfm =
1562  dynamic_cast<RangeSummarisableTimeValueModel *>(model);
1563 
1564  if (completion > 0) {
1565  pb->setMaximum(100); // was 0, for indeterminate start
1566  }
1567 
1568  if (completion >= 100) {
1569 
1571  if (wfm ||
1572  (model &&
1573  (wfm = dynamic_cast<RangeSummarisableTimeValueModel *>
1574  (model->getSourceModel())))) {
1575  completion = wfm->getAlignmentCompletion();
1576 // SVDEBUG << "View::checkProgress: Alignment completion = " << completion << endl;
1577  if (completion < 100) {
1578  text = tr("Alignment");
1579  }
1580  }
1581 
1582  } else if (wfm) {
1583  update(); // ensure duration &c gets updated
1584  }
1585 
1586  if (completion >= 100) {
1587 
1588  pb->hide();
1589  cancel->hide();
1590  timer->stop();
1591 
1592  } else {
1593 
1594 // cerr << "progress = " << completion << endl;
1595 
1596  if (!pb->isVisible()) {
1597  i->second.lastCheck = 0;
1598  timer->setInterval(2000);
1599  timer->start();
1600  }
1601 
1602  cancel->move(0, ph - pb->height()/2 - 10);
1603  cancel->show();
1604 
1605  pb->setValue(completion);
1606  pb->move(20, ph - pb->height());
1607 
1608  pb->show();
1609  pb->update();
1610 
1611  ph -= pb->height();
1612  }
1613  } else {
1614  if (pb->isVisible()) {
1615  ph -= pb->height();
1616  }
1617  }
1618  }
1619 }
1620 
1621 void
1623 {
1624  QObject *s = sender();
1625  QTimer *t = qobject_cast<QTimer *>(s);
1626  if (!t) return;
1627  for (ProgressMap::iterator i = m_progressBars.begin();
1628  i != m_progressBars.end(); ++i) {
1629  if (i->second.checkTimer == t) {
1630  int value = i->second.bar->value();
1631  if (value > 0 && value == i->second.lastCheck) {
1632  i->second.bar->setMaximum(0); // indeterminate
1633  }
1634  i->second.lastCheck = value;
1635  return;
1636  }
1637  }
1638 }
1639 
1640 int
1642 {
1643  for (ProgressMap::const_iterator i = m_progressBars.begin();
1644  i != m_progressBars.end(); ++i) {
1645  if (i->second.bar && i->second.bar->isVisible()) {
1646  return i->second.bar->width();
1647  }
1648  }
1649 
1650  return 0;
1651 }
1652 
1653 void
1654 View::setPaintFont(QPainter &paint)
1655 {
1656  QFont font(paint.font());
1657  font.setPointSize(Preferences::getInstance()->getViewFontSize());
1658  paint.setFont(font);
1659 }
1660 
1661 void
1662 View::paintEvent(QPaintEvent *e)
1663 {
1664 // Profiler prof("View::paintEvent", false);
1665 // cerr << "View::paintEvent: centre frame is " << m_centreFrame << endl;
1666 
1667  if (m_layerStack.empty()) {
1668  QFrame::paintEvent(e);
1669  return;
1670  }
1671 
1672  // ensure our constraints are met
1673 
1681  QPainter paint;
1682  bool repaintCache = false;
1683  bool paintedCacheRect = false;
1684 
1685  QRect cacheRect(rect());
1686 
1687  if (e) {
1688  cacheRect &= e->rect();
1689 #ifdef DEBUG_VIEW_WIDGET_PAINT
1690  cerr << "paint rect " << cacheRect.width() << "x" << cacheRect.height()
1691  << ", my rect " << width() << "x" << height() << endl;
1692 #endif
1693  }
1694 
1695  QRect nonCacheRect(cacheRect);
1696 
1697  // If not all layers are scrollable, but some of the back layers
1698  // are, we should store only those in the cache.
1699 
1700  bool layersChanged = false;
1701  LayerList scrollables = getScrollableBackLayers(true, layersChanged);
1702  LayerList nonScrollables = getNonScrollableFrontLayers(true, layersChanged);
1703  bool selectionCacheable = nonScrollables.empty();
1704  bool haveSelections = m_manager && !m_manager->getSelections().empty();
1705 
1706  // If all the non-scrollable layers are non-opaque, then we draw
1707  // the selection rectangle behind them and cache it. If any are
1708  // opaque, however, we can't cache.
1709  //
1710  if (!selectionCacheable) {
1711  selectionCacheable = true;
1712  for (LayerList::const_iterator i = nonScrollables.begin();
1713  i != nonScrollables.end(); ++i) {
1714  if ((*i)->isLayerOpaque()) {
1715  selectionCacheable = false;
1716  break;
1717  }
1718  }
1719  }
1720 
1721  if (selectionCacheable) {
1722  QPoint localPos;
1723  bool closeToLeft, closeToRight;
1724  if (shouldIlluminateLocalSelection(localPos, closeToLeft, closeToRight)) {
1725  selectionCacheable = false;
1726  }
1727  }
1728 
1729 #ifdef DEBUG_VIEW_WIDGET_PAINT
1730  cerr << "View(" << this << ")::paintEvent: have " << scrollables.size()
1731  << " scrollable back layers and " << nonScrollables.size()
1732  << " non-scrollable front layers" << endl;
1733  cerr << "haveSelections " << haveSelections << ", selectionCacheable "
1734  << selectionCacheable << ", m_selectionCached " << m_selectionCached << endl;
1735 #endif
1736 
1737  if (layersChanged || scrollables.empty() ||
1738  (haveSelections && (selectionCacheable != m_selectionCached))) {
1739  delete m_cache;
1740  m_cache = 0;
1741  m_selectionCached = false;
1742  }
1743 
1744  if (!scrollables.empty()) {
1745 
1746 #ifdef DEBUG_VIEW_WIDGET_PAINT
1747  cerr << "View(" << this << "): cache " << m_cache << ", cache zoom "
1748  << m_cacheZoomLevel << ", zoom " << m_zoomLevel << endl;
1749 #endif
1750 
1751  if (!m_cache ||
1753  width() != m_cache->width() ||
1754  height() != m_cache->height()) {
1755 
1756  // cache is not valid
1757 
1758  if (cacheRect.width() < width()/10) {
1759  delete m_cache;
1760  m_cache = 0;
1761 #ifdef DEBUG_VIEW_WIDGET_PAINT
1762  cerr << "View(" << this << ")::paintEvent: small repaint, not bothering to recreate cache" << endl;
1763 #endif
1764  } else {
1765  delete m_cache;
1766  m_cache = new QPixmap(width(), height());
1767 #ifdef DEBUG_VIEW_WIDGET_PAINT
1768  cerr << "View(" << this << ")::paintEvent: recreated cache" << endl;
1769 #endif
1770  cacheRect = rect();
1771  repaintCache = true;
1772  }
1773 
1774  } else if (m_cacheCentreFrame != m_centreFrame) {
1775 
1776  int dx =
1779 
1780  if (dx > -width() && dx < width()) {
1781 #ifdef PIXMAP_COPY_TO_SELF
1782  // This is not normally defined. Copying a pixmap to
1783  // itself doesn't work properly on Windows, Mac, or
1784  // X11 with the raster backend (it only works when
1785  // moving in one direction and then presumably only by
1786  // accident). It does actually seem to be fine on X11
1787  // with the native backend, but we prefer not to use
1788  // that anyway
1789  paint.begin(m_cache);
1790  paint.drawPixmap(dx, 0, *m_cache);
1791  paint.end();
1792 #else
1793  static QPixmap *tmpPixmap = 0;
1794  if (!tmpPixmap ||
1795  tmpPixmap->width() != width() ||
1796  tmpPixmap->height() != height()) {
1797  delete tmpPixmap;
1798  tmpPixmap = new QPixmap(width(), height());
1799  }
1800  paint.begin(tmpPixmap);
1801  paint.drawPixmap(0, 0, *m_cache);
1802  paint.end();
1803  paint.begin(m_cache);
1804  paint.drawPixmap(dx, 0, *tmpPixmap);
1805  paint.end();
1806 #endif
1807  if (dx < 0) {
1808  cacheRect = QRect(width() + dx, 0, -dx, height());
1809  } else {
1810  cacheRect = QRect(0, 0, dx, height());
1811  }
1812 #ifdef DEBUG_VIEW_WIDGET_PAINT
1813  cerr << "View(" << this << ")::paintEvent: scrolled cache by " << dx << endl;
1814 #endif
1815  } else {
1816  cacheRect = rect();
1817 #ifdef DEBUG_VIEW_WIDGET_PAINT
1818  cerr << "View(" << this << ")::paintEvent: scrolling too far" << endl;
1819 #endif
1820  }
1821  repaintCache = true;
1822 
1823  } else {
1824 #ifdef DEBUG_VIEW_WIDGET_PAINT
1825  cerr << "View(" << this << ")::paintEvent: cache is good" << endl;
1826 #endif
1827  paint.begin(this);
1828  paint.drawPixmap(cacheRect, *m_cache, cacheRect);
1829  paint.end();
1830  QFrame::paintEvent(e);
1831  paintedCacheRect = true;
1832  }
1833 
1836  }
1837 
1838 #ifdef DEBUG_VIEW_WIDGET_PAINT
1839 // cerr << "View(" << this << ")::paintEvent: cacheRect " << cacheRect << ", nonCacheRect " << (nonCacheRect | cacheRect) << ", repaintCache " << repaintCache << ", paintedCacheRect " << paintedCacheRect << endl;
1840 #endif
1841 
1842  // Scrollable (cacheable) items first
1843 
1844  if (!paintedCacheRect) {
1845 
1846  if (repaintCache) paint.begin(m_cache);
1847  else paint.begin(this);
1848  setPaintFont(paint);
1849  paint.setClipRect(cacheRect);
1850 
1851  paint.setPen(getBackground());
1852  paint.setBrush(getBackground());
1853  paint.drawRect(cacheRect);
1854 
1855  paint.setPen(getForeground());
1856  paint.setBrush(Qt::NoBrush);
1857 
1858  for (LayerList::iterator i = scrollables.begin(); i != scrollables.end(); ++i) {
1859  paint.setRenderHint(QPainter::Antialiasing, false);
1860  paint.save();
1861  (*i)->paint(this, paint, cacheRect);
1862  paint.restore();
1863  }
1864 
1865  if (haveSelections && selectionCacheable) {
1866  drawSelections(paint);
1867  m_selectionCached = repaintCache;
1868  }
1869 
1870  paint.end();
1871 
1872  if (repaintCache) {
1873  cacheRect |= (e ? e->rect() : rect());
1874  paint.begin(this);
1875  paint.drawPixmap(cacheRect, *m_cache, cacheRect);
1876  paint.end();
1877  }
1878  }
1879 
1880  // Now non-cacheable items. We always need to redraw the
1881  // non-cacheable items across at least the area we drew of the
1882  // cacheable items.
1883 
1884  nonCacheRect |= cacheRect;
1885 
1886  paint.begin(this);
1887  paint.setClipRect(nonCacheRect);
1888  setPaintFont(paint);
1889  if (scrollables.empty()) {
1890  paint.setPen(getBackground());
1891  paint.setBrush(getBackground());
1892  paint.drawRect(nonCacheRect);
1893  }
1894 
1895  paint.setPen(getForeground());
1896  paint.setBrush(Qt::NoBrush);
1897 
1898  for (LayerList::iterator i = nonScrollables.begin(); i != nonScrollables.end(); ++i) {
1899 // Profiler profiler2("View::paintEvent non-cacheable");
1900  (*i)->paint(this, paint, nonCacheRect);
1901  }
1902 
1903  paint.end();
1904 
1905  paint.begin(this);
1906  setPaintFont(paint);
1907  if (e) paint.setClipRect(e->rect());
1908  if (!m_selectionCached) {
1909  drawSelections(paint);
1910  }
1911  paint.end();
1912 
1913  bool showPlayPointer = true;
1915  showPlayPointer = false;
1916  } else if (m_playPointerFrame <= getStartFrame() ||
1918  showPlayPointer = false;
1919  } else if (m_manager && !m_manager->isPlaying()) {
1923  // Don't show the play pointer when it is redundant with
1924  // the centre line
1925  showPlayPointer = false;
1926  }
1927  }
1928 
1929  if (showPlayPointer) {
1930 
1931  paint.begin(this);
1932 
1933  int playx = getXForFrame(m_playPointerFrame);
1934 
1935  paint.setPen(getForeground());
1936  paint.drawLine(playx - 1, 0, playx - 1, height() - 1);
1937  paint.drawLine(playx + 1, 0, playx + 1, height() - 1);
1938  paint.drawPoint(playx, 0);
1939  paint.drawPoint(playx, height() - 1);
1940  paint.setPen(getBackground());
1941  paint.drawLine(playx, 1, playx, height() - 2);
1942 
1943  paint.end();
1944  }
1945 
1946  QFrame::paintEvent(e);
1947 }
1948 
1949 void
1950 View::drawSelections(QPainter &paint)
1951 {
1952  if (!hasTopLayerTimeXAxis()) return;
1953 
1954  MultiSelection::SelectionList selections;
1955 
1956  if (m_manager) {
1957  selections = m_manager->getSelections();
1959  bool exclusive;
1960  Selection inProgressSelection =
1961  m_manager->getInProgressSelection(exclusive);
1962  if (exclusive) selections.clear();
1963  selections.insert(inProgressSelection);
1964  }
1965  }
1966 
1967  paint.save();
1968 
1969  bool translucent = !areLayerColoursSignificant();
1970 
1971  if (translucent) {
1972  paint.setBrush(QColor(150, 150, 255, 80));
1973  } else {
1974  paint.setBrush(Qt::NoBrush);
1975  }
1976 
1977  int sampleRate = getModelsSampleRate();
1978 
1979  QPoint localPos;
1980  int illuminateFrame = -1;
1981  bool closeToLeft, closeToRight;
1982 
1983  if (shouldIlluminateLocalSelection(localPos, closeToLeft, closeToRight)) {
1984  illuminateFrame = getFrameForX(localPos.x());
1985  }
1986 
1987  const QFontMetrics &metrics = paint.fontMetrics();
1988 
1989  for (MultiSelection::SelectionList::iterator i = selections.begin();
1990  i != selections.end(); ++i) {
1991 
1992  int p0 = getXForFrame(alignFromReference(i->getStartFrame()));
1993  int p1 = getXForFrame(alignFromReference(i->getEndFrame()));
1994 
1995  if (p1 < 0 || p0 > width()) continue;
1996 
1997 #ifdef DEBUG_VIEW_WIDGET_PAINT
1998  SVDEBUG << "View::drawSelections: " << p0 << ",-1 [" << (p1-p0) << "x" << (height()+1) << "]" << endl;
1999 #endif
2000 
2001  bool illuminateThis =
2002  (illuminateFrame >= 0 && i->contains(illuminateFrame));
2003 
2004  paint.setPen(QColor(150, 150, 255));
2005 
2006  if (translucent && shouldLabelSelections()) {
2007  paint.drawRect(p0, -1, p1 - p0, height() + 1);
2008  } else {
2009  // Make the top & bottom lines of the box visible if we
2010  // are lacking some of the other visual cues. There's no
2011  // particular logic to this, it's just a question of what
2012  // I happen to think looks nice.
2013  paint.drawRect(p0, 0, p1 - p0, height() - 1);
2014  }
2015 
2016  if (illuminateThis) {
2017  paint.save();
2018  paint.setPen(QPen(getForeground(), 2));
2019  if (closeToLeft) {
2020  paint.drawLine(p0, 1, p1, 1);
2021  paint.drawLine(p0, 0, p0, height());
2022  paint.drawLine(p0, height() - 1, p1, height() - 1);
2023  } else if (closeToRight) {
2024  paint.drawLine(p0, 1, p1, 1);
2025  paint.drawLine(p1, 0, p1, height());
2026  paint.drawLine(p0, height() - 1, p1, height() - 1);
2027  } else {
2028  paint.setBrush(Qt::NoBrush);
2029  paint.drawRect(p0, 1, p1 - p0, height() - 2);
2030  }
2031  paint.restore();
2032  }
2033 
2034  if (sampleRate && shouldLabelSelections() && m_manager &&
2036 
2037  QString startText = QString("%1 / %2")
2038  .arg(QString::fromStdString
2039  (RealTime::frame2RealTime
2040  (i->getStartFrame(), sampleRate).toText(true)))
2041  .arg(i->getStartFrame());
2042 
2043  QString endText = QString(" %1 / %2")
2044  .arg(QString::fromStdString
2045  (RealTime::frame2RealTime
2046  (i->getEndFrame(), sampleRate).toText(true)))
2047  .arg(i->getEndFrame());
2048 
2049  QString durationText = QString("(%1 / %2) ")
2050  .arg(QString::fromStdString
2051  (RealTime::frame2RealTime
2052  (i->getEndFrame() - i->getStartFrame(), sampleRate)
2053  .toText(true)))
2054  .arg(i->getEndFrame() - i->getStartFrame());
2055 
2056  int sw = metrics.width(startText),
2057  ew = metrics.width(endText),
2058  dw = metrics.width(durationText);
2059 
2060  int sy = metrics.ascent() + metrics.height() + 4;
2061  int ey = sy;
2062  int dy = sy + metrics.height();
2063 
2064  int sx = p0 + 2;
2065  int ex = sx;
2066  int dx = sx;
2067 
2068  bool durationBothEnds = true;
2069 
2070  if (sw + ew > (p1 - p0)) {
2071  ey += metrics.height();
2072  dy += metrics.height();
2073  durationBothEnds = false;
2074  }
2075 
2076  if (ew < (p1 - p0)) {
2077  ex = p1 - 2 - ew;
2078  }
2079 
2080  if (dw < (p1 - p0)) {
2081  dx = p1 - 2 - dw;
2082  }
2083 
2084  paint.drawText(sx, sy, startText);
2085  paint.drawText(ex, ey, endText);
2086  paint.drawText(dx, dy, durationText);
2087  if (durationBothEnds) {
2088  paint.drawText(sx, dy, durationText);
2089  }
2090  }
2091  }
2092 
2093  paint.restore();
2094 }
2095 
2096 void
2097 View::drawMeasurementRect(QPainter &paint, const Layer *topLayer, QRect r,
2098  bool focus) const
2099 {
2100 // SVDEBUG << "View::drawMeasurementRect(" << r.x() << "," << r.y() << " "
2101 // << r.width() << "x" << r.height() << ")" << endl;
2102 
2103  if (r.x() + r.width() < 0 || r.x() >= width()) return;
2104 
2105  if (r.width() != 0 || r.height() != 0) {
2106  paint.save();
2107  if (focus) {
2108  paint.setPen(Qt::NoPen);
2109  QColor brushColour(Qt::black);
2110  brushColour.setAlpha(hasLightBackground() ? 15 : 40);
2111  paint.setBrush(brushColour);
2112  if (r.x() > 0) {
2113  paint.drawRect(0, 0, r.x(), height());
2114  }
2115  if (r.x() + r.width() < width()) {
2116  paint.drawRect(r.x() + r.width(), 0, width()-r.x()-r.width(), height());
2117  }
2118  if (r.y() > 0) {
2119  paint.drawRect(r.x(), 0, r.width(), r.y());
2120  }
2121  if (r.y() + r.height() < height()) {
2122  paint.drawRect(r.x(), r.y() + r.height(), r.width(), height()-r.y()-r.height());
2123  }
2124  paint.setBrush(Qt::NoBrush);
2125  }
2126  paint.setPen(Qt::green);
2127  paint.drawRect(r);
2128  paint.restore();
2129  } else {
2130  paint.save();
2131  paint.setPen(Qt::green);
2132  paint.drawPoint(r.x(), r.y());
2133  paint.restore();
2134  }
2135 
2136  if (!focus) return;
2137 
2138  paint.save();
2139  QFont fn = paint.font();
2140  if (fn.pointSize() > 8) {
2141  fn.setPointSize(fn.pointSize() - 1);
2142  paint.setFont(fn);
2143  }
2144 
2145  int fontHeight = paint.fontMetrics().height();
2146  int fontAscent = paint.fontMetrics().ascent();
2147 
2148  float v0, v1;
2149  QString u0, u1;
2150  bool b0 = false, b1 = false;
2151 
2152  QString axs, ays, bxs, bys, dxs, dys;
2153 
2154  int axx, axy, bxx, bxy, dxx, dxy;
2155  int aw = 0, bw = 0, dw = 0;
2156 
2157  int labelCount = 0;
2158 
2159  // top-left point, x-coord
2160 
2161  if ((b0 = topLayer->getXScaleValue(this, r.x(), v0, u0))) {
2162  axs = QString("%1 %2").arg(v0).arg(u0);
2163  if (u0 == "Hz" && Pitch::isFrequencyInMidiRange(v0)) {
2164  axs = QString("%1 (%2)").arg(axs)
2165  .arg(Pitch::getPitchLabelForFrequency(v0));
2166  }
2167  aw = paint.fontMetrics().width(axs);
2168  ++labelCount;
2169  }
2170 
2171  // bottom-right point, x-coord
2172 
2173  if (r.width() > 0) {
2174  if ((b1 = topLayer->getXScaleValue(this, r.x() + r.width(), v1, u1))) {
2175  bxs = QString("%1 %2").arg(v1).arg(u1);
2176  if (u1 == "Hz" && Pitch::isFrequencyInMidiRange(v1)) {
2177  bxs = QString("%1 (%2)").arg(bxs)
2178  .arg(Pitch::getPitchLabelForFrequency(v1));
2179  }
2180  bw = paint.fontMetrics().width(bxs);
2181  }
2182  }
2183 
2184  // dimension, width
2185 
2186  if (b0 && b1 && v1 != v0 && u0 == u1) {
2187  dxs = QString("[%1 %2]").arg(fabs(v1 - v0)).arg(u1);
2188  dw = paint.fontMetrics().width(dxs);
2189  }
2190 
2191  b0 = false;
2192  b1 = false;
2193 
2194  // top-left point, y-coord
2195 
2196  if ((b0 = topLayer->getYScaleValue(this, r.y(), v0, u0))) {
2197  ays = QString("%1 %2").arg(v0).arg(u0);
2198  if (u0 == "Hz" && Pitch::isFrequencyInMidiRange(v0)) {
2199  ays = QString("%1 (%2)").arg(ays)
2200  .arg(Pitch::getPitchLabelForFrequency(v0));
2201  }
2202  aw = std::max(aw, paint.fontMetrics().width(ays));
2203  ++labelCount;
2204  }
2205 
2206  // bottom-right point, y-coord
2207 
2208  if (r.height() > 0) {
2209  if ((b1 = topLayer->getYScaleValue(this, r.y() + r.height(), v1, u1))) {
2210  bys = QString("%1 %2").arg(v1).arg(u1);
2211  if (u1 == "Hz" && Pitch::isFrequencyInMidiRange(v1)) {
2212  bys = QString("%1 (%2)").arg(bys)
2213  .arg(Pitch::getPitchLabelForFrequency(v1));
2214  }
2215  bw = std::max(bw, paint.fontMetrics().width(bys));
2216  }
2217  }
2218 
2219  bool bd = false;
2220  float dy = 0.f;
2221  QString du;
2222 
2223  // dimension, height
2224 
2225  if ((bd = topLayer->getYScaleDifference(this, r.y(), r.y() + r.height(),
2226  dy, du)) &&
2227  dy != 0) {
2228  if (du != "") {
2229  if (du == "Hz") {
2230  int semis;
2231  float cents;
2232  semis = Pitch::getPitchForFrequencyDifference(v0, v1, &cents);
2233  dys = QString("[%1 %2 (%3)]")
2234  .arg(dy).arg(du)
2235  .arg(Pitch::getLabelForPitchRange(semis, cents));
2236  } else {
2237  dys = QString("[%1 %2]").arg(dy).arg(du);
2238  }
2239  } else {
2240  dys = QString("[%1]").arg(dy);
2241  }
2242  dw = std::max(dw, paint.fontMetrics().width(dys));
2243  }
2244 
2245  int mw = r.width();
2246  int mh = r.height();
2247 
2248  bool edgeLabelsInside = false;
2249  bool sizeLabelsInside = false;
2250 
2251  if (mw < std::max(aw, std::max(bw, dw)) + 4) {
2252  // defaults stand
2253  } else if (mw < aw + bw + 4) {
2254  if (mh > fontHeight * labelCount * 3 + 4) {
2255  edgeLabelsInside = true;
2256  sizeLabelsInside = true;
2257  } else if (mh > fontHeight * labelCount * 2 + 4) {
2258  edgeLabelsInside = true;
2259  }
2260  } else if (mw < aw + bw + dw + 4) {
2261  if (mh > fontHeight * labelCount * 3 + 4) {
2262  edgeLabelsInside = true;
2263  sizeLabelsInside = true;
2264  } else if (mh > fontHeight * labelCount + 4) {
2265  edgeLabelsInside = true;
2266  }
2267  } else {
2268  if (mh > fontHeight * labelCount + 4) {
2269  edgeLabelsInside = true;
2270  sizeLabelsInside = true;
2271  }
2272  }
2273 
2274  if (edgeLabelsInside) {
2275 
2276  axx = r.x() + 2;
2277  axy = r.y() + fontAscent + 2;
2278 
2279  bxx = r.x() + r.width() - bw - 2;
2280  bxy = r.y() + r.height() - (labelCount-1) * fontHeight - 2;
2281 
2282  } else {
2283 
2284  axx = r.x() - aw - 2;
2285  axy = r.y() + fontAscent;
2286 
2287  bxx = r.x() + r.width() + 2;
2288  bxy = r.y() + r.height() - (labelCount-1) * fontHeight;
2289  }
2290 
2291  dxx = r.width()/2 + r.x() - dw/2;
2292 
2293  if (sizeLabelsInside) {
2294 
2295  dxy = r.height()/2 + r.y() - (labelCount * fontHeight)/2 + fontAscent;
2296 
2297  } else {
2298 
2299  dxy = r.y() + r.height() + fontAscent + 2;
2300  }
2301 
2302  if (axs != "") {
2303  drawVisibleText(paint, axx, axy, axs, OutlinedText);
2304  axy += fontHeight;
2305  }
2306 
2307  if (ays != "") {
2308  drawVisibleText(paint, axx, axy, ays, OutlinedText);
2309  axy += fontHeight;
2310  }
2311 
2312  if (bxs != "") {
2313  drawVisibleText(paint, bxx, bxy, bxs, OutlinedText);
2314  bxy += fontHeight;
2315  }
2316 
2317  if (bys != "") {
2318  drawVisibleText(paint, bxx, bxy, bys, OutlinedText);
2319  bxy += fontHeight;
2320  }
2321 
2322  if (dxs != "") {
2323  drawVisibleText(paint, dxx, dxy, dxs, OutlinedText);
2324  dxy += fontHeight;
2325  }
2326 
2327  if (dys != "") {
2328  drawVisibleText(paint, dxx, dxy, dys, OutlinedText);
2329  dxy += fontHeight;
2330  }
2331 
2332  paint.restore();
2333 }
2334 
2335 bool
2336 View::render(QPainter &paint, int xorigin, int f0, int f1)
2337 {
2338  int x0 = f0 / m_zoomLevel;
2339  int x1 = f1 / m_zoomLevel;
2340 
2341  int w = x1 - x0;
2342 
2343  int origCentreFrame = m_centreFrame;
2344 
2345  bool someLayersIncomplete = false;
2346 
2347  for (LayerList::iterator i = m_layerStack.begin();
2348  i != m_layerStack.end(); ++i) {
2349 
2350  int c = (*i)->getCompletion(this);
2351  if (c < 100) {
2352  someLayersIncomplete = true;
2353  break;
2354  }
2355  }
2356 
2357  if (someLayersIncomplete) {
2358 
2359  QProgressDialog progress(tr("Waiting for layers to be ready..."),
2360  tr("Cancel"), 0, 100, this);
2361 
2362  int layerCompletion = 0;
2363 
2364  while (layerCompletion < 100) {
2365 
2366  for (LayerList::iterator i = m_layerStack.begin();
2367  i != m_layerStack.end(); ++i) {
2368 
2369  int c = (*i)->getCompletion(this);
2370  if (i == m_layerStack.begin() || c < layerCompletion) {
2371  layerCompletion = c;
2372  }
2373  }
2374 
2375  if (layerCompletion >= 100) break;
2376 
2377  progress.setValue(layerCompletion);
2378  qApp->processEvents();
2379  if (progress.wasCanceled()) {
2380  update();
2381  return false;
2382  }
2383 
2384  usleep(50000);
2385  }
2386  }
2387 
2388  QProgressDialog progress(tr("Rendering image..."),
2389  tr("Cancel"), 0, w / width(), this);
2390 
2391  for (int x = 0; x < w; x += width()) {
2392 
2393  progress.setValue(x / width());
2394  qApp->processEvents();
2395  if (progress.wasCanceled()) {
2396  m_centreFrame = origCentreFrame;
2397  update();
2398  return false;
2399  }
2400 
2401  m_centreFrame = f0 + (x + width()/2) * m_zoomLevel;
2402 
2403  QRect chunk(0, 0, width(), height());
2404 
2405  paint.setPen(getBackground());
2406  paint.setBrush(getBackground());
2407 
2408  paint.drawRect(QRect(xorigin + x, 0, width(), height()));
2409 
2410  paint.setPen(getForeground());
2411  paint.setBrush(Qt::NoBrush);
2412 
2413  for (LayerList::iterator i = m_layerStack.begin();
2414  i != m_layerStack.end(); ++i) {
2415  if(!((*i)->isLayerDormant(this))){
2416 
2417  paint.setRenderHint(QPainter::Antialiasing, false);
2418 
2419  paint.save();
2420  paint.translate(xorigin + x, 0);
2421 
2422  cerr << "Centre frame now: " << m_centreFrame << " drawing to " << chunk.x() + x + xorigin << ", " << chunk.width() << endl;
2423 
2424  (*i)->setSynchronousPainting(true);
2425 
2426  (*i)->paint(this, paint, chunk);
2427 
2428  (*i)->setSynchronousPainting(false);
2429 
2430  paint.restore();
2431  }
2432  }
2433  }
2434 
2435  m_centreFrame = origCentreFrame;
2436  update();
2437  return true;
2438 }
2439 
2440 QImage *
2442 {
2443  int f0 = getModelsStartFrame();
2444  int f1 = getModelsEndFrame();
2445 
2446  return toNewImage(f0, f1);
2447 }
2448 
2449 QImage *
2450 View::toNewImage(int f0, int f1)
2451 {
2452  int x0 = f0 / getZoomLevel();
2453  int x1 = f1 / getZoomLevel();
2454 
2455  QImage *image = new QImage(x1 - x0, height(), QImage::Format_RGB32);
2456 
2457  QPainter *paint = new QPainter(image);
2458  if (!render(*paint, 0, f0, f1)) {
2459  delete paint;
2460  delete image;
2461  return 0;
2462  } else {
2463  delete paint;
2464  return image;
2465  }
2466 }
2467 
2468 QSize
2470 {
2471  int f0 = getModelsStartFrame();
2472  int f1 = getModelsEndFrame();
2473 
2474  return getImageSize(f0, f1);
2475 }
2476 
2477 QSize
2478 View::getImageSize(int f0, int f1)
2479 {
2480  int x0 = f0 / getZoomLevel();
2481  int x1 = f1 / getZoomLevel();
2482 
2483  return QSize(x1 - x0, height());
2484 }
2485 
2486 void
2487 View::toXml(QTextStream &stream,
2488  QString indent, QString extraAttributes) const
2489 {
2490  stream << indent;
2491 
2492  stream << QString("<view "
2493  "centre=\"%1\" "
2494  "zoom=\"%2\" "
2495  "followPan=\"%3\" "
2496  "followZoom=\"%4\" "
2497  "tracking=\"%5\" "
2498  " %6>\n")
2499  .arg(m_centreFrame)
2500  .arg(m_zoomLevel)
2501  .arg(m_followPan)
2502  .arg(m_followZoom)
2503  .arg(m_followPlay == PlaybackScrollContinuous ? "scroll" :
2505  m_followPlay == PlaybackScrollPage ? "daw" :
2506  "ignore")
2507  .arg(extraAttributes);
2508 
2509  for (int i = 0; i < (int)m_fixedOrderLayers.size(); ++i) {
2510  bool visible = !m_fixedOrderLayers[i]->isLayerDormant(this);
2511  m_fixedOrderLayers[i]->toBriefXml(stream, indent + " ",
2512  QString("visible=\"%1\"")
2513  .arg(visible ? "true" : "false"));
2514  }
2515 
2516  stream << indent + "</view>\n";
2517 }
2518 
2520  m_v(v)
2521 {
2522 // cerr << "ViewPropertyContainer: " << this << " is owned by View " << v << endl;
2523  connect(m_v, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
2524  this, SIGNAL(propertyChanged(PropertyContainer::PropertyName)));
2525 }
2526 
2528 {
2529 }
int getFrameForX(int x) const
Return the closest frame to the given pixel x-coordinate.
Definition: View.cpp:363
virtual void globalCentreFrameChanged(int)
Definition: View.cpp:998
virtual void setDefaultColourFor(View *v)
int getPlaybackFrame() const
virtual const PropertyContainer * getPropertyContainer(int i) const
Definition: View.cpp:171
int getModelsSampleRate() const
Definition: View.cpp:1234
void propertyContainerNameChanged(PropertyContainer *pc)
The base class for visual representations of the data found in a Model.
Definition: Layer.h:52
int getZoomConstraintBlockSize(int blockSize, ZoomConstraint::RoundingDirection dir=ZoomConstraint::RoundNearest) const
Definition: View.cpp:1424
View scrolls continuously during playback, keeping the playback position at the centre.
Definition: ViewManager.h:39
void checkProgress(void *object)
Definition: View.cpp:1532
LayerList m_fixedOrderLayers
Definition: View.h:426
int m_zoomLevel
Definition: View.h:409
void propertyContainerSelected(PropertyContainer *pc)
std::set< Model * > ModelSet
Definition: View.h:317
QString m_lastError
Definition: View.h:429
int getGlobalZoom() const
bool m_followPlayIsDetached
Definition: View.h:413
bool m_followZoom
Definition: View.h:411
bool haveInProgressSelection() const
bool m_deleting
Definition: View.h:423
virtual void setProperty(const PropertyName &, int value)
Definition: View.cpp:148
void propertyContainerRemoved(PropertyContainer *pc)
virtual QColor getForeground() const
Definition: View.cpp:513
float getYForFrequency(float frequency, float minFreq, float maxFreq, bool logarithmic) const
Return the pixel y-coordinate corresponding to a given frequency, if the frequency range is as specif...
Definition: View.cpp:377
virtual bool getValueExtents(QString unit, float &min, float &max, bool &log) const
Definition: View.cpp:185
virtual int getLayerCount() const
Return the number of layers, regardless of whether visible or dormant, i.e.
Definition: View.h:166
int getModelsStartFrame() const
Definition: View.cpp:1190
virtual PropertyContainer::PropertyType getPropertyType(const PropertyName &) const
Definition: View.cpp:101
virtual void modelReplaced()
Definition: View.cpp:947
Model * getPlaybackModel() const
LayerList getNonScrollableFrontLayers(bool testChanged, bool &changed) const
Definition: View.cpp:1392
virtual void zoom(bool in)
Zoom in or out.
Definition: View.cpp:1475
virtual ~View()
Deleting a View does not delete any of its layers.
Definition: View.cpp:73
ProgressMap m_progressBars
Definition: View.h:442
virtual void viewCentreFrameChanged(View *, int)
Definition: View.cpp:1011
virtual int getPropertyRangeAndValue(const PropertyName &, int *min, int *max, int *deflt) const
Definition: View.cpp:110
int getAlignedPlaybackFrame() const
Definition: View.cpp:1331
QTimer * checkTimer
Definition: View.h:439
virtual void selectionChanged()
Definition: View.cpp:1161
std::vector< Layer * > LayerList
Definition: View.h:380
virtual QImage * toNewImage()
Definition: View.cpp:2441
bool isPlaying() const
void zoomLevelChanged(int, bool)
void propertyContainerPropertyChanged(PropertyContainer *pc)
virtual void paintEvent(QPaintEvent *e)
Definition: View.cpp:1662
LayerList getScrollableBackLayers(bool testChanged, bool &changed) const
Definition: View.cpp:1356
ColourSignificance
Definition: Layer.h:304
virtual void layerParameterRangesChanged()
Definition: View.cpp:977
virtual bool hasLightBackground() const
Definition: View.cpp:463
virtual void addLayer(Layer *v)
Add a layer to the view.
Definition: View.cpp:527
LayerList m_layerStack
Definition: View.h:425
virtual bool getYScaleValue(const View *, int, float &, QString &) const
Return the value and unit at the given y coordinate in the given view.
Definition: Layer.h:467
virtual int getTextLabelHeight(const Layer *layer, QPainter &) const
Definition: View.cpp:222
virtual void setViewManager(ViewManager *m)
Definition: View.cpp:692
int getZoomLevel() const
Return the zoom level, i.e.
Definition: View.cpp:443
void propertyContainerAdded(PropertyContainer *pc)
bool hasTopLayerTimeXAxis() const
Definition: View.cpp:1466
virtual void setPaintFont(QPainter &paint)
Definition: View.cpp:1654
ViewManager * m_manager
Definition: View.h:444
virtual int getFirstVisibleFrame() const
Definition: View.cpp:1172
int alignToReference(int) const
Definition: View.cpp:1322
View follows playback page-by-page, but dragging the view relocates playback to the centre frame.
Definition: ViewManager.h:46
virtual void modelAlignmentCompletionChanged()
Definition: View.cpp:938
virtual bool shouldIlluminateLocalSelection(QPoint &, bool &, bool &) const
Definition: View.h:263
Model * getAligningModel() const
!!
Definition: View.cpp:1273
void movePlayPointer(int f)
Definition: View.cpp:1037
virtual void cancelClicked()
Definition: View.cpp:1513
const Selection & getInProgressSelection(bool &exclusive) const
virtual QSize getImageSize()
Definition: View.cpp:2469
int getStartFrame() const
Retrieve the first visible sample frame on the widget.
Definition: View.cpp:302
int alignFromReference(int) const
Definition: View.cpp:1313
bool m_selectionCached
Definition: View.h:421
PlaybackFollowMode
Definition: ViewManager.h:33
virtual int getPropertyContainerCount() const
Definition: View.cpp:165
bool areLayersScrollable() const
Definition: View.cpp:1346
View is detached from playback.
Definition: ViewManager.h:59
virtual void modelCompletionChanged()
Definition: View.cpp:929
virtual void layerParametersChanged()
Definition: View.cpp:959
bool getPlaySelectionMode() const
Definition: ViewManager.h:150
virtual void modelChanged()
Definition: View.cpp:844
virtual void modelChangedWithin(int startFrame, int endFrame)
Definition: View.cpp:880
int m_centreFrame
Definition: View.h:408
virtual ~ViewPropertyContainer()
Definition: View.cpp:2527
int getEndFrame() const
Retrieve the last visible sample frame on the widget.
Definition: View.cpp:308
virtual QString getPropertyValueLabel(const PropertyName &, int value) const
Definition: View.cpp:133
QPushButton * cancel
Definition: View.h:436
bool shouldShowSelectionExtents() const
Definition: ViewManager.h:211
PropertyContainer::PropertyName PropertyName
Definition: View.h:270
QProgressBar * bar
Definition: View.h:437
bool m_haveSelectedLayer
Definition: View.h:427
virtual void drawSelections(QPainter &)
Definition: View.cpp:1950
int getProgressBarWidth() const
Definition: View.cpp:1641
virtual void layerNameChanged()
Definition: View.cpp:991
virtual Layer * getLayer(int n)
Return the nth layer, counted in stacking order.
Definition: View.h:174
PlaybackFollowMode m_followPlay
Definition: View.h:412
virtual bool isLayerOpaque() const
This should return true if the layer completely obscures any underlying layers.
Definition: Layer.h:302
virtual void progressCheckStalledTimerElapsed()
Definition: View.cpp:1622
int m_playPointerFrame
Definition: View.h:414
LayerList m_lastScrollableBackLayers
Definition: View.h:432
virtual void setFollowGlobalPan(bool f)
Definition: View.cpp:773
virtual bool getYScaleDifference(const View *v, int y0, int y1, float &diff, QString &unit) const
Return the difference between the values at the given y coordinates in the given view,...
Definition: Layer.cpp:155
virtual bool shouldLabelSelections() const
Definition: View.h:376
int getModelsEndFrame() const
Definition: View.cpp:1211
int m_cacheCentreFrame
Definition: View.h:419
void propertyContainerPropertyRangeChanged(PropertyContainer *pc)
virtual void setPlaybackFollow(PlaybackFollowMode m)
Definition: View.cpp:837
bool m_followPan
Definition: View.h:410
virtual const Model * getModel() const =0
View is the base class of widgets that display one or more overlaid views of data against a horizonta...
Definition: View.h:50
int m_cacheZoomLevel
Definition: View.h:420
virtual void removeLayer(Layer *v)
Remove a layer from the view.
Definition: View.cpp:594
The ViewManager manages properties that may need to be synchronised between separate Views.
Definition: ViewManager.h:73
virtual void toolModeChanged()
Definition: View.cpp:282
virtual void setZoomLevel(int z)
Set the zoom level, i.e.
Definition: View.cpp:452
virtual bool getXScaleValue(const View *v, int x, float &value, QString &unit) const
Return the value and unit at the given x coordinate in the given view.
Definition: Layer.cpp:142
virtual void zoomWheelsEnabledChanged()
Definition: View.cpp:296
View(QWidget *, bool showProgress)
Definition: View.cpp:51
virtual QString getPropertyLabel(const PropertyName &) const
Definition: View.cpp:92
bool m_showProgress
Definition: View.h:416
virtual void viewZoomLevelChanged(View *, int, bool)
Definition: View.cpp:1150
virtual void drawVisibleText(QPainter &p, int x, int y, QString text, TextStyle style) const
Definition: View.cpp:787
int getGlobalCentreFrame() const
LayerList m_lastNonScrollableBackLayers
Definition: View.h:433
bool shouldShowCentreLine() const
Definition: ViewManager.h:197
const MultiSelection::SelectionList & getSelections() const
int getCentreFrame() const
Return the centre frame of the visible widget.
Definition: View.h:81
virtual void viewManagerPlaybackFrameChanged(int)
Definition: View.cpp:1017
virtual Layer * getSelectedLayer()
Return the layer most recently selected by the user.
Definition: View.cpp:676
virtual void overlayModeChanged()
Definition: View.cpp:288
virtual void scroll(bool right, bool lots, bool doEmit=true)
Scroll left or right by a smallish or largish amount.
Definition: View.cpp:1493
virtual int getLastVisibleFrame() const
Definition: View.cpp:1181
virtual void setFollowGlobalZoom(bool f)
Definition: View.cpp:780
void setCentreFrame(int f)
Set the centre frame of the visible widget.
Definition: View.h:86
float getFrequencyForY(int y, float minFreq, float maxFreq, bool logarithmic) const
Return the closest frequency to the given pixel y-coordinate, if the frequency range is as specified.
Definition: View.cpp:411
View follows playback page-by-page, and the play head is moved (by the user) separately from dragging...
Definition: ViewManager.h:53
TextStyle
Definition: View.h:245
virtual bool render(QPainter &paint, int x0, int f0, int f1)
Definition: View.cpp:2336
virtual void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
Definition: View.cpp:2487
virtual PropertyContainer::PropertyList getProperties() const
Definition: View.cpp:82
void setStartFrame(int)
Set the widget pan based on the given first visible frame.
Definition: View.cpp:314
void layerModelChanged()
bool getAlignMode() const
Definition: ViewManager.h:156
virtual QColor getBackground() const
Definition: View.cpp:493
bool areLayerColoursSignificant() const
Definition: View.cpp:1455
virtual Layer * getInteractionLayer()
Return the layer currently active for tool interaction.
Definition: View.cpp:650
ModelSet getModels()
Definition: View.cpp:1251
virtual void layerMeasurementRectsChanged()
Definition: View.cpp:984
virtual bool isLayerDormant(const View *v) const
Return whether the layer is dormant (i.e.
Definition: Layer.cpp:126
QPixmap * m_cache
Definition: View.h:418
int getXForFrame(int frame) const
Return the pixel x-coordinate corresponding to a given sample frame (which may be negative).
Definition: View.cpp:357
virtual void drawMeasurementRect(QPainter &p, const Layer *, QRect rect, bool focus) const
Definition: View.cpp:2097
ViewPropertyContainer * m_propertyContainer
Definition: View.h:445
ViewPropertyContainer(View *v)
Definition: View.cpp:2519
void centreFrameChanged(int frame, bool globalScroll, PlaybackFollowMode followMode)
bool getGlobalDarkBackground() const