Chris@49: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@52: Sonic Visualiser Chris@52: An audio file viewer and annotation editor. Chris@52: Centre for Digital Music, Queen Mary, University of London. Chris@52: This file copyright 2006 Chris Cannam. Chris@0: Chris@52: This program is free software; you can redistribute it and/or Chris@52: modify it under the terms of the GNU General Public License as Chris@52: published by the Free Software Foundation; either version 2 of the Chris@52: License, or (at your option) any later version. See the file Chris@52: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "base/View.h" Chris@0: #include "base/Layer.h" Chris@0: #include "base/Model.h" Chris@0: #include "base/ZoomConstraint.h" Chris@0: #include "base/Profiler.h" Chris@0: Chris@0: #include "layer/TimeRulerLayer.h" //!!! damn, shouldn't be including that here Chris@25: #include "model/PowerOfSqrtTwoZoomConstraint.h" //!!! likewise Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@16: #include Martin@37: #include Chris@0: Chris@134: #define DEBUG_VIEW_WIDGET_PAINT 1 Chris@0: Chris@0: using std::cerr; Chris@0: using std::endl; Chris@0: Chris@0: View::View(QWidget *w, bool showProgress) : Chris@0: QFrame(w), Chris@0: m_centreFrame(0), Chris@0: m_zoomLevel(1024), Chris@0: m_followPan(true), Chris@0: m_followZoom(true), Chris@0: m_followPlay(PlaybackScrollPage), Chris@0: m_lightBackground(true), Chris@0: m_showProgress(showProgress), Chris@0: m_cache(0), Chris@0: m_cacheCentreFrame(0), Chris@0: m_cacheZoomLevel(1024), Chris@9: m_selectionCached(false), Chris@0: m_deleting(false), Chris@8: m_haveSelectedLayer(false), Chris@29: m_manager(0), Chris@29: m_propertyContainer(new ViewPropertyContainer(this)) Chris@0: { Chris@0: // QWidget::setAttribute(Qt::WA_PaintOnScreen); Chris@0: } Chris@0: Chris@0: View::~View() Chris@0: { Chris@117: // std::cerr << "View::~View(" << this << ")" << std::endl; Chris@43: Chris@0: m_deleting = true; Chris@29: delete m_propertyContainer; Chris@0: } Chris@0: Chris@0: PropertyContainer::PropertyList Chris@0: View::getProperties() const Chris@0: { Chris@29: PropertyContainer::PropertyList list; Chris@94: list.push_back("Global Scroll"); Chris@94: list.push_back("Global Zoom"); Chris@94: list.push_back("Follow Playback"); Chris@0: return list; Chris@0: } Chris@0: Chris@94: QString Chris@94: View::getPropertyLabel(const PropertyName &pn) const Chris@94: { Chris@94: if (pn == "Global Scroll") return tr("Global Scroll"); Chris@94: if (pn == "Global Zoom") return tr("Global Zoom"); Chris@94: if (pn == "Follow Playback") return tr("Follow Playback"); Chris@94: return ""; Chris@94: } Chris@94: Chris@0: PropertyContainer::PropertyType Chris@29: View::getPropertyType(const PropertyContainer::PropertyName &name) const Chris@0: { Chris@94: if (name == "Global Scroll") return PropertyContainer::ToggleProperty; Chris@94: if (name == "Global Zoom") return PropertyContainer::ToggleProperty; Chris@94: if (name == "Follow Playback") return PropertyContainer::ValueProperty; Chris@29: return PropertyContainer::InvalidProperty; Chris@0: } Chris@0: Chris@0: int Chris@29: View::getPropertyRangeAndValue(const PropertyContainer::PropertyName &name, Chris@29: int *min, int *max) const Chris@0: { Chris@94: if (name == "Global Scroll") return m_followPan; Chris@94: if (name == "Global Zoom") return m_followZoom; Chris@94: if (name == "Follow Playback") { Chris@29: if (min) *min = 0; Chris@29: if (max) *max = 2; Chris@29: return int(m_followPlay); Chris@29: } Chris@29: if (min) *min = 0; Chris@29: if (max) *max = 0; Chris@29: return 0; Chris@0: } Chris@0: Chris@0: QString Chris@29: View::getPropertyValueLabel(const PropertyContainer::PropertyName &name, Chris@29: int value) const Chris@0: { Chris@94: if (name == "Follow Playback") { Chris@0: switch (value) { Chris@0: default: Chris@0: case 0: return tr("Scroll"); Chris@0: case 1: return tr("Page"); Chris@0: case 2: return tr("Off"); Chris@0: } Chris@0: } Chris@0: return tr(""); Chris@0: } Chris@0: Chris@0: void Chris@29: View::setProperty(const PropertyContainer::PropertyName &name, int value) Chris@0: { Chris@94: if (name == "Global Scroll") { Chris@0: setFollowGlobalPan(value != 0); Chris@94: } else if (name == "Global Zoom") { Chris@0: setFollowGlobalZoom(value != 0); Chris@94: } else if (name == "Follow Playback") { Chris@0: switch (value) { Chris@0: default: Chris@0: case 0: setPlaybackFollow(PlaybackScrollContinuous); break; Chris@0: case 1: setPlaybackFollow(PlaybackScrollPage); break; Chris@0: case 2: setPlaybackFollow(PlaybackIgnore); break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: size_t Chris@0: View::getPropertyContainerCount() const Chris@0: { Chris@0: return m_layers.size() + 1; // the 1 is for me Chris@0: } Chris@0: Chris@0: const PropertyContainer * Chris@0: View::getPropertyContainer(size_t i) const Chris@0: { Chris@0: return (const PropertyContainer *)(((View *)this)-> Chris@0: getPropertyContainer(i)); Chris@0: } Chris@0: Chris@0: PropertyContainer * Chris@0: View::getPropertyContainer(size_t i) Chris@0: { Chris@29: if (i == 0) return m_propertyContainer; Chris@0: return m_layers[i-1]; Chris@0: } Chris@0: Chris@78: bool Chris@117: View::getValueExtents(QString unit, float &min, float &max, bool &log) const Chris@78: { Chris@78: bool have = false; Chris@78: Chris@78: for (LayerList::const_iterator i = m_layers.begin(); Chris@78: i != m_layers.end(); ++i) { Chris@78: Chris@78: QString layerUnit; Chris@117: float layerMin = 0.0, layerMax = 0.0; Chris@117: float displayMin = 0.0, displayMax = 0.0; Chris@117: bool layerLog = false; Chris@78: Chris@117: if ((*i)->getValueExtents(layerMin, layerMax, layerLog, layerUnit) && Chris@78: layerUnit.toLower() == unit.toLower()) { Chris@78: Chris@117: if ((*i)->getDisplayExtents(displayMin, displayMax)) { Chris@117: Chris@117: min = displayMin; Chris@117: max = displayMax; Chris@117: log = layerLog; Chris@117: have = true; Chris@117: break; Chris@117: Chris@117: } else { Chris@117: Chris@117: if (!have || layerMin < min) min = layerMin; Chris@117: if (!have || layerMax > max) max = layerMax; Chris@117: if (layerLog) log = true; Chris@117: have = true; Chris@117: } Chris@78: } Chris@78: } Chris@78: Chris@78: return have; Chris@78: } Chris@78: Chris@78: int Chris@78: View::getTextLabelHeight(const Layer *layer, QPainter &paint) const Chris@78: { Chris@85: std::map sortedLayers; Chris@78: Chris@78: for (LayerList::const_iterator i = m_layers.begin(); Chris@78: i != m_layers.end(); ++i) { Chris@85: if ((*i)->needsTextLabelHeight()) { Chris@85: sortedLayers[getObjectExportId(*i)] = *i; Chris@85: } Chris@85: } Chris@78: Chris@85: int y = 15 + paint.fontMetrics().ascent(); Chris@78: Chris@85: for (std::map::const_iterator i = sortedLayers.begin(); Chris@85: i != sortedLayers.end(); ++i) { Chris@85: if (i->second == layer) return y; Chris@85: y += paint.fontMetrics().height(); Chris@78: } Chris@78: Chris@78: return y; Chris@78: } Chris@78: Chris@0: void Chris@44: View::propertyContainerSelected(View *client, PropertyContainer *pc) Chris@0: { Chris@44: if (client != this) return; Chris@44: Chris@29: if (pc == m_propertyContainer) { Chris@8: if (m_haveSelectedLayer) { Chris@8: m_haveSelectedLayer = false; Chris@8: update(); Chris@8: } Chris@8: return; Chris@8: } Chris@0: Chris@0: delete m_cache; Chris@0: m_cache = 0; Chris@0: Chris@0: Layer *selectedLayer = 0; Chris@0: Chris@0: for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@0: if (*i == pc) { Chris@0: selectedLayer = *i; Chris@0: m_layers.erase(i); Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if (selectedLayer) { Chris@8: m_haveSelectedLayer = true; Chris@0: m_layers.push_back(selectedLayer); Chris@0: update(); Chris@8: } else { Chris@8: m_haveSelectedLayer = false; Chris@0: } Chris@0: } Chris@0: Chris@8: void Chris@8: View::toolModeChanged() Chris@8: { Chris@117: // std::cerr << "View::toolModeChanged(" << m_manager->getToolMode() << ")" << std::endl; Chris@8: } Chris@8: Chris@0: long Chris@0: View::getStartFrame() const Chris@0: { Chris@0: size_t w2 = (width() / 2) * m_zoomLevel; Chris@0: size_t frame = m_centreFrame; Chris@0: if (frame >= w2) { Chris@0: frame -= w2; Chris@0: return (frame / m_zoomLevel * m_zoomLevel); Chris@0: } else { Chris@0: frame = w2 - frame; Chris@0: frame = frame / m_zoomLevel * m_zoomLevel; Chris@0: return -(long)frame - m_zoomLevel; Chris@0: } Chris@0: } Chris@0: Chris@0: size_t Chris@0: View::getEndFrame() const Chris@0: { Chris@16: return getFrameForX(width()) - 1; Chris@0: } Chris@0: Chris@0: void Chris@0: View::setStartFrame(long f) Chris@0: { Chris@0: setCentreFrame(f + m_zoomLevel * (width() / 2)); Chris@0: } Chris@0: Chris@10: bool Chris@0: View::setCentreFrame(size_t f, bool e) Chris@0: { Chris@10: bool changeVisible = false; Chris@10: Chris@0: if (m_centreFrame != f) { Chris@0: Chris@0: int formerPixel = m_centreFrame / m_zoomLevel; Chris@0: Chris@0: m_centreFrame = f; Chris@0: Chris@0: int newPixel = m_centreFrame / m_zoomLevel; Chris@0: Chris@0: if (newPixel != formerPixel) { Chris@0: Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@0: std::cout << "View(" << this << ")::setCentreFrame: newPixel " << newPixel << ", formerPixel " << formerPixel << std::endl; Chris@0: #endif Chris@0: update(); Chris@10: Chris@10: changeVisible = true; Chris@0: } Chris@0: Chris@0: if (e) emit centreFrameChanged(this, f, m_followPan); Chris@0: } Chris@10: Chris@10: return changeVisible; Chris@0: } Chris@0: Chris@15: int Chris@15: View::getXForFrame(long frame) const Chris@15: { Chris@15: return (frame - getStartFrame()) / m_zoomLevel; Chris@15: } Chris@15: Chris@15: long Chris@15: View::getFrameForX(int x) const Chris@15: { Chris@15: return (long(x) * long(m_zoomLevel)) + getStartFrame(); Chris@15: } Chris@15: Chris@33: float Chris@33: View::getYForFrequency(float frequency, Chris@33: float minf, Chris@33: float maxf, Chris@33: bool logarithmic) const Chris@33: { Chris@33: int h = height(); Chris@33: Chris@33: if (logarithmic) { Chris@33: Chris@33: static float lastminf = 0.0, lastmaxf = 0.0; Chris@33: static float logminf = 0.0, logmaxf = 0.0; Chris@33: Chris@33: if (lastminf != minf) { Chris@33: lastminf = (minf == 0.0 ? 1.0 : minf); Chris@33: logminf = log10f(minf); Chris@33: } Chris@33: if (lastmaxf != maxf) { Chris@33: lastmaxf = (maxf < lastminf ? lastminf : maxf); Chris@33: logmaxf = log10f(maxf); Chris@33: } Chris@33: Chris@33: if (logminf == logmaxf) return 0; Chris@33: return h - (h * (log10f(frequency) - logminf)) / (logmaxf - logminf); Chris@33: Chris@33: } else { Chris@33: Chris@33: if (minf == maxf) return 0; Chris@33: return h - (h * (frequency - minf)) / (maxf - minf); Chris@33: } Chris@33: } Chris@33: Chris@33: float Chris@33: View::getFrequencyForY(int y, Chris@33: float minf, Chris@33: float maxf, Chris@33: bool logarithmic) const Chris@33: { Chris@33: int h = height(); Chris@33: Chris@33: if (logarithmic) { Chris@33: Chris@33: static float lastminf = 0.0, lastmaxf = 0.0; Chris@33: static float logminf = 0.0, logmaxf = 0.0; Chris@33: Chris@33: if (lastminf != minf) { Chris@33: lastminf = (minf == 0.0 ? 1.0 : minf); Chris@33: logminf = log10f(minf); Chris@33: } Chris@33: if (lastmaxf != maxf) { Chris@33: lastmaxf = (maxf < lastminf ? lastminf : maxf); Chris@33: logmaxf = log10f(maxf); Chris@33: } Chris@33: Chris@33: if (logminf == logmaxf) return 0; Chris@33: return pow(10.f, logminf + ((logmaxf - logminf) * (h - y)) / h); Chris@33: Chris@33: } else { Chris@33: Chris@33: if (minf == maxf) return 0; Chris@33: return minf + ((h - y) * (maxf - minf)) / h; Chris@33: } Chris@33: } Chris@33: Chris@22: int Chris@22: View::getZoomLevel() const Chris@22: { Martin@54: #ifdef DEBUG_VIEW_WIDGET_PAINT Martin@54: std::cout << "zoom level: " << m_zoomLevel << std::endl; Martin@54: #endif Chris@22: return m_zoomLevel; Chris@22: } Chris@22: Chris@0: void Chris@0: View::setZoomLevel(size_t z) Chris@0: { Chris@0: if (m_zoomLevel != int(z)) { Chris@0: m_zoomLevel = z; Chris@0: emit zoomLevelChanged(this, z, m_followZoom); Chris@0: update(); Chris@0: } Chris@0: } Chris@0: Chris@16: View::LayerProgressBar::LayerProgressBar(QWidget *parent) : Chris@16: QProgressBar(parent) Chris@16: { Chris@16: QFont f(font()); Chris@16: f.setPointSize(f.pointSize() * 8 / 10); Chris@16: setFont(f); Chris@16: } Chris@16: Chris@0: void Chris@0: View::addLayer(Layer *layer) Chris@0: { Chris@0: delete m_cache; Chris@0: m_cache = 0; Chris@0: Chris@0: m_layers.push_back(layer); Chris@0: Chris@0: m_progressBars[layer] = new LayerProgressBar(this); Chris@0: m_progressBars[layer]->setMinimum(0); Chris@0: m_progressBars[layer]->setMaximum(100); Chris@0: m_progressBars[layer]->setMinimumWidth(80); Chris@0: m_progressBars[layer]->hide(); Chris@16: Chris@0: connect(layer, SIGNAL(layerParametersChanged()), Chris@0: this, SLOT(layerParametersChanged())); Chris@0: connect(layer, SIGNAL(layerNameChanged()), Chris@0: this, SLOT(layerNameChanged())); Chris@0: connect(layer, SIGNAL(modelChanged()), Chris@0: this, SLOT(modelChanged())); Chris@0: connect(layer, SIGNAL(modelCompletionChanged()), Chris@0: this, SLOT(modelCompletionChanged())); Chris@0: connect(layer, SIGNAL(modelChanged(size_t, size_t)), Chris@0: this, SLOT(modelChanged(size_t, size_t))); Chris@0: connect(layer, SIGNAL(modelReplaced()), Chris@0: this, SLOT(modelReplaced())); Chris@0: Chris@0: update(); Chris@0: Chris@0: emit propertyContainerAdded(layer); Chris@0: } Chris@0: Chris@0: void Chris@0: View::removeLayer(Layer *layer) Chris@0: { Chris@0: if (m_deleting) { Chris@0: return; Chris@0: } Chris@0: Chris@0: delete m_cache; Chris@0: m_cache = 0; Chris@0: Chris@0: for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@0: if (*i == layer) { Chris@0: m_layers.erase(i); Chris@0: if (m_progressBars.find(layer) != m_progressBars.end()) { Chris@0: delete m_progressBars[layer]; Chris@0: m_progressBars.erase(layer); Chris@0: } Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: update(); Chris@0: Chris@0: emit propertyContainerRemoved(layer); Chris@0: } Chris@0: Chris@8: Layer * Chris@8: View::getSelectedLayer() Chris@8: { Chris@8: if (m_haveSelectedLayer && !m_layers.empty()) { Chris@8: return getLayer(getLayerCount() - 1); Chris@8: } else { Chris@8: return 0; Chris@8: } Chris@8: } Chris@8: Chris@36: const Layer * Chris@36: View::getSelectedLayer() const Chris@36: { Chris@36: return const_cast(const_cast(this)->getSelectedLayer()); Chris@36: } Chris@36: Chris@0: void Chris@0: View::setViewManager(ViewManager *manager) Chris@0: { Chris@0: if (m_manager) { Chris@0: m_manager->disconnect(this, SLOT(viewManagerCentreFrameChanged(void *, unsigned long, bool))); Chris@0: m_manager->disconnect(this, SLOT(viewManagerZoomLevelChanged(void *, unsigned long, bool))); Chris@0: disconnect(m_manager, SIGNAL(centreFrameChanged(void *, unsigned long, bool))); Chris@0: disconnect(m_manager, SIGNAL(zoomLevelChanged(void *, unsigned long, bool))); Chris@8: disconnect(m_manager, SIGNAL(toolModeChanged())); Chris@8: disconnect(m_manager, SIGNAL(selectionChanged())); Chris@9: disconnect(m_manager, SIGNAL(inProgressSelectionChanged())); Chris@0: } Chris@0: Chris@0: m_manager = manager; Chris@0: if (m_followPan) setCentreFrame(m_manager->getGlobalCentreFrame(), false); Chris@0: if (m_followZoom) setZoomLevel(m_manager->getGlobalZoom()); Chris@0: Chris@0: connect(m_manager, SIGNAL(centreFrameChanged(void *, unsigned long, bool)), Chris@0: this, SLOT(viewManagerCentreFrameChanged(void *, unsigned long, bool))); Chris@0: connect(m_manager, SIGNAL(playbackFrameChanged(unsigned long)), Chris@0: this, SLOT(viewManagerPlaybackFrameChanged(unsigned long))); Chris@0: connect(m_manager, SIGNAL(zoomLevelChanged(void *, unsigned long, bool)), Chris@0: this, SLOT(viewManagerZoomLevelChanged(void *, unsigned long, bool))); Chris@8: connect(m_manager, SIGNAL(toolModeChanged()), Chris@8: this, SLOT(toolModeChanged())); Chris@8: connect(m_manager, SIGNAL(selectionChanged()), Chris@9: this, SLOT(selectionChanged())); Chris@9: connect(m_manager, SIGNAL(inProgressSelectionChanged()), Chris@9: this, SLOT(selectionChanged())); Chris@65: connect(m_manager, SIGNAL(overlayModeChanged()), Chris@65: this, SLOT(update())); Chris@0: Chris@0: connect(this, SIGNAL(centreFrameChanged(void *, unsigned long, bool)), Chris@0: m_manager, SIGNAL(centreFrameChanged(void *, unsigned long, bool))); Chris@0: connect(this, SIGNAL(zoomLevelChanged(void *, unsigned long, bool)), Chris@0: m_manager, SIGNAL(zoomLevelChanged(void *, unsigned long, bool))); Chris@8: Chris@8: toolModeChanged(); Chris@0: } Chris@0: Chris@0: void Chris@0: View::setFollowGlobalPan(bool f) Chris@0: { Chris@0: m_followPan = f; Chris@29: emit propertyContainerPropertyChanged(m_propertyContainer); Chris@0: } Chris@0: Chris@0: void Chris@0: View::setFollowGlobalZoom(bool f) Chris@0: { Chris@0: m_followZoom = f; Chris@29: emit propertyContainerPropertyChanged(m_propertyContainer); Chris@0: } Chris@0: Chris@0: void Chris@46: View::drawVisibleText(QPainter &paint, int x, int y, QString text, TextStyle style) Chris@45: { Chris@46: if (style == OutlinedText) { Chris@46: Chris@46: QColor origPenColour = paint.pen().color(); Chris@46: QColor penColour = origPenColour; Chris@46: QColor surroundColour = Qt::white; //palette().background().color(); Chris@46: Chris@46: if (!hasLightBackground()) { Chris@46: int h, s, v; Chris@46: penColour.getHsv(&h, &s, &v); Chris@46: penColour = QColor::fromHsv(h, s, 255 - v); Chris@46: surroundColour = Qt::black; Chris@46: } Chris@46: Chris@46: paint.setPen(surroundColour); Chris@46: Chris@46: for (int dx = -1; dx <= 1; ++dx) { Chris@46: for (int dy = -1; dy <= 1; ++dy) { Chris@46: if (!(dx || dy)) continue; Chris@46: paint.drawText(x + dx, y + dy, text); Chris@46: } Chris@46: } Chris@46: Chris@46: paint.setPen(penColour); Chris@46: Chris@46: paint.drawText(x, y, text); Chris@46: Chris@46: paint.setPen(origPenColour); Chris@46: Chris@46: } else { Chris@46: Chris@46: std::cerr << "ERROR: View::drawVisibleText: Boxed style not yet implemented!" << std::endl; Chris@46: } Chris@45: } Chris@45: Chris@45: void Chris@0: View::setPlaybackFollow(PlaybackFollowMode m) Chris@0: { Chris@0: m_followPlay = m; Chris@29: emit propertyContainerPropertyChanged(m_propertyContainer); Chris@0: } Chris@0: Chris@0: void Chris@0: View::modelChanged() Chris@0: { Chris@0: QObject *obj = sender(); Chris@0: Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1: std::cerr << "View(" << this << ")::modelChanged()" << std::endl; Chris@0: #endif Chris@31: Chris@31: // If the model that has changed is not used by any of the cached Chris@31: // layers, we won't need to recreate the cache Chris@31: Chris@31: bool recreate = false; Chris@31: Chris@31: bool discard; Chris@31: LayerList scrollables = getScrollableBackLayers(false, discard); Chris@31: for (LayerList::const_iterator i = scrollables.begin(); Chris@31: i != scrollables.end(); ++i) { Chris@31: if (*i == obj || (*i)->getModel() == obj) { Chris@31: recreate = true; Chris@31: break; Chris@31: } Chris@31: } Chris@31: Chris@31: if (recreate) { Chris@31: delete m_cache; Chris@31: m_cache = 0; Chris@31: } Chris@0: Chris@0: checkProgress(obj); Chris@0: Chris@0: update(); Chris@0: } Chris@0: Chris@0: void Chris@0: View::modelChanged(size_t startFrame, size_t endFrame) Chris@0: { Chris@0: QObject *obj = sender(); Chris@0: Chris@0: long myStartFrame = getStartFrame(); Chris@0: size_t myEndFrame = getEndFrame(); Chris@0: Chris@1: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1: std::cerr << "View(" << this << ")::modelChanged(" << startFrame << "," << endFrame << ") [me " << myStartFrame << "," << myEndFrame << "]" << std::endl; Chris@1: #endif Chris@1: Chris@0: if (myStartFrame > 0 && endFrame < size_t(myStartFrame)) { Chris@0: checkProgress(obj); Chris@0: return; Chris@0: } Chris@0: if (startFrame > myEndFrame) { Chris@0: checkProgress(obj); Chris@0: return; Chris@0: } Chris@0: Chris@31: // If the model that has changed is not used by any of the cached Chris@31: // layers, we won't need to recreate the cache Chris@31: Chris@31: bool recreate = false; Chris@31: Chris@31: bool discard; Chris@31: LayerList scrollables = getScrollableBackLayers(false, discard); Chris@31: for (LayerList::const_iterator i = scrollables.begin(); Chris@31: i != scrollables.end(); ++i) { Chris@31: if (*i == obj || (*i)->getModel() == obj) { Chris@31: recreate = true; Chris@31: break; Chris@31: } Chris@31: } Chris@31: Chris@31: if (recreate) { Chris@31: delete m_cache; Chris@31: m_cache = 0; Chris@31: } Chris@0: Chris@0: if (long(startFrame) < myStartFrame) startFrame = myStartFrame; Chris@0: if (endFrame > myEndFrame) endFrame = myEndFrame; Chris@0: Chris@15: int x0 = getXForFrame(startFrame); Chris@15: int x1 = getXForFrame(endFrame + 1); Chris@15: if (x1 < x0) x1 = x0; Chris@0: Chris@0: checkProgress(obj); Chris@0: Chris@31: update(); Chris@31: //!! update(x0, 0, x1 - x0 + 1, height()); Chris@0: } Chris@0: Chris@0: void Chris@0: View::modelCompletionChanged() Chris@0: { Chris@0: QObject *obj = sender(); Chris@0: checkProgress(obj); Chris@0: } Chris@0: Chris@0: void Chris@0: View::modelReplaced() Chris@0: { Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1: std::cerr << "View(" << this << ")::modelReplaced()" << std::endl; Chris@0: #endif Chris@1: delete m_cache; Chris@1: m_cache = 0; Chris@1: Chris@0: update(); Chris@0: } Chris@0: Chris@0: void Chris@0: View::layerParametersChanged() Chris@0: { Chris@0: Layer *layer = dynamic_cast(sender()); Chris@0: Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@0: std::cerr << "View::layerParametersChanged()" << std::endl; Chris@0: #endif Chris@0: Chris@0: delete m_cache; Chris@0: m_cache = 0; Chris@0: update(); Chris@0: Chris@0: if (layer) { Chris@0: emit propertyContainerPropertyChanged(layer); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: View::layerNameChanged() Chris@0: { Chris@0: Layer *layer = dynamic_cast(sender()); Chris@0: if (layer) emit propertyContainerNameChanged(layer); Chris@0: } Chris@0: Chris@0: void Chris@0: View::viewManagerCentreFrameChanged(void *p, unsigned long f, bool locked) Chris@0: { Chris@0: if (m_followPan && p != this && locked) { Chris@0: if (m_manager && (sender() == m_manager)) { Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@0: std::cerr << this << ": manager frame changed " << f << " from " << p << std::endl; Chris@0: #endif Chris@0: setCentreFrame(f); Chris@0: if (p == this) repaint(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: View::viewManagerPlaybackFrameChanged(unsigned long f) Chris@0: { Chris@0: if (m_manager) { Chris@0: if (sender() != m_manager) return; Chris@0: } Chris@0: Chris@0: if (m_playPointerFrame == f) return; Chris@15: bool visible = (getXForFrame(m_playPointerFrame) != getXForFrame(f)); Chris@0: size_t oldPlayPointerFrame = m_playPointerFrame; Chris@0: m_playPointerFrame = f; Chris@0: if (!visible) return; Chris@0: Chris@0: switch (m_followPlay) { Chris@0: Chris@0: case PlaybackScrollContinuous: Chris@107: if (QApplication::mouseButtons() == Qt::NoButton) { Chris@0: setCentreFrame(f, false); Chris@0: } Chris@0: break; Chris@0: Chris@0: case PlaybackScrollPage: Chris@0: { Chris@15: int xold = getXForFrame(oldPlayPointerFrame); Chris@107: update(xold - 1, 0, 3, height()); Chris@10: Chris@15: long w = getEndFrame() - getStartFrame(); Chris@0: w -= w/5; Chris@0: long sf = (f / w) * w - w/8; Chris@10: Chris@10: if (m_manager && Chris@10: m_manager->isPlaying() && Chris@10: m_manager->getPlaySelectionMode()) { Chris@24: MultiSelection::SelectionList selections = m_manager->getSelections(); Chris@10: if (!selections.empty()) { Chris@10: size_t selectionStart = selections.begin()->getStartFrame(); Chris@10: if (sf < long(selectionStart) - w / 10) { Chris@10: sf = long(selectionStart) - w / 10; Chris@10: } Chris@10: } Chris@10: } Chris@10: Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@0: std::cerr << "PlaybackScrollPage: f = " << f << ", sf = " << sf << ", start frame " Chris@0: << getStartFrame() << std::endl; Chris@0: #endif Chris@10: Chris@15: // We don't consider scrolling unless the pointer is outside Chris@15: // the clearly visible range already Chris@15: Chris@15: int xnew = getXForFrame(m_playPointerFrame); Chris@15: Chris@18: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@18: std::cerr << "xnew = " << xnew << ", width = " << width() << std::endl; Chris@18: #endif Chris@18: Chris@15: if (xnew < width()/8 || xnew > (width()*7)/8) { Chris@107: if (QApplication::mouseButtons() == Qt::NoButton) { Chris@15: long offset = getFrameForX(width()/2) - getStartFrame(); Chris@15: long newCentre = sf + offset; Chris@15: bool changed = setCentreFrame(newCentre, false); Chris@15: if (changed) { Chris@15: xold = getXForFrame(oldPlayPointerFrame); Chris@15: update(xold - 1, 0, 3, height()); Chris@15: } Chris@10: } Chris@10: } Chris@10: Chris@0: update(xnew - 1, 0, 3, height()); Chris@10: Chris@0: break; Chris@0: } Chris@0: Chris@0: case PlaybackIgnore: Chris@0: if (long(f) >= getStartFrame() && f < getEndFrame()) { Chris@0: update(); Chris@0: } Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: View::viewManagerZoomLevelChanged(void *p, unsigned long z, bool locked) Chris@0: { Chris@0: if (m_followZoom && p != this && locked) { Chris@0: if (m_manager && (sender() == m_manager)) { Chris@0: setZoomLevel(z); Chris@0: if (p == this) repaint(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@9: void Chris@9: View::selectionChanged() Chris@9: { Chris@9: if (m_selectionCached) { Chris@9: delete m_cache; Chris@9: m_cache = 0; Chris@9: m_selectionCached = false; Chris@9: } Chris@9: update(); Chris@9: } Chris@9: Chris@0: size_t Chris@0: View::getModelsStartFrame() const Chris@0: { Chris@0: bool first = true; Chris@0: size_t startFrame = 0; Chris@0: Chris@0: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@0: Chris@0: if ((*i)->getModel() && (*i)->getModel()->isOK()) { Chris@0: Chris@0: size_t thisStartFrame = (*i)->getModel()->getStartFrame(); Chris@0: Chris@0: if (first || thisStartFrame < startFrame) { Chris@0: startFrame = thisStartFrame; Chris@0: } Chris@0: first = false; Chris@0: } Chris@0: } Chris@0: return startFrame; Chris@0: } Chris@0: Chris@0: size_t Chris@0: View::getModelsEndFrame() const Chris@0: { Chris@0: bool first = true; Chris@0: size_t endFrame = 0; Chris@0: Chris@0: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@0: Chris@0: if ((*i)->getModel() && (*i)->getModel()->isOK()) { Chris@0: Chris@0: size_t thisEndFrame = (*i)->getModel()->getEndFrame(); Chris@0: Chris@0: if (first || thisEndFrame > endFrame) { Chris@0: endFrame = thisEndFrame; Chris@0: } Chris@0: first = false; Chris@0: } Chris@0: } Chris@0: Chris@0: if (first) return getModelsStartFrame(); Chris@0: return endFrame; Chris@0: } Chris@0: Chris@0: int Chris@0: View::getModelsSampleRate() const Chris@0: { Chris@0: //!!! Just go for the first, for now. If we were supporting Chris@0: // multiple samplerates, we'd probably want to do frame/time Chris@0: // conversion in the model Chris@0: Chris@0: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@0: if ((*i)->getModel() && (*i)->getModel()->isOK()) { Chris@0: return (*i)->getModel()->getSampleRate(); Chris@0: } Chris@0: } Chris@0: return 0; Chris@0: } Chris@0: Chris@0: bool Chris@0: View::areLayersScrollable() const Chris@0: { Chris@0: // True iff all views are scrollable Chris@0: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@36: if (!(*i)->isLayerScrollable(this)) return false; Chris@0: } Chris@0: return true; Chris@0: } Chris@0: Chris@0: View::LayerList Chris@31: View::getScrollableBackLayers(bool testChanged, bool &changed) const Chris@0: { Chris@0: changed = false; Chris@0: Chris@46: // We want a list of all the scrollable layers that are behind the Chris@46: // backmost non-scrollable layer. Chris@46: Chris@0: LayerList scrollables; Chris@111: bool metUnscrollable = false; Chris@111: Chris@0: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@36: if ((*i)->isLayerDormant(this)) continue; Chris@46: if ((*i)->isLayerOpaque()) { Chris@46: // You can't see anything behind an opaque layer! Chris@46: scrollables.clear(); Chris@111: if (metUnscrollable) break; Chris@46: } Chris@111: if (!metUnscrollable && (*i)->isLayerScrollable(this)) { Chris@111: scrollables.push_back(*i); Chris@111: } else { Chris@111: metUnscrollable = true; Chris@111: } Chris@0: } Chris@0: Chris@31: if (testChanged && scrollables != m_lastScrollableBackLayers) { Chris@0: m_lastScrollableBackLayers = scrollables; Chris@0: changed = true; Chris@0: } Chris@0: return scrollables; Chris@0: } Chris@0: Chris@0: View::LayerList Chris@31: View::getNonScrollableFrontLayers(bool testChanged, bool &changed) const Chris@0: { Chris@0: changed = false; Chris@0: LayerList nonScrollables; Chris@0: Chris@0: // Everything in front of the first non-scrollable from the back Chris@0: // should also be considered non-scrollable Chris@0: Chris@46: bool started = false; Chris@46: Chris@0: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@36: if ((*i)->isLayerDormant(this)) continue; Chris@46: if (!started && (*i)->isLayerScrollable(this)) { Chris@0: continue; Chris@0: } Chris@46: started = true; Chris@46: if ((*i)->isLayerOpaque()) { Chris@46: // You can't see anything behind an opaque layer! Chris@46: nonScrollables.clear(); Chris@46: } Chris@0: nonScrollables.push_back(*i); Chris@0: } Chris@0: Chris@31: if (testChanged && nonScrollables != m_lastNonScrollableBackLayers) { Chris@0: m_lastNonScrollableBackLayers = nonScrollables; Chris@0: changed = true; Chris@0: } Chris@0: Chris@0: return nonScrollables; Chris@0: } Chris@0: Chris@0: size_t Chris@0: View::getZoomConstraintBlockSize(size_t blockSize, Chris@0: ZoomConstraint::RoundingDirection dir) Chris@0: const Chris@0: { Chris@0: size_t candidate = blockSize; Chris@0: bool haveCandidate = false; Chris@0: Chris@25: PowerOfSqrtTwoZoomConstraint defaultZoomConstraint; Chris@25: Chris@0: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@0: Chris@25: const ZoomConstraint *zoomConstraint = (*i)->getZoomConstraint(); Chris@25: if (!zoomConstraint) zoomConstraint = &defaultZoomConstraint; Chris@0: Chris@25: size_t thisBlockSize = Chris@25: zoomConstraint->getNearestBlockSize(blockSize, dir); Chris@0: Chris@25: // Go for the block size that's furthest from the one Chris@25: // passed in. Most of the time, that's what we want. Chris@25: if (!haveCandidate || Chris@25: (thisBlockSize > blockSize && thisBlockSize > candidate) || Chris@25: (thisBlockSize < blockSize && thisBlockSize < candidate)) { Chris@25: candidate = thisBlockSize; Chris@25: haveCandidate = true; Chris@0: } Chris@0: } Chris@0: Chris@0: return candidate; Chris@0: } Chris@0: Chris@0: void Chris@0: View::zoom(bool in) Chris@0: { Chris@0: int newZoomLevel = m_zoomLevel; Chris@0: Chris@0: if (in) { Chris@0: newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1, Chris@0: ZoomConstraint::RoundDown); Chris@0: } else { Chris@0: newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1, Chris@0: ZoomConstraint::RoundUp); Chris@0: } Chris@0: Chris@0: if (newZoomLevel != m_zoomLevel) { Chris@0: setZoomLevel(newZoomLevel); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@12: View::scroll(bool right, bool lots) Chris@12: { Chris@12: long delta; Chris@12: if (lots) { Chris@15: delta = (getEndFrame() - getStartFrame()) / 2; Chris@12: } else { Chris@15: delta = (getEndFrame() - getStartFrame()) / 20; Chris@12: } Chris@12: if (right) delta = -delta; Chris@12: Chris@12: if (int(m_centreFrame) < delta) { Chris@12: setCentreFrame(0); Chris@12: } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) { Chris@12: setCentreFrame(getModelsEndFrame()); Chris@12: } else { Chris@12: setCentreFrame(m_centreFrame - delta); Chris@12: } Chris@12: } Chris@12: Chris@12: void Chris@0: View::checkProgress(void *object) Chris@0: { Chris@0: if (!m_showProgress) return; Chris@0: Chris@0: int ph = height(); Chris@0: Chris@0: for (ProgressMap::const_iterator i = m_progressBars.begin(); Chris@0: i != m_progressBars.end(); ++i) { Chris@0: Chris@0: if (i->first == object) { Chris@0: Chris@134: int completion = i->first->getCompletion(this); Chris@0: Chris@0: if (completion >= 100) { Chris@0: Chris@0: i->second->hide(); Chris@0: Chris@0: } else { Chris@0: Chris@0: i->second->setText(i->first->getPropertyContainerName()); Chris@0: i->second->setValue(completion); Chris@0: i->second->move(0, ph - i->second->height()); Chris@0: Chris@0: i->second->show(); Chris@0: i->second->update(); Chris@0: Chris@0: ph -= i->second->height(); Chris@0: } Chris@0: } else { Chris@0: if (i->second->isVisible()) { Chris@0: ph -= i->second->height(); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: View::paintEvent(QPaintEvent *e) Chris@0: { Chris@120: // Profiler prof("View::paintEvent", false); Chris@0: // std::cerr << "View::paintEvent" << std::endl; Chris@0: Chris@0: if (m_layers.empty()) { Chris@0: QFrame::paintEvent(e); Chris@0: return; Chris@0: } Chris@0: Chris@0: // ensure our constraints are met Chris@0: m_zoomLevel = getZoomConstraintBlockSize(m_zoomLevel, Chris@0: ZoomConstraint::RoundUp); Chris@0: Chris@0: QPainter paint; Chris@0: bool repaintCache = false; Chris@0: bool paintedCacheRect = false; Chris@0: Chris@0: QRect cacheRect(rect()); Chris@0: Chris@0: if (e) { Chris@0: cacheRect &= e->rect(); Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@0: std::cerr << "paint rect " << cacheRect.width() << "x" << cacheRect.height() Chris@0: << ", my rect " << width() << "x" << height() << std::endl; Chris@0: #endif Chris@0: } Chris@0: Chris@0: QRect nonCacheRect(cacheRect); Chris@0: Chris@0: // If not all layers are scrollable, but some of the back layers Chris@46: // are, we should store only those in the cache. Chris@0: Chris@0: bool layersChanged = false; Chris@31: LayerList scrollables = getScrollableBackLayers(true, layersChanged); Chris@31: LayerList nonScrollables = getNonScrollableFrontLayers(true, layersChanged); Chris@9: bool selectionCacheable = nonScrollables.empty(); Chris@9: bool haveSelections = m_manager && !m_manager->getSelections().empty(); Chris@9: bool selectionDrawn = false; Chris@0: Chris@46: // If all the non-scrollable layers are non-opaque, then we draw Chris@46: // the selection rectangle behind them and cache it. If any are Chris@46: // opaque, however, we can't cache. Chris@46: // Chris@10: if (!selectionCacheable) { Chris@10: selectionCacheable = true; Chris@10: for (LayerList::const_iterator i = nonScrollables.begin(); Chris@10: i != nonScrollables.end(); ++i) { Chris@10: if ((*i)->isLayerOpaque()) { Chris@10: selectionCacheable = false; Chris@10: break; Chris@10: } Chris@10: } Chris@10: } Chris@10: Chris@34: if (selectionCacheable) { Chris@34: QPoint localPos; Chris@34: bool closeToLeft, closeToRight; Chris@34: if (shouldIlluminateLocalSelection(localPos, closeToLeft, closeToRight)) { Chris@34: selectionCacheable = false; Chris@34: } Chris@34: } Chris@34: Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@0: std::cerr << "View(" << this << ")::paintEvent: have " << scrollables.size() Chris@0: << " scrollable back layers and " << nonScrollables.size() Chris@0: << " non-scrollable front layers" << std::endl; Chris@9: std::cerr << "haveSelections " << haveSelections << ", selectionCacheable " Chris@9: << selectionCacheable << ", m_selectionCached " << m_selectionCached << std::endl; Chris@0: #endif Chris@0: Chris@9: if (layersChanged || scrollables.empty() || Chris@9: (haveSelections && (selectionCacheable != m_selectionCached))) { Chris@0: delete m_cache; Chris@0: m_cache = 0; Chris@9: m_selectionCached = false; Chris@0: } Chris@0: Chris@0: if (!scrollables.empty()) { Chris@0: if (!m_cache || Chris@0: m_cacheZoomLevel != m_zoomLevel || Chris@0: width() != m_cache->width() || Chris@0: height() != m_cache->height()) { Chris@0: Chris@0: // cache is not valid Chris@0: Chris@0: if (cacheRect.width() < width()/10) { Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@0: std::cerr << "View(" << this << ")::paintEvent: small repaint, not bothering to recreate cache" << std::endl; Chris@0: #endif Chris@0: } else { Chris@0: delete m_cache; Chris@0: m_cache = new QPixmap(width(), height()); Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@0: std::cerr << "View(" << this << ")::paintEvent: recreated cache" << std::endl; Chris@0: #endif Chris@0: cacheRect = rect(); Chris@0: repaintCache = true; Chris@0: } Chris@0: Chris@0: } else if (m_cacheCentreFrame != m_centreFrame) { Chris@0: Chris@15: long dx = Chris@15: getXForFrame(m_cacheCentreFrame) - Chris@15: getXForFrame(m_centreFrame); Chris@0: Chris@0: if (dx > -width() && dx < width()) { Chris@0: #if defined(Q_WS_WIN32) || defined(Q_WS_MAC) Chris@0: // Copying a pixmap to itself doesn't work properly on Windows Chris@0: // or Mac (it only works when moving in one direction) Chris@0: static QPixmap *tmpPixmap = 0; Chris@0: if (!tmpPixmap || Chris@0: tmpPixmap->width() != width() || Chris@0: tmpPixmap->height() != height()) { Chris@0: delete tmpPixmap; Chris@0: tmpPixmap = new QPixmap(width(), height()); Chris@0: } Chris@0: paint.begin(tmpPixmap); Chris@0: paint.drawPixmap(0, 0, *m_cache); Chris@0: paint.end(); Chris@0: paint.begin(m_cache); Chris@0: paint.drawPixmap(dx, 0, *tmpPixmap); Chris@0: paint.end(); Chris@0: #else Chris@0: // But it seems to be fine on X11 Chris@0: paint.begin(m_cache); Chris@0: paint.drawPixmap(dx, 0, *m_cache); Chris@0: paint.end(); Chris@0: #endif Chris@0: Chris@0: if (dx < 0) { Chris@0: cacheRect = QRect(width() + dx, 0, -dx, height()); Chris@0: } else { Chris@0: cacheRect = QRect(0, 0, dx, height()); Chris@0: } Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@0: std::cerr << "View(" << this << ")::paintEvent: scrolled cache by " << dx << std::endl; Chris@0: #endif Chris@0: } else { Chris@0: cacheRect = rect(); Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@0: std::cerr << "View(" << this << ")::paintEvent: scrolling too far" << std::endl; Chris@0: #endif Chris@0: } Chris@0: repaintCache = true; Chris@0: Chris@0: } else { Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@0: std::cerr << "View(" << this << ")::paintEvent: cache is good" << std::endl; Chris@0: #endif Chris@0: paint.begin(this); Chris@0: paint.drawPixmap(cacheRect, *m_cache, cacheRect); Chris@0: paint.end(); Chris@0: QFrame::paintEvent(e); Chris@0: paintedCacheRect = true; Chris@0: } Chris@0: Chris@0: m_cacheCentreFrame = m_centreFrame; Chris@0: m_cacheZoomLevel = m_zoomLevel; Chris@0: } Chris@0: Chris@0: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@0: // std::cerr << "View(" << this << ")::paintEvent: cacheRect " << cacheRect << ", nonCacheRect " << (nonCacheRect | cacheRect) << ", repaintCache " << repaintCache << ", paintedCacheRect " << paintedCacheRect << std::endl; Chris@0: #endif Chris@0: Chris@0: // Scrollable (cacheable) items first Chris@0: Chris@0: if (!paintedCacheRect) { Chris@0: Chris@0: if (repaintCache) paint.begin(m_cache); Chris@0: else paint.begin(this); Chris@0: Chris@0: paint.setClipRect(cacheRect); Chris@0: Chris@0: if (hasLightBackground()) { Chris@0: paint.setPen(Qt::white); Chris@0: paint.setBrush(Qt::white); Chris@0: } else { Chris@0: paint.setPen(Qt::black); Chris@0: paint.setBrush(Qt::black); Chris@0: } Chris@0: paint.drawRect(cacheRect); Chris@0: Chris@0: paint.setPen(Qt::black); Chris@0: paint.setBrush(Qt::NoBrush); Chris@0: Chris@0: for (LayerList::iterator i = scrollables.begin(); i != scrollables.end(); ++i) { Chris@8: paint.setRenderHint(QPainter::Antialiasing, false); Chris@8: paint.save(); Chris@36: (*i)->paint(this, paint, cacheRect); Chris@8: paint.restore(); Chris@0: } Chris@9: Chris@9: if (haveSelections && selectionCacheable) { Chris@9: drawSelections(paint); Chris@9: m_selectionCached = repaintCache; Chris@9: selectionDrawn = true; Chris@9: } Chris@0: Chris@0: paint.end(); Chris@0: Chris@0: if (repaintCache) { Chris@0: cacheRect |= (e ? e->rect() : rect()); Chris@0: paint.begin(this); Chris@0: paint.drawPixmap(cacheRect, *m_cache, cacheRect); Chris@0: paint.end(); Chris@0: } Chris@0: } Chris@0: Chris@0: // Now non-cacheable items. We always need to redraw the Chris@0: // non-cacheable items across at least the area we drew of the Chris@0: // cacheable items. Chris@0: Chris@0: nonCacheRect |= cacheRect; Chris@0: Chris@0: paint.begin(this); Chris@0: paint.setClipRect(nonCacheRect); Chris@0: Chris@0: if (scrollables.empty()) { Chris@0: if (hasLightBackground()) { Chris@0: paint.setPen(Qt::white); Chris@0: paint.setBrush(Qt::white); Chris@0: } else { Chris@0: paint.setPen(Qt::black); Chris@0: paint.setBrush(Qt::black); Chris@0: } Chris@0: paint.drawRect(nonCacheRect); Chris@0: } Chris@0: Chris@0: paint.setPen(Qt::black); Chris@0: paint.setBrush(Qt::NoBrush); Chris@0: Chris@0: for (LayerList::iterator i = nonScrollables.begin(); i != nonScrollables.end(); ++i) { Chris@120: // Profiler profiler2("View::paintEvent non-cacheable"); Chris@36: (*i)->paint(this, paint, nonCacheRect); Chris@0: } Chris@0: Chris@9: paint.end(); Chris@8: Chris@9: paint.begin(this); Chris@9: if (e) paint.setClipRect(e->rect()); Chris@9: if (!m_selectionCached) { Chris@9: drawSelections(paint); Chris@8: } Chris@0: paint.end(); Chris@0: Chris@0: if (m_followPlay != PlaybackScrollContinuous) { Chris@0: Chris@0: paint.begin(this); Chris@0: Chris@0: if (long(m_playPointerFrame) > getStartFrame() && Chris@0: m_playPointerFrame < getEndFrame()) { Chris@0: Chris@15: int playx = getXForFrame(m_playPointerFrame); Chris@0: Chris@0: paint.setPen(Qt::black); Chris@0: paint.drawLine(playx - 1, 0, playx - 1, height() - 1); Chris@0: paint.drawLine(playx + 1, 0, playx + 1, height() - 1); Chris@0: paint.drawPoint(playx, 0); Chris@0: paint.drawPoint(playx, height() - 1); Chris@0: paint.setPen(Qt::white); Chris@0: paint.drawLine(playx, 1, playx, height() - 2); Chris@0: } Chris@0: Chris@0: paint.end(); Chris@0: } Chris@0: Chris@0: QFrame::paintEvent(e); Chris@0: } Chris@0: Chris@9: void Chris@9: View::drawSelections(QPainter &paint) Chris@9: { Chris@24: MultiSelection::SelectionList selections; Chris@9: Chris@9: if (m_manager) { Chris@9: selections = m_manager->getSelections(); Chris@9: if (m_manager->haveInProgressSelection()) { Chris@9: bool exclusive; Chris@9: Selection inProgressSelection = Chris@9: m_manager->getInProgressSelection(exclusive); Chris@9: if (exclusive) selections.clear(); Chris@9: selections.insert(inProgressSelection); Chris@9: } Chris@9: } Chris@9: Chris@9: paint.save(); Chris@9: paint.setBrush(QColor(150, 150, 255, 80)); Chris@9: Chris@10: int sampleRate = getModelsSampleRate(); Chris@10: Chris@34: QPoint localPos; Chris@34: long illuminateFrame = -1; Chris@34: bool closeToLeft, closeToRight; Chris@34: Chris@34: if (shouldIlluminateLocalSelection(localPos, closeToLeft, closeToRight)) { Chris@34: illuminateFrame = getFrameForX(localPos.x()); Chris@34: } Chris@34: Chris@10: const QFontMetrics &metrics = paint.fontMetrics(); Chris@10: Chris@24: for (MultiSelection::SelectionList::iterator i = selections.begin(); Chris@9: i != selections.end(); ++i) { Chris@9: Chris@15: int p0 = getXForFrame(i->getStartFrame()); Chris@15: int p1 = getXForFrame(i->getEndFrame()); Chris@9: Chris@10: if (p1 < 0 || p0 > width()) continue; Chris@9: Chris@9: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@9: std::cerr << "View::drawSelections: " << p0 << ",-1 [" << (p1-p0) << "x" << (height()+1) << "]" << std::endl; Chris@9: #endif Chris@9: Chris@34: bool illuminateThis = Chris@34: (illuminateFrame >= 0 && i->contains(illuminateFrame)); Chris@34: Chris@34: paint.setPen(QColor(150, 150, 255)); Chris@9: paint.drawRect(p0, -1, p1 - p0, height() + 1); Chris@10: Chris@34: if (illuminateThis) { Chris@34: paint.save(); Chris@34: if (hasLightBackground()) { Chris@34: paint.setPen(QPen(Qt::black, 2)); Chris@34: } else { Chris@34: paint.setPen(QPen(Qt::white, 2)); Chris@34: } Chris@34: if (closeToLeft) { Chris@34: paint.drawLine(p0, 1, p1, 1); Chris@34: paint.drawLine(p0, 0, p0, height()); Chris@34: paint.drawLine(p0, height() - 1, p1, height() - 1); Chris@34: } else if (closeToRight) { Chris@34: paint.drawLine(p0, 1, p1, 1); Chris@34: paint.drawLine(p1, 0, p1, height()); Chris@34: paint.drawLine(p0, height() - 1, p1, height() - 1); Chris@34: } else { Chris@34: paint.setBrush(Qt::NoBrush); Chris@34: paint.drawRect(p0, 1, p1 - p0, height() - 2); Chris@34: } Chris@34: paint.restore(); Chris@34: } Chris@34: Chris@110: if (sampleRate && shouldLabelSelections() && m_manager && Chris@110: m_manager->getOverlayMode() != ViewManager::NoOverlays) { Chris@10: Chris@10: QString startText = QString("%1 / %2") Chris@10: .arg(QString::fromStdString Chris@10: (RealTime::frame2RealTime Chris@10: (i->getStartFrame(), sampleRate).toText(true))) Chris@10: .arg(i->getStartFrame()); Chris@10: Chris@10: QString endText = QString(" %1 / %2") Chris@10: .arg(QString::fromStdString Chris@10: (RealTime::frame2RealTime Chris@10: (i->getEndFrame(), sampleRate).toText(true))) Chris@10: .arg(i->getEndFrame()); Chris@10: Chris@10: QString durationText = QString("(%1 / %2) ") Chris@10: .arg(QString::fromStdString Chris@10: (RealTime::frame2RealTime Chris@10: (i->getEndFrame() - i->getStartFrame(), sampleRate) Chris@10: .toText(true))) Chris@10: .arg(i->getEndFrame() - i->getStartFrame()); Chris@10: Chris@10: int sw = metrics.width(startText), Chris@10: ew = metrics.width(endText), Chris@10: dw = metrics.width(durationText); Chris@10: Chris@10: int sy = metrics.ascent() + metrics.height() + 4; Chris@10: int ey = sy; Chris@10: int dy = sy + metrics.height(); Chris@10: Chris@10: int sx = p0 + 2; Chris@10: int ex = sx; Chris@10: int dx = sx; Chris@10: Chris@10: if (sw + ew > (p1 - p0)) { Chris@10: ey += metrics.height(); Chris@10: dy += metrics.height(); Chris@10: } Chris@10: Chris@10: if (ew < (p1 - p0)) { Chris@10: ex = p1 - 2 - ew; Chris@10: } Chris@10: Chris@10: if (dw < (p1 - p0)) { Chris@10: dx = p1 - 2 - dw; Chris@10: } Chris@10: Chris@10: paint.drawText(sx, sy, startText); Chris@10: paint.drawText(ex, ey, endText); Chris@10: paint.drawText(dx, dy, durationText); Chris@10: } Chris@9: } Chris@9: Chris@9: paint.restore(); Chris@9: } Chris@9: Chris@3: QString Chris@3: View::toXmlString(QString indent, QString extraAttributes) const Chris@3: { Chris@3: QString s; Chris@3: Chris@3: s += indent; Chris@3: Chris@3: s += QString("\n") Chris@3: .arg(m_centreFrame) Chris@3: .arg(m_zoomLevel) Chris@3: .arg(m_followPan) Chris@3: .arg(m_followZoom) Chris@3: .arg(m_followPlay == PlaybackScrollContinuous ? "scroll" : Chris@3: m_followPlay == PlaybackScrollPage ? "page" : "ignore") Chris@3: .arg(m_lightBackground) Chris@3: .arg(extraAttributes); Chris@3: Chris@3: for (size_t i = 0; i < m_layers.size(); ++i) { Chris@3: s += m_layers[i]->toXmlString(indent + " "); Chris@3: } Chris@3: Chris@4: s += indent + "\n"; Chris@3: Chris@3: return s; Chris@3: } Chris@3: Chris@29: ViewPropertyContainer::ViewPropertyContainer(View *v) : Chris@29: m_v(v) Chris@29: { Chris@29: connect(m_v, SIGNAL(propertyChanged(PropertyName)), Chris@29: this, SIGNAL(propertyChanged(PropertyName))); Chris@29: } Chris@3: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "View.moc.cpp" Chris@0: #endif Chris@0: