Chris@127: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@127: Chris@127: /* Chris@127: Sonic Visualiser Chris@127: An audio file viewer and annotation editor. Chris@127: Centre for Digital Music, Queen Mary, University of London. Chris@127: This file copyright 2006 Chris Cannam. Chris@127: Chris@127: This program is free software; you can redistribute it and/or Chris@127: modify it under the terms of the GNU General Public License as Chris@127: published by the Free Software Foundation; either version 2 of the Chris@127: License, or (at your option) any later version. See the file Chris@127: COPYING included with this distribution for more information. Chris@127: */ Chris@127: Chris@128: #include "View.h" Chris@128: #include "layer/Layer.h" Chris@128: #include "data/model/Model.h" Chris@127: #include "base/ZoomConstraint.h" Chris@127: #include "base/Profiler.h" Chris@278: #include "base/Pitch.h" Chris@338: #include "base/Preferences.h" Chris@127: Chris@301: #include "layer/TimeRulerLayer.h" Chris@287: #include "layer/SingleColourLayer.h" Chris@301: #include "data/model/PowerOfSqrtTwoZoomConstraint.h" Chris@301: #include "data/model/RangeSummarisableTimeValueModel.h" Chris@127: Chris@127: #include Chris@127: #include Chris@127: #include Chris@127: #include Chris@226: #include Chris@316: #include Chris@338: #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@298: Chris@298: emit propertyContainerSelected(pc); 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@313: return getFrameForX(0); 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@333: if (e) { Chris@333: size_t rf = alignToReference(f); Chris@355: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@333: std::cerr << "View[" << this << "]::setCentreFrame(" << f Chris@333: << "): emitting centreFrameChanged(" Chris@333: << rf << ")" << std::endl; Chris@355: #endif Chris@333: emit centreFrameChanged(rf, m_followPan, m_followPlay); Chris@333: } Chris@127: } Chris@127: Chris@127: return changeVisible; Chris@127: } Chris@127: Chris@127: int Chris@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@313: long z = (long)m_zoomLevel; Chris@313: long frame = m_centreFrame - (width()/2) * z; Chris@354: Chris@355: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@354: std::cerr << "View::getFrameForX(" << x << "): z = " << z << ", m_centreFrame = " << m_centreFrame << ", width() = " << width() << ", frame = " << frame << std::endl; Chris@355: #endif Chris@354: Chris@313: frame = (frame / z) * z; // this is start frame Chris@313: return frame + x * z; 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@287: bool darkPalette = false; Chris@292: if (m_manager) darkPalette = m_manager->getGlobalDarkBackground(); Chris@287: Chris@287: Layer::ColourSignificance maxSignificance = Layer::ColourAbsent; Chris@287: bool mostSignificantHasDarkBackground = false; Chris@287: Chris@224: for (LayerList::const_iterator i = m_layers.begin(); Chris@224: i != m_layers.end(); ++i) { Chris@287: Chris@287: Layer::ColourSignificance s = (*i)->getLayerColourSignificance(); Chris@287: bool light = (*i)->hasLightBackground(); Chris@287: Chris@287: if (int(s) > int(maxSignificance)) { Chris@287: maxSignificance = s; Chris@287: mostSignificantHasDarkBackground = !light; Chris@287: } else if (s == maxSignificance && !light) { Chris@287: mostSignificantHasDarkBackground = true; Chris@287: } Chris@224: } Chris@287: Chris@287: if (int(maxSignificance) >= int(Layer::ColourAndBackgroundSignificant)) { Chris@287: return !mostSignificantHasDarkBackground; Chris@287: } else { Chris@287: return !darkPalette; Chris@287: } Chris@287: } Chris@287: Chris@287: QColor Chris@287: View::getBackground() const Chris@287: { Chris@287: bool light = hasLightBackground(); Chris@287: Chris@287: QColor widgetbg = palette().window().color(); Chris@287: bool widgetLight = Chris@287: (widgetbg.red() + widgetbg.green() + widgetbg.blue()) > 384; Chris@287: Chris@296: if (widgetLight == light) { Chris@296: if (widgetLight) { Chris@296: return widgetbg.light(); Chris@296: } else { Chris@296: return widgetbg.dark(); Chris@296: } Chris@296: } Chris@287: else if (light) return Qt::white; Chris@287: else return Qt::black; Chris@287: } Chris@287: Chris@287: QColor Chris@287: View::getForeground() const Chris@287: { Chris@287: bool light = hasLightBackground(); Chris@287: Chris@287: QColor widgetfg = palette().text().color(); Chris@287: bool widgetLight = Chris@287: (widgetfg.red() + widgetfg.green() + widgetfg.blue()) > 384; Chris@287: Chris@287: if (widgetLight != light) return widgetfg; Chris@287: else if (light) return Qt::black; Chris@287: else return Qt::white; Chris@224: } Chris@224: Chris@127: View::LayerProgressBar::LayerProgressBar(QWidget *parent) : Chris@127: QProgressBar(parent) Chris@127: { 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@287: SingleColourLayer *scl = dynamic_cast(layer); Chris@287: if (scl) scl->setDefaultColourFor(this); Chris@287: 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@339: Chris@338: QFont f(m_progressBars[layer]->font()); Chris@339: int fs = Preferences::getInstance()->getViewFontSize(); Chris@339: f.setPointSize(std::min(fs, int(ceil(fs * 0.85)))); Chris@339: Chris@338: m_progressBars[layer]->setFont(f); 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@320: connect(layer, SIGNAL(modelAlignmentCompletionChanged()), Chris@320: this, SLOT(modelAlignmentCompletionChanged())); 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@320: disconnect(layer, SIGNAL(modelAlignmentCompletionChanged()), Chris@320: this, SLOT(modelAlignmentCompletionChanged())); 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@287: penColour = getForeground(); Chris@287: surroundColour = getBackground(); 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@287: 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@336: emit layerModelChanged(); Chris@336: 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@326: // std::cerr << "View(" << this << ")::modelCompletionChanged()" << std::endl; Chris@301: Chris@127: QObject *obj = sender(); Chris@127: checkProgress(obj); Chris@127: } Chris@127: Chris@127: void Chris@320: View::modelAlignmentCompletionChanged() Chris@320: { Chris@326: // std::cerr << "View(" << this << ")::modelAlignmentCompletionChanged()" << std::endl; Chris@320: Chris@320: QObject *obj = sender(); Chris@320: checkProgress(obj); Chris@320: } Chris@320: Chris@320: void Chris@127: View::modelReplaced() Chris@127: { Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@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@333: View::globalCentreFrameChanged(unsigned long rf) Chris@127: { Chris@211: if (m_followPan) { Chris@333: size_t f = alignFromReference(rf); Chris@333: std::cerr << "View[" << this << "]::globalCentreFrameChanged(" << rf Chris@333: << "): setting centre frame to " << f << std::endl; Chris@333: 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@301: f = getAlignedPlaybackFrame(); Chris@301: 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@315: View::ModelSet Chris@315: View::getModels() Chris@315: { Chris@315: ModelSet models; Chris@315: Chris@315: for (int i = 0; i < getLayerCount(); ++i) { Chris@315: Chris@315: Layer *layer = getLayer(i); Chris@315: Chris@315: if (dynamic_cast(layer)) { Chris@315: continue; Chris@315: } Chris@315: Chris@315: if (layer && layer->getModel()) { Chris@315: Model *model = layer->getModel(); Chris@315: models.insert(model); Chris@315: } Chris@315: } Chris@315: Chris@315: return models; Chris@315: } Chris@315: Chris@320: Model * Chris@320: View::getAligningModel() const Chris@301: { Chris@320: if (!m_manager || Chris@320: !m_manager->getAlignMode() || Chris@314: !m_manager->getPlaybackModel()) { Chris@320: return 0; Chris@314: } Chris@301: Chris@320: Model *anyModel = 0; Chris@320: Model *goodModel = 0; Chris@301: Chris@320: for (LayerList::const_iterator i = m_layers.begin(); Chris@320: i != m_layers.end(); ++i) { Chris@320: Chris@320: Layer *layer = *i; Chris@320: Chris@320: if (!layer) continue; Chris@320: if (dynamic_cast(layer)) continue; Chris@301: Chris@301: Model *model = (*i)->getModel(); Chris@301: if (!model) continue; Chris@301: Chris@320: if (model->getAlignmentReference()) { Chris@320: anyModel = model; Chris@320: if (layer->isLayerOpaque() || Chris@320: dynamic_cast(model)) { Chris@320: goodModel = model; Chris@320: } Chris@301: } Chris@320: } Chris@301: Chris@320: if (goodModel) return goodModel; Chris@320: else return anyModel; Chris@320: } Chris@320: Chris@320: size_t Chris@320: View::alignFromReference(size_t f) const Chris@320: { Chris@321: if (!m_manager->getAlignMode()) return f; Chris@320: Model *aligningModel = getAligningModel(); Chris@320: if (!aligningModel) return f; Chris@320: return aligningModel->alignFromReference(f); Chris@320: } Chris@320: Chris@320: size_t Chris@320: View::alignToReference(size_t f) const Chris@320: { Chris@321: if (!m_manager->getAlignMode()) return f; Chris@320: Model *aligningModel = getAligningModel(); Chris@320: if (!aligningModel) return f; Chris@320: return aligningModel->alignToReference(f); Chris@320: } Chris@320: Chris@320: int Chris@320: View::getAlignedPlaybackFrame() const Chris@320: { Chris@321: int pf = m_manager->getPlaybackFrame(); Chris@321: if (!m_manager->getAlignMode()) return pf; Chris@321: Chris@320: Model *aligningModel = getAligningModel(); Chris@320: if (!aligningModel) return pf; Chris@333: /* Chris@320: Model *pm = m_manager->getPlaybackModel(); Chris@301: Chris@301: // std::cerr << "View[" << this << "]::getAlignedPlaybackFrame: pf = " << pf; Chris@301: Chris@301: if (pm) { Chris@333: pf = pm->alignToReference(pf); Chris@301: // std::cerr << " -> " << pf; Chris@301: } Chris@333: */ Chris@333: int af = aligningModel->alignFromReference(pf); Chris@301: Chris@301: // std::cerr << ", aligned = " << af << std::endl; Chris@301: return af; Chris@301: } Chris@301: Chris@127: bool Chris@127: View::areLayersScrollable() const Chris@127: { Chris@127: // True iff all views are scrollable Chris@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@287: if ((*i)->getLayerColourSignificance() == Chris@287: Layer::ColourHasMeaningfulValue) return true; Chris@183: if ((*i)->isLayerOpaque()) break; Chris@183: } Chris@183: return false; Chris@183: } Chris@183: Chris@217: bool Chris@217: View::hasTopLayerTimeXAxis() const Chris@217: { Chris@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@301: QString text = i->first->getPropertyContainerName(); Chris@301: Chris@301: if (completion >= 100) { Chris@301: Chris@301: //!!! Chris@301: Model *model = i->first->getModel(); Chris@301: RangeSummarisableTimeValueModel *wfm = Chris@301: dynamic_cast(model); Chris@326: if (wfm || Chris@328: (wfm = dynamic_cast Chris@328: (model->getSourceModel()))) { Chris@301: completion = wfm->getAlignmentCompletion(); Chris@301: if (completion < 100) { Chris@301: text = tr("Alignment"); Chris@301: } Chris@301: } Chris@301: } Chris@127: Chris@127: if (completion >= 100) { Chris@127: Chris@127: i->second->hide(); Chris@127: Chris@127: } else { Chris@127: Chris@301: i->second->setText(text); 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@339: View::setPaintFont(QPainter &paint) Chris@339: { Chris@339: QFont font(paint.font()); Chris@339: font.setPointSize(Preferences::getInstance()->getViewFontSize()); Chris@339: paint.setFont(font); Chris@339: } Chris@339: Chris@339: 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@339: setPaintFont(paint); Chris@127: paint.setClipRect(cacheRect); Chris@287: Chris@287: paint.setPen(getBackground()); Chris@287: paint.setBrush(getBackground()); Chris@127: paint.drawRect(cacheRect); Chris@127: Chris@287: paint.setPen(getForeground()); 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@339: setPaintFont(paint); Chris@127: if (scrollables.empty()) { Chris@287: paint.setPen(getBackground()); Chris@287: paint.setBrush(getBackground()); Chris@127: paint.drawRect(nonCacheRect); Chris@127: } Chris@127: Chris@287: paint.setPen(getForeground()); 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@339: setPaintFont(paint); 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@287: paint.setPen(getForeground()); Chris@211: paint.drawLine(playx - 1, 0, playx - 1, height() - 1); Chris@211: paint.drawLine(playx + 1, 0, playx + 1, height() - 1); Chris@211: paint.drawPoint(playx, 0); Chris@211: paint.drawPoint(playx, height() - 1); Chris@287: paint.setPen(getBackground()); Chris@211: paint.drawLine(playx, 1, playx, height() - 2); Chris@127: Chris@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@333: int p0 = getXForFrame(alignFromReference(i->getStartFrame())); Chris@333: int p1 = getXForFrame(alignFromReference(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@287: paint.setPen(QPen(getForeground(), 2)); 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@287: paint.setPen(getBackground()); Chris@287: paint.setBrush(getBackground()); Chris@227: Chris@229: paint.drawRect(QRect(xorigin + x, 0, width(), height())); Chris@227: Chris@287: paint.setPen(getForeground()); 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@316: void Chris@316: View::toXml(QTextStream &stream, Chris@316: QString indent, QString extraAttributes) const Chris@127: { Chris@316: stream << indent; Chris@127: Chris@316: stream << QString("\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@316: m_layers[i]->toBriefXml(stream, indent + " ", Chris@316: QString("visible=\"%1\"") Chris@316: .arg(visible ? "true" : "false")); Chris@127: } Chris@127: Chris@316: stream << indent + "\n"; 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: