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_model(0), 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@411: m_editingCommand(0), 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@411: void Chris@411: RegionLayer::setModel(RegionModel *model) Chris@411: { Chris@411: if (m_model == model) return; Chris@411: m_model = model; Chris@411: Chris@411: connectSignals(m_model); Chris@411: Chris@433: connect(m_model, SIGNAL(modelChanged()), this, SLOT(recalcSpacing())); Chris@433: recalcSpacing(); Chris@433: Chris@587: // SVDEBUG << "RegionLayer::setModel(" << model << ")" << endl; Chris@411: Chris@610: if (m_model && m_model->getRDFTypeURI().endsWith("Segment")) { Chris@610: setPlotStyle(PlotSegmentation); Chris@610: } Chris@610: if (m_model && m_model->getRDFTypeURI().endsWith("Change")) { Chris@610: setPlotStyle(PlotSegmentation); Chris@610: } Chris@610: 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@411: if (m_model) { Chris@411: val = UnitDatabase::getInstance()->getUnitId Chris@701: (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@411: if (m_model) { Chris@411: m_model->setScaleUnits Chris@411: (UnitDatabase::getInstance()->getUnitById(value)); Chris@411: emit modelChanged(); 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@433: if (!m_model) return; Chris@433: Chris@587: // SVDEBUG << "RegionLayer::recalcSpacing" << endl; Chris@433: Chris@433: for (RegionModel::PointList::const_iterator i = m_model->getPoints().begin(); Chris@433: i != m_model->getPoints().end(); ++i) { Chris@551: m_distributionMap[i->value]++; Chris@587: // SVDEBUG << "RegionLayer::recalcSpacing: value found: " << i->value << " (now have " << m_distributionMap[i->value] << " 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@411: if (!m_model) return false; Chris@411: min = m_model->getValueMinimum(); Chris@411: max = m_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@433: if (!m_model || Chris@433: m_verticalScale == AutoAlignScale || Chris@433: m_verticalScale == EqualSpaced) return false; Chris@411: Chris@411: min = m_model->getValueMinimum(); Chris@411: max = m_model->getValueMaximum(); Chris@411: Chris@411: return true; Chris@411: } Chris@411: Chris@411: RegionModel::PointList Chris@918: RegionLayer::getLocalPoints(LayerGeometryProvider *v, int x) const Chris@411: { Chris@411: if (!m_model) return RegionModel::PointList(); Chris@411: Chris@989: sv_frame_t frame = v->getFrameForX(x); Chris@411: Chris@411: RegionModel::PointList onPoints = Chris@1266: m_model->getPoints(frame); Chris@411: Chris@411: if (!onPoints.empty()) { Chris@1266: return onPoints; Chris@411: } Chris@411: Chris@411: RegionModel::PointList prevPoints = Chris@1266: m_model->getPreviousPoints(frame); Chris@411: RegionModel::PointList nextPoints = Chris@1266: m_model->getNextPoints(frame); Chris@411: Chris@411: RegionModel::PointList usePoints = prevPoints; Chris@411: Chris@411: if (prevPoints.empty()) { Chris@1266: usePoints = nextPoints; Chris@411: } else if (long(prevPoints.begin()->frame) < v->getStartFrame() && Chris@1266: !(nextPoints.begin()->frame > v->getEndFrame())) { Chris@1266: usePoints = nextPoints; Chris@411: } else if (long(nextPoints.begin()->frame) - frame < Chris@1266: frame - long(prevPoints.begin()->frame)) { Chris@1266: usePoints = nextPoints; Chris@411: } Chris@411: Chris@411: if (!usePoints.empty()) { Chris@1391: int fuzz = ViewManager::scalePixelSize(2); Chris@1266: int px = v->getXForFrame(usePoints.begin()->frame); Chris@1266: if ((px > x && px - x > fuzz) || Chris@1266: (px < x && x - px > fuzz + 1)) { Chris@1266: usePoints.clear(); Chris@1266: } Chris@411: } Chris@411: Chris@411: return usePoints; Chris@411: } Chris@411: Chris@550: bool Chris@918: RegionLayer::getPointToDrag(LayerGeometryProvider *v, int x, int y, RegionModel::Point &p) const Chris@550: { Chris@550: if (!m_model) return false; Chris@550: Chris@989: sv_frame_t frame = v->getFrameForX(x); Chris@550: Chris@550: RegionModel::PointList onPoints = m_model->getPoints(frame); Chris@550: if (onPoints.empty()) return false; Chris@550: Chris@550: int nearestDistance = -1; Chris@550: Chris@550: for (RegionModel::PointList::const_iterator i = onPoints.begin(); Chris@550: i != onPoints.end(); ++i) { Chris@550: Chris@550: int distance = getYForValue(v, (*i).value) - y; Chris@550: if (distance < 0) distance = -distance; Chris@550: if (nearestDistance == -1 || distance < nearestDistance) { Chris@550: nearestDistance = distance; Chris@550: p = *i; 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@552: if (!m_model) return ""; Chris@552: RegionModel::PointList points = m_model->getPreviousPoints(frame); Chris@552: for (RegionModel::PointList::const_iterator i = points.begin(); Chris@552: i != points.end(); ++i) { Chris@552: if (i->label != "") return i->label; Chris@552: } Chris@552: return ""; 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@411: if (!m_model || !m_model->getSampleRate()) return ""; Chris@411: Chris@411: RegionModel::PointList points = getLocalPoints(v, x); Chris@411: Chris@411: if (points.empty()) { Chris@1266: if (!m_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@411: RegionRec region(0); Chris@411: RegionModel::PointList::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@1266: int y = getYForValue(v, i->value); Chris@1266: int h = 3; Chris@411: Chris@1266: if (m_model->getValueQuantization() != 0.0) { Chris@1266: h = y - getYForValue(v, i->value + m_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@411: RealTime rt = RealTime::frame2RealTime(region.frame, Chris@1266: m_model->getSampleRate()); Chris@411: RealTime rd = RealTime::frame2RealTime(region.duration, Chris@1266: m_model->getSampleRate()); Chris@411: Chris@411: QString valueText; Chris@411: Chris@701: valueText = tr("%1 %2").arg(region.value).arg(getScaleUnits()); Chris@411: Chris@411: QString text; Chris@411: Chris@411: if (region.label == "") { 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@1266: .arg(region.label); Chris@411: } Chris@411: Chris@411: pos = QPoint(v->getXForFrame(region.frame), Chris@1266: getYForValue(v, region.value)); 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@414: SnapType snap) const Chris@411: { Chris@411: if (!m_model) { Chris@1266: return Layer::snapToFeatureFrame(v, frame, resolution, snap); Chris@411: } Chris@411: Chris@411: resolution = m_model->getResolution(); Chris@411: RegionModel::PointList points; Chris@411: Chris@411: if (snap == SnapNeighbouring) { Chris@1266: Chris@1266: points = getLocalPoints(v, v->getXForFrame(frame)); Chris@1266: if (points.empty()) return false; Chris@1266: frame = points.begin()->frame; Chris@1266: return true; Chris@411: } Chris@411: Chris@411: points = m_model->getPoints(frame, frame); Chris@905: sv_frame_t snapped = frame; Chris@411: bool found = false; Chris@411: Chris@411: for (RegionModel::PointList::const_iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@411: Chris@1266: if (snap == SnapRight) { Chris@411: Chris@414: // The best frame to snap to is the end frame of whichever Chris@414: // feature we would have snapped to the start frame of if Chris@414: // we had been snapping left. Chris@414: Chris@1266: if (i->frame <= frame) { Chris@414: if (i->frame + i->duration > frame) { Chris@414: snapped = i->frame + i->duration; Chris@414: found = true; // don't break, as the next may be better Chris@414: } Chris@414: } else { Chris@414: if (!found) { Chris@414: snapped = i->frame; Chris@414: found = true; Chris@414: } Chris@414: break; Chris@414: } Chris@411: Chris@1266: } else if (snap == SnapLeft) { Chris@411: Chris@1266: if (i->frame <= frame) { Chris@1266: snapped = i->frame; Chris@1266: found = true; // don't break, as the next may be better Chris@1266: } else { Chris@1266: break; Chris@1266: } Chris@411: Chris@1266: } else { // nearest Chris@411: Chris@1266: RegionModel::PointList::const_iterator j = i; Chris@1266: ++j; Chris@411: Chris@1266: if (j == points.end()) { Chris@411: Chris@1266: snapped = i->frame; Chris@1266: found = true; Chris@1266: break; Chris@411: Chris@1266: } else if (j->frame >= frame) { Chris@411: Chris@1266: if (j->frame - frame < frame - i->frame) { Chris@1266: snapped = j->frame; Chris@1266: } else { Chris@1266: snapped = i->frame; Chris@1266: } Chris@1266: found = true; Chris@1266: break; Chris@1266: } Chris@1266: } Chris@411: } Chris@411: Chris@411: frame = snapped; Chris@411: return found; 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@559: if (!m_model) { Chris@1266: return Layer::snapToSimilarFeature(v, frame, resolution, snap); Chris@559: } Chris@559: Chris@559: resolution = m_model->getResolution(); Chris@559: Chris@559: const RegionModel::PointList &points = m_model->getPoints(); Chris@559: RegionModel::PointList close = m_model->getPoints(frame, frame); Chris@559: Chris@559: RegionModel::PointList::const_iterator i; Chris@559: Chris@905: sv_frame_t matchframe = frame; Chris@905: double matchvalue = 0.f; Chris@559: Chris@559: for (i = close.begin(); i != close.end(); ++i) { Chris@559: if (i->frame > frame) break; Chris@559: matchvalue = i->value; Chris@559: matchframe = i->frame; Chris@559: } Chris@559: Chris@905: sv_frame_t snapped = frame; Chris@559: bool found = false; Chris@559: bool distant = false; Chris@905: double epsilon = 0.0001; Chris@559: Chris@559: i = close.begin(); Chris@559: Chris@559: // Scan through the close points first, then the more distant ones Chris@846: // if no suitable close one is found. So the while-termination Chris@846: // condition here can only happen once i has passed through the Chris@846: // whole of the close container and then the whole of the separate Chris@846: // points container. The two iterators are totally distinct, but Chris@846: // have the same type so we cheekily use the same variable and a Chris@846: // single loop for both. Chris@559: Chris@559: while (i != points.end()) { Chris@559: Chris@846: if (!distant) { Chris@846: if (i == close.end()) { Chris@846: // switch from the close container to the points container Chris@846: i = points.begin(); Chris@846: distant = true; Chris@846: } Chris@559: } Chris@559: Chris@1266: if (snap == SnapRight) { Chris@559: Chris@1266: if (i->frame > matchframe && Chris@905: fabs(i->value - matchvalue) < epsilon) { Chris@1266: snapped = i->frame; Chris@1266: found = true; Chris@1266: break; Chris@1266: } Chris@559: Chris@1266: } else if (snap == SnapLeft) { Chris@559: Chris@1266: if (i->frame < matchframe) { Chris@905: if (fabs(i->value - matchvalue) < epsilon) { Chris@559: snapped = i->frame; Chris@559: found = true; // don't break, as the next may be better Chris@559: } Chris@1266: } else if (found || distant) { Chris@1266: break; Chris@1266: } Chris@559: Chris@1266: } else { Chris@559: // no other snap types supported Chris@1266: } Chris@559: Chris@559: ++i; Chris@559: } Chris@559: Chris@559: frame = snapped; Chris@559: return found; Chris@559: } Chris@559: Chris@701: QString Chris@701: RegionLayer::getScaleUnits() const Chris@701: { Chris@701: if (m_model) return m_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@411: QString queryUnits; Chris@701: queryUnits = getScaleUnits(); Chris@411: Chris@411: if (m_verticalScale == AutoAlignScale) { Chris@411: Chris@411: if (!v->getValueExtents(queryUnits, min, max, log)) { Chris@411: Chris@411: min = m_model->getValueMinimum(); Chris@411: max = m_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@411: min = m_model->getValueMinimum(); Chris@411: max = m_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@411: if (!m_model || !m_model->isOK()) return; Chris@411: Chris@905: sv_samplerate_t sampleRate = m_model->getSampleRate(); Chris@411: if (!sampleRate) return; Chris@411: Chris@411: // Profiler profiler("RegionLayer::paint", true); Chris@411: Chris@552: int x0 = rect.left() - 40, x1 = rect.right(); Chris@905: sv_frame_t frame0 = v->getFrameForX(x0); Chris@905: sv_frame_t frame1 = v->getFrameForX(x1); Chris@411: Chris@411: RegionModel::PointList points(m_model->getPoints(frame0, frame1)); 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@1266: // << m_model->getResolution() << " frames" << endl; Chris@411: Chris@905: double min = m_model->getValueMinimum(); Chris@905: double max = m_model->getValueMaximum(); Chris@411: if (max == min) max = min + 1.0; Chris@411: Chris@411: QPoint localPos; Chris@551: RegionModel::Point 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@411: for (RegionModel::PointList::const_iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@411: Chris@1266: const RegionModel::Point &p(*i); Chris@411: Chris@1266: int x = v->getXForFrame(p.frame); Chris@1266: int y = getYForValue(v, p.value); Chris@1266: int w = v->getXForFrame(p.frame + p.duration) - x; Chris@1266: int h = 9; Chris@1266: int ex = x + w; Chris@427: Chris@427: RegionModel::PointList::const_iterator j = i; Chris@1266: ++j; Chris@427: Chris@1266: if (j != points.end()) { Chris@1266: const RegionModel::Point &q(*j); Chris@1266: int nx = v->getXForFrame(q.frame); Chris@433: if (nx < ex) ex = nx; Chris@427: } Chris@427: Chris@1266: if (m_model->getValueQuantization() != 0.0) { Chris@1266: h = y - getYForValue(v, p.value + m_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@427: paint.setBrush(getColourForValue(v, p.value)); 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@551: if (!shouldIlluminate || Chris@551: // "illuminatePoint != p" Chris@551: RegionModel::Point::Comparator()(illuminatePoint, p) || Chris@551: RegionModel::Point::Comparator()(p, illuminatePoint)) { 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@1266: paint.drawRect(x, -1, ex - x, v->getPaintHeight() + 2); Chris@427: Chris@1266: } else { Chris@427: Chris@551: if (shouldIlluminate && Chris@551: // "illuminatePoint == p" Chris@551: !RegionModel::Point::Comparator()(illuminatePoint, p) && Chris@551: !RegionModel::Point::Comparator()(p, illuminatePoint)) { Chris@551: Chris@551: paint.setPen(v->getForeground()); Chris@551: paint.setBrush(v->getForeground()); Chris@551: Chris@701: QString vlabel = QString("%1%2").arg(p.value).arg(getScaleUnits()); Chris@1078: PaintAssistant::drawVisibleText(v, paint, Chris@551: x - paint.fontMetrics().width(vlabel) - 2, 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@551: (p.frame, m_model->getSampleRate()).toText(true).c_str(); Chris@1078: PaintAssistant::drawVisibleText(v, paint, Chris@551: x, Chris@551: y - h/2 - paint.fontMetrics().descent() - 2, 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@552: for (RegionModel::PointList::const_iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@552: Chris@1266: const RegionModel::Point &p(*i); Chris@552: Chris@1266: int x = v->getXForFrame(p.frame); Chris@1266: int y = getYForValue(v, p.value); Chris@552: Chris@552: bool illuminated = false; Chris@552: Chris@1266: if (m_plotStyle != PlotSegmentation) { Chris@552: Chris@552: if (shouldIlluminate && Chris@552: // "illuminatePoint == p" Chris@552: !RegionModel::Point::Comparator()(illuminatePoint, p) && Chris@552: !RegionModel::Point::Comparator()(p, illuminatePoint)) { Chris@552: Chris@552: illuminated = true; Chris@552: } Chris@552: } Chris@427: Chris@551: if (!illuminated) { Chris@551: QString label = p.label; Chris@551: if (label == "") { Chris@701: label = QString("%1%2").arg(p.value).arg(getScaleUnits()); Chris@551: } Chris@551: Chris@552: int labelX, labelY; Chris@552: Chris@551: if (m_plotStyle != PlotSegmentation) { Chris@552: labelX = x - paint.fontMetrics().width(label) - 2; Chris@552: labelY = y + paint.fontMetrics().height()/2 Chris@552: - paint.fontMetrics().descent(); Chris@551: } else { Chris@552: labelX = x + 5; Chris@552: labelY = v->getTextLabelHeight(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@552: nextLabelMinX = labelX + paint.fontMetrics().width(label); Chris@551: } Chris@552: Chris@1078: PaintAssistant::drawVisibleText(v, paint, labelX, labelY, label, 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@701: if (!m_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@717: if (!m_model || m_model->getPoints().empty()) 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@411: if (!m_model) return; Chris@411: Chris@989: sv_frame_t frame = v->getFrameForX(e->x()); Chris@411: if (frame < 0) frame = 0; Chris@411: frame = frame / m_model->getResolution() * m_model->getResolution(); Chris@411: Chris@905: double value = getValueForY(v, e->y()); Chris@411: Chris@905: m_editingPoint = RegionModel::Point(frame, float(value), 0, ""); Chris@411: m_originalPoint = m_editingPoint; Chris@411: Chris@411: if (m_editingCommand) finish(m_editingCommand); Chris@411: m_editingCommand = new RegionModel::EditCommand(m_model, Chris@543: tr("Draw Region")); Chris@411: m_editingCommand->addPoint(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@411: if (!m_model || !m_editing) return; Chris@411: Chris@905: sv_frame_t frame = v->getFrameForX(e->x()); Chris@411: if (frame < 0) frame = 0; Chris@411: frame = frame / m_model->getResolution() * m_model->getResolution(); Chris@411: Chris@905: double newValue = m_editingPoint.value; Chris@542: if (m_verticalScale != EqualSpaced) newValue = getValueForY(v, e->y()); Chris@411: Chris@905: sv_frame_t newFrame = m_editingPoint.frame; 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@411: m_editingCommand->deletePoint(m_editingPoint); Chris@411: m_editingPoint.frame = newFrame; Chris@905: m_editingPoint.value = float(newValue); Chris@411: m_editingPoint.duration = newDuration; Chris@411: m_editingCommand->addPoint(m_editingPoint); Chris@550: Chris@551: recalcSpacing(); Chris@411: } Chris@411: Chris@411: void Chris@918: RegionLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *) Chris@411: { Chris@411: if (!m_model || !m_editing) return; Chris@411: finish(m_editingCommand); Chris@411: m_editingCommand = 0; 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@411: if (!m_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@1266: m_editingCommand = 0; 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@411: if (!m_model || !m_editing) return; Chris@411: Chris@411: m_editing = false; Chris@551: Chris@550: RegionModel::Point p(0); Chris@550: if (!getPointToDrag(v, e->x(), e->y(), p)) return; Chris@550: if (p.frame != m_editingPoint.frame || p.value != m_editingPoint.value) return; Chris@411: Chris@411: m_editingCommand = new RegionModel::EditCommand Chris@543: (m_model, tr("Erase Region")); Chris@411: Chris@411: m_editingCommand->deletePoint(m_editingPoint); Chris@411: Chris@411: finish(m_editingCommand); Chris@411: m_editingCommand = 0; 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@411: if (!m_model) return; Chris@411: Chris@550: if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) { Chris@550: return; Chris@550: } Chris@550: Chris@551: m_dragPointX = v->getXForFrame(m_editingPoint.frame); Chris@551: m_dragPointY = getYForValue(v, m_editingPoint.value); Chris@551: Chris@411: m_originalPoint = m_editingPoint; Chris@411: Chris@411: if (m_editingCommand) { Chris@1266: finish(m_editingCommand); Chris@1266: m_editingCommand = 0; 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@411: if (!m_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@411: frame = frame / m_model->getResolution() * m_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@551: int avoid = m_spacingMap[m_editingPoint.value]; Chris@551: Chris@551: // ... unless there are other points with the same value Chris@551: if (m_distributionMap[m_editingPoint.value] > 1) avoid = -1; Chris@551: Chris@905: double value = getValueForY(v, newy, avoid); Chris@411: Chris@411: if (!m_editingCommand) { Chris@1266: m_editingCommand = new RegionModel::EditCommand(m_model, Chris@1266: tr("Drag Region")); Chris@411: } Chris@411: Chris@411: m_editingCommand->deletePoint(m_editingPoint); Chris@411: m_editingPoint.frame = frame; Chris@905: m_editingPoint.value = float(value); Chris@411: m_editingCommand->addPoint(m_editingPoint); Chris@550: recalcSpacing(); Chris@411: } Chris@411: Chris@411: void Chris@918: RegionLayer::editEnd(LayerGeometryProvider *, QMouseEvent *) Chris@411: { Chris@411: if (!m_model || !m_editing) return; Chris@411: Chris@411: if (m_editingCommand) { Chris@411: Chris@1266: QString newName = m_editingCommand->getName(); Chris@411: Chris@1266: if (m_editingPoint.frame != m_originalPoint.frame) { Chris@1266: if (m_editingPoint.value != m_originalPoint.value) { 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@411: m_editingCommand = 0; 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@411: if (!m_model) return false; Chris@411: Chris@550: RegionModel::Point region(0); Chris@550: if (!getPointToDrag(v, e->x(), e->y(), region)) return false; Chris@550: Chris@411: ItemEditDialog *dialog = new ItemEditDialog Chris@411: (m_model->getSampleRate(), Chris@411: ItemEditDialog::ShowTime | Chris@411: ItemEditDialog::ShowDuration | Chris@411: ItemEditDialog::ShowValue | Chris@411: ItemEditDialog::ShowText, Chris@701: getScaleUnits()); Chris@411: Chris@411: dialog->setFrameTime(region.frame); Chris@411: dialog->setValue(region.value); Chris@411: dialog->setFrameDuration(region.duration); Chris@411: dialog->setText(region.label); Chris@411: Chris@411: if (dialog->exec() == QDialog::Accepted) { Chris@411: Chris@411: RegionModel::Point newRegion = region; Chris@411: newRegion.frame = dialog->getFrameTime(); Chris@411: newRegion.value = dialog->getValue(); Chris@411: newRegion.duration = dialog->getFrameDuration(); Chris@411: newRegion.label = dialog->getText(); Chris@411: Chris@411: RegionModel::EditCommand *command = new RegionModel::EditCommand Chris@543: (m_model, tr("Edit Region")); Chris@411: command->deletePoint(region); Chris@411: command->addPoint(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@411: if (!m_model) return; Chris@411: Chris@411: RegionModel::EditCommand *command = Chris@1266: new RegionModel::EditCommand(m_model, tr("Drag Selection")); Chris@411: Chris@411: RegionModel::PointList points = Chris@1266: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@411: Chris@411: for (RegionModel::PointList::iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@411: Chris@1266: if (s.contains(i->frame)) { Chris@1266: RegionModel::Point newPoint(*i); Chris@1266: newPoint.frame = i->frame + newStartFrame - s.getStartFrame(); Chris@1266: command->deletePoint(*i); Chris@1266: command->addPoint(newPoint); Chris@1266: } 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@411: if (!m_model) return; Chris@411: Chris@411: RegionModel::EditCommand *command = Chris@1266: new RegionModel::EditCommand(m_model, tr("Resize Selection")); Chris@411: Chris@411: RegionModel::PointList points = Chris@1266: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@411: Chris@411: double ratio = Chris@1266: double(newSize.getEndFrame() - newSize.getStartFrame()) / Chris@1266: double(s.getEndFrame() - s.getStartFrame()); Chris@411: Chris@411: for (RegionModel::PointList::iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@411: Chris@1266: if (s.contains(i->frame)) { Chris@411: Chris@1266: double targetStart = double(i->frame); Chris@1266: targetStart = double(newSize.getStartFrame()) + Chris@1266: targetStart - double(s.getStartFrame()) * ratio; Chris@411: Chris@1266: double targetEnd = double(i->frame + i->duration); Chris@1266: targetEnd = double(newSize.getStartFrame()) + Chris@1266: targetEnd - double(s.getStartFrame()) * ratio; Chris@411: Chris@1266: RegionModel::Point newPoint(*i); Chris@1266: newPoint.frame = lrint(targetStart); Chris@1266: newPoint.duration = lrint(targetEnd - targetStart); Chris@1266: command->deletePoint(*i); Chris@1266: command->addPoint(newPoint); Chris@1266: } 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@411: if (!m_model) return; Chris@411: Chris@411: RegionModel::EditCommand *command = Chris@1266: new RegionModel::EditCommand(m_model, tr("Delete Selected Points")); Chris@411: Chris@411: RegionModel::PointList points = Chris@1266: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@411: Chris@411: for (RegionModel::PointList::iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@411: Chris@411: if (s.contains(i->frame)) { Chris@411: command->deletePoint(*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@411: if (!m_model) return; Chris@411: Chris@411: RegionModel::PointList points = Chris@1266: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@411: Chris@411: for (RegionModel::PointList::iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@1266: if (s.contains(i->frame)) { Chris@411: Clipboard::Point point(i->frame, i->value, i->duration, i->label); Chris@411: point.setReferenceFrame(alignToReference(v, i->frame)); Chris@411: to.addPoint(point); Chris@411: } Chris@411: } Chris@411: } Chris@411: Chris@411: bool Chris@918: RegionLayer::paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t /* frameOffset */, bool /* interactive */) Chris@411: { Chris@411: if (!m_model) return false; Chris@411: Chris@411: const Clipboard::PointList &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@411: RegionModel::EditCommand *command = Chris@1266: new RegionModel::EditCommand(m_model, tr("Paste")); Chris@411: Chris@411: for (Clipboard::PointList::const_iterator i = points.begin(); Chris@411: i != points.end(); ++i) { Chris@411: Chris@411: if (!i->haveFrame()) continue; 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@411: if (i->haveReferenceFrame()) { 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@411: RegionModel::Point newPoint(frame); Chris@411: Chris@411: if (i->haveLabel()) newPoint.label = i->getLabel(); Chris@411: if (i->haveValue()) newPoint.value = i->getValue(); Chris@411: else newPoint.value = (m_model->getValueMinimum() + Chris@411: m_model->getValueMaximum()) / 2; Chris@411: if (i->haveDuration()) newPoint.duration = i->getDuration(); Chris@411: else { Chris@905: sv_frame_t nextFrame = frame; Chris@411: Clipboard::PointList::const_iterator j = i; Chris@411: for (; j != points.end(); ++j) { Chris@411: if (!j->haveFrame()) continue; 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@411: newPoint.duration = m_model->getResolution(); Chris@411: } else { Chris@411: newPoint.duration = nextFrame - frame; Chris@411: } Chris@411: } Chris@411: Chris@411: command->addPoint(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: