Chris@0: /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: A waveform viewer and audio annotation editor. Chris@1: Chris Cannam, Queen Mary University of London, 2005-2006 Chris@0: Chris@0: This is experimental software. Not for distribution. 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@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@16: #include Chris@0: Chris@18: //#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@0: m_manager(0) Chris@0: { Chris@0: // QWidget::setAttribute(Qt::WA_PaintOnScreen); Chris@0: } Chris@0: Chris@0: View::~View() Chris@0: { Chris@0: m_deleting = true; Chris@0: Chris@0: for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@0: delete *i; Chris@0: } Chris@0: } Chris@0: Chris@0: PropertyContainer::PropertyList Chris@0: View::getProperties() const Chris@0: { Chris@0: PropertyList list; Chris@0: list.push_back(tr("Global Scroll")); Chris@0: list.push_back(tr("Global Zoom")); Chris@0: list.push_back(tr("Follow Playback")); Chris@0: return list; Chris@0: } Chris@0: Chris@0: PropertyContainer::PropertyType Chris@0: View::getPropertyType(const PropertyName &name) const Chris@0: { Chris@0: if (name == tr("Global Scroll")) return ToggleProperty; Chris@0: if (name == tr("Global Zoom")) return ToggleProperty; Chris@0: if (name == tr("Follow Playback")) return ValueProperty; Chris@0: return InvalidProperty; Chris@0: } Chris@0: Chris@0: int Chris@0: View::getPropertyRangeAndValue(const PropertyName &name, Chris@0: int *min, int *max) const Chris@0: { Chris@0: if (name == tr("Global Scroll")) return m_followPan; Chris@0: if (name == tr("Global Zoom")) return m_followZoom; Chris@0: if (name == tr("Follow Playback")) { *min = 0; *max = 2; return int(m_followPlay); } Chris@0: return PropertyContainer::getPropertyRangeAndValue(name, min, max); Chris@0: } Chris@0: Chris@0: QString Chris@0: View::getPropertyValueLabel(const PropertyName &name, Chris@0: int value) const Chris@0: { Chris@0: if (name == tr("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@0: View::setProperty(const PropertyName &name, int value) Chris@0: { Chris@0: if (name == tr("Global Scroll")) { Chris@0: setFollowGlobalPan(value != 0); Chris@0: } else if (name == tr("Global Zoom")) { Chris@0: setFollowGlobalZoom(value != 0); Chris@0: } else if (name == tr("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@0: if (i == 0) return this; Chris@0: return m_layers[i-1]; Chris@0: } Chris@0: Chris@0: void Chris@0: View::propertyContainerSelected(PropertyContainer *pc) Chris@0: { Chris@8: if (pc == this) { 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@8: 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@22: int Chris@22: View::getZoomLevel() const Chris@22: { 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@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@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@0: emit propertyContainerPropertyChanged(this); Chris@0: } Chris@0: Chris@0: void Chris@0: View::setFollowGlobalZoom(bool f) Chris@0: { Chris@0: m_followZoom = f; Chris@0: emit propertyContainerPropertyChanged(this); Chris@0: } Chris@0: Chris@0: void Chris@0: View::setPlaybackFollow(PlaybackFollowMode m) Chris@0: { Chris@0: m_followPlay = m; Chris@0: emit propertyContainerPropertyChanged(this); 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@0: delete m_cache; Chris@0: m_cache = 0; 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@0: delete m_cache; Chris@0: m_cache = 0; 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@0: 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@21: bool modifierPressed = // we only care about these ones Chris@21: ((QApplication::keyboardModifiers() & Qt::ShiftModifier) || Chris@21: (QApplication::keyboardModifiers() & Qt::ControlModifier)); Chris@20: Chris@0: switch (m_followPlay) { Chris@0: Chris@0: case PlaybackScrollContinuous: Chris@10: if (QApplication::mouseButtons() == Qt::NoButton && Chris@21: !modifierPressed) { 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@10: repaint(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@15: if (QApplication::mouseButtons() == Qt::NoButton && Chris@21: !modifierPressed) { 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@0: if (!(*i)->isLayerScrollable()) return false; Chris@0: } Chris@0: return true; Chris@0: } Chris@0: Chris@0: View::LayerList Chris@0: View::getScrollableBackLayers(bool &changed) const Chris@0: { Chris@0: changed = false; Chris@0: Chris@0: LayerList scrollables; Chris@0: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@0: if ((*i)->isLayerScrollable()) scrollables.push_back(*i); Chris@0: else { Chris@0: if (scrollables != m_lastScrollableBackLayers) { Chris@0: m_lastScrollableBackLayers = scrollables; Chris@0: changed = true; Chris@0: } Chris@0: return scrollables; Chris@0: } Chris@0: } Chris@0: Chris@0: if (scrollables.size() == 1 && Chris@0: dynamic_cast(*scrollables.begin())) { Chris@0: Chris@0: // If only the ruler is scrollable, it's not worth the bother Chris@0: // -- it probably redraws as quickly as it refreshes from Chris@0: // cache Chris@0: scrollables.clear(); Chris@0: } Chris@0: Chris@0: if (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@0: View::getNonScrollableFrontLayers(bool &changed) const Chris@0: { Chris@0: changed = false; Chris@0: LayerList scrollables = getScrollableBackLayers(changed); 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@0: size_t count = 0; Chris@0: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@0: if (count < scrollables.size()) { Chris@0: ++count; Chris@0: continue; Chris@0: } Chris@0: nonScrollables.push_back(*i); Chris@0: } Chris@0: Chris@0: if (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@0: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@0: Chris@0: if ((*i)->getZoomConstraint()) { Chris@0: Chris@0: size_t thisBlockSize = Chris@0: (*i)->getZoomConstraint()->getNearestBlockSize Chris@0: (blockSize, dir); Chris@0: Chris@0: // Go for the block size that's furthest from the one Chris@0: // passed in. Most of the time, that's what we want. Chris@0: if (!haveCandidate || Chris@0: (thisBlockSize > blockSize && thisBlockSize > candidate) || Chris@0: (thisBlockSize < blockSize && thisBlockSize < candidate)) { Chris@0: candidate = thisBlockSize; Chris@0: haveCandidate = true; Chris@0: } 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@0: int completion = i->first->getCompletion(); 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@0: // Profiler prof("View::paintEvent", true); 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@0: // are, we should store only those in the cache Chris@0: Chris@0: bool layersChanged = false; Chris@0: LayerList scrollables = getScrollableBackLayers(layersChanged); Chris@0: LayerList nonScrollables = getNonScrollableFrontLayers(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@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@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@0: (*i)->paint(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@0: (*i)->paint(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.setPen(QColor(150, 150, 255)); Chris@9: paint.setBrush(QColor(150, 150, 255, 80)); Chris@9: Chris@10: int sampleRate = getModelsSampleRate(); Chris@10: 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@9: paint.drawRect(p0, -1, p1 - p0, height() + 1); Chris@10: Chris@10: if (sampleRate && shouldLabelSelections()) { 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@3: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "View.moc.cpp" Chris@0: #endif Chris@0: