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@411: #include "ColourDatabase.h" Chris@427: #include "ColourMapper.h" Chris@411: #include "view/View.h" Chris@411: Chris@411: #include "data/model/RegionModel.h" Chris@411: Chris@411: #include "widgets/ItemEditDialog.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@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@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@412: Chris@412: if (min) *min = 0; Chris@412: if (max) *max = 1; Chris@412: if (deflt) *deflt = 0; Chris@412: Chris@412: val = int(m_plotStyle); Chris@412: Chris@412: } else if (name == "Vertical Scale") { Chris@411: Chris@411: if (min) *min = 0; Chris@411: if (max) *max = 3; Chris@433: if (deflt) *deflt = int(EqualSpaced); Chris@411: Chris@411: 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@411: (m_model->getScaleUnits()); Chris@411: } Chris@411: Chris@411: } else { Chris@411: Chris@411: 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@427: return ColourMapper::getColourMapName(value); Chris@427: } else if (name == "Plot Type") { Chris@412: Chris@412: switch (value) { Chris@412: default: Chris@427: case 0: return tr("Bars"); Chris@412: case 1: return tr("Segmentation"); Chris@412: } Chris@412: Chris@412: } else if (name == "Vertical Scale") { Chris@411: switch (value) { Chris@411: default: Chris@411: case 0: return tr("Auto-Align"); Chris@433: case 1: return tr("Equal Spaced"); Chris@433: case 2: return tr("Linear"); Chris@433: case 3: return tr("Log"); Chris@411: } 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@412: setPlotStyle(PlotStyle(value)); Chris@412: } else if (name == "Vertical Scale") { Chris@411: 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@411: RegionLayer::isLayerScrollable(const View *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@411: RegionLayer::getValueExtents(float &min, float &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@411: unit = m_model->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@411: RegionLayer::getDisplayExtents(float &min, float &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@411: RegionLayer::getLocalPoints(View *v, int x) const Chris@411: { Chris@411: if (!m_model) return RegionModel::PointList(); Chris@411: Chris@411: long frame = v->getFrameForX(x); Chris@411: Chris@411: RegionModel::PointList onPoints = Chris@411: m_model->getPoints(frame); Chris@411: Chris@411: if (!onPoints.empty()) { Chris@411: return onPoints; Chris@411: } Chris@411: Chris@411: RegionModel::PointList prevPoints = Chris@411: m_model->getPreviousPoints(frame); Chris@411: RegionModel::PointList nextPoints = Chris@411: m_model->getNextPoints(frame); Chris@411: Chris@411: RegionModel::PointList usePoints = prevPoints; Chris@411: Chris@411: if (prevPoints.empty()) { Chris@411: usePoints = nextPoints; Chris@411: } else if (long(prevPoints.begin()->frame) < v->getStartFrame() && Chris@411: !(nextPoints.begin()->frame > v->getEndFrame())) { Chris@411: usePoints = nextPoints; Chris@411: } else if (long(nextPoints.begin()->frame) - frame < Chris@411: frame - long(prevPoints.begin()->frame)) { Chris@411: usePoints = nextPoints; Chris@411: } Chris@411: Chris@411: if (!usePoints.empty()) { Chris@411: int fuzz = 2; Chris@411: int px = v->getXForFrame(usePoints.begin()->frame); Chris@411: if ((px > x && px - x > fuzz) || Chris@411: (px < x && x - px > fuzz + 1)) { Chris@411: usePoints.clear(); Chris@411: } Chris@411: } Chris@411: Chris@411: return usePoints; Chris@411: } Chris@411: Chris@550: bool Chris@550: RegionLayer::getPointToDrag(View *v, int x, int y, RegionModel::Point &p) const Chris@550: { Chris@550: if (!m_model) return false; Chris@550: Chris@550: long 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@552: RegionLayer::getLabelPreceding(size_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@411: RegionLayer::getFeatureDescription(View *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@411: if (!m_model->isReady()) { Chris@411: return tr("In progress"); Chris@411: } else { Chris@411: return tr("No local points"); Chris@411: } 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@411: int y = getYForValue(v, i->value); Chris@411: int h = 3; Chris@411: Chris@411: if (m_model->getValueQuantization() != 0.0) { Chris@411: h = y - getYForValue(v, i->value + m_model->getValueQuantization()); Chris@411: if (h < 3) h = 3; Chris@411: } Chris@411: Chris@411: if (pos.y() >= y - h && pos.y() <= y) { Chris@411: region = *i; Chris@411: break; Chris@411: } 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@411: m_model->getSampleRate()); Chris@411: RealTime rd = RealTime::frame2RealTime(region.duration, Chris@411: m_model->getSampleRate()); Chris@411: Chris@411: QString valueText; Chris@411: Chris@411: valueText = tr("%1 %2").arg(region.value).arg(m_model->getScaleUnits()); Chris@411: Chris@411: QString text; Chris@411: Chris@411: if (region.label == "") { Chris@411: text = QString(tr("Time:\t%1\nValue:\t%2\nDuration:\t%3\nNo label")) Chris@411: .arg(rt.toText(true).c_str()) Chris@411: .arg(valueText) Chris@411: .arg(rd.toText(true).c_str()); Chris@411: } else { Chris@411: text = QString(tr("Time:\t%1\nValue:\t%2\nDuration:\t%3\nLabel:\t%4")) Chris@411: .arg(rt.toText(true).c_str()) Chris@411: .arg(valueText) Chris@411: .arg(rd.toText(true).c_str()) Chris@411: .arg(region.label); Chris@411: } Chris@411: Chris@411: pos = QPoint(v->getXForFrame(region.frame), Chris@411: getYForValue(v, region.value)); Chris@411: return text; Chris@411: } Chris@411: Chris@411: bool Chris@411: RegionLayer::snapToFeatureFrame(View *v, int &frame, Chris@414: size_t &resolution, Chris@414: SnapType snap) const Chris@411: { Chris@411: if (!m_model) { Chris@411: 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@411: Chris@411: points = getLocalPoints(v, v->getXForFrame(frame)); Chris@411: if (points.empty()) return false; Chris@411: frame = points.begin()->frame; Chris@411: return true; Chris@411: } Chris@411: Chris@411: points = m_model->getPoints(frame, frame); Chris@411: int snapped = frame; Chris@411: bool found = false; Chris@411: Chris@411: for (RegionModel::PointList::const_iterator i = points.begin(); Chris@411: i != points.end(); ++i) { Chris@411: Chris@411: 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@414: 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@411: } else if (snap == SnapLeft) { Chris@411: Chris@411: if (i->frame <= frame) { Chris@411: snapped = i->frame; Chris@411: found = true; // don't break, as the next may be better Chris@411: } else { Chris@411: break; Chris@411: } Chris@411: Chris@411: } else { // nearest Chris@411: Chris@411: RegionModel::PointList::const_iterator j = i; Chris@411: ++j; Chris@411: Chris@411: if (j == points.end()) { Chris@411: Chris@411: snapped = i->frame; Chris@411: found = true; Chris@411: break; Chris@411: Chris@411: } else if (j->frame >= frame) { Chris@411: Chris@411: if (j->frame - frame < frame - i->frame) { Chris@411: snapped = j->frame; Chris@411: } else { Chris@411: snapped = i->frame; Chris@411: } Chris@411: found = true; Chris@411: break; Chris@411: } Chris@411: } Chris@411: } Chris@411: Chris@411: frame = snapped; Chris@411: return found; Chris@411: } Chris@411: Chris@559: bool Chris@559: RegionLayer::snapToSimilarFeature(View *v, int &frame, Chris@559: size_t &resolution, Chris@559: SnapType snap) const Chris@559: { Chris@559: if (!m_model) { Chris@559: 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@559: int matchframe = frame; Chris@559: float 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@559: int snapped = frame; Chris@559: bool found = false; Chris@559: bool distant = false; Chris@559: float 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@559: // if no suitable close one is found Chris@559: Chris@559: while (i != points.end()) { Chris@559: Chris@559: if (i == close.end()) { Chris@559: i = points.begin(); Chris@559: distant = true; Chris@559: } Chris@559: Chris@559: if (snap == SnapRight) { Chris@559: Chris@559: if (i->frame > matchframe && Chris@559: fabsf(i->value - matchvalue) < epsilon) { Chris@559: snapped = i->frame; Chris@559: found = true; Chris@559: break; Chris@559: } Chris@559: Chris@559: } else if (snap == SnapLeft) { Chris@559: Chris@559: if (i->frame < matchframe) { Chris@559: if (fabsf(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@559: } else if (found || distant) { Chris@559: break; Chris@559: } Chris@559: Chris@559: } else { Chris@559: // no other snap types supported Chris@559: } Chris@559: Chris@559: ++i; Chris@559: } Chris@559: Chris@559: frame = snapped; Chris@559: return found; Chris@559: } Chris@559: Chris@411: void Chris@411: RegionLayer::getScaleExtents(View *v, float &min, float &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@411: queryUnits = m_model->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@542: // std::cerr << "RegionLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl; Chris@411: Chris@411: } else if (log) { Chris@411: Chris@411: LogRange::mapRange(min, max); Chris@411: Chris@542: // std::cerr << "RegionLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::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@542: // std::cerr << "RegionLayer[" << this << "]::getScaleExtents: equal spaced; min = " << min << ", max = " << max << ", log = " << log << std::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@542: RegionLayer::spacingIndexToY(View *v, int i) const Chris@542: { Chris@542: int h = v->height(); Chris@542: int n = 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@542: float Chris@542: RegionLayer::yToSpacingIndex(View *v, int y) const Chris@542: { Chris@542: // we return an inexact result here (float rather than int) Chris@542: int h = v->height(); Chris@542: int n = m_spacingMap.size(); Chris@551: // from y = h - ((h * i) / n) + (h / (2 * n)) as above (vh taking place of i) Chris@551: float vh = float(2*h*n - h - 2*n*y) / float(2*h); Chris@542: return vh; Chris@542: } Chris@542: Chris@542: int Chris@411: RegionLayer::getYForValue(View *v, float val) const Chris@411: { Chris@411: float min = 0.0, max = 0.0; Chris@411: bool logarithmic = false; Chris@411: int h = v->height(); 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@432: // std::cerr << "RegionLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << std::endl; Chris@432: // std::cerr << "h = " << h << ", margin = " << margin << std::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@542: float Chris@551: RegionLayer::getValueForY(View *v, int y, int avoid) const Chris@542: { Chris@542: float min = 0.0, max = 0.0; Chris@542: bool logarithmic = false; Chris@542: int h = v->height(); 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@542: float 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@542: int n = m_spacingMap.size(); Chris@542: int ivh = lrintf(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@551: // std::cerr << "getValueForY: y = " << y << ", vh = " << vh << ", ivh = " << ivh << " of " << n << ", iy = " << iy << ", dist = " << dist << ", gap = " << gap << std::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@551: // std::cerr << "nearest existing value = " << i->first << " at " << iy << std::endl; Chris@551: Chris@542: float val = 0; Chris@542: Chris@551: // std::cerr << "note: avoid = " << avoid << ", i->second = " << i->second << std::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@551: // std::cerr << "extended down to " << val << std::endl; Chris@542: } else { Chris@542: SpacingMap::const_iterator j = i; Chris@542: --j; Chris@542: val = (i->first + j->first) / 2; Chris@551: // std::cerr << "bisected down to " << val << std::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@551: // std::cerr << "extended up to " << val << std::endl; Chris@542: } else { Chris@542: val = (i->first + j->first) / 2; Chris@551: // std::cerr << "bisected up to " << val << std::endl; Chris@542: } Chris@551: } else { Chris@551: // snap Chris@551: val = i->first; Chris@551: // std::cerr << "snapped to " << val << std::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@542: float val = min + (float(h - y) * float(max - min)) / h; Chris@542: Chris@542: if (logarithmic) { Chris@542: val = powf(10.f, val); Chris@542: } Chris@542: Chris@542: return val; Chris@542: } Chris@542: } Chris@542: Chris@427: QColor Chris@427: RegionLayer::getColourForValue(View *v, float val) const Chris@427: { Chris@427: float 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@427: QColor solid = ColourMapper(m_colourMap, 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@411: RegionLayer::paint(View *v, QPainter &paint, QRect rect) const Chris@411: { Chris@411: if (!m_model || !m_model->isOK()) return; Chris@411: Chris@411: int 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@411: long frame0 = v->getFrameForX(x0); Chris@411: long 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@585: // << m_model->getResolution() << " frames" << endl; Chris@411: Chris@411: float min = m_model->getValueMinimum(); Chris@411: float 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: int fontAscent = paint.fontMetrics().ascent(); Chris@550: Chris@411: for (RegionModel::PointList::const_iterator i = points.begin(); Chris@411: i != points.end(); ++i) { Chris@411: Chris@411: const RegionModel::Point &p(*i); Chris@411: Chris@411: int x = v->getXForFrame(p.frame); Chris@411: int y = getYForValue(v, p.value); Chris@411: int w = v->getXForFrame(p.frame + p.duration) - x; Chris@413: int h = 9; Chris@433: int ex = x + w; Chris@427: Chris@427: RegionModel::PointList::const_iterator j = i; Chris@427: ++j; Chris@427: Chris@427: if (j != points.end()) { Chris@427: const RegionModel::Point &q(*j); Chris@433: int nx = v->getXForFrame(q.frame); Chris@433: if (nx < ex) ex = nx; Chris@427: } Chris@427: Chris@411: if (m_model->getValueQuantization() != 0.0) { Chris@411: h = y - getYForValue(v, p.value + m_model->getValueQuantization()); Chris@411: if (h < 3) h = 3; Chris@411: } Chris@411: Chris@411: if (w < 1) w = 1; Chris@411: Chris@427: if (m_plotStyle == PlotSegmentation) { Chris@427: paint.setPen(getForegroundQColor(v)); 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@427: if (m_plotStyle == PlotSegmentation) { Chris@427: Chris@433: 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@559: paint.setPen(QPen(getForegroundQColor(v), 1)); Chris@552: paint.drawLine(x, 0, x, v->height()); Chris@551: paint.setPen(Qt::NoPen); Chris@552: Chris@552: } else { Chris@552: paint.setPen(QPen(getForegroundQColor(v), 2)); Chris@551: } Chris@427: Chris@559: paint.drawRect(x, -1, ex - x, v->height() + 2); Chris@427: Chris@427: } 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@551: QString vlabel = QString("%1%2").arg(p.value).arg(m_model->getScaleUnits()); Chris@551: v->drawVisibleText(paint, Chris@551: x - paint.fontMetrics().width(vlabel) - 2, Chris@551: y + paint.fontMetrics().height()/2 Chris@551: - paint.fontMetrics().descent(), Chris@551: vlabel, View::OutlinedText); Chris@551: Chris@551: QString hlabel = RealTime::frame2RealTime Chris@551: (p.frame, m_model->getSampleRate()).toText(true).c_str(); Chris@551: v->drawVisibleText(paint, Chris@551: x, Chris@551: y - h/2 - paint.fontMetrics().descent() - 2, Chris@551: hlabel, View::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@552: i != points.end(); ++i) { Chris@552: Chris@552: const RegionModel::Point &p(*i); Chris@552: Chris@552: int x = v->getXForFrame(p.frame); Chris@552: int y = getYForValue(v, p.value); Chris@552: Chris@552: bool illuminated = false; Chris@552: Chris@552: 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@551: label = QString("%1%2").arg(p.value).arg(m_model->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@552: if (lastLabelY < v->height()/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@552: v->drawVisibleText(paint, labelX, labelY, label, View::OutlinedText); Chris@550: } Chris@411: } Chris@411: Chris@411: paint.restore(); Chris@411: } Chris@411: Chris@411: void Chris@411: RegionLayer::drawStart(View *v, QMouseEvent *e) Chris@411: { Chris@411: if (!m_model) return; Chris@411: Chris@411: long 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@411: float value = getValueForY(v, e->y()); Chris@411: Chris@550: m_editingPoint = RegionModel::Point(frame, 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@411: RegionLayer::drawDrag(View *v, QMouseEvent *e) Chris@411: { Chris@411: if (!m_model || !m_editing) return; Chris@411: Chris@411: long 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@542: float newValue = m_editingPoint.value; Chris@542: if (m_verticalScale != EqualSpaced) newValue = getValueForY(v, e->y()); Chris@411: Chris@411: long newFrame = m_editingPoint.frame; Chris@411: long 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@411: m_editingPoint.value = 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@411: RegionLayer::drawEnd(View *, 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@411: RegionLayer::eraseStart(View *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@411: finish(m_editingCommand); Chris@411: m_editingCommand = 0; Chris@411: } Chris@411: Chris@411: m_editing = true; Chris@550: recalcSpacing(); Chris@411: } Chris@411: Chris@411: void Chris@411: RegionLayer::eraseDrag(View *v, QMouseEvent *e) Chris@411: { Chris@411: } Chris@411: Chris@411: void Chris@411: RegionLayer::eraseEnd(View *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@411: RegionLayer::editStart(View *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@411: finish(m_editingCommand); Chris@411: 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@411: RegionLayer::editDrag(View *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@551: long 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@551: float value = getValueForY(v, newy, avoid); Chris@411: Chris@411: if (!m_editingCommand) { Chris@411: m_editingCommand = new RegionModel::EditCommand(m_model, Chris@543: tr("Drag Region")); Chris@411: } Chris@411: Chris@411: m_editingCommand->deletePoint(m_editingPoint); Chris@411: m_editingPoint.frame = frame; Chris@411: m_editingPoint.value = value; Chris@411: m_editingCommand->addPoint(m_editingPoint); Chris@550: recalcSpacing(); Chris@411: } Chris@411: Chris@411: void Chris@550: RegionLayer::editEnd(View *, QMouseEvent *e) Chris@411: { Chris@411: if (!m_model || !m_editing) return; Chris@411: Chris@411: if (m_editingCommand) { Chris@411: Chris@411: QString newName = m_editingCommand->getName(); Chris@411: Chris@411: if (m_editingPoint.frame != m_originalPoint.frame) { Chris@411: if (m_editingPoint.value != m_originalPoint.value) { Chris@543: newName = tr("Edit Region"); Chris@411: } else { Chris@543: newName = tr("Relocate Region"); Chris@411: } Chris@411: } else { Chris@411: newName = tr("Change Point Value"); Chris@411: } Chris@411: Chris@411: m_editingCommand->setName(newName); Chris@411: 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@411: RegionLayer::editOpen(View *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@411: m_model->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@411: RegionLayer::moveSelection(Selection s, size_t newStartFrame) Chris@411: { Chris@411: if (!m_model) return; Chris@411: Chris@411: RegionModel::EditCommand *command = Chris@411: new RegionModel::EditCommand(m_model, tr("Drag Selection")); Chris@411: Chris@411: RegionModel::PointList points = Chris@411: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@411: Chris@411: for (RegionModel::PointList::iterator i = points.begin(); Chris@411: i != points.end(); ++i) { Chris@411: Chris@411: if (s.contains(i->frame)) { Chris@411: RegionModel::Point newPoint(*i); Chris@411: newPoint.frame = i->frame + newStartFrame - s.getStartFrame(); Chris@411: command->deletePoint(*i); Chris@411: command->addPoint(newPoint); Chris@411: } 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@411: new RegionModel::EditCommand(m_model, tr("Resize Selection")); Chris@411: Chris@411: RegionModel::PointList points = Chris@411: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@411: Chris@411: double ratio = Chris@411: double(newSize.getEndFrame() - newSize.getStartFrame()) / Chris@411: double(s.getEndFrame() - s.getStartFrame()); Chris@411: Chris@411: for (RegionModel::PointList::iterator i = points.begin(); Chris@411: i != points.end(); ++i) { Chris@411: Chris@411: if (s.contains(i->frame)) { Chris@411: Chris@411: double targetStart = i->frame; Chris@411: targetStart = newSize.getStartFrame() + Chris@411: double(targetStart - s.getStartFrame()) * ratio; Chris@411: Chris@411: double targetEnd = i->frame + i->duration; Chris@411: targetEnd = newSize.getStartFrame() + Chris@411: double(targetEnd - s.getStartFrame()) * ratio; Chris@411: Chris@411: RegionModel::Point newPoint(*i); Chris@411: newPoint.frame = lrint(targetStart); Chris@411: newPoint.duration = lrint(targetEnd - targetStart); Chris@411: command->deletePoint(*i); Chris@411: command->addPoint(newPoint); Chris@411: } 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@411: new RegionModel::EditCommand(m_model, tr("Delete Selected Points")); Chris@411: Chris@411: RegionModel::PointList points = Chris@411: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@411: Chris@411: for (RegionModel::PointList::iterator i = points.begin(); Chris@411: 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@411: RegionLayer::copy(View *v, Selection s, Clipboard &to) Chris@411: { Chris@411: if (!m_model) return; Chris@411: Chris@411: RegionModel::PointList points = Chris@411: m_model->getPoints(s.getStartFrame(), s.getEndFrame()); Chris@411: Chris@411: for (RegionModel::PointList::iterator i = points.begin(); Chris@411: i != points.end(); ++i) { Chris@411: 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@411: RegionLayer::paste(View *v, const Clipboard &from, int 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@411: QMessageBox::question(v, 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@411: 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@411: size_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@411: size_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@411: SingleColourLayer::toXml(stream, indent, extraAttributes + Chris@412: QString(" verticalScale=\"%1\" plotStyle=\"%2\"") Chris@412: .arg(m_verticalScale) Chris@412: .arg(m_plotStyle)); 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@411: attributes.value("verticalScale").toInt(&ok); Chris@411: if (ok) setVerticalScale(scale); Chris@412: PlotStyle style = (PlotStyle) Chris@412: attributes.value("plotStyle").toInt(&ok); Chris@412: if (ok) setPlotStyle(style); Chris@411: } Chris@411: Chris@411: