Chris@127: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@127: Chris@127: /* Chris@127: Sonic Visualiser Chris@127: An audio file viewer and annotation editor. Chris@127: Centre for Digital Music, Queen Mary, University of London. Chris@127: This file copyright 2006 Chris Cannam. Chris@127: Chris@127: This program is free software; you can redistribute it and/or Chris@127: modify it under the terms of the GNU General Public License as Chris@127: published by the Free Software Foundation; either version 2 of the Chris@127: License, or (at your option) any later version. See the file Chris@127: COPYING included with this distribution for more information. Chris@127: */ Chris@127: Chris@128: #include "View.h" Chris@128: #include "layer/Layer.h" Chris@128: #include "data/model/Model.h" Chris@127: #include "base/ZoomConstraint.h" Chris@127: #include "base/Profiler.h" Chris@278: #include "base/Pitch.h" Chris@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@226: #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_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: } 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@216: int *min, int *max, int *deflt) const Chris@127: { Chris@216: if (deflt) *deflt = 1; Chris@127: if (name == "Global Scroll") return m_followPan; Chris@127: if (name == "Global Zoom") return m_followZoom; Chris@127: if (name == "Follow Playback") { Chris@127: if (min) *min = 0; Chris@127: if (max) *max = 2; Chris@216: if (deflt) *deflt = int(PlaybackScrollPage); Chris@127: return int(m_followPlay); Chris@127: } Chris@127: if (min) *min = 0; Chris@127: if (max) *max = 0; Chris@216: if (deflt) *deflt = 0; Chris@127: return 0; Chris@127: } Chris@127: Chris@127: QString Chris@127: View::getPropertyValueLabel(const PropertyContainer::PropertyName &name, Chris@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@206: delete m_cache; Chris@206: m_cache = 0; Chris@133: update(); Chris@133: } Chris@133: Chris@133: void Chris@133: View::zoomWheelsEnabledChanged() Chris@133: { Chris@133: // subclass might override this Chris@133: } Chris@133: Chris@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@211: if (e) emit centreFrameChanged(f, m_followPan, m_followPlay); 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@244: // 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@222: emit zoomLevelChanged(z, m_followZoom); Chris@127: update(); Chris@127: } Chris@127: } Chris@127: Chris@224: bool Chris@224: View::hasLightBackground() const Chris@224: { Chris@224: for (LayerList::const_iterator i = m_layers.begin(); Chris@224: i != m_layers.end(); ++i) { Chris@224: if (!(*i)->hasLightBackground()) return false; Chris@224: } Chris@224: return true; Chris@224: } Chris@224: 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@268: connect(layer, SIGNAL(layerMeasurementRectsChanged()), Chris@268: this, SLOT(layerMeasurementRectsChanged())); 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@211: m_manager->disconnect(this, SLOT(globalCentreFrameChanged(unsigned long))); Chris@211: m_manager->disconnect(this, SLOT(viewCentreFrameChanged(View *, unsigned long))); Chris@211: m_manager->disconnect(this, SLOT(viewManagerPlaybackFrameChanged(unsigned long))); Chris@222: m_manager->disconnect(this, SLOT(viewZoomLevelChanged(View *, unsigned long, bool))); Chris@211: m_manager->disconnect(this, SLOT(toolModeChanged())); Chris@211: m_manager->disconnect(this, SLOT(selectionChanged())); Chris@211: m_manager->disconnect(this, SLOT(overlayModeChanged())); Chris@211: m_manager->disconnect(this, SLOT(zoomWheelsEnabledChanged())); Chris@222: disconnect(m_manager, SLOT(viewCentreFrameChanged(unsigned long, bool, PlaybackFollowMode))); Chris@222: disconnect(m_manager, SLOT(zoomLevelChanged(unsigned long, bool))); Chris@127: } Chris@127: Chris@127: m_manager = manager; Chris@127: Chris@211: connect(m_manager, SIGNAL(globalCentreFrameChanged(unsigned long)), Chris@211: this, SLOT(globalCentreFrameChanged(unsigned long))); Chris@213: connect(m_manager, SIGNAL(viewCentreFrameChanged(View *, unsigned long)), Chris@211: this, SLOT(viewCentreFrameChanged(View *, unsigned long))); Chris@127: connect(m_manager, SIGNAL(playbackFrameChanged(unsigned long)), Chris@127: this, SLOT(viewManagerPlaybackFrameChanged(unsigned long))); Chris@211: Chris@222: connect(m_manager, SIGNAL(viewZoomLevelChanged(View *, unsigned long, bool)), Chris@222: this, SLOT(viewZoomLevelChanged(View *, unsigned long, bool))); Chris@211: 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@211: connect(this, SIGNAL(centreFrameChanged(unsigned long, bool, Chris@211: PlaybackFollowMode)), Chris@211: m_manager, SLOT(viewCentreFrameChanged(unsigned long, bool, Chris@211: PlaybackFollowMode))); Chris@211: Chris@222: connect(this, SIGNAL(zoomLevelChanged(unsigned long, bool)), Chris@222: m_manager, SLOT(viewZoomLevelChanged(unsigned long, bool))); Chris@127: Chris@236: if (m_followPlay != PlaybackIgnore) { Chris@236: // std::cerr << "View::setViewManager: setting centre frame to playback frame: " << m_manager->getPlaybackFrame() << std::endl; Chris@236: setCentreFrame(m_manager->getPlaybackFrame(), false); Chris@236: } else if (m_followPan) { Chris@236: // std::cerr << "View::setViewManager: setting centre frame to global centre frame: " << m_manager->getGlobalCentreFrame() << std::endl; Chris@236: setCentreFrame(m_manager->getGlobalCentreFrame(), false); Chris@236: } Chris@236: if (m_followZoom) setZoomLevel(m_manager->getGlobalZoom()); Chris@236: 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@267: View::drawVisibleText(QPainter &paint, int x, int y, QString text, TextStyle style) const Chris@127: { Chris@127: if (style == OutlinedText) { Chris@127: Chris@279: paint.save(); Chris@279: Chris@279: QColor penColour, surroundColour; Chris@279: Chris@279: if (hasLightBackground()) { Chris@279: penColour = Qt::black; Chris@279: surroundColour = Qt::white; Chris@279: } else { Chris@279: penColour = Qt::white; Chris@279: surroundColour = Qt::black; Chris@279: } Chris@279: Chris@279: /* 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@279: */ 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@279: /* Chris@127: paint.setPen(origPenColour); Chris@279: */ Chris@279: paint.restore(); 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: checkProgress(obj); Chris@127: Chris@127: update(); 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@268: View::layerMeasurementRectsChanged() Chris@268: { Chris@268: Layer *layer = dynamic_cast(sender()); Chris@268: if (layer) update(); Chris@268: } Chris@268: Chris@268: void Chris@127: View::layerNameChanged() Chris@127: { Chris@127: Layer *layer = dynamic_cast(sender()); Chris@127: if (layer) emit propertyContainerNameChanged(layer); Chris@127: } Chris@127: Chris@127: void Chris@211: View::globalCentreFrameChanged(unsigned long f) Chris@127: { Chris@211: if (m_followPan) { Chris@211: setCentreFrame(f, false); Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@248: View::viewCentreFrameChanged(View *, unsigned long ) Chris@211: { Chris@211: // We do nothing with this, but a subclass might Chris@211: } Chris@211: Chris@211: 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@211: setCentreFrame(f, false); 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@222: View::viewZoomLevelChanged(View *p, unsigned long z, bool locked) Chris@127: { Chris@244: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@244: std::cerr << "View[" << this << "]: viewZoomLevelChanged(" << p << ", " << z << ", " << locked << ")" << std::endl; Chris@244: #endif Chris@127: if (m_followZoom && p != this && locked) { Chris@222: setZoomLevel(z); Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@127: View::selectionChanged() Chris@127: { Chris@127: if (m_selectionCached) { Chris@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@222: View::getFirstVisibleFrame() const Chris@222: { Chris@222: long f0 = getStartFrame(); Chris@222: size_t f = getModelsStartFrame(); Chris@222: if (f0 < 0 || f0 < long(f)) return f; Chris@222: return f0; Chris@222: } Chris@222: Chris@222: size_t Chris@222: View::getLastVisibleFrame() const Chris@222: { Chris@222: size_t f0 = getEndFrame(); Chris@222: size_t f = getModelsEndFrame(); Chris@222: if (f0 > f) return f; Chris@222: return f0; Chris@222: } Chris@222: Chris@222: 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@217: bool Chris@217: View::hasTopLayerTimeXAxis() const Chris@217: { Chris@217: LayerList::const_iterator i = m_layers.end(); Chris@217: if (i == m_layers.begin()) return false; Chris@217: --i; Chris@217: return (*i)->hasTimeXAxis(); Chris@217: } Chris@217: Chris@127: void Chris@127: View::zoom(bool in) Chris@127: { Chris@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@236: // std::cerr << "View::paintEvent: centre frame is " << m_centreFrame << 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@244: Chris@244: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@244: std::cerr << "View(" << this << "): cache " << m_cache << ", cache zoom " Chris@244: << m_cacheZoomLevel << ", zoom " << m_zoomLevel << std::endl; Chris@244: #endif Chris@244: 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@244: delete m_cache; Chris@244: m_cache = 0; 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@211: bool showPlayPointer = true; Chris@211: if (m_followPlay == PlaybackScrollContinuous) { Chris@211: showPlayPointer = false; Chris@211: } else if (long(m_playPointerFrame) <= getStartFrame() || Chris@211: m_playPointerFrame >= getEndFrame()) { Chris@211: showPlayPointer = false; Chris@211: } else if (m_manager && !m_manager->isPlaying()) { Chris@211: if (m_playPointerFrame == getCentreFrame() && Chris@211: m_followPlay != PlaybackIgnore) { Chris@211: showPlayPointer = false; Chris@211: } Chris@211: } Chris@211: Chris@211: if (showPlayPointer) { Chris@127: Chris@127: paint.begin(this); Chris@127: Chris@211: int playx = getXForFrame(m_playPointerFrame); Chris@211: Chris@211: paint.setPen(Qt::black); Chris@211: paint.drawLine(playx - 1, 0, playx - 1, height() - 1); Chris@211: paint.drawLine(playx + 1, 0, playx + 1, height() - 1); Chris@211: paint.drawPoint(playx, 0); Chris@211: paint.drawPoint(playx, height() - 1); Chris@211: paint.setPen(Qt::white); Chris@211: paint.drawLine(playx, 1, playx, height() - 2); 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@217: if (!hasTopLayerTimeXAxis()) return; Chris@217: 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@267: void Chris@270: View::drawMeasurementRect(QPainter &paint, const Layer *topLayer, QRect r, Chris@270: bool focus) const Chris@267: { Chris@268: // std::cerr << "View::drawMeasurementRect(" << r.x() << "," << r.y() << " " Chris@268: // << r.width() << "x" << r.height() << ")" << std::endl; Chris@268: Chris@267: if (r.x() + r.width() < 0 || r.x() >= width()) return; Chris@267: Chris@270: if (r.width() != 0 || r.height() != 0) { Chris@270: paint.save(); Chris@270: if (focus) { Chris@270: paint.setPen(Qt::NoPen); Chris@272: QColor brushColour(Qt::black); Chris@272: brushColour.setAlpha(hasLightBackground() ? 15 : 40); Chris@270: paint.setBrush(brushColour); Chris@270: if (r.x() > 0) { Chris@270: paint.drawRect(0, 0, r.x(), height()); Chris@270: } Chris@270: if (r.x() + r.width() < width()) { Chris@270: paint.drawRect(r.x() + r.width(), 0, width()-r.x()-r.width(), height()); Chris@270: } Chris@270: if (r.y() > 0) { Chris@270: paint.drawRect(r.x(), 0, r.width(), r.y()); Chris@270: } Chris@270: if (r.y() + r.height() < height()) { Chris@270: paint.drawRect(r.x(), r.y() + r.height(), r.width(), height()-r.y()-r.height()); Chris@270: } Chris@270: paint.setBrush(Qt::NoBrush); Chris@270: } Chris@270: paint.setPen(Qt::green); Chris@270: paint.drawRect(r); Chris@270: paint.restore(); Chris@270: } else { Chris@270: paint.save(); Chris@270: paint.setPen(Qt::green); Chris@270: paint.drawPoint(r.x(), r.y()); Chris@270: paint.restore(); Chris@270: } Chris@270: Chris@270: if (!focus) return; Chris@270: Chris@278: paint.save(); Chris@278: QFont fn = paint.font(); Chris@278: if (fn.pointSize() > 8) { Chris@278: fn.setPointSize(fn.pointSize() - 1); Chris@278: paint.setFont(fn); Chris@278: } Chris@278: Chris@267: int fontHeight = paint.fontMetrics().height(); Chris@267: int fontAscent = paint.fontMetrics().ascent(); Chris@267: Chris@267: float v0, v1; Chris@267: QString u0, u1; Chris@267: bool b0 = false, b1 = false; Chris@267: Chris@267: QString axs, ays, bxs, bys, dxs, dys; Chris@267: Chris@267: int axx, axy, bxx, bxy, dxx, dxy; Chris@267: int aw = 0, bw = 0, dw = 0; Chris@267: Chris@267: int labelCount = 0; Chris@267: Chris@267: if ((b0 = topLayer->getXScaleValue(this, r.x(), v0, u0))) { Chris@267: axs = QString("%1 %2").arg(v0).arg(u0); Chris@278: if (u0 == "Hz" && Pitch::isFrequencyInMidiRange(v0)) { Chris@278: axs = QString("%1 (%2)").arg(axs) Chris@278: .arg(Pitch::getPitchLabelForFrequency(v0)); Chris@278: } Chris@267: aw = paint.fontMetrics().width(axs); Chris@267: ++labelCount; Chris@267: } Chris@267: Chris@267: if (r.width() > 0) { Chris@267: if ((b1 = topLayer->getXScaleValue(this, r.x() + r.width(), v1, u1))) { Chris@267: bxs = QString("%1 %2").arg(v1).arg(u1); Chris@278: if (u1 == "Hz" && Pitch::isFrequencyInMidiRange(v1)) { Chris@278: bxs = QString("%1 (%2)").arg(bxs) Chris@278: .arg(Pitch::getPitchLabelForFrequency(v1)); Chris@278: } Chris@267: bw = paint.fontMetrics().width(bxs); Chris@267: } Chris@267: } Chris@267: Chris@283: if (b0 && b1 && v1 != v0 && u0 == u1) { Chris@267: dxs = QString("(%1 %2)").arg(fabs(v1 - v0)).arg(u1); Chris@267: dw = paint.fontMetrics().width(dxs); Chris@267: } Chris@267: Chris@267: b0 = false; Chris@267: b1 = false; Chris@267: Chris@267: if ((b0 = topLayer->getYScaleValue(this, r.y(), v0, u0))) { Chris@267: ays = QString("%1 %2").arg(v0).arg(u0); Chris@278: if (u0 == "Hz" && Pitch::isFrequencyInMidiRange(v0)) { Chris@278: ays = QString("%1 (%2)").arg(ays) Chris@278: .arg(Pitch::getPitchLabelForFrequency(v0)); Chris@278: } Chris@267: aw = std::max(aw, paint.fontMetrics().width(ays)); Chris@267: ++labelCount; Chris@267: } Chris@267: Chris@267: if (r.height() > 0) { Chris@267: if ((b1 = topLayer->getYScaleValue(this, r.y() + r.height(), v1, u1))) { Chris@267: bys = QString("%1 %2").arg(v1).arg(u1); Chris@278: if (u1 == "Hz" && Pitch::isFrequencyInMidiRange(v1)) { Chris@278: bys = QString("%1 (%2)").arg(bys) Chris@278: .arg(Pitch::getPitchLabelForFrequency(v1)); Chris@278: } Chris@267: bw = std::max(bw, paint.fontMetrics().width(bys)); Chris@267: } Chris@267: } Chris@274: Chris@274: bool bd = false; Chris@274: float dy = 0.f; Chris@274: QString du; Chris@274: Chris@274: if ((bd = topLayer->getYScaleDifference(this, r.y(), r.y() + r.height(), Chris@283: dy, du)) && Chris@283: dy != 0) { Chris@274: if (du != "") { Chris@274: dys = QString("(%1 %2)").arg(dy).arg(du); Chris@274: } else { Chris@274: dys = QString("(%1)").arg(dy); Chris@274: } Chris@267: dw = std::max(dw, paint.fontMetrics().width(dys)); Chris@267: } Chris@267: Chris@267: int mw = r.width(); Chris@267: int mh = r.height(); Chris@267: Chris@267: bool edgeLabelsInside = false; Chris@267: bool sizeLabelsInside = false; Chris@267: Chris@267: if (mw < std::max(aw, std::max(bw, dw)) + 4) { Chris@267: // defaults stand Chris@267: } else if (mw < aw + bw + 4) { Chris@267: if (mh > fontHeight * labelCount * 3 + 4) { Chris@267: edgeLabelsInside = true; Chris@267: sizeLabelsInside = true; Chris@267: } else if (mh > fontHeight * labelCount * 2 + 4) { Chris@267: edgeLabelsInside = true; Chris@267: } Chris@267: } else if (mw < aw + bw + dw + 4) { Chris@267: if (mh > fontHeight * labelCount * 3 + 4) { Chris@267: edgeLabelsInside = true; Chris@267: sizeLabelsInside = true; Chris@267: } else if (mh > fontHeight * labelCount + 4) { Chris@267: edgeLabelsInside = true; Chris@267: } Chris@267: } else { Chris@267: if (mh > fontHeight * labelCount + 4) { Chris@267: edgeLabelsInside = true; Chris@267: sizeLabelsInside = true; Chris@267: } Chris@267: } Chris@267: Chris@267: if (edgeLabelsInside) { Chris@267: Chris@267: axx = r.x() + 2; Chris@267: axy = r.y() + fontAscent + 2; Chris@267: Chris@267: bxx = r.x() + r.width() - bw - 2; Chris@267: bxy = r.y() + r.height() - (labelCount-1) * fontHeight - 2; Chris@267: Chris@267: } else { Chris@267: Chris@267: axx = r.x() - aw - 2; Chris@267: axy = r.y() + fontAscent; Chris@267: Chris@267: bxx = r.x() + r.width() + 2; Chris@267: bxy = r.y() + r.height() - (labelCount-1) * fontHeight; Chris@267: } Chris@267: Chris@267: dxx = r.width()/2 + r.x() - dw/2; Chris@267: Chris@267: if (sizeLabelsInside) { Chris@267: Chris@267: dxy = r.height()/2 + r.y() - (labelCount * fontHeight)/2 + fontAscent; Chris@267: Chris@267: } else { Chris@267: Chris@267: dxy = r.y() + r.height() + fontAscent + 2; Chris@267: } Chris@267: Chris@267: if (axs != "") { Chris@267: drawVisibleText(paint, axx, axy, axs, OutlinedText); Chris@267: axy += fontHeight; Chris@267: } Chris@267: Chris@267: if (ays != "") { Chris@267: drawVisibleText(paint, axx, axy, ays, OutlinedText); Chris@267: axy += fontHeight; Chris@267: } Chris@267: Chris@267: if (bxs != "") { Chris@267: drawVisibleText(paint, bxx, bxy, bxs, OutlinedText); Chris@267: bxy += fontHeight; Chris@267: } Chris@267: Chris@267: if (bys != "") { Chris@267: drawVisibleText(paint, bxx, bxy, bys, OutlinedText); Chris@267: bxy += fontHeight; Chris@267: } Chris@267: Chris@267: if (dxs != "") { Chris@267: drawVisibleText(paint, dxx, dxy, dxs, OutlinedText); Chris@267: dxy += fontHeight; Chris@267: } Chris@267: Chris@267: if (dys != "") { Chris@267: drawVisibleText(paint, dxx, dxy, dys, OutlinedText); Chris@267: dxy += fontHeight; Chris@267: } Chris@278: Chris@278: paint.restore(); Chris@267: } Chris@267: Chris@227: bool Chris@229: View::render(QPainter &paint, int xorigin, size_t f0, size_t f1) Chris@227: { Chris@227: size_t x0 = f0 / m_zoomLevel; Chris@227: size_t x1 = f1 / m_zoomLevel; Chris@227: Chris@227: size_t w = x1 - x0; Chris@227: Chris@227: size_t origCentreFrame = m_centreFrame; Chris@227: Chris@227: bool someLayersIncomplete = false; Chris@227: Chris@227: for (LayerList::iterator i = m_layers.begin(); Chris@227: i != m_layers.end(); ++i) { Chris@227: Chris@227: int c = (*i)->getCompletion(this); Chris@227: if (c < 100) { Chris@227: someLayersIncomplete = true; Chris@227: break; Chris@227: } Chris@227: } Chris@227: Chris@227: if (someLayersIncomplete) { Chris@227: Chris@227: QProgressDialog progress(tr("Waiting for layers to be ready..."), Chris@227: tr("Cancel"), 0, 100, this); Chris@227: Chris@227: int layerCompletion = 0; Chris@227: Chris@227: while (layerCompletion < 100) { Chris@227: Chris@227: for (LayerList::iterator i = m_layers.begin(); Chris@227: i != m_layers.end(); ++i) { Chris@227: Chris@227: int c = (*i)->getCompletion(this); Chris@227: if (i == m_layers.begin() || c < layerCompletion) { Chris@227: layerCompletion = c; Chris@227: } Chris@227: } Chris@227: Chris@227: if (layerCompletion >= 100) break; Chris@227: Chris@227: progress.setValue(layerCompletion); Chris@227: qApp->processEvents(); Chris@227: if (progress.wasCanceled()) { Chris@227: update(); Chris@227: return false; Chris@227: } Chris@227: Chris@227: usleep(50000); Chris@227: } Chris@227: } Chris@227: Chris@227: QProgressDialog progress(tr("Rendering image..."), Chris@227: tr("Cancel"), 0, w / width(), this); Chris@227: Chris@227: for (size_t x = 0; x < w; x += width()) { Chris@227: Chris@227: progress.setValue(x / width()); Chris@227: qApp->processEvents(); Chris@227: if (progress.wasCanceled()) { Chris@227: m_centreFrame = origCentreFrame; Chris@227: update(); Chris@227: return false; Chris@227: } Chris@227: Chris@229: m_centreFrame = f0 + (x + width()/2) * m_zoomLevel; Chris@227: Chris@227: QRect chunk(0, 0, width(), height()); Chris@227: Chris@227: if (hasLightBackground()) { Chris@227: paint.setPen(Qt::white); Chris@227: paint.setBrush(Qt::white); Chris@227: } else { Chris@227: paint.setPen(Qt::black); Chris@227: paint.setBrush(Qt::black); Chris@227: } Chris@227: Chris@229: paint.drawRect(QRect(xorigin + x, 0, width(), height())); Chris@227: Chris@227: paint.setPen(Qt::black); Chris@227: paint.setBrush(Qt::NoBrush); Chris@227: Chris@227: for (LayerList::iterator i = m_layers.begin(); Chris@227: i != m_layers.end(); ++i) { Chris@227: Chris@227: paint.setRenderHint(QPainter::Antialiasing, false); Chris@227: Chris@227: paint.save(); Chris@229: paint.translate(xorigin + x, 0); Chris@227: Chris@229: std::cerr << "Centre frame now: " << m_centreFrame << " drawing to " << chunk.x() + x + xorigin << ", " << chunk.width() << std::endl; Chris@227: Chris@227: (*i)->paint(this, paint, chunk); Chris@227: Chris@227: paint.restore(); Chris@227: } Chris@227: } Chris@227: Chris@227: m_centreFrame = origCentreFrame; Chris@227: update(); Chris@227: return true; Chris@227: } Chris@227: Chris@227: QImage * Chris@227: View::toNewImage() Chris@227: { Chris@227: size_t f0 = getModelsStartFrame(); Chris@227: size_t f1 = getModelsEndFrame(); Chris@227: Chris@229: return toNewImage(f0, f1); Chris@229: } Chris@229: Chris@229: QImage * Chris@229: View::toNewImage(size_t f0, size_t f1) Chris@229: { Chris@227: size_t x0 = f0 / getZoomLevel(); Chris@227: size_t x1 = f1 / getZoomLevel(); Chris@227: Chris@227: QImage *image = new QImage(x1 - x0, height(), QImage::Format_RGB32); Chris@227: Chris@227: QPainter *paint = new QPainter(image); Chris@229: if (!render(*paint, 0, f0, f1)) { Chris@227: delete paint; Chris@227: delete image; Chris@227: return 0; Chris@227: } else { Chris@227: delete paint; Chris@227: return image; Chris@227: } Chris@227: } Chris@227: Chris@229: QSize Chris@229: View::getImageSize() Chris@229: { Chris@229: size_t f0 = getModelsStartFrame(); Chris@229: size_t f1 = getModelsEndFrame(); Chris@229: Chris@229: return getImageSize(f0, f1); Chris@229: } Chris@229: Chris@229: QSize Chris@229: View::getImageSize(size_t f0, size_t f1) Chris@229: { Chris@229: size_t x0 = f0 / getZoomLevel(); Chris@229: size_t x1 = f1 / getZoomLevel(); Chris@229: Chris@229: return QSize(x1 - x0, height()); Chris@229: } Chris@229: 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(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@269: s += m_layers[i]->toBriefXmlString(indent + " ", Chris@269: QString("visible=\"%1\"") Chris@269: .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: