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@127: Chris@127: #include "layer/TimeRulerLayer.h" //!!! damn, shouldn't be including that here Chris@128: #include "data/model/PowerOfSqrtTwoZoomConstraint.h" //!!! likewise Chris@127: Chris@127: #include Chris@127: #include Chris@127: #include Chris@127: #include Chris@127: Chris@127: #include Chris@127: #include Chris@127: #include Chris@127: Chris@127: //#define DEBUG_VIEW_WIDGET_PAINT 1 Chris@127: Chris@127: using std::cerr; Chris@127: using std::endl; Chris@127: Chris@127: View::View(QWidget *w, bool showProgress) : Chris@127: QFrame(w), Chris@127: m_centreFrame(0), Chris@127: m_zoomLevel(1024), Chris@127: m_followPan(true), Chris@127: m_followZoom(true), Chris@127: m_followPlay(PlaybackScrollPage), Chris@153: m_playPointerFrame(0), Chris@127: m_lightBackground(true), Chris@127: m_showProgress(showProgress), Chris@127: m_cache(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@127: // QWidget::setAttribute(Qt::WA_PaintOnScreen); Chris@127: } Chris@127: Chris@127: View::~View() Chris@127: { Chris@127: // std::cerr << "View::~View(" << this << ")" << std::endl; Chris@127: Chris@127: m_deleting = true; Chris@127: delete m_propertyContainer; 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@127: int *min, int *max) const Chris@127: { 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@127: if (min) *min = 0; Chris@127: if (max) *max = 2; Chris@127: return int(m_followPlay); Chris@127: } Chris@127: if (min) *min = 0; Chris@127: if (max) *max = 0; Chris@127: return 0; Chris@127: } Chris@127: Chris@127: QString Chris@127: View::getPropertyValueLabel(const PropertyContainer::PropertyName &name, Chris@127: int value) const Chris@127: { Chris@127: if (name == "Follow Playback") { Chris@127: switch (value) { Chris@127: default: Chris@127: case 0: return tr("Scroll"); Chris@127: case 1: return tr("Page"); Chris@127: case 2: return tr("Off"); Chris@127: } Chris@127: } Chris@127: return tr(""); 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@127: setFollowGlobalPan(value != 0); Chris@127: } else if (name == "Global Zoom") { Chris@127: setFollowGlobalZoom(value != 0); Chris@127: } else if (name == "Follow Playback") { Chris@127: switch (value) { Chris@127: default: Chris@127: case 0: setPlaybackFollow(PlaybackScrollContinuous); break; Chris@127: case 1: setPlaybackFollow(PlaybackScrollPage); break; Chris@127: case 2: setPlaybackFollow(PlaybackIgnore); break; Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: size_t Chris@127: View::getPropertyContainerCount() const Chris@127: { Chris@127: return m_layers.size() + 1; // the 1 is for me Chris@127: } Chris@127: Chris@127: const PropertyContainer * Chris@127: View::getPropertyContainer(size_t i) const Chris@127: { Chris@127: return (const PropertyContainer *)(((View *)this)-> Chris@127: getPropertyContainer(i)); Chris@127: } Chris@127: Chris@127: PropertyContainer * Chris@127: View::getPropertyContainer(size_t i) Chris@127: { Chris@127: if (i == 0) return m_propertyContainer; Chris@127: return m_layers[i-1]; Chris@127: } Chris@127: Chris@127: bool Chris@127: View::getValueExtents(QString unit, float &min, float &max, bool &log) const Chris@127: { Chris@127: bool have = false; Chris@127: Chris@127: for (LayerList::const_iterator i = m_layers.begin(); Chris@127: i != m_layers.end(); ++i) { Chris@127: Chris@127: QString layerUnit; Chris@127: float layerMin = 0.0, layerMax = 0.0; Chris@127: float 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 sortedLayers; Chris@127: Chris@127: for (LayerList::const_iterator i = m_layers.begin(); Chris@127: i != m_layers.end(); ++i) { Chris@127: if ((*i)->needsTextLabelHeight()) { Chris@127: sortedLayers[getObjectExportId(*i)] = *i; Chris@127: } Chris@127: } Chris@127: Chris@127: int y = 15 + paint.fontMetrics().ascent(); Chris@127: Chris@127: for (std::map::const_iterator i = sortedLayers.begin(); Chris@127: i != sortedLayers.end(); ++i) { Chris@127: if (i->second == layer) return y; 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@127: if (m_haveSelectedLayer) { Chris@127: m_haveSelectedLayer = false; Chris@127: update(); Chris@127: } Chris@127: 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@127: for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@127: if (*i == pc) { Chris@127: selectedLayer = *i; Chris@127: m_layers.erase(i); Chris@127: break; Chris@127: } Chris@127: } Chris@127: Chris@127: if (selectedLayer) { Chris@127: m_haveSelectedLayer = true; Chris@127: m_layers.push_back(selectedLayer); Chris@127: update(); Chris@127: } else { Chris@127: m_haveSelectedLayer = false; Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@127: View::toolModeChanged() Chris@127: { Chris@127: // std::cerr << "View::toolModeChanged(" << m_manager->getToolMode() << ")" << std::endl; Chris@127: } Chris@127: Chris@133: void Chris@133: View::overlayModeChanged() Chris@133: { 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@127: long Chris@127: View::getStartFrame() const Chris@127: { Chris@127: size_t w2 = (width() / 2) * m_zoomLevel; Chris@127: size_t frame = m_centreFrame; Chris@127: if (frame >= w2) { Chris@127: frame -= w2; Chris@127: return (frame / m_zoomLevel * m_zoomLevel); Chris@127: } else { Chris@127: frame = w2 - frame; Chris@127: frame = frame / m_zoomLevel * m_zoomLevel; Chris@127: return -(long)frame - m_zoomLevel; Chris@127: } Chris@127: } Chris@127: Chris@127: size_t Chris@127: View::getEndFrame() const Chris@127: { Chris@127: return getFrameForX(width()) - 1; Chris@127: } Chris@127: Chris@127: void Chris@127: View::setStartFrame(long f) Chris@127: { Chris@127: setCentreFrame(f + m_zoomLevel * (width() / 2)); Chris@127: } Chris@127: Chris@127: bool Chris@127: View::setCentreFrame(size_t f, bool e) Chris@127: { Chris@127: bool changeVisible = false; Chris@127: Chris@127: if (m_centreFrame != f) { Chris@127: Chris@127: int formerPixel = m_centreFrame / m_zoomLevel; Chris@127: Chris@127: m_centreFrame = f; Chris@127: Chris@127: int newPixel = m_centreFrame / m_zoomLevel; Chris@127: Chris@127: if (newPixel != formerPixel) { Chris@127: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cout << "View(" << this << ")::setCentreFrame: newPixel " << newPixel << ", formerPixel " << formerPixel << std::endl; Chris@127: #endif Chris@127: update(); Chris@127: Chris@127: changeVisible = true; Chris@127: } Chris@127: Chris@127: if (e) emit centreFrameChanged(this, f, m_followPan); Chris@127: } Chris@127: Chris@127: return changeVisible; Chris@127: } Chris@127: Chris@127: int Chris@127: View::getXForFrame(long frame) const Chris@127: { Chris@127: return (frame - getStartFrame()) / m_zoomLevel; Chris@127: } Chris@127: Chris@127: long Chris@127: View::getFrameForX(int x) const Chris@127: { Chris@127: return (long(x) * long(m_zoomLevel)) + getStartFrame(); Chris@127: } Chris@127: Chris@127: float Chris@127: View::getYForFrequency(float frequency, Chris@127: float minf, Chris@127: float maxf, Chris@127: bool logarithmic) const Chris@127: { Chris@127: int h = height(); Chris@127: Chris@127: if (logarithmic) { Chris@127: Chris@127: static float lastminf = 0.0, lastmaxf = 0.0; Chris@127: static float logminf = 0.0, logmaxf = 0.0; Chris@127: Chris@127: if (lastminf != minf) { Chris@127: lastminf = (minf == 0.0 ? 1.0 : minf); Chris@127: logminf = log10f(minf); Chris@127: } Chris@127: if (lastmaxf != maxf) { Chris@127: lastmaxf = (maxf < lastminf ? lastminf : maxf); Chris@127: logmaxf = log10f(maxf); Chris@127: } Chris@127: Chris@127: if (logminf == logmaxf) return 0; Chris@127: return h - (h * (log10f(frequency) - logminf)) / (logmaxf - logminf); Chris@127: Chris@127: } else { Chris@127: Chris@127: if (minf == maxf) return 0; Chris@127: return h - (h * (frequency - minf)) / (maxf - minf); Chris@127: } Chris@127: } Chris@127: Chris@127: float Chris@127: View::getFrequencyForY(int y, Chris@127: float minf, Chris@127: float maxf, Chris@127: bool logarithmic) const Chris@127: { Chris@127: int h = height(); Chris@127: Chris@127: if (logarithmic) { Chris@127: Chris@127: static float lastminf = 0.0, lastmaxf = 0.0; Chris@127: static float logminf = 0.0, logmaxf = 0.0; Chris@127: Chris@127: if (lastminf != minf) { Chris@127: lastminf = (minf == 0.0 ? 1.0 : minf); Chris@127: logminf = log10f(minf); Chris@127: } Chris@127: if (lastmaxf != maxf) { Chris@127: lastmaxf = (maxf < lastminf ? lastminf : maxf); Chris@127: logmaxf = log10f(maxf); Chris@127: } Chris@127: Chris@127: if (logminf == logmaxf) return 0; Chris@127: return pow(10.f, logminf + ((logmaxf - logminf) * (h - y)) / h); Chris@127: Chris@127: } else { Chris@127: Chris@127: if (minf == maxf) return 0; Chris@127: return minf + ((h - y) * (maxf - minf)) / h; Chris@127: } Chris@127: } Chris@127: Chris@127: int Chris@127: View::getZoomLevel() const Chris@127: { Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cout << "zoom level: " << m_zoomLevel << std::endl; Chris@127: #endif Chris@127: return m_zoomLevel; Chris@127: } Chris@127: Chris@127: void Chris@127: View::setZoomLevel(size_t z) Chris@127: { Chris@127: if (m_zoomLevel != int(z)) { Chris@127: m_zoomLevel = z; Chris@127: emit zoomLevelChanged(this, z, m_followZoom); Chris@127: update(); Chris@127: } Chris@127: } Chris@127: Chris@127: View::LayerProgressBar::LayerProgressBar(QWidget *parent) : Chris@127: QProgressBar(parent) Chris@127: { Chris@127: QFont f(font()); Chris@127: f.setPointSize(f.pointSize() * 8 / 10); Chris@127: setFont(f); Chris@127: } Chris@127: Chris@127: void Chris@127: View::addLayer(Layer *layer) Chris@127: { Chris@127: delete m_cache; Chris@127: m_cache = 0; Chris@127: Chris@127: m_layers.push_back(layer); Chris@127: Chris@127: m_progressBars[layer] = new LayerProgressBar(this); Chris@127: m_progressBars[layer]->setMinimum(0); Chris@127: m_progressBars[layer]->setMaximum(100); Chris@127: m_progressBars[layer]->setMinimumWidth(80); Chris@127: m_progressBars[layer]->hide(); Chris@127: Chris@127: connect(layer, SIGNAL(layerParametersChanged()), Chris@127: this, SLOT(layerParametersChanged())); Chris@197: connect(layer, SIGNAL(layerParameterRangesChanged()), Chris@197: this, SLOT(layerParameterRangesChanged())); Chris@127: connect(layer, SIGNAL(layerNameChanged()), Chris@127: this, SLOT(layerNameChanged())); Chris@127: connect(layer, SIGNAL(modelChanged()), Chris@127: this, SLOT(modelChanged())); Chris@127: connect(layer, SIGNAL(modelCompletionChanged()), Chris@127: this, SLOT(modelCompletionChanged())); Chris@127: connect(layer, SIGNAL(modelChanged(size_t, size_t)), Chris@127: this, SLOT(modelChanged(size_t, size_t))); Chris@127: connect(layer, SIGNAL(modelReplaced()), Chris@127: 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@127: return; Chris@127: } Chris@127: Chris@127: delete m_cache; Chris@127: m_cache = 0; Chris@127: Chris@127: for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@127: if (*i == layer) { Chris@127: m_layers.erase(i); Chris@127: if (m_progressBars.find(layer) != m_progressBars.end()) { Chris@127: delete m_progressBars[layer]; Chris@127: m_progressBars.erase(layer); Chris@127: } Chris@127: break; Chris@127: } 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@197: disconnect(layer, SIGNAL(modelChanged(size_t, size_t)), Chris@197: this, SLOT(modelChanged(size_t, size_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@127: View::getSelectedLayer() Chris@127: { Chris@127: if (m_haveSelectedLayer && !m_layers.empty()) { Chris@127: return getLayer(getLayerCount() - 1); Chris@127: } else { Chris@127: 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_cast(this)->getSelectedLayer()); Chris@127: } Chris@127: Chris@127: void Chris@127: View::setViewManager(ViewManager *manager) Chris@127: { Chris@127: if (m_manager) { Chris@127: m_manager->disconnect(this, SLOT(viewManagerCentreFrameChanged(void *, unsigned long, bool))); Chris@127: m_manager->disconnect(this, SLOT(viewManagerZoomLevelChanged(void *, unsigned long, bool))); Chris@127: disconnect(m_manager, SIGNAL(centreFrameChanged(void *, unsigned long, bool))); Chris@127: disconnect(m_manager, SIGNAL(zoomLevelChanged(void *, unsigned long, bool))); Chris@127: disconnect(m_manager, SIGNAL(toolModeChanged())); Chris@127: disconnect(m_manager, SIGNAL(selectionChanged())); Chris@127: disconnect(m_manager, SIGNAL(inProgressSelectionChanged())); Chris@127: } Chris@127: Chris@127: m_manager = manager; Chris@127: if (m_followPan) setCentreFrame(m_manager->getGlobalCentreFrame(), false); Chris@127: if (m_followZoom) setZoomLevel(m_manager->getGlobalZoom()); Chris@127: Chris@127: connect(m_manager, SIGNAL(centreFrameChanged(void *, unsigned long, bool)), Chris@127: this, SLOT(viewManagerCentreFrameChanged(void *, unsigned long, bool))); Chris@127: connect(m_manager, SIGNAL(playbackFrameChanged(unsigned long)), Chris@127: this, SLOT(viewManagerPlaybackFrameChanged(unsigned long))); Chris@127: connect(m_manager, SIGNAL(zoomLevelChanged(void *, unsigned long, bool)), Chris@127: this, SLOT(viewManagerZoomLevelChanged(void *, unsigned long, bool))); Chris@127: connect(m_manager, SIGNAL(toolModeChanged()), Chris@127: this, SLOT(toolModeChanged())); Chris@127: connect(m_manager, SIGNAL(selectionChanged()), Chris@127: this, SLOT(selectionChanged())); Chris@127: connect(m_manager, SIGNAL(inProgressSelectionChanged()), Chris@127: this, SLOT(selectionChanged())); Chris@127: connect(m_manager, SIGNAL(overlayModeChanged()), Chris@133: this, SLOT(overlayModeChanged())); Chris@133: connect(m_manager, SIGNAL(zoomWheelsEnabledChanged()), Chris@133: this, SLOT(zoomWheelsEnabledChanged())); Chris@127: Chris@127: connect(this, SIGNAL(centreFrameChanged(void *, unsigned long, bool)), Chris@127: m_manager, SIGNAL(centreFrameChanged(void *, unsigned long, bool))); Chris@127: connect(this, SIGNAL(zoomLevelChanged(void *, unsigned long, bool)), Chris@127: m_manager, SIGNAL(zoomLevelChanged(void *, unsigned long, bool))); Chris@127: Chris@127: toolModeChanged(); Chris@127: } Chris@127: Chris@127: 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::drawVisibleText(QPainter &paint, int x, int y, QString text, TextStyle style) Chris@127: { Chris@127: if (style == OutlinedText) { Chris@127: Chris@127: QColor origPenColour = paint.pen().color(); Chris@127: QColor penColour = origPenColour; Chris@127: QColor surroundColour = Qt::white; //palette().background().color(); Chris@127: Chris@127: if (!hasLightBackground()) { Chris@127: int h, s, v; Chris@127: penColour.getHsv(&h, &s, &v); Chris@127: penColour = QColor::fromHsv(h, s, 255 - v); Chris@127: surroundColour = Qt::black; Chris@127: } Chris@127: Chris@127: paint.setPen(surroundColour); Chris@127: Chris@127: for (int dx = -1; dx <= 1; ++dx) { Chris@127: for (int dy = -1; dy <= 1; ++dy) { Chris@127: if (!(dx || dy)) continue; Chris@127: paint.drawText(x + dx, y + dy, text); Chris@127: } Chris@127: } Chris@127: Chris@127: paint.setPen(penColour); Chris@127: Chris@127: paint.drawText(x, y, text); Chris@127: Chris@127: paint.setPen(origPenColour); Chris@127: Chris@127: } else { Chris@127: Chris@127: std::cerr << "ERROR: View::drawVisibleText: Boxed style not yet implemented!" << std::endl; Chris@127: } 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@127: std::cerr << "View(" << this << ")::modelChanged()" << std::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@127: i != scrollables.end(); ++i) { Chris@127: if (*i == obj || (*i)->getModel() == obj) { Chris@127: recreate = true; Chris@127: break; Chris@127: } Chris@127: } Chris@127: Chris@127: if (recreate) { Chris@127: delete m_cache; Chris@127: m_cache = 0; Chris@127: } Chris@127: Chris@127: checkProgress(obj); Chris@127: Chris@127: update(); Chris@127: } Chris@127: Chris@127: void Chris@127: View::modelChanged(size_t startFrame, size_t endFrame) Chris@127: { Chris@127: QObject *obj = sender(); Chris@127: Chris@127: long myStartFrame = getStartFrame(); Chris@127: size_t myEndFrame = getEndFrame(); Chris@127: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << "View(" << this << ")::modelChanged(" << startFrame << "," << endFrame << ") [me " << myStartFrame << "," << myEndFrame << "]" << std::endl; Chris@127: #endif Chris@127: Chris@127: if (myStartFrame > 0 && endFrame < size_t(myStartFrame)) { Chris@127: checkProgress(obj); Chris@127: return; Chris@127: } Chris@127: if (startFrame > myEndFrame) { Chris@127: checkProgress(obj); Chris@127: 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@127: i != scrollables.end(); ++i) { Chris@127: if (*i == obj || (*i)->getModel() == obj) { Chris@127: recreate = true; Chris@127: break; Chris@127: } Chris@127: } Chris@127: Chris@127: if (recreate) { Chris@127: delete m_cache; Chris@127: m_cache = 0; Chris@127: } Chris@127: Chris@127: if (long(startFrame) < myStartFrame) startFrame = myStartFrame; Chris@127: if (endFrame > myEndFrame) endFrame = myEndFrame; Chris@127: Chris@127: int x0 = getXForFrame(startFrame); Chris@127: int x1 = getXForFrame(endFrame + 1); Chris@127: if (x1 < x0) x1 = x0; Chris@127: Chris@127: checkProgress(obj); Chris@127: Chris@127: update(); Chris@127: //!!! update(x0, 0, x1 - x0 + 1, height()); Chris@127: } Chris@127: Chris@127: void Chris@127: View::modelCompletionChanged() Chris@127: { Chris@127: QObject *obj = sender(); Chris@127: checkProgress(obj); Chris@127: } Chris@127: Chris@127: void Chris@127: View::modelReplaced() Chris@127: { Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << "View(" << this << ")::modelReplaced()" << std::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(sender()); Chris@127: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << "View::layerParametersChanged()" << std::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@127: emit propertyContainerPropertyChanged(layer); Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@197: View::layerParameterRangesChanged() Chris@197: { Chris@197: Layer *layer = dynamic_cast(sender()); Chris@197: if (layer) emit propertyContainerPropertyRangeChanged(layer); Chris@197: } Chris@197: Chris@197: void Chris@127: View::layerNameChanged() Chris@127: { Chris@127: Layer *layer = dynamic_cast(sender()); Chris@127: if (layer) emit propertyContainerNameChanged(layer); Chris@127: } Chris@127: Chris@127: void Chris@127: View::viewManagerCentreFrameChanged(void *p, unsigned long f, bool locked) Chris@127: { Chris@127: if (m_followPan && p != this && locked) { Chris@127: if (m_manager && (sender() == m_manager)) { Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << this << ": manager frame changed " << f << " from " << p << std::endl; Chris@127: #endif Chris@127: setCentreFrame(f); Chris@127: if (p == this) repaint(); Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@127: View::viewManagerPlaybackFrameChanged(unsigned long f) Chris@127: { Chris@127: if (m_manager) { Chris@127: if (sender() != m_manager) return; Chris@127: } Chris@127: Chris@127: if (m_playPointerFrame == f) return; Chris@127: bool visible = (getXForFrame(m_playPointerFrame) != getXForFrame(f)); Chris@127: size_t oldPlayPointerFrame = m_playPointerFrame; Chris@127: m_playPointerFrame = f; Chris@127: if (!visible) return; Chris@127: Chris@127: switch (m_followPlay) { Chris@127: Chris@127: case PlaybackScrollContinuous: Chris@127: if (QApplication::mouseButtons() == Qt::NoButton) { Chris@180: setCentreFrame(f, true); //!!! Chris@127: } Chris@127: break; Chris@127: Chris@127: case PlaybackScrollPage: Chris@127: { Chris@127: int xold = getXForFrame(oldPlayPointerFrame); Chris@127: update(xold - 1, 0, 3, height()); Chris@127: Chris@127: long w = getEndFrame() - getStartFrame(); Chris@127: w -= w/5; Chris@127: long sf = (f / w) * w - w/8; Chris@127: Chris@127: if (m_manager && Chris@127: m_manager->isPlaying() && Chris@127: m_manager->getPlaySelectionMode()) { Chris@127: MultiSelection::SelectionList selections = m_manager->getSelections(); Chris@127: if (!selections.empty()) { Chris@127: size_t selectionStart = selections.begin()->getStartFrame(); Chris@127: if (sf < long(selectionStart) - w / 10) { Chris@127: sf = long(selectionStart) - w / 10; Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << "PlaybackScrollPage: f = " << f << ", sf = " << sf << ", start frame " Chris@127: << getStartFrame() << std::endl; Chris@127: #endif Chris@127: Chris@127: // We don't consider scrolling unless the pointer is outside Chris@127: // the clearly visible range already Chris@127: Chris@127: int xnew = getXForFrame(m_playPointerFrame); Chris@127: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << "xnew = " << xnew << ", width = " << width() << std::endl; Chris@127: #endif Chris@127: Chris@127: if (xnew < width()/8 || xnew > (width()*7)/8) { Chris@127: if (QApplication::mouseButtons() == Qt::NoButton) { Chris@127: long offset = getFrameForX(width()/2) - getStartFrame(); Chris@127: long newCentre = sf + offset; Chris@127: bool changed = setCentreFrame(newCentre, false); Chris@127: if (changed) { Chris@127: xold = getXForFrame(oldPlayPointerFrame); Chris@127: update(xold - 1, 0, 3, height()); Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: update(xnew - 1, 0, 3, height()); Chris@127: Chris@127: break; Chris@127: } Chris@127: Chris@127: case PlaybackIgnore: Chris@127: if (long(f) >= getStartFrame() && f < getEndFrame()) { Chris@127: update(); Chris@127: } Chris@127: break; Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@127: View::viewManagerZoomLevelChanged(void *p, unsigned long z, bool locked) Chris@127: { Chris@127: if (m_followZoom && p != this && locked) { Chris@127: if (m_manager && (sender() == m_manager)) { Chris@127: setZoomLevel(z); Chris@127: if (p == this) repaint(); Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@127: View::selectionChanged() Chris@127: { Chris@127: if (m_selectionCached) { Chris@127: delete m_cache; Chris@127: m_cache = 0; Chris@127: m_selectionCached = false; Chris@127: } Chris@127: update(); Chris@127: } Chris@127: Chris@127: size_t Chris@127: View::getModelsStartFrame() const Chris@127: { Chris@127: bool first = true; Chris@127: size_t startFrame = 0; Chris@127: Chris@127: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@127: Chris@127: if ((*i)->getModel() && (*i)->getModel()->isOK()) { Chris@127: Chris@127: size_t thisStartFrame = (*i)->getModel()->getStartFrame(); Chris@127: Chris@127: if (first || thisStartFrame < startFrame) { Chris@127: startFrame = thisStartFrame; Chris@127: } Chris@127: first = false; Chris@127: } Chris@127: } Chris@127: return startFrame; Chris@127: } Chris@127: Chris@127: size_t Chris@127: View::getModelsEndFrame() const Chris@127: { Chris@127: bool first = true; Chris@127: size_t endFrame = 0; Chris@127: Chris@127: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@127: Chris@127: if ((*i)->getModel() && (*i)->getModel()->isOK()) { Chris@127: Chris@127: size_t thisEndFrame = (*i)->getModel()->getEndFrame(); Chris@127: Chris@127: if (first || thisEndFrame > endFrame) { Chris@127: endFrame = thisEndFrame; Chris@127: } Chris@127: first = false; Chris@127: } Chris@127: } Chris@127: Chris@127: if (first) return getModelsStartFrame(); Chris@127: return endFrame; Chris@127: } Chris@127: Chris@127: int 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@127: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@127: if ((*i)->getModel() && (*i)->getModel()->isOK()) { Chris@127: return (*i)->getModel()->getSampleRate(); Chris@127: } Chris@127: } Chris@127: return 0; Chris@127: } Chris@127: Chris@127: bool Chris@127: View::areLayersScrollable() const Chris@127: { Chris@127: // True iff all views are scrollable Chris@127: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@127: 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@127: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@132: // std::cerr << "View::getScrollableBackLayers: calling isLayerDormant on layer " << *i << std::endl; Chris@132: // std::cerr << "(name is " << (*i)->objectName().toStdString() << ")" Chris@132: // << std::endl; Chris@132: // std::cerr << "View::getScrollableBackLayers: I am " << this << std::endl; Chris@127: if ((*i)->isLayerDormant(this)) continue; Chris@127: if ((*i)->isLayerOpaque()) { Chris@127: // You can't see anything behind an opaque layer! Chris@127: scrollables.clear(); Chris@127: if (metUnscrollable) break; Chris@127: } Chris@127: 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@127: m_lastScrollableBackLayers = scrollables; Chris@127: 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@127: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@127: if ((*i)->isLayerDormant(this)) continue; Chris@127: if (!started && (*i)->isLayerScrollable(this)) { Chris@127: continue; Chris@127: } Chris@127: started = true; Chris@127: if ((*i)->isLayerOpaque()) { Chris@127: // You can't see anything behind an opaque layer! Chris@127: nonScrollables.clear(); Chris@127: } Chris@127: nonScrollables.push_back(*i); Chris@127: } Chris@127: Chris@127: if (testChanged && nonScrollables != m_lastNonScrollableBackLayers) { Chris@127: m_lastNonScrollableBackLayers = nonScrollables; Chris@127: changed = true; Chris@127: } Chris@127: Chris@127: return nonScrollables; Chris@127: } Chris@127: Chris@127: size_t Chris@127: View::getZoomConstraintBlockSize(size_t blockSize, Chris@127: ZoomConstraint::RoundingDirection dir) Chris@127: const Chris@127: { Chris@127: size_t candidate = blockSize; Chris@127: bool haveCandidate = false; Chris@127: Chris@127: PowerOfSqrtTwoZoomConstraint defaultZoomConstraint; Chris@127: Chris@127: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@127: Chris@127: const ZoomConstraint *zoomConstraint = (*i)->getZoomConstraint(); Chris@127: if (!zoomConstraint) zoomConstraint = &defaultZoomConstraint; Chris@127: Chris@127: size_t thisBlockSize = Chris@127: zoomConstraint->getNearestBlockSize(blockSize, dir); Chris@127: Chris@127: // Go for the block size that's furthest from the one Chris@127: // passed in. Most of the time, that's what we want. Chris@127: if (!haveCandidate || Chris@127: (thisBlockSize > blockSize && thisBlockSize > candidate) || Chris@127: (thisBlockSize < blockSize && thisBlockSize < candidate)) { Chris@127: candidate = thisBlockSize; Chris@127: haveCandidate = true; Chris@127: } Chris@127: } Chris@127: Chris@127: return candidate; Chris@127: } Chris@127: Chris@183: bool Chris@183: View::areLayerColoursSignificant() const Chris@183: { Chris@183: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@183: if ((*i)->isLayerColourSignificant()) return true; Chris@183: if ((*i)->isLayerOpaque()) break; Chris@183: } Chris@183: return false; Chris@183: } Chris@183: Chris@127: void Chris@127: View::zoom(bool in) Chris@127: { Chris@127: int newZoomLevel = m_zoomLevel; Chris@127: Chris@127: if (in) { Chris@127: newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1, Chris@127: ZoomConstraint::RoundDown); Chris@127: } else { Chris@127: newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1, Chris@127: ZoomConstraint::RoundUp); Chris@127: } Chris@127: Chris@127: if (newZoomLevel != m_zoomLevel) { Chris@127: setZoomLevel(newZoomLevel); Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@127: View::scroll(bool right, bool lots) Chris@127: { Chris@127: long delta; Chris@127: if (lots) { Chris@127: delta = (getEndFrame() - getStartFrame()) / 2; Chris@127: } else { Chris@127: delta = (getEndFrame() - getStartFrame()) / 20; Chris@127: } Chris@127: if (right) delta = -delta; Chris@127: Chris@127: if (int(m_centreFrame) < delta) { Chris@127: setCentreFrame(0); Chris@127: } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) { Chris@127: setCentreFrame(getModelsEndFrame()); Chris@127: } else { Chris@127: setCentreFrame(m_centreFrame - delta); Chris@127: } Chris@127: } Chris@127: Chris@127: 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@127: for (ProgressMap::const_iterator i = m_progressBars.begin(); Chris@127: i != m_progressBars.end(); ++i) { Chris@127: Chris@127: if (i->first == object) { Chris@127: Chris@127: int completion = i->first->getCompletion(this); Chris@127: Chris@127: if (completion >= 100) { Chris@127: Chris@127: i->second->hide(); Chris@127: Chris@127: } else { Chris@127: Chris@127: i->second->setText(i->first->getPropertyContainerName()); Chris@132: Chris@127: i->second->setValue(completion); Chris@127: i->second->move(0, ph - i->second->height()); Chris@127: Chris@127: i->second->show(); Chris@127: i->second->update(); Chris@127: Chris@127: ph -= i->second->height(); Chris@127: } Chris@127: } else { Chris@127: if (i->second->isVisible()) { Chris@127: ph -= i->second->height(); Chris@127: } Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@127: View::paintEvent(QPaintEvent *e) Chris@127: { Chris@127: // Profiler prof("View::paintEvent", false); Chris@127: // std::cerr << "View::paintEvent" << std::endl; Chris@127: Chris@127: if (m_layers.empty()) { Chris@127: QFrame::paintEvent(e); Chris@127: 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@127: 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@127: cacheRect &= e->rect(); Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << "paint rect " << cacheRect.width() << "x" << cacheRect.height() Chris@127: << ", my rect " << width() << "x" << height() << std::endl; Chris@127: #endif Chris@127: } Chris@127: Chris@127: QRect nonCacheRect(cacheRect); Chris@127: 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: bool selectionDrawn = false; 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@127: // opaque, however, we can't cache. Chris@127: // Chris@127: if (!selectionCacheable) { Chris@127: selectionCacheable = true; Chris@127: for (LayerList::const_iterator i = nonScrollables.begin(); Chris@127: i != nonScrollables.end(); ++i) { Chris@127: if ((*i)->isLayerOpaque()) { Chris@127: selectionCacheable = false; Chris@127: break; Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: if (selectionCacheable) { Chris@127: QPoint localPos; Chris@127: bool closeToLeft, closeToRight; Chris@127: if (shouldIlluminateLocalSelection(localPos, closeToLeft, closeToRight)) { Chris@127: selectionCacheable = false; Chris@127: } Chris@127: } Chris@127: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << "View(" << this << ")::paintEvent: have " << scrollables.size() Chris@127: << " scrollable back layers and " << nonScrollables.size() Chris@127: << " non-scrollable front layers" << std::endl; Chris@127: std::cerr << "haveSelections " << haveSelections << ", selectionCacheable " Chris@127: << selectionCacheable << ", m_selectionCached " << m_selectionCached << std::endl; Chris@127: #endif Chris@127: Chris@127: if (layersChanged || scrollables.empty() || Chris@127: (haveSelections && (selectionCacheable != m_selectionCached))) { Chris@127: delete m_cache; Chris@127: m_cache = 0; Chris@127: m_selectionCached = false; Chris@127: } Chris@127: Chris@127: if (!scrollables.empty()) { Chris@127: if (!m_cache || Chris@127: m_cacheZoomLevel != m_zoomLevel || Chris@127: width() != m_cache->width() || Chris@127: height() != m_cache->height()) { Chris@127: Chris@127: // cache is not valid Chris@127: Chris@127: if (cacheRect.width() < width()/10) { Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << "View(" << this << ")::paintEvent: small repaint, not bothering to recreate cache" << std::endl; Chris@127: #endif Chris@127: } else { Chris@127: delete m_cache; Chris@127: m_cache = new QPixmap(width(), height()); Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << "View(" << this << ")::paintEvent: recreated cache" << std::endl; Chris@127: #endif Chris@127: cacheRect = rect(); Chris@127: repaintCache = true; Chris@127: } Chris@127: Chris@127: } else if (m_cacheCentreFrame != m_centreFrame) { Chris@127: Chris@127: long dx = Chris@127: getXForFrame(m_cacheCentreFrame) - Chris@127: getXForFrame(m_centreFrame); Chris@127: Chris@127: if (dx > -width() && dx < width()) { Chris@127: #if defined(Q_WS_WIN32) || defined(Q_WS_MAC) Chris@127: // Copying a pixmap to itself doesn't work properly on Windows Chris@127: // or Mac (it only works when moving in one direction) Chris@127: static QPixmap *tmpPixmap = 0; Chris@127: if (!tmpPixmap || Chris@127: tmpPixmap->width() != width() || Chris@127: tmpPixmap->height() != height()) { Chris@127: delete tmpPixmap; Chris@127: tmpPixmap = new QPixmap(width(), height()); Chris@127: } Chris@127: paint.begin(tmpPixmap); Chris@127: paint.drawPixmap(0, 0, *m_cache); Chris@127: paint.end(); Chris@127: paint.begin(m_cache); Chris@127: paint.drawPixmap(dx, 0, *tmpPixmap); Chris@127: paint.end(); Chris@127: #else Chris@127: // But it seems to be fine on X11 Chris@127: paint.begin(m_cache); Chris@127: paint.drawPixmap(dx, 0, *m_cache); Chris@127: paint.end(); Chris@127: #endif Chris@127: Chris@127: if (dx < 0) { Chris@127: cacheRect = QRect(width() + dx, 0, -dx, height()); Chris@127: } else { Chris@127: cacheRect = QRect(0, 0, dx, height()); Chris@127: } Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << "View(" << this << ")::paintEvent: scrolled cache by " << dx << std::endl; Chris@127: #endif Chris@127: } else { Chris@127: cacheRect = rect(); Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << "View(" << this << ")::paintEvent: scrolling too far" << std::endl; Chris@127: #endif Chris@127: } Chris@127: repaintCache = true; Chris@127: Chris@127: } else { Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << "View(" << this << ")::paintEvent: cache is good" << std::endl; Chris@127: #endif Chris@127: paint.begin(this); Chris@127: paint.drawPixmap(cacheRect, *m_cache, cacheRect); Chris@127: paint.end(); Chris@127: QFrame::paintEvent(e); Chris@127: paintedCacheRect = true; Chris@127: } Chris@127: Chris@127: m_cacheCentreFrame = m_centreFrame; Chris@127: m_cacheZoomLevel = m_zoomLevel; Chris@127: } Chris@127: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: // std::cerr << "View(" << this << ")::paintEvent: cacheRect " << cacheRect << ", nonCacheRect " << (nonCacheRect | cacheRect) << ", repaintCache " << repaintCache << ", paintedCacheRect " << paintedCacheRect << std::endl; Chris@127: #endif Chris@127: Chris@127: // Scrollable (cacheable) items first Chris@127: Chris@127: if (!paintedCacheRect) { Chris@127: Chris@127: if (repaintCache) paint.begin(m_cache); Chris@127: else paint.begin(this); Chris@127: Chris@127: paint.setClipRect(cacheRect); Chris@127: Chris@127: if (hasLightBackground()) { Chris@127: paint.setPen(Qt::white); Chris@127: paint.setBrush(Qt::white); Chris@127: } else { Chris@127: paint.setPen(Qt::black); Chris@127: paint.setBrush(Qt::black); Chris@127: } Chris@127: paint.drawRect(cacheRect); Chris@127: Chris@127: paint.setPen(Qt::black); Chris@127: paint.setBrush(Qt::NoBrush); Chris@127: Chris@127: for (LayerList::iterator i = scrollables.begin(); i != scrollables.end(); ++i) { Chris@127: paint.setRenderHint(QPainter::Antialiasing, false); Chris@127: paint.save(); Chris@127: (*i)->paint(this, paint, cacheRect); Chris@127: paint.restore(); Chris@127: } Chris@127: Chris@127: if (haveSelections && selectionCacheable) { Chris@127: drawSelections(paint); Chris@127: m_selectionCached = repaintCache; Chris@127: selectionDrawn = true; Chris@127: } Chris@127: Chris@127: paint.end(); Chris@127: Chris@127: if (repaintCache) { Chris@127: cacheRect |= (e ? e->rect() : rect()); Chris@127: paint.begin(this); Chris@127: paint.drawPixmap(cacheRect, *m_cache, cacheRect); Chris@127: paint.end(); Chris@127: } 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@127: paint.begin(this); Chris@127: paint.setClipRect(nonCacheRect); Chris@127: Chris@127: if (scrollables.empty()) { Chris@127: if (hasLightBackground()) { Chris@127: paint.setPen(Qt::white); Chris@127: paint.setBrush(Qt::white); Chris@127: } else { Chris@127: paint.setPen(Qt::black); Chris@127: paint.setBrush(Qt::black); Chris@127: } Chris@127: paint.drawRect(nonCacheRect); Chris@127: } Chris@127: Chris@127: paint.setPen(Qt::black); Chris@127: paint.setBrush(Qt::NoBrush); Chris@127: Chris@127: for (LayerList::iterator i = nonScrollables.begin(); i != nonScrollables.end(); ++i) { Chris@127: // Profiler profiler2("View::paintEvent non-cacheable"); Chris@127: (*i)->paint(this, paint, nonCacheRect); Chris@127: } Chris@127: Chris@127: paint.end(); Chris@127: Chris@127: paint.begin(this); Chris@127: if (e) paint.setClipRect(e->rect()); Chris@127: if (!m_selectionCached) { Chris@127: drawSelections(paint); Chris@127: } Chris@127: paint.end(); Chris@127: Chris@127: if (m_followPlay != PlaybackScrollContinuous) { Chris@127: Chris@127: paint.begin(this); Chris@127: Chris@127: if (long(m_playPointerFrame) > getStartFrame() && Chris@127: m_playPointerFrame < getEndFrame()) { Chris@127: Chris@127: int playx = getXForFrame(m_playPointerFrame); Chris@127: Chris@127: paint.setPen(Qt::black); Chris@127: paint.drawLine(playx - 1, 0, playx - 1, height() - 1); Chris@127: paint.drawLine(playx + 1, 0, playx + 1, height() - 1); Chris@127: paint.drawPoint(playx, 0); Chris@127: paint.drawPoint(playx, height() - 1); Chris@127: paint.setPen(Qt::white); Chris@127: paint.drawLine(playx, 1, playx, height() - 2); Chris@127: } Chris@127: Chris@127: 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@127: MultiSelection::SelectionList selections; Chris@127: Chris@127: if (m_manager) { Chris@127: selections = m_manager->getSelections(); Chris@127: if (m_manager->haveInProgressSelection()) { Chris@127: bool exclusive; Chris@127: Selection inProgressSelection = Chris@127: m_manager->getInProgressSelection(exclusive); Chris@127: if (exclusive) selections.clear(); Chris@127: selections.insert(inProgressSelection); Chris@127: } 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@127: int sampleRate = getModelsSampleRate(); Chris@127: Chris@127: QPoint localPos; Chris@127: long illuminateFrame = -1; Chris@127: bool closeToLeft, closeToRight; Chris@127: Chris@127: if (shouldIlluminateLocalSelection(localPos, closeToLeft, closeToRight)) { Chris@127: 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@127: i != selections.end(); ++i) { Chris@127: Chris@127: int p0 = getXForFrame(i->getStartFrame()); Chris@127: int p1 = getXForFrame(i->getEndFrame()); Chris@127: Chris@127: if (p1 < 0 || p0 > width()) continue; Chris@127: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@127: std::cerr << "View::drawSelections: " << p0 << ",-1 [" << (p1-p0) << "x" << (height()+1) << "]" << std::endl; Chris@127: #endif Chris@127: Chris@127: bool illuminateThis = Chris@127: (illuminateFrame >= 0 && i->contains(illuminateFrame)); Chris@127: Chris@127: paint.setPen(QColor(150, 150, 255)); Chris@183: Chris@183: if (translucent && shouldLabelSelections()) { Chris@183: paint.drawRect(p0, -1, p1 - p0, height() + 1); 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@183: paint.drawRect(p0, 0, p1 - p0, height() - 1); Chris@183: } Chris@127: Chris@127: if (illuminateThis) { Chris@127: paint.save(); Chris@127: if (hasLightBackground()) { Chris@127: paint.setPen(QPen(Qt::black, 2)); Chris@127: } else { Chris@127: paint.setPen(QPen(Qt::white, 2)); Chris@127: } Chris@127: if (closeToLeft) { Chris@127: paint.drawLine(p0, 1, p1, 1); Chris@127: paint.drawLine(p0, 0, p0, height()); Chris@127: paint.drawLine(p0, height() - 1, p1, height() - 1); Chris@127: } else if (closeToRight) { Chris@127: paint.drawLine(p0, 1, p1, 1); Chris@127: paint.drawLine(p1, 0, p1, height()); Chris@127: paint.drawLine(p0, height() - 1, p1, height() - 1); Chris@127: } else { Chris@127: paint.setBrush(Qt::NoBrush); Chris@127: paint.drawRect(p0, 1, p1 - p0, height() - 2); Chris@127: } Chris@127: paint.restore(); Chris@127: } Chris@127: Chris@127: if (sampleRate && shouldLabelSelections() && m_manager && Chris@189: m_manager->shouldShowSelectionExtents()) { Chris@127: Chris@127: QString startText = QString("%1 / %2") Chris@127: .arg(QString::fromStdString Chris@127: (RealTime::frame2RealTime Chris@127: (i->getStartFrame(), sampleRate).toText(true))) Chris@127: .arg(i->getStartFrame()); Chris@127: Chris@127: QString endText = QString(" %1 / %2") Chris@127: .arg(QString::fromStdString Chris@127: (RealTime::frame2RealTime Chris@127: (i->getEndFrame(), sampleRate).toText(true))) Chris@127: .arg(i->getEndFrame()); Chris@127: Chris@127: QString durationText = QString("(%1 / %2) ") Chris@127: .arg(QString::fromStdString Chris@127: (RealTime::frame2RealTime Chris@127: (i->getEndFrame() - i->getStartFrame(), sampleRate) Chris@127: .toText(true))) Chris@127: .arg(i->getEndFrame() - i->getStartFrame()); Chris@127: Chris@127: int sw = metrics.width(startText), Chris@127: ew = metrics.width(endText), Chris@127: dw = metrics.width(durationText); Chris@127: Chris@127: int sy = metrics.ascent() + metrics.height() + 4; Chris@127: int ey = sy; Chris@127: int dy = sy + metrics.height(); Chris@127: Chris@127: int sx = p0 + 2; Chris@127: int ex = sx; Chris@127: int dx = sx; Chris@127: Chris@127: if (sw + ew > (p1 - p0)) { Chris@127: ey += metrics.height(); Chris@127: dy += metrics.height(); Chris@127: } Chris@127: Chris@127: if (ew < (p1 - p0)) { Chris@127: ex = p1 - 2 - ew; Chris@127: } Chris@127: Chris@127: if (dw < (p1 - p0)) { Chris@127: dx = p1 - 2 - dw; Chris@127: } Chris@127: Chris@127: paint.drawText(sx, sy, startText); Chris@127: paint.drawText(ex, ey, endText); Chris@127: paint.drawText(dx, dy, durationText); Chris@127: } Chris@127: } Chris@127: Chris@127: paint.restore(); Chris@127: } Chris@127: Chris@127: QString Chris@127: View::toXmlString(QString indent, QString extraAttributes) const Chris@127: { Chris@127: QString s; Chris@127: Chris@127: s += indent; Chris@127: Chris@127: s += QString("\n") Chris@127: .arg(m_centreFrame) Chris@127: .arg(m_zoomLevel) Chris@127: .arg(m_followPan) Chris@127: .arg(m_followZoom) Chris@127: .arg(m_followPlay == PlaybackScrollContinuous ? "scroll" : Chris@127: m_followPlay == PlaybackScrollPage ? "page" : "ignore") Chris@127: .arg(m_lightBackground) Chris@127: .arg(extraAttributes); Chris@127: Chris@127: for (size_t i = 0; i < m_layers.size(); ++i) { Chris@186: bool visible = !m_layers[i]->isLayerDormant(this); Chris@186: s += m_layers[i]->toXmlString(indent + " ", Chris@186: QString("visible=\"%1\"") Chris@186: .arg(visible ? "true" : "false")); Chris@127: } Chris@127: Chris@127: s += indent + "\n"; Chris@127: Chris@127: return s; Chris@127: } Chris@127: Chris@127: ViewPropertyContainer::ViewPropertyContainer(View *v) : Chris@127: m_v(v) Chris@127: { Chris@127: connect(m_v, SIGNAL(propertyChanged(PropertyContainer::PropertyName)), Chris@127: this, SIGNAL(propertyChanged(PropertyContainer::PropertyName))); Chris@127: } Chris@127: Chris@127: #ifdef INCLUDE_MOCFILES Chris@127: #include "View.moc.cpp" Chris@127: #endif Chris@127: