Chris@127: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@127: 
Chris@127: /*
Chris@127:     Sonic Visualiser
Chris@127:     An audio file viewer and annotation editor.
Chris@127:     Centre for Digital Music, Queen Mary, University of London.
Chris@127:     This file copyright 2006 Chris Cannam.
Chris@127:     
Chris@127:     This program is free software; you can redistribute it and/or
Chris@127:     modify it under the terms of the GNU General Public License as
Chris@127:     published by the Free Software Foundation; either version 2 of the
Chris@127:     License, or (at your option) any later version.  See the file
Chris@127:     COPYING included with this distribution for more information.
Chris@127: */
Chris@127: 
Chris@128: #include "View.h"
Chris@128: #include "layer/Layer.h"
Chris@128: #include "data/model/Model.h"
Chris@127: #include "base/ZoomConstraint.h"
Chris@127: #include "base/Profiler.h"
Chris@278: #include "base/Pitch.h"
Chris@338: #include "base/Preferences.h"
Chris@919: #include "ViewProxy.h"
Chris@127: 
Chris@301: #include "layer/TimeRulerLayer.h"
Chris@287: #include "layer/SingleColourLayer.h"
Chris@1078: #include "layer/PaintAssistant.h"
Chris@1078: 
Chris@301: #include "data/model/PowerOfSqrtTwoZoomConstraint.h"
Chris@301: #include "data/model/RangeSummarisableTimeValueModel.h"
Chris@127: 
Chris@797: #include "widgets/IconLoader.h"
Chris@797: 
Chris@127: #include <QPainter>
Chris@127: #include <QPaintEvent>
Chris@127: #include <QRect>
Chris@127: #include <QApplication>
Chris@226: #include <QProgressDialog>
Chris@316: #include <QTextStream>
Chris@338: #include <QFont>
Chris@583: #include <QMessageBox>
Chris@797: #include <QPushButton>
Chris@956: #include <QSettings>
Chris@1202: #include <QSvgGenerator>
Chris@127: 
Chris@127: #include <iostream>
Chris@127: #include <cassert>
Chris@520: #include <cmath>
Chris@127: 
Chris@1003: //#define DEBUG_VIEW 1
Chris@1003: //#define DEBUG_VIEW_WIDGET_PAINT 1
Chris@127: 
Chris@127: View::View(QWidget *w, bool showProgress) :
Chris@127:     QFrame(w),
Chris@1044:     m_id(getNextId()),
Chris@127:     m_centreFrame(0),
Chris@127:     m_zoomLevel(1024),
Chris@127:     m_followPan(true),
Chris@127:     m_followZoom(true),
Chris@815:     m_followPlay(PlaybackScrollPageWithCentre),
Chris@789:     m_followPlayIsDetached(false),
Chris@153:     m_playPointerFrame(0),
Chris@127:     m_showProgress(showProgress),
Chris@127:     m_cache(0),
Chris@952:     m_buffer(0),
Chris@127:     m_cacheCentreFrame(0),
Chris@127:     m_cacheZoomLevel(1024),
Chris@127:     m_selectionCached(false),
Chris@127:     m_deleting(false),
Chris@127:     m_haveSelectedLayer(false),
Chris@127:     m_manager(0),
Chris@127:     m_propertyContainer(new ViewPropertyContainer(this))
Chris@127: {
Chris@728: //    cerr << "View::View(" << this << ")" << endl;
Chris@127: }
Chris@127: 
Chris@127: View::~View()
Chris@127: {
Chris@728: //    cerr << "View::~View(" << this << ")" << endl;
Chris@127: 
Chris@127:     m_deleting = true;
Chris@127:     delete m_propertyContainer;
Chris@1215:     delete m_cache;
Chris@1215:     delete m_buffer;
Chris@127: }
Chris@127: 
Chris@127: PropertyContainer::PropertyList
Chris@127: View::getProperties() const
Chris@127: {
Chris@127:     PropertyContainer::PropertyList list;
Chris@127:     list.push_back("Global Scroll");
Chris@127:     list.push_back("Global Zoom");
Chris@127:     list.push_back("Follow Playback");
Chris@127:     return list;
Chris@127: }
Chris@127: 
Chris@127: QString
Chris@127: View::getPropertyLabel(const PropertyName &pn) const
Chris@127: {
Chris@127:     if (pn == "Global Scroll") return tr("Global Scroll");
Chris@127:     if (pn == "Global Zoom") return tr("Global Zoom");
Chris@127:     if (pn == "Follow Playback") return tr("Follow Playback");
Chris@127:     return "";
Chris@127: }
Chris@127: 
Chris@127: PropertyContainer::PropertyType
Chris@127: View::getPropertyType(const PropertyContainer::PropertyName &name) const
Chris@127: {
Chris@127:     if (name == "Global Scroll") return PropertyContainer::ToggleProperty;
Chris@127:     if (name == "Global Zoom") return PropertyContainer::ToggleProperty;
Chris@127:     if (name == "Follow Playback") return PropertyContainer::ValueProperty;
Chris@127:     return PropertyContainer::InvalidProperty;
Chris@127: }
Chris@127: 
Chris@127: int
Chris@127: View::getPropertyRangeAndValue(const PropertyContainer::PropertyName &name,
Chris@1266:                                int *min, int *max, int *deflt) const
Chris@127: {
Chris@216:     if (deflt) *deflt = 1;
Chris@127:     if (name == "Global Scroll") return m_followPan;
Chris@127:     if (name == "Global Zoom") return m_followZoom;
Chris@127:     if (name == "Follow Playback") {
Chris@1266:         if (min) *min = 0;
Chris@1266:         if (max) *max = 2;
Chris@815:         if (deflt) *deflt = int(PlaybackScrollPageWithCentre);
Chris@815:         switch (m_followPlay) {
Chris@815:         case PlaybackScrollContinuous: return 0;
Chris@815:         case PlaybackScrollPageWithCentre: case PlaybackScrollPage: return 1;
Chris@815:         case PlaybackIgnore: return 2;
Chris@815:         }
Chris@127:     }
Chris@127:     if (min) *min = 0;
Chris@127:     if (max) *max = 0;
Chris@216:     if (deflt) *deflt = 0;
Chris@127:     return 0;
Chris@127: }
Chris@127: 
Chris@127: QString
Chris@127: View::getPropertyValueLabel(const PropertyContainer::PropertyName &name,
Chris@1266:                             int value) const
Chris@127: {
Chris@127:     if (name == "Follow Playback") {
Chris@1266:         switch (value) {
Chris@1266:         default:
Chris@1266:         case 0: return tr("Scroll");
Chris@1266:         case 1: return tr("Page");
Chris@1266:         case 2: return tr("Off");
Chris@1266:         }
Chris@127:     }
Chris@127:     return tr("<unknown>");
Chris@127: }
Chris@127: 
Chris@127: void
Chris@127: View::setProperty(const PropertyContainer::PropertyName &name, int value)
Chris@127: {
Chris@127:     if (name == "Global Scroll") {
Chris@1266:         setFollowGlobalPan(value != 0);
Chris@127:     } else if (name == "Global Zoom") {
Chris@1266:         setFollowGlobalZoom(value != 0);
Chris@127:     } else if (name == "Follow Playback") {
Chris@1266:         switch (value) {
Chris@1266:         default:
Chris@1266:         case 0: setPlaybackFollow(PlaybackScrollContinuous); break;
Chris@1266:         case 1: setPlaybackFollow(PlaybackScrollPageWithCentre); break;
Chris@1266:         case 2: setPlaybackFollow(PlaybackIgnore); break;
Chris@1266:         }
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@806: int
Chris@127: View::getPropertyContainerCount() const
Chris@127: {
Chris@908:     return int(m_fixedOrderLayers.size()) + 1; // the 1 is for me
Chris@127: }
Chris@127: 
Chris@127: const PropertyContainer *
Chris@806: View::getPropertyContainer(int i) const
Chris@127: {
Chris@127:     return (const PropertyContainer *)(((View *)this)->
Chris@1266:                                        getPropertyContainer(i));
Chris@127: }
Chris@127: 
Chris@127: PropertyContainer *
Chris@806: View::getPropertyContainer(int i)
Chris@127: {
Chris@127:     if (i == 0) return m_propertyContainer;
Chris@837:     return m_fixedOrderLayers[i-1];
Chris@127: }
Chris@127: 
Chris@127: bool
Chris@904: View::getValueExtents(QString unit, double &min, double &max, bool &log) const
Chris@127: {
Chris@127:     bool have = false;
Chris@127: 
Chris@835:     for (LayerList::const_iterator i = m_layerStack.begin();
Chris@835:          i != m_layerStack.end(); ++i) { 
Chris@127: 
Chris@127:         QString layerUnit;
Chris@904:         double layerMin = 0.0, layerMax = 0.0;
Chris@904:         double displayMin = 0.0, displayMax = 0.0;
Chris@127:         bool layerLog = false;
Chris@127: 
Chris@127:         if ((*i)->getValueExtents(layerMin, layerMax, layerLog, layerUnit) &&
Chris@127:             layerUnit.toLower() == unit.toLower()) {
Chris@127: 
Chris@127:             if ((*i)->getDisplayExtents(displayMin, displayMax)) {
Chris@127: 
Chris@127:                 min = displayMin;
Chris@127:                 max = displayMax;
Chris@127:                 log = layerLog;
Chris@127:                 have = true;
Chris@127:                 break;
Chris@127: 
Chris@127:             } else {
Chris@127: 
Chris@127:                 if (!have || layerMin < min) min = layerMin;
Chris@127:                 if (!have || layerMax > max) max = layerMax;
Chris@127:                 if (layerLog) log = true;
Chris@127:                 have = true;
Chris@127:             }
Chris@127:         }
Chris@127:     }
Chris@127: 
Chris@127:     return have;
Chris@127: }
Chris@127: 
Chris@127: int
Chris@127: View::getTextLabelHeight(const Layer *layer, QPainter &paint) const
Chris@127: {
Chris@127:     std::map<int, Layer *> sortedLayers;
Chris@127: 
Chris@835:     for (LayerList::const_iterator i = m_layerStack.begin();
Chris@835:          i != m_layerStack.end(); ++i) { 
Chris@127:         if ((*i)->needsTextLabelHeight()) {
Chris@127:             sortedLayers[getObjectExportId(*i)] = *i;
Chris@127:         }
Chris@127:     }
Chris@127: 
Chris@1273:     int y = ViewManager::scalePixelSize(15) + paint.fontMetrics().ascent();
Chris@127: 
Chris@127:     for (std::map<int, Layer *>::const_iterator i = sortedLayers.begin();
Chris@127:          i != sortedLayers.end(); ++i) {
Chris@1273:         if (i->second == layer) break;
Chris@127:         y += paint.fontMetrics().height();
Chris@127:     }
Chris@127: 
Chris@127:     return y;
Chris@127: }
Chris@127: 
Chris@127: void
Chris@127: View::propertyContainerSelected(View *client, PropertyContainer *pc)
Chris@127: {
Chris@127:     if (client != this) return;
Chris@127:     
Chris@127:     if (pc == m_propertyContainer) {
Chris@1266:         if (m_haveSelectedLayer) {
Chris@1266:             m_haveSelectedLayer = false;
Chris@1266:             update();
Chris@1266:         }
Chris@1266:         return;
Chris@127:     }
Chris@127: 
Chris@127:     delete m_cache;
Chris@127:     m_cache = 0;
Chris@127: 
Chris@127:     Layer *selectedLayer = 0;
Chris@127: 
Chris@835:     for (LayerList::iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@1266:         if (*i == pc) {
Chris@1266:             selectedLayer = *i;
Chris@1266:             m_layerStack.erase(i);
Chris@1266:             break;
Chris@1266:         }
Chris@127:     }
Chris@127: 
Chris@127:     if (selectedLayer) {
Chris@1266:         m_haveSelectedLayer = true;
Chris@1266:         m_layerStack.push_back(selectedLayer);
Chris@1266:         update();
Chris@127:     } else {
Chris@1266:         m_haveSelectedLayer = false;
Chris@127:     }
Chris@298: 
Chris@298:     emit propertyContainerSelected(pc);
Chris@127: }
Chris@127: 
Chris@127: void
Chris@127: View::toolModeChanged()
Chris@127: {
Chris@587: //    SVDEBUG << "View::toolModeChanged(" << m_manager->getToolMode() << ")" << endl;
Chris@127: }
Chris@127: 
Chris@133: void
Chris@133: View::overlayModeChanged()
Chris@133: {
Chris@206:     delete m_cache;
Chris@206:     m_cache = 0;
Chris@133:     update();
Chris@133: }
Chris@133: 
Chris@133: void
Chris@133: View::zoomWheelsEnabledChanged()
Chris@133: {
Chris@133:     // subclass might override this
Chris@133: }
Chris@133: 
Chris@908: sv_frame_t
Chris@127: View::getStartFrame() const
Chris@127: {
Chris@313:     return getFrameForX(0);
Chris@127: }
Chris@127: 
Chris@908: sv_frame_t
Chris@127: View::getEndFrame() const
Chris@127: {
Chris@127:     return getFrameForX(width()) - 1;
Chris@127: }
Chris@127: 
Chris@127: void
Chris@908: View::setStartFrame(sv_frame_t f)
Chris@127: {
Chris@127:     setCentreFrame(f + m_zoomLevel * (width() / 2));
Chris@127: }
Chris@127: 
Chris@127: bool
Chris@908: View::setCentreFrame(sv_frame_t f, bool e)
Chris@127: {
Chris@127:     bool changeVisible = false;
Chris@127: 
Chris@127:     if (m_centreFrame != f) {
Chris@127: 
Chris@1266:         int formerPixel = int(m_centreFrame / m_zoomLevel);
Chris@1266: 
Chris@1266:         m_centreFrame = f;
Chris@1266: 
Chris@1266:         int newPixel = int(m_centreFrame / m_zoomLevel);
Chris@1266:         
Chris@1266:         if (newPixel != formerPixel) {
Chris@127: 
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266:             cout << "View(" << this << ")::setCentreFrame: newPixel " << newPixel << ", formerPixel " << formerPixel << endl;
Chris@127: #endif
Chris@1266:             update();
Chris@1266: 
Chris@1266:             changeVisible = true;
Chris@1266:         }
Chris@1266: 
Chris@1266:         if (e) {
Chris@908:             sv_frame_t rf = alignToReference(f);
Chris@830: #ifdef DEBUG_VIEW
Chris@682:             cerr << "View[" << this << "]::setCentreFrame(" << f
Chris@333:                       << "): emitting centreFrameChanged("
Chris@682:                       << rf << ")" << endl;
Chris@355: #endif
Chris@333:             emit centreFrameChanged(rf, m_followPan, m_followPlay);
Chris@333:         }
Chris@127:     }
Chris@127: 
Chris@127:     return changeVisible;
Chris@127: }
Chris@127: 
Chris@127: int
Chris@908: View::getXForFrame(sv_frame_t frame) const
Chris@127: {
Chris@908:     return int((frame - getStartFrame()) / m_zoomLevel);
Chris@127: }
Chris@127: 
Chris@908: sv_frame_t
Chris@127: View::getFrameForX(int x) const
Chris@127: {
Chris@1020:     sv_frame_t z = m_zoomLevel; // nb not just int, or multiplication may overflow
Chris@908:     sv_frame_t frame = m_centreFrame - (width()/2) * z;
Chris@354: 
Chris@1020:     frame = (frame / z) * z; // this is start frame
Chris@1020:     frame = frame + x * z;
Chris@1020: 
Chris@355: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1020:     cerr << "View::getFrameForX(" << x << "): z = " << z << ", m_centreFrame = " << m_centreFrame << ", width() = " << width() << ", frame = " << frame << endl;
Chris@355: #endif
Chris@354: 
Chris@1020:     return frame;
Chris@127: }
Chris@127: 
Chris@904: double
Chris@904: View::getYForFrequency(double frequency,
Chris@1266:                        double minf,
Chris@1266:                        double maxf, 
Chris@1266:                        bool logarithmic) const
Chris@127: {
Chris@382:     Profiler profiler("View::getYForFrequency");
Chris@382: 
Chris@127:     int h = height();
Chris@127: 
Chris@127:     if (logarithmic) {
Chris@127: 
Chris@1266:         static double lastminf = 0.0, lastmaxf = 0.0;
Chris@1266:         static double logminf = 0.0, logmaxf = 0.0;
Chris@1266: 
Chris@1266:         if (lastminf != minf) {
Chris@1266:             lastminf = (minf == 0.0 ? 1.0 : minf);
Chris@1266:             logminf = log10(minf);
Chris@1266:         }
Chris@1266:         if (lastmaxf != maxf) {
Chris@1266:             lastmaxf = (maxf < lastminf ? lastminf : maxf);
Chris@1266:             logmaxf = log10(maxf);
Chris@1266:         }
Chris@1266: 
Chris@1266:         if (logminf == logmaxf) return 0;
Chris@1266:         return h - (h * (log10(frequency) - logminf)) / (logmaxf - logminf);
Chris@127: 
Chris@127:     } else {
Chris@1266:         
Chris@1266:         if (minf == maxf) return 0;
Chris@1266:         return h - (h * (frequency - minf)) / (maxf - minf);
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@904: double
Chris@1085: View::getFrequencyForY(double y,
Chris@1266:                        double minf,
Chris@1266:                        double maxf,
Chris@1266:                        bool logarithmic) const
Chris@127: {
Chris@1085:     double h = height();
Chris@127: 
Chris@127:     if (logarithmic) {
Chris@127: 
Chris@1266:         static double lastminf = 0.0, lastmaxf = 0.0;
Chris@1266:         static double logminf = 0.0, logmaxf = 0.0;
Chris@1266: 
Chris@1266:         if (lastminf != minf) {
Chris@1266:             lastminf = (minf == 0.0 ? 1.0 : minf);
Chris@1266:             logminf = log10(minf);
Chris@1266:         }
Chris@1266:         if (lastmaxf != maxf) {
Chris@1266:             lastmaxf = (maxf < lastminf ? lastminf : maxf);
Chris@1266:             logmaxf = log10(maxf);
Chris@1266:         }
Chris@1266: 
Chris@1266:         if (logminf == logmaxf) return 0;
Chris@1266:         return pow(10.0, logminf + ((logmaxf - logminf) * (h - y)) / h);
Chris@127: 
Chris@127:     } else {
Chris@127: 
Chris@1266:         if (minf == maxf) return 0;
Chris@1266:         return minf + ((h - y) * (maxf - minf)) / h;
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@1183: ZoomLevel
Chris@127: View::getZoomLevel() const
Chris@127: {
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266: //        cout << "zoom level: " << m_zoomLevel << endl;
Chris@127: #endif
Chris@127:     return m_zoomLevel;
Chris@127: }
Chris@127: 
Chris@956: int
Chris@956: View::effectiveDevicePixelRatio() const
Chris@956: {
Chris@956: #ifdef Q_OS_MAC
Chris@956:     int dpratio = devicePixelRatio();
Chris@956:     if (dpratio > 1) {
Chris@956:         QSettings settings;
Chris@956:         settings.beginGroup("Preferences");
Chris@956:         if (!settings.value("scaledHiDpi", true).toBool()) {
Chris@956:             dpratio = 1;
Chris@956:         }
Chris@956:         settings.endGroup();
Chris@956:     }
Chris@956:     return dpratio;
Chris@956: #else
Chris@956:     return 1;
Chris@956: #endif
Chris@956: }
Chris@956: 
Chris@127: void
Chris@1183: View::setZoomLevel(ZoomLevel z)
Chris@127: {
Chris@956:     int dpratio = effectiveDevicePixelRatio();
Chris@950:     if (z < dpratio) return;
Chris@462:     if (z < 1) z = 1;
Chris@127:     if (m_zoomLevel != int(z)) {
Chris@1266:         m_zoomLevel = z;
Chris@1266:         emit zoomLevelChanged(z, m_followZoom);
Chris@1266:         update();
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@224: bool
Chris@224: View::hasLightBackground() const
Chris@224: {
Chris@287:     bool darkPalette = false;
Chris@292:     if (m_manager) darkPalette = m_manager->getGlobalDarkBackground();
Chris@287: 
Chris@287:     Layer::ColourSignificance maxSignificance = Layer::ColourAbsent;
Chris@287:     bool mostSignificantHasDarkBackground = false;
Chris@287:     
Chris@835:     for (LayerList::const_iterator i = m_layerStack.begin();
Chris@835:          i != m_layerStack.end(); ++i) {
Chris@287: 
Chris@287:         Layer::ColourSignificance s = (*i)->getLayerColourSignificance();
Chris@287:         bool light = (*i)->hasLightBackground();
Chris@287: 
Chris@287:         if (int(s) > int(maxSignificance)) {
Chris@287:             maxSignificance = s;
Chris@287:             mostSignificantHasDarkBackground = !light;
Chris@287:         } else if (s == maxSignificance && !light) {
Chris@287:             mostSignificantHasDarkBackground = true;
Chris@287:         }
Chris@224:     }
Chris@287: 
Chris@1314:     if (int(maxSignificance) >= int(Layer::ColourDistinguishes)) {
Chris@287:         return !mostSignificantHasDarkBackground;
Chris@287:     } else {
Chris@287:         return !darkPalette;
Chris@287:     }
Chris@287: }
Chris@287: 
Chris@287: QColor
Chris@287: View::getBackground() const
Chris@287: {
Chris@287:     bool light = hasLightBackground();
Chris@287: 
Chris@287:     QColor widgetbg = palette().window().color();
Chris@287:     bool widgetLight =
Chris@287:         (widgetbg.red() + widgetbg.green() + widgetbg.blue()) > 384;
Chris@287: 
Chris@296:     if (widgetLight == light) {
Chris@296:         if (widgetLight) {
Chris@296:             return widgetbg.light();
Chris@296:         } else {
Chris@296:             return widgetbg.dark();
Chris@296:         }
Chris@296:     }
Chris@287:     else if (light) return Qt::white;
Chris@287:     else return Qt::black;
Chris@287: }
Chris@287: 
Chris@287: QColor
Chris@287: View::getForeground() const
Chris@287: {
Chris@287:     bool light = hasLightBackground();
Chris@287: 
Chris@287:     QColor widgetfg = palette().text().color();
Chris@287:     bool widgetLight =
Chris@287:         (widgetfg.red() + widgetfg.green() + widgetfg.blue()) > 384;
Chris@287: 
Chris@287:     if (widgetLight != light) return widgetfg;
Chris@287:     else if (light) return Qt::black;
Chris@287:     else return Qt::white;
Chris@224: }
Chris@224: 
Chris@127: void
Chris@127: View::addLayer(Layer *layer)
Chris@127: {
Chris@127:     delete m_cache;
Chris@127:     m_cache = 0;
Chris@127: 
Chris@287:     SingleColourLayer *scl = dynamic_cast<SingleColourLayer *>(layer);
Chris@287:     if (scl) scl->setDefaultColourFor(this);
Chris@287: 
Chris@836:     m_fixedOrderLayers.push_back(layer);
Chris@835:     m_layerStack.push_back(layer);
Chris@127: 
Chris@555:     QProgressBar *pb = new QProgressBar(this);
Chris@555:     pb->setMinimum(0);
Chris@555:     pb->setMaximum(0);
Chris@555:     pb->setFixedWidth(80);
Chris@555:     pb->setTextVisible(false);
Chris@555: 
Chris@797:     QPushButton *cancel = new QPushButton(this);
Chris@797:     cancel->setIcon(IconLoader().load("fileclose"));
Chris@797:     cancel->setFlat(true);
Chris@797:     cancel->setFixedSize(QSize(20, 20));
Chris@797:     connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
Chris@797:     
Chris@555:     ProgressBarRec pbr;
Chris@797:     pbr.cancel = cancel;
Chris@555:     pbr.bar = pb;
Chris@555:     pbr.lastCheck = 0;
Chris@555:     pbr.checkTimer = new QTimer();
Chris@555:     connect(pbr.checkTimer, SIGNAL(timeout()), this,
Chris@555:             SLOT(progressCheckStalledTimerElapsed()));
Chris@555: 
Chris@555:     m_progressBars[layer] = pbr;
Chris@555: 
Chris@555:     QFont f(pb->font());
Chris@339:     int fs = Preferences::getInstance()->getViewFontSize();
Chris@339:     f.setPointSize(std::min(fs, int(ceil(fs * 0.85))));
Chris@339: 
Chris@797:     cancel->hide();
Chris@797: 
Chris@555:     pb->setFont(f);
Chris@555:     pb->hide();
Chris@127:     
Chris@127:     connect(layer, SIGNAL(layerParametersChanged()),
Chris@1266:             this,    SLOT(layerParametersChanged()));
Chris@197:     connect(layer, SIGNAL(layerParameterRangesChanged()),
Chris@1266:             this,    SLOT(layerParameterRangesChanged()));
Chris@268:     connect(layer, SIGNAL(layerMeasurementRectsChanged()),
Chris@1266:             this,    SLOT(layerMeasurementRectsChanged()));
Chris@127:     connect(layer, SIGNAL(layerNameChanged()),
Chris@1266:             this,    SLOT(layerNameChanged()));
Chris@127:     connect(layer, SIGNAL(modelChanged()),
Chris@1266:             this,    SLOT(modelChanged()));
Chris@127:     connect(layer, SIGNAL(modelCompletionChanged()),
Chris@1266:             this,    SLOT(modelCompletionChanged()));
Chris@320:     connect(layer, SIGNAL(modelAlignmentCompletionChanged()),
Chris@1266:             this,    SLOT(modelAlignmentCompletionChanged()));
Chris@908:     connect(layer, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@1266:             this,    SLOT(modelChangedWithin(sv_frame_t, sv_frame_t)));
Chris@127:     connect(layer, SIGNAL(modelReplaced()),
Chris@1266:             this,    SLOT(modelReplaced()));
Chris@127: 
Chris@127:     update();
Chris@127: 
Chris@127:     emit propertyContainerAdded(layer);
Chris@127: }
Chris@127: 
Chris@127: void
Chris@127: View::removeLayer(Layer *layer)
Chris@127: {
Chris@127:     if (m_deleting) {
Chris@1266:         return;
Chris@127:     }
Chris@127: 
Chris@127:     delete m_cache;
Chris@127:     m_cache = 0;
Chris@127: 
Chris@836:     for (LayerList::iterator i = m_fixedOrderLayers.begin();
Chris@836:          i != m_fixedOrderLayers.end();
Chris@836:          ++i) {
Chris@1266:         if (*i == layer) {
Chris@1266:             m_fixedOrderLayers.erase(i);
Chris@836:             break;
Chris@836:         }
Chris@836:     }
Chris@836: 
Chris@836:     for (LayerList::iterator i = m_layerStack.begin(); 
Chris@836:          i != m_layerStack.end();
Chris@836:          ++i) {
Chris@1266:         if (*i == layer) {
Chris@1266:             m_layerStack.erase(i);
Chris@1266:             if (m_progressBars.find(layer) != m_progressBars.end()) {
Chris@1266:                 delete m_progressBars[layer].bar;
Chris@797:                 delete m_progressBars[layer].cancel;
Chris@1266:                 delete m_progressBars[layer].checkTimer;
Chris@1266:                 m_progressBars.erase(layer);
Chris@1266:             }
Chris@1266:             break;
Chris@1266:         }
Chris@127:     }
Chris@127:     
Chris@197:     disconnect(layer, SIGNAL(layerParametersChanged()),
Chris@197:                this,    SLOT(layerParametersChanged()));
Chris@197:     disconnect(layer, SIGNAL(layerParameterRangesChanged()),
Chris@197:                this,    SLOT(layerParameterRangesChanged()));
Chris@197:     disconnect(layer, SIGNAL(layerNameChanged()),
Chris@197:                this,    SLOT(layerNameChanged()));
Chris@197:     disconnect(layer, SIGNAL(modelChanged()),
Chris@197:                this,    SLOT(modelChanged()));
Chris@197:     disconnect(layer, SIGNAL(modelCompletionChanged()),
Chris@197:                this,    SLOT(modelCompletionChanged()));
Chris@320:     disconnect(layer, SIGNAL(modelAlignmentCompletionChanged()),
Chris@320:                this,    SLOT(modelAlignmentCompletionChanged()));
Chris@908:     disconnect(layer, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@908:                this,    SLOT(modelChangedWithin(sv_frame_t, sv_frame_t)));
Chris@197:     disconnect(layer, SIGNAL(modelReplaced()),
Chris@197:                this,    SLOT(modelReplaced()));
Chris@197: 
Chris@127:     update();
Chris@127: 
Chris@127:     emit propertyContainerRemoved(layer);
Chris@127: }
Chris@127: 
Chris@127: Layer *
Chris@834: View::getInteractionLayer()
Chris@834: {
Chris@834:     Layer *sl = getSelectedLayer();
Chris@834:     if (sl && !(sl->isLayerDormant(this))) {
Chris@834:         return sl;
Chris@834:     }
Chris@835:     if (!m_layerStack.empty()) {
Chris@834:         int n = getLayerCount();
Chris@834:         while (n > 0) {
Chris@834:             --n;
Chris@834:             Layer *layer = getLayer(n);
Chris@834:             if (!(layer->isLayerDormant(this))) {
Chris@834:                 return layer;
Chris@834:             }
Chris@834:         }
Chris@834:     }
Chris@834:     return 0;
Chris@834: }
Chris@834: 
Chris@841: const Layer *
Chris@841: View::getInteractionLayer() const
Chris@841: {
Chris@841:     return const_cast<const Layer *>(const_cast<View *>(this)->getInteractionLayer());
Chris@841: }
Chris@841: 
Chris@834: Layer *
Chris@127: View::getSelectedLayer()
Chris@127: {
Chris@835:     if (m_haveSelectedLayer && !m_layerStack.empty()) {
Chris@839:         return getLayer(getLayerCount() - 1);
Chris@127:     } else {
Chris@1266:         return 0;
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@127: const Layer *
Chris@127: View::getSelectedLayer() const
Chris@127: {
Chris@127:     return const_cast<const Layer *>(const_cast<View *>(this)->getSelectedLayer());
Chris@127: }
Chris@127: 
Chris@127: void
Chris@127: View::setViewManager(ViewManager *manager)
Chris@127: {
Chris@127:     if (m_manager) {
Chris@1266:         m_manager->disconnect(this, SLOT(globalCentreFrameChanged(sv_frame_t)));
Chris@1266:         m_manager->disconnect(this, SLOT(viewCentreFrameChanged(View *, sv_frame_t)));
Chris@1266:         m_manager->disconnect(this, SLOT(viewManagerPlaybackFrameChanged(sv_frame_t)));
Chris@1324:         m_manager->disconnect(this, SLOT(viewZoomLevelChanged(View *, ZoomLevel, bool)));
Chris@211:         m_manager->disconnect(this, SLOT(toolModeChanged()));
Chris@211:         m_manager->disconnect(this, SLOT(selectionChanged()));
Chris@211:         m_manager->disconnect(this, SLOT(overlayModeChanged()));
Chris@211:         m_manager->disconnect(this, SLOT(zoomWheelsEnabledChanged()));
Chris@908:         disconnect(m_manager, SLOT(viewCentreFrameChanged(sv_frame_t, bool, PlaybackFollowMode)));
Chris@1324:         disconnect(m_manager, SLOT(zoomLevelChanged(ZoomLevel, bool)));
Chris@127:     }
Chris@127: 
Chris@127:     m_manager = manager;
Chris@127: 
Chris@908:     connect(m_manager, SIGNAL(globalCentreFrameChanged(sv_frame_t)),
Chris@1266:             this, SLOT(globalCentreFrameChanged(sv_frame_t)));
Chris@908:     connect(m_manager, SIGNAL(viewCentreFrameChanged(View *, sv_frame_t)),
Chris@1266:             this, SLOT(viewCentreFrameChanged(View *, sv_frame_t)));
Chris@908:     connect(m_manager, SIGNAL(playbackFrameChanged(sv_frame_t)),
Chris@1266:             this, SLOT(viewManagerPlaybackFrameChanged(sv_frame_t)));
Chris@806: 
Chris@1183:     connect(m_manager, SIGNAL(viewZoomLevelChanged(View *, ZoomLevel, bool)),
Chris@1324:             this, SLOT(viewZoomLevelChanged(View *, ZoomLevel, bool)));
Chris@211: 
Chris@127:     connect(m_manager, SIGNAL(toolModeChanged()),
Chris@1266:             this, SLOT(toolModeChanged()));
Chris@127:     connect(m_manager, SIGNAL(selectionChanged()),
Chris@1266:             this, SLOT(selectionChanged()));
Chris@127:     connect(m_manager, SIGNAL(inProgressSelectionChanged()),
Chris@1266:             this, SLOT(selectionChanged()));
Chris@127:     connect(m_manager, SIGNAL(overlayModeChanged()),
Chris@133:             this, SLOT(overlayModeChanged()));
Chris@607:     connect(m_manager, SIGNAL(showCentreLineChanged()),
Chris@607:             this, SLOT(overlayModeChanged()));
Chris@133:     connect(m_manager, SIGNAL(zoomWheelsEnabledChanged()),
Chris@133:             this, SLOT(zoomWheelsEnabledChanged()));
Chris@127: 
Chris@908:     connect(this, SIGNAL(centreFrameChanged(sv_frame_t, bool,
Chris@211:                                             PlaybackFollowMode)),
Chris@908:             m_manager, SLOT(viewCentreFrameChanged(sv_frame_t, bool,
Chris@211:                                                    PlaybackFollowMode)));
Chris@211: 
Chris@1183:     connect(this, SIGNAL(zoomLevelChanged(ZoomLevel, bool)),
Chris@1324:             m_manager, SLOT(viewZoomLevelChanged(ZoomLevel, bool)));
Chris@127: 
Chris@815:     switch (m_followPlay) {
Chris@815:         
Chris@815:     case PlaybackScrollPage:
Chris@815:     case PlaybackScrollPageWithCentre:
Chris@364:         setCentreFrame(m_manager->getGlobalCentreFrame(), false);
Chris@815:         break;
Chris@815: 
Chris@815:     case PlaybackScrollContinuous:
Chris@236:         setCentreFrame(m_manager->getPlaybackFrame(), false);
Chris@815:         break;
Chris@815: 
Chris@815:     case PlaybackIgnore:
Chris@815:         if (m_followPan) {
Chris@815:             setCentreFrame(m_manager->getGlobalCentreFrame(), false);
Chris@815:         }
Chris@815:         break;
Chris@236:     }
Chris@516: 
Chris@236:     if (m_followZoom) setZoomLevel(m_manager->getGlobalZoom());
Chris@236: 
Chris@511:     movePlayPointer(getAlignedPlaybackFrame());
Chris@511: 
Chris@127:     toolModeChanged();
Chris@127: }
Chris@127: 
Chris@127: void
Chris@908: View::setViewManager(ViewManager *vm, sv_frame_t initialCentreFrame)
Chris@516: {
Chris@516:     setViewManager(vm);
Chris@516:     setCentreFrame(initialCentreFrame, false);
Chris@516: }
Chris@516: 
Chris@516: void
Chris@127: View::setFollowGlobalPan(bool f)
Chris@127: {
Chris@127:     m_followPan = f;
Chris@127:     emit propertyContainerPropertyChanged(m_propertyContainer);
Chris@127: }
Chris@127: 
Chris@127: void
Chris@127: View::setFollowGlobalZoom(bool f)
Chris@127: {
Chris@127:     m_followZoom = f;
Chris@127:     emit propertyContainerPropertyChanged(m_propertyContainer);
Chris@127: }
Chris@127: 
Chris@127: void
Chris@127: View::setPlaybackFollow(PlaybackFollowMode m)
Chris@127: {
Chris@127:     m_followPlay = m;
Chris@127:     emit propertyContainerPropertyChanged(m_propertyContainer);
Chris@127: }
Chris@127: 
Chris@127: void
Chris@127: View::modelChanged()
Chris@127: {
Chris@127:     QObject *obj = sender();
Chris@127: 
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@682:     cerr << "View(" << this << ")::modelChanged()" << endl;
Chris@127: #endif
Chris@127:     
Chris@127:     // If the model that has changed is not used by any of the cached
Chris@127:     // layers, we won't need to recreate the cache
Chris@127:     
Chris@127:     bool recreate = false;
Chris@127: 
Chris@127:     bool discard;
Chris@127:     LayerList scrollables = getScrollableBackLayers(false, discard);
Chris@127:     for (LayerList::const_iterator i = scrollables.begin();
Chris@1266:          i != scrollables.end(); ++i) {
Chris@1266:         if (*i == obj || (*i)->getModel() == obj) {
Chris@1266:             recreate = true;
Chris@1266:             break;
Chris@1266:         }
Chris@127:     }
Chris@127: 
Chris@127:     if (recreate) {
Chris@1266:         delete m_cache;
Chris@1266:         m_cache = 0;
Chris@127:     }
Chris@127: 
Chris@336:     emit layerModelChanged();
Chris@336: 
Chris@127:     checkProgress(obj);
Chris@127: 
Chris@127:     update();
Chris@127: }
Chris@127: 
Chris@127: void
Chris@908: View::modelChangedWithin(sv_frame_t startFrame, sv_frame_t endFrame)
Chris@127: {
Chris@127:     QObject *obj = sender();
Chris@127: 
Chris@908:     sv_frame_t myStartFrame = getStartFrame();
Chris@908:     sv_frame_t myEndFrame = getEndFrame();
Chris@127: 
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@806:     cerr << "View(" << this << ")::modelChangedWithin(" << startFrame << "," << endFrame << ") [me " << myStartFrame << "," << myEndFrame << "]" << endl;
Chris@127: #endif
Chris@127: 
Chris@908:     if (myStartFrame > 0 && endFrame < myStartFrame) {
Chris@1266:         checkProgress(obj);
Chris@1266:         return;
Chris@127:     }
Chris@127:     if (startFrame > myEndFrame) {
Chris@1266:         checkProgress(obj);
Chris@1266:         return;
Chris@127:     }
Chris@127: 
Chris@127:     // If the model that has changed is not used by any of the cached
Chris@127:     // layers, we won't need to recreate the cache
Chris@127:     
Chris@127:     bool recreate = false;
Chris@127: 
Chris@127:     bool discard;
Chris@127:     LayerList scrollables = getScrollableBackLayers(false, discard);
Chris@127:     for (LayerList::const_iterator i = scrollables.begin();
Chris@1266:          i != scrollables.end(); ++i) {
Chris@1266:         if (*i == obj || (*i)->getModel() == obj) {
Chris@1266:             recreate = true;
Chris@1266:             break;
Chris@1266:         }
Chris@127:     }
Chris@127: 
Chris@127:     if (recreate) {
Chris@1266:         delete m_cache;
Chris@1266:         m_cache = 0;
Chris@127:     }
Chris@127: 
Chris@806:     if (startFrame < myStartFrame) startFrame = myStartFrame;
Chris@127:     if (endFrame > myEndFrame) endFrame = myEndFrame;
Chris@127: 
Chris@127:     checkProgress(obj);
Chris@127: 
Chris@127:     update();
Chris@127: }    
Chris@127: 
Chris@127: void
Chris@127: View::modelCompletionChanged()
Chris@127: {
Chris@682: //    cerr << "View(" << this << ")::modelCompletionChanged()" << endl;
Chris@301: 
Chris@127:     QObject *obj = sender();
Chris@127:     checkProgress(obj);
Chris@127: }
Chris@127: 
Chris@127: void
Chris@320: View::modelAlignmentCompletionChanged()
Chris@320: {
Chris@682: //    cerr << "View(" << this << ")::modelAlignmentCompletionChanged()" << endl;
Chris@320: 
Chris@320:     QObject *obj = sender();
Chris@320:     checkProgress(obj);
Chris@320: }
Chris@320: 
Chris@320: void
Chris@127: View::modelReplaced()
Chris@127: {
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@682:     cerr << "View(" << this << ")::modelReplaced()" << endl;
Chris@127: #endif
Chris@127:     delete m_cache;
Chris@127:     m_cache = 0;
Chris@127: 
Chris@127:     update();
Chris@127: }
Chris@127: 
Chris@127: void
Chris@127: View::layerParametersChanged()
Chris@127: {
Chris@127:     Layer *layer = dynamic_cast<Layer *>(sender());
Chris@127: 
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@587:     SVDEBUG << "View::layerParametersChanged()" << endl;
Chris@127: #endif
Chris@127: 
Chris@127:     delete m_cache;
Chris@127:     m_cache = 0;
Chris@127:     update();
Chris@127: 
Chris@127:     if (layer) {
Chris@1266:         emit propertyContainerPropertyChanged(layer);
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@127: void
Chris@197: View::layerParameterRangesChanged()
Chris@197: {
Chris@197:     Layer *layer = dynamic_cast<Layer *>(sender());
Chris@197:     if (layer) emit propertyContainerPropertyRangeChanged(layer);
Chris@197: }
Chris@197: 
Chris@197: void
Chris@268: View::layerMeasurementRectsChanged()
Chris@268: {
Chris@268:     Layer *layer = dynamic_cast<Layer *>(sender());
Chris@268:     if (layer) update();
Chris@268: }
Chris@268: 
Chris@268: void
Chris@127: View::layerNameChanged()
Chris@127: {
Chris@127:     Layer *layer = dynamic_cast<Layer *>(sender());
Chris@127:     if (layer) emit propertyContainerNameChanged(layer);
Chris@127: }
Chris@127: 
Chris@127: void
Chris@908: View::globalCentreFrameChanged(sv_frame_t rf)
Chris@127: {
Chris@211:     if (m_followPan) {
Chris@908:         sv_frame_t f = alignFromReference(rf);
Chris@830: #ifdef DEBUG_VIEW
Chris@682:         cerr << "View[" << this << "]::globalCentreFrameChanged(" << rf
Chris@682:                   << "): setting centre frame to " << f << endl;
Chris@363: #endif
Chris@333:         setCentreFrame(f, false);
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@127: void
Chris@908: View::viewCentreFrameChanged(View *, sv_frame_t )
Chris@211: {
Chris@211:     // We do nothing with this, but a subclass might
Chris@211: }
Chris@211: 
Chris@211: void
Chris@908: View::viewManagerPlaybackFrameChanged(sv_frame_t f)
Chris@127: {
Chris@127:     if (m_manager) {
Chris@1266:         if (sender() != m_manager) return;
Chris@127:     }
Chris@127: 
Chris@830: #ifdef DEBUG_VIEW        
Chris@830:     cerr << "View::viewManagerPlaybackFrameChanged(" << f << ")" << endl;
Chris@830: #endif
Chris@830: 
Chris@301:     f = getAlignedPlaybackFrame();
Chris@301: 
Chris@830: #ifdef DEBUG_VIEW
Chris@951:     cerr << " -> aligned frame = " << f << endl;
Chris@830: #endif
Chris@830: 
Chris@511:     movePlayPointer(f);
Chris@511: }
Chris@511: 
Chris@511: void
Chris@908: View::movePlayPointer(sv_frame_t newFrame)
Chris@511: {
Chris@830: #ifdef DEBUG_VIEW
Chris@830:     cerr << "View(" << this << ")::movePlayPointer(" << newFrame << ")" << endl;
Chris@830: #endif
Chris@830: 
Chris@511:     if (m_playPointerFrame == newFrame) return;
Chris@511:     bool visibleChange =
Chris@511:         (getXForFrame(m_playPointerFrame) != getXForFrame(newFrame));
Chris@908:     sv_frame_t oldPlayPointerFrame = m_playPointerFrame;
Chris@511:     m_playPointerFrame = newFrame;
Chris@511:     if (!visibleChange) return;
Chris@127: 
Chris@513:     bool somethingGoingOn =
Chris@513:         ((QApplication::mouseButtons() != Qt::NoButton) ||
Chris@513:          (QApplication::keyboardModifiers() & Qt::AltModifier));
Chris@513: 
Chris@789:     bool pointerInVisibleArea =
Chris@1266:         long(m_playPointerFrame) >= getStartFrame() &&
Chris@789:         (m_playPointerFrame < getEndFrame() ||
Chris@789:          // include old pointer location so we know to refresh when moving out
Chris@789:          oldPlayPointerFrame < getEndFrame());
Chris@789: 
Chris@127:     switch (m_followPlay) {
Chris@127: 
Chris@127:     case PlaybackScrollContinuous:
Chris@1266:         if (!somethingGoingOn) {
Chris@1266:             setCentreFrame(m_playPointerFrame, false);
Chris@1266:         }
Chris@1266:         break;
Chris@127: 
Chris@127:     case PlaybackScrollPage:
Chris@815:     case PlaybackScrollPageWithCentre:
Chris@789: 
Chris@789:         if (!pointerInVisibleArea && somethingGoingOn) {
Chris@789: 
Chris@789:             m_followPlayIsDetached = true;
Chris@789: 
Chris@789:         } else if (!pointerInVisibleArea && m_followPlayIsDetached) {
Chris@789: 
Chris@789:             // do nothing; we aren't tracking until the pointer comes back in
Chris@789: 
Chris@789:         } else {
Chris@789: 
Chris@789:             int xold = getXForFrame(oldPlayPointerFrame);
Chris@789:             update(xold - 4, 0, 9, height());
Chris@789: 
Chris@908:             sv_frame_t w = getEndFrame() - getStartFrame();
Chris@789:             w -= w/5;
Chris@908:             sv_frame_t sf = (m_playPointerFrame / w) * w - w/8;
Chris@789: 
Chris@789:             if (m_manager &&
Chris@789:                 m_manager->isPlaying() &&
Chris@789:                 m_manager->getPlaySelectionMode()) {
Chris@789:                 MultiSelection::SelectionList selections = m_manager->getSelections();
Chris@789:                 if (!selections.empty()) {
Chris@908:                     sv_frame_t selectionStart = selections.begin()->getStartFrame();
Chris@808:                     if (sf < selectionStart - w / 10) {
Chris@808:                         sf = selectionStart - w / 10;
Chris@789:                     }
Chris@789:                 }
Chris@789:             }
Chris@127: 
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@789:             cerr << "PlaybackScrollPage: f = " << m_playPointerFrame << ", sf = " << sf << ", start frame "
Chris@789:                  << getStartFrame() << endl;
Chris@127: #endif
Chris@127: 
Chris@789:             // We don't consider scrolling unless the pointer is outside
Chris@789:             // the central visible range already
Chris@789: 
Chris@789:             int xnew = getXForFrame(m_playPointerFrame);
Chris@127: 
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@789:             cerr << "xnew = " << xnew << ", width = " << width() << endl;
Chris@127: #endif
Chris@127: 
Chris@789:             bool shouldScroll = (xnew > (width() * 7) / 8);
Chris@789: 
Chris@789:             if (!m_followPlayIsDetached && (xnew < width() / 8)) {
Chris@789:                 shouldScroll = true;
Chris@789:             }
Chris@789: 
Chris@789:             if (xnew > width() / 8) {
Chris@789:                 m_followPlayIsDetached = false;
Chris@791:             } else if (somethingGoingOn) {
Chris@791:                 m_followPlayIsDetached = true;
Chris@789:             }
Chris@789: 
Chris@789:             if (!somethingGoingOn && shouldScroll) {
Chris@908:                 sv_frame_t offset = getFrameForX(width()/2) - getStartFrame();
Chris@908:                 sv_frame_t newCentre = sf + offset;
Chris@789:                 bool changed = setCentreFrame(newCentre, false);
Chris@789:                 if (changed) {
Chris@789:                     xold = getXForFrame(oldPlayPointerFrame);
Chris@789:                     update(xold - 4, 0, 9, height());
Chris@789:                 }
Chris@789:             }
Chris@789: 
Chris@789:             update(xnew - 4, 0, 9, height());
Chris@789:         }
Chris@789:         break;
Chris@127: 
Chris@127:     case PlaybackIgnore:
Chris@1266:         if (m_playPointerFrame >= getStartFrame() &&
Chris@511:             m_playPointerFrame < getEndFrame()) {
Chris@1266:             update();
Chris@1266:         }
Chris@1266:         break;
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@127: void
Chris@1183: View::viewZoomLevelChanged(View *p, ZoomLevel z, bool locked)
Chris@127: {
Chris@244: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@682:     cerr  << "View[" << this << "]: viewZoomLevelChanged(" << p << ", " << z << ", " << locked << ")" << endl;
Chris@244: #endif
Chris@127:     if (m_followZoom && p != this && locked) {
Chris@222:         setZoomLevel(z);
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@127: void
Chris@127: View::selectionChanged()
Chris@127: {
Chris@127:     if (m_selectionCached) {
Chris@1266:         delete m_cache;
Chris@1266:         m_cache = 0;
Chris@1266:         m_selectionCached = false;
Chris@127:     }
Chris@127:     update();
Chris@127: }
Chris@127: 
Chris@908: sv_frame_t
Chris@222: View::getFirstVisibleFrame() const
Chris@222: {
Chris@908:     sv_frame_t f0 = getStartFrame();
Chris@908:     sv_frame_t f = getModelsStartFrame();
Chris@806:     if (f0 < 0 || f0 < f) return f;
Chris@222:     return f0;
Chris@222: }
Chris@222: 
Chris@908: sv_frame_t 
Chris@222: View::getLastVisibleFrame() const
Chris@222: {
Chris@908:     sv_frame_t f0 = getEndFrame();
Chris@908:     sv_frame_t f = getModelsEndFrame();
Chris@222:     if (f0 > f) return f;
Chris@222:     return f0;
Chris@222: }
Chris@222: 
Chris@908: sv_frame_t
Chris@127: View::getModelsStartFrame() const
Chris@127: {
Chris@127:     bool first = true;
Chris@908:     sv_frame_t startFrame = 0;
Chris@127: 
Chris@835:     for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@127: 
Chris@1266:         if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@1266: 
Chris@1266:             sv_frame_t thisStartFrame = (*i)->getModel()->getStartFrame();
Chris@1266: 
Chris@1266:             if (first || thisStartFrame < startFrame) {
Chris@1266:                 startFrame = thisStartFrame;
Chris@1266:             }
Chris@1266:             first = false;
Chris@1266:         }
Chris@127:     }
Chris@127:     return startFrame;
Chris@127: }
Chris@127: 
Chris@908: sv_frame_t
Chris@127: View::getModelsEndFrame() const
Chris@127: {
Chris@127:     bool first = true;
Chris@908:     sv_frame_t endFrame = 0;
Chris@127: 
Chris@835:     for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@127: 
Chris@1266:         if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@1266: 
Chris@1266:             sv_frame_t thisEndFrame = (*i)->getModel()->getEndFrame();
Chris@1266: 
Chris@1266:             if (first || thisEndFrame > endFrame) {
Chris@1266:                 endFrame = thisEndFrame;
Chris@1266:             }
Chris@1266:             first = false;
Chris@1266:         }
Chris@127:     }
Chris@127: 
Chris@127:     if (first) return getModelsStartFrame();
Chris@127:     return endFrame;
Chris@127: }
Chris@127: 
Chris@908: sv_samplerate_t
Chris@127: View::getModelsSampleRate() const
Chris@127: {
Chris@127:     //!!! Just go for the first, for now.  If we were supporting
Chris@127:     // multiple samplerates, we'd probably want to do frame/time
Chris@127:     // conversion in the model
Chris@127: 
Chris@159:     //!!! nah, this wants to always return the sr of the main model!
Chris@159: 
Chris@835:     for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@1266:         if ((*i)->getModel() && (*i)->getModel()->isOK()) {
Chris@1266:             return (*i)->getModel()->getSampleRate();
Chris@1266:         }
Chris@127:     }
Chris@127:     return 0;
Chris@127: }
Chris@127: 
Chris@315: View::ModelSet
Chris@315: View::getModels()
Chris@315: {
Chris@315:     ModelSet models;
Chris@315: 
Chris@315:     for (int i = 0; i < getLayerCount(); ++i) {
Chris@315: 
Chris@315:         Layer *layer = getLayer(i);
Chris@315: 
Chris@315:         if (dynamic_cast<TimeRulerLayer *>(layer)) {
Chris@315:             continue;
Chris@315:         }
Chris@315: 
Chris@315:         if (layer && layer->getModel()) {
Chris@315:             Model *model = layer->getModel();
Chris@315:             models.insert(model);
Chris@315:         }
Chris@315:     }
Chris@315: 
Chris@315:     return models;
Chris@315: }
Chris@315: 
Chris@320: Model *
Chris@320: View::getAligningModel() const
Chris@301: {
Chris@320:     if (!m_manager ||
Chris@320:         !m_manager->getAlignMode() ||
Chris@314:         !m_manager->getPlaybackModel()) {
Chris@320:         return 0;
Chris@314:     }
Chris@301: 
Chris@320:     Model *anyModel = 0;
Chris@359:     Model *alignedModel = 0;
Chris@320:     Model *goodModel = 0;
Chris@301: 
Chris@835:     for (LayerList::const_iterator i = m_layerStack.begin();
Chris@835:          i != m_layerStack.end(); ++i) {
Chris@320: 
Chris@320:         Layer *layer = *i;
Chris@320: 
Chris@320:         if (!layer) continue;
Chris@320:         if (dynamic_cast<TimeRulerLayer *>(layer)) continue;
Chris@301: 
Chris@301:         Model *model = (*i)->getModel();
Chris@301:         if (!model) continue;
Chris@301: 
Chris@359:         anyModel = model;
Chris@359: 
Chris@320:         if (model->getAlignmentReference()) {
Chris@359:             alignedModel = model;
Chris@320:             if (layer->isLayerOpaque() ||
Chris@320:                 dynamic_cast<RangeSummarisableTimeValueModel *>(model)) {
Chris@320:                 goodModel = model;
Chris@320:             }
Chris@301:         }
Chris@320:     }
Chris@301: 
Chris@320:     if (goodModel) return goodModel;
Chris@359:     else if (alignedModel) return alignedModel;
Chris@320:     else return anyModel;
Chris@320: }
Chris@320: 
Chris@908: sv_frame_t
Chris@908: View::alignFromReference(sv_frame_t f) const
Chris@320: {
Chris@856:     if (!m_manager || !m_manager->getAlignMode()) return f;
Chris@320:     Model *aligningModel = getAligningModel();
Chris@320:     if (!aligningModel) return f;
Chris@320:     return aligningModel->alignFromReference(f);
Chris@320: }
Chris@320: 
Chris@908: sv_frame_t
Chris@908: View::alignToReference(sv_frame_t f) const
Chris@320: {
Chris@321:     if (!m_manager->getAlignMode()) return f;
Chris@320:     Model *aligningModel = getAligningModel();
Chris@320:     if (!aligningModel) return f;
Chris@320:     return aligningModel->alignToReference(f);
Chris@320: }
Chris@320: 
Chris@908: sv_frame_t
Chris@320: View::getAlignedPlaybackFrame() const
Chris@320: {
Chris@856:     if (!m_manager) return 0;
Chris@908:     sv_frame_t pf = m_manager->getPlaybackFrame();
Chris@321:     if (!m_manager->getAlignMode()) return pf;
Chris@321: 
Chris@320:     Model *aligningModel = getAligningModel();
Chris@320:     if (!aligningModel) return pf;
Chris@830: 
Chris@908:     sv_frame_t af = aligningModel->alignFromReference(pf);
Chris@301: 
Chris@301:     return af;
Chris@301: }
Chris@301: 
Chris@127: bool
Chris@127: View::areLayersScrollable() const
Chris@127: {
Chris@127:     // True iff all views are scrollable
Chris@835:     for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@1266:         if (!(*i)->isLayerScrollable(this)) return false;
Chris@127:     }
Chris@127:     return true;
Chris@127: }
Chris@127: 
Chris@127: View::LayerList
Chris@127: View::getScrollableBackLayers(bool testChanged, bool &changed) const
Chris@127: {
Chris@127:     changed = false;
Chris@127: 
Chris@127:     // We want a list of all the scrollable layers that are behind the
Chris@127:     // backmost non-scrollable layer.
Chris@127: 
Chris@127:     LayerList scrollables;
Chris@127:     bool metUnscrollable = false;
Chris@127: 
Chris@835:     for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@587: //        SVDEBUG << "View::getScrollableBackLayers: calling isLayerDormant on layer " << *i << endl;
Chris@682: //        cerr << "(name is " << (*i)->objectName() << ")"
Chris@682: //                  << endl;
Chris@587: //        SVDEBUG << "View::getScrollableBackLayers: I am " << this << endl;
Chris@1266:         if ((*i)->isLayerDormant(this)) continue;
Chris@1266:         if ((*i)->isLayerOpaque()) {
Chris@1266:             // You can't see anything behind an opaque layer!
Chris@1266:             scrollables.clear();
Chris@127:             if (metUnscrollable) break;
Chris@1266:         }
Chris@1266:         if (!metUnscrollable && (*i)->isLayerScrollable(this)) {
Chris@127:             scrollables.push_back(*i);
Chris@127:         } else {
Chris@127:             metUnscrollable = true;
Chris@127:         }
Chris@127:     }
Chris@127: 
Chris@127:     if (testChanged && scrollables != m_lastScrollableBackLayers) {
Chris@1266:         m_lastScrollableBackLayers = scrollables;
Chris@1266:         changed = true;
Chris@127:     }
Chris@127:     return scrollables;
Chris@127: }
Chris@127: 
Chris@127: View::LayerList
Chris@127: View::getNonScrollableFrontLayers(bool testChanged, bool &changed) const
Chris@127: {
Chris@127:     changed = false;
Chris@127:     LayerList nonScrollables;
Chris@127: 
Chris@127:     // Everything in front of the first non-scrollable from the back
Chris@127:     // should also be considered non-scrollable
Chris@127: 
Chris@127:     bool started = false;
Chris@127: 
Chris@835:     for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@1266:         if ((*i)->isLayerDormant(this)) continue;
Chris@1266:         if (!started && (*i)->isLayerScrollable(this)) {
Chris@1266:             continue;
Chris@1266:         }
Chris@1266:         started = true;
Chris@1266:         if ((*i)->isLayerOpaque()) {
Chris@1266:             // You can't see anything behind an opaque layer!
Chris@1266:             nonScrollables.clear();
Chris@1266:         }
Chris@1266:         nonScrollables.push_back(*i);
Chris@127:     }
Chris@127: 
Chris@127:     if (testChanged && nonScrollables != m_lastNonScrollableBackLayers) {
Chris@1266:         m_lastNonScrollableBackLayers = nonScrollables;
Chris@1266:         changed = true;
Chris@127:     }
Chris@127: 
Chris@127:     return nonScrollables;
Chris@127: }
Chris@127: 
Chris@806: int
Chris@806: View::getZoomConstraintBlockSize(int blockSize,
Chris@1266:                                  ZoomConstraint::RoundingDirection dir)
Chris@127:     const
Chris@127: {
Chris@806:     int candidate = blockSize;
Chris@127:     bool haveCandidate = false;
Chris@127: 
Chris@127:     PowerOfSqrtTwoZoomConstraint defaultZoomConstraint;
Chris@127: 
Chris@835:     for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@127: 
Chris@1266:         const ZoomConstraint *zoomConstraint = (*i)->getZoomConstraint();
Chris@1266:         if (!zoomConstraint) zoomConstraint = &defaultZoomConstraint;
Chris@1266: 
Chris@1266:         int thisBlockSize =
Chris@1266:             zoomConstraint->getNearestBlockSize(blockSize, dir);
Chris@1266: 
Chris@1266:         // Go for the block size that's furthest from the one
Chris@1266:         // passed in.  Most of the time, that's what we want.
Chris@1266:         if (!haveCandidate ||
Chris@1266:             (thisBlockSize > blockSize && thisBlockSize > candidate) ||
Chris@1266:             (thisBlockSize < blockSize && thisBlockSize < candidate)) {
Chris@1266:             candidate = thisBlockSize;
Chris@1266:             haveCandidate = true;
Chris@1266:         }
Chris@127:     }
Chris@127: 
Chris@127:     return candidate;
Chris@127: }
Chris@127: 
Chris@183: bool
Chris@183: View::areLayerColoursSignificant() const
Chris@183: {
Chris@835:     for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) {
Chris@1266:         if ((*i)->getLayerColourSignificance() ==
Chris@287:             Layer::ColourHasMeaningfulValue) return true;
Chris@183:         if ((*i)->isLayerOpaque()) break;
Chris@183:     }
Chris@183:     return false;
Chris@183: }
Chris@183: 
Chris@217: bool
Chris@217: View::hasTopLayerTimeXAxis() const
Chris@217: {
Chris@835:     LayerList::const_iterator i = m_layerStack.end();
Chris@835:     if (i == m_layerStack.begin()) return false;
Chris@217:     --i;
Chris@217:     return (*i)->hasTimeXAxis();
Chris@217: }
Chris@217: 
Chris@127: void
Chris@127: View::zoom(bool in)
Chris@127: {
Chris@1183:     ZoomLevel newZoomLevel = m_zoomLevel;
Chris@127: 
Chris@127:     if (in) {
Chris@1324:         newZoomLevel = getZoomConstraintLevel(m_zoomLevel.decremented(),
Chris@1183:                                               ZoomConstraint::RoundDown);
Chris@127:     } else {
Chris@1324:         newZoomLevel = getZoomConstraintLevel(m_zoomLevel.incremented(),
Chris@1183:                                               ZoomConstraint::RoundUp);
Chris@127:     }
Chris@127: 
Chris@127:     if (newZoomLevel != m_zoomLevel) {
Chris@1266:         setZoomLevel(newZoomLevel);
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@127: void
Chris@510: View::scroll(bool right, bool lots, bool e)
Chris@127: {
Chris@908:     sv_frame_t delta;
Chris@127:     if (lots) {
Chris@1266:         delta = (getEndFrame() - getStartFrame()) / 2;
Chris@127:     } else {
Chris@1266:         delta = (getEndFrame() - getStartFrame()) / 20;
Chris@127:     }
Chris@127:     if (right) delta = -delta;
Chris@127: 
Chris@908:     if (m_centreFrame < delta) {
Chris@1266:         setCentreFrame(0, e);
Chris@908:     } else if (m_centreFrame - delta >= getModelsEndFrame()) {
Chris@1266:         setCentreFrame(getModelsEndFrame(), e);
Chris@127:     } else {
Chris@1266:         setCentreFrame(m_centreFrame - delta, e);
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@127: void
Chris@797: View::cancelClicked()
Chris@797: {
Chris@797:     QPushButton *cancel = qobject_cast<QPushButton *>(sender());
Chris@797:     if (!cancel) return;
Chris@797: 
Chris@797:     for (ProgressMap::iterator i = m_progressBars.begin();
Chris@1266:          i != m_progressBars.end(); ++i) {
Chris@797: 
Chris@797:         if (i->second.cancel == cancel) {
Chris@797: 
Chris@797:             Layer *layer = i->first;
Chris@797:             Model *model = layer->getModel();
Chris@797: 
Chris@797:             if (model) model->abandon();
Chris@797:         }
Chris@797:     }
Chris@797: }
Chris@797: 
Chris@797: void
Chris@127: View::checkProgress(void *object)
Chris@127: {
Chris@127:     if (!m_showProgress) return;
Chris@127: 
Chris@127:     int ph = height();
Chris@127: 
Chris@555:     for (ProgressMap::iterator i = m_progressBars.begin();
Chris@1266:          i != m_progressBars.end(); ++i) {
Chris@127: 
Chris@555:         QProgressBar *pb = i->second.bar;
Chris@797:         QPushButton *cancel = i->second.cancel;
Chris@555: 
Chris@1266:         if (i->first == object) {
Chris@127: 
Chris@555:             // The timer is used to test for stalls.  If the progress
Chris@555:             // bar does not get updated for some length of time, the
Chris@555:             // timer prompts it to go back into "indeterminate" mode
Chris@555:             QTimer *timer = i->second.checkTimer;
Chris@555: 
Chris@1266:             int completion = i->first->getCompletion(this);
Chris@301:             QString text = i->first->getPropertyContainerName();
Chris@583:             QString error = i->first->getError(this);
Chris@583: 
Chris@583:             if (error != "" && error != m_lastError) {
Chris@583:                 QMessageBox::critical(this, tr("Layer rendering error"), error);
Chris@583:                 m_lastError = error;
Chris@583:             }
Chris@301: 
Chris@387:             Model *model = i->first->getModel();
Chris@387:             RangeSummarisableTimeValueModel *wfm = 
Chris@387:                 dynamic_cast<RangeSummarisableTimeValueModel *>(model);
Chris@387: 
Chris@388:             if (completion > 0) {
Chris@555:                 pb->setMaximum(100); // was 0, for indeterminate start
Chris@388:             }
Chris@388: 
Chris@301:             if (completion >= 100) {
Chris@301: 
Chris@301:                 //!!!
Chris@326:                 if (wfm ||
Chris@776:                     (model && 
Chris@776:                      (wfm = dynamic_cast<RangeSummarisableTimeValueModel *>
Chris@776:                       (model->getSourceModel())))) {
Chris@301:                     completion = wfm->getAlignmentCompletion();
Chris@587: //                    SVDEBUG << "View::checkProgress: Alignment completion = " << completion << endl;
Chris@301:                     if (completion < 100) {
Chris@301:                         text = tr("Alignment");
Chris@301:                     }
Chris@301:                 }
Chris@387: 
Chris@387:             } else if (wfm) {
Chris@387:                 update(); // ensure duration &c gets updated
Chris@301:             }
Chris@127: 
Chris@1266:             if (completion >= 100) {
Chris@1266: 
Chris@1266:                 pb->hide();
Chris@797:                 cancel->hide();
Chris@555:                 timer->stop();
Chris@127: 
Chris@1266:             } else {
Chris@127: 
Chris@682: //                cerr << "progress = " << completion << endl;
Chris@555: 
Chris@555:                 if (!pb->isVisible()) {
Chris@555:                     i->second.lastCheck = 0;
Chris@555:                     timer->setInterval(2000);
Chris@555:                     timer->start();
Chris@555:                 }
Chris@555: 
Chris@797:                 cancel->move(0, ph - pb->height()/2 - 10);
Chris@797:                 cancel->show();
Chris@797: 
Chris@1266:                 pb->setValue(completion);
Chris@1266:                 pb->move(20, ph - pb->height());
Chris@1266: 
Chris@1266:                 pb->show();
Chris@1266:                 pb->update();
Chris@1266: 
Chris@1266:                 ph -= pb->height();
Chris@1266:             }
Chris@1266:         } else {
Chris@1266:             if (pb->isVisible()) {
Chris@1266:                 ph -= pb->height();
Chris@1266:             }
Chris@1266:         }
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@555: void
Chris@555: View::progressCheckStalledTimerElapsed()
Chris@555: {
Chris@555:     QObject *s = sender();
Chris@555:     QTimer *t = qobject_cast<QTimer *>(s);
Chris@555:     if (!t) return;
Chris@555:     for (ProgressMap::iterator i =  m_progressBars.begin();
Chris@555:          i != m_progressBars.end(); ++i) {
Chris@555:         if (i->second.checkTimer == t) {
Chris@555:             int value = i->second.bar->value();
Chris@555:             if (value > 0 && value == i->second.lastCheck) {
Chris@555:                 i->second.bar->setMaximum(0); // indeterminate
Chris@555:             }
Chris@555:             i->second.lastCheck = value;
Chris@555:             return;
Chris@555:         }
Chris@555:     }
Chris@555: }
Chris@555: 
Chris@384: int
Chris@384: View::getProgressBarWidth() const
Chris@384: {
Chris@384:     for (ProgressMap::const_iterator i = m_progressBars.begin();
Chris@1266:          i != m_progressBars.end(); ++i) {
Chris@555:         if (i->second.bar && i->second.bar->isVisible()) {
Chris@555:             return i->second.bar->width();
Chris@555:         }
Chris@384:     }
Chris@384: 
Chris@384:     return 0;
Chris@384: }
Chris@384: 
Chris@127: void
Chris@339: View::setPaintFont(QPainter &paint)
Chris@339: {
Chris@955:     int scaleFactor = 1;
Chris@956:     int dpratio = effectiveDevicePixelRatio();
Chris@955:     if (dpratio > 1) {
Chris@955:         QPaintDevice *dev = paint.device();
Chris@955:         if (dynamic_cast<QPixmap *>(dev) || dynamic_cast<QImage *>(dev)) {
Chris@955:             scaleFactor = dpratio;
Chris@955:         }
Chris@955:     }
Chris@955: 
Chris@339:     QFont font(paint.font());
Chris@955:     font.setPointSize(Preferences::getInstance()->getViewFontSize()
Chris@955:                       * scaleFactor);
Chris@339:     paint.setFont(font);
Chris@339: }
Chris@339: 
Chris@915: QRect
Chris@915: View::getPaintRect() const
Chris@915: {
Chris@919:     return rect();
Chris@915: }
Chris@915: 
Chris@339: void
Chris@127: View::paintEvent(QPaintEvent *e)
Chris@127: {
Chris@127: //    Profiler prof("View::paintEvent", false);
Chris@800: //    cerr << "View::paintEvent: centre frame is " << m_centreFrame << endl;
matthiasm@785: 
Chris@835:     if (m_layerStack.empty()) {
Chris@1266:         QFrame::paintEvent(e);
Chris@1266:         return;
Chris@127:     }
Chris@127: 
Chris@127:     // ensure our constraints are met
Chris@137: 
Chris@137: /*!!! Should we do this only if we have layers that can't support other
Chris@137:   zoom levels?
Chris@137: 
Chris@127:     m_zoomLevel = getZoomConstraintBlockSize(m_zoomLevel,
Chris@1266:                                              ZoomConstraint::RoundUp);
Chris@137: */
Chris@127: 
Chris@127:     QPainter paint;
Chris@127:     bool repaintCache = false;
Chris@127:     bool paintedCacheRect = false;
Chris@127: 
Chris@127:     QRect cacheRect(rect());
Chris@127: 
Chris@127:     if (e) {
Chris@1266:         cacheRect &= e->rect();
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266:         cerr << "paint rect " << cacheRect.width() << "x" << cacheRect.height()
Chris@1266:                   << ", my rect " << width() << "x" << height() << endl;
Chris@127: #endif
Chris@127:     }
Chris@127: 
Chris@127:     QRect nonCacheRect(cacheRect);
Chris@127: 
Chris@956:     int dpratio = effectiveDevicePixelRatio();
Chris@953: 
Chris@127:     // If not all layers are scrollable, but some of the back layers
Chris@127:     // are, we should store only those in the cache.
Chris@127: 
Chris@127:     bool layersChanged = false;
Chris@127:     LayerList scrollables = getScrollableBackLayers(true, layersChanged);
Chris@127:     LayerList nonScrollables = getNonScrollableFrontLayers(true, layersChanged);
Chris@127:     bool selectionCacheable = nonScrollables.empty();
Chris@127:     bool haveSelections = m_manager && !m_manager->getSelections().empty();
Chris@127: 
Chris@127:     // If all the non-scrollable layers are non-opaque, then we draw
Chris@127:     // the selection rectangle behind them and cache it.  If any are
Chris@953:     // opaque, however, or if our device-pixel ratio is not 1 (so we
Chris@953:     // need to paint direct to the widget), then we can't cache.
Chris@127:     //
Chris@953:     if (dpratio == 1) {
Chris@953: 
Chris@953:         if (!selectionCacheable) {
Chris@953:             selectionCacheable = true;
Chris@953:             for (LayerList::const_iterator i = nonScrollables.begin();
Chris@953:                  i != nonScrollables.end(); ++i) {
Chris@953:                 if ((*i)->isLayerOpaque()) {
Chris@953:                     selectionCacheable = false;
Chris@953:                     break;
Chris@953:                 }
Chris@953:             }
Chris@953:         }
Chris@953: 
Chris@953:         if (selectionCacheable) {
Chris@953:             QPoint localPos;
Chris@953:             bool closeToLeft, closeToRight;
Chris@953:             if (shouldIlluminateLocalSelection
Chris@953:                 (localPos, closeToLeft, closeToRight)) {
Chris@953:                 selectionCacheable = false;
Chris@953:             }
Chris@953:         }
Chris@953: 
Chris@953:     } else {
Chris@953: 
Chris@953:         selectionCacheable = false;
Chris@127:     }
Chris@127: 
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@682:     cerr << "View(" << this << ")::paintEvent: have " << scrollables.size()
Chris@1266:               << " scrollable back layers and " << nonScrollables.size()
Chris@1266:               << " non-scrollable front layers" << endl;
Chris@682:     cerr << "haveSelections " << haveSelections << ", selectionCacheable "
Chris@1266:               << selectionCacheable << ", m_selectionCached " << m_selectionCached << endl;
Chris@127: #endif
Chris@127: 
Chris@127:     if (layersChanged || scrollables.empty() ||
Chris@1266:         (haveSelections && (selectionCacheable != m_selectionCached))) {
Chris@1266:         delete m_cache;
Chris@1266:         m_cache = 0;
Chris@1266:         m_selectionCached = false;
Chris@127:     }
Chris@127: 
Chris@952:     QSize scaledCacheSize(scaledSize(size(), dpratio));
Chris@952:     QRect scaledCacheRect(scaledRect(cacheRect, dpratio));
Chris@952: 
Chris@952:     if (!m_buffer || scaledCacheSize != m_buffer->size()) {
Chris@952:         delete m_buffer;
Chris@952:         m_buffer = new QPixmap(scaledCacheSize);
Chris@952:     }
Chris@952:     
Chris@127:     if (!scrollables.empty()) {
Chris@244: 
Chris@244: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@682:         cerr << "View(" << this << "): cache " << m_cache << ", cache zoom "
Chris@682:                   << m_cacheZoomLevel << ", zoom " << m_zoomLevel << endl;
Chris@244: #endif
Chris@244: 
Chris@1266:         if (!m_cache ||
Chris@1266:             m_cacheZoomLevel != m_zoomLevel ||
Chris@914:             scaledCacheSize != m_cache->size()) {
Chris@127: 
Chris@1266:             // cache is not valid
Chris@1266: 
Chris@1266:             if (cacheRect.width() < width()/10) {
Chris@1266:                 delete m_cache;
Chris@244:                 m_cache = 0;
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266:                 cerr << "View(" << this << ")::paintEvent: small repaint, not bothering to recreate cache" << endl;
Chris@127: #endif
Chris@1266:             } else {
Chris@1266:                 delete m_cache;
Chris@1266:                 m_cache = new QPixmap(scaledCacheSize);
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266:                 cerr << "View(" << this << ")::paintEvent: recreated cache" << endl;
Chris@127: #endif
Chris@1266:                 cacheRect = rect();
Chris@1266:                 repaintCache = true;
Chris@1266:             }
Chris@1266: 
Chris@1266:         } else if (m_cacheCentreFrame != m_centreFrame) {
Chris@1266: 
Chris@1266:             int dx =
Chris@1266:                 getXForFrame(m_cacheCentreFrame) -
Chris@1266:                 getXForFrame(m_centreFrame);
Chris@1266: 
Chris@1266:             if (dx > -width() && dx < width()) {
Chris@1266:                 static QPixmap *tmpPixmap = 0;
Chris@1266:                 if (!tmpPixmap || tmpPixmap->size() != scaledCacheSize) {
Chris@1266:                     delete tmpPixmap;
Chris@1266:                     tmpPixmap = new QPixmap(scaledCacheSize);
Chris@1266:                 }
Chris@1266:                 paint.begin(tmpPixmap);
Chris@1266:                 paint.drawPixmap(0, 0, *m_cache);
Chris@1266:                 paint.end();
Chris@1266:                 paint.begin(m_cache);
Chris@1266:                 paint.drawPixmap(dx, 0, *tmpPixmap);
Chris@1266:                 paint.end();
Chris@1266:                 if (dx < 0) {
Chris@1266:                     cacheRect = QRect(width() + dx, 0, -dx, height());
Chris@1266:                 } else {
Chris@1266:                     cacheRect = QRect(0, 0, dx, height());
Chris@1266:                 }
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266:                 cerr << "View(" << this << ")::paintEvent: scrolled cache by " << dx << endl;
Chris@127: #endif
Chris@1266:             } else {
Chris@1266:                 cacheRect = rect();
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266:                 cerr << "View(" << this << ")::paintEvent: scrolling too far" << endl;
Chris@127: #endif
Chris@1266:             }
Chris@1266:             repaintCache = true;
Chris@1266: 
Chris@1266:         } else {
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266:             cerr << "View(" << this << ")::paintEvent: cache is good" << endl;
Chris@127: #endif
Chris@1266:             paint.begin(m_buffer);
Chris@1266:             paint.drawPixmap(scaledCacheRect, *m_cache, scaledCacheRect);
Chris@1266:             paint.end();
Chris@1266:             QFrame::paintEvent(e);
Chris@1266:             paintedCacheRect = true;
Chris@1266:         }
Chris@1266: 
Chris@1266:         m_cacheCentreFrame = m_centreFrame;
Chris@1266:         m_cacheZoomLevel = m_zoomLevel;
Chris@127:     }
Chris@127: 
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@682: //    cerr << "View(" << this << ")::paintEvent: cacheRect " << cacheRect << ", nonCacheRect " << (nonCacheRect | cacheRect) << ", repaintCache " << repaintCache << ", paintedCacheRect " << paintedCacheRect << endl;
Chris@127: #endif
Chris@127: 
Chris@127:     // Scrollable (cacheable) items first
Chris@127: 
Chris@919:     ViewProxy proxy(this, dpratio);
Chris@919:     
Chris@127:     if (!paintedCacheRect) {
Chris@127: 
Chris@914:         QRect rectToPaint;
Chris@914: 
Chris@1266:         if (repaintCache) {
Chris@914:             paint.begin(m_cache);
Chris@914:             rectToPaint = scaledCacheRect;
Chris@914:         } else {
Chris@952:             paint.begin(m_buffer);
Chris@952:             rectToPaint = scaledCacheRect;
Chris@914:         }
Chris@914: 
Chris@339:         setPaintFont(paint);
Chris@1266:         paint.setClipRect(rectToPaint);
Chris@287: 
Chris@287:         paint.setPen(getBackground());
Chris@287:         paint.setBrush(getBackground());
Chris@1266:         paint.drawRect(rectToPaint);
Chris@1266: 
Chris@1266:         paint.setPen(getForeground());
Chris@1266:         paint.setBrush(Qt::NoBrush);
Chris@1266:         
Chris@1266:         for (LayerList::iterator i = scrollables.begin(); i != scrollables.end(); ++i) {
Chris@1266:             paint.setRenderHint(QPainter::Antialiasing, false);
Chris@1266:             paint.save();
Chris@951: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@951:             cerr << "Painting scrollable layer " << *i << " using proxy with repaintCache = " << repaintCache << ", dpratio = " << dpratio << ", rectToPaint = " << rectToPaint.x() << "," << rectToPaint.y() << " " << rectToPaint.width() << "x" << rectToPaint.height() << endl;
Chris@951: #endif
Chris@919:             (*i)->paint(&proxy, paint, rectToPaint);
Chris@1266:             paint.restore();
Chris@1266:         }
Chris@1266: 
Chris@1266:         if (haveSelections && selectionCacheable) {
Chris@1266:             drawSelections(paint);
Chris@1266:             m_selectionCached = repaintCache;
Chris@1266:         }
Chris@1266:         
Chris@1266:         paint.end();
Chris@1266: 
Chris@1266:         if (repaintCache) {
Chris@1266:             cacheRect |= (e ? e->rect() : rect());
Chris@952:             scaledCacheRect = scaledRect(cacheRect, dpratio);
Chris@1266:             paint.begin(m_buffer);
Chris@1266:             paint.drawPixmap(scaledCacheRect, *m_cache, scaledCacheRect);
Chris@1266:             paint.end();
Chris@1266:         }
Chris@127:     }
Chris@127: 
Chris@127:     // Now non-cacheable items.  We always need to redraw the
Chris@127:     // non-cacheable items across at least the area we drew of the
Chris@127:     // cacheable items.
Chris@127: 
Chris@127:     nonCacheRect |= cacheRect;
Chris@127: 
Chris@952:     QRect scaledNonCacheRect = scaledRect(nonCacheRect, dpratio);
Chris@952:     
Chris@952:     paint.begin(m_buffer);
Chris@952:     paint.setClipRect(scaledNonCacheRect);
Chris@339:     setPaintFont(paint);
Chris@127:     if (scrollables.empty()) {
Chris@287:         paint.setPen(getBackground());
Chris@287:         paint.setBrush(getBackground());
Chris@1266:         paint.drawRect(scaledNonCacheRect);
Chris@127:     }
Chris@1266:         
Chris@287:     paint.setPen(getForeground());
Chris@127:     paint.setBrush(Qt::NoBrush);
Chris@1266:         
Chris@127:     for (LayerList::iterator i = nonScrollables.begin(); i != nonScrollables.end(); ++i) {
Chris@127: //        Profiler profiler2("View::paintEvent non-cacheable");
Chris@951: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@951:         cerr << "Painting non-scrollable layer " << *i << " without proxy with repaintCache = " << repaintCache << ", dpratio = " << dpratio << ", rectToPaint = " << nonCacheRect.x() << "," << nonCacheRect.y() << " " << nonCacheRect.width() << "x" << nonCacheRect.height() << endl;
Chris@951: #endif
Chris@1266:         (*i)->paint(&proxy, paint, scaledNonCacheRect);
Chris@127:     }
Chris@1266:         
Chris@127:     paint.end();
Chris@953:     
Chris@953:     paint.begin(this);
Chris@953:     QRect finalPaintRect = e ? e->rect() : rect();
Chris@953:     paint.drawPixmap(finalPaintRect, *m_buffer, scaledRect(finalPaintRect, dpratio));
Chris@953:     paint.end();
Chris@953: 
Chris@953:     paint.begin(this);
Chris@339:     setPaintFont(paint);
Chris@953:     if (e) paint.setClipRect(e->rect());
Chris@127:     if (!m_selectionCached) {
Chris@1266:         drawSelections(paint);
Chris@127:     }
Chris@127:     paint.end();
Chris@127: 
Chris@211:     bool showPlayPointer = true;
Chris@211:     if (m_followPlay == PlaybackScrollContinuous) {
Chris@211:         showPlayPointer = false;
Chris@806:     } else if (m_playPointerFrame <= getStartFrame() ||
Chris@211:                m_playPointerFrame >= getEndFrame()) {
Chris@211:         showPlayPointer = false;
Chris@211:     } else if (m_manager && !m_manager->isPlaying()) {
Chris@211:         if (m_playPointerFrame == getCentreFrame() &&
Chris@787:             m_manager->shouldShowCentreLine() &&
Chris@211:             m_followPlay != PlaybackIgnore) {
Chris@787:             // Don't show the play pointer when it is redundant with
Chris@787:             // the centre line
Chris@211:             showPlayPointer = false;
Chris@211:         }
Chris@211:     }
Chris@952:     
Chris@211:     if (showPlayPointer) {
Chris@127: 
Chris@1266:         paint.begin(this);
Chris@127: 
Chris@211:         int playx = getXForFrame(m_playPointerFrame);
Chris@211:         
Chris@287:         paint.setPen(getForeground());
Chris@211:         paint.drawLine(playx - 1, 0, playx - 1, height() - 1);
Chris@211:         paint.drawLine(playx + 1, 0, playx + 1, height() - 1);
Chris@211:         paint.drawPoint(playx, 0);
Chris@211:         paint.drawPoint(playx, height() - 1);
Chris@287:         paint.setPen(getBackground());
Chris@211:         paint.drawLine(playx, 1, playx, height() - 2);
Chris@127: 
Chris@1266:         paint.end();
Chris@127:     }
Chris@127: 
Chris@127:     QFrame::paintEvent(e);
Chris@127: }
Chris@127: 
Chris@127: void
Chris@127: View::drawSelections(QPainter &paint)
Chris@127: {
Chris@217:     if (!hasTopLayerTimeXAxis()) return;
Chris@217: 
Chris@127:     MultiSelection::SelectionList selections;
Chris@127: 
Chris@127:     if (m_manager) {
Chris@1266:         selections = m_manager->getSelections();
Chris@1266:         if (m_manager->haveInProgressSelection()) {
Chris@1266:             bool exclusive;
Chris@1266:             Selection inProgressSelection =
Chris@1266:                 m_manager->getInProgressSelection(exclusive);
Chris@1266:             if (exclusive) selections.clear();
Chris@1266:             selections.insert(inProgressSelection);
Chris@1266:         }
Chris@127:     }
Chris@127: 
Chris@127:     paint.save();
Chris@183: 
Chris@183:     bool translucent = !areLayerColoursSignificant();
Chris@183: 
Chris@183:     if (translucent) {
Chris@183:         paint.setBrush(QColor(150, 150, 255, 80));
Chris@183:     } else {
Chris@183:         paint.setBrush(Qt::NoBrush);
Chris@183:     }
Chris@127: 
Chris@908:     sv_samplerate_t sampleRate = getModelsSampleRate();
Chris@127: 
Chris@127:     QPoint localPos;
Chris@908:     sv_frame_t illuminateFrame = -1;
Chris@127:     bool closeToLeft, closeToRight;
Chris@127: 
Chris@127:     if (shouldIlluminateLocalSelection(localPos, closeToLeft, closeToRight)) {
Chris@1266:         illuminateFrame = getFrameForX(localPos.x());
Chris@127:     }
Chris@127: 
Chris@127:     const QFontMetrics &metrics = paint.fontMetrics();
Chris@127: 
Chris@127:     for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@1266:          i != selections.end(); ++i) {
Chris@1266: 
Chris@1266:         int p0 = getXForFrame(alignFromReference(i->getStartFrame()));
Chris@1266:         int p1 = getXForFrame(alignFromReference(i->getEndFrame()));
Chris@1266: 
Chris@1266:         if (p1 < 0 || p0 > width()) continue;
Chris@127: 
Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT
Chris@1266:         SVDEBUG << "View::drawSelections: " << p0 << ",-1 [" << (p1-p0) << "x" << (height()+1) << "]" << endl;
Chris@127: #endif
Chris@127: 
Chris@1266:         bool illuminateThis =
Chris@1266:             (illuminateFrame >= 0 && i->contains(illuminateFrame));
Chris@1266: 
Chris@1270:         double h = height();
Chris@1270:         double penWidth = PaintAssistant::scalePenWidth(1.0);
Chris@1270:         double half = penWidth/2.0;
Chris@1270: 
Chris@1270:         paint.setPen(QPen(QColor(150, 150, 255), penWidth));
Chris@183: 
Chris@183:         if (translucent && shouldLabelSelections()) {
Chris@1270:             paint.drawRect(QRectF(p0, -penWidth, p1 - p0, h + 2*penWidth));
Chris@183:         } else {
Chris@183:             // Make the top & bottom lines of the box visible if we
Chris@183:             // are lacking some of the other visual cues.  There's no
Chris@183:             // particular logic to this, it's just a question of what
Chris@183:             // I happen to think looks nice.
Chris@1270:             paint.drawRect(QRectF(p0, half, p1 - p0, h - penWidth));
Chris@183:         }
Chris@127: 
Chris@1266:         if (illuminateThis) {
Chris@1266:             paint.save();
Chris@1270:             penWidth = PaintAssistant::scalePenWidth(2.0);
Chris@1270:             half = penWidth/2.0;
Chris@1270:             paint.setPen(QPen(getForeground(), penWidth));
Chris@1266:             if (closeToLeft) {
Chris@1270:                 paint.drawLine(QLineF(p0, half, p1, half));
Chris@1270:                 paint.drawLine(QLineF(p0, half, p0, h - half));
Chris@1270:                 paint.drawLine(QLineF(p0, h - half, p1, h - half));
Chris@1266:             } else if (closeToRight) {
Chris@1270:                 paint.drawLine(QLineF(p0, half, p1, half));
Chris@1270:                 paint.drawLine(QLineF(p1, half, p1, h - half));
Chris@1270:                 paint.drawLine(QLineF(p0, h - half, p1, h - half));
Chris@1266:             } else {
Chris@1266:                 paint.setBrush(Qt::NoBrush);
Chris@1270:                 paint.drawRect(QRectF(p0, half, p1 - p0, h - penWidth));
Chris@1266:             }
Chris@1266:             paint.restore();
Chris@1266:         }
Chris@1266: 
Chris@1266:         if (sampleRate && shouldLabelSelections() && m_manager &&
Chris@189:             m_manager->shouldShowSelectionExtents()) {
Chris@1266:             
Chris@1266:             QString startText = QString("%1 / %2")
Chris@1266:                 .arg(QString::fromStdString
Chris@1266:                      (RealTime::frame2RealTime
Chris@1266:                       (i->getStartFrame(), sampleRate).toText(true)))
Chris@1266:                 .arg(i->getStartFrame());
Chris@1266:             
Chris@1266:             QString endText = QString(" %1 / %2")
Chris@1266:                 .arg(QString::fromStdString
Chris@1266:                      (RealTime::frame2RealTime
Chris@1266:                       (i->getEndFrame(), sampleRate).toText(true)))
Chris@1266:                 .arg(i->getEndFrame());
Chris@1266:             
Chris@1266:             QString durationText = QString("(%1 / %2) ")
Chris@1266:                 .arg(QString::fromStdString
Chris@1266:                      (RealTime::frame2RealTime
Chris@1266:                       (i->getEndFrame() - i->getStartFrame(), sampleRate)
Chris@1266:                       .toText(true)))
Chris@1266:                 .arg(i->getEndFrame() - i->getStartFrame());
Chris@1266: 
Chris@1266:             int sw = metrics.width(startText),
Chris@1266:                 ew = metrics.width(endText),
Chris@1266:                 dw = metrics.width(durationText);
Chris@1266: 
Chris@1266:             int sy = metrics.ascent() + metrics.height() + 4;
Chris@1266:             int ey = sy;
Chris@1266:             int dy = sy + metrics.height();
Chris@1266: 
Chris@1266:             int sx = p0 + 2;
Chris@1266:             int ex = sx;
Chris@1266:             int dx = sx;
Chris@127: 
Chris@501:             bool durationBothEnds = true;
Chris@501: 
Chris@1266:             if (sw + ew > (p1 - p0)) {
Chris@1266:                 ey += metrics.height();
Chris@1266:                 dy += metrics.height();
Chris@501:                 durationBothEnds = false;
Chris@1266:             }
Chris@1266: 
Chris@1266:             if (ew < (p1 - p0)) {
Chris@1266:                 ex = p1 - 2 - ew;
Chris@1266:             }
Chris@1266: 
Chris@1266:             if (dw < (p1 - p0)) {
Chris@1266:                 dx = p1 - 2 - dw;
Chris@1266:             }
Chris@127: 
Chris@1193:             PaintAssistant::drawVisibleText(this, paint, sx, sy, startText,
Chris@1193:                                             PaintAssistant::OutlinedText);
Chris@1193:             PaintAssistant::drawVisibleText(this, paint, ex, ey, endText,
Chris@1193:                                             PaintAssistant::OutlinedText);
Chris@1193:             PaintAssistant::drawVisibleText(this, paint, dx, dy, durationText,
Chris@1193:                                             PaintAssistant::OutlinedText);
Chris@501:             if (durationBothEnds) {
Chris@1193:                 PaintAssistant::drawVisibleText(this, paint, sx, dy, durationText,
Chris@1193:                                                 PaintAssistant::OutlinedText);
Chris@501:             }
Chris@1266:         }
Chris@127:     }
Chris@127: 
Chris@127:     paint.restore();
Chris@127: }
Chris@127: 
Chris@267: void
Chris@270: View::drawMeasurementRect(QPainter &paint, const Layer *topLayer, QRect r,
Chris@270:                           bool focus) const
Chris@267: {
Chris@587: //    SVDEBUG << "View::drawMeasurementRect(" << r.x() << "," << r.y() << " "
Chris@585: //              << r.width() << "x" << r.height() << ")" << endl;
Chris@268: 
Chris@267:     if (r.x() + r.width() < 0 || r.x() >= width()) return;
Chris@267: 
Chris@270:     if (r.width() != 0 || r.height() != 0) {
Chris@270:         paint.save();
Chris@270:         if (focus) {
Chris@270:             paint.setPen(Qt::NoPen);
Chris@272:             QColor brushColour(Qt::black);
Chris@272:             brushColour.setAlpha(hasLightBackground() ? 15 : 40);
Chris@270:             paint.setBrush(brushColour);
Chris@270:             if (r.x() > 0) {
Chris@270:                 paint.drawRect(0, 0, r.x(), height());
Chris@270:             }
Chris@270:             if (r.x() + r.width() < width()) {
Chris@270:                 paint.drawRect(r.x() + r.width(), 0, width()-r.x()-r.width(), height());
Chris@270:             }
Chris@270:             if (r.y() > 0) {
Chris@270:                 paint.drawRect(r.x(), 0, r.width(), r.y());
Chris@270:             }
Chris@270:             if (r.y() + r.height() < height()) {
Chris@270:                 paint.drawRect(r.x(), r.y() + r.height(), r.width(), height()-r.y()-r.height());
Chris@270:             }
Chris@270:             paint.setBrush(Qt::NoBrush);
Chris@270:         }
Chris@270:         paint.setPen(Qt::green);
Chris@270:         paint.drawRect(r);
Chris@270:         paint.restore();
Chris@270:     } else {
Chris@270:         paint.save();
Chris@270:         paint.setPen(Qt::green);
Chris@270:         paint.drawPoint(r.x(), r.y());
Chris@270:         paint.restore();
Chris@270:     }
Chris@270: 
Chris@270:     if (!focus) return;
Chris@270: 
Chris@278:     paint.save();
Chris@278:     QFont fn = paint.font();
Chris@278:     if (fn.pointSize() > 8) {
Chris@278:         fn.setPointSize(fn.pointSize() - 1);
Chris@278:         paint.setFont(fn);
Chris@278:     }
Chris@278: 
Chris@267:     int fontHeight = paint.fontMetrics().height();
Chris@267:     int fontAscent = paint.fontMetrics().ascent();
Chris@267: 
Chris@904:     double v0, v1;
Chris@267:     QString u0, u1;
Chris@267:     bool b0 = false, b1 = false;
Chris@267: 
Chris@267:     QString axs, ays, bxs, bys, dxs, dys;
Chris@267: 
Chris@267:     int axx, axy, bxx, bxy, dxx, dxy;
Chris@267:     int aw = 0, bw = 0, dw = 0;
Chris@267:     
Chris@267:     int labelCount = 0;
Chris@267: 
Chris@362:     // top-left point, x-coord
Chris@362: 
Chris@267:     if ((b0 = topLayer->getXScaleValue(this, r.x(), v0, u0))) {
Chris@267:         axs = QString("%1 %2").arg(v0).arg(u0);
Chris@278:         if (u0 == "Hz" && Pitch::isFrequencyInMidiRange(v0)) {
Chris@278:             axs = QString("%1 (%2)").arg(axs)
Chris@278:                 .arg(Pitch::getPitchLabelForFrequency(v0));
Chris@278:         }
Chris@267:         aw = paint.fontMetrics().width(axs);
Chris@267:         ++labelCount;
Chris@267:     }
Chris@362: 
Chris@362:     // bottom-right point, x-coord
Chris@267:         
Chris@267:     if (r.width() > 0) {
Chris@267:         if ((b1 = topLayer->getXScaleValue(this, r.x() + r.width(), v1, u1))) {
Chris@267:             bxs = QString("%1 %2").arg(v1).arg(u1);
Chris@278:             if (u1 == "Hz" && Pitch::isFrequencyInMidiRange(v1)) {
Chris@278:                 bxs = QString("%1 (%2)").arg(bxs)
Chris@278:                     .arg(Pitch::getPitchLabelForFrequency(v1));
Chris@278:             }
Chris@267:             bw = paint.fontMetrics().width(bxs);
Chris@267:         }
Chris@267:     }
Chris@362: 
Chris@362:     // dimension, width
Chris@267:         
Chris@283:     if (b0 && b1 && v1 != v0 && u0 == u1) {
Chris@362:         dxs = QString("[%1 %2]").arg(fabs(v1 - v0)).arg(u1);
Chris@267:         dw = paint.fontMetrics().width(dxs);
Chris@267:     }
Chris@267:     
Chris@267:     b0 = false;
Chris@267:     b1 = false;
Chris@267: 
Chris@362:     // top-left point, y-coord
Chris@362: 
Chris@267:     if ((b0 = topLayer->getYScaleValue(this, r.y(), v0, u0))) {
Chris@267:         ays = QString("%1 %2").arg(v0).arg(u0);
Chris@278:         if (u0 == "Hz" && Pitch::isFrequencyInMidiRange(v0)) {
Chris@278:             ays = QString("%1 (%2)").arg(ays)
Chris@278:                 .arg(Pitch::getPitchLabelForFrequency(v0));
Chris@278:         }
Chris@267:         aw = std::max(aw, paint.fontMetrics().width(ays));
Chris@267:         ++labelCount;
Chris@267:     }
Chris@267: 
Chris@362:     // bottom-right point, y-coord
Chris@362: 
Chris@267:     if (r.height() > 0) {
Chris@267:         if ((b1 = topLayer->getYScaleValue(this, r.y() + r.height(), v1, u1))) {
Chris@267:             bys = QString("%1 %2").arg(v1).arg(u1);
Chris@278:             if (u1 == "Hz" && Pitch::isFrequencyInMidiRange(v1)) {
Chris@278:                 bys = QString("%1 (%2)").arg(bys)
Chris@278:                     .arg(Pitch::getPitchLabelForFrequency(v1));
Chris@278:             }
Chris@267:             bw = std::max(bw, paint.fontMetrics().width(bys));
Chris@267:         }
Chris@267:     }
Chris@274: 
Chris@274:     bool bd = false;
Chris@904:     double dy = 0.f;
Chris@274:     QString du;
Chris@274: 
Chris@362:     // dimension, height
Chris@362:         
Chris@274:     if ((bd = topLayer->getYScaleDifference(this, r.y(), r.y() + r.height(),
Chris@283:                                             dy, du)) &&
Chris@283:         dy != 0) {
Chris@274:         if (du != "") {
Chris@362:             if (du == "Hz") {
Chris@362:                 int semis;
Chris@904:                 double cents;
Chris@362:                 semis = Pitch::getPitchForFrequencyDifference(v0, v1, &cents);
Chris@362:                 dys = QString("[%1 %2 (%3)]")
Chris@362:                     .arg(dy).arg(du)
Chris@362:                     .arg(Pitch::getLabelForPitchRange(semis, cents));
Chris@362:             } else {
Chris@362:                 dys = QString("[%1 %2]").arg(dy).arg(du);
Chris@362:             }
Chris@274:         } else {
Chris@362:             dys = QString("[%1]").arg(dy);
Chris@274:         }
Chris@267:         dw = std::max(dw, paint.fontMetrics().width(dys));
Chris@267:     }
Chris@267: 
Chris@267:     int mw = r.width();
Chris@267:     int mh = r.height();
Chris@267: 
Chris@267:     bool edgeLabelsInside = false;
Chris@267:     bool sizeLabelsInside = false;
Chris@267: 
Chris@267:     if (mw < std::max(aw, std::max(bw, dw)) + 4) {
Chris@267:         // defaults stand
Chris@267:     } else if (mw < aw + bw + 4) {
Chris@267:         if (mh > fontHeight * labelCount * 3 + 4) {
Chris@267:             edgeLabelsInside = true;
Chris@267:             sizeLabelsInside = true;
Chris@267:         } else if (mh > fontHeight * labelCount * 2 + 4) {
Chris@267:             edgeLabelsInside = true;
Chris@267:         }
Chris@267:     } else if (mw < aw + bw + dw + 4) {
Chris@267:         if (mh > fontHeight * labelCount * 3 + 4) {
Chris@267:             edgeLabelsInside = true;
Chris@267:             sizeLabelsInside = true;
Chris@267:         } else if (mh > fontHeight * labelCount + 4) {
Chris@267:             edgeLabelsInside = true;
Chris@267:         }
Chris@267:     } else {
Chris@267:         if (mh > fontHeight * labelCount + 4) {
Chris@267:             edgeLabelsInside = true;
Chris@267:             sizeLabelsInside = true;
Chris@267:         }
Chris@267:     }
Chris@267: 
Chris@267:     if (edgeLabelsInside) {
Chris@267: 
Chris@267:         axx = r.x() + 2;
Chris@267:         axy = r.y() + fontAscent + 2;
Chris@267: 
Chris@267:         bxx = r.x() + r.width() - bw - 2;
Chris@267:         bxy = r.y() + r.height() - (labelCount-1) * fontHeight - 2;
Chris@267: 
Chris@267:     } else {
Chris@267: 
Chris@267:         axx = r.x() - aw - 2;
Chris@267:         axy = r.y() + fontAscent;
Chris@267:         
Chris@267:         bxx = r.x() + r.width() + 2;
Chris@267:         bxy = r.y() + r.height() - (labelCount-1) * fontHeight;
Chris@267:     }
Chris@267: 
Chris@267:     dxx = r.width()/2 + r.x() - dw/2;
Chris@267: 
Chris@267:     if (sizeLabelsInside) {
Chris@267: 
Chris@267:         dxy = r.height()/2 + r.y() - (labelCount * fontHeight)/2 + fontAscent;
Chris@267: 
Chris@267:     } else {
Chris@267: 
Chris@267:         dxy = r.y() + r.height() + fontAscent + 2;
Chris@267:     }
Chris@267:     
Chris@267:     if (axs != "") {
Chris@1078:         PaintAssistant::drawVisibleText(this, paint, axx, axy, axs, PaintAssistant::OutlinedText);
Chris@267:         axy += fontHeight;
Chris@267:     }
Chris@267:     
Chris@267:     if (ays != "") {
Chris@1078:         PaintAssistant::drawVisibleText(this, paint, axx, axy, ays, PaintAssistant::OutlinedText);
Chris@267:         axy += fontHeight;
Chris@267:     }
Chris@267: 
Chris@267:     if (bxs != "") {
Chris@1078:         PaintAssistant::drawVisibleText(this, paint, bxx, bxy, bxs, PaintAssistant::OutlinedText);
Chris@267:         bxy += fontHeight;
Chris@267:     }
Chris@267: 
Chris@267:     if (bys != "") {
Chris@1078:         PaintAssistant::drawVisibleText(this, paint, bxx, bxy, bys, PaintAssistant::OutlinedText);
Chris@267:         bxy += fontHeight;
Chris@267:     }
Chris@267: 
Chris@267:     if (dxs != "") {
Chris@1078:         PaintAssistant::drawVisibleText(this, paint, dxx, dxy, dxs, PaintAssistant::OutlinedText);
Chris@267:         dxy += fontHeight;
Chris@267:     }
Chris@267: 
Chris@267:     if (dys != "") {
Chris@1078:         PaintAssistant::drawVisibleText(this, paint, dxx, dxy, dys, PaintAssistant::OutlinedText);
Chris@267:         dxy += fontHeight;
Chris@267:     }
Chris@278: 
Chris@278:     paint.restore();
Chris@267: }
Chris@267: 
Chris@227: bool
Chris@908: View::render(QPainter &paint, int xorigin, sv_frame_t f0, sv_frame_t f1)
Chris@227: {
Chris@908:     int x0 = int(f0 / m_zoomLevel);
Chris@908:     int x1 = int(f1 / m_zoomLevel);
Chris@806: 
Chris@806:     int w = x1 - x0;
Chris@806: 
Chris@908:     sv_frame_t origCentreFrame = m_centreFrame;
Chris@227: 
Chris@227:     bool someLayersIncomplete = false;
Chris@227: 
Chris@835:     for (LayerList::iterator i = m_layerStack.begin();
Chris@835:          i != m_layerStack.end(); ++i) {
Chris@227: 
Chris@227:         int c = (*i)->getCompletion(this);
Chris@227:         if (c < 100) {
Chris@227:             someLayersIncomplete = true;
Chris@227:             break;
Chris@227:         }
Chris@227:     }
Chris@227: 
Chris@227:     if (someLayersIncomplete) {
Chris@227: 
Chris@227:         QProgressDialog progress(tr("Waiting for layers to be ready..."),
Chris@227:                                  tr("Cancel"), 0, 100, this);
Chris@227:         
Chris@227:         int layerCompletion = 0;
Chris@227: 
Chris@227:         while (layerCompletion < 100) {
Chris@227: 
Chris@835:             for (LayerList::iterator i = m_layerStack.begin();
Chris@835:                  i != m_layerStack.end(); ++i) {
Chris@227: 
Chris@227:                 int c = (*i)->getCompletion(this);
Chris@835:                 if (i == m_layerStack.begin() || c < layerCompletion) {
Chris@227:                     layerCompletion = c;
Chris@227:                 }
Chris@227:             }
Chris@227: 
Chris@227:             if (layerCompletion >= 100) break;
Chris@227: 
Chris@227:             progress.setValue(layerCompletion);
Chris@227:             qApp->processEvents();
Chris@227:             if (progress.wasCanceled()) {
Chris@227:                 update();
Chris@227:                 return false;
Chris@227:             }
Chris@227: 
Chris@227:             usleep(50000);
Chris@227:         }
Chris@227:     }
Chris@227: 
Chris@227:     QProgressDialog progress(tr("Rendering image..."),
Chris@227:                              tr("Cancel"), 0, w / width(), this);
Chris@227: 
Chris@806:     for (int x = 0; x < w; x += width()) {
Chris@227: 
Chris@227:         progress.setValue(x / width());
Chris@227:         qApp->processEvents();
Chris@227:         if (progress.wasCanceled()) {
Chris@227:             m_centreFrame = origCentreFrame;
Chris@227:             update();
Chris@227:             return false;
Chris@227:         }
Chris@227: 
Chris@229:         m_centreFrame = f0 + (x + width()/2) * m_zoomLevel;
Chris@227:         
Chris@227:         QRect chunk(0, 0, width(), height());
Chris@227: 
Chris@287:         paint.setPen(getBackground());
Chris@287:         paint.setBrush(getBackground());
Chris@227: 
Chris@1266:         paint.drawRect(QRect(xorigin + x, 0, width(), height()));
Chris@1266: 
Chris@1266:         paint.setPen(getForeground());
Chris@1266:         paint.setBrush(Qt::NoBrush);
Chris@1266: 
Chris@1266:         for (LayerList::iterator i = m_layerStack.begin();
Chris@835:              i != m_layerStack.end(); ++i) {
Chris@919:             if (!((*i)->isLayerDormant(this))){
Chris@919: 
Chris@919:                 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@919: 
Chris@919:                 paint.save();
Chris@919:                 paint.translate(xorigin + x, 0);
Chris@919: 
Chris@919:                 cerr << "Centre frame now: " << m_centreFrame << " drawing to " << chunk.x() + x + xorigin << ", " << chunk.width() << endl;
Chris@919: 
Chris@919:                 (*i)->setSynchronousPainting(true);
Chris@919: 
Chris@919:                 (*i)->paint(this, paint, chunk);
Chris@919: 
Chris@919:                 (*i)->setSynchronousPainting(false);
Chris@919: 
Chris@919:                 paint.restore();
Chris@919:             }
Chris@1266:         }
Chris@227:     }
Chris@227: 
Chris@227:     m_centreFrame = origCentreFrame;
Chris@227:     update();
Chris@227:     return true;
Chris@227: }
Chris@227: 
Chris@227: QImage *
Chris@1202: View::renderToNewImage()
Chris@227: {
Chris@908:     sv_frame_t f0 = getModelsStartFrame();
Chris@908:     sv_frame_t f1 = getModelsEndFrame();
Chris@227: 
Chris@1202:     return renderPartToNewImage(f0, f1);
Chris@229: }
Chris@229: 
Chris@229: QImage *
Chris@1202: View::renderPartToNewImage(sv_frame_t f0, sv_frame_t f1)
Chris@229: {
Chris@908:     int x0 = int(f0 / getZoomLevel());
Chris@908:     int x1 = int(f1 / getZoomLevel());
Chris@227:     
Chris@227:     QImage *image = new QImage(x1 - x0, height(), QImage::Format_RGB32);
Chris@227: 
Chris@227:     QPainter *paint = new QPainter(image);
Chris@229:     if (!render(*paint, 0, f0, f1)) {
Chris@227:         delete paint;
Chris@227:         delete image;
Chris@227:         return 0;
Chris@227:     } else {
Chris@227:         delete paint;
Chris@227:         return image;
Chris@227:     }
Chris@227: }
Chris@227: 
Chris@229: QSize
Chris@1202: View::getRenderedImageSize()
Chris@229: {
Chris@908:     sv_frame_t f0 = getModelsStartFrame();
Chris@908:     sv_frame_t f1 = getModelsEndFrame();
Chris@229: 
Chris@1202:     return getRenderedPartImageSize(f0, f1);
Chris@229: }
Chris@229:     
Chris@229: QSize
Chris@1202: View::getRenderedPartImageSize(sv_frame_t f0, sv_frame_t f1)
Chris@229: {
Chris@908:     int x0 = int(f0 / getZoomLevel());
Chris@908:     int x1 = int(f1 / getZoomLevel());
Chris@229: 
Chris@229:     return QSize(x1 - x0, height());
Chris@229: }
Chris@229: 
Chris@1202: bool
Chris@1202: View::renderToSvgFile(QString filename)
Chris@1202: {
Chris@1202:     sv_frame_t f0 = getModelsStartFrame();
Chris@1202:     sv_frame_t f1 = getModelsEndFrame();
Chris@1202: 
Chris@1202:     return renderPartToSvgFile(filename, f0, f1);
Chris@1202: }
Chris@1202: 
Chris@1202: bool
Chris@1202: View::renderPartToSvgFile(QString filename, sv_frame_t f0, sv_frame_t f1)
Chris@1202: {
Chris@1202:     int x0 = int(f0 / getZoomLevel());
Chris@1202:     int x1 = int(f1 / getZoomLevel());
Chris@1202: 
Chris@1202:     QSvgGenerator generator;
Chris@1202:     generator.setFileName(filename);
Chris@1202:     generator.setSize(QSize(x1 - x0, height()));
Chris@1202:     generator.setViewBox(QRect(0, 0, x1 - x0, height()));
Chris@1202:     generator.setTitle(tr("Exported image from %1")
Chris@1202:                        .arg(QApplication::applicationName()));
Chris@1202:     
Chris@1202:     QPainter paint;
Chris@1202:     paint.begin(&generator);
Chris@1202:     bool result = render(paint, 0, f0, f1);
Chris@1202:     paint.end();
Chris@1202:     return result;
Chris@1202: }
Chris@1202: 
Chris@316: void
Chris@316: View::toXml(QTextStream &stream,
Chris@316:             QString indent, QString extraAttributes) const
Chris@127: {
Chris@316:     stream << indent;
Chris@127: 
Chris@316:     stream << QString("<view "
Chris@316:                       "centre=\"%1\" "
Chris@316:                       "zoom=\"%2\" "
Chris@316:                       "followPan=\"%3\" "
Chris@316:                       "followZoom=\"%4\" "
Chris@316:                       "tracking=\"%5\" "
Chris@316:                       " %6>\n")
Chris@1266:         .arg(m_centreFrame)
Chris@1266:         .arg(m_zoomLevel)
Chris@1266:         .arg(m_followPan)
Chris@1266:         .arg(m_followZoom)
Chris@1266:         .arg(m_followPlay == PlaybackScrollContinuous ? "scroll" :
Chris@1266:              m_followPlay == PlaybackScrollPageWithCentre ? "page" :
Chris@1266:              m_followPlay == PlaybackScrollPage ? "daw" :
Chris@815:              "ignore")
Chris@1266:         .arg(extraAttributes);
Chris@127: 
Chris@838:     for (int i = 0; i < (int)m_fixedOrderLayers.size(); ++i) {
Chris@838:         bool visible = !m_fixedOrderLayers[i]->isLayerDormant(this);
Chris@838:         m_fixedOrderLayers[i]->toBriefXml(stream, indent + "  ",
Chris@838:                                           QString("visible=\"%1\"")
Chris@838:                                           .arg(visible ? "true" : "false"));
Chris@127:     }
Chris@127: 
Chris@316:     stream << indent + "</view>\n";
Chris@127: }
Chris@127: 
Chris@127: ViewPropertyContainer::ViewPropertyContainer(View *v) :
Chris@127:     m_v(v)
Chris@127: {
Chris@728: //    cerr << "ViewPropertyContainer: " << this << " is owned by View " << v << endl;
Chris@127:     connect(m_v, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@1266:             this, SIGNAL(propertyChanged(PropertyContainer::PropertyName)));
Chris@127: }
Chris@127: 
Chris@728: ViewPropertyContainer::~ViewPropertyContainer()
Chris@728: {
Chris@728: }