Chris@411: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@411: Chris@411: /* Chris@411: Sonic Visualiser Chris@411: An audio file viewer and annotation editor. Chris@411: Centre for Digital Music, Queen Mary, University of London. Chris@411: This file copyright 2006-2008 Chris Cannam and QMUL. Chris@411: Chris@411: This program is free software; you can redistribute it and/or Chris@411: modify it under the terms of the GNU General Public License as Chris@411: published by the Free Software Foundation; either version 2 of the Chris@411: License, or (at your option) any later version. See the file Chris@411: COPYING included with this distribution for more information. Chris@411: */ Chris@411: Chris@411: #include "RegionLayer.h" Chris@411: Chris@411: #include "data/model/Model.h" Chris@411: #include "base/RealTime.h" Chris@411: #include "base/Profiler.h" Chris@411: #include "base/LogRange.h" Chris@1078: Chris@411: #include "ColourDatabase.h" Chris@427: #include "ColourMapper.h" Chris@701: #include "LinearNumericalScale.h" Chris@701: #include "LogNumericalScale.h" Chris@701: #include "LinearColourScale.h" Chris@701: #include "LogColourScale.h" Chris@1078: #include "PaintAssistant.h" Chris@701: Chris@411: #include "view/View.h" Chris@411: Chris@411: #include "data/model/RegionModel.h" Chris@411: Chris@411: #include "widgets/ItemEditDialog.h" Chris@701: #include "widgets/TextAbbrev.h" Chris@411: Chris@411: #include Chris@411: #include Chris@411: #include Chris@411: #include Chris@411: #include Chris@411: Chris@411: #include Chris@411: #include Chris@411: Chris@411: RegionLayer::RegionLayer() : Chris@411: SingleColourLayer(), Chris@411: m_editing(false), Chris@846: m_dragPointX(0), Chris@846: m_dragPointY(0), Chris@846: m_dragStartX(0), Chris@846: m_dragStartY(0), Chris@543: m_originalPoint(0, 0.0, 0, tr("New Region")), Chris@543: m_editingPoint(0, 0.0, 0, tr("New Region")), Chris@1408: m_editingCommand(nullptr), Chris@433: m_verticalScale(EqualSpaced), Chris@427: m_colourMap(0), Chris@1362: m_colourInverted(false), Chris@412: m_plotStyle(PlotLines) Chris@411: { Chris@411: Chris@411: } Chris@411: Chris@1470: int Chris@1470: RegionLayer::getCompletion(LayerGeometryProvider *) const Chris@1470: { Chris@1470: auto model = ModelById::get(m_model); Chris@1470: if (model) return model->getCompletion(); Chris@1470: else return 0; Chris@1470: } Chris@1470: Chris@411: void Chris@1471: RegionLayer::setModel(ModelId modelId) Chris@411: { Chris@1471: auto oldModel = ModelById::getAs(m_model); Chris@1471: auto newModel = ModelById::getAs(modelId); Chris@1471: Chris@1471: if (!modelId.isNone() && !newModel) { Chris@1471: throw std::logic_error("Not a RegionModel"); Chris@1471: } Chris@1471: Chris@1471: if (m_model == modelId) return; Chris@1471: m_model = modelId; Chris@411: Chris@1471: if (newModel) { Chris@1471: Chris@1471: connectSignals(m_model); Chris@411: Chris@1488: connect(newModel.get(), SIGNAL(modelChanged(ModelId)), Chris@1471: this, SLOT(recalcSpacing())); Chris@1471: Chris@1471: recalcSpacing(); Chris@433: Chris@1471: if (newModel->getRDFTypeURI().endsWith("Segment")) { Chris@1471: setPlotStyle(PlotSegmentation); Chris@1471: } Chris@1471: if (newModel->getRDFTypeURI().endsWith("Change")) { Chris@1471: setPlotStyle(PlotSegmentation); Chris@1471: } Chris@610: } Chris@1471: Chris@411: emit modelReplaced(); Chris@411: } Chris@411: Chris@411: Layer::PropertyList Chris@411: RegionLayer::getProperties() const Chris@411: { Chris@411: PropertyList list = SingleColourLayer::getProperties(); Chris@411: list.push_back("Vertical Scale"); Chris@411: list.push_back("Scale Units"); Chris@412: list.push_back("Plot Type"); Chris@411: return list; Chris@411: } Chris@411: Chris@411: QString Chris@411: RegionLayer::getPropertyLabel(const PropertyName &name) const Chris@411: { Chris@411: if (name == "Vertical Scale") return tr("Vertical Scale"); Chris@411: if (name == "Scale Units") return tr("Scale Units"); Chris@412: if (name == "Plot Type") return tr("Plot Type"); Chris@411: return SingleColourLayer::getPropertyLabel(name); Chris@411: } Chris@411: Chris@411: Layer::PropertyType Chris@411: RegionLayer::getPropertyType(const PropertyName &name) const Chris@411: { Chris@411: if (name == "Scale Units") return UnitsProperty; Chris@411: if (name == "Vertical Scale") return ValueProperty; Chris@412: if (name == "Plot Type") return ValueProperty; Chris@427: if (name == "Colour" && m_plotStyle == PlotSegmentation) return ValueProperty; Chris@411: return SingleColourLayer::getPropertyType(name); Chris@411: } Chris@411: Chris@411: QString Chris@411: RegionLayer::getPropertyGroupName(const PropertyName &name) const Chris@411: { Chris@411: if (name == "Vertical Scale" || name == "Scale Units") { Chris@411: return tr("Scale"); Chris@411: } Chris@411: return SingleColourLayer::getPropertyGroupName(name); Chris@411: } Chris@411: Chris@411: int Chris@411: RegionLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@411: int *min, int *max, int *deflt) const Chris@411: { Chris@411: int val = 0; Chris@411: Chris@427: if (name == "Colour" && m_plotStyle == PlotSegmentation) { Chris@427: Chris@427: if (min) *min = 0; Chris@427: if (max) *max = ColourMapper::getColourMapCount() - 1; Chris@427: if (deflt) *deflt = 0; Chris@427: Chris@427: val = m_colourMap; Chris@427: Chris@427: } else if (name == "Plot Type") { Chris@1266: Chris@1266: if (min) *min = 0; Chris@1266: if (max) *max = 1; Chris@412: if (deflt) *deflt = 0; Chris@1266: Chris@1266: val = int(m_plotStyle); Chris@412: Chris@412: } else if (name == "Vertical Scale") { Chris@1266: Chris@1266: if (min) *min = 0; Chris@1266: if (max) *max = 3; Chris@433: if (deflt) *deflt = int(EqualSpaced); Chris@1266: Chris@1266: val = int(m_verticalScale); Chris@411: Chris@411: } else if (name == "Scale Units") { Chris@411: Chris@411: if (deflt) *deflt = 0; Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (model) { Chris@411: val = UnitDatabase::getInstance()->getUnitId Chris@1471: (model->getScaleUnits()); Chris@411: } Chris@411: Chris@411: } else { Chris@411: Chris@1266: val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt); Chris@411: } Chris@411: Chris@411: return val; Chris@411: } Chris@411: Chris@411: QString Chris@411: RegionLayer::getPropertyValueLabel(const PropertyName &name, Chris@427: int value) const Chris@411: { Chris@427: if (name == "Colour" && m_plotStyle == PlotSegmentation) { Chris@1362: return ColourMapper::getColourMapLabel(value); Chris@427: } else if (name == "Plot Type") { Chris@412: Chris@1266: switch (value) { Chris@1266: default: Chris@1266: case 0: return tr("Bars"); Chris@1266: case 1: return tr("Segmentation"); Chris@1266: } Chris@412: Chris@412: } else if (name == "Vertical Scale") { Chris@1266: switch (value) { Chris@1266: default: Chris@1266: case 0: return tr("Auto-Align"); Chris@1266: case 1: return tr("Equal Spaced"); Chris@1266: case 2: return tr("Linear"); Chris@1266: case 3: return tr("Log"); Chris@1266: } Chris@411: } Chris@411: return SingleColourLayer::getPropertyValueLabel(name, value); Chris@411: } Chris@411: Chris@411: void Chris@411: RegionLayer::setProperty(const PropertyName &name, int value) Chris@411: { Chris@427: if (name == "Colour" && m_plotStyle == PlotSegmentation) { Chris@427: setFillColourMap(value); Chris@427: } else if (name == "Plot Type") { Chris@1266: setPlotStyle(PlotStyle(value)); Chris@412: } else if (name == "Vertical Scale") { Chris@1266: setVerticalScale(VerticalScale(value)); Chris@411: } else if (name == "Scale Units") { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (model) { Chris@1471: model->setScaleUnits Chris@411: (UnitDatabase::getInstance()->getUnitById(value)); Chris@1481: emit modelChanged(m_model); Chris@411: } Chris@411: } else { Chris@411: return SingleColourLayer::setProperty(name, value); Chris@411: } Chris@411: } Chris@411: Chris@411: void Chris@427: RegionLayer::setFillColourMap(int map) Chris@427: { Chris@427: if (m_colourMap == map) return; Chris@427: m_colourMap = map; Chris@427: emit layerParametersChanged(); Chris@427: } Chris@427: Chris@427: void Chris@412: RegionLayer::setPlotStyle(PlotStyle style) Chris@412: { Chris@412: if (m_plotStyle == style) return; Chris@427: bool colourTypeChanged = (style == PlotSegmentation || Chris@427: m_plotStyle == PlotSegmentation); Chris@412: m_plotStyle = style; Chris@427: if (colourTypeChanged) { Chris@427: emit layerParameterRangesChanged(); Chris@427: } Chris@412: emit layerParametersChanged(); Chris@412: } Chris@412: Chris@412: void Chris@411: RegionLayer::setVerticalScale(VerticalScale scale) Chris@411: { Chris@411: if (m_verticalScale == scale) return; Chris@411: m_verticalScale = scale; Chris@411: emit layerParametersChanged(); Chris@411: } Chris@411: Chris@411: bool Chris@918: RegionLayer::isLayerScrollable(const LayerGeometryProvider *v) const Chris@411: { Chris@411: QPoint discard; Chris@411: return !v->shouldIlluminateLocalFeatures(this, discard); Chris@411: } Chris@411: Chris@433: void Chris@433: RegionLayer::recalcSpacing() Chris@433: { Chris@433: m_spacingMap.clear(); Chris@551: m_distributionMap.clear(); Chris@1471: Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@433: Chris@587: // SVDEBUG << "RegionLayer::recalcSpacing" << endl; Chris@433: Chris@1471: EventVector allEvents = model->getAllEvents(); Chris@1428: for (const Event &e: allEvents) { Chris@1428: m_distributionMap[e.getValue()]++; Chris@1428: // SVDEBUG << "RegionLayer::recalcSpacing: value found: " << e.getValue() << " (now have " << m_distributionMap[e.getValue()] << " of this value)" << endl; Chris@433: } Chris@433: Chris@433: int n = 0; Chris@433: Chris@551: for (SpacingMap::const_iterator i = m_distributionMap.begin(); Chris@551: i != m_distributionMap.end(); ++i) { Chris@551: m_spacingMap[i->first] = n++; Chris@587: // SVDEBUG << "RegionLayer::recalcSpacing: " << i->first << " -> " << m_spacingMap[i->first] << endl; Chris@433: } Chris@433: } Chris@433: Chris@411: bool Chris@905: RegionLayer::getValueExtents(double &min, double &max, Chris@411: bool &logarithmic, QString &unit) const Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return false; Chris@1471: min = model->getValueMinimum(); Chris@1471: max = model->getValueMaximum(); Chris@701: unit = getScaleUnits(); Chris@411: Chris@411: if (m_verticalScale == LogScale) logarithmic = true; Chris@411: Chris@411: return true; Chris@411: } Chris@411: Chris@411: bool Chris@905: RegionLayer::getDisplayExtents(double &min, double &max) const Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || Chris@433: m_verticalScale == AutoAlignScale || Chris@433: m_verticalScale == EqualSpaced) return false; Chris@411: Chris@1471: min = model->getValueMinimum(); Chris@1471: max = model->getValueMaximum(); Chris@411: Chris@411: return true; Chris@411: } Chris@411: Chris@1428: EventVector Chris@918: RegionLayer::getLocalPoints(LayerGeometryProvider *v, int x) const Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return EventVector(); Chris@411: Chris@989: sv_frame_t frame = v->getFrameForX(x); Chris@411: Chris@1471: EventVector local = model->getEventsCovering(frame); Chris@1428: if (!local.empty()) return local; Chris@411: Chris@1428: int fuzz = ViewManager::scalePixelSize(2); Chris@1428: sv_frame_t start = v->getFrameForX(x - fuzz); Chris@1428: sv_frame_t end = v->getFrameForX(x + fuzz); Chris@411: Chris@1471: local = model->getEventsStartingWithin(frame, end - frame); Chris@1428: if (!local.empty()) return local; Chris@411: Chris@1471: local = model->getEventsSpanning(start, frame - start); Chris@1428: if (!local.empty()) return local; Chris@411: Chris@1428: return {}; Chris@411: } Chris@411: Chris@550: bool Chris@1428: RegionLayer::getPointToDrag(LayerGeometryProvider *v, int x, int y, Event &point) const Chris@550: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return false; Chris@550: Chris@989: sv_frame_t frame = v->getFrameForX(x); Chris@550: Chris@1471: EventVector onPoints = model->getEventsCovering(frame); Chris@550: if (onPoints.empty()) return false; Chris@550: Chris@550: int nearestDistance = -1; Chris@1428: for (const auto &p: onPoints) { Chris@1428: int distance = getYForValue(v, p.getValue()) - y; Chris@550: if (distance < 0) distance = -distance; Chris@550: if (nearestDistance == -1 || distance < nearestDistance) { Chris@550: nearestDistance = distance; Chris@1428: point = p; Chris@550: } Chris@550: } Chris@550: Chris@550: return true; Chris@550: } Chris@550: Chris@411: QString Chris@909: RegionLayer::getLabelPreceding(sv_frame_t frame) const Chris@552: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return ""; Chris@1471: EventVector points = model->getEventsStartingWithin Chris@1471: (model->getStartFrame(), frame - model->getStartFrame()); Chris@1428: if (!points.empty()) { Chris@1428: for (auto i = points.rbegin(); i != points.rend(); ++i) { Chris@1428: if (i->getLabel() != QString()) { Chris@1428: return i->getLabel(); Chris@1428: } Chris@1428: } Chris@552: } Chris@1428: return QString(); Chris@552: } Chris@552: Chris@552: QString Chris@918: RegionLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const Chris@411: { Chris@411: int x = pos.x(); Chris@411: Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !model->getSampleRate()) return ""; Chris@411: Chris@1428: EventVector points = getLocalPoints(v, x); Chris@411: Chris@411: if (points.empty()) { Chris@1471: if (!model->isReady()) { Chris@1266: return tr("In progress"); Chris@1266: } else { Chris@1266: return tr("No local points"); Chris@1266: } Chris@411: } Chris@411: Chris@1428: Event region; Chris@1428: EventVector::iterator i; Chris@411: Chris@413: //!!! harmonise with whatever decision is made about point y Chris@413: //!!! coords in paint method Chris@413: Chris@411: for (i = points.begin(); i != points.end(); ++i) { Chris@411: Chris@1428: int y = getYForValue(v, i->getValue()); Chris@1266: int h = 3; Chris@411: Chris@1471: if (model->getValueQuantization() != 0.0) { Chris@1428: h = y - getYForValue Chris@1471: (v, i->getValue() + model->getValueQuantization()); Chris@1266: if (h < 3) h = 3; Chris@1266: } Chris@411: Chris@1266: if (pos.y() >= y - h && pos.y() <= y) { Chris@1266: region = *i; Chris@1266: break; Chris@1266: } Chris@411: } Chris@411: Chris@411: if (i == points.end()) return tr("No local points"); Chris@411: Chris@1428: RealTime rt = RealTime::frame2RealTime(region.getFrame(), Chris@1471: model->getSampleRate()); Chris@1428: RealTime rd = RealTime::frame2RealTime(region.getDuration(), Chris@1471: model->getSampleRate()); Chris@411: Chris@411: QString valueText; Chris@411: Chris@1428: valueText = tr("%1 %2").arg(region.getValue()).arg(getScaleUnits()); Chris@411: Chris@411: QString text; Chris@411: Chris@1428: if (region.getLabel() == "") { Chris@1266: text = QString(tr("Time:\t%1\nValue:\t%2\nDuration:\t%3\nNo label")) Chris@1266: .arg(rt.toText(true).c_str()) Chris@1266: .arg(valueText) Chris@1266: .arg(rd.toText(true).c_str()); Chris@411: } else { Chris@1266: text = QString(tr("Time:\t%1\nValue:\t%2\nDuration:\t%3\nLabel:\t%4")) Chris@1266: .arg(rt.toText(true).c_str()) Chris@1266: .arg(valueText) Chris@1266: .arg(rd.toText(true).c_str()) Chris@1428: .arg(region.getLabel()); Chris@411: } Chris@411: Chris@1428: pos = QPoint(v->getXForFrame(region.getFrame()), Chris@1428: getYForValue(v, region.getValue())); Chris@411: return text; Chris@411: } Chris@411: Chris@411: bool Chris@918: RegionLayer::snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame, Chris@805: int &resolution, Chris@1547: SnapType snap, int ycoord) const Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) { Chris@1547: return Layer::snapToFeatureFrame(v, frame, resolution, snap, ycoord); Chris@411: } Chris@411: Chris@1432: // SnapLeft / SnapRight: return frame of nearest feature in that Chris@1432: // direction no matter how far away Chris@1432: // Chris@1432: // SnapNeighbouring: return frame of feature that would be used in Chris@1432: // an editing operation, i.e. closest feature in either direction Chris@1432: // but only if it is "close enough" Chris@1432: Chris@1471: resolution = model->getResolution(); Chris@411: Chris@411: if (snap == SnapNeighbouring) { Chris@1432: EventVector points = getLocalPoints(v, v->getXForFrame(frame)); Chris@1266: if (points.empty()) return false; Chris@1428: frame = points.begin()->getFrame(); Chris@1266: return true; Chris@411: } Chris@411: Chris@1432: // Normally we snap to the start frame of whichever event we Chris@1432: // find. However here, for SnapRight only, if the end frame of Chris@1432: // whichever event we would have snapped to had we been snapping Chris@1432: // left is closer than the start frame of the next event to the Chris@1432: // right, then we snap to that frame instead. Clear? Chris@1428: Chris@1432: Event left; Chris@1432: bool haveLeft = false; Chris@1471: if (model->getNearestEventMatching Chris@1432: (frame, [](Event) { return true; }, EventSeries::Backward, left)) { Chris@1432: haveLeft = true; Chris@1432: } Chris@411: Chris@1432: if (snap == SnapLeft) { Chris@1432: frame = left.getFrame(); Chris@1432: return haveLeft; Chris@1432: } Chris@411: Chris@1432: Event right; Chris@1432: bool haveRight = false; Chris@1471: if (model->getNearestEventMatching Chris@1432: (frame, [](Event) { return true; }, EventSeries::Forward, right)) { Chris@1432: haveRight = true; Chris@1432: } Chris@411: Chris@1432: if (haveLeft) { Chris@1432: sv_frame_t leftEnd = left.getFrame() + left.getDuration(); Chris@1432: if (leftEnd > frame) { Chris@1432: if (haveRight) { Chris@1432: if (leftEnd - frame < right.getFrame() - frame) { Chris@1432: frame = leftEnd; Chris@1432: } else { Chris@1432: frame = right.getFrame(); Chris@414: } Chris@414: } else { Chris@1432: frame = leftEnd; Chris@414: } Chris@1432: return true; Chris@1266: } Chris@411: } Chris@411: Chris@1432: if (haveRight) { Chris@1432: frame = right.getFrame(); Chris@1432: return true; Chris@1432: } Chris@1432: Chris@1432: return false; Chris@411: } Chris@411: Chris@559: bool Chris@918: RegionLayer::snapToSimilarFeature(LayerGeometryProvider *v, sv_frame_t &frame, Chris@805: int &resolution, Chris@559: SnapType snap) const Chris@559: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) { Chris@1266: return Layer::snapToSimilarFeature(v, frame, resolution, snap); Chris@559: } Chris@559: Chris@1432: // snap is only permitted to be SnapLeft or SnapRight here. We Chris@1432: // don't do the same trick as in snapToFeatureFrame, of snapping Chris@1432: // to the end of a feature sometimes. Chris@1432: Chris@1471: resolution = model->getResolution(); Chris@559: Chris@1432: Event ref; Chris@1432: Event e; Chris@1432: float matchvalue; Chris@1432: bool found; Chris@559: Chris@1471: found = model->getNearestEventMatching Chris@1432: (frame, [](Event) { return true; }, EventSeries::Backward, ref); Chris@559: Chris@1432: if (!found) { Chris@1432: return false; Chris@559: } Chris@559: Chris@1432: matchvalue = ref.getValue(); Chris@1432: Chris@1471: found = model->getNearestEventMatching Chris@1432: (frame, Chris@1432: [matchvalue](Event e) { Chris@1432: double epsilon = 0.0001; Chris@1432: return fabs(e.getValue() - matchvalue) < epsilon; Chris@1432: }, Chris@1432: snap == SnapLeft ? EventSeries::Backward : EventSeries::Forward, Chris@1432: e); Chris@559: Chris@1432: if (!found) { Chris@1432: return false; Chris@559: } Chris@559: Chris@1432: frame = e.getFrame(); Chris@1432: return true; Chris@559: } Chris@559: Chris@701: QString Chris@701: RegionLayer::getScaleUnits() const Chris@701: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (model) return model->getScaleUnits(); Chris@701: else return ""; Chris@701: } Chris@701: Chris@411: void Chris@918: RegionLayer::getScaleExtents(LayerGeometryProvider *v, double &min, double &max, bool &log) const Chris@411: { Chris@411: min = 0.0; Chris@411: max = 0.0; Chris@411: log = false; Chris@411: Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@1471: Chris@411: QString queryUnits; Chris@701: queryUnits = getScaleUnits(); Chris@411: Chris@411: if (m_verticalScale == AutoAlignScale) { Chris@411: Chris@1537: if (!v->getVisibleExtentsForUnit(queryUnits, min, max, log)) { Chris@411: Chris@1471: min = model->getValueMinimum(); Chris@1471: max = model->getValueMaximum(); Chris@411: Chris@682: // cerr << "RegionLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl; Chris@411: Chris@411: } else if (log) { Chris@411: Chris@411: LogRange::mapRange(min, max); Chris@411: Chris@682: // cerr << "RegionLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl; Chris@411: Chris@411: } Chris@411: Chris@433: } else if (m_verticalScale == EqualSpaced) { Chris@433: Chris@433: if (!m_spacingMap.empty()) { Chris@433: SpacingMap::const_iterator i = m_spacingMap.begin(); Chris@433: min = i->second; Chris@433: i = m_spacingMap.end(); Chris@433: --i; Chris@433: max = i->second; Chris@682: // cerr << "RegionLayer[" << this << "]::getScaleExtents: equal spaced; min = " << min << ", max = " << max << ", log = " << log << endl; Chris@433: } Chris@433: Chris@411: } else { Chris@411: Chris@1471: min = model->getValueMinimum(); Chris@1471: max = model->getValueMaximum(); Chris@411: Chris@411: if (m_verticalScale == LogScale) { Chris@411: LogRange::mapRange(min, max); Chris@411: log = true; Chris@411: } Chris@411: } Chris@411: Chris@411: if (max == min) max = min + 1.0; Chris@411: } Chris@411: Chris@411: int Chris@918: RegionLayer::spacingIndexToY(LayerGeometryProvider *v, int i) const Chris@542: { Chris@918: int h = v->getPaintHeight(); Chris@905: int n = int(m_spacingMap.size()); Chris@542: // this maps from i (spacing of the value from the spacing Chris@542: // map) and n (number of region types) to y Chris@542: int y = h - (((h * i) / n) + (h / (2 * n))); Chris@542: return y; Chris@542: } Chris@542: Chris@905: double Chris@918: RegionLayer::yToSpacingIndex(LayerGeometryProvider *v, int y) const Chris@542: { Chris@905: // we return an inexact result here (double rather than int) Chris@918: int h = v->getPaintHeight(); Chris@905: int n = int(m_spacingMap.size()); Chris@551: // from y = h - ((h * i) / n) + (h / (2 * n)) as above (vh taking place of i) Chris@905: double vh = double(2*h*n - h - 2*n*y) / double(2*h); Chris@542: return vh; Chris@542: } Chris@542: Chris@542: int Chris@918: RegionLayer::getYForValue(LayerGeometryProvider *v, double val) const Chris@411: { Chris@905: double min = 0.0, max = 0.0; Chris@411: bool logarithmic = false; Chris@918: int h = v->getPaintHeight(); Chris@411: Chris@433: if (m_verticalScale == EqualSpaced) { Chris@433: Chris@433: if (m_spacingMap.empty()) return h/2; Chris@433: Chris@433: SpacingMap::const_iterator i = m_spacingMap.lower_bound(val); Chris@433: //!!! what now, if i->first != v? Chris@433: Chris@542: int y = spacingIndexToY(v, i->second); Chris@433: Chris@587: // SVDEBUG << "RegionLayer::getYForValue: value " << val << " -> i->second " << i->second << " -> y " << y << endl; Chris@542: return y; Chris@433: Chris@433: Chris@433: } else { Chris@433: Chris@433: getScaleExtents(v, min, max, logarithmic); Chris@411: Chris@682: // cerr << "RegionLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << endl; Chris@682: // cerr << "h = " << h << ", margin = " << margin << endl; Chris@411: Chris@433: if (logarithmic) { Chris@433: val = LogRange::map(val); Chris@433: } Chris@433: Chris@433: return int(h - ((val - min) * h) / (max - min)); Chris@411: } Chris@411: } Chris@411: Chris@905: double Chris@918: RegionLayer::getValueForY(LayerGeometryProvider *v, int y) const Chris@701: { Chris@701: return getValueForY(v, y, -1); Chris@701: } Chris@701: Chris@905: double Chris@918: RegionLayer::getValueForY(LayerGeometryProvider *v, int y, int avoid) const Chris@542: { Chris@905: double min = 0.0, max = 0.0; Chris@542: bool logarithmic = false; Chris@918: int h = v->getPaintHeight(); Chris@542: Chris@542: if (m_verticalScale == EqualSpaced) { Chris@542: Chris@542: // if we're equal spaced, we probably want to snap to the Chris@542: // nearest item when close to it, and give some notification Chris@542: // that we're doing so Chris@542: Chris@542: if (m_spacingMap.empty()) return 1.f; Chris@542: Chris@542: // n is the number of distinct regions. if we are close to Chris@542: // one of the m/n divisions in the y scale, we should snap to Chris@542: // the value of the mth region. Chris@542: Chris@905: double vh = yToSpacingIndex(v, y); Chris@542: Chris@542: // spacings in the map are integral, so find the closest one, Chris@542: // map it back to its y coordinate, and see how far we are Chris@542: // from it Chris@542: Chris@905: int n = int(m_spacingMap.size()); Chris@905: int ivh = int(lrint(vh)); Chris@542: if (ivh < 0) ivh = 0; Chris@542: if (ivh > n-1) ivh = n-1; Chris@542: int iy = spacingIndexToY(v, ivh); Chris@542: Chris@542: int dist = iy - y; Chris@542: int gap = h / n; // between region lines Chris@542: Chris@682: // cerr << "getValueForY: y = " << y << ", vh = " << vh << ", ivh = " << ivh << " of " << n << ", iy = " << iy << ", dist = " << dist << ", gap = " << gap << endl; Chris@542: Chris@542: SpacingMap::const_iterator i = m_spacingMap.begin(); Chris@542: while (i != m_spacingMap.end()) { Chris@542: if (i->second == ivh) break; Chris@542: ++i; Chris@542: } Chris@542: if (i == m_spacingMap.end()) i = m_spacingMap.begin(); Chris@542: Chris@682: // cerr << "nearest existing value = " << i->first << " at " << iy << endl; Chris@551: Chris@905: double val = 0; Chris@542: Chris@682: // cerr << "note: avoid = " << avoid << ", i->second = " << i->second << endl; Chris@551: Chris@551: if (dist < -gap/3 && Chris@551: ((avoid == -1) || Chris@551: (avoid != i->second && avoid != i->second - 1))) { Chris@542: // bisect gap to prior Chris@542: if (i == m_spacingMap.begin()) { Chris@542: val = i->first - 1.f; Chris@682: // cerr << "extended down to " << val << endl; Chris@542: } else { Chris@542: SpacingMap::const_iterator j = i; Chris@542: --j; Chris@542: val = (i->first + j->first) / 2; Chris@682: // cerr << "bisected down to " << val << endl; Chris@542: } Chris@551: } else if (dist > gap/3 && Chris@551: ((avoid == -1) || Chris@551: (avoid != i->second && avoid != i->second + 1))) { Chris@542: // bisect gap to following Chris@542: SpacingMap::const_iterator j = i; Chris@542: ++j; Chris@542: if (j == m_spacingMap.end()) { Chris@542: val = i->first + 1.f; Chris@682: // cerr << "extended up to " << val << endl; Chris@542: } else { Chris@542: val = (i->first + j->first) / 2; Chris@682: // cerr << "bisected up to " << val << endl; Chris@542: } Chris@551: } else { Chris@551: // snap Chris@551: val = i->first; Chris@682: // cerr << "snapped to " << val << endl; Chris@542: } Chris@542: Chris@542: return val; Chris@542: Chris@542: } else { Chris@542: Chris@542: getScaleExtents(v, min, max, logarithmic); Chris@542: Chris@905: double val = min + (double(h - y) * double(max - min)) / h; Chris@542: Chris@542: if (logarithmic) { Chris@905: val = pow(10.0, val); Chris@542: } Chris@542: Chris@542: return val; Chris@542: } Chris@542: } Chris@542: Chris@427: QColor Chris@918: RegionLayer::getColourForValue(LayerGeometryProvider *v, double val) const Chris@427: { Chris@905: double min, max; Chris@427: bool log; Chris@427: getScaleExtents(v, min, max, log); Chris@427: Chris@427: if (min > max) std::swap(min, max); Chris@427: if (max == min) max = min + 1; Chris@427: Chris@427: if (log) { Chris@427: LogRange::mapRange(min, max); Chris@427: val = LogRange::map(val); Chris@427: } Chris@427: Chris@587: // SVDEBUG << "RegionLayer::getColourForValue: min " << min << ", max " Chris@585: // << max << ", log " << log << ", value " << val << endl; Chris@427: Chris@1362: QColor solid = ColourMapper(m_colourMap, m_colourInverted, min, max).map(val); Chris@427: return QColor(solid.red(), solid.green(), solid.blue(), 120); Chris@427: } Chris@427: Chris@427: int Chris@427: RegionLayer::getDefaultColourHint(bool darkbg, bool &impose) Chris@427: { Chris@427: impose = false; Chris@427: return ColourDatabase::getInstance()->getColourIndex Chris@427: (QString(darkbg ? "Bright Blue" : "Blue")); Chris@427: } Chris@427: Chris@411: void Chris@916: RegionLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !model->isOK()) return; Chris@411: Chris@1471: sv_samplerate_t sampleRate = model->getSampleRate(); Chris@411: if (!sampleRate) return; Chris@411: Chris@411: // Profiler profiler("RegionLayer::paint", true); Chris@411: Chris@1550: int x0 = rect.left() - 40; Chris@1550: int x1 = x0 + rect.width() + 80; Chris@411: Chris@1409: sv_frame_t wholeFrame0 = v->getFrameForX(0); Chris@1409: sv_frame_t wholeFrame1 = v->getFrameForX(v->getPaintWidth()); Chris@1409: Chris@1471: EventVector points(model->getEventsSpanning(wholeFrame0, Chris@1428: wholeFrame1 - wholeFrame0)); Chris@411: if (points.empty()) return; Chris@411: Chris@411: paint.setPen(getBaseQColor()); Chris@411: Chris@411: QColor brushColour(getBaseQColor()); Chris@411: brushColour.setAlpha(80); Chris@411: Chris@587: // SVDEBUG << "RegionLayer::paint: resolution is " Chris@1471: // << model->getResolution() << " frames" << endl; Chris@411: Chris@1471: double min = model->getValueMinimum(); Chris@1471: double max = model->getValueMaximum(); Chris@411: if (max == min) max = min + 1.0; Chris@411: Chris@411: QPoint localPos; Chris@1428: Event illuminatePoint(0); Chris@551: bool shouldIlluminate = false; Chris@411: Chris@411: if (v->shouldIlluminateLocalFeatures(this, localPos)) { Chris@551: shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(), Chris@551: illuminatePoint); Chris@411: } Chris@411: Chris@411: paint.save(); Chris@411: paint.setRenderHint(QPainter::Antialiasing, false); Chris@411: Chris@413: //!!! point y coords if model does not haveDistinctValues() should Chris@413: //!!! be assigned to avoid overlaps Chris@413: Chris@413: //!!! if it does have distinct values, we should still ensure y Chris@413: //!!! coord is never completely flat on the top or bottom Chris@413: Chris@550: int fontHeight = paint.fontMetrics().height(); Chris@550: Chris@1428: for (EventVector::const_iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@411: Chris@1428: const Event &p(*i); Chris@411: Chris@1428: int x = v->getXForFrame(p.getFrame()); Chris@1428: int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x; Chris@1428: int y = getYForValue(v, p.getValue()); Chris@1266: int h = 9; Chris@1266: int ex = x + w; Chris@427: Chris@1409: int gap = v->scalePixelSize(2); Chris@1409: Chris@1428: EventVector::const_iterator j = i; Chris@1266: ++j; Chris@427: Chris@1266: if (j != points.end()) { Chris@1428: const Event &q(*j); Chris@1428: int nx = v->getXForFrame(q.getFrame()); Chris@433: if (nx < ex) ex = nx; Chris@427: } Chris@427: Chris@1471: if (model->getValueQuantization() != 0.0) { Chris@1428: h = y - getYForValue Chris@1471: (v, p.getValue() + model->getValueQuantization()); Chris@1266: if (h < 3) h = 3; Chris@1266: } Chris@411: Chris@1266: if (w < 1) w = 1; Chris@411: Chris@1266: if (m_plotStyle == PlotSegmentation) { Chris@918: paint.setPen(getForegroundQColor(v->getView())); Chris@1428: paint.setBrush(getColourForValue(v, p.getValue())); Chris@427: } else { Chris@427: paint.setPen(getBaseQColor()); Chris@427: paint.setBrush(brushColour); Chris@427: } Chris@427: Chris@1266: if (m_plotStyle == PlotSegmentation) { Chris@427: Chris@1266: if (ex <= x) continue; Chris@427: Chris@1428: if (!shouldIlluminate || illuminatePoint != p) { Chris@551: Chris@918: paint.setPen(QPen(getForegroundQColor(v->getView()), 1)); Chris@918: paint.drawLine(x, 0, x, v->getPaintHeight()); Chris@551: paint.setPen(Qt::NoPen); Chris@552: Chris@552: } else { Chris@918: paint.setPen(QPen(getForegroundQColor(v->getView()), 2)); Chris@551: } Chris@427: Chris@1409: paint.drawRect(x, -1, ex - x, v->getPaintHeight() + gap); Chris@427: Chris@1266: } else { Chris@427: Chris@1428: if (shouldIlluminate && illuminatePoint == p) { Chris@551: Chris@551: paint.setPen(v->getForeground()); Chris@551: paint.setBrush(v->getForeground()); Chris@551: Chris@1471: // Qt 5.13 deprecates QFontMetrics::width(), but its suggested Chris@1471: // replacement (horizontalAdvance) was only added in Qt 5.11 Chris@1471: // which is too new for us Chris@1471: #pragma GCC diagnostic ignored "-Wdeprecated-declarations" Chris@1471: Chris@1428: QString vlabel = Chris@1428: QString("%1%2").arg(p.getValue()).arg(getScaleUnits()); Chris@1078: PaintAssistant::drawVisibleText(v, paint, Chris@1409: x - paint.fontMetrics().width(vlabel) - gap, Chris@551: y + paint.fontMetrics().height()/2 Chris@551: - paint.fontMetrics().descent(), Chris@1078: vlabel, PaintAssistant::OutlinedText); Chris@551: Chris@551: QString hlabel = RealTime::frame2RealTime Chris@1471: (p.getFrame(), model->getSampleRate()).toText(true).c_str(); Chris@1078: PaintAssistant::drawVisibleText(v, paint, Chris@551: x, Chris@1409: y - h/2 - paint.fontMetrics().descent() - gap, Chris@1078: hlabel, PaintAssistant::OutlinedText); Chris@427: } Chris@551: Chris@427: paint.drawLine(x, y-1, x + w, y-1); Chris@427: paint.drawLine(x, y+1, x + w, y+1); Chris@427: paint.drawLine(x, y - h/2, x, y + h/2); Chris@427: paint.drawLine(x+w, y - h/2, x + w, y + h/2); Chris@427: } Chris@552: } Chris@552: Chris@552: int nextLabelMinX = -100; Chris@552: int lastLabelY = 0; Chris@552: Chris@1428: for (EventVector::const_iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@552: Chris@1428: const Event &p(*i); Chris@552: Chris@1428: int x = v->getXForFrame(p.getFrame()); Chris@1428: int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x; Chris@1428: int y = getYForValue(v, p.getValue()); Chris@552: Chris@1428: QString label = p.getLabel(); Chris@1409: if (label == "") { Chris@1428: label = QString("%1%2").arg(p.getValue()).arg(getScaleUnits()); Chris@1409: } Chris@1409: int labelWidth = paint.fontMetrics().width(label); Chris@1409: Chris@1409: int gap = v->scalePixelSize(2); Chris@1409: Chris@1409: if (m_plotStyle == PlotSegmentation) { Chris@1409: if ((x + w < x0 && x + labelWidth + gap < x0) || x > x1) { Chris@1409: continue; Chris@1409: } Chris@1409: } else { Chris@1409: if (x + w < x0 || x - labelWidth - gap > x1) { Chris@1409: continue; Chris@1409: } Chris@1409: } Chris@1409: Chris@552: bool illuminated = false; Chris@552: Chris@1266: if (m_plotStyle != PlotSegmentation) { Chris@1428: if (shouldIlluminate && illuminatePoint == p) { Chris@552: illuminated = true; Chris@552: } Chris@552: } Chris@427: Chris@551: if (!illuminated) { Chris@551: Chris@552: int labelX, labelY; Chris@552: Chris@551: if (m_plotStyle != PlotSegmentation) { Chris@1409: labelX = x - labelWidth - gap; Chris@552: labelY = y + paint.fontMetrics().height()/2 Chris@552: - paint.fontMetrics().descent(); Chris@551: } else { Chris@552: labelX = x + 5; Chris@1537: labelY = v->getTextLabelYCoord(this, paint); Chris@552: if (labelX < nextLabelMinX) { Chris@918: if (lastLabelY < v->getPaintHeight()/2) { Chris@552: labelY = lastLabelY + fontHeight; Chris@552: } Chris@552: } Chris@552: lastLabelY = labelY; Chris@1409: nextLabelMinX = labelX + labelWidth; Chris@551: } Chris@552: Chris@1409: PaintAssistant::drawVisibleText(v, paint, labelX, labelY, label, Chris@1409: PaintAssistant::OutlinedText); Chris@550: } Chris@411: } Chris@411: Chris@411: paint.restore(); Chris@411: } Chris@411: Chris@701: int Chris@918: RegionLayer::getVerticalScaleWidth(LayerGeometryProvider *v, bool, QPainter &paint) const Chris@701: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || Chris@701: m_verticalScale == AutoAlignScale || Chris@701: m_verticalScale == EqualSpaced) { Chris@701: return 0; Chris@701: } else if (m_plotStyle == PlotSegmentation) { Chris@701: if (m_verticalScale == LogScale) { Chris@701: return LogColourScale().getWidth(v, paint); Chris@701: } else { Chris@701: return LinearColourScale().getWidth(v, paint); Chris@701: } Chris@701: } else { Chris@701: if (m_verticalScale == LogScale) { Chris@701: return LogNumericalScale().getWidth(v, paint); Chris@701: } else { Chris@701: return LinearNumericalScale().getWidth(v, paint); Chris@701: } Chris@701: } Chris@701: } Chris@701: Chris@701: void Chris@918: RegionLayer::paintVerticalScale(LayerGeometryProvider *v, bool, QPainter &paint, QRect) const Chris@701: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || model->isEmpty()) return; Chris@701: Chris@701: QString unit; Chris@905: double min, max; Chris@701: bool logarithmic; Chris@701: Chris@701: int w = getVerticalScaleWidth(v, false, paint); Chris@701: Chris@701: if (m_plotStyle == PlotSegmentation) { Chris@701: Chris@701: getValueExtents(min, max, logarithmic, unit); Chris@701: Chris@701: if (logarithmic) { Chris@701: LogRange::mapRange(min, max); Chris@701: LogColourScale().paintVertical(v, this, paint, 0, min, max); Chris@701: } else { Chris@701: LinearColourScale().paintVertical(v, this, paint, 0, min, max); Chris@701: } Chris@701: Chris@701: } else { Chris@701: Chris@701: getScaleExtents(v, min, max, logarithmic); Chris@701: Chris@701: if (logarithmic) { Chris@701: LogNumericalScale().paintVertical(v, this, paint, 0, min, max); Chris@701: } else { Chris@701: LinearNumericalScale().paintVertical(v, this, paint, 0, min, max); Chris@701: } Chris@701: } Chris@701: Chris@701: if (getScaleUnits() != "") { Chris@701: int mw = w - 5; Chris@701: paint.drawText(5, Chris@701: 5 + paint.fontMetrics().ascent(), Chris@701: TextAbbrev::abbreviate(getScaleUnits(), Chris@701: paint.fontMetrics(), Chris@701: mw)); Chris@701: } Chris@701: } Chris@701: Chris@411: void Chris@918: RegionLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@411: Chris@989: sv_frame_t frame = v->getFrameForX(e->x()); Chris@411: if (frame < 0) frame = 0; Chris@1471: frame = frame / model->getResolution() * model->getResolution(); Chris@411: Chris@905: double value = getValueForY(v, e->y()); Chris@411: Chris@1428: m_editingPoint = Event(frame, float(value), 0, ""); Chris@411: m_originalPoint = m_editingPoint; Chris@411: Chris@411: if (m_editingCommand) finish(m_editingCommand); Chris@1471: m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Draw Region")); Chris@1428: m_editingCommand->add(m_editingPoint); Chris@411: Chris@550: recalcSpacing(); Chris@550: Chris@411: m_editing = true; Chris@411: } Chris@411: Chris@411: void Chris@918: RegionLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !m_editing) return; Chris@411: Chris@905: sv_frame_t frame = v->getFrameForX(e->x()); Chris@411: if (frame < 0) frame = 0; Chris@1471: frame = frame / model->getResolution() * model->getResolution(); Chris@411: Chris@1428: double newValue = m_editingPoint.getValue(); Chris@542: if (m_verticalScale != EqualSpaced) newValue = getValueForY(v, e->y()); Chris@411: Chris@1428: sv_frame_t newFrame = m_editingPoint.getFrame(); Chris@905: sv_frame_t newDuration = frame - newFrame; Chris@411: if (newDuration < 0) { Chris@411: newFrame = frame; Chris@411: newDuration = -newDuration; Chris@411: } else if (newDuration == 0) { Chris@411: newDuration = 1; Chris@411: } Chris@411: Chris@1428: m_editingCommand->remove(m_editingPoint); Chris@1428: m_editingPoint = m_editingPoint Chris@1428: .withFrame(newFrame) Chris@1428: .withValue(float(newValue)) Chris@1428: .withDuration(newDuration); Chris@1428: m_editingCommand->add(m_editingPoint); Chris@550: Chris@551: recalcSpacing(); Chris@411: } Chris@411: Chris@411: void Chris@918: RegionLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !m_editing) return; Chris@411: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@411: m_editing = false; Chris@550: Chris@550: recalcSpacing(); Chris@411: } Chris@411: Chris@411: void Chris@918: RegionLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@411: Chris@550: if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return; Chris@411: Chris@411: if (m_editingCommand) { Chris@1266: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@411: } Chris@411: Chris@411: m_editing = true; Chris@550: recalcSpacing(); Chris@411: } Chris@411: Chris@411: void Chris@918: RegionLayer::eraseDrag(LayerGeometryProvider *, QMouseEvent *) Chris@411: { Chris@411: } Chris@411: Chris@411: void Chris@918: RegionLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !m_editing) return; Chris@411: Chris@411: m_editing = false; Chris@551: Chris@1428: Event p(0); Chris@550: if (!getPointToDrag(v, e->x(), e->y(), p)) return; Chris@1428: if (p.getFrame() != m_editingPoint.getFrame() || Chris@1428: p.getValue() != m_editingPoint.getValue()) return; Chris@411: Chris@1428: m_editingCommand = new ChangeEventsCommand Chris@1471: (m_model.untyped, tr("Erase Region")); Chris@411: Chris@1428: m_editingCommand->remove(m_editingPoint); Chris@411: Chris@411: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@411: m_editing = false; Chris@550: recalcSpacing(); Chris@411: } Chris@411: Chris@411: void Chris@918: RegionLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@411: Chris@550: if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) { Chris@550: return; Chris@550: } Chris@550: Chris@1428: m_dragPointX = v->getXForFrame(m_editingPoint.getFrame()); Chris@1428: m_dragPointY = getYForValue(v, m_editingPoint.getValue()); Chris@551: Chris@411: m_originalPoint = m_editingPoint; Chris@411: Chris@411: if (m_editingCommand) { Chris@1266: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@411: } Chris@411: Chris@411: m_editing = true; Chris@551: m_dragStartX = e->x(); Chris@551: m_dragStartY = e->y(); Chris@550: recalcSpacing(); Chris@411: } Chris@411: Chris@411: void Chris@918: RegionLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !m_editing) return; Chris@411: Chris@551: int xdist = e->x() - m_dragStartX; Chris@551: int ydist = e->y() - m_dragStartY; Chris@551: int newx = m_dragPointX + xdist; Chris@551: int newy = m_dragPointY + ydist; Chris@551: Chris@989: sv_frame_t frame = v->getFrameForX(newx); Chris@411: if (frame < 0) frame = 0; Chris@1471: frame = frame / model->getResolution() * model->getResolution(); Chris@411: Chris@551: // Do not bisect between two values, if one of those values is Chris@551: // that of the point we're actually moving ... Chris@1428: int avoid = m_spacingMap[m_editingPoint.getValue()]; Chris@551: Chris@551: // ... unless there are other points with the same value Chris@1428: if (m_distributionMap[m_editingPoint.getValue()] > 1) avoid = -1; Chris@551: Chris@905: double value = getValueForY(v, newy, avoid); Chris@411: Chris@411: if (!m_editingCommand) { Chris@1471: m_editingCommand = new ChangeEventsCommand(m_model.untyped, Chris@1266: tr("Drag Region")); Chris@411: } Chris@411: Chris@1428: m_editingCommand->remove(m_editingPoint); Chris@1428: m_editingPoint = m_editingPoint Chris@1428: .withFrame(frame) Chris@1428: .withValue(float(value)); Chris@1428: m_editingCommand->add(m_editingPoint); Chris@550: recalcSpacing(); Chris@411: } Chris@411: Chris@411: void Chris@918: RegionLayer::editEnd(LayerGeometryProvider *, QMouseEvent *) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !m_editing) return; Chris@411: Chris@411: if (m_editingCommand) { Chris@411: Chris@1266: QString newName = m_editingCommand->getName(); Chris@411: Chris@1428: if (m_editingPoint.getFrame() != m_originalPoint.getFrame()) { Chris@1428: if (m_editingPoint.getValue() != m_originalPoint.getValue()) { Chris@1266: newName = tr("Edit Region"); Chris@1266: } else { Chris@1266: newName = tr("Relocate Region"); Chris@1266: } Chris@1266: } else { Chris@1266: newName = tr("Change Point Value"); Chris@1266: } Chris@411: Chris@1266: m_editingCommand->setName(newName); Chris@1266: finish(m_editingCommand); Chris@411: } Chris@411: Chris@1408: m_editingCommand = nullptr; Chris@411: m_editing = false; Chris@550: recalcSpacing(); Chris@411: } Chris@411: Chris@411: bool Chris@918: RegionLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return false; Chris@411: Chris@1428: Event region(0); Chris@550: if (!getPointToDrag(v, e->x(), e->y(), region)) return false; Chris@550: Chris@411: ItemEditDialog *dialog = new ItemEditDialog Chris@1471: (model->getSampleRate(), Chris@411: ItemEditDialog::ShowTime | Chris@411: ItemEditDialog::ShowDuration | Chris@411: ItemEditDialog::ShowValue | Chris@411: ItemEditDialog::ShowText, Chris@701: getScaleUnits()); Chris@411: Chris@1428: dialog->setFrameTime(region.getFrame()); Chris@1428: dialog->setValue(region.getValue()); Chris@1428: dialog->setFrameDuration(region.getDuration()); Chris@1428: dialog->setText(region.getLabel()); Chris@411: Chris@411: if (dialog->exec() == QDialog::Accepted) { Chris@411: Chris@1428: Event newRegion = region Chris@1428: .withFrame(dialog->getFrameTime()) Chris@1428: .withValue(dialog->getValue()) Chris@1428: .withDuration(dialog->getFrameDuration()) Chris@1428: .withLabel(dialog->getText()); Chris@411: Chris@1428: ChangeEventsCommand *command = new ChangeEventsCommand Chris@1471: (m_model.untyped, tr("Edit Region")); Chris@1428: command->remove(region); Chris@1428: command->add(newRegion); Chris@411: finish(command); Chris@411: } Chris@411: Chris@411: delete dialog; Chris@550: recalcSpacing(); Chris@411: return true; Chris@411: } Chris@411: Chris@411: void Chris@905: RegionLayer::moveSelection(Selection s, sv_frame_t newStartFrame) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@411: Chris@1428: ChangeEventsCommand *command = Chris@1471: new ChangeEventsCommand(m_model.untyped, tr("Drag Selection")); Chris@411: Chris@1428: EventVector points = Chris@1471: model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); Chris@411: Chris@1428: for (EventVector::iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@411: Chris@1429: Event newPoint = (*i) Chris@1429: .withFrame(i->getFrame() + newStartFrame - s.getStartFrame()); Chris@1429: command->remove(*i); Chris@1429: command->add(newPoint); Chris@411: } Chris@411: Chris@411: finish(command); Chris@550: recalcSpacing(); Chris@411: } Chris@411: Chris@411: void Chris@411: RegionLayer::resizeSelection(Selection s, Selection newSize) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !s.getDuration()) return; Chris@411: Chris@1428: ChangeEventsCommand *command = Chris@1471: new ChangeEventsCommand(m_model.untyped, tr("Resize Selection")); Chris@411: Chris@1428: EventVector points = Chris@1471: model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); Chris@411: Chris@1428: double ratio = double(newSize.getDuration()) / double(s.getDuration()); Chris@1428: double oldStart = double(s.getStartFrame()); Chris@1428: double newStart = double(newSize.getStartFrame()); Chris@1428: Chris@1428: for (Event p: points) { Chris@411: Chris@1428: double newFrame = (double(p.getFrame()) - oldStart) * ratio + newStart; Chris@1428: double newDuration = double(p.getDuration()) * ratio; Chris@411: Chris@1428: Event newPoint = p Chris@1428: .withFrame(lrint(newFrame)) Chris@1428: .withDuration(lrint(newDuration)); Chris@1428: command->remove(p); Chris@1428: command->add(newPoint); Chris@411: } Chris@411: Chris@411: finish(command); Chris@550: recalcSpacing(); Chris@411: } Chris@411: Chris@411: void Chris@411: RegionLayer::deleteSelection(Selection s) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@411: Chris@1428: ChangeEventsCommand *command = Chris@1471: new ChangeEventsCommand(m_model.untyped, tr("Delete Selected Points")); Chris@411: Chris@1428: EventVector points = Chris@1471: model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); Chris@411: Chris@1428: for (EventVector::iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@411: Chris@1428: if (s.contains(i->getFrame())) { Chris@1428: command->remove(*i); Chris@411: } Chris@411: } Chris@411: Chris@411: finish(command); Chris@550: recalcSpacing(); Chris@411: } Chris@411: Chris@411: void Chris@918: RegionLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@411: Chris@1428: EventVector points = Chris@1471: model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); Chris@411: Chris@1428: for (Event p: points) { Chris@1428: to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame()))); Chris@411: } Chris@411: } Chris@411: Chris@411: bool Chris@1533: RegionLayer::paste(LayerGeometryProvider *v, const Clipboard &from, Chris@1533: sv_frame_t /* frameOffset */, bool /* interactive */) Chris@411: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return false; Chris@411: Chris@1423: const EventVector &points = from.getPoints(); Chris@411: Chris@411: bool realign = false; Chris@411: Chris@411: if (clipboardHasDifferentAlignment(v, from)) { Chris@411: Chris@411: QMessageBox::StandardButton button = Chris@918: QMessageBox::question(v->getView(), tr("Re-align pasted items?"), Chris@411: tr("The items you are pasting came from a layer with different source material from this one. Do you want to re-align them in time, to match the source material for this layer?"), Chris@411: QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, Chris@411: QMessageBox::Yes); Chris@411: Chris@411: if (button == QMessageBox::Cancel) { Chris@411: return false; Chris@411: } Chris@411: Chris@411: if (button == QMessageBox::Yes) { Chris@411: realign = true; Chris@411: } Chris@411: } Chris@411: Chris@1428: ChangeEventsCommand *command = Chris@1471: new ChangeEventsCommand(m_model.untyped, tr("Paste")); Chris@411: Chris@1423: for (EventVector::const_iterator i = points.begin(); Chris@411: i != points.end(); ++i) { Chris@411: Chris@905: sv_frame_t frame = 0; Chris@411: Chris@411: if (!realign) { Chris@411: Chris@411: frame = i->getFrame(); Chris@411: Chris@411: } else { Chris@411: Chris@1423: if (i->hasReferenceFrame()) { Chris@411: frame = i->getReferenceFrame(); Chris@411: frame = alignFromReference(v, frame); Chris@411: } else { Chris@411: frame = i->getFrame(); Chris@411: } Chris@411: } Chris@411: Chris@1533: Event p = i->withFrame(frame); Chris@1533: Chris@1428: Event newPoint = p; Chris@1428: if (!p.hasValue()) { Chris@1471: newPoint = newPoint.withValue((model->getValueMinimum() + Chris@1471: model->getValueMaximum()) / 2); Chris@1428: } Chris@1428: if (!p.hasDuration()) { Chris@905: sv_frame_t nextFrame = frame; Chris@1423: EventVector::const_iterator j = i; Chris@411: for (; j != points.end(); ++j) { Chris@411: if (j != i) break; Chris@411: } Chris@411: if (j != points.end()) { Chris@411: nextFrame = j->getFrame(); Chris@411: } Chris@411: if (nextFrame == frame) { Chris@1471: newPoint = newPoint.withDuration(model->getResolution()); Chris@411: } else { Chris@1428: newPoint = newPoint.withDuration(nextFrame - frame); Chris@411: } Chris@411: } Chris@411: Chris@1428: command->add(newPoint); Chris@411: } Chris@411: Chris@411: finish(command); Chris@550: recalcSpacing(); Chris@411: return true; Chris@411: } Chris@411: Chris@411: void Chris@411: RegionLayer::toXml(QTextStream &stream, Chris@411: QString indent, QString extraAttributes) const Chris@411: { Chris@1362: QString s; Chris@1362: Chris@1362: s += QString("verticalScale=\"%1\" " Chris@1362: "plotStyle=\"%2\" ") Chris@1362: .arg(m_verticalScale) Chris@1362: .arg(m_plotStyle); Chris@1362: Chris@1362: // New-style colour map attribute, by string id rather than by Chris@1362: // number Chris@1362: Chris@1362: s += QString("fillColourMap=\"%1\" ") Chris@1362: .arg(ColourMapper::getColourMapId(m_colourMap)); Chris@1362: Chris@1362: // Old-style colour map attribute Chris@1362: Chris@1362: s += QString("colourMap=\"%1\" ") Chris@1362: .arg(ColourMapper::getBackwardCompatibilityColourMap(m_colourMap)); Chris@1362: Chris@1362: SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s); Chris@411: } Chris@411: Chris@411: void Chris@411: RegionLayer::setProperties(const QXmlAttributes &attributes) Chris@411: { Chris@411: SingleColourLayer::setProperties(attributes); Chris@411: Chris@411: bool ok; Chris@411: VerticalScale scale = (VerticalScale) Chris@1266: attributes.value("verticalScale").toInt(&ok); Chris@411: if (ok) setVerticalScale(scale); Chris@412: PlotStyle style = (PlotStyle) Chris@1266: attributes.value("plotStyle").toInt(&ok); Chris@412: if (ok) setPlotStyle(style); Chris@1362: Chris@1362: QString colourMapId = attributes.value("fillColourMap"); Chris@1362: int colourMap = ColourMapper::getColourMapById(colourMapId); Chris@1362: if (colourMap >= 0) { Chris@1362: setFillColourMap(colourMap); Chris@1362: } else { Chris@1362: colourMap = attributes.value("colourMap").toInt(&ok); Chris@1362: if (ok && colourMap < ColourMapper::getColourMapCount()) { Chris@1362: setFillColourMap(colourMap); Chris@1362: } Chris@1362: } Chris@411: } Chris@411: Chris@411: