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@1357: #include "base/HitCount.h" Chris@919: #include "ViewProxy.h" Chris@127: Chris@301: #include "layer/TimeRulerLayer.h" Chris@287: #include "layer/SingleColourLayer.h" Chris@1078: #include "layer/PaintAssistant.h" Chris@1078: Chris@1354: #include "data/model/RelativelyFineZoomConstraint.h" Chris@301: #include "data/model/RangeSummarisableTimeValueModel.h" Chris@127: Chris@797: #include "widgets/IconLoader.h" Chris@797: Chris@127: #include Chris@127: #include Chris@127: #include Chris@127: #include Chris@226: #include Chris@316: #include Chris@338: #include Chris@583: #include Chris@797: #include Chris@956: #include Chris@1202: #include Chris@127: Chris@127: #include Chris@127: #include Chris@520: #include Chris@127: Chris@1352: //#define DEBUG_VIEW 1 Chris@1003: //#define DEBUG_VIEW_WIDGET_PAINT 1 Chris@1448: //#define DEBUG_PROGRESS_STUFF 1 Chris@127: Chris@127: View::View(QWidget *w, bool showProgress) : Chris@127: QFrame(w), Chris@1044: m_id(getNextId()), Chris@127: m_centreFrame(0), Chris@1326: m_zoomLevel(ZoomLevel::FramesPerPixel, 1024), Chris@127: m_followPan(true), Chris@127: m_followZoom(true), Chris@815: m_followPlay(PlaybackScrollPageWithCentre), Chris@789: m_followPlayIsDetached(false), Chris@153: m_playPointerFrame(0), Chris@127: m_showProgress(showProgress), Chris@1408: m_cache(nullptr), Chris@1408: m_buffer(nullptr), Chris@1357: m_cacheValid(false), Chris@127: m_cacheCentreFrame(0), Chris@1326: m_cacheZoomLevel(ZoomLevel::FramesPerPixel, 1024), Chris@127: m_selectionCached(false), Chris@127: m_deleting(false), Chris@127: m_haveSelectedLayer(false), Chris@1490: m_useAligningProxy(false), Chris@1496: m_alignmentProgressBar({ {}, nullptr }), Chris@1408: m_manager(nullptr), Chris@127: m_propertyContainer(new ViewPropertyContainer(this)) Chris@127: { Chris@1506: // SVCERR << "View::View[" << getId() << "]" << endl; Chris@127: } Chris@127: Chris@127: View::~View() Chris@127: { Chris@1506: // SVCERR << "View::~View[" << getId() << "]" << endl; Chris@127: Chris@127: m_deleting = true; Chris@127: delete m_propertyContainer; Chris@1215: delete m_cache; Chris@1215: delete m_buffer; 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@1266: 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@1266: if (min) *min = 0; Chris@1266: if (max) *max = 2; Chris@815: if (deflt) *deflt = int(PlaybackScrollPageWithCentre); Chris@815: switch (m_followPlay) { Chris@815: case PlaybackScrollContinuous: return 0; Chris@815: case PlaybackScrollPageWithCentre: case PlaybackScrollPage: return 1; Chris@815: case PlaybackIgnore: return 2; Chris@815: } 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@1266: int value) const Chris@127: { Chris@127: if (name == "Follow Playback") { Chris@1266: switch (value) { Chris@1266: default: Chris@1266: case 0: return tr("Scroll"); Chris@1266: case 1: return tr("Page"); Chris@1266: case 2: return tr("Off"); Chris@1266: } 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@1266: setFollowGlobalPan(value != 0); Chris@127: } else if (name == "Global Zoom") { Chris@1266: setFollowGlobalZoom(value != 0); Chris@127: } else if (name == "Follow Playback") { Chris@1266: switch (value) { Chris@1266: default: Chris@1266: case 0: setPlaybackFollow(PlaybackScrollContinuous); break; Chris@1266: case 1: setPlaybackFollow(PlaybackScrollPageWithCentre); break; Chris@1266: case 2: setPlaybackFollow(PlaybackIgnore); break; Chris@1266: } Chris@127: } Chris@127: } Chris@127: Chris@806: int Chris@127: View::getPropertyContainerCount() const Chris@127: { Chris@908: return int(m_fixedOrderLayers.size()) + 1; // the 1 is for me Chris@127: } Chris@127: Chris@127: const PropertyContainer * Chris@806: View::getPropertyContainer(int i) const Chris@127: { Chris@127: return (const PropertyContainer *)(((View *)this)-> Chris@1266: getPropertyContainer(i)); Chris@127: } Chris@127: Chris@127: PropertyContainer * Chris@806: View::getPropertyContainer(int i) Chris@127: { Chris@127: if (i == 0) return m_propertyContainer; Chris@837: return m_fixedOrderLayers[i-1]; Chris@127: } Chris@127: Chris@127: bool Chris@1537: View::getVisibleExtentsForUnit(QString unit, Chris@1537: double &min, double &max, Chris@1537: bool &log) const Chris@127: { Chris@1539: Layer *layer = getScaleProvidingLayerForUnit(unit); Chris@1539: if (!layer) { Chris@1539: return false; Chris@1539: } Chris@1539: Chris@1539: //!!! clumsy Chris@1539: Chris@1539: QString layerUnit; Chris@1539: double layerMin, layerMax; Chris@1539: Chris@1539: if (layer->getValueExtents(layerMin, layerMax, log, layerUnit)) { Chris@1539: if (layer->getDisplayExtents(min, max)) { Chris@1539: return true; Chris@1539: } else { Chris@1539: min = layerMin; Chris@1539: max = layerMax; Chris@1539: return true; Chris@1539: } Chris@1539: } else { Chris@1539: return false; Chris@1539: } Chris@1539: } Chris@1539: Chris@1539: Layer * Chris@1539: View::getScaleProvidingLayerForUnit(QString unit) const Chris@1539: { Chris@1539: // Iterate in reverse order, so as to use topmost layer that fits Chris@1539: // the bill Chris@1537: Chris@1537: for (auto i = m_layerStack.rbegin(); i != m_layerStack.rend(); ++i) { Chris@1537: Chris@1537: Layer *layer = *i; Chris@1537: Chris@1537: if (layer->isLayerDormant(this)) { Chris@1537: continue; Chris@1537: } Chris@1537: Chris@127: QString layerUnit; Chris@904: double layerMin = 0.0, layerMax = 0.0; Chris@1537: bool layerLog = false; Chris@1537: Chris@1537: if (!layer->getValueExtents(layerMin, layerMax, layerLog, layerUnit)) { Chris@1537: continue; Chris@1537: } Chris@1537: if (layerUnit.toLower() != unit.toLower()) { Chris@1537: continue; Chris@1537: } Chris@1537: Chris@1539: return layer; Chris@127: } Chris@127: Chris@1539: return nullptr; Chris@127: } Chris@127: Chris@1537: bool Chris@1537: View::getVisibleExtentsForAnyUnit(double &min, double &max, Chris@1537: bool &log, QString &unit) const Chris@1537: { Chris@1537: bool have = false; Chris@1537: Chris@1537: // Iterate in reverse order, so as to return display extents of Chris@1537: // topmost layer that fits the bill Chris@1537: Chris@1537: for (auto i = m_layerStack.rbegin(); i != m_layerStack.rend(); ++i) { Chris@1537: Chris@1537: Layer *layer = *i; Chris@1537: Chris@1537: if (layer->isLayerDormant(this)) { Chris@1537: continue; Chris@1537: } Chris@1537: Chris@1537: QString layerUnit; Chris@1537: double layerMin = 0.0, layerMax = 0.0; Chris@1537: bool layerLog = false; Chris@1537: Chris@1537: if (!layer->getValueExtents(layerMin, layerMax, layerLog, layerUnit)) { Chris@1537: continue; Chris@1537: } Chris@1537: if (layerUnit == "") { Chris@1537: continue; Chris@1537: } Chris@1537: Chris@1537: double displayMin = 0.0, displayMax = 0.0; Chris@1537: Chris@1537: if (layer->getDisplayExtents(displayMin, displayMax)) { Chris@1537: Chris@1537: min = displayMin; Chris@1537: max = displayMax; Chris@1537: log = layerLog; Chris@1537: unit = layerUnit; Chris@1537: have = true; Chris@1537: break; Chris@1537: } Chris@1537: } Chris@1537: Chris@1537: return have; Chris@1537: } Chris@1537: Chris@127: int Chris@1537: View::getTextLabelYCoord(const Layer *layer, QPainter &paint) const Chris@127: { Chris@127: std::map sortedLayers; Chris@127: Chris@835: for (LayerList::const_iterator i = m_layerStack.begin(); Chris@835: i != m_layerStack.end(); ++i) { Chris@127: if ((*i)->needsTextLabelHeight()) { Chris@1439: sortedLayers[(*i)->getExportId()] = *i; Chris@127: } Chris@127: } Chris@127: Chris@1402: int y = scalePixelSize(15) + paint.fontMetrics().ascent(); Chris@127: Chris@127: for (std::map::const_iterator i = sortedLayers.begin(); Chris@127: i != sortedLayers.end(); ++i) { Chris@1273: if (i->second == layer) break; 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@1266: if (m_haveSelectedLayer) { Chris@1266: m_haveSelectedLayer = false; Chris@1266: update(); Chris@1266: } Chris@1266: return; Chris@127: } Chris@127: Chris@1357: m_cacheValid = false; Chris@127: Chris@1408: Layer *selectedLayer = nullptr; Chris@127: Chris@835: for (LayerList::iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) { Chris@1266: if (*i == pc) { Chris@1266: selectedLayer = *i; Chris@1266: m_layerStack.erase(i); Chris@1266: break; Chris@1266: } Chris@127: } Chris@127: Chris@127: if (selectedLayer) { Chris@1266: m_haveSelectedLayer = true; Chris@1266: m_layerStack.push_back(selectedLayer); Chris@1266: update(); Chris@127: } else { Chris@1266: 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@587: // SVDEBUG << "View::toolModeChanged(" << m_manager->getToolMode() << ")" << endl; Chris@127: } Chris@127: Chris@133: void Chris@133: View::overlayModeChanged() Chris@133: { Chris@1357: m_cacheValid = false; 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@908: sv_frame_t Chris@127: View::getStartFrame() const Chris@127: { Chris@313: return getFrameForX(0); Chris@127: } Chris@127: Chris@908: sv_frame_t Chris@127: View::getEndFrame() const Chris@127: { Chris@127: return getFrameForX(width()) - 1; Chris@127: } Chris@127: Chris@127: void Chris@908: View::setStartFrame(sv_frame_t f) Chris@127: { Chris@1326: setCentreFrame(f + sv_frame_t(round Chris@1326: (m_zoomLevel.pixelsToFrames(width() / 2)))); Chris@127: } Chris@127: Chris@127: bool Chris@908: View::setCentreFrame(sv_frame_t f, bool e) Chris@127: { Chris@127: bool changeVisible = false; Chris@127: Chris@1329: #ifdef DEBUG_VIEW Chris@1506: SVCERR << "View[" << getId() << "]::setCentreFrame: from " << m_centreFrame Chris@1329: << " to " << f << endl; Chris@1329: #endif Chris@1329: Chris@127: if (m_centreFrame != f) { Chris@127: Chris@1327: sv_frame_t formerCentre = m_centreFrame; Chris@1266: m_centreFrame = f; Chris@1266: Chris@1327: if (m_zoomLevel.zone == ZoomLevel::PixelsPerFrame) { Chris@127: Chris@1363: #ifdef DEBUG_VIEW Chris@1506: SVCERR << "View[" << getId() << "]::setCentreFrame: in PixelsPerFrame zone, so change must be visible" << endl; Chris@127: #endif Chris@1266: update(); Chris@1266: changeVisible = true; Chris@1327: Chris@1327: } else { Chris@1327: Chris@1327: int formerPixel = int(formerCentre / m_zoomLevel.level); Chris@1327: int newPixel = int(m_centreFrame / m_zoomLevel.level); Chris@1327: Chris@1327: if (newPixel != formerPixel) { Chris@1327: Chris@1363: #ifdef DEBUG_VIEW Chris@1506: SVCERR << "View[" << getId() << "]::setCentreFrame: newPixel " << newPixel << ", formerPixel " << formerPixel << endl; Chris@1327: #endif Chris@1329: // ensure the centre frame is a multiple of the zoom level Chris@1329: m_centreFrame = sv_frame_t(newPixel) * m_zoomLevel.level; Chris@1363: Chris@1363: #ifdef DEBUG_VIEW Chris@1506: SVCERR << "View[" << getId() Chris@1506: << "]::setCentreFrame: centre frame rounded to " Chris@1363: << m_centreFrame << " (zoom level is " Chris@1363: << m_zoomLevel.level << ")" << endl; Chris@1363: #endif Chris@1329: Chris@1327: update(); Chris@1327: changeVisible = true; Chris@1327: } Chris@1266: } Chris@1266: Chris@1266: if (e) { Chris@1363: sv_frame_t rf = alignToReference(m_centreFrame); Chris@830: #ifdef DEBUG_VIEW Chris@1506: SVCERR << "View[" << getId() << "]::setCentreFrame(" << f Chris@1363: << "): m_centreFrame = " << m_centreFrame Chris@1363: << ", emitting centreFrameChanged with aligned frame " Chris@1363: << rf << 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@908: View::getXForFrame(sv_frame_t frame) const Chris@127: { Chris@1341: // In FramesPerPixel mode, the pixel should be the one "covering" Chris@1341: // the given frame, i.e. to the "left" of it - not necessarily the Chris@1341: // nearest boundary. Chris@1341: Chris@1341: sv_frame_t level = m_zoomLevel.level; Chris@1507: sv_frame_t fdiff = frame - m_centreFrame; Chris@1375: int result = 0; Chris@1375: Chris@1375: bool inRange = false; Chris@1327: if (m_zoomLevel.zone == ZoomLevel::FramesPerPixel) { Chris@1375: inRange = ((fdiff / level) < sv_frame_t(INT_MAX) && Chris@1375: (fdiff / level) > sv_frame_t(INT_MIN)); Chris@1375: } else { Chris@1375: inRange = (fdiff < sv_frame_t(INT_MAX) / level && Chris@1375: fdiff > sv_frame_t(INT_MIN) / level); Chris@1375: } Chris@1375: Chris@1375: if (inRange) { Chris@1375: Chris@1375: sv_frame_t adjusted; Chris@1375: Chris@1375: if (m_zoomLevel.zone == ZoomLevel::FramesPerPixel) { Chris@1507: sv_frame_t roundedCentreFrame = (m_centreFrame / level) * level; Chris@1507: fdiff = frame - roundedCentreFrame; Chris@1375: adjusted = fdiff / level; Chris@1375: if ((fdiff < 0) && ((fdiff % level) != 0)) { Chris@1375: --adjusted; // round to the left Chris@1375: } Chris@1375: } else { Chris@1375: adjusted = fdiff * level; Chris@1341: } Chris@1375: Chris@1375: adjusted = adjusted + (width()/2); Chris@1375: Chris@1375: if (adjusted > INT_MAX || adjusted < INT_MIN) { Chris@1375: inRange = false; Chris@1375: } else { Chris@1375: result = int(adjusted); Chris@1375: } Chris@1327: } Chris@1327: Chris@1375: if (!inRange) { Chris@1375: SVCERR << "ERROR: Frame " << frame Chris@1375: << " is out of range in View::getXForFrame" << endl; Chris@1375: SVCERR << "ERROR: (centre frame = " << getCentreFrame() << ", fdiff = " Chris@1375: << fdiff << ", zoom level = " << m_zoomLevel << ")" << endl; Chris@1375: SVCERR << "ERROR: This is a logic error: getXForFrame should not be " Chris@1375: << "called for locations unadjacent to the current view" Chris@1375: << endl; Chris@1375: return 0; Chris@1375: } Chris@1375: Chris@1507: #ifdef DEBUG_VIEW Chris@1507: if (m_zoomLevel.zone == ZoomLevel::PixelsPerFrame) { Chris@1507: sv_frame_t reversed = getFrameForX(result); Chris@1507: if (reversed != frame) { Chris@1507: SVCERR << "View[" << getId() << "]::getXForFrame: WARNING: Converted frame " << frame << " to x " << result << " in PixelsPerFrame zone, but the reverse conversion gives frame " << reversed << " (error = " << reversed - frame << ")" << endl; Chris@1507: SVCERR << "(centre frame = " << getCentreFrame() << ", fdiff = " Chris@1507: << fdiff << ", level = " << level << ", centre % level = " Chris@1507: << (getCentreFrame() % level) << ", fdiff % level = " Chris@1507: << (fdiff % level) << ", frame % level = " Chris@1507: << (frame % level) << ", reversed % level = " Chris@1507: << (reversed % level) << ")" << endl; Chris@1507: } Chris@1507: } Chris@1507: #endif Chris@1507: Chris@1341: return result; Chris@127: } Chris@127: Chris@908: sv_frame_t Chris@127: View::getFrameForX(int x) const Chris@127: { Chris@1330: // Note, this must always return a value that is on a zoom-level Chris@1330: // boundary - regardless of whether the nominal centre frame is on Chris@1507: // such a boundary or not. (It is legitimate for the centre frame Chris@1507: // not to be on a zoom-level boundary, because the centre frame Chris@1507: // may be shared with other views having different zoom levels.) Chris@1507: Chris@1507: // In FramesPerPixel mode, the frame returned for a given x should Chris@1507: // be the first for which getXForFrame(frame) == x; a corollary is Chris@1507: // that if the centre frame is not on a zoom-level boundary, then Chris@1507: // getFrameForX(x) should not return the centre frame for any x. Chris@1341: Chris@1507: // In PixelsPerFrame mode, the frame returned should be the one Chris@1507: // immediately left of the given pixel, not necessarily the Chris@1507: // nearest. Chris@1330: Chris@1327: int diff = x - (width()/2); Chris@1330: sv_frame_t level = m_zoomLevel.level; Chris@1330: sv_frame_t fdiff, result; Chris@1330: Chris@1327: if (m_zoomLevel.zone == ZoomLevel::FramesPerPixel) { Chris@1507: sv_frame_t roundedCentreFrame = (m_centreFrame / level) * level; Chris@1330: fdiff = diff * level; Chris@1507: result = fdiff + roundedCentreFrame; Chris@1327: } else { Chris@1330: fdiff = diff / level; Chris@1341: if ((diff < 0) && ((diff % level) != 0)) { Chris@1341: --fdiff; // round to the left Chris@1341: } Chris@1330: result = fdiff + m_centreFrame; Chris@1327: } Chris@1341: Chris@1363: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1507: /* Chris@1330: if (x == 0) { Chris@1330: SVCERR << "getFrameForX(" << x << "): diff = " << diff << ", fdiff = " Chris@1330: << fdiff << ", m_centreFrame = " << m_centreFrame Chris@1330: << ", level = " << m_zoomLevel.level Chris@1330: << ", diff % level = " << (diff % m_zoomLevel.level) Chris@1330: << ", nominal " << fdiff + m_centreFrame Chris@1330: << ", will return " << result Chris@1330: << endl; Chris@1330: } Chris@1507: */ Chris@1507: #endif Chris@1507: Chris@1507: #ifdef DEBUG_VIEW Chris@1507: if (m_zoomLevel.zone == ZoomLevel::FramesPerPixel) { Chris@1507: int reversed = getXForFrame(result); Chris@1507: if (reversed != x) { Chris@1507: SVCERR << "View[" << getId() << "]::getFrameForX: WARNING: Converted pixel " << x << " to frame " << result << " in FramesPerPixel zone, but the reverse conversion gives pixel " << reversed << " (error = " << reversed - x << ")" << endl; Chris@1507: SVCERR << "(centre frame = " << getCentreFrame() Chris@1507: << ", width/2 = " << width()/2 << ", diff = " << diff Chris@1507: << ", fdiff = " << fdiff << ", level = " << level Chris@1507: << ", centre % level = " << (getCentreFrame() % level) Chris@1507: << ", fdiff % level = " << (fdiff % level) Chris@1507: << ", frame % level = " << (result % level) << ")" << endl; Chris@1507: } Chris@1507: } Chris@1352: #endif Chris@1352: Chris@1330: return result; Chris@127: } Chris@127: Chris@904: double Chris@904: View::getYForFrequency(double frequency, Chris@1266: double minf, Chris@1266: double maxf, Chris@1266: bool logarithmic) const Chris@127: { Chris@382: Profiler profiler("View::getYForFrequency"); Chris@382: Chris@127: int h = height(); Chris@127: Chris@127: if (logarithmic) { Chris@127: Chris@1266: static double lastminf = 0.0, lastmaxf = 0.0; Chris@1266: static double logminf = 0.0, logmaxf = 0.0; Chris@1266: Chris@1266: if (lastminf != minf) { Chris@1266: lastminf = (minf == 0.0 ? 1.0 : minf); Chris@1266: logminf = log10(minf); Chris@1266: } Chris@1266: if (lastmaxf != maxf) { Chris@1266: lastmaxf = (maxf < lastminf ? lastminf : maxf); Chris@1266: logmaxf = log10(maxf); Chris@1266: } Chris@1266: Chris@1266: if (logminf == logmaxf) return 0; Chris@1266: return h - (h * (log10(frequency) - logminf)) / (logmaxf - logminf); Chris@127: Chris@127: } else { Chris@1266: Chris@1266: if (minf == maxf) return 0; Chris@1266: return h - (h * (frequency - minf)) / (maxf - minf); Chris@127: } Chris@127: } Chris@127: Chris@904: double Chris@1085: View::getFrequencyForY(double y, Chris@1266: double minf, Chris@1266: double maxf, Chris@1266: bool logarithmic) const Chris@127: { Chris@1085: double h = height(); Chris@127: Chris@127: if (logarithmic) { Chris@127: Chris@1266: static double lastminf = 0.0, lastmaxf = 0.0; Chris@1266: static double logminf = 0.0, logmaxf = 0.0; Chris@1266: Chris@1266: if (lastminf != minf) { Chris@1266: lastminf = (minf == 0.0 ? 1.0 : minf); Chris@1266: logminf = log10(minf); Chris@1266: } Chris@1266: if (lastmaxf != maxf) { Chris@1266: lastmaxf = (maxf < lastminf ? lastminf : maxf); Chris@1266: logmaxf = log10(maxf); Chris@1266: } Chris@1266: Chris@1266: if (logminf == logmaxf) return 0; Chris@1266: return pow(10.0, logminf + ((logmaxf - logminf) * (h - y)) / h); Chris@127: Chris@127: } else { Chris@127: Chris@1266: if (minf == maxf) return 0; Chris@1266: return minf + ((h - y) * (maxf - minf)) / h; Chris@127: } Chris@127: } Chris@127: Chris@1183: ZoomLevel Chris@127: View::getZoomLevel() const Chris@127: { Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1266: // cout << "zoom level: " << m_zoomLevel << endl; Chris@127: #endif Chris@127: return m_zoomLevel; Chris@127: } Chris@127: Chris@956: int Chris@956: View::effectiveDevicePixelRatio() const Chris@956: { Chris@956: #ifdef Q_OS_MAC Chris@956: int dpratio = devicePixelRatio(); Chris@956: if (dpratio > 1) { Chris@956: QSettings settings; Chris@956: settings.beginGroup("Preferences"); Chris@956: if (!settings.value("scaledHiDpi", true).toBool()) { Chris@956: dpratio = 1; Chris@956: } Chris@956: settings.endGroup(); Chris@956: } Chris@956: return dpratio; Chris@956: #else Chris@956: return 1; Chris@956: #endif Chris@956: } Chris@956: Chris@127: void Chris@1183: View::setZoomLevel(ZoomLevel z) Chris@127: { Chris@1327: //!!! int dpratio = effectiveDevicePixelRatio(); Chris@1327: // if (z < dpratio) return; Chris@1327: // if (z < 1) z = 1; Chris@1327: if (m_zoomLevel == z) { Chris@1327: return; Chris@127: } Chris@1327: m_zoomLevel = z; Chris@1327: emit zoomLevelChanged(z, m_followZoom); Chris@1327: update(); 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@835: for (LayerList::const_iterator i = m_layerStack.begin(); Chris@835: i != m_layerStack.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@1314: if (int(maxSignificance) >= int(Layer::ColourDistinguishes)) { 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@1475: return widgetbg.lighter(); Chris@296: } else { Chris@1475: return widgetbg.darker(); 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: void Chris@127: View::addLayer(Layer *layer) Chris@127: { Chris@1357: m_cacheValid = false; Chris@127: Chris@287: SingleColourLayer *scl = dynamic_cast(layer); Chris@287: if (scl) scl->setDefaultColourFor(this); Chris@287: Chris@836: m_fixedOrderLayers.push_back(layer); Chris@835: m_layerStack.push_back(layer); Chris@127: Chris@555: QProgressBar *pb = new QProgressBar(this); Chris@555: pb->setMinimum(0); Chris@555: pb->setMaximum(0); Chris@555: pb->setFixedWidth(80); Chris@555: pb->setTextVisible(false); Chris@555: Chris@797: QPushButton *cancel = new QPushButton(this); Chris@1350: cancel->setIcon(IconLoader().load("cancel")); Chris@797: cancel->setFlat(true); Chris@1402: int scaled20 = scalePixelSize(20); Chris@1351: cancel->setFixedSize(QSize(scaled20, scaled20)); Chris@797: connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked())); Chris@797: Chris@555: ProgressBarRec pbr; Chris@797: pbr.cancel = cancel; Chris@555: pbr.bar = pb; Chris@1496: pbr.lastStallCheckValue = 0; Chris@1496: pbr.stallCheckTimer = new QTimer(); Chris@1496: connect(pbr.stallCheckTimer, SIGNAL(timeout()), this, Chris@555: SLOT(progressCheckStalledTimerElapsed())); Chris@555: Chris@555: m_progressBars[layer] = pbr; Chris@555: Chris@555: QFont f(pb->font()); Chris@339: int fs = Preferences::getInstance()->getViewFontSize(); Chris@339: f.setPointSize(std::min(fs, int(ceil(fs * 0.85)))); Chris@339: Chris@797: cancel->hide(); Chris@797: Chris@555: pb->setFont(f); Chris@555: pb->hide(); Chris@127: Chris@127: connect(layer, SIGNAL(layerParametersChanged()), Chris@1266: this, SLOT(layerParametersChanged())); Chris@197: connect(layer, SIGNAL(layerParameterRangesChanged()), Chris@1266: this, SLOT(layerParameterRangesChanged())); Chris@268: connect(layer, SIGNAL(layerMeasurementRectsChanged()), Chris@1266: this, SLOT(layerMeasurementRectsChanged())); Chris@127: connect(layer, SIGNAL(layerNameChanged()), Chris@1266: this, SLOT(layerNameChanged())); Chris@1481: connect(layer, SIGNAL(modelChanged(ModelId)), Chris@1481: this, SLOT(modelChanged(ModelId))); Chris@1481: connect(layer, SIGNAL(modelCompletionChanged(ModelId)), Chris@1481: this, SLOT(modelCompletionChanged(ModelId))); Chris@1481: connect(layer, SIGNAL(modelAlignmentCompletionChanged(ModelId)), Chris@1481: this, SLOT(modelAlignmentCompletionChanged(ModelId))); Chris@1481: connect(layer, SIGNAL(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)), Chris@1481: this, SLOT(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t))); Chris@127: connect(layer, SIGNAL(modelReplaced()), Chris@1266: 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@1266: return; Chris@127: } Chris@127: Chris@1357: m_cacheValid = false; Chris@127: Chris@836: for (LayerList::iterator i = m_fixedOrderLayers.begin(); Chris@836: i != m_fixedOrderLayers.end(); Chris@836: ++i) { Chris@1266: if (*i == layer) { Chris@1266: m_fixedOrderLayers.erase(i); Chris@836: break; Chris@836: } Chris@836: } Chris@836: Chris@836: for (LayerList::iterator i = m_layerStack.begin(); Chris@836: i != m_layerStack.end(); Chris@836: ++i) { Chris@1266: if (*i == layer) { Chris@1266: m_layerStack.erase(i); Chris@1266: if (m_progressBars.find(layer) != m_progressBars.end()) { Chris@1266: delete m_progressBars[layer].bar; Chris@797: delete m_progressBars[layer].cancel; Chris@1496: delete m_progressBars[layer].stallCheckTimer; Chris@1266: m_progressBars.erase(layer); Chris@1266: } Chris@1266: break; Chris@1266: } 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@1481: disconnect(layer, SIGNAL(modelChanged(ModelId)), Chris@1481: this, SLOT(modelChanged(ModelId))); Chris@1481: disconnect(layer, SIGNAL(modelCompletionChanged(ModelId)), Chris@1481: this, SLOT(modelCompletionChanged(ModelId))); Chris@1481: disconnect(layer, SIGNAL(modelAlignmentCompletionChanged(ModelId)), Chris@1481: this, SLOT(modelAlignmentCompletionChanged(ModelId))); Chris@1481: disconnect(layer, SIGNAL(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)), Chris@1481: this, SLOT(modelChangedWithin(ModelId, sv_frame_t, sv_frame_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@834: View::getInteractionLayer() Chris@834: { Chris@834: Layer *sl = getSelectedLayer(); Chris@834: if (sl && !(sl->isLayerDormant(this))) { Chris@834: return sl; Chris@834: } Chris@835: if (!m_layerStack.empty()) { Chris@834: int n = getLayerCount(); Chris@834: while (n > 0) { Chris@834: --n; Chris@834: Layer *layer = getLayer(n); Chris@834: if (!(layer->isLayerDormant(this))) { Chris@834: return layer; Chris@834: } Chris@834: } Chris@834: } Chris@1408: return nullptr; Chris@834: } Chris@834: Chris@841: const Layer * Chris@841: View::getInteractionLayer() const Chris@841: { Chris@841: return const_cast(const_cast(this)->getInteractionLayer()); Chris@841: } Chris@841: Chris@834: Layer * Chris@127: View::getSelectedLayer() Chris@127: { Chris@835: if (m_haveSelectedLayer && !m_layerStack.empty()) { Chris@839: return getLayer(getLayerCount() - 1); Chris@127: } else { Chris@1408: return nullptr; 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@1266: m_manager->disconnect(this, SLOT(globalCentreFrameChanged(sv_frame_t))); Chris@1266: m_manager->disconnect(this, SLOT(viewCentreFrameChanged(View *, sv_frame_t))); Chris@1266: m_manager->disconnect(this, SLOT(viewManagerPlaybackFrameChanged(sv_frame_t))); Chris@1324: m_manager->disconnect(this, SLOT(viewZoomLevelChanged(View *, ZoomLevel, 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@908: disconnect(m_manager, SLOT(viewCentreFrameChanged(sv_frame_t, bool, PlaybackFollowMode))); Chris@1324: disconnect(m_manager, SLOT(zoomLevelChanged(ZoomLevel, bool))); Chris@127: } Chris@127: Chris@127: m_manager = manager; Chris@127: Chris@908: connect(m_manager, SIGNAL(globalCentreFrameChanged(sv_frame_t)), Chris@1266: this, SLOT(globalCentreFrameChanged(sv_frame_t))); Chris@908: connect(m_manager, SIGNAL(viewCentreFrameChanged(View *, sv_frame_t)), Chris@1266: this, SLOT(viewCentreFrameChanged(View *, sv_frame_t))); Chris@908: connect(m_manager, SIGNAL(playbackFrameChanged(sv_frame_t)), Chris@1266: this, SLOT(viewManagerPlaybackFrameChanged(sv_frame_t))); Chris@806: Chris@1183: connect(m_manager, SIGNAL(viewZoomLevelChanged(View *, ZoomLevel, bool)), Chris@1324: this, SLOT(viewZoomLevelChanged(View *, ZoomLevel, bool))); Chris@211: Chris@127: connect(m_manager, SIGNAL(toolModeChanged()), Chris@1266: this, SLOT(toolModeChanged())); Chris@127: connect(m_manager, SIGNAL(selectionChanged()), Chris@1266: this, SLOT(selectionChanged())); Chris@127: connect(m_manager, SIGNAL(inProgressSelectionChanged()), Chris@1266: this, SLOT(selectionChanged())); Chris@127: connect(m_manager, SIGNAL(overlayModeChanged()), Chris@133: this, SLOT(overlayModeChanged())); Chris@607: connect(m_manager, SIGNAL(showCentreLineChanged()), Chris@607: this, SLOT(overlayModeChanged())); Chris@133: connect(m_manager, SIGNAL(zoomWheelsEnabledChanged()), Chris@133: this, SLOT(zoomWheelsEnabledChanged())); Chris@127: Chris@908: connect(this, SIGNAL(centreFrameChanged(sv_frame_t, bool, Chris@211: PlaybackFollowMode)), Chris@908: m_manager, SLOT(viewCentreFrameChanged(sv_frame_t, bool, Chris@211: PlaybackFollowMode))); Chris@211: Chris@1183: connect(this, SIGNAL(zoomLevelChanged(ZoomLevel, bool)), Chris@1324: m_manager, SLOT(viewZoomLevelChanged(ZoomLevel, bool))); Chris@127: Chris@815: switch (m_followPlay) { Chris@815: Chris@815: case PlaybackScrollPage: Chris@815: case PlaybackScrollPageWithCentre: Chris@364: setCentreFrame(m_manager->getGlobalCentreFrame(), false); Chris@815: break; Chris@815: Chris@815: case PlaybackScrollContinuous: Chris@236: setCentreFrame(m_manager->getPlaybackFrame(), false); Chris@815: break; Chris@815: Chris@815: case PlaybackIgnore: Chris@815: if (m_followPan) { Chris@815: setCentreFrame(m_manager->getGlobalCentreFrame(), false); Chris@815: } Chris@815: break; Chris@236: } Chris@516: Chris@236: if (m_followZoom) setZoomLevel(m_manager->getGlobalZoom()); Chris@236: Chris@511: movePlayPointer(getAlignedPlaybackFrame()); Chris@511: Chris@127: toolModeChanged(); Chris@127: } Chris@127: Chris@127: void Chris@908: View::setViewManager(ViewManager *vm, sv_frame_t initialCentreFrame) Chris@516: { Chris@516: setViewManager(vm); Chris@516: setCentreFrame(initialCentreFrame, false); Chris@516: } Chris@516: Chris@516: void Chris@127: View::setFollowGlobalPan(bool f) Chris@127: { Chris@127: m_followPan = f; Chris@127: emit propertyContainerPropertyChanged(m_propertyContainer); Chris@127: } Chris@127: Chris@127: void Chris@127: View::setFollowGlobalZoom(bool f) Chris@127: { Chris@127: m_followZoom = f; Chris@127: emit propertyContainerPropertyChanged(m_propertyContainer); Chris@127: } Chris@127: Chris@127: void Chris@127: View::setPlaybackFollow(PlaybackFollowMode m) Chris@127: { Chris@127: m_followPlay = m; Chris@127: emit propertyContainerPropertyChanged(m_propertyContainer); Chris@127: } Chris@127: Chris@127: void Chris@1481: View::modelChanged(ModelId modelId) Chris@127: { Chris@1495: #if defined(DEBUG_VIEW_WIDGET_PAINT) || defined(DEBUG_PROGRESS_STUFF) Chris@1506: SVCERR << "View[" << getId() << "]::modelChanged(" << modelId << ")" << endl; Chris@127: #endif Chris@1475: 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@1266: i != scrollables.end(); ++i) { Chris@1481: if ((*i)->getModel() == modelId) { Chris@1266: recreate = true; Chris@1266: break; Chris@1266: } Chris@127: } Chris@127: Chris@127: if (recreate) { Chris@1357: m_cacheValid = false; Chris@127: } Chris@127: Chris@336: emit layerModelChanged(); Chris@336: Chris@1481: checkProgress(modelId); Chris@127: Chris@127: update(); Chris@127: } Chris@127: Chris@127: void Chris@1481: View::modelChangedWithin(ModelId modelId, Chris@1481: sv_frame_t startFrame, sv_frame_t endFrame) Chris@127: { Chris@908: sv_frame_t myStartFrame = getStartFrame(); Chris@908: sv_frame_t myEndFrame = getEndFrame(); Chris@127: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "View[" << getId() << "]::modelChangedWithin(" << startFrame << "," << endFrame << ") [me " << myStartFrame << "," << myEndFrame << "]" << endl; Chris@127: #endif Chris@127: Chris@908: if (myStartFrame > 0 && endFrame < myStartFrame) { Chris@1481: checkProgress(modelId); Chris@1266: return; Chris@127: } Chris@127: if (startFrame > myEndFrame) { Chris@1481: checkProgress(modelId); Chris@1266: 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@1266: i != scrollables.end(); ++i) { Chris@1481: if ((*i)->getModel() == modelId) { Chris@1266: recreate = true; Chris@1266: break; Chris@1266: } Chris@127: } Chris@127: Chris@127: if (recreate) { Chris@1357: m_cacheValid = false; Chris@127: } Chris@127: Chris@806: if (startFrame < myStartFrame) startFrame = myStartFrame; Chris@127: if (endFrame > myEndFrame) endFrame = myEndFrame; Chris@127: Chris@1481: checkProgress(modelId); Chris@127: Chris@127: update(); Chris@127: } Chris@127: Chris@127: void Chris@1481: View::modelCompletionChanged(ModelId modelId) Chris@127: { Chris@1495: #ifdef DEBUG_PROGRESS_STUFF Chris@1506: SVCERR << "View[" << getId() << "]::modelCompletionChanged(" << modelId << ")" << endl; Chris@1495: #endif Chris@1481: checkProgress(modelId); Chris@127: } Chris@127: Chris@127: void Chris@1481: View::modelAlignmentCompletionChanged(ModelId modelId) Chris@320: { Chris@1495: #ifdef DEBUG_PROGRESS_STUFF Chris@1506: SVCERR << "View[" << getId() << "]::modelAlignmentCompletionChanged(" << modelId << ")" << endl; Chris@1495: #endif Chris@1496: checkAlignmentProgress(modelId); Chris@320: } Chris@320: Chris@320: void Chris@127: View::modelReplaced() Chris@127: { Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "View[" << getId() << "]::modelReplaced()" << endl; Chris@127: #endif Chris@1357: m_cacheValid = false; 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@587: SVDEBUG << "View::layerParametersChanged()" << endl; Chris@127: #endif Chris@127: Chris@1357: m_cacheValid = false; Chris@127: update(); Chris@127: Chris@127: if (layer) { Chris@1266: 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@908: View::globalCentreFrameChanged(sv_frame_t rf) Chris@127: { Chris@211: if (m_followPan) { Chris@908: sv_frame_t f = alignFromReference(rf); Chris@830: #ifdef DEBUG_VIEW Chris@1506: SVCERR << "View[" << getId() << "]::globalCentreFrameChanged(" << rf Chris@682: << "): setting centre frame to " << f << endl; Chris@363: #endif Chris@333: setCentreFrame(f, false); Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@908: View::viewCentreFrameChanged(View *, sv_frame_t ) Chris@211: { Chris@211: // We do nothing with this, but a subclass might Chris@211: } Chris@211: Chris@211: void Chris@908: View::viewManagerPlaybackFrameChanged(sv_frame_t f) Chris@127: { Chris@127: if (m_manager) { Chris@1266: if (sender() != m_manager) return; Chris@127: } Chris@127: Chris@830: #ifdef DEBUG_VIEW Chris@1506: SVCERR << "View[" << getId() << "]::viewManagerPlaybackFrameChanged(" << f << ")" << endl; Chris@830: #endif Chris@830: Chris@301: f = getAlignedPlaybackFrame(); Chris@301: Chris@830: #ifdef DEBUG_VIEW Chris@1506: SVCERR << " -> aligned frame = " << f << endl; Chris@830: #endif Chris@830: Chris@511: movePlayPointer(f); Chris@511: } Chris@511: Chris@511: void Chris@908: View::movePlayPointer(sv_frame_t newFrame) Chris@511: { Chris@830: #ifdef DEBUG_VIEW Chris@1506: SVCERR << "View[" << getId() << "]::movePlayPointer(" << newFrame << ")" << endl; Chris@830: #endif Chris@830: Chris@511: if (m_playPointerFrame == newFrame) return; Chris@511: bool visibleChange = Chris@511: (getXForFrame(m_playPointerFrame) != getXForFrame(newFrame)); Chris@1506: Chris@1506: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "View[" << getId() << "]::movePlayPointer: moving from " Chris@1506: << m_playPointerFrame << " to " << newFrame << ", visible = " Chris@1506: << visibleChange << endl; Chris@1506: #endif Chris@1506: Chris@908: sv_frame_t oldPlayPointerFrame = m_playPointerFrame; Chris@511: m_playPointerFrame = newFrame; Chris@511: if (!visibleChange) return; Chris@127: Chris@513: bool somethingGoingOn = Chris@513: ((QApplication::mouseButtons() != Qt::NoButton) || Chris@513: (QApplication::keyboardModifiers() & Qt::AltModifier)); Chris@513: Chris@789: bool pointerInVisibleArea = Chris@1266: long(m_playPointerFrame) >= getStartFrame() && Chris@789: (m_playPointerFrame < getEndFrame() || Chris@789: // include old pointer location so we know to refresh when moving out Chris@789: oldPlayPointerFrame < getEndFrame()); Chris@789: Chris@127: switch (m_followPlay) { Chris@127: Chris@127: case PlaybackScrollContinuous: Chris@1266: if (!somethingGoingOn) { Chris@1266: setCentreFrame(m_playPointerFrame, false); Chris@1266: } Chris@1266: break; Chris@127: Chris@127: case PlaybackScrollPage: Chris@815: case PlaybackScrollPageWithCentre: Chris@789: Chris@789: if (!pointerInVisibleArea && somethingGoingOn) { Chris@789: Chris@789: m_followPlayIsDetached = true; Chris@789: Chris@789: } else if (!pointerInVisibleArea && m_followPlayIsDetached) { Chris@789: Chris@789: // do nothing; we aren't tracking until the pointer comes back in Chris@789: Chris@789: } else { Chris@789: Chris@789: int xold = getXForFrame(oldPlayPointerFrame); Chris@789: update(xold - 4, 0, 9, height()); Chris@789: Chris@908: sv_frame_t w = getEndFrame() - getStartFrame(); Chris@789: w -= w/5; Chris@1416: sv_frame_t sf = m_playPointerFrame; Chris@1416: if (w > 0) { Chris@1416: sf = (sf / w) * w - w/8; Chris@1416: } Chris@789: Chris@789: if (m_manager && Chris@789: m_manager->isPlaying() && Chris@789: m_manager->getPlaySelectionMode()) { Chris@789: MultiSelection::SelectionList selections = m_manager->getSelections(); Chris@789: if (!selections.empty()) { Chris@908: sv_frame_t selectionStart = selections.begin()->getStartFrame(); Chris@808: if (sf < selectionStart - w / 10) { Chris@808: sf = selectionStart - w / 10; Chris@789: } Chris@789: } Chris@789: } Chris@127: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "PlaybackScrollPage: f = " << m_playPointerFrame << ", sf = " << sf << ", start frame " Chris@789: << getStartFrame() << endl; Chris@127: #endif Chris@127: Chris@789: // We don't consider scrolling unless the pointer is outside Chris@789: // the central visible range already Chris@789: Chris@789: int xnew = getXForFrame(m_playPointerFrame); Chris@127: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "xnew = " << xnew << ", width = " << width() << endl; Chris@127: #endif Chris@127: Chris@789: bool shouldScroll = (xnew > (width() * 7) / 8); Chris@789: Chris@789: if (!m_followPlayIsDetached && (xnew < width() / 8)) { Chris@789: shouldScroll = true; Chris@789: } Chris@789: Chris@789: if (xnew > width() / 8) { Chris@789: m_followPlayIsDetached = false; Chris@791: } else if (somethingGoingOn) { Chris@791: m_followPlayIsDetached = true; Chris@789: } Chris@789: Chris@789: if (!somethingGoingOn && shouldScroll) { Chris@908: sv_frame_t offset = getFrameForX(width()/2) - getStartFrame(); Chris@908: sv_frame_t newCentre = sf + offset; Chris@789: bool changed = setCentreFrame(newCentre, false); Chris@789: if (changed) { Chris@789: xold = getXForFrame(oldPlayPointerFrame); Chris@789: update(xold - 4, 0, 9, height()); Chris@789: } Chris@789: } Chris@789: Chris@789: update(xnew - 4, 0, 9, height()); Chris@789: } Chris@789: break; Chris@127: Chris@127: case PlaybackIgnore: Chris@1266: if (m_playPointerFrame >= getStartFrame() && Chris@511: m_playPointerFrame < getEndFrame()) { Chris@1266: update(); Chris@1266: } Chris@1266: break; Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@1183: View::viewZoomLevelChanged(View *p, ZoomLevel z, bool locked) Chris@127: { Chris@244: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "View[" << getId() << "]: viewZoomLevelChanged(" << p << ", " << z << ", " << locked << ")" << 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@1357: m_cacheValid = false; Chris@1266: m_selectionCached = false; Chris@127: } Chris@127: update(); Chris@127: } Chris@127: Chris@908: sv_frame_t Chris@222: View::getFirstVisibleFrame() const Chris@222: { Chris@908: sv_frame_t f0 = getStartFrame(); Chris@908: sv_frame_t f = getModelsStartFrame(); Chris@806: if (f0 < 0 || f0 < f) return f; Chris@222: return f0; Chris@222: } Chris@222: Chris@908: sv_frame_t Chris@222: View::getLastVisibleFrame() const Chris@222: { Chris@908: sv_frame_t f0 = getEndFrame(); Chris@908: sv_frame_t f = getModelsEndFrame(); Chris@222: if (f0 > f) return f; Chris@222: return f0; Chris@222: } Chris@222: Chris@908: sv_frame_t Chris@127: View::getModelsStartFrame() const Chris@127: { Chris@127: bool first = true; Chris@908: sv_frame_t startFrame = 0; Chris@127: Chris@1475: for (Layer *layer: m_layerStack) { Chris@1475: Chris@1475: auto model = ModelById::get(layer->getModel()); Chris@1475: Chris@1475: if (model && model->isOK()) { Chris@1475: Chris@1475: sv_frame_t thisStartFrame = model->getStartFrame(); Chris@1266: Chris@1266: if (first || thisStartFrame < startFrame) { Chris@1266: startFrame = thisStartFrame; Chris@1266: } Chris@1266: first = false; Chris@1266: } Chris@127: } Chris@1475: Chris@127: return startFrame; Chris@127: } Chris@127: Chris@908: sv_frame_t Chris@127: View::getModelsEndFrame() const Chris@127: { Chris@127: bool first = true; Chris@908: sv_frame_t endFrame = 0; Chris@127: Chris@1475: for (Layer *layer: m_layerStack) { Chris@1475: Chris@1475: auto model = ModelById::get(layer->getModel()); Chris@1475: Chris@1475: if (model && model->isOK()) { Chris@1475: Chris@1475: sv_frame_t thisEndFrame = model->getEndFrame(); Chris@1266: Chris@1266: if (first || thisEndFrame > endFrame) { Chris@1266: endFrame = thisEndFrame; Chris@1266: } Chris@1266: first = false; Chris@1266: } Chris@127: } Chris@127: Chris@127: if (first) return getModelsStartFrame(); Chris@127: return endFrame; Chris@127: } Chris@127: Chris@908: sv_samplerate_t 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@1475: for (Layer *layer: m_layerStack) { Chris@1475: Chris@1475: auto model = ModelById::get(layer->getModel()); Chris@1475: Chris@1475: if (model && model->isOK()) { Chris@1475: return model->getSampleRate(); Chris@1266: } Chris@127: } Chris@1475: 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@1475: if (layer && !layer->getModel().isNone()) { Chris@1475: models.insert(layer->getModel()); Chris@315: } Chris@315: } Chris@315: Chris@315: return models; Chris@315: } Chris@315: Chris@1475: ModelId Chris@320: View::getAligningModel() const Chris@301: { Chris@1490: ModelId aligning, reference; Chris@1490: getAligningAndReferenceModels(aligning, reference); Chris@1490: return aligning; Chris@1490: } Chris@1490: Chris@1490: void Chris@1490: View::getAligningAndReferenceModels(ModelId &aligning, Chris@1490: ModelId &reference) const Chris@1490: { Chris@320: if (!m_manager || Chris@320: !m_manager->getAlignMode() || Chris@1479: m_manager->getPlaybackModel().isNone()) { Chris@1490: return; Chris@314: } Chris@301: Chris@1475: ModelId anyModel; Chris@1475: Chris@1475: for (auto layer: m_layerStack) { Chris@320: Chris@320: if (!layer) continue; Chris@320: if (dynamic_cast(layer)) continue; Chris@301: Chris@1481: ModelId thisId = layer->getModel(); Chris@1481: auto model = ModelById::get(thisId); Chris@301: if (!model) continue; Chris@301: Chris@1481: anyModel = thisId; Chris@1475: Chris@1475: if (!model->getAlignmentReference().isNone()) { Chris@1490: Chris@320: if (layer->isLayerOpaque() || Chris@1475: std::dynamic_pointer_cast Chris@1475: (model)) { Chris@1490: Chris@1490: aligning = thisId; Chris@1490: reference = model->getAlignmentReference(); Chris@1490: return; Chris@1490: Chris@1490: } else if (aligning.isNone()) { Chris@1490: Chris@1490: aligning = thisId; Chris@1490: reference = model->getAlignmentReference(); Chris@320: } Chris@301: } Chris@320: } Chris@301: Chris@1490: if (aligning.isNone()) { Chris@1490: aligning = anyModel; Chris@1490: reference = {}; Chris@1490: } Chris@320: } Chris@320: Chris@908: sv_frame_t Chris@908: View::alignFromReference(sv_frame_t f) const Chris@320: { Chris@856: if (!m_manager || !m_manager->getAlignMode()) return f; Chris@1475: auto aligningModel = ModelById::get(getAligningModel()); Chris@320: if (!aligningModel) return f; Chris@320: return aligningModel->alignFromReference(f); Chris@320: } Chris@320: Chris@908: sv_frame_t Chris@908: View::alignToReference(sv_frame_t f) const Chris@320: { Chris@321: if (!m_manager->getAlignMode()) return f; Chris@1475: auto aligningModel = ModelById::get(getAligningModel()); Chris@320: if (!aligningModel) return f; Chris@320: return aligningModel->alignToReference(f); Chris@320: } Chris@320: Chris@908: sv_frame_t Chris@320: View::getAlignedPlaybackFrame() const Chris@320: { Chris@856: if (!m_manager) return 0; Chris@908: sv_frame_t pf = m_manager->getPlaybackFrame(); Chris@321: if (!m_manager->getAlignMode()) return pf; Chris@321: Chris@1475: auto aligningModel = ModelById::get(getAligningModel()); Chris@320: if (!aligningModel) return pf; Chris@830: Chris@908: sv_frame_t af = aligningModel->alignFromReference(pf); Chris@301: 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@835: for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) { Chris@1266: 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@835: for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) { Chris@587: // SVDEBUG << "View::getScrollableBackLayers: calling isLayerDormant on layer " << *i << endl; Chris@1506: // SVCERR << "(name is " << (*i)->objectName() << ")" Chris@682: // << endl; Chris@1495: // SVDEBUG << "View::getScrollableBackLayers: I am " << getId() << endl; Chris@1266: if ((*i)->isLayerDormant(this)) continue; Chris@1266: if ((*i)->isLayerOpaque()) { Chris@1266: // You can't see anything behind an opaque layer! Chris@1266: scrollables.clear(); Chris@127: if (metUnscrollable) break; Chris@1266: } Chris@1266: 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@1266: m_lastScrollableBackLayers = scrollables; Chris@1266: 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@835: for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) { Chris@1266: if ((*i)->isLayerDormant(this)) continue; Chris@1266: if (!started && (*i)->isLayerScrollable(this)) { Chris@1266: continue; Chris@1266: } Chris@1266: started = true; Chris@1266: if ((*i)->isLayerOpaque()) { Chris@1266: // You can't see anything behind an opaque layer! Chris@1266: nonScrollables.clear(); Chris@1266: } Chris@1266: nonScrollables.push_back(*i); Chris@127: } Chris@127: Chris@127: if (testChanged && nonScrollables != m_lastNonScrollableBackLayers) { Chris@1266: m_lastNonScrollableBackLayers = nonScrollables; Chris@1266: changed = true; Chris@127: } Chris@127: Chris@127: return nonScrollables; Chris@127: } Chris@127: Chris@1327: ZoomLevel Chris@1327: View::getZoomConstraintLevel(ZoomLevel zoomLevel, Chris@1327: ZoomConstraint::RoundingDirection dir) Chris@127: const Chris@127: { Chris@1327: using namespace std::rel_ops; Chris@1327: Chris@1354: ZoomLevel candidate = Chris@1354: RelativelyFineZoomConstraint().getNearestZoomLevel(zoomLevel, dir); Chris@1354: Chris@1354: for (auto i : m_layerStack) { Chris@1354: Chris@1354: if (i->supportsOtherZoomLevels() || !(i->getZoomConstraint())) { Chris@1354: continue; Chris@1354: } Chris@1354: Chris@1327: ZoomLevel thisLevel = Chris@1354: i->getZoomConstraint()->getNearestZoomLevel(zoomLevel, dir); Chris@1266: Chris@1266: // Go for the block size that's furthest from the one Chris@1266: // passed in. Most of the time, that's what we want. Chris@1354: if ((thisLevel > zoomLevel && thisLevel > candidate) || Chris@1327: (thisLevel < zoomLevel && thisLevel < candidate)) { Chris@1327: candidate = thisLevel; Chris@1266: } Chris@127: } Chris@127: Chris@127: return candidate; Chris@127: } Chris@127: Chris@1354: int Chris@1354: View::countZoomLevels() const Chris@1354: { Chris@1354: int n = 0; Chris@1354: ZoomLevel min = ZoomConstraint().getMinZoomLevel(); Chris@1354: ZoomLevel max = ZoomConstraint().getMaxZoomLevel(); Chris@1354: ZoomLevel level = min; Chris@1354: while (true) { Chris@1354: ++n; Chris@1354: if (level == max) { Chris@1354: break; Chris@1354: } Chris@1354: level = getZoomConstraintLevel Chris@1354: (level.incremented(), ZoomConstraint::RoundUp); Chris@1354: } Chris@1506: // SVCERR << "View::countZoomLevels: " << n << endl; Chris@1354: return n; Chris@1354: } Chris@1354: Chris@1354: ZoomLevel Chris@1354: View::getZoomLevelByIndex(int ix) const Chris@1354: { Chris@1354: int n = 0; Chris@1354: ZoomLevel min = ZoomConstraint().getMinZoomLevel(); Chris@1354: ZoomLevel max = ZoomConstraint().getMaxZoomLevel(); Chris@1354: ZoomLevel level = min; Chris@1354: while (true) { Chris@1354: if (n == ix) { Chris@1506: // SVCERR << "View::getZoomLevelByIndex: " << ix << " -> " << level Chris@1355: // << endl; Chris@1354: return level; Chris@1354: } Chris@1354: ++n; Chris@1354: if (level == max) { Chris@1354: break; Chris@1354: } Chris@1354: level = getZoomConstraintLevel Chris@1354: (level.incremented(), ZoomConstraint::RoundUp); Chris@1354: } Chris@1506: // SVCERR << "View::getZoomLevelByIndex: " << ix << " -> " << max << " (max)" Chris@1355: // << endl; Chris@1354: return max; Chris@1354: } Chris@1354: Chris@1354: int Chris@1354: View::getZoomLevelIndex(ZoomLevel z) const Chris@1354: { Chris@1354: int n = 0; Chris@1354: ZoomLevel min = ZoomConstraint().getMinZoomLevel(); Chris@1354: ZoomLevel max = ZoomConstraint().getMaxZoomLevel(); Chris@1354: ZoomLevel level = min; Chris@1354: while (true) { Chris@1354: if (z == level) { Chris@1506: // SVCERR << "View::getZoomLevelIndex: " << z << " -> " << n Chris@1355: // << endl; Chris@1354: return n; Chris@1354: } Chris@1354: ++n; Chris@1354: if (level == max) { Chris@1354: break; Chris@1354: } Chris@1354: level = getZoomConstraintLevel Chris@1354: (level.incremented(), ZoomConstraint::RoundUp); Chris@1354: } Chris@1506: // SVCERR << "View::getZoomLevelIndex: " << z << " -> " << n << " (max)" Chris@1355: // << endl; Chris@1354: return n; Chris@1354: } Chris@1354: Chris@1401: double Chris@1401: View::scaleSize(double size) const Chris@1401: { Chris@1401: static double ratio = 0.0; Chris@1401: Chris@1401: if (ratio == 0.0) { Chris@1401: double baseEm; Chris@1401: #ifdef Q_OS_MAC Chris@1401: baseEm = 17.0; Chris@1401: #else Chris@1401: baseEm = 15.0; Chris@1401: #endif Chris@1401: double em = QFontMetrics(QFont()).height(); Chris@1401: ratio = em / baseEm; Chris@1401: Chris@1401: SVDEBUG << "View::scaleSize: ratio is " << ratio Chris@1401: << " (em = " << em << ")" << endl; Chris@1401: Chris@1401: if (ratio < 1.0) { Chris@1401: SVDEBUG << "View::scaleSize: rounding ratio up to 1.0" << endl; Chris@1401: ratio = 1.0; Chris@1401: } Chris@1401: } Chris@1401: Chris@1401: return size * ratio; Chris@1401: } Chris@1401: Chris@1402: int Chris@1402: View::scalePixelSize(int size) const Chris@1402: { Chris@1402: double d = scaleSize(size); Chris@1402: int i = int(d + 0.5); Chris@1402: if (size != 0 && i == 0) i = 1; Chris@1402: return i; Chris@1402: } Chris@1402: Chris@1401: double Chris@1401: View::scalePenWidth(double width) const Chris@1401: { Chris@1401: if (width <= 0) { // zero-width pen, produce a scaled one-pixel pen Chris@1401: width = 1; Chris@1401: } Chris@1401: double ratio = scaleSize(1.0); Chris@1401: return width * sqrt(ratio); Chris@1401: } Chris@1401: Chris@1401: QPen Chris@1401: View::scalePen(QPen pen) const Chris@1401: { Chris@1401: return QPen(pen.color(), scalePenWidth(pen.width())); Chris@1401: } Chris@1401: Chris@183: bool Chris@183: View::areLayerColoursSignificant() const Chris@183: { Chris@835: for (LayerList::const_iterator i = m_layerStack.begin(); i != m_layerStack.end(); ++i) { Chris@1266: 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@835: LayerList::const_iterator i = m_layerStack.end(); Chris@835: if (i == m_layerStack.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@1183: ZoomLevel newZoomLevel = m_zoomLevel; Chris@127: Chris@127: if (in) { Chris@1324: newZoomLevel = getZoomConstraintLevel(m_zoomLevel.decremented(), Chris@1183: ZoomConstraint::RoundDown); Chris@127: } else { Chris@1324: newZoomLevel = getZoomConstraintLevel(m_zoomLevel.incremented(), Chris@1183: ZoomConstraint::RoundUp); Chris@127: } Chris@127: Chris@1327: using namespace std::rel_ops; Chris@1327: Chris@127: if (newZoomLevel != m_zoomLevel) { Chris@1266: setZoomLevel(newZoomLevel); Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@510: View::scroll(bool right, bool lots, bool e) Chris@127: { Chris@908: sv_frame_t delta; Chris@127: if (lots) { Chris@1266: delta = (getEndFrame() - getStartFrame()) / 2; Chris@127: } else { Chris@1266: delta = (getEndFrame() - getStartFrame()) / 20; Chris@127: } Chris@127: if (right) delta = -delta; Chris@127: Chris@1363: #ifdef DEBUG_VIEW Chris@1363: SVCERR << "View::scroll(" << right << ", " << lots << ", " << e << "): " Chris@1363: << "delta = " << delta << ", m_centreFrame = " << m_centreFrame Chris@1363: << endl; Chris@1363: #endif Chris@1363: Chris@908: if (m_centreFrame < delta) { Chris@1266: setCentreFrame(0, e); Chris@908: } else if (m_centreFrame - delta >= getModelsEndFrame()) { Chris@1266: setCentreFrame(getModelsEndFrame(), e); Chris@127: } else { Chris@1266: setCentreFrame(m_centreFrame - delta, e); Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@797: View::cancelClicked() Chris@797: { Chris@797: QPushButton *cancel = qobject_cast(sender()); Chris@797: if (!cancel) return; Chris@797: Chris@1483: Layer *layer = nullptr; Chris@1483: Chris@797: for (ProgressMap::iterator i = m_progressBars.begin(); Chris@1266: i != m_progressBars.end(); ++i) { Chris@797: if (i->second.cancel == cancel) { Chris@1483: layer = i->first; Chris@1483: break; Chris@797: } Chris@797: } Chris@1483: Chris@1483: if (layer) { Chris@1483: emit cancelButtonPressed(layer); Chris@1483: } Chris@797: } Chris@797: Chris@797: void Chris@1481: View::checkProgress(ModelId modelId) Chris@127: { Chris@1448: if (!m_showProgress) { Chris@1448: #ifdef DEBUG_PROGRESS_STUFF Chris@1495: SVCERR << "View[" << getId() << "]::checkProgress(" << modelId << "): " Chris@1448: << "m_showProgress is off" << endl; Chris@1448: #endif Chris@1448: return; Chris@1448: } Chris@127: Chris@1496: #ifdef DEBUG_PROGRESS_STUFF Chris@1496: SVCERR << "View[" << getId() << "]::checkProgress(" << modelId << ")" << endl; Chris@1496: #endif Chris@1496: Chris@1458: QSettings settings; Chris@1458: settings.beginGroup("View"); Chris@1458: bool showCancelButton = settings.value("showcancelbuttons", true).toBool(); Chris@1458: settings.endGroup(); Chris@1458: Chris@127: int ph = height(); Chris@1448: bool found = false; Chris@127: Chris@1496: if (m_alignmentProgressBar.bar) { Chris@1496: ph -= m_alignmentProgressBar.bar->height(); Chris@1496: } Chris@1496: Chris@555: for (ProgressMap::iterator i = m_progressBars.begin(); Chris@1266: i != m_progressBars.end(); ++i) { Chris@127: Chris@555: QProgressBar *pb = i->second.bar; Chris@797: QPushButton *cancel = i->second.cancel; Chris@555: Chris@1481: if (i->first && i->first->getModel() == modelId) { Chris@127: Chris@1448: found = true; Chris@1448: Chris@555: // The timer is used to test for stalls. If the progress Chris@555: // bar does not get updated for some length of time, the Chris@555: // timer prompts it to go back into "indeterminate" mode Chris@1496: QTimer *timer = i->second.stallCheckTimer; Chris@555: Chris@1266: int completion = i->first->getCompletion(this); Chris@583: QString error = i->first->getError(this); Chris@583: Chris@1448: #ifdef DEBUG_PROGRESS_STUFF Chris@1495: SVCERR << "View[" << getId() << "]::checkProgress(" << modelId << "): " Chris@1448: << "found progress bar " << pb << " for layer at height " << ph Chris@1448: << ": completion = " << completion << endl; Chris@1448: #endif Chris@1448: Chris@583: if (error != "" && error != m_lastError) { Chris@583: QMessageBox::critical(this, tr("Layer rendering error"), error); Chris@583: m_lastError = error; Chris@583: } Chris@301: Chris@388: if (completion > 0) { Chris@555: pb->setMaximum(100); // was 0, for indeterminate start Chris@388: } Chris@388: Chris@1496: if (completion < 100 && Chris@1496: ModelById::isa(modelId)) { Chris@387: update(); // ensure duration &c gets updated Chris@301: } Chris@127: Chris@1266: if (completion >= 100) { Chris@1266: Chris@1266: pb->hide(); Chris@797: cancel->hide(); Chris@555: timer->stop(); Chris@127: Chris@1496: } else if (i->first->isLayerDormant(this)) { Chris@1495: Chris@1495: // A dormant (invisible) layer can still be busy Chris@1495: // generating, but we don't usually want to indicate Chris@1495: // it because it probably means it's a duplicate of a Chris@1495: // visible layer Chris@1495: #ifdef DEBUG_PROGRESS_STUFF Chris@1495: SVCERR << "View[" << getId() << "]::checkProgress(" Chris@1495: << modelId << "): layer is dormant" << endl; Chris@1495: #endif Chris@1495: pb->hide(); Chris@1495: cancel->hide(); Chris@1495: timer->stop(); Chris@1495: Chris@1266: } else { Chris@127: Chris@555: if (!pb->isVisible()) { Chris@1496: i->second.lastStallCheckValue = 0; Chris@555: timer->setInterval(2000); Chris@555: timer->start(); Chris@555: } Chris@555: Chris@1458: if (showCancelButton) { Chris@1458: Chris@1458: int scaled20 = scalePixelSize(20); Chris@1458: Chris@1458: cancel->move(0, ph - pb->height()/2 - scaled20/2); Chris@1458: cancel->show(); Chris@1458: Chris@1458: pb->setValue(completion); Chris@1458: pb->move(scaled20, ph - pb->height()); Chris@1458: Chris@1458: } else { Chris@1458: Chris@1458: cancel->hide(); Chris@1458: Chris@1458: pb->setValue(completion); Chris@1458: pb->move(0, ph - pb->height()); Chris@1458: } Chris@1266: Chris@1266: pb->show(); Chris@1266: pb->update(); Chris@1266: Chris@1495: if (pb->isVisible()) { Chris@1495: ph -= pb->height(); Chris@1495: } Chris@1266: } Chris@1266: } else { Chris@1266: if (pb->isVisible()) { Chris@1266: ph -= pb->height(); Chris@1266: } Chris@1266: } Chris@127: } Chris@1448: Chris@1448: if (!found) { Chris@1448: #ifdef DEBUG_PROGRESS_STUFF Chris@1495: SVCERR << "View[" << getId() << "]::checkProgress(" << modelId << "): " Chris@1487: << "failed to find layer for model in progress map" Chris@1448: << endl; Chris@1448: #endif Chris@1448: } Chris@127: } Chris@127: Chris@555: void Chris@1496: View::checkAlignmentProgress(ModelId modelId) Chris@1496: { Chris@1496: if (!m_showProgress) { Chris@1496: #ifdef DEBUG_PROGRESS_STUFF Chris@1496: SVCERR << "View[" << getId() << "]::checkAlignmentProgress(" << modelId << "): " Chris@1496: << "m_showProgress is off" << endl; Chris@1496: #endif Chris@1496: return; Chris@1496: } Chris@1496: Chris@1496: #ifdef DEBUG_PROGRESS_STUFF Chris@1496: SVCERR << "View[" << getId() << "]::checkAlignmentProgress(" << modelId << ")" << endl; Chris@1496: #endif Chris@1496: Chris@1496: if (!m_alignmentProgressBar.alignedModel.isNone() && Chris@1496: modelId != m_alignmentProgressBar.alignedModel) { Chris@1496: #ifdef DEBUG_PROGRESS_STUFF Chris@1496: SVCERR << "View[" << getId() << "]::checkAlignmentProgress(" << modelId << "): Different model (we're currently showing alignment progress for " << modelId << ", ignoring" << endl; Chris@1496: #endif Chris@1496: return; Chris@1496: } Chris@1496: Chris@1496: auto model = ModelById::get(modelId); Chris@1496: if (!model) { Chris@1496: #ifdef DEBUG_PROGRESS_STUFF Chris@1496: SVCERR << "View[" << getId() << "]::checkAlignmentProgress(" << modelId << "): Model gone" << endl; Chris@1496: #endif Chris@1496: m_alignmentProgressBar.alignedModel = {}; Chris@1496: delete m_alignmentProgressBar.bar; Chris@1496: m_alignmentProgressBar.bar = nullptr; Chris@1496: return; Chris@1496: } Chris@1496: Chris@1496: int completion = model->getAlignmentCompletion(); Chris@1496: Chris@1496: #ifdef DEBUG_PROGRESS_STUFF Chris@1496: SVCERR << "View[" << getId() << "]::checkAlignmentProgress(" << modelId << "): Completion is " << completion << endl; Chris@1496: #endif Chris@1496: Chris@1496: int ph = height(); Chris@1496: Chris@1496: if (completion >= 100) { Chris@1496: m_alignmentProgressBar.alignedModel = {}; Chris@1496: delete m_alignmentProgressBar.bar; Chris@1496: m_alignmentProgressBar.bar = nullptr; Chris@1496: return; Chris@1496: } Chris@1496: Chris@1496: QProgressBar *pb = m_alignmentProgressBar.bar; Chris@1496: if (!pb) { Chris@1496: pb = new QProgressBar(this); Chris@1496: pb->setMinimum(0); Chris@1496: pb->setMaximum(100); Chris@1496: pb->setFixedWidth(80); Chris@1496: pb->setTextVisible(false); Chris@1496: m_alignmentProgressBar.alignedModel = modelId; Chris@1496: m_alignmentProgressBar.bar = pb; Chris@1496: } Chris@1496: Chris@1496: pb->setValue(completion); Chris@1496: pb->move(0, ph - pb->height()); Chris@1496: pb->show(); Chris@1496: pb->update(); Chris@1496: } Chris@1496: Chris@1496: void Chris@555: View::progressCheckStalledTimerElapsed() Chris@555: { Chris@555: QObject *s = sender(); Chris@555: QTimer *t = qobject_cast(s); Chris@555: if (!t) return; Chris@1448: Chris@555: for (ProgressMap::iterator i = m_progressBars.begin(); Chris@555: i != m_progressBars.end(); ++i) { Chris@1448: Chris@1496: if (i->second.stallCheckTimer == t) { Chris@1448: Chris@1495: int value = i->second.bar->value(); Chris@1495: Chris@1448: #ifdef DEBUG_PROGRESS_STUFF Chris@1495: SVCERR << "View[" << getId() << "]::progressCheckStalledTimerElapsed for layer " << i->first << ": value is " << value << endl; Chris@1448: #endif Chris@1448: Chris@1496: if (value > 0 && value == i->second.lastStallCheckValue) { Chris@555: i->second.bar->setMaximum(0); // indeterminate Chris@555: } Chris@1496: i->second.lastStallCheckValue = value; Chris@555: return; Chris@555: } Chris@555: } Chris@555: } Chris@555: Chris@384: int Chris@384: View::getProgressBarWidth() const Chris@384: { Chris@1496: if (m_alignmentProgressBar.bar) { Chris@1496: return m_alignmentProgressBar.bar->width(); Chris@1496: } Chris@1496: Chris@384: for (ProgressMap::const_iterator i = m_progressBars.begin(); Chris@1266: i != m_progressBars.end(); ++i) { Chris@555: if (i->second.bar && i->second.bar->isVisible()) { Chris@555: return i->second.bar->width(); Chris@555: } Chris@384: } Chris@384: Chris@384: return 0; Chris@384: } Chris@384: Chris@127: void Chris@339: View::setPaintFont(QPainter &paint) Chris@339: { Chris@955: int scaleFactor = 1; Chris@956: int dpratio = effectiveDevicePixelRatio(); Chris@955: if (dpratio > 1) { Chris@955: QPaintDevice *dev = paint.device(); Chris@955: if (dynamic_cast(dev) || dynamic_cast(dev)) { Chris@955: scaleFactor = dpratio; Chris@955: } Chris@955: } Chris@955: Chris@339: QFont font(paint.font()); Chris@955: font.setPointSize(Preferences::getInstance()->getViewFontSize() Chris@955: * scaleFactor); Chris@339: paint.setFont(font); Chris@339: } Chris@339: Chris@915: QRect Chris@915: View::getPaintRect() const Chris@915: { Chris@919: return rect(); Chris@915: } Chris@915: Chris@339: void Chris@127: View::paintEvent(QPaintEvent *e) Chris@127: { Chris@127: // Profiler prof("View::paintEvent", false); Chris@1506: Chris@1506: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: { Chris@1506: sv_frame_t startFrame = getStartFrame(); Chris@1506: SVCERR << "View[" << getId() << "]::paintEvent: centre frame is " << m_centreFrame << " (start frame " << startFrame << ", height " << height() << ")" << endl; Chris@1506: } Chris@1506: #endif matthiasm@785: Chris@835: if (m_layerStack.empty()) { Chris@1266: QFrame::paintEvent(e); Chris@1266: return; Chris@127: } Chris@127: Chris@127: // ensure our constraints are met Chris@1354: m_zoomLevel = getZoomConstraintLevel Chris@1354: (m_zoomLevel, ZoomConstraint::RoundNearest); Chris@127: Chris@1357: // We have a cache, which retains the state of scrollable (back) Chris@1357: // layers from one paint to the next, and a buffer, which we paint Chris@1357: // onto before copying directly to the widget. Both are at scaled Chris@1357: // resolution (e.g. 2x on a pixel-doubled display), whereas the Chris@1357: // paint event always comes in at formal (1x) resolution. Chris@1357: Chris@1357: // If we touch the cache, we always leave it in a valid state Chris@1357: // across its whole extent. When another method invalidates the Chris@1357: // cache, it does so by setting m_cacheValid false, so if that Chris@1357: // flag is true on entry, then the cache is valid across its whole Chris@1357: // extent - although it may be valid for a different centre frame, Chris@1357: // zoom level, or view size from those now in effect. Chris@1357: Chris@1357: // Our process goes: Chris@1357: // Chris@1357: // 1. Check whether we have any scrollable (cacheable) layers. If Chris@1357: // we don't, then invalidate and ignore the cache and go to Chris@1357: // step 5. Otherwise: Chris@1357: // Chris@1357: // 2. Check the cache, scroll as necessary, identify any area that Chris@1357: // needs to be refreshed (this might be the whole cache). Chris@1357: // Chris@1357: // 3. Paint to cache the area that needs to be refreshed, from the Chris@1357: // stack of scrollable layers. Chris@1357: // Chris@1357: // 4. Paint to buffer from cache: if there are no non-cached areas Chris@1357: // or selections and the cache has not scrolled, then paint the Chris@1357: // union of the area of cache that has changed and the area Chris@1357: // that the paint event reported as exposed; otherwise paint Chris@1357: // the whole. Chris@1357: // Chris@1357: // 5. Paint the exposed area to the buffer from the cache plus all Chris@1357: // the layers that haven't been cached, plus selections etc. Chris@1357: // Chris@1357: // 6. Paint the exposed rect from the buffer. Chris@1357: // Chris@1357: // Note that all rects except the target for the final step are at Chris@1357: // cache (scaled, 2x as applicable) resolution. Chris@1357: Chris@1357: int dpratio = effectiveDevicePixelRatio(); Chris@1357: Chris@1357: QRect requestedPaintArea(scaledRect(rect(), dpratio)); Chris@127: if (e) { Chris@1357: // cut down to only the area actually exposed Chris@1357: requestedPaintArea &= scaledRect(e->rect(), dpratio); Chris@127: } 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: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "View[" << getId() << "]::paintEvent: have " << scrollables.size() Chris@1266: << " scrollable back layers and " << nonScrollables.size() Chris@1266: << " non-scrollable front layers" << endl; Chris@127: #endif Chris@127: Chris@1357: if (layersChanged || scrollables.empty()) { Chris@1357: m_cacheValid = false; Chris@127: } Chris@127: Chris@1357: QRect wholeArea(scaledRect(rect(), dpratio)); Chris@1357: QSize wholeSize(scaledSize(size(), dpratio)); Chris@1357: Chris@1357: if (!m_buffer || wholeSize != m_buffer->size()) { Chris@952: delete m_buffer; Chris@1357: m_buffer = new QPixmap(wholeSize); Chris@952: } Chris@1357: Chris@1357: bool shouldUseCache = false; Chris@1357: bool shouldRepaintCache = false; Chris@1357: QRect cacheAreaToRepaint; Chris@952: Chris@1357: static HitCount count("View cache"); Chris@1357: Chris@127: if (!scrollables.empty()) { Chris@244: Chris@1357: shouldUseCache = true; Chris@1357: shouldRepaintCache = true; Chris@1357: cacheAreaToRepaint = wholeArea; Chris@1357: Chris@244: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "View[" << getId() << "]: cache " << m_cache << ", cache zoom " Chris@682: << m_cacheZoomLevel << ", zoom " << m_zoomLevel << endl; Chris@244: #endif Chris@244: Chris@1327: using namespace std::rel_ops; Chris@1327: Chris@1357: if (!m_cacheValid || Chris@1357: !m_cache || Chris@1266: m_cacheZoomLevel != m_zoomLevel || Chris@1357: m_cache->size() != wholeSize) { Chris@1357: Chris@1357: // cache is not valid at all Chris@1357: Chris@1357: if (requestedPaintArea.width() < wholeSize.width() / 10) { Chris@1357: Chris@1357: m_cacheValid = false; Chris@1357: shouldUseCache = false; Chris@1357: shouldRepaintCache = false; Chris@1357: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "View[" << getId() << "]::paintEvent: cache is invalid but only small area requested, will repaint directly instead" << endl; Chris@127: #endif Chris@1266: } else { Chris@1357: Chris@1357: if (!m_cache || Chris@1357: m_cache->size() != wholeSize) { Chris@1357: delete m_cache; Chris@1357: m_cache = new QPixmap(wholeSize); Chris@1357: } Chris@1357: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "View[" << getId() << "]::paintEvent: cache is invalid, will repaint whole" << endl; Chris@127: #endif Chris@1266: } Chris@1266: Chris@1357: count.miss(); Chris@1357: Chris@1266: } else if (m_cacheCentreFrame != m_centreFrame) { Chris@1266: Chris@1506: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "View[" << getId() << "]::paintEvent: cache centre frame is " << m_cacheCentreFrame << endl; Chris@1506: #endif Chris@1506: Chris@1357: int dx = dpratio * (getXForFrame(m_cacheCentreFrame) - Chris@1357: getXForFrame(m_centreFrame)); Chris@1357: Chris@1357: if (dx > -m_cache->width() && dx < m_cache->width()) { Chris@1357: Chris@1408: m_cache->scroll(dx, 0, m_cache->rect(), nullptr); Chris@1357: Chris@1357: if (dx < 0) { Chris@1357: cacheAreaToRepaint = Chris@1357: QRect(m_cache->width() + dx, 0, -dx, m_cache->height()); Chris@1357: } else { Chris@1357: cacheAreaToRepaint = Chris@1357: QRect(0, 0, dx, m_cache->height()); Chris@1266: } Chris@1357: Chris@1357: count.partial(); Chris@1357: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "View[" << getId() << "]::paintEvent: scrolled cache by " << dx << endl; Chris@127: #endif Chris@1266: } else { Chris@1357: count.miss(); Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "View[" << getId() << "]::paintEvent: scrolling too far" << endl; Chris@127: #endif Chris@1266: } Chris@1266: Chris@1266: } else { Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "View[" << getId() << "]::paintEvent: cache is good" << endl; Chris@127: #endif Chris@1357: count.hit(); Chris@1357: shouldRepaintCache = false; Chris@1266: } Chris@1357: } Chris@1357: Chris@1357: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "View[" << getId() << "]::paintEvent: m_cacheValid = " << m_cacheValid << ", shouldUseCache = " << shouldUseCache << ", shouldRepaintCache = " << shouldRepaintCache << ", cacheAreaToRepaint = " << cacheAreaToRepaint.x() << "," << cacheAreaToRepaint.y() << " " << cacheAreaToRepaint.width() << "x" << cacheAreaToRepaint.height() << endl; Chris@1357: #endif Chris@1357: Chris@1357: if (shouldRepaintCache && !shouldUseCache) { Chris@1357: // If we are repainting the cache, then we paint the Chris@1357: // scrollables only to the cache, not to the buffer. So if Chris@1357: // shouldUseCache is also false, then the scrollables can't Chris@1357: // appear because they will only be on the cache Chris@1357: throw std::logic_error("ERROR: shouldRepaintCache is true, but shouldUseCache is false: this can't lead to the correct result"); Chris@1357: } Chris@1357: Chris@1490: // Create the ViewProxy for geometry provision, using the Chris@1490: // device-pixel ratio for pixel-doubled hi-dpi rendering as Chris@1490: // appropriate. Chris@1490: Chris@1490: ViewProxy proxy(this, dpratio); Chris@1490: Chris@1490: // Some layers may need an aligning proxy. If a layer's model has Chris@1490: // a source model that is the reference model for the aligning Chris@1508: // model, and the layer is tagged as to be aligned, then we might Chris@1508: // use an aligning proxy. Note this is actually made use of only Chris@1508: // if m_useAligningProxy is true further down. Chris@1490: Chris@1490: ModelId alignmentModelId; Chris@1490: ModelId alignmentReferenceId; Chris@1490: auto aligningModel = ModelById::get(getAligningModel()); Chris@1490: if (aligningModel) { Chris@1490: alignmentModelId = aligningModel->getAlignment(); Chris@1490: alignmentReferenceId = aligningModel->getAlignmentReference(); Chris@1493: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1492: SVCERR << "alignmentModelId = " << alignmentModelId << " (reference = " << alignmentReferenceId << ")" << endl; Chris@1493: #endif Chris@1490: } else { Chris@1493: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1490: SVCERR << "no aligningModel" << endl; Chris@1493: #endif Chris@1490: } Chris@1490: ViewProxy aligningProxy(this, dpratio, alignmentModelId); Chris@1490: Chris@1357: // Scrollable (cacheable) items first. If we are repainting the Chris@1357: // cache, then we paint these to the cache; otherwise straight to Chris@1357: // the buffer. Chris@1357: QRect areaToPaint; Chris@1357: QPainter paint; Chris@1357: Chris@1357: if (shouldRepaintCache) { Chris@1357: paint.begin(m_cache); Chris@1357: areaToPaint = cacheAreaToRepaint; Chris@1357: } else { Chris@1357: paint.begin(m_buffer); Chris@1357: areaToPaint = requestedPaintArea; Chris@1357: } Chris@1357: Chris@1357: setPaintFont(paint); Chris@1357: paint.setClipRect(areaToPaint); Chris@1357: Chris@1357: paint.setPen(getBackground()); Chris@1357: paint.setBrush(getBackground()); Chris@1357: paint.drawRect(areaToPaint); Chris@1357: Chris@1357: paint.setPen(getForeground()); Chris@1357: paint.setBrush(Qt::NoBrush); Chris@1357: Chris@1357: for (LayerList::iterator i = scrollables.begin(); Chris@1357: i != scrollables.end(); ++i) { Chris@1357: Chris@1357: paint.setRenderHint(QPainter::Antialiasing, false); Chris@1357: paint.save(); Chris@1357: Chris@1490: Layer *layer = *i; Chris@1490: Chris@1490: bool useAligningProxy = false; Chris@1490: if (m_useAligningProxy) { Chris@1490: if (layer->getModel() == alignmentReferenceId || Chris@1490: layer->getSourceModel() == alignmentReferenceId) { Chris@1490: useAligningProxy = true; Chris@1490: } Chris@1490: } Chris@1490: Chris@1357: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "Painting scrollable layer " << layer << " (model " << layer->getModel() << ", source model " << layer->getSourceModel() << ") with shouldRepaintCache = " << shouldRepaintCache << ", useAligningProxy = " << useAligningProxy << ", dpratio = " << dpratio << ", areaToPaint = " << areaToPaint.x() << "," << areaToPaint.y() << " " << areaToPaint.width() << "x" << areaToPaint.height() << endl; Chris@1357: #endif Chris@1490: Chris@1490: layer->paint(useAligningProxy ? &aligningProxy : &proxy, Chris@1490: paint, areaToPaint); Chris@1357: Chris@1357: paint.restore(); Chris@1357: } Chris@1357: Chris@1357: paint.end(); Chris@1357: Chris@1357: if (shouldRepaintCache) { Chris@1357: // and now we have Chris@1357: m_cacheValid = true; Chris@1266: m_cacheCentreFrame = m_centreFrame; Chris@1266: m_cacheZoomLevel = m_zoomLevel; Chris@127: } Chris@127: Chris@1357: if (shouldUseCache) { Chris@1357: paint.begin(m_buffer); Chris@1357: paint.drawPixmap(requestedPaintArea, *m_cache, requestedPaintArea); Chris@1266: paint.end(); Chris@127: } Chris@127: Chris@1357: // Now non-cacheable items. Chris@1357: Chris@952: paint.begin(m_buffer); Chris@1357: paint.setClipRect(requestedPaintArea); Chris@339: setPaintFont(paint); Chris@127: if (scrollables.empty()) { Chris@287: paint.setPen(getBackground()); Chris@287: paint.setBrush(getBackground()); Chris@1357: paint.drawRect(requestedPaintArea); Chris@127: } Chris@1266: Chris@287: paint.setPen(getForeground()); Chris@127: paint.setBrush(Qt::NoBrush); Chris@1266: Chris@1357: for (LayerList::iterator i = nonScrollables.begin(); Chris@1357: i != nonScrollables.end(); ++i) { Chris@1490: Chris@1490: Layer *layer = *i; Chris@1490: Chris@1490: bool useAligningProxy = false; Chris@1490: if (m_useAligningProxy) { Chris@1490: if (layer->getModel() == alignmentReferenceId || Chris@1490: layer->getSourceModel() == alignmentReferenceId) { Chris@1490: useAligningProxy = true; Chris@1490: } Chris@1490: } Chris@1490: Chris@951: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1506: SVCERR << "Painting non-scrollable layer " << layer << " (model " << layer->getModel() << ", source model " << layer->getSourceModel() << ") with shouldRepaintCache = " << shouldRepaintCache << ", useAligningProxy = " << useAligningProxy << ", dpratio = " << dpratio << ", requestedPaintArea = " << requestedPaintArea.x() << "," << requestedPaintArea.y() << " " << requestedPaintArea.width() << "x" << requestedPaintArea.height() << endl; Chris@951: #endif Chris@1490: Chris@1490: layer->paint(useAligningProxy ? &aligningProxy : &proxy, Chris@1490: paint, requestedPaintArea); Chris@127: } Chris@1266: Chris@127: paint.end(); Chris@1357: Chris@1357: // Now paint to widget from buffer: target rects from here on, Chris@1357: // unlike all the preceding, are at formal (1x) resolution Chris@953: Chris@953: paint.begin(this); Chris@339: setPaintFont(paint); Chris@953: if (e) paint.setClipRect(e->rect()); Chris@1357: Chris@1357: QRect finalPaintRect = e ? e->rect() : rect(); Chris@1357: paint.drawPixmap(finalPaintRect, *m_buffer, Chris@1357: scaledRect(finalPaintRect, dpratio)); Chris@1357: Chris@1357: drawSelections(paint); Chris@1357: drawPlayPointer(paint); Chris@1357: Chris@127: paint.end(); 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@1266: selections = m_manager->getSelections(); Chris@1266: if (m_manager->haveInProgressSelection()) { Chris@1266: bool exclusive; Chris@1266: Selection inProgressSelection = Chris@1266: m_manager->getInProgressSelection(exclusive); Chris@1266: if (exclusive) selections.clear(); Chris@1266: selections.insert(inProgressSelection); Chris@1266: } 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@908: sv_samplerate_t sampleRate = getModelsSampleRate(); Chris@127: Chris@127: QPoint localPos; Chris@908: sv_frame_t illuminateFrame = -1; Chris@127: bool closeToLeft, closeToRight; Chris@127: Chris@127: if (shouldIlluminateLocalSelection(localPos, closeToLeft, closeToRight)) { Chris@1266: 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@1266: i != selections.end(); ++i) { Chris@1266: Chris@1266: int p0 = getXForFrame(alignFromReference(i->getStartFrame())); Chris@1266: int p1 = getXForFrame(alignFromReference(i->getEndFrame())); Chris@1266: Chris@1266: if (p1 < 0 || p0 > width()) continue; Chris@127: Chris@127: #ifdef DEBUG_VIEW_WIDGET_PAINT Chris@1266: SVDEBUG << "View::drawSelections: " << p0 << ",-1 [" << (p1-p0) << "x" << (height()+1) << "]" << endl; Chris@127: #endif Chris@127: Chris@1266: bool illuminateThis = Chris@1266: (illuminateFrame >= 0 && i->contains(illuminateFrame)); Chris@1266: Chris@1270: double h = height(); Chris@1401: double penWidth = scalePenWidth(1.0); Chris@1270: double half = penWidth/2.0; Chris@1270: Chris@1270: paint.setPen(QPen(QColor(150, 150, 255), penWidth)); Chris@183: Chris@183: if (translucent && shouldLabelSelections()) { Chris@1270: paint.drawRect(QRectF(p0, -penWidth, p1 - p0, h + 2*penWidth)); 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@1270: paint.drawRect(QRectF(p0, half, p1 - p0, h - penWidth)); Chris@183: } Chris@127: Chris@1266: if (illuminateThis) { Chris@1266: paint.save(); Chris@1401: penWidth = scalePenWidth(2.0); Chris@1270: half = penWidth/2.0; Chris@1270: paint.setPen(QPen(getForeground(), penWidth)); Chris@1266: if (closeToLeft) { Chris@1270: paint.drawLine(QLineF(p0, half, p1, half)); Chris@1270: paint.drawLine(QLineF(p0, half, p0, h - half)); Chris@1270: paint.drawLine(QLineF(p0, h - half, p1, h - half)); Chris@1266: } else if (closeToRight) { Chris@1270: paint.drawLine(QLineF(p0, half, p1, half)); Chris@1270: paint.drawLine(QLineF(p1, half, p1, h - half)); Chris@1270: paint.drawLine(QLineF(p0, h - half, p1, h - half)); Chris@1266: } else { Chris@1266: paint.setBrush(Qt::NoBrush); Chris@1270: paint.drawRect(QRectF(p0, half, p1 - p0, h - penWidth)); Chris@1266: } Chris@1266: paint.restore(); Chris@1266: } Chris@1266: Chris@1266: if (sampleRate && shouldLabelSelections() && m_manager && Chris@189: m_manager->shouldShowSelectionExtents()) { Chris@1266: Chris@1266: QString startText = QString("%1 / %2") Chris@1266: .arg(QString::fromStdString Chris@1266: (RealTime::frame2RealTime Chris@1266: (i->getStartFrame(), sampleRate).toText(true))) Chris@1266: .arg(i->getStartFrame()); Chris@1266: Chris@1266: QString endText = QString(" %1 / %2") Chris@1266: .arg(QString::fromStdString Chris@1266: (RealTime::frame2RealTime Chris@1266: (i->getEndFrame(), sampleRate).toText(true))) Chris@1266: .arg(i->getEndFrame()); Chris@1266: Chris@1266: QString durationText = QString("(%1 / %2) ") Chris@1266: .arg(QString::fromStdString Chris@1266: (RealTime::frame2RealTime Chris@1266: (i->getEndFrame() - i->getStartFrame(), sampleRate) Chris@1266: .toText(true))) Chris@1266: .arg(i->getEndFrame() - i->getStartFrame()); Chris@1476: Chris@1476: // Qt 5.13 deprecates QFontMetrics::width(), but its suggested Chris@1476: // replacement (horizontalAdvance) was only added in Qt 5.11 Chris@1476: // which is too new for us Chris@1476: #pragma GCC diagnostic ignored "-Wdeprecated-declarations" Chris@1266: Chris@1266: int sw = metrics.width(startText), Chris@1266: ew = metrics.width(endText), Chris@1266: dw = metrics.width(durationText); Chris@1266: Chris@1266: int sy = metrics.ascent() + metrics.height() + 4; Chris@1266: int ey = sy; Chris@1266: int dy = sy + metrics.height(); Chris@1266: Chris@1266: int sx = p0 + 2; Chris@1266: int ex = sx; Chris@1266: int dx = sx; Chris@127: Chris@501: bool durationBothEnds = true; Chris@501: Chris@1266: if (sw + ew > (p1 - p0)) { Chris@1266: ey += metrics.height(); Chris@1266: dy += metrics.height(); Chris@501: durationBothEnds = false; Chris@1266: } Chris@1266: Chris@1266: if (ew < (p1 - p0)) { Chris@1266: ex = p1 - 2 - ew; Chris@1266: } Chris@1266: Chris@1266: if (dw < (p1 - p0)) { Chris@1266: dx = p1 - 2 - dw; Chris@1266: } Chris@127: Chris@1193: PaintAssistant::drawVisibleText(this, paint, sx, sy, startText, Chris@1193: PaintAssistant::OutlinedText); Chris@1193: PaintAssistant::drawVisibleText(this, paint, ex, ey, endText, Chris@1193: PaintAssistant::OutlinedText); Chris@1193: PaintAssistant::drawVisibleText(this, paint, dx, dy, durationText, Chris@1193: PaintAssistant::OutlinedText); Chris@501: if (durationBothEnds) { Chris@1193: PaintAssistant::drawVisibleText(this, paint, sx, dy, durationText, Chris@1193: PaintAssistant::OutlinedText); Chris@501: } Chris@1266: } Chris@127: } Chris@127: Chris@127: paint.restore(); Chris@127: } Chris@127: Chris@267: void Chris@1357: View::drawPlayPointer(QPainter &paint) Chris@1357: { Chris@1357: bool showPlayPointer = true; Chris@1357: Chris@1357: if (m_followPlay == PlaybackScrollContinuous) { Chris@1357: showPlayPointer = false; Chris@1357: } else if (m_playPointerFrame <= getStartFrame() || Chris@1357: m_playPointerFrame >= getEndFrame()) { Chris@1357: showPlayPointer = false; Chris@1357: } else if (m_manager && !m_manager->isPlaying()) { Chris@1357: if (m_playPointerFrame == getCentreFrame() && Chris@1357: m_manager->shouldShowCentreLine() && Chris@1357: m_followPlay != PlaybackIgnore) { Chris@1357: // Don't show the play pointer when it is redundant with Chris@1357: // the centre line Chris@1357: showPlayPointer = false; Chris@1357: } Chris@1357: } Chris@1357: Chris@1357: if (showPlayPointer) { Chris@1357: Chris@1357: int playx = getXForFrame(m_playPointerFrame); Chris@1357: Chris@1357: paint.setPen(getForeground()); Chris@1357: paint.drawLine(playx - 1, 0, playx - 1, height() - 1); Chris@1357: paint.drawLine(playx + 1, 0, playx + 1, height() - 1); Chris@1357: paint.drawPoint(playx, 0); Chris@1357: paint.drawPoint(playx, height() - 1); Chris@1357: paint.setPen(getBackground()); Chris@1357: paint.drawLine(playx, 1, playx, height() - 2); Chris@1357: } Chris@1357: } Chris@1357: Chris@1357: void Chris@270: View::drawMeasurementRect(QPainter &paint, const Layer *topLayer, QRect r, Chris@270: bool focus) const Chris@267: { Chris@587: // SVDEBUG << "View::drawMeasurementRect(" << r.x() << "," << r.y() << " " Chris@585: // << r.width() << "x" << r.height() << ")" << 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@904: double 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@362: // top-left point, x-coord Chris@362: 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@362: Chris@362: // bottom-right point, x-coord 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@362: Chris@362: // dimension, width Chris@267: Chris@283: if (b0 && b1 && v1 != v0 && u0 == u1) { Chris@362: 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@362: // top-left point, y-coord Chris@362: 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@362: // bottom-right point, y-coord Chris@362: 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@904: double dy = 0.f; Chris@274: QString du; Chris@274: Chris@362: // dimension, height Chris@362: 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@362: if (du == "Hz") { Chris@362: int semis; Chris@904: double cents; Chris@362: semis = Pitch::getPitchForFrequencyDifference(v0, v1, ¢s); Chris@362: dys = QString("[%1 %2 (%3)]") Chris@362: .arg(dy).arg(du) Chris@362: .arg(Pitch::getLabelForPitchRange(semis, cents)); Chris@362: } else { Chris@362: dys = QString("[%1 %2]").arg(dy).arg(du); Chris@362: } Chris@274: } else { Chris@362: 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@1078: PaintAssistant::drawVisibleText(this, paint, axx, axy, axs, PaintAssistant::OutlinedText); Chris@267: axy += fontHeight; Chris@267: } Chris@267: Chris@267: if (ays != "") { Chris@1078: PaintAssistant::drawVisibleText(this, paint, axx, axy, ays, PaintAssistant::OutlinedText); Chris@267: axy += fontHeight; Chris@267: } Chris@267: Chris@267: if (bxs != "") { Chris@1078: PaintAssistant::drawVisibleText(this, paint, bxx, bxy, bxs, PaintAssistant::OutlinedText); Chris@267: bxy += fontHeight; Chris@267: } Chris@267: Chris@267: if (bys != "") { Chris@1078: PaintAssistant::drawVisibleText(this, paint, bxx, bxy, bys, PaintAssistant::OutlinedText); Chris@267: bxy += fontHeight; Chris@267: } Chris@267: Chris@267: if (dxs != "") { Chris@1078: PaintAssistant::drawVisibleText(this, paint, dxx, dxy, dxs, PaintAssistant::OutlinedText); Chris@267: dxy += fontHeight; Chris@267: } Chris@267: Chris@267: if (dys != "") { Chris@1078: PaintAssistant::drawVisibleText(this, paint, dxx, dxy, dys, PaintAssistant::OutlinedText); Chris@267: dxy += fontHeight; Chris@267: } Chris@278: Chris@278: paint.restore(); Chris@267: } Chris@267: Chris@227: bool Chris@908: View::render(QPainter &paint, int xorigin, sv_frame_t f0, sv_frame_t f1) Chris@227: { Chris@1327: int x0 = int(round(m_zoomLevel.framesToPixels(double(f0)))); Chris@1327: int x1 = int(round(m_zoomLevel.framesToPixels(double(f1)))); Chris@806: Chris@806: int w = x1 - x0; Chris@806: Chris@908: sv_frame_t origCentreFrame = m_centreFrame; Chris@227: Chris@227: bool someLayersIncomplete = false; Chris@227: Chris@835: for (LayerList::iterator i = m_layerStack.begin(); Chris@835: i != m_layerStack.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@835: for (LayerList::iterator i = m_layerStack.begin(); Chris@835: i != m_layerStack.end(); ++i) { Chris@227: Chris@227: int c = (*i)->getCompletion(this); Chris@835: if (i == m_layerStack.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@806: for (int 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@1327: m_centreFrame = f0 + sv_frame_t(round(m_zoomLevel.pixelsToFrames Chris@1327: (x + width()/2))); 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@1266: paint.drawRect(QRect(xorigin + x, 0, width(), height())); Chris@1266: Chris@1266: paint.setPen(getForeground()); Chris@1266: paint.setBrush(Qt::NoBrush); Chris@1266: Chris@1266: for (LayerList::iterator i = m_layerStack.begin(); Chris@835: i != m_layerStack.end(); ++i) { Chris@919: if (!((*i)->isLayerDormant(this))){ Chris@919: Chris@919: paint.setRenderHint(QPainter::Antialiasing, false); Chris@919: Chris@919: paint.save(); Chris@919: paint.translate(xorigin + x, 0); Chris@919: Chris@1506: SVCERR << "Centre frame now: " << m_centreFrame << " drawing to " << chunk.x() + x + xorigin << ", " << chunk.width() << endl; Chris@919: Chris@919: (*i)->setSynchronousPainting(true); Chris@919: Chris@919: (*i)->paint(this, paint, chunk); Chris@919: Chris@919: (*i)->setSynchronousPainting(false); Chris@919: Chris@919: paint.restore(); Chris@919: } Chris@1266: } Chris@227: } Chris@227: Chris@227: m_centreFrame = origCentreFrame; Chris@227: update(); Chris@227: return true; Chris@227: } Chris@227: Chris@227: QImage * Chris@1202: View::renderToNewImage() Chris@227: { Chris@908: sv_frame_t f0 = getModelsStartFrame(); Chris@908: sv_frame_t f1 = getModelsEndFrame(); Chris@227: Chris@1202: return renderPartToNewImage(f0, f1); Chris@229: } Chris@229: Chris@229: QImage * Chris@1202: View::renderPartToNewImage(sv_frame_t f0, sv_frame_t f1) Chris@229: { Chris@1327: int x0 = int(round(getZoomLevel().framesToPixels(double(f0)))); Chris@1327: int x1 = int(round(getZoomLevel().framesToPixels(double(f1)))); 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@1408: return nullptr; Chris@227: } else { Chris@227: delete paint; Chris@227: return image; Chris@227: } Chris@227: } Chris@227: Chris@229: QSize Chris@1202: View::getRenderedImageSize() Chris@229: { Chris@908: sv_frame_t f0 = getModelsStartFrame(); Chris@908: sv_frame_t f1 = getModelsEndFrame(); Chris@229: Chris@1202: return getRenderedPartImageSize(f0, f1); Chris@229: } Chris@229: Chris@229: QSize Chris@1202: View::getRenderedPartImageSize(sv_frame_t f0, sv_frame_t f1) Chris@229: { Chris@1327: int x0 = int(round(getZoomLevel().framesToPixels(double(f0)))); Chris@1327: int x1 = int(round(getZoomLevel().framesToPixels(double(f1)))); Chris@229: Chris@229: return QSize(x1 - x0, height()); Chris@229: } Chris@229: Chris@1202: bool Chris@1202: View::renderToSvgFile(QString filename) Chris@1202: { Chris@1202: sv_frame_t f0 = getModelsStartFrame(); Chris@1202: sv_frame_t f1 = getModelsEndFrame(); Chris@1202: Chris@1202: return renderPartToSvgFile(filename, f0, f1); Chris@1202: } Chris@1202: Chris@1202: bool Chris@1202: View::renderPartToSvgFile(QString filename, sv_frame_t f0, sv_frame_t f1) Chris@1202: { Chris@1327: int x0 = int(round(getZoomLevel().framesToPixels(double(f0)))); Chris@1327: int x1 = int(round(getZoomLevel().framesToPixels(double(f1)))); Chris@1202: Chris@1202: QSvgGenerator generator; Chris@1202: generator.setFileName(filename); Chris@1202: generator.setSize(QSize(x1 - x0, height())); Chris@1202: generator.setViewBox(QRect(0, 0, x1 - x0, height())); Chris@1202: generator.setTitle(tr("Exported image from %1") Chris@1202: .arg(QApplication::applicationName())); Chris@1202: Chris@1202: QPainter paint; Chris@1202: paint.begin(&generator); Chris@1202: bool result = render(paint, 0, f0, f1); Chris@1202: paint.end(); Chris@1202: return result; Chris@1202: } Chris@1202: 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@1327: int classicZoomValue, deepZoomValue; Chris@1327: Chris@1327: if (m_zoomLevel.zone == ZoomLevel::FramesPerPixel) { Chris@1327: classicZoomValue = m_zoomLevel.level; Chris@1327: deepZoomValue = 1; Chris@1327: } else { Chris@1327: classicZoomValue = 1; Chris@1327: deepZoomValue = m_zoomLevel.level; Chris@1327: } Chris@1327: Chris@316: stream << QString("\n") Chris@1266: .arg(m_centreFrame) Chris@1327: .arg(classicZoomValue) Chris@1327: .arg(deepZoomValue) Chris@1266: .arg(m_followPan) Chris@1266: .arg(m_followZoom) Chris@1266: .arg(m_followPlay == PlaybackScrollContinuous ? "scroll" : Chris@1266: m_followPlay == PlaybackScrollPageWithCentre ? "page" : Chris@1266: m_followPlay == PlaybackScrollPage ? "daw" : Chris@815: "ignore") Chris@1266: .arg(extraAttributes); Chris@127: Chris@838: for (int i = 0; i < (int)m_fixedOrderLayers.size(); ++i) { Chris@838: bool visible = !m_fixedOrderLayers[i]->isLayerDormant(this); Chris@838: m_fixedOrderLayers[i]->toBriefXml(stream, indent + " ", Chris@838: QString("visible=\"%1\"") Chris@838: .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@1506: // SVCERR << "ViewPropertyContainer: " << getId() << " is owned by View " << v << endl; Chris@127: connect(m_v, SIGNAL(propertyChanged(PropertyContainer::PropertyName)), Chris@1266: this, SIGNAL(propertyChanged(PropertyContainer::PropertyName))); Chris@127: } Chris@127: Chris@728: ViewPropertyContainer::~ViewPropertyContainer() Chris@728: { Chris@728: }