annotate layer/RegionLayer.cpp @ 1470:696e569ff21b by-id

Further layer updates for ById
author Chris Cannam
date Fri, 28 Jun 2019 17:37:22 +0100
parents 5b9692768beb
children f2525e6cbdf1
rev   line source
Chris@411 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@411 2
Chris@411 3 /*
Chris@411 4 Sonic Visualiser
Chris@411 5 An audio file viewer and annotation editor.
Chris@411 6 Centre for Digital Music, Queen Mary, University of London.
Chris@411 7 This file copyright 2006-2008 Chris Cannam and QMUL.
Chris@411 8
Chris@411 9 This program is free software; you can redistribute it and/or
Chris@411 10 modify it under the terms of the GNU General Public License as
Chris@411 11 published by the Free Software Foundation; either version 2 of the
Chris@411 12 License, or (at your option) any later version. See the file
Chris@411 13 COPYING included with this distribution for more information.
Chris@411 14 */
Chris@411 15
Chris@411 16 #include "RegionLayer.h"
Chris@411 17
Chris@411 18 #include "data/model/Model.h"
Chris@411 19 #include "base/RealTime.h"
Chris@411 20 #include "base/Profiler.h"
Chris@411 21 #include "base/LogRange.h"
Chris@1078 22
Chris@411 23 #include "ColourDatabase.h"
Chris@427 24 #include "ColourMapper.h"
Chris@701 25 #include "LinearNumericalScale.h"
Chris@701 26 #include "LogNumericalScale.h"
Chris@701 27 #include "LinearColourScale.h"
Chris@701 28 #include "LogColourScale.h"
Chris@1078 29 #include "PaintAssistant.h"
Chris@701 30
Chris@411 31 #include "view/View.h"
Chris@411 32
Chris@411 33 #include "data/model/RegionModel.h"
Chris@411 34
Chris@411 35 #include "widgets/ItemEditDialog.h"
Chris@701 36 #include "widgets/TextAbbrev.h"
Chris@411 37
Chris@411 38 #include <QPainter>
Chris@411 39 #include <QPainterPath>
Chris@411 40 #include <QMouseEvent>
Chris@411 41 #include <QTextStream>
Chris@411 42 #include <QMessageBox>
Chris@411 43
Chris@411 44 #include <iostream>
Chris@411 45 #include <cmath>
Chris@411 46
Chris@411 47 RegionLayer::RegionLayer() :
Chris@411 48 SingleColourLayer(),
Chris@1408 49 m_model(nullptr),
Chris@411 50 m_editing(false),
Chris@846 51 m_dragPointX(0),
Chris@846 52 m_dragPointY(0),
Chris@846 53 m_dragStartX(0),
Chris@846 54 m_dragStartY(0),
Chris@543 55 m_originalPoint(0, 0.0, 0, tr("New Region")),
Chris@543 56 m_editingPoint(0, 0.0, 0, tr("New Region")),
Chris@1408 57 m_editingCommand(nullptr),
Chris@433 58 m_verticalScale(EqualSpaced),
Chris@427 59 m_colourMap(0),
Chris@1362 60 m_colourInverted(false),
Chris@412 61 m_plotStyle(PlotLines)
Chris@411 62 {
Chris@411 63
Chris@411 64 }
Chris@411 65
Chris@1470 66 int
Chris@1470 67 RegionLayer::getCompletion(LayerGeometryProvider *) const
Chris@1470 68 {
Chris@1470 69 auto model = ModelById::get(m_model);
Chris@1470 70 if (model) return model->getCompletion();
Chris@1470 71 else return 0;
Chris@1470 72 }
Chris@1470 73
Chris@411 74 void
Chris@411 75 RegionLayer::setModel(RegionModel *model)
Chris@411 76 {
Chris@411 77 if (m_model == model) return;
Chris@411 78 m_model = model;
Chris@411 79
Chris@411 80 connectSignals(m_model);
Chris@411 81
Chris@433 82 connect(m_model, SIGNAL(modelChanged()), this, SLOT(recalcSpacing()));
Chris@433 83 recalcSpacing();
Chris@433 84
Chris@587 85 // SVDEBUG << "RegionLayer::setModel(" << model << ")" << endl;
Chris@411 86
Chris@610 87 if (m_model && m_model->getRDFTypeURI().endsWith("Segment")) {
Chris@610 88 setPlotStyle(PlotSegmentation);
Chris@610 89 }
Chris@610 90 if (m_model && m_model->getRDFTypeURI().endsWith("Change")) {
Chris@610 91 setPlotStyle(PlotSegmentation);
Chris@610 92 }
Chris@610 93
Chris@411 94 emit modelReplaced();
Chris@411 95 }
Chris@411 96
Chris@411 97 Layer::PropertyList
Chris@411 98 RegionLayer::getProperties() const
Chris@411 99 {
Chris@411 100 PropertyList list = SingleColourLayer::getProperties();
Chris@411 101 list.push_back("Vertical Scale");
Chris@411 102 list.push_back("Scale Units");
Chris@412 103 list.push_back("Plot Type");
Chris@411 104 return list;
Chris@411 105 }
Chris@411 106
Chris@411 107 QString
Chris@411 108 RegionLayer::getPropertyLabel(const PropertyName &name) const
Chris@411 109 {
Chris@411 110 if (name == "Vertical Scale") return tr("Vertical Scale");
Chris@411 111 if (name == "Scale Units") return tr("Scale Units");
Chris@412 112 if (name == "Plot Type") return tr("Plot Type");
Chris@411 113 return SingleColourLayer::getPropertyLabel(name);
Chris@411 114 }
Chris@411 115
Chris@411 116 Layer::PropertyType
Chris@411 117 RegionLayer::getPropertyType(const PropertyName &name) const
Chris@411 118 {
Chris@411 119 if (name == "Scale Units") return UnitsProperty;
Chris@411 120 if (name == "Vertical Scale") return ValueProperty;
Chris@412 121 if (name == "Plot Type") return ValueProperty;
Chris@427 122 if (name == "Colour" && m_plotStyle == PlotSegmentation) return ValueProperty;
Chris@411 123 return SingleColourLayer::getPropertyType(name);
Chris@411 124 }
Chris@411 125
Chris@411 126 QString
Chris@411 127 RegionLayer::getPropertyGroupName(const PropertyName &name) const
Chris@411 128 {
Chris@411 129 if (name == "Vertical Scale" || name == "Scale Units") {
Chris@411 130 return tr("Scale");
Chris@411 131 }
Chris@411 132 return SingleColourLayer::getPropertyGroupName(name);
Chris@411 133 }
Chris@411 134
Chris@411 135 int
Chris@411 136 RegionLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@411 137 int *min, int *max, int *deflt) const
Chris@411 138 {
Chris@411 139 int val = 0;
Chris@411 140
Chris@427 141 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
Chris@427 142
Chris@427 143 if (min) *min = 0;
Chris@427 144 if (max) *max = ColourMapper::getColourMapCount() - 1;
Chris@427 145 if (deflt) *deflt = 0;
Chris@427 146
Chris@427 147 val = m_colourMap;
Chris@427 148
Chris@427 149 } else if (name == "Plot Type") {
Chris@1266 150
Chris@1266 151 if (min) *min = 0;
Chris@1266 152 if (max) *max = 1;
Chris@412 153 if (deflt) *deflt = 0;
Chris@1266 154
Chris@1266 155 val = int(m_plotStyle);
Chris@412 156
Chris@412 157 } else if (name == "Vertical Scale") {
Chris@1266 158
Chris@1266 159 if (min) *min = 0;
Chris@1266 160 if (max) *max = 3;
Chris@433 161 if (deflt) *deflt = int(EqualSpaced);
Chris@1266 162
Chris@1266 163 val = int(m_verticalScale);
Chris@411 164
Chris@411 165 } else if (name == "Scale Units") {
Chris@411 166
Chris@411 167 if (deflt) *deflt = 0;
Chris@411 168 if (m_model) {
Chris@411 169 val = UnitDatabase::getInstance()->getUnitId
Chris@701 170 (getScaleUnits());
Chris@411 171 }
Chris@411 172
Chris@411 173 } else {
Chris@411 174
Chris@1266 175 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@411 176 }
Chris@411 177
Chris@411 178 return val;
Chris@411 179 }
Chris@411 180
Chris@411 181 QString
Chris@411 182 RegionLayer::getPropertyValueLabel(const PropertyName &name,
Chris@427 183 int value) const
Chris@411 184 {
Chris@427 185 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
Chris@1362 186 return ColourMapper::getColourMapLabel(value);
Chris@427 187 } else if (name == "Plot Type") {
Chris@412 188
Chris@1266 189 switch (value) {
Chris@1266 190 default:
Chris@1266 191 case 0: return tr("Bars");
Chris@1266 192 case 1: return tr("Segmentation");
Chris@1266 193 }
Chris@412 194
Chris@412 195 } else if (name == "Vertical Scale") {
Chris@1266 196 switch (value) {
Chris@1266 197 default:
Chris@1266 198 case 0: return tr("Auto-Align");
Chris@1266 199 case 1: return tr("Equal Spaced");
Chris@1266 200 case 2: return tr("Linear");
Chris@1266 201 case 3: return tr("Log");
Chris@1266 202 }
Chris@411 203 }
Chris@411 204 return SingleColourLayer::getPropertyValueLabel(name, value);
Chris@411 205 }
Chris@411 206
Chris@411 207 void
Chris@411 208 RegionLayer::setProperty(const PropertyName &name, int value)
Chris@411 209 {
Chris@427 210 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
Chris@427 211 setFillColourMap(value);
Chris@427 212 } else if (name == "Plot Type") {
Chris@1266 213 setPlotStyle(PlotStyle(value));
Chris@412 214 } else if (name == "Vertical Scale") {
Chris@1266 215 setVerticalScale(VerticalScale(value));
Chris@411 216 } else if (name == "Scale Units") {
Chris@411 217 if (m_model) {
Chris@411 218 m_model->setScaleUnits
Chris@411 219 (UnitDatabase::getInstance()->getUnitById(value));
Chris@411 220 emit modelChanged();
Chris@411 221 }
Chris@411 222 } else {
Chris@411 223 return SingleColourLayer::setProperty(name, value);
Chris@411 224 }
Chris@411 225 }
Chris@411 226
Chris@411 227 void
Chris@427 228 RegionLayer::setFillColourMap(int map)
Chris@427 229 {
Chris@427 230 if (m_colourMap == map) return;
Chris@427 231 m_colourMap = map;
Chris@427 232 emit layerParametersChanged();
Chris@427 233 }
Chris@427 234
Chris@427 235 void
Chris@412 236 RegionLayer::setPlotStyle(PlotStyle style)
Chris@412 237 {
Chris@412 238 if (m_plotStyle == style) return;
Chris@427 239 bool colourTypeChanged = (style == PlotSegmentation ||
Chris@427 240 m_plotStyle == PlotSegmentation);
Chris@412 241 m_plotStyle = style;
Chris@427 242 if (colourTypeChanged) {
Chris@427 243 emit layerParameterRangesChanged();
Chris@427 244 }
Chris@412 245 emit layerParametersChanged();
Chris@412 246 }
Chris@412 247
Chris@412 248 void
Chris@411 249 RegionLayer::setVerticalScale(VerticalScale scale)
Chris@411 250 {
Chris@411 251 if (m_verticalScale == scale) return;
Chris@411 252 m_verticalScale = scale;
Chris@411 253 emit layerParametersChanged();
Chris@411 254 }
Chris@411 255
Chris@411 256 bool
Chris@918 257 RegionLayer::isLayerScrollable(const LayerGeometryProvider *v) const
Chris@411 258 {
Chris@411 259 QPoint discard;
Chris@411 260 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@411 261 }
Chris@411 262
Chris@433 263 void
Chris@433 264 RegionLayer::recalcSpacing()
Chris@433 265 {
Chris@433 266 m_spacingMap.clear();
Chris@551 267 m_distributionMap.clear();
Chris@433 268 if (!m_model) return;
Chris@433 269
Chris@587 270 // SVDEBUG << "RegionLayer::recalcSpacing" << endl;
Chris@433 271
Chris@1428 272 EventVector allEvents = m_model->getAllEvents();
Chris@1428 273 for (const Event &e: allEvents) {
Chris@1428 274 m_distributionMap[e.getValue()]++;
Chris@1428 275 // SVDEBUG << "RegionLayer::recalcSpacing: value found: " << e.getValue() << " (now have " << m_distributionMap[e.getValue()] << " of this value)" << endl;
Chris@433 276 }
Chris@433 277
Chris@433 278 int n = 0;
Chris@433 279
Chris@551 280 for (SpacingMap::const_iterator i = m_distributionMap.begin();
Chris@551 281 i != m_distributionMap.end(); ++i) {
Chris@551 282 m_spacingMap[i->first] = n++;
Chris@587 283 // SVDEBUG << "RegionLayer::recalcSpacing: " << i->first << " -> " << m_spacingMap[i->first] << endl;
Chris@433 284 }
Chris@433 285 }
Chris@433 286
Chris@411 287 bool
Chris@905 288 RegionLayer::getValueExtents(double &min, double &max,
Chris@411 289 bool &logarithmic, QString &unit) const
Chris@411 290 {
Chris@411 291 if (!m_model) return false;
Chris@411 292 min = m_model->getValueMinimum();
Chris@411 293 max = m_model->getValueMaximum();
Chris@701 294 unit = getScaleUnits();
Chris@411 295
Chris@411 296 if (m_verticalScale == LogScale) logarithmic = true;
Chris@411 297
Chris@411 298 return true;
Chris@411 299 }
Chris@411 300
Chris@411 301 bool
Chris@905 302 RegionLayer::getDisplayExtents(double &min, double &max) const
Chris@411 303 {
Chris@433 304 if (!m_model ||
Chris@433 305 m_verticalScale == AutoAlignScale ||
Chris@433 306 m_verticalScale == EqualSpaced) return false;
Chris@411 307
Chris@411 308 min = m_model->getValueMinimum();
Chris@411 309 max = m_model->getValueMaximum();
Chris@411 310
Chris@411 311 return true;
Chris@411 312 }
Chris@411 313
Chris@1428 314 EventVector
Chris@918 315 RegionLayer::getLocalPoints(LayerGeometryProvider *v, int x) const
Chris@411 316 {
Chris@1428 317 if (!m_model) return EventVector();
Chris@411 318
Chris@989 319 sv_frame_t frame = v->getFrameForX(x);
Chris@411 320
Chris@1428 321 EventVector local = m_model->getEventsCovering(frame);
Chris@1428 322 if (!local.empty()) return local;
Chris@411 323
Chris@1428 324 int fuzz = ViewManager::scalePixelSize(2);
Chris@1428 325 sv_frame_t start = v->getFrameForX(x - fuzz);
Chris@1428 326 sv_frame_t end = v->getFrameForX(x + fuzz);
Chris@411 327
Chris@1428 328 local = m_model->getEventsStartingWithin(frame, end - frame);
Chris@1428 329 if (!local.empty()) return local;
Chris@411 330
Chris@1428 331 local = m_model->getEventsSpanning(start, frame - start);
Chris@1428 332 if (!local.empty()) return local;
Chris@411 333
Chris@1428 334 return {};
Chris@411 335 }
Chris@411 336
Chris@550 337 bool
Chris@1428 338 RegionLayer::getPointToDrag(LayerGeometryProvider *v, int x, int y, Event &point) const
Chris@550 339 {
Chris@550 340 if (!m_model) return false;
Chris@550 341
Chris@989 342 sv_frame_t frame = v->getFrameForX(x);
Chris@550 343
Chris@1428 344 EventVector onPoints = m_model->getEventsCovering(frame);
Chris@550 345 if (onPoints.empty()) return false;
Chris@550 346
Chris@550 347 int nearestDistance = -1;
Chris@1428 348 for (const auto &p: onPoints) {
Chris@1428 349 int distance = getYForValue(v, p.getValue()) - y;
Chris@550 350 if (distance < 0) distance = -distance;
Chris@550 351 if (nearestDistance == -1 || distance < nearestDistance) {
Chris@550 352 nearestDistance = distance;
Chris@1428 353 point = p;
Chris@550 354 }
Chris@550 355 }
Chris@550 356
Chris@550 357 return true;
Chris@550 358 }
Chris@550 359
Chris@411 360 QString
Chris@909 361 RegionLayer::getLabelPreceding(sv_frame_t frame) const
Chris@552 362 {
Chris@552 363 if (!m_model) return "";
Chris@1428 364 EventVector points = m_model->getEventsStartingWithin
Chris@1428 365 (m_model->getStartFrame(), frame - m_model->getStartFrame());
Chris@1428 366 if (!points.empty()) {
Chris@1428 367 for (auto i = points.rbegin(); i != points.rend(); ++i) {
Chris@1428 368 if (i->getLabel() != QString()) {
Chris@1428 369 return i->getLabel();
Chris@1428 370 }
Chris@1428 371 }
Chris@552 372 }
Chris@1428 373 return QString();
Chris@552 374 }
Chris@552 375
Chris@552 376 QString
Chris@918 377 RegionLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const
Chris@411 378 {
Chris@411 379 int x = pos.x();
Chris@411 380
Chris@411 381 if (!m_model || !m_model->getSampleRate()) return "";
Chris@411 382
Chris@1428 383 EventVector points = getLocalPoints(v, x);
Chris@411 384
Chris@411 385 if (points.empty()) {
Chris@1266 386 if (!m_model->isReady()) {
Chris@1266 387 return tr("In progress");
Chris@1266 388 } else {
Chris@1266 389 return tr("No local points");
Chris@1266 390 }
Chris@411 391 }
Chris@411 392
Chris@1428 393 Event region;
Chris@1428 394 EventVector::iterator i;
Chris@411 395
Chris@413 396 //!!! harmonise with whatever decision is made about point y
Chris@413 397 //!!! coords in paint method
Chris@413 398
Chris@411 399 for (i = points.begin(); i != points.end(); ++i) {
Chris@411 400
Chris@1428 401 int y = getYForValue(v, i->getValue());
Chris@1266 402 int h = 3;
Chris@411 403
Chris@1266 404 if (m_model->getValueQuantization() != 0.0) {
Chris@1428 405 h = y - getYForValue
Chris@1428 406 (v, i->getValue() + m_model->getValueQuantization());
Chris@1266 407 if (h < 3) h = 3;
Chris@1266 408 }
Chris@411 409
Chris@1266 410 if (pos.y() >= y - h && pos.y() <= y) {
Chris@1266 411 region = *i;
Chris@1266 412 break;
Chris@1266 413 }
Chris@411 414 }
Chris@411 415
Chris@411 416 if (i == points.end()) return tr("No local points");
Chris@411 417
Chris@1428 418 RealTime rt = RealTime::frame2RealTime(region.getFrame(),
Chris@1266 419 m_model->getSampleRate());
Chris@1428 420 RealTime rd = RealTime::frame2RealTime(region.getDuration(),
Chris@1266 421 m_model->getSampleRate());
Chris@411 422
Chris@411 423 QString valueText;
Chris@411 424
Chris@1428 425 valueText = tr("%1 %2").arg(region.getValue()).arg(getScaleUnits());
Chris@411 426
Chris@411 427 QString text;
Chris@411 428
Chris@1428 429 if (region.getLabel() == "") {
Chris@1266 430 text = QString(tr("Time:\t%1\nValue:\t%2\nDuration:\t%3\nNo label"))
Chris@1266 431 .arg(rt.toText(true).c_str())
Chris@1266 432 .arg(valueText)
Chris@1266 433 .arg(rd.toText(true).c_str());
Chris@411 434 } else {
Chris@1266 435 text = QString(tr("Time:\t%1\nValue:\t%2\nDuration:\t%3\nLabel:\t%4"))
Chris@1266 436 .arg(rt.toText(true).c_str())
Chris@1266 437 .arg(valueText)
Chris@1266 438 .arg(rd.toText(true).c_str())
Chris@1428 439 .arg(region.getLabel());
Chris@411 440 }
Chris@411 441
Chris@1428 442 pos = QPoint(v->getXForFrame(region.getFrame()),
Chris@1428 443 getYForValue(v, region.getValue()));
Chris@411 444 return text;
Chris@411 445 }
Chris@411 446
Chris@411 447 bool
Chris@918 448 RegionLayer::snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame,
Chris@805 449 int &resolution,
Chris@414 450 SnapType snap) const
Chris@411 451 {
Chris@411 452 if (!m_model) {
Chris@1266 453 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@411 454 }
Chris@411 455
Chris@1432 456 // SnapLeft / SnapRight: return frame of nearest feature in that
Chris@1432 457 // direction no matter how far away
Chris@1432 458 //
Chris@1432 459 // SnapNeighbouring: return frame of feature that would be used in
Chris@1432 460 // an editing operation, i.e. closest feature in either direction
Chris@1432 461 // but only if it is "close enough"
Chris@1432 462
Chris@411 463 resolution = m_model->getResolution();
Chris@411 464
Chris@411 465 if (snap == SnapNeighbouring) {
Chris@1432 466 EventVector points = getLocalPoints(v, v->getXForFrame(frame));
Chris@1266 467 if (points.empty()) return false;
Chris@1428 468 frame = points.begin()->getFrame();
Chris@1266 469 return true;
Chris@411 470 }
Chris@411 471
Chris@1432 472 // Normally we snap to the start frame of whichever event we
Chris@1432 473 // find. However here, for SnapRight only, if the end frame of
Chris@1432 474 // whichever event we would have snapped to had we been snapping
Chris@1432 475 // left is closer than the start frame of the next event to the
Chris@1432 476 // right, then we snap to that frame instead. Clear?
Chris@1428 477
Chris@1432 478 Event left;
Chris@1432 479 bool haveLeft = false;
Chris@1432 480 if (m_model->getNearestEventMatching
Chris@1432 481 (frame, [](Event) { return true; }, EventSeries::Backward, left)) {
Chris@1432 482 haveLeft = true;
Chris@1432 483 }
Chris@411 484
Chris@1432 485 if (snap == SnapLeft) {
Chris@1432 486 frame = left.getFrame();
Chris@1432 487 return haveLeft;
Chris@1432 488 }
Chris@411 489
Chris@1432 490 Event right;
Chris@1432 491 bool haveRight = false;
Chris@1432 492 if (m_model->getNearestEventMatching
Chris@1432 493 (frame, [](Event) { return true; }, EventSeries::Forward, right)) {
Chris@1432 494 haveRight = true;
Chris@1432 495 }
Chris@411 496
Chris@1432 497 if (haveLeft) {
Chris@1432 498 sv_frame_t leftEnd = left.getFrame() + left.getDuration();
Chris@1432 499 if (leftEnd > frame) {
Chris@1432 500 if (haveRight) {
Chris@1432 501 if (leftEnd - frame < right.getFrame() - frame) {
Chris@1432 502 frame = leftEnd;
Chris@1432 503 } else {
Chris@1432 504 frame = right.getFrame();
Chris@414 505 }
Chris@414 506 } else {
Chris@1432 507 frame = leftEnd;
Chris@414 508 }
Chris@1432 509 return true;
Chris@1266 510 }
Chris@411 511 }
Chris@411 512
Chris@1432 513 if (haveRight) {
Chris@1432 514 frame = right.getFrame();
Chris@1432 515 return true;
Chris@1432 516 }
Chris@1432 517
Chris@1432 518 return false;
Chris@411 519 }
Chris@411 520
Chris@559 521 bool
Chris@918 522 RegionLayer::snapToSimilarFeature(LayerGeometryProvider *v, sv_frame_t &frame,
Chris@805 523 int &resolution,
Chris@559 524 SnapType snap) const
Chris@559 525 {
Chris@559 526 if (!m_model) {
Chris@1266 527 return Layer::snapToSimilarFeature(v, frame, resolution, snap);
Chris@559 528 }
Chris@559 529
Chris@1432 530 // snap is only permitted to be SnapLeft or SnapRight here. We
Chris@1432 531 // don't do the same trick as in snapToFeatureFrame, of snapping
Chris@1432 532 // to the end of a feature sometimes.
Chris@1432 533
Chris@559 534 resolution = m_model->getResolution();
Chris@559 535
Chris@1432 536 Event ref;
Chris@1432 537 Event e;
Chris@1432 538 float matchvalue;
Chris@1432 539 bool found;
Chris@559 540
Chris@1432 541 found = m_model->getNearestEventMatching
Chris@1432 542 (frame, [](Event) { return true; }, EventSeries::Backward, ref);
Chris@559 543
Chris@1432 544 if (!found) {
Chris@1432 545 return false;
Chris@559 546 }
Chris@559 547
Chris@1432 548 matchvalue = ref.getValue();
Chris@1432 549
Chris@1432 550 found = m_model->getNearestEventMatching
Chris@1432 551 (frame,
Chris@1432 552 [matchvalue](Event e) {
Chris@1432 553 double epsilon = 0.0001;
Chris@1432 554 return fabs(e.getValue() - matchvalue) < epsilon;
Chris@1432 555 },
Chris@1432 556 snap == SnapLeft ? EventSeries::Backward : EventSeries::Forward,
Chris@1432 557 e);
Chris@559 558
Chris@1432 559 if (!found) {
Chris@1432 560 return false;
Chris@559 561 }
Chris@559 562
Chris@1432 563 frame = e.getFrame();
Chris@1432 564 return true;
Chris@559 565 }
Chris@559 566
Chris@701 567 QString
Chris@701 568 RegionLayer::getScaleUnits() const
Chris@701 569 {
Chris@701 570 if (m_model) return m_model->getScaleUnits();
Chris@701 571 else return "";
Chris@701 572 }
Chris@701 573
Chris@411 574 void
Chris@918 575 RegionLayer::getScaleExtents(LayerGeometryProvider *v, double &min, double &max, bool &log) const
Chris@411 576 {
Chris@411 577 min = 0.0;
Chris@411 578 max = 0.0;
Chris@411 579 log = false;
Chris@411 580
Chris@411 581 QString queryUnits;
Chris@701 582 queryUnits = getScaleUnits();
Chris@411 583
Chris@411 584 if (m_verticalScale == AutoAlignScale) {
Chris@411 585
Chris@411 586 if (!v->getValueExtents(queryUnits, min, max, log)) {
Chris@411 587
Chris@411 588 min = m_model->getValueMinimum();
Chris@411 589 max = m_model->getValueMaximum();
Chris@411 590
Chris@682 591 // cerr << "RegionLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl;
Chris@411 592
Chris@411 593 } else if (log) {
Chris@411 594
Chris@411 595 LogRange::mapRange(min, max);
Chris@411 596
Chris@682 597 // cerr << "RegionLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl;
Chris@411 598
Chris@411 599 }
Chris@411 600
Chris@433 601 } else if (m_verticalScale == EqualSpaced) {
Chris@433 602
Chris@433 603 if (!m_spacingMap.empty()) {
Chris@433 604 SpacingMap::const_iterator i = m_spacingMap.begin();
Chris@433 605 min = i->second;
Chris@433 606 i = m_spacingMap.end();
Chris@433 607 --i;
Chris@433 608 max = i->second;
Chris@682 609 // cerr << "RegionLayer[" << this << "]::getScaleExtents: equal spaced; min = " << min << ", max = " << max << ", log = " << log << endl;
Chris@433 610 }
Chris@433 611
Chris@411 612 } else {
Chris@411 613
Chris@411 614 min = m_model->getValueMinimum();
Chris@411 615 max = m_model->getValueMaximum();
Chris@411 616
Chris@411 617 if (m_verticalScale == LogScale) {
Chris@411 618 LogRange::mapRange(min, max);
Chris@411 619 log = true;
Chris@411 620 }
Chris@411 621 }
Chris@411 622
Chris@411 623 if (max == min) max = min + 1.0;
Chris@411 624 }
Chris@411 625
Chris@411 626 int
Chris@918 627 RegionLayer::spacingIndexToY(LayerGeometryProvider *v, int i) const
Chris@542 628 {
Chris@918 629 int h = v->getPaintHeight();
Chris@905 630 int n = int(m_spacingMap.size());
Chris@542 631 // this maps from i (spacing of the value from the spacing
Chris@542 632 // map) and n (number of region types) to y
Chris@542 633 int y = h - (((h * i) / n) + (h / (2 * n)));
Chris@542 634 return y;
Chris@542 635 }
Chris@542 636
Chris@905 637 double
Chris@918 638 RegionLayer::yToSpacingIndex(LayerGeometryProvider *v, int y) const
Chris@542 639 {
Chris@905 640 // we return an inexact result here (double rather than int)
Chris@918 641 int h = v->getPaintHeight();
Chris@905 642 int n = int(m_spacingMap.size());
Chris@551 643 // from y = h - ((h * i) / n) + (h / (2 * n)) as above (vh taking place of i)
Chris@905 644 double vh = double(2*h*n - h - 2*n*y) / double(2*h);
Chris@542 645 return vh;
Chris@542 646 }
Chris@542 647
Chris@542 648 int
Chris@918 649 RegionLayer::getYForValue(LayerGeometryProvider *v, double val) const
Chris@411 650 {
Chris@905 651 double min = 0.0, max = 0.0;
Chris@411 652 bool logarithmic = false;
Chris@918 653 int h = v->getPaintHeight();
Chris@411 654
Chris@433 655 if (m_verticalScale == EqualSpaced) {
Chris@433 656
Chris@433 657 if (m_spacingMap.empty()) return h/2;
Chris@433 658
Chris@433 659 SpacingMap::const_iterator i = m_spacingMap.lower_bound(val);
Chris@433 660 //!!! what now, if i->first != v?
Chris@433 661
Chris@542 662 int y = spacingIndexToY(v, i->second);
Chris@433 663
Chris@587 664 // SVDEBUG << "RegionLayer::getYForValue: value " << val << " -> i->second " << i->second << " -> y " << y << endl;
Chris@542 665 return y;
Chris@433 666
Chris@433 667
Chris@433 668 } else {
Chris@433 669
Chris@433 670 getScaleExtents(v, min, max, logarithmic);
Chris@411 671
Chris@682 672 // cerr << "RegionLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << endl;
Chris@682 673 // cerr << "h = " << h << ", margin = " << margin << endl;
Chris@411 674
Chris@433 675 if (logarithmic) {
Chris@433 676 val = LogRange::map(val);
Chris@433 677 }
Chris@433 678
Chris@433 679 return int(h - ((val - min) * h) / (max - min));
Chris@411 680 }
Chris@411 681 }
Chris@411 682
Chris@905 683 double
Chris@918 684 RegionLayer::getValueForY(LayerGeometryProvider *v, int y) const
Chris@701 685 {
Chris@701 686 return getValueForY(v, y, -1);
Chris@701 687 }
Chris@701 688
Chris@905 689 double
Chris@918 690 RegionLayer::getValueForY(LayerGeometryProvider *v, int y, int avoid) const
Chris@542 691 {
Chris@905 692 double min = 0.0, max = 0.0;
Chris@542 693 bool logarithmic = false;
Chris@918 694 int h = v->getPaintHeight();
Chris@542 695
Chris@542 696 if (m_verticalScale == EqualSpaced) {
Chris@542 697
Chris@542 698 // if we're equal spaced, we probably want to snap to the
Chris@542 699 // nearest item when close to it, and give some notification
Chris@542 700 // that we're doing so
Chris@542 701
Chris@542 702 if (m_spacingMap.empty()) return 1.f;
Chris@542 703
Chris@542 704 // n is the number of distinct regions. if we are close to
Chris@542 705 // one of the m/n divisions in the y scale, we should snap to
Chris@542 706 // the value of the mth region.
Chris@542 707
Chris@905 708 double vh = yToSpacingIndex(v, y);
Chris@542 709
Chris@542 710 // spacings in the map are integral, so find the closest one,
Chris@542 711 // map it back to its y coordinate, and see how far we are
Chris@542 712 // from it
Chris@542 713
Chris@905 714 int n = int(m_spacingMap.size());
Chris@905 715 int ivh = int(lrint(vh));
Chris@542 716 if (ivh < 0) ivh = 0;
Chris@542 717 if (ivh > n-1) ivh = n-1;
Chris@542 718 int iy = spacingIndexToY(v, ivh);
Chris@542 719
Chris@542 720 int dist = iy - y;
Chris@542 721 int gap = h / n; // between region lines
Chris@542 722
Chris@682 723 // cerr << "getValueForY: y = " << y << ", vh = " << vh << ", ivh = " << ivh << " of " << n << ", iy = " << iy << ", dist = " << dist << ", gap = " << gap << endl;
Chris@542 724
Chris@542 725 SpacingMap::const_iterator i = m_spacingMap.begin();
Chris@542 726 while (i != m_spacingMap.end()) {
Chris@542 727 if (i->second == ivh) break;
Chris@542 728 ++i;
Chris@542 729 }
Chris@542 730 if (i == m_spacingMap.end()) i = m_spacingMap.begin();
Chris@542 731
Chris@682 732 // cerr << "nearest existing value = " << i->first << " at " << iy << endl;
Chris@551 733
Chris@905 734 double val = 0;
Chris@542 735
Chris@682 736 // cerr << "note: avoid = " << avoid << ", i->second = " << i->second << endl;
Chris@551 737
Chris@551 738 if (dist < -gap/3 &&
Chris@551 739 ((avoid == -1) ||
Chris@551 740 (avoid != i->second && avoid != i->second - 1))) {
Chris@542 741 // bisect gap to prior
Chris@542 742 if (i == m_spacingMap.begin()) {
Chris@542 743 val = i->first - 1.f;
Chris@682 744 // cerr << "extended down to " << val << endl;
Chris@542 745 } else {
Chris@542 746 SpacingMap::const_iterator j = i;
Chris@542 747 --j;
Chris@542 748 val = (i->first + j->first) / 2;
Chris@682 749 // cerr << "bisected down to " << val << endl;
Chris@542 750 }
Chris@551 751 } else if (dist > gap/3 &&
Chris@551 752 ((avoid == -1) ||
Chris@551 753 (avoid != i->second && avoid != i->second + 1))) {
Chris@542 754 // bisect gap to following
Chris@542 755 SpacingMap::const_iterator j = i;
Chris@542 756 ++j;
Chris@542 757 if (j == m_spacingMap.end()) {
Chris@542 758 val = i->first + 1.f;
Chris@682 759 // cerr << "extended up to " << val << endl;
Chris@542 760 } else {
Chris@542 761 val = (i->first + j->first) / 2;
Chris@682 762 // cerr << "bisected up to " << val << endl;
Chris@542 763 }
Chris@551 764 } else {
Chris@551 765 // snap
Chris@551 766 val = i->first;
Chris@682 767 // cerr << "snapped to " << val << endl;
Chris@542 768 }
Chris@542 769
Chris@542 770 return val;
Chris@542 771
Chris@542 772 } else {
Chris@542 773
Chris@542 774 getScaleExtents(v, min, max, logarithmic);
Chris@542 775
Chris@905 776 double val = min + (double(h - y) * double(max - min)) / h;
Chris@542 777
Chris@542 778 if (logarithmic) {
Chris@905 779 val = pow(10.0, val);
Chris@542 780 }
Chris@542 781
Chris@542 782 return val;
Chris@542 783 }
Chris@542 784 }
Chris@542 785
Chris@427 786 QColor
Chris@918 787 RegionLayer::getColourForValue(LayerGeometryProvider *v, double val) const
Chris@427 788 {
Chris@905 789 double min, max;
Chris@427 790 bool log;
Chris@427 791 getScaleExtents(v, min, max, log);
Chris@427 792
Chris@427 793 if (min > max) std::swap(min, max);
Chris@427 794 if (max == min) max = min + 1;
Chris@427 795
Chris@427 796 if (log) {
Chris@427 797 LogRange::mapRange(min, max);
Chris@427 798 val = LogRange::map(val);
Chris@427 799 }
Chris@427 800
Chris@587 801 // SVDEBUG << "RegionLayer::getColourForValue: min " << min << ", max "
Chris@585 802 // << max << ", log " << log << ", value " << val << endl;
Chris@427 803
Chris@1362 804 QColor solid = ColourMapper(m_colourMap, m_colourInverted, min, max).map(val);
Chris@427 805 return QColor(solid.red(), solid.green(), solid.blue(), 120);
Chris@427 806 }
Chris@427 807
Chris@427 808 int
Chris@427 809 RegionLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@427 810 {
Chris@427 811 impose = false;
Chris@427 812 return ColourDatabase::getInstance()->getColourIndex
Chris@427 813 (QString(darkbg ? "Bright Blue" : "Blue"));
Chris@427 814 }
Chris@427 815
Chris@411 816 void
Chris@916 817 RegionLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@411 818 {
Chris@411 819 if (!m_model || !m_model->isOK()) return;
Chris@411 820
Chris@905 821 sv_samplerate_t sampleRate = m_model->getSampleRate();
Chris@411 822 if (!sampleRate) return;
Chris@411 823
Chris@411 824 // Profiler profiler("RegionLayer::paint", true);
Chris@411 825
Chris@552 826 int x0 = rect.left() - 40, x1 = rect.right();
Chris@411 827
Chris@1409 828 sv_frame_t wholeFrame0 = v->getFrameForX(0);
Chris@1409 829 sv_frame_t wholeFrame1 = v->getFrameForX(v->getPaintWidth());
Chris@1409 830
Chris@1428 831 EventVector points(m_model->getEventsSpanning(wholeFrame0,
Chris@1428 832 wholeFrame1 - wholeFrame0));
Chris@411 833 if (points.empty()) return;
Chris@411 834
Chris@411 835 paint.setPen(getBaseQColor());
Chris@411 836
Chris@411 837 QColor brushColour(getBaseQColor());
Chris@411 838 brushColour.setAlpha(80);
Chris@411 839
Chris@587 840 // SVDEBUG << "RegionLayer::paint: resolution is "
Chris@1266 841 // << m_model->getResolution() << " frames" << endl;
Chris@411 842
Chris@905 843 double min = m_model->getValueMinimum();
Chris@905 844 double max = m_model->getValueMaximum();
Chris@411 845 if (max == min) max = min + 1.0;
Chris@411 846
Chris@411 847 QPoint localPos;
Chris@1428 848 Event illuminatePoint(0);
Chris@551 849 bool shouldIlluminate = false;
Chris@411 850
Chris@411 851 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@551 852 shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(),
Chris@551 853 illuminatePoint);
Chris@411 854 }
Chris@411 855
Chris@411 856 paint.save();
Chris@411 857 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@411 858
Chris@413 859 //!!! point y coords if model does not haveDistinctValues() should
Chris@413 860 //!!! be assigned to avoid overlaps
Chris@413 861
Chris@413 862 //!!! if it does have distinct values, we should still ensure y
Chris@413 863 //!!! coord is never completely flat on the top or bottom
Chris@413 864
Chris@550 865 int fontHeight = paint.fontMetrics().height();
Chris@550 866
Chris@1428 867 for (EventVector::const_iterator i = points.begin();
Chris@1266 868 i != points.end(); ++i) {
Chris@411 869
Chris@1428 870 const Event &p(*i);
Chris@411 871
Chris@1428 872 int x = v->getXForFrame(p.getFrame());
Chris@1428 873 int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x;
Chris@1428 874 int y = getYForValue(v, p.getValue());
Chris@1266 875 int h = 9;
Chris@1266 876 int ex = x + w;
Chris@427 877
Chris@1409 878 int gap = v->scalePixelSize(2);
Chris@1409 879
Chris@1428 880 EventVector::const_iterator j = i;
Chris@1266 881 ++j;
Chris@427 882
Chris@1266 883 if (j != points.end()) {
Chris@1428 884 const Event &q(*j);
Chris@1428 885 int nx = v->getXForFrame(q.getFrame());
Chris@433 886 if (nx < ex) ex = nx;
Chris@427 887 }
Chris@427 888
Chris@1266 889 if (m_model->getValueQuantization() != 0.0) {
Chris@1428 890 h = y - getYForValue
Chris@1428 891 (v, p.getValue() + m_model->getValueQuantization());
Chris@1266 892 if (h < 3) h = 3;
Chris@1266 893 }
Chris@411 894
Chris@1266 895 if (w < 1) w = 1;
Chris@411 896
Chris@1266 897 if (m_plotStyle == PlotSegmentation) {
Chris@918 898 paint.setPen(getForegroundQColor(v->getView()));
Chris@1428 899 paint.setBrush(getColourForValue(v, p.getValue()));
Chris@427 900 } else {
Chris@427 901 paint.setPen(getBaseQColor());
Chris@427 902 paint.setBrush(brushColour);
Chris@427 903 }
Chris@427 904
Chris@1266 905 if (m_plotStyle == PlotSegmentation) {
Chris@427 906
Chris@1266 907 if (ex <= x) continue;
Chris@427 908
Chris@1428 909 if (!shouldIlluminate || illuminatePoint != p) {
Chris@551 910
Chris@918 911 paint.setPen(QPen(getForegroundQColor(v->getView()), 1));
Chris@918 912 paint.drawLine(x, 0, x, v->getPaintHeight());
Chris@551 913 paint.setPen(Qt::NoPen);
Chris@552 914
Chris@552 915 } else {
Chris@918 916 paint.setPen(QPen(getForegroundQColor(v->getView()), 2));
Chris@551 917 }
Chris@427 918
Chris@1409 919 paint.drawRect(x, -1, ex - x, v->getPaintHeight() + gap);
Chris@427 920
Chris@1266 921 } else {
Chris@427 922
Chris@1428 923 if (shouldIlluminate && illuminatePoint == p) {
Chris@551 924
Chris@551 925 paint.setPen(v->getForeground());
Chris@551 926 paint.setBrush(v->getForeground());
Chris@551 927
Chris@1428 928 QString vlabel =
Chris@1428 929 QString("%1%2").arg(p.getValue()).arg(getScaleUnits());
Chris@1078 930 PaintAssistant::drawVisibleText(v, paint,
Chris@1409 931 x - paint.fontMetrics().width(vlabel) - gap,
Chris@551 932 y + paint.fontMetrics().height()/2
Chris@551 933 - paint.fontMetrics().descent(),
Chris@1078 934 vlabel, PaintAssistant::OutlinedText);
Chris@551 935
Chris@551 936 QString hlabel = RealTime::frame2RealTime
Chris@1428 937 (p.getFrame(), m_model->getSampleRate()).toText(true).c_str();
Chris@1078 938 PaintAssistant::drawVisibleText(v, paint,
Chris@551 939 x,
Chris@1409 940 y - h/2 - paint.fontMetrics().descent() - gap,
Chris@1078 941 hlabel, PaintAssistant::OutlinedText);
Chris@427 942 }
Chris@551 943
Chris@427 944 paint.drawLine(x, y-1, x + w, y-1);
Chris@427 945 paint.drawLine(x, y+1, x + w, y+1);
Chris@427 946 paint.drawLine(x, y - h/2, x, y + h/2);
Chris@427 947 paint.drawLine(x+w, y - h/2, x + w, y + h/2);
Chris@427 948 }
Chris@552 949 }
Chris@552 950
Chris@552 951 int nextLabelMinX = -100;
Chris@552 952 int lastLabelY = 0;
Chris@552 953
Chris@1428 954 for (EventVector::const_iterator i = points.begin();
Chris@1266 955 i != points.end(); ++i) {
Chris@552 956
Chris@1428 957 const Event &p(*i);
Chris@552 958
Chris@1428 959 int x = v->getXForFrame(p.getFrame());
Chris@1428 960 int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x;
Chris@1428 961 int y = getYForValue(v, p.getValue());
Chris@552 962
Chris@1428 963 QString label = p.getLabel();
Chris@1409 964 if (label == "") {
Chris@1428 965 label = QString("%1%2").arg(p.getValue()).arg(getScaleUnits());
Chris@1409 966 }
Chris@1409 967 int labelWidth = paint.fontMetrics().width(label);
Chris@1409 968
Chris@1409 969 int gap = v->scalePixelSize(2);
Chris@1409 970
Chris@1409 971 if (m_plotStyle == PlotSegmentation) {
Chris@1409 972 if ((x + w < x0 && x + labelWidth + gap < x0) || x > x1) {
Chris@1409 973 continue;
Chris@1409 974 }
Chris@1409 975 } else {
Chris@1409 976 if (x + w < x0 || x - labelWidth - gap > x1) {
Chris@1409 977 continue;
Chris@1409 978 }
Chris@1409 979 }
Chris@1409 980
Chris@552 981 bool illuminated = false;
Chris@552 982
Chris@1266 983 if (m_plotStyle != PlotSegmentation) {
Chris@1428 984 if (shouldIlluminate && illuminatePoint == p) {
Chris@552 985 illuminated = true;
Chris@552 986 }
Chris@552 987 }
Chris@427 988
Chris@551 989 if (!illuminated) {
Chris@551 990
Chris@552 991 int labelX, labelY;
Chris@552 992
Chris@551 993 if (m_plotStyle != PlotSegmentation) {
Chris@1409 994 labelX = x - labelWidth - gap;
Chris@552 995 labelY = y + paint.fontMetrics().height()/2
Chris@552 996 - paint.fontMetrics().descent();
Chris@551 997 } else {
Chris@552 998 labelX = x + 5;
Chris@552 999 labelY = v->getTextLabelHeight(this, paint);
Chris@552 1000 if (labelX < nextLabelMinX) {
Chris@918 1001 if (lastLabelY < v->getPaintHeight()/2) {
Chris@552 1002 labelY = lastLabelY + fontHeight;
Chris@552 1003 }
Chris@552 1004 }
Chris@552 1005 lastLabelY = labelY;
Chris@1409 1006 nextLabelMinX = labelX + labelWidth;
Chris@551 1007 }
Chris@552 1008
Chris@1409 1009 PaintAssistant::drawVisibleText(v, paint, labelX, labelY, label,
Chris@1409 1010 PaintAssistant::OutlinedText);
Chris@550 1011 }
Chris@411 1012 }
Chris@411 1013
Chris@411 1014 paint.restore();
Chris@411 1015 }
Chris@411 1016
Chris@701 1017 int
Chris@918 1018 RegionLayer::getVerticalScaleWidth(LayerGeometryProvider *v, bool, QPainter &paint) const
Chris@701 1019 {
Chris@701 1020 if (!m_model ||
Chris@701 1021 m_verticalScale == AutoAlignScale ||
Chris@701 1022 m_verticalScale == EqualSpaced) {
Chris@701 1023 return 0;
Chris@701 1024 } else if (m_plotStyle == PlotSegmentation) {
Chris@701 1025 if (m_verticalScale == LogScale) {
Chris@701 1026 return LogColourScale().getWidth(v, paint);
Chris@701 1027 } else {
Chris@701 1028 return LinearColourScale().getWidth(v, paint);
Chris@701 1029 }
Chris@701 1030 } else {
Chris@701 1031 if (m_verticalScale == LogScale) {
Chris@701 1032 return LogNumericalScale().getWidth(v, paint);
Chris@701 1033 } else {
Chris@701 1034 return LinearNumericalScale().getWidth(v, paint);
Chris@701 1035 }
Chris@701 1036 }
Chris@701 1037 }
Chris@701 1038
Chris@701 1039 void
Chris@918 1040 RegionLayer::paintVerticalScale(LayerGeometryProvider *v, bool, QPainter &paint, QRect) const
Chris@701 1041 {
Chris@1428 1042 if (!m_model || m_model->isEmpty()) return;
Chris@701 1043
Chris@701 1044 QString unit;
Chris@905 1045 double min, max;
Chris@701 1046 bool logarithmic;
Chris@701 1047
Chris@701 1048 int w = getVerticalScaleWidth(v, false, paint);
Chris@701 1049
Chris@701 1050 if (m_plotStyle == PlotSegmentation) {
Chris@701 1051
Chris@701 1052 getValueExtents(min, max, logarithmic, unit);
Chris@701 1053
Chris@701 1054 if (logarithmic) {
Chris@701 1055 LogRange::mapRange(min, max);
Chris@701 1056 LogColourScale().paintVertical(v, this, paint, 0, min, max);
Chris@701 1057 } else {
Chris@701 1058 LinearColourScale().paintVertical(v, this, paint, 0, min, max);
Chris@701 1059 }
Chris@701 1060
Chris@701 1061 } else {
Chris@701 1062
Chris@701 1063 getScaleExtents(v, min, max, logarithmic);
Chris@701 1064
Chris@701 1065 if (logarithmic) {
Chris@701 1066 LogNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@701 1067 } else {
Chris@701 1068 LinearNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@701 1069 }
Chris@701 1070 }
Chris@701 1071
Chris@701 1072 if (getScaleUnits() != "") {
Chris@701 1073 int mw = w - 5;
Chris@701 1074 paint.drawText(5,
Chris@701 1075 5 + paint.fontMetrics().ascent(),
Chris@701 1076 TextAbbrev::abbreviate(getScaleUnits(),
Chris@701 1077 paint.fontMetrics(),
Chris@701 1078 mw));
Chris@701 1079 }
Chris@701 1080 }
Chris@701 1081
Chris@411 1082 void
Chris@918 1083 RegionLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@411 1084 {
Chris@411 1085 if (!m_model) return;
Chris@411 1086
Chris@989 1087 sv_frame_t frame = v->getFrameForX(e->x());
Chris@411 1088 if (frame < 0) frame = 0;
Chris@411 1089 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@411 1090
Chris@905 1091 double value = getValueForY(v, e->y());
Chris@411 1092
Chris@1428 1093 m_editingPoint = Event(frame, float(value), 0, "");
Chris@411 1094 m_originalPoint = m_editingPoint;
Chris@411 1095
Chris@411 1096 if (m_editingCommand) finish(m_editingCommand);
Chris@1428 1097 m_editingCommand = new ChangeEventsCommand(m_model,
Chris@543 1098 tr("Draw Region"));
Chris@1428 1099 m_editingCommand->add(m_editingPoint);
Chris@411 1100
Chris@550 1101 recalcSpacing();
Chris@550 1102
Chris@411 1103 m_editing = true;
Chris@411 1104 }
Chris@411 1105
Chris@411 1106 void
Chris@918 1107 RegionLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e)
Chris@411 1108 {
Chris@411 1109 if (!m_model || !m_editing) return;
Chris@411 1110
Chris@905 1111 sv_frame_t frame = v->getFrameForX(e->x());
Chris@411 1112 if (frame < 0) frame = 0;
Chris@411 1113 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@411 1114
Chris@1428 1115 double newValue = m_editingPoint.getValue();
Chris@542 1116 if (m_verticalScale != EqualSpaced) newValue = getValueForY(v, e->y());
Chris@411 1117
Chris@1428 1118 sv_frame_t newFrame = m_editingPoint.getFrame();
Chris@905 1119 sv_frame_t newDuration = frame - newFrame;
Chris@411 1120 if (newDuration < 0) {
Chris@411 1121 newFrame = frame;
Chris@411 1122 newDuration = -newDuration;
Chris@411 1123 } else if (newDuration == 0) {
Chris@411 1124 newDuration = 1;
Chris@411 1125 }
Chris@411 1126
Chris@1428 1127 m_editingCommand->remove(m_editingPoint);
Chris@1428 1128 m_editingPoint = m_editingPoint
Chris@1428 1129 .withFrame(newFrame)
Chris@1428 1130 .withValue(float(newValue))
Chris@1428 1131 .withDuration(newDuration);
Chris@1428 1132 m_editingCommand->add(m_editingPoint);
Chris@550 1133
Chris@551 1134 recalcSpacing();
Chris@411 1135 }
Chris@411 1136
Chris@411 1137 void
Chris@918 1138 RegionLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *)
Chris@411 1139 {
Chris@411 1140 if (!m_model || !m_editing) return;
Chris@411 1141 finish(m_editingCommand);
Chris@1408 1142 m_editingCommand = nullptr;
Chris@411 1143 m_editing = false;
Chris@550 1144
Chris@550 1145 recalcSpacing();
Chris@411 1146 }
Chris@411 1147
Chris@411 1148 void
Chris@918 1149 RegionLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@411 1150 {
Chris@411 1151 if (!m_model) return;
Chris@411 1152
Chris@550 1153 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@411 1154
Chris@411 1155 if (m_editingCommand) {
Chris@1266 1156 finish(m_editingCommand);
Chris@1408 1157 m_editingCommand = nullptr;
Chris@411 1158 }
Chris@411 1159
Chris@411 1160 m_editing = true;
Chris@550 1161 recalcSpacing();
Chris@411 1162 }
Chris@411 1163
Chris@411 1164 void
Chris@918 1165 RegionLayer::eraseDrag(LayerGeometryProvider *, QMouseEvent *)
Chris@411 1166 {
Chris@411 1167 }
Chris@411 1168
Chris@411 1169 void
Chris@918 1170 RegionLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e)
Chris@411 1171 {
Chris@411 1172 if (!m_model || !m_editing) return;
Chris@411 1173
Chris@411 1174 m_editing = false;
Chris@551 1175
Chris@1428 1176 Event p(0);
Chris@550 1177 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
Chris@1428 1178 if (p.getFrame() != m_editingPoint.getFrame() ||
Chris@1428 1179 p.getValue() != m_editingPoint.getValue()) return;
Chris@411 1180
Chris@1428 1181 m_editingCommand = new ChangeEventsCommand
Chris@543 1182 (m_model, tr("Erase Region"));
Chris@411 1183
Chris@1428 1184 m_editingCommand->remove(m_editingPoint);
Chris@411 1185
Chris@411 1186 finish(m_editingCommand);
Chris@1408 1187 m_editingCommand = nullptr;
Chris@411 1188 m_editing = false;
Chris@550 1189 recalcSpacing();
Chris@411 1190 }
Chris@411 1191
Chris@411 1192 void
Chris@918 1193 RegionLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@411 1194 {
Chris@411 1195 if (!m_model) return;
Chris@411 1196
Chris@550 1197 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) {
Chris@550 1198 return;
Chris@550 1199 }
Chris@550 1200
Chris@1428 1201 m_dragPointX = v->getXForFrame(m_editingPoint.getFrame());
Chris@1428 1202 m_dragPointY = getYForValue(v, m_editingPoint.getValue());
Chris@551 1203
Chris@411 1204 m_originalPoint = m_editingPoint;
Chris@411 1205
Chris@411 1206 if (m_editingCommand) {
Chris@1266 1207 finish(m_editingCommand);
Chris@1408 1208 m_editingCommand = nullptr;
Chris@411 1209 }
Chris@411 1210
Chris@411 1211 m_editing = true;
Chris@551 1212 m_dragStartX = e->x();
Chris@551 1213 m_dragStartY = e->y();
Chris@550 1214 recalcSpacing();
Chris@411 1215 }
Chris@411 1216
Chris@411 1217 void
Chris@918 1218 RegionLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e)
Chris@411 1219 {
Chris@411 1220 if (!m_model || !m_editing) return;
Chris@411 1221
Chris@551 1222 int xdist = e->x() - m_dragStartX;
Chris@551 1223 int ydist = e->y() - m_dragStartY;
Chris@551 1224 int newx = m_dragPointX + xdist;
Chris@551 1225 int newy = m_dragPointY + ydist;
Chris@551 1226
Chris@989 1227 sv_frame_t frame = v->getFrameForX(newx);
Chris@411 1228 if (frame < 0) frame = 0;
Chris@411 1229 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@411 1230
Chris@551 1231 // Do not bisect between two values, if one of those values is
Chris@551 1232 // that of the point we're actually moving ...
Chris@1428 1233 int avoid = m_spacingMap[m_editingPoint.getValue()];
Chris@551 1234
Chris@551 1235 // ... unless there are other points with the same value
Chris@1428 1236 if (m_distributionMap[m_editingPoint.getValue()] > 1) avoid = -1;
Chris@551 1237
Chris@905 1238 double value = getValueForY(v, newy, avoid);
Chris@411 1239
Chris@411 1240 if (!m_editingCommand) {
Chris@1428 1241 m_editingCommand = new ChangeEventsCommand(m_model,
Chris@1266 1242 tr("Drag Region"));
Chris@411 1243 }
Chris@411 1244
Chris@1428 1245 m_editingCommand->remove(m_editingPoint);
Chris@1428 1246 m_editingPoint = m_editingPoint
Chris@1428 1247 .withFrame(frame)
Chris@1428 1248 .withValue(float(value));
Chris@1428 1249 m_editingCommand->add(m_editingPoint);
Chris@550 1250 recalcSpacing();
Chris@411 1251 }
Chris@411 1252
Chris@411 1253 void
Chris@918 1254 RegionLayer::editEnd(LayerGeometryProvider *, QMouseEvent *)
Chris@411 1255 {
Chris@411 1256 if (!m_model || !m_editing) return;
Chris@411 1257
Chris@411 1258 if (m_editingCommand) {
Chris@411 1259
Chris@1266 1260 QString newName = m_editingCommand->getName();
Chris@411 1261
Chris@1428 1262 if (m_editingPoint.getFrame() != m_originalPoint.getFrame()) {
Chris@1428 1263 if (m_editingPoint.getValue() != m_originalPoint.getValue()) {
Chris@1266 1264 newName = tr("Edit Region");
Chris@1266 1265 } else {
Chris@1266 1266 newName = tr("Relocate Region");
Chris@1266 1267 }
Chris@1266 1268 } else {
Chris@1266 1269 newName = tr("Change Point Value");
Chris@1266 1270 }
Chris@411 1271
Chris@1266 1272 m_editingCommand->setName(newName);
Chris@1266 1273 finish(m_editingCommand);
Chris@411 1274 }
Chris@411 1275
Chris@1408 1276 m_editingCommand = nullptr;
Chris@411 1277 m_editing = false;
Chris@550 1278 recalcSpacing();
Chris@411 1279 }
Chris@411 1280
Chris@411 1281 bool
Chris@918 1282 RegionLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e)
Chris@411 1283 {
Chris@411 1284 if (!m_model) return false;
Chris@411 1285
Chris@1428 1286 Event region(0);
Chris@550 1287 if (!getPointToDrag(v, e->x(), e->y(), region)) return false;
Chris@550 1288
Chris@411 1289 ItemEditDialog *dialog = new ItemEditDialog
Chris@411 1290 (m_model->getSampleRate(),
Chris@411 1291 ItemEditDialog::ShowTime |
Chris@411 1292 ItemEditDialog::ShowDuration |
Chris@411 1293 ItemEditDialog::ShowValue |
Chris@411 1294 ItemEditDialog::ShowText,
Chris@701 1295 getScaleUnits());
Chris@411 1296
Chris@1428 1297 dialog->setFrameTime(region.getFrame());
Chris@1428 1298 dialog->setValue(region.getValue());
Chris@1428 1299 dialog->setFrameDuration(region.getDuration());
Chris@1428 1300 dialog->setText(region.getLabel());
Chris@411 1301
Chris@411 1302 if (dialog->exec() == QDialog::Accepted) {
Chris@411 1303
Chris@1428 1304 Event newRegion = region
Chris@1428 1305 .withFrame(dialog->getFrameTime())
Chris@1428 1306 .withValue(dialog->getValue())
Chris@1428 1307 .withDuration(dialog->getFrameDuration())
Chris@1428 1308 .withLabel(dialog->getText());
Chris@411 1309
Chris@1428 1310 ChangeEventsCommand *command = new ChangeEventsCommand
Chris@543 1311 (m_model, tr("Edit Region"));
Chris@1428 1312 command->remove(region);
Chris@1428 1313 command->add(newRegion);
Chris@411 1314 finish(command);
Chris@411 1315 }
Chris@411 1316
Chris@411 1317 delete dialog;
Chris@550 1318 recalcSpacing();
Chris@411 1319 return true;
Chris@411 1320 }
Chris@411 1321
Chris@411 1322 void
Chris@905 1323 RegionLayer::moveSelection(Selection s, sv_frame_t newStartFrame)
Chris@411 1324 {
Chris@411 1325 if (!m_model) return;
Chris@411 1326
Chris@1428 1327 ChangeEventsCommand *command =
Chris@1428 1328 new ChangeEventsCommand(m_model, tr("Drag Selection"));
Chris@411 1329
Chris@1428 1330 EventVector points =
Chris@1428 1331 m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@411 1332
Chris@1428 1333 for (EventVector::iterator i = points.begin();
Chris@1266 1334 i != points.end(); ++i) {
Chris@411 1335
Chris@1429 1336 Event newPoint = (*i)
Chris@1429 1337 .withFrame(i->getFrame() + newStartFrame - s.getStartFrame());
Chris@1429 1338 command->remove(*i);
Chris@1429 1339 command->add(newPoint);
Chris@411 1340 }
Chris@411 1341
Chris@411 1342 finish(command);
Chris@550 1343 recalcSpacing();
Chris@411 1344 }
Chris@411 1345
Chris@411 1346 void
Chris@411 1347 RegionLayer::resizeSelection(Selection s, Selection newSize)
Chris@411 1348 {
Chris@1428 1349 if (!m_model || !s.getDuration()) return;
Chris@411 1350
Chris@1428 1351 ChangeEventsCommand *command =
Chris@1428 1352 new ChangeEventsCommand(m_model, tr("Resize Selection"));
Chris@411 1353
Chris@1428 1354 EventVector points =
Chris@1428 1355 m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@411 1356
Chris@1428 1357 double ratio = double(newSize.getDuration()) / double(s.getDuration());
Chris@1428 1358 double oldStart = double(s.getStartFrame());
Chris@1428 1359 double newStart = double(newSize.getStartFrame());
Chris@1428 1360
Chris@1428 1361 for (Event p: points) {
Chris@411 1362
Chris@1428 1363 double newFrame = (double(p.getFrame()) - oldStart) * ratio + newStart;
Chris@1428 1364 double newDuration = double(p.getDuration()) * ratio;
Chris@411 1365
Chris@1428 1366 Event newPoint = p
Chris@1428 1367 .withFrame(lrint(newFrame))
Chris@1428 1368 .withDuration(lrint(newDuration));
Chris@1428 1369 command->remove(p);
Chris@1428 1370 command->add(newPoint);
Chris@411 1371 }
Chris@411 1372
Chris@411 1373 finish(command);
Chris@550 1374 recalcSpacing();
Chris@411 1375 }
Chris@411 1376
Chris@411 1377 void
Chris@411 1378 RegionLayer::deleteSelection(Selection s)
Chris@411 1379 {
Chris@411 1380 if (!m_model) return;
Chris@411 1381
Chris@1428 1382 ChangeEventsCommand *command =
Chris@1428 1383 new ChangeEventsCommand(m_model, tr("Delete Selected Points"));
Chris@411 1384
Chris@1428 1385 EventVector points =
Chris@1428 1386 m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@411 1387
Chris@1428 1388 for (EventVector::iterator i = points.begin();
Chris@1266 1389 i != points.end(); ++i) {
Chris@411 1390
Chris@1428 1391 if (s.contains(i->getFrame())) {
Chris@1428 1392 command->remove(*i);
Chris@411 1393 }
Chris@411 1394 }
Chris@411 1395
Chris@411 1396 finish(command);
Chris@550 1397 recalcSpacing();
Chris@411 1398 }
Chris@411 1399
Chris@411 1400 void
Chris@918 1401 RegionLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to)
Chris@411 1402 {
Chris@411 1403 if (!m_model) return;
Chris@411 1404
Chris@1428 1405 EventVector points =
Chris@1428 1406 m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@411 1407
Chris@1428 1408 for (Event p: points) {
Chris@1428 1409 to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame())));
Chris@411 1410 }
Chris@411 1411 }
Chris@411 1412
Chris@411 1413 bool
Chris@918 1414 RegionLayer::paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t /* frameOffset */, bool /* interactive */)
Chris@411 1415 {
Chris@411 1416 if (!m_model) return false;
Chris@411 1417
Chris@1423 1418 const EventVector &points = from.getPoints();
Chris@411 1419
Chris@411 1420 bool realign = false;
Chris@411 1421
Chris@411 1422 if (clipboardHasDifferentAlignment(v, from)) {
Chris@411 1423
Chris@411 1424 QMessageBox::StandardButton button =
Chris@918 1425 QMessageBox::question(v->getView(), tr("Re-align pasted items?"),
Chris@411 1426 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 1427 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@411 1428 QMessageBox::Yes);
Chris@411 1429
Chris@411 1430 if (button == QMessageBox::Cancel) {
Chris@411 1431 return false;
Chris@411 1432 }
Chris@411 1433
Chris@411 1434 if (button == QMessageBox::Yes) {
Chris@411 1435 realign = true;
Chris@411 1436 }
Chris@411 1437 }
Chris@411 1438
Chris@1428 1439 ChangeEventsCommand *command =
Chris@1428 1440 new ChangeEventsCommand(m_model, tr("Paste"));
Chris@411 1441
Chris@1423 1442 for (EventVector::const_iterator i = points.begin();
Chris@411 1443 i != points.end(); ++i) {
Chris@411 1444
Chris@905 1445 sv_frame_t frame = 0;
Chris@411 1446
Chris@411 1447 if (!realign) {
Chris@411 1448
Chris@411 1449 frame = i->getFrame();
Chris@411 1450
Chris@411 1451 } else {
Chris@411 1452
Chris@1423 1453 if (i->hasReferenceFrame()) {
Chris@411 1454 frame = i->getReferenceFrame();
Chris@411 1455 frame = alignFromReference(v, frame);
Chris@411 1456 } else {
Chris@411 1457 frame = i->getFrame();
Chris@411 1458 }
Chris@411 1459 }
Chris@411 1460
Chris@1428 1461 Event p = *i;
Chris@1428 1462 Event newPoint = p;
Chris@1428 1463 if (!p.hasValue()) {
Chris@1428 1464 newPoint = newPoint.withValue((m_model->getValueMinimum() +
Chris@1428 1465 m_model->getValueMaximum()) / 2);
Chris@1428 1466 }
Chris@1428 1467 if (!p.hasDuration()) {
Chris@905 1468 sv_frame_t nextFrame = frame;
Chris@1423 1469 EventVector::const_iterator j = i;
Chris@411 1470 for (; j != points.end(); ++j) {
Chris@411 1471 if (j != i) break;
Chris@411 1472 }
Chris@411 1473 if (j != points.end()) {
Chris@411 1474 nextFrame = j->getFrame();
Chris@411 1475 }
Chris@411 1476 if (nextFrame == frame) {
Chris@1428 1477 newPoint = newPoint.withDuration(m_model->getResolution());
Chris@411 1478 } else {
Chris@1428 1479 newPoint = newPoint.withDuration(nextFrame - frame);
Chris@411 1480 }
Chris@411 1481 }
Chris@411 1482
Chris@1428 1483 command->add(newPoint);
Chris@411 1484 }
Chris@411 1485
Chris@411 1486 finish(command);
Chris@550 1487 recalcSpacing();
Chris@411 1488 return true;
Chris@411 1489 }
Chris@411 1490
Chris@411 1491 void
Chris@411 1492 RegionLayer::toXml(QTextStream &stream,
Chris@411 1493 QString indent, QString extraAttributes) const
Chris@411 1494 {
Chris@1362 1495 QString s;
Chris@1362 1496
Chris@1362 1497 s += QString("verticalScale=\"%1\" "
Chris@1362 1498 "plotStyle=\"%2\" ")
Chris@1362 1499 .arg(m_verticalScale)
Chris@1362 1500 .arg(m_plotStyle);
Chris@1362 1501
Chris@1362 1502 // New-style colour map attribute, by string id rather than by
Chris@1362 1503 // number
Chris@1362 1504
Chris@1362 1505 s += QString("fillColourMap=\"%1\" ")
Chris@1362 1506 .arg(ColourMapper::getColourMapId(m_colourMap));
Chris@1362 1507
Chris@1362 1508 // Old-style colour map attribute
Chris@1362 1509
Chris@1362 1510 s += QString("colourMap=\"%1\" ")
Chris@1362 1511 .arg(ColourMapper::getBackwardCompatibilityColourMap(m_colourMap));
Chris@1362 1512
Chris@1362 1513 SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s);
Chris@411 1514 }
Chris@411 1515
Chris@411 1516 void
Chris@411 1517 RegionLayer::setProperties(const QXmlAttributes &attributes)
Chris@411 1518 {
Chris@411 1519 SingleColourLayer::setProperties(attributes);
Chris@411 1520
Chris@411 1521 bool ok;
Chris@411 1522 VerticalScale scale = (VerticalScale)
Chris@1266 1523 attributes.value("verticalScale").toInt(&ok);
Chris@411 1524 if (ok) setVerticalScale(scale);
Chris@412 1525 PlotStyle style = (PlotStyle)
Chris@1266 1526 attributes.value("plotStyle").toInt(&ok);
Chris@412 1527 if (ok) setPlotStyle(style);
Chris@1362 1528
Chris@1362 1529 QString colourMapId = attributes.value("fillColourMap");
Chris@1362 1530 int colourMap = ColourMapper::getColourMapById(colourMapId);
Chris@1362 1531 if (colourMap >= 0) {
Chris@1362 1532 setFillColourMap(colourMap);
Chris@1362 1533 } else {
Chris@1362 1534 colourMap = attributes.value("colourMap").toInt(&ok);
Chris@1362 1535 if (ok && colourMap < ColourMapper::getColourMapCount()) {
Chris@1362 1536 setFillColourMap(colourMap);
Chris@1362 1537 }
Chris@1362 1538 }
Chris@411 1539 }
Chris@411 1540
Chris@411 1541