annotate layer/BoxLayer.cpp @ 1535:873ff035364c

Tidy / debug
author Chris Cannam
date Mon, 14 Oct 2019 14:20:02 +0100
parents 37df1530519d
children 4f8c72adbf43
rev   line source
Chris@1511 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1511 2
Chris@1511 3 /*
Chris@1511 4 Sonic Visualiser
Chris@1511 5 An audio file viewer and annotation editor.
Chris@1511 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1511 7
Chris@1511 8 This program is free software; you can redistribute it and/or
Chris@1511 9 modify it under the terms of the GNU General Public License as
Chris@1511 10 published by the Free Software Foundation; either version 2 of the
Chris@1511 11 License, or (at your option) any later version. See the file
Chris@1511 12 COPYING included with this distribution for more information.
Chris@1511 13 */
Chris@1511 14
Chris@1518 15 #include "BoxLayer.h"
Chris@1511 16
Chris@1511 17 #include "data/model/Model.h"
Chris@1511 18 #include "base/RealTime.h"
Chris@1511 19 #include "base/Profiler.h"
Chris@1511 20 #include "base/LogRange.h"
Chris@1511 21
Chris@1511 22 #include "ColourDatabase.h"
Chris@1511 23 #include "ColourMapper.h"
Chris@1511 24 #include "LinearNumericalScale.h"
Chris@1511 25 #include "LogNumericalScale.h"
Chris@1511 26 #include "PaintAssistant.h"
Chris@1511 27
Chris@1511 28 #include "view/View.h"
Chris@1511 29
Chris@1518 30 #include "data/model/BoxModel.h"
Chris@1511 31
Chris@1511 32 #include "widgets/ItemEditDialog.h"
Chris@1511 33 #include "widgets/TextAbbrev.h"
Chris@1511 34
Chris@1511 35 #include <QPainter>
Chris@1511 36 #include <QPainterPath>
Chris@1511 37 #include <QMouseEvent>
Chris@1511 38 #include <QTextStream>
Chris@1511 39 #include <QMessageBox>
Chris@1511 40
Chris@1511 41 #include <iostream>
Chris@1511 42 #include <cmath>
Chris@1511 43
Chris@1518 44 BoxLayer::BoxLayer() :
Chris@1511 45 SingleColourLayer(),
Chris@1511 46 m_editing(false),
Chris@1511 47 m_dragPointX(0),
Chris@1511 48 m_dragPointY(0),
Chris@1511 49 m_dragStartX(0),
Chris@1511 50 m_dragStartY(0),
Chris@1511 51 m_originalPoint(0, 0.0, 0, tr("New Box")),
Chris@1511 52 m_editingPoint(0, 0.0, 0, tr("New Box")),
Chris@1518 53 m_editingCommand(nullptr),
Chris@1518 54 m_verticalScale(AutoAlignScale)
Chris@1511 55 {
Chris@1511 56
Chris@1511 57 }
Chris@1511 58
Chris@1511 59 int
Chris@1518 60 BoxLayer::getCompletion(LayerGeometryProvider *) const
Chris@1511 61 {
Chris@1511 62 auto model = ModelById::get(m_model);
Chris@1511 63 if (model) return model->getCompletion();
Chris@1511 64 else return 0;
Chris@1511 65 }
Chris@1511 66
Chris@1511 67 void
Chris@1518 68 BoxLayer::setModel(ModelId modelId)
Chris@1511 69 {
Chris@1518 70 auto oldModel = ModelById::getAs<BoxModel>(m_model);
Chris@1518 71 auto newModel = ModelById::getAs<BoxModel>(modelId);
Chris@1511 72
Chris@1511 73 if (!modelId.isNone() && !newModel) {
Chris@1518 74 throw std::logic_error("Not a BoxModel");
Chris@1511 75 }
Chris@1511 76
Chris@1511 77 if (m_model == modelId) return;
Chris@1511 78 m_model = modelId;
Chris@1511 79
Chris@1511 80 if (newModel) {
Chris@1511 81 connectSignals(m_model);
Chris@1511 82 }
Chris@1511 83
Chris@1511 84 emit modelReplaced();
Chris@1511 85 }
Chris@1511 86
Chris@1511 87 Layer::PropertyList
Chris@1518 88 BoxLayer::getProperties() const
Chris@1511 89 {
Chris@1511 90 PropertyList list = SingleColourLayer::getProperties();
Chris@1511 91 list.push_back("Vertical Scale");
Chris@1518 92 list.push_back("Scale Units");
Chris@1511 93 return list;
Chris@1511 94 }
Chris@1511 95
Chris@1511 96 QString
Chris@1518 97 BoxLayer::getPropertyLabel(const PropertyName &name) const
Chris@1511 98 {
Chris@1511 99 if (name == "Vertical Scale") return tr("Vertical Scale");
Chris@1518 100 if (name == "Scale Units") return tr("Scale Units");
Chris@1511 101 return SingleColourLayer::getPropertyLabel(name);
Chris@1511 102 }
Chris@1511 103
Chris@1511 104 Layer::PropertyType
Chris@1518 105 BoxLayer::getPropertyType(const PropertyName &name) const
Chris@1511 106 {
Chris@1511 107 if (name == "Vertical Scale") return ValueProperty;
Chris@1518 108 if (name == "Scale Units") return UnitsProperty;
Chris@1511 109 return SingleColourLayer::getPropertyType(name);
Chris@1511 110 }
Chris@1511 111
Chris@1511 112 QString
Chris@1518 113 BoxLayer::getPropertyGroupName(const PropertyName &name) const
Chris@1511 114 {
Chris@1518 115 if (name == "Vertical Scale" || name == "Scale Units") {
Chris@1511 116 return tr("Scale");
Chris@1511 117 }
Chris@1511 118 return SingleColourLayer::getPropertyGroupName(name);
Chris@1511 119 }
Chris@1511 120
Chris@1511 121 int
Chris@1518 122 BoxLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@1511 123 int *min, int *max, int *deflt) const
Chris@1511 124 {
Chris@1511 125 int val = 0;
Chris@1511 126
Chris@1511 127 if (name == "Vertical Scale") {
Chris@1511 128
Chris@1511 129 if (min) *min = 0;
Chris@1511 130 if (max) *max = 2;
Chris@1511 131 if (deflt) *deflt = int(LinearScale);
Chris@1511 132
Chris@1511 133 val = int(m_verticalScale);
Chris@1511 134
Chris@1518 135 } else if (name == "Scale Units") {
Chris@1518 136
Chris@1518 137 if (deflt) *deflt = 0;
Chris@1518 138 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1518 139 if (model) {
Chris@1518 140 val = UnitDatabase::getInstance()->getUnitId
Chris@1518 141 (model->getScaleUnits());
Chris@1518 142 }
Chris@1518 143
Chris@1511 144 } else {
Chris@1511 145 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@1511 146 }
Chris@1511 147
Chris@1511 148 return val;
Chris@1511 149 }
Chris@1511 150
Chris@1511 151 QString
Chris@1518 152 BoxLayer::getPropertyValueLabel(const PropertyName &name,
Chris@1511 153 int value) const
Chris@1511 154 {
Chris@1511 155 if (name == "Vertical Scale") {
Chris@1511 156 switch (value) {
Chris@1511 157 default:
Chris@1511 158 case 0: return tr("Auto-Align");
Chris@1511 159 case 1: return tr("Linear");
Chris@1511 160 case 2: return tr("Log");
Chris@1511 161 }
Chris@1511 162 }
Chris@1511 163 return SingleColourLayer::getPropertyValueLabel(name, value);
Chris@1511 164 }
Chris@1511 165
Chris@1511 166 void
Chris@1518 167 BoxLayer::setProperty(const PropertyName &name, int value)
Chris@1511 168 {
Chris@1511 169 if (name == "Vertical Scale") {
Chris@1511 170 setVerticalScale(VerticalScale(value));
Chris@1518 171 } else if (name == "Scale Units") {
Chris@1518 172 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1518 173 if (model) {
Chris@1518 174 model->setScaleUnits
Chris@1518 175 (UnitDatabase::getInstance()->getUnitById(value));
Chris@1518 176 emit modelChanged(m_model);
Chris@1518 177 }
Chris@1511 178 } else {
Chris@1511 179 return SingleColourLayer::setProperty(name, value);
Chris@1511 180 }
Chris@1511 181 }
Chris@1511 182
Chris@1511 183 void
Chris@1518 184 BoxLayer::setVerticalScale(VerticalScale scale)
Chris@1511 185 {
Chris@1511 186 if (m_verticalScale == scale) return;
Chris@1511 187 m_verticalScale = scale;
Chris@1511 188 emit layerParametersChanged();
Chris@1511 189 }
Chris@1511 190
Chris@1511 191 bool
Chris@1518 192 BoxLayer::isLayerScrollable(const LayerGeometryProvider *v) const
Chris@1511 193 {
Chris@1511 194 QPoint discard;
Chris@1511 195 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@1511 196 }
Chris@1511 197
Chris@1511 198 bool
Chris@1518 199 BoxLayer::getValueExtents(double &min, double &max,
Chris@1518 200 bool &logarithmic, QString &unit) const
Chris@1511 201 {
Chris@1518 202 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 203 if (!model) return false;
Chris@1518 204 min = model->getValueMinimum();
Chris@1518 205 max = model->getValueMaximum();
Chris@1511 206 unit = getScaleUnits();
Chris@1511 207
Chris@1511 208 if (m_verticalScale == LogScale) logarithmic = true;
Chris@1511 209
Chris@1511 210 return true;
Chris@1511 211 }
Chris@1511 212
Chris@1511 213 bool
Chris@1518 214 BoxLayer::getDisplayExtents(double &min, double &max) const
Chris@1511 215 {
Chris@1518 216 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 217 if (!model || m_verticalScale == AutoAlignScale) return false;
Chris@1511 218
Chris@1518 219 min = model->getValueMinimum();
Chris@1518 220 max = model->getValueMaximum();
Chris@1511 221
Chris@1511 222 return true;
Chris@1511 223 }
Chris@1511 224
Chris@1518 225 bool
Chris@1535 226 BoxLayer::adoptExtents(double min, double max, QString unit)
Chris@1518 227 {
Chris@1518 228 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1518 229 if (!model) return false;
Chris@1535 230
Chris@1535 231 SVDEBUG << "BoxLayer[" << this << "]::adoptExtents: min " << min
Chris@1535 232 << ", max " << max << ", unit " << unit << endl;
Chris@1535 233
Chris@1518 234 if (model->getScaleUnits() == "") {
Chris@1518 235 model->setScaleUnits(unit);
Chris@1518 236 return true;
Chris@1518 237 } else {
Chris@1518 238 return false;
Chris@1518 239 }
Chris@1518 240 }
Chris@1518 241
Chris@1511 242 EventVector
Chris@1518 243 BoxLayer::getLocalPoints(LayerGeometryProvider *v, int x) const
Chris@1511 244 {
Chris@1518 245 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 246 if (!model) return EventVector();
Chris@1511 247
Chris@1511 248 sv_frame_t frame = v->getFrameForX(x);
Chris@1511 249
Chris@1511 250 EventVector local = model->getEventsCovering(frame);
Chris@1511 251 if (!local.empty()) return local;
Chris@1511 252
Chris@1511 253 int fuzz = ViewManager::scalePixelSize(2);
Chris@1511 254 sv_frame_t start = v->getFrameForX(x - fuzz);
Chris@1511 255 sv_frame_t end = v->getFrameForX(x + fuzz);
Chris@1511 256
Chris@1511 257 local = model->getEventsStartingWithin(frame, end - frame);
Chris@1511 258 if (!local.empty()) return local;
Chris@1511 259
Chris@1511 260 local = model->getEventsSpanning(start, frame - start);
Chris@1511 261 if (!local.empty()) return local;
Chris@1511 262
Chris@1511 263 return {};
Chris@1511 264 }
Chris@1511 265
Chris@1511 266 bool
Chris@1518 267 BoxLayer::getPointToDrag(LayerGeometryProvider *v, int x, int y,
Chris@1511 268 Event &point) const
Chris@1511 269 {
Chris@1518 270 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 271 if (!model) return false;
Chris@1511 272
Chris@1511 273 sv_frame_t frame = v->getFrameForX(x);
Chris@1511 274
Chris@1511 275 EventVector onPoints = model->getEventsCovering(frame);
Chris@1511 276 if (onPoints.empty()) return false;
Chris@1511 277
Chris@1511 278 int nearestDistance = -1;
Chris@1511 279 for (const auto &p: onPoints) {
Chris@1511 280 int distance = getYForValue(v, p.getValue()) - y;
Chris@1511 281 if (distance < 0) distance = -distance;
Chris@1511 282 if (nearestDistance == -1 || distance < nearestDistance) {
Chris@1511 283 nearestDistance = distance;
Chris@1511 284 point = p;
Chris@1511 285 }
Chris@1511 286 }
Chris@1511 287
Chris@1511 288 return true;
Chris@1511 289 }
Chris@1511 290
Chris@1511 291 QString
Chris@1518 292 BoxLayer::getLabelPreceding(sv_frame_t frame) const
Chris@1511 293 {
Chris@1518 294 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 295 if (!model) return "";
Chris@1511 296 EventVector points = model->getEventsStartingWithin
Chris@1511 297 (model->getStartFrame(), frame - model->getStartFrame());
Chris@1511 298 if (!points.empty()) {
Chris@1511 299 for (auto i = points.rbegin(); i != points.rend(); ++i) {
Chris@1511 300 if (i->getLabel() != QString()) {
Chris@1511 301 return i->getLabel();
Chris@1511 302 }
Chris@1511 303 }
Chris@1511 304 }
Chris@1511 305 return QString();
Chris@1511 306 }
Chris@1511 307
Chris@1511 308 QString
Chris@1518 309 BoxLayer::getFeatureDescription(LayerGeometryProvider *v,
Chris@1511 310 QPoint &pos) const
Chris@1511 311 {
Chris@1511 312 int x = pos.x();
Chris@1511 313
Chris@1518 314 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 315 if (!model || !model->getSampleRate()) return "";
Chris@1511 316
Chris@1511 317 EventVector points = getLocalPoints(v, x);
Chris@1511 318
Chris@1511 319 if (points.empty()) {
Chris@1511 320 if (!model->isReady()) {
Chris@1511 321 return tr("In progress");
Chris@1511 322 } else {
Chris@1511 323 return tr("No local points");
Chris@1511 324 }
Chris@1511 325 }
Chris@1511 326
Chris@1511 327 Event box;
Chris@1511 328 EventVector::iterator i;
Chris@1511 329
Chris@1511 330 for (i = points.begin(); i != points.end(); ++i) {
Chris@1511 331
Chris@1511 332 int y0 = getYForValue(v, i->getValue());
Chris@1511 333 int y1 = getYForValue(v, i->getValue() + fabsf(i->getLevel()));
Chris@1511 334
Chris@1511 335 if (pos.y() >= y0 && pos.y() <= y1) {
Chris@1511 336 box = *i;
Chris@1511 337 break;
Chris@1511 338 }
Chris@1511 339 }
Chris@1511 340
Chris@1511 341 if (i == points.end()) return tr("No local points");
Chris@1511 342
Chris@1511 343 RealTime rt = RealTime::frame2RealTime(box.getFrame(),
Chris@1511 344 model->getSampleRate());
Chris@1511 345 RealTime rd = RealTime::frame2RealTime(box.getDuration(),
Chris@1511 346 model->getSampleRate());
Chris@1511 347
Chris@1511 348 QString rangeText;
Chris@1511 349
Chris@1511 350 rangeText = tr("%1 %2 - %3 %4")
Chris@1511 351 .arg(box.getValue()).arg(getScaleUnits())
Chris@1511 352 .arg(box.getValue() + fabsf(box.getLevel())).arg(getScaleUnits());
Chris@1511 353
Chris@1511 354 QString text;
Chris@1511 355
Chris@1511 356 if (box.getLabel() == "") {
Chris@1518 357 text = QString(tr("Time:\t%1\nDuration:\t%2\nValue:\t%3\nNo label"))
Chris@1511 358 .arg(rt.toText(true).c_str())
Chris@1511 359 .arg(rd.toText(true).c_str())
Chris@1511 360 .arg(rangeText);
Chris@1511 361 } else {
Chris@1518 362 text = QString(tr("Time:\t%1\nDuration:\t%2\nValue:\t%3\nLabel:\t%4"))
Chris@1511 363 .arg(rt.toText(true).c_str())
Chris@1511 364 .arg(rd.toText(true).c_str())
Chris@1511 365 .arg(rangeText)
Chris@1511 366 .arg(box.getLabel());
Chris@1511 367 }
Chris@1511 368
Chris@1511 369 pos = QPoint(v->getXForFrame(box.getFrame()),
Chris@1511 370 getYForValue(v, box.getValue()));
Chris@1511 371 return text;
Chris@1511 372 }
Chris@1511 373
Chris@1511 374 bool
Chris@1518 375 BoxLayer::snapToFeatureFrame(LayerGeometryProvider *v,
Chris@1511 376 sv_frame_t &frame,
Chris@1511 377 int &resolution,
Chris@1511 378 SnapType snap) const
Chris@1511 379 {
Chris@1518 380 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 381 if (!model) {
Chris@1511 382 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@1511 383 }
Chris@1511 384
Chris@1511 385 // SnapLeft / SnapRight: return frame of nearest feature in that
Chris@1511 386 // direction no matter how far away
Chris@1511 387 //
Chris@1511 388 // SnapNeighbouring: return frame of feature that would be used in
Chris@1511 389 // an editing operation, i.e. closest feature in either direction
Chris@1511 390 // but only if it is "close enough"
Chris@1511 391
Chris@1511 392 resolution = model->getResolution();
Chris@1511 393
Chris@1511 394 if (snap == SnapNeighbouring) {
Chris@1511 395 EventVector points = getLocalPoints(v, v->getXForFrame(frame));
Chris@1511 396 if (points.empty()) return false;
Chris@1511 397 frame = points.begin()->getFrame();
Chris@1511 398 return true;
Chris@1511 399 }
Chris@1511 400
Chris@1511 401 // Normally we snap to the start frame of whichever event we
Chris@1511 402 // find. However here, for SnapRight only, if the end frame of
Chris@1511 403 // whichever event we would have snapped to had we been snapping
Chris@1511 404 // left is closer than the start frame of the next event to the
Chris@1511 405 // right, then we snap to that frame instead. Clear?
Chris@1511 406
Chris@1511 407 Event left;
Chris@1511 408 bool haveLeft = false;
Chris@1511 409 if (model->getNearestEventMatching
Chris@1511 410 (frame, [](Event) { return true; }, EventSeries::Backward, left)) {
Chris@1511 411 haveLeft = true;
Chris@1511 412 }
Chris@1511 413
Chris@1511 414 if (snap == SnapLeft) {
Chris@1511 415 frame = left.getFrame();
Chris@1511 416 return haveLeft;
Chris@1511 417 }
Chris@1511 418
Chris@1511 419 Event right;
Chris@1511 420 bool haveRight = false;
Chris@1511 421 if (model->getNearestEventMatching
Chris@1511 422 (frame, [](Event) { return true; }, EventSeries::Forward, right)) {
Chris@1511 423 haveRight = true;
Chris@1511 424 }
Chris@1511 425
Chris@1511 426 if (haveLeft) {
Chris@1511 427 sv_frame_t leftEnd = left.getFrame() + left.getDuration();
Chris@1511 428 if (leftEnd > frame) {
Chris@1511 429 if (haveRight) {
Chris@1511 430 if (leftEnd - frame < right.getFrame() - frame) {
Chris@1511 431 frame = leftEnd;
Chris@1511 432 } else {
Chris@1511 433 frame = right.getFrame();
Chris@1511 434 }
Chris@1511 435 } else {
Chris@1511 436 frame = leftEnd;
Chris@1511 437 }
Chris@1511 438 return true;
Chris@1511 439 }
Chris@1511 440 }
Chris@1511 441
Chris@1511 442 if (haveRight) {
Chris@1511 443 frame = right.getFrame();
Chris@1511 444 return true;
Chris@1511 445 }
Chris@1511 446
Chris@1511 447 return false;
Chris@1511 448 }
Chris@1511 449
Chris@1511 450 QString
Chris@1518 451 BoxLayer::getScaleUnits() const
Chris@1511 452 {
Chris@1518 453 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 454 if (model) return model->getScaleUnits();
Chris@1511 455 else return "";
Chris@1511 456 }
Chris@1511 457
Chris@1511 458 void
Chris@1518 459 BoxLayer::getScaleExtents(LayerGeometryProvider *v,
Chris@1511 460 double &min, double &max,
Chris@1511 461 bool &log) const
Chris@1511 462 {
Chris@1511 463 min = 0.0;
Chris@1511 464 max = 0.0;
Chris@1511 465 log = false;
Chris@1511 466
Chris@1518 467 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 468 if (!model) return;
Chris@1511 469
Chris@1511 470 QString queryUnits;
Chris@1511 471 queryUnits = getScaleUnits();
Chris@1511 472
Chris@1511 473 if (m_verticalScale == AutoAlignScale) {
Chris@1511 474
Chris@1511 475 if (!v->getValueExtents(queryUnits, min, max, log)) {
Chris@1511 476
Chris@1518 477 min = model->getValueMinimum();
Chris@1518 478 max = model->getValueMaximum();
Chris@1511 479
Chris@1518 480 // cerr << "BoxLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl;
Chris@1511 481
Chris@1511 482 } else if (log) {
Chris@1511 483
Chris@1511 484 LogRange::mapRange(min, max);
Chris@1511 485
Chris@1518 486 // cerr << "BoxLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl;
Chris@1511 487
Chris@1511 488 }
Chris@1511 489
Chris@1511 490 } else {
Chris@1511 491
Chris@1518 492 min = model->getValueMinimum();
Chris@1518 493 max = model->getValueMaximum();
Chris@1511 494
Chris@1511 495 if (m_verticalScale == LogScale) {
Chris@1511 496 LogRange::mapRange(min, max);
Chris@1511 497 log = true;
Chris@1511 498 }
Chris@1511 499 }
Chris@1511 500
Chris@1511 501 if (max == min) max = min + 1.0;
Chris@1511 502 }
Chris@1511 503
Chris@1511 504 int
Chris@1518 505 BoxLayer::getYForValue(LayerGeometryProvider *v, double val) const
Chris@1511 506 {
Chris@1511 507 double min = 0.0, max = 0.0;
Chris@1511 508 bool logarithmic = false;
Chris@1511 509 int h = v->getPaintHeight();
Chris@1511 510
Chris@1511 511 getScaleExtents(v, min, max, logarithmic);
Chris@1511 512
Chris@1518 513 // cerr << "BoxLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << endl;
Chris@1511 514 // cerr << "h = " << h << ", margin = " << margin << endl;
Chris@1511 515
Chris@1511 516 if (logarithmic) {
Chris@1511 517 val = LogRange::map(val);
Chris@1511 518 }
Chris@1511 519
Chris@1511 520 return int(h - ((val - min) * h) / (max - min));
Chris@1511 521 }
Chris@1511 522
Chris@1511 523 double
Chris@1518 524 BoxLayer::getValueForY(LayerGeometryProvider *v, int y) const
Chris@1511 525 {
Chris@1511 526 double min = 0.0, max = 0.0;
Chris@1511 527 bool logarithmic = false;
Chris@1511 528 int h = v->getPaintHeight();
Chris@1511 529
Chris@1511 530 getScaleExtents(v, min, max, logarithmic);
Chris@1511 531
Chris@1511 532 double val = min + (double(h - y) * double(max - min)) / h;
Chris@1511 533
Chris@1511 534 if (logarithmic) {
Chris@1511 535 val = pow(10.0, val);
Chris@1511 536 }
Chris@1511 537
Chris@1511 538 return val;
Chris@1511 539 }
Chris@1511 540
Chris@1511 541 void
Chris@1518 542 BoxLayer::paint(LayerGeometryProvider *v, QPainter &paint,
Chris@1511 543 QRect rect) const
Chris@1511 544 {
Chris@1518 545 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 546 if (!model || !model->isOK()) return;
Chris@1511 547
Chris@1511 548 sv_samplerate_t sampleRate = model->getSampleRate();
Chris@1511 549 if (!sampleRate) return;
Chris@1511 550
Chris@1518 551 // Profiler profiler("BoxLayer::paint", true);
Chris@1511 552
Chris@1511 553 int x0 = rect.left() - 40, x1 = rect.right();
Chris@1511 554
Chris@1511 555 sv_frame_t wholeFrame0 = v->getFrameForX(0);
Chris@1511 556 sv_frame_t wholeFrame1 = v->getFrameForX(v->getPaintWidth());
Chris@1511 557
Chris@1511 558 EventVector points(model->getEventsSpanning(wholeFrame0,
Chris@1511 559 wholeFrame1 - wholeFrame0));
Chris@1511 560 if (points.empty()) return;
Chris@1511 561
Chris@1511 562 paint.setPen(getBaseQColor());
Chris@1511 563
Chris@1518 564 // SVDEBUG << "BoxLayer::paint: resolution is "
Chris@1511 565 // << model->getResolution() << " frames" << endl;
Chris@1511 566
Chris@1518 567 double min = model->getValueMinimum();
Chris@1518 568 double max = model->getValueMaximum();
Chris@1511 569 if (max == min) max = min + 1.0;
Chris@1511 570
Chris@1511 571 QPoint localPos;
Chris@1511 572 Event illuminatePoint(0);
Chris@1511 573 bool shouldIlluminate = false;
Chris@1511 574
Chris@1511 575 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@1511 576 shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(),
Chris@1511 577 illuminatePoint);
Chris@1511 578 }
Chris@1511 579
Chris@1511 580 paint.save();
Chris@1511 581 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@1511 582
Chris@1514 583 QFontMetrics fm = paint.fontMetrics();
Chris@1514 584
Chris@1511 585 for (EventVector::const_iterator i = points.begin();
Chris@1511 586 i != points.end(); ++i) {
Chris@1511 587
Chris@1511 588 const Event &p(*i);
Chris@1511 589
Chris@1511 590 int x = v->getXForFrame(p.getFrame());
Chris@1511 591 int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x;
Chris@1511 592 int y = getYForValue(v, p.getValue());
Chris@1514 593 int h = getYForValue(v, p.getValue() + fabsf(p.getLevel())) - y;
Chris@1511 594 int ex = x + w;
Chris@1514 595 int gap = v->scalePixelSize(2);
Chris@1511 596
Chris@1511 597 EventVector::const_iterator j = i;
Chris@1511 598 ++j;
Chris@1511 599
Chris@1511 600 if (j != points.end()) {
Chris@1511 601 const Event &q(*j);
Chris@1511 602 int nx = v->getXForFrame(q.getFrame());
Chris@1511 603 if (nx < ex) ex = nx;
Chris@1511 604 }
Chris@1511 605
Chris@1511 606 if (w < 1) w = 1;
Chris@1511 607
Chris@1511 608 paint.setPen(getBaseQColor());
Chris@1514 609 paint.setBrush(Qt::NoBrush);
Chris@1511 610
Chris@1514 611 if ((shouldIlluminate && illuminatePoint == p) ||
Chris@1514 612 (m_editing && m_editingPoint == p)) {
Chris@1511 613
Chris@1514 614 paint.setPen(QPen(getBaseQColor(), v->scalePixelSize(2)));
Chris@1511 615
Chris@1511 616 // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
Chris@1511 617 // replacement (horizontalAdvance) was only added in Qt 5.11
Chris@1511 618 // which is too new for us
Chris@1511 619 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Chris@1511 620
Chris@1514 621 if (abs(h) > 2 * fm.height()) {
Chris@1514 622
Chris@1514 623 QString y0label = QString("%1 %2")
Chris@1514 624 .arg(p.getValue())
Chris@1514 625 .arg(getScaleUnits());
Chris@1514 626
Chris@1514 627 QString y1label = QString("%1 %2")
Chris@1514 628 .arg(p.getValue() + fabsf(p.getLevel()))
Chris@1514 629 .arg(getScaleUnits());
Chris@1514 630
Chris@1514 631 PaintAssistant::drawVisibleText
Chris@1514 632 (v, paint,
Chris@1514 633 x - fm.width(y0label) - gap,
Chris@1514 634 y - fm.descent(),
Chris@1514 635 y0label, PaintAssistant::OutlinedText);
Chris@1514 636
Chris@1514 637 PaintAssistant::drawVisibleText
Chris@1514 638 (v, paint,
Chris@1514 639 x - fm.width(y1label) - gap,
Chris@1514 640 y + h + fm.ascent(),
Chris@1514 641 y1label, PaintAssistant::OutlinedText);
Chris@1514 642
Chris@1514 643 } else {
Chris@1514 644
Chris@1514 645 QString ylabel = QString("%1 %2 - %3 %4")
Chris@1514 646 .arg(p.getValue())
Chris@1514 647 .arg(getScaleUnits())
Chris@1514 648 .arg(p.getValue() + fabsf(p.getLevel()))
Chris@1514 649 .arg(getScaleUnits());
Chris@1514 650
Chris@1514 651 PaintAssistant::drawVisibleText
Chris@1514 652 (v, paint,
Chris@1514 653 x - fm.width(ylabel) - gap,
Chris@1514 654 y - fm.descent(),
Chris@1514 655 ylabel, PaintAssistant::OutlinedText);
Chris@1514 656 }
Chris@1514 657
Chris@1514 658 QString t0label = RealTime::frame2RealTime
Chris@1514 659 (p.getFrame(), model->getSampleRate()).toText(true).c_str();
Chris@1514 660
Chris@1514 661 QString t1label = RealTime::frame2RealTime
Chris@1514 662 (p.getFrame() + p.getDuration(), model->getSampleRate())
Chris@1514 663 .toText(true).c_str();
Chris@1514 664
Chris@1511 665 PaintAssistant::drawVisibleText
Chris@1514 666 (v, paint, x, y + fm.ascent() + gap,
Chris@1514 667 t0label, PaintAssistant::OutlinedText);
Chris@1514 668
Chris@1514 669 if (w > fm.width(t0label) + fm.width(t1label) + gap * 3) {
Chris@1514 670
Chris@1514 671 PaintAssistant::drawVisibleText
Chris@1514 672 (v, paint,
Chris@1514 673 x + w - fm.width(t1label),
Chris@1514 674 y + fm.ascent() + gap,
Chris@1514 675 t1label, PaintAssistant::OutlinedText);
Chris@1514 676
Chris@1514 677 } else {
Chris@1514 678
Chris@1514 679 PaintAssistant::drawVisibleText
Chris@1514 680 (v, paint,
Chris@1514 681 x + w - fm.width(t1label),
Chris@1514 682 y + fm.ascent() + fm.height() + gap,
Chris@1514 683 t1label, PaintAssistant::OutlinedText);
Chris@1514 684 }
Chris@1511 685 }
Chris@1511 686
Chris@1511 687 paint.drawRect(x, y, w, h);
Chris@1511 688 }
Chris@1511 689
Chris@1511 690 for (EventVector::const_iterator i = points.begin();
Chris@1511 691 i != points.end(); ++i) {
Chris@1511 692
Chris@1511 693 const Event &p(*i);
Chris@1511 694
Chris@1514 695 QString label = p.getLabel();
Chris@1514 696 if (label == "") continue;
Chris@1514 697
Chris@1514 698 if (shouldIlluminate && illuminatePoint == p) {
Chris@1514 699 continue; // already handled this in illumination special case
Chris@1514 700 }
Chris@1514 701
Chris@1511 702 int x = v->getXForFrame(p.getFrame());
Chris@1511 703 int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x;
Chris@1511 704 int y = getYForValue(v, p.getValue());
Chris@1511 705
Chris@1514 706 int labelWidth = fm.width(label);
Chris@1511 707
Chris@1511 708 int gap = v->scalePixelSize(2);
Chris@1511 709
Chris@1511 710 if (x + w < x0 || x - labelWidth - gap > x1) {
Chris@1511 711 continue;
Chris@1511 712 }
Chris@1511 713
Chris@1514 714 int labelX, labelY;
Chris@1511 715
Chris@1514 716 labelX = x - labelWidth - gap;
Chris@1514 717 labelY = y - fm.descent();
Chris@1511 718
Chris@1514 719 PaintAssistant::drawVisibleText(v, paint, labelX, labelY, label,
Chris@1514 720 PaintAssistant::OutlinedText);
Chris@1511 721 }
Chris@1511 722
Chris@1511 723 paint.restore();
Chris@1511 724 }
Chris@1511 725
Chris@1511 726 int
Chris@1518 727 BoxLayer::getVerticalScaleWidth(LayerGeometryProvider *v,
Chris@1511 728 bool, QPainter &paint) const
Chris@1511 729 {
Chris@1518 730 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 731 if (!model || m_verticalScale == AutoAlignScale) {
Chris@1511 732 return 0;
Chris@1511 733 } else {
Chris@1511 734 if (m_verticalScale == LogScale) {
Chris@1511 735 return LogNumericalScale().getWidth(v, paint);
Chris@1511 736 } else {
Chris@1511 737 return LinearNumericalScale().getWidth(v, paint);
Chris@1511 738 }
Chris@1511 739 }
Chris@1511 740 }
Chris@1511 741
Chris@1511 742 void
Chris@1518 743 BoxLayer::paintVerticalScale(LayerGeometryProvider *v,
Chris@1511 744 bool, QPainter &paint, QRect) const
Chris@1511 745 {
Chris@1518 746 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 747 if (!model || model->isEmpty()) return;
Chris@1511 748
Chris@1511 749 QString unit;
Chris@1511 750 double min, max;
Chris@1511 751 bool logarithmic;
Chris@1511 752
Chris@1511 753 int w = getVerticalScaleWidth(v, false, paint);
Chris@1511 754
Chris@1511 755 getScaleExtents(v, min, max, logarithmic);
Chris@1511 756
Chris@1511 757 if (logarithmic) {
Chris@1511 758 LogNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@1511 759 } else {
Chris@1511 760 LinearNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@1511 761 }
Chris@1511 762
Chris@1511 763 if (getScaleUnits() != "") {
Chris@1511 764 int mw = w - 5;
Chris@1511 765 paint.drawText(5,
Chris@1511 766 5 + paint.fontMetrics().ascent(),
Chris@1511 767 TextAbbrev::abbreviate(getScaleUnits(),
Chris@1511 768 paint.fontMetrics(),
Chris@1511 769 mw));
Chris@1511 770 }
Chris@1511 771 }
Chris@1511 772
Chris@1511 773 void
Chris@1518 774 BoxLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 775 {
Chris@1518 776 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 777 if (!model) return;
Chris@1511 778
Chris@1511 779 sv_frame_t frame = v->getFrameForX(e->x());
Chris@1511 780 if (frame < 0) frame = 0;
Chris@1511 781 frame = frame / model->getResolution() * model->getResolution();
Chris@1511 782
Chris@1518 783 double value = getValueForY(v, e->y());
Chris@1511 784
Chris@1518 785 m_editingPoint = Event(frame, float(value), 0, "");
Chris@1511 786 m_originalPoint = m_editingPoint;
Chris@1511 787
Chris@1511 788 if (m_editingCommand) finish(m_editingCommand);
Chris@1511 789 m_editingCommand = new ChangeEventsCommand(m_model.untyped,
Chris@1518 790 tr("Draw Box"));
Chris@1511 791 m_editingCommand->add(m_editingPoint);
Chris@1511 792
Chris@1511 793 m_editing = true;
Chris@1511 794 }
Chris@1511 795
Chris@1511 796 void
Chris@1518 797 BoxLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 798 {
Chris@1518 799 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 800 if (!model || !m_editing) return;
Chris@1511 801
Chris@1514 802 sv_frame_t dragFrame = v->getFrameForX(e->x());
Chris@1514 803 if (dragFrame < 0) dragFrame = 0;
Chris@1514 804 dragFrame = dragFrame / model->getResolution() * model->getResolution();
Chris@1511 805
Chris@1514 806 sv_frame_t eventFrame = m_originalPoint.getFrame();
Chris@1514 807 sv_frame_t eventDuration = dragFrame - eventFrame;
Chris@1514 808 if (eventDuration < 0) {
Chris@1514 809 eventFrame = eventFrame + eventDuration;
Chris@1514 810 eventDuration = -eventDuration;
Chris@1514 811 } else if (eventDuration == 0) {
Chris@1514 812 eventDuration = model->getResolution();
Chris@1514 813 }
Chris@1511 814
Chris@1518 815 double dragValue = getValueForY(v, e->y());
Chris@1514 816
Chris@1518 817 double eventValue = m_originalPoint.getValue();
Chris@1518 818 double eventFreqDiff = dragValue - eventValue;
Chris@1514 819 if (eventFreqDiff < 0) {
Chris@1518 820 eventValue = eventValue + eventFreqDiff;
Chris@1514 821 eventFreqDiff = -eventFreqDiff;
Chris@1511 822 }
Chris@1511 823
Chris@1511 824 m_editingCommand->remove(m_editingPoint);
Chris@1511 825 m_editingPoint = m_editingPoint
Chris@1514 826 .withFrame(eventFrame)
Chris@1514 827 .withDuration(eventDuration)
Chris@1518 828 .withValue(float(eventValue))
Chris@1514 829 .withLevel(float(eventFreqDiff));
Chris@1511 830 m_editingCommand->add(m_editingPoint);
Chris@1511 831 }
Chris@1511 832
Chris@1511 833 void
Chris@1518 834 BoxLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *)
Chris@1511 835 {
Chris@1518 836 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 837 if (!model || !m_editing) return;
Chris@1511 838 finish(m_editingCommand);
Chris@1511 839 m_editingCommand = nullptr;
Chris@1511 840 m_editing = false;
Chris@1511 841 }
Chris@1511 842
Chris@1511 843 void
Chris@1518 844 BoxLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 845 {
Chris@1518 846 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 847 if (!model) return;
Chris@1511 848
Chris@1511 849 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@1511 850
Chris@1511 851 if (m_editingCommand) {
Chris@1511 852 finish(m_editingCommand);
Chris@1511 853 m_editingCommand = nullptr;
Chris@1511 854 }
Chris@1511 855
Chris@1511 856 m_editing = true;
Chris@1511 857 }
Chris@1511 858
Chris@1511 859 void
Chris@1518 860 BoxLayer::eraseDrag(LayerGeometryProvider *, QMouseEvent *)
Chris@1511 861 {
Chris@1511 862 }
Chris@1511 863
Chris@1511 864 void
Chris@1518 865 BoxLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 866 {
Chris@1518 867 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 868 if (!model || !m_editing) return;
Chris@1511 869
Chris@1511 870 m_editing = false;
Chris@1511 871
Chris@1511 872 Event p(0);
Chris@1511 873 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
Chris@1511 874 if (p.getFrame() != m_editingPoint.getFrame() ||
Chris@1511 875 p.getValue() != m_editingPoint.getValue()) return;
Chris@1511 876
Chris@1511 877 m_editingCommand = new ChangeEventsCommand
Chris@1518 878 (m_model.untyped, tr("Erase Box"));
Chris@1511 879
Chris@1511 880 m_editingCommand->remove(m_editingPoint);
Chris@1511 881
Chris@1511 882 finish(m_editingCommand);
Chris@1511 883 m_editingCommand = nullptr;
Chris@1511 884 m_editing = false;
Chris@1511 885 }
Chris@1511 886
Chris@1511 887 void
Chris@1518 888 BoxLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 889 {
Chris@1518 890 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 891 if (!model) return;
Chris@1511 892
Chris@1511 893 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) {
Chris@1511 894 return;
Chris@1511 895 }
Chris@1511 896
Chris@1511 897 m_dragPointX = v->getXForFrame(m_editingPoint.getFrame());
Chris@1511 898 m_dragPointY = getYForValue(v, m_editingPoint.getValue());
Chris@1511 899
Chris@1511 900 m_originalPoint = m_editingPoint;
Chris@1511 901
Chris@1511 902 if (m_editingCommand) {
Chris@1511 903 finish(m_editingCommand);
Chris@1511 904 m_editingCommand = nullptr;
Chris@1511 905 }
Chris@1511 906
Chris@1511 907 m_editing = true;
Chris@1511 908 m_dragStartX = e->x();
Chris@1511 909 m_dragStartY = e->y();
Chris@1511 910 }
Chris@1511 911
Chris@1511 912 void
Chris@1518 913 BoxLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 914 {
Chris@1518 915 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 916 if (!model || !m_editing) return;
Chris@1511 917
Chris@1511 918 int xdist = e->x() - m_dragStartX;
Chris@1511 919 int ydist = e->y() - m_dragStartY;
Chris@1511 920 int newx = m_dragPointX + xdist;
Chris@1511 921 int newy = m_dragPointY + ydist;
Chris@1511 922
Chris@1511 923 sv_frame_t frame = v->getFrameForX(newx);
Chris@1511 924 if (frame < 0) frame = 0;
Chris@1511 925 frame = frame / model->getResolution() * model->getResolution();
Chris@1511 926
Chris@1511 927 double value = getValueForY(v, newy);
Chris@1511 928
Chris@1511 929 if (!m_editingCommand) {
Chris@1511 930 m_editingCommand = new ChangeEventsCommand
Chris@1511 931 (m_model.untyped,
Chris@1518 932 tr("Drag Box"));
Chris@1511 933 }
Chris@1511 934
Chris@1511 935 m_editingCommand->remove(m_editingPoint);
Chris@1511 936 m_editingPoint = m_editingPoint
Chris@1511 937 .withFrame(frame)
Chris@1511 938 .withValue(float(value));
Chris@1511 939 m_editingCommand->add(m_editingPoint);
Chris@1511 940 }
Chris@1511 941
Chris@1511 942 void
Chris@1518 943 BoxLayer::editEnd(LayerGeometryProvider *, QMouseEvent *)
Chris@1511 944 {
Chris@1518 945 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 946 if (!model || !m_editing) return;
Chris@1511 947
Chris@1511 948 if (m_editingCommand) {
Chris@1511 949
Chris@1511 950 QString newName = m_editingCommand->getName();
Chris@1511 951
Chris@1511 952 if (m_editingPoint.getFrame() != m_originalPoint.getFrame()) {
Chris@1511 953 if (m_editingPoint.getValue() != m_originalPoint.getValue()) {
Chris@1518 954 newName = tr("Edit Box");
Chris@1511 955 } else {
Chris@1518 956 newName = tr("Relocate Box");
Chris@1511 957 }
Chris@1511 958 } else {
Chris@1511 959 newName = tr("Change Point Value");
Chris@1511 960 }
Chris@1511 961
Chris@1511 962 m_editingCommand->setName(newName);
Chris@1511 963 finish(m_editingCommand);
Chris@1511 964 }
Chris@1511 965
Chris@1511 966 m_editingCommand = nullptr;
Chris@1511 967 m_editing = false;
Chris@1511 968 }
Chris@1511 969
Chris@1511 970 bool
Chris@1518 971 BoxLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 972 {
Chris@1518 973 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 974 if (!model) return false;
Chris@1511 975
Chris@1511 976 Event region(0);
Chris@1511 977 if (!getPointToDrag(v, e->x(), e->y(), region)) return false;
Chris@1511 978
Chris@1515 979 ItemEditDialog::LabelOptions labelOptions;
Chris@1518 980 labelOptions.valueLabel = tr("Minimum Value");
Chris@1518 981 labelOptions.levelLabel = tr("Value Extent");
Chris@1515 982 labelOptions.valueUnits = getScaleUnits();
Chris@1515 983 labelOptions.levelUnits = getScaleUnits();
Chris@1515 984
Chris@1511 985 ItemEditDialog *dialog = new ItemEditDialog
Chris@1511 986 (model->getSampleRate(),
Chris@1511 987 ItemEditDialog::ShowTime |
Chris@1511 988 ItemEditDialog::ShowDuration |
Chris@1511 989 ItemEditDialog::ShowValue |
Chris@1515 990 ItemEditDialog::ShowLevel |
Chris@1511 991 ItemEditDialog::ShowText,
Chris@1515 992 labelOptions);
Chris@1511 993
Chris@1511 994 dialog->setFrameTime(region.getFrame());
Chris@1511 995 dialog->setValue(region.getValue());
Chris@1515 996 dialog->setLevel(region.getLevel());
Chris@1511 997 dialog->setFrameDuration(region.getDuration());
Chris@1511 998 dialog->setText(region.getLabel());
Chris@1511 999
Chris@1511 1000 if (dialog->exec() == QDialog::Accepted) {
Chris@1511 1001
Chris@1518 1002 Event newBox = region
Chris@1511 1003 .withFrame(dialog->getFrameTime())
Chris@1511 1004 .withValue(dialog->getValue())
Chris@1515 1005 .withLevel(dialog->getLevel())
Chris@1511 1006 .withDuration(dialog->getFrameDuration())
Chris@1511 1007 .withLabel(dialog->getText());
Chris@1511 1008
Chris@1511 1009 ChangeEventsCommand *command = new ChangeEventsCommand
Chris@1518 1010 (m_model.untyped, tr("Edit Box"));
Chris@1511 1011 command->remove(region);
Chris@1518 1012 command->add(newBox);
Chris@1511 1013 finish(command);
Chris@1511 1014 }
Chris@1511 1015
Chris@1511 1016 delete dialog;
Chris@1511 1017 return true;
Chris@1511 1018 }
Chris@1511 1019
Chris@1511 1020 void
Chris@1518 1021 BoxLayer::moveSelection(Selection s, sv_frame_t newStartFrame)
Chris@1511 1022 {
Chris@1518 1023 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 1024 if (!model) return;
Chris@1511 1025
Chris@1511 1026 ChangeEventsCommand *command =
Chris@1511 1027 new ChangeEventsCommand(m_model.untyped, tr("Drag Selection"));
Chris@1511 1028
Chris@1511 1029 EventVector points =
Chris@1511 1030 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@1511 1031
Chris@1511 1032 for (EventVector::iterator i = points.begin();
Chris@1511 1033 i != points.end(); ++i) {
Chris@1511 1034
Chris@1511 1035 Event newPoint = (*i)
Chris@1511 1036 .withFrame(i->getFrame() + newStartFrame - s.getStartFrame());
Chris@1511 1037 command->remove(*i);
Chris@1511 1038 command->add(newPoint);
Chris@1511 1039 }
Chris@1511 1040
Chris@1511 1041 finish(command);
Chris@1511 1042 }
Chris@1511 1043
Chris@1511 1044 void
Chris@1518 1045 BoxLayer::resizeSelection(Selection s, Selection newSize)
Chris@1511 1046 {
Chris@1518 1047 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 1048 if (!model || !s.getDuration()) return;
Chris@1511 1049
Chris@1511 1050 ChangeEventsCommand *command =
Chris@1511 1051 new ChangeEventsCommand(m_model.untyped, tr("Resize Selection"));
Chris@1511 1052
Chris@1511 1053 EventVector points =
Chris@1511 1054 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@1511 1055
Chris@1511 1056 double ratio = double(newSize.getDuration()) / double(s.getDuration());
Chris@1511 1057 double oldStart = double(s.getStartFrame());
Chris@1511 1058 double newStart = double(newSize.getStartFrame());
Chris@1511 1059
Chris@1511 1060 for (Event p: points) {
Chris@1511 1061
Chris@1511 1062 double newFrame = (double(p.getFrame()) - oldStart) * ratio + newStart;
Chris@1511 1063 double newDuration = double(p.getDuration()) * ratio;
Chris@1511 1064
Chris@1511 1065 Event newPoint = p
Chris@1511 1066 .withFrame(lrint(newFrame))
Chris@1511 1067 .withDuration(lrint(newDuration));
Chris@1511 1068 command->remove(p);
Chris@1511 1069 command->add(newPoint);
Chris@1511 1070 }
Chris@1511 1071
Chris@1511 1072 finish(command);
Chris@1511 1073 }
Chris@1511 1074
Chris@1511 1075 void
Chris@1518 1076 BoxLayer::deleteSelection(Selection s)
Chris@1511 1077 {
Chris@1518 1078 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 1079 if (!model) return;
Chris@1511 1080
Chris@1511 1081 ChangeEventsCommand *command =
Chris@1511 1082 new ChangeEventsCommand(m_model.untyped, tr("Delete Selected Points"));
Chris@1511 1083
Chris@1511 1084 EventVector points =
Chris@1511 1085 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@1511 1086
Chris@1511 1087 for (EventVector::iterator i = points.begin();
Chris@1511 1088 i != points.end(); ++i) {
Chris@1511 1089
Chris@1511 1090 if (s.contains(i->getFrame())) {
Chris@1511 1091 command->remove(*i);
Chris@1511 1092 }
Chris@1511 1093 }
Chris@1511 1094
Chris@1511 1095 finish(command);
Chris@1511 1096 }
Chris@1511 1097
Chris@1511 1098 void
Chris@1518 1099 BoxLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to)
Chris@1511 1100 {
Chris@1518 1101 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 1102 if (!model) return;
Chris@1511 1103
Chris@1511 1104 EventVector points =
Chris@1511 1105 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@1511 1106
Chris@1511 1107 for (Event p: points) {
Chris@1511 1108 to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame())));
Chris@1511 1109 }
Chris@1511 1110 }
Chris@1511 1111
Chris@1511 1112 bool
Chris@1533 1113 BoxLayer::paste(LayerGeometryProvider *v, const Clipboard &from,
Chris@1533 1114 sv_frame_t /* frameOffset */, bool /* interactive */)
Chris@1511 1115 {
Chris@1518 1116 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 1117 if (!model) return false;
Chris@1511 1118
Chris@1511 1119 const EventVector &points = from.getPoints();
Chris@1511 1120
Chris@1511 1121 bool realign = false;
Chris@1511 1122
Chris@1511 1123 if (clipboardHasDifferentAlignment(v, from)) {
Chris@1511 1124
Chris@1511 1125 QMessageBox::StandardButton button =
Chris@1511 1126 QMessageBox::question(v->getView(), tr("Re-align pasted items?"),
Chris@1511 1127 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@1511 1128 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@1511 1129 QMessageBox::Yes);
Chris@1511 1130
Chris@1511 1131 if (button == QMessageBox::Cancel) {
Chris@1511 1132 return false;
Chris@1511 1133 }
Chris@1511 1134
Chris@1511 1135 if (button == QMessageBox::Yes) {
Chris@1511 1136 realign = true;
Chris@1511 1137 }
Chris@1511 1138 }
Chris@1511 1139
Chris@1511 1140 ChangeEventsCommand *command =
Chris@1511 1141 new ChangeEventsCommand(m_model.untyped, tr("Paste"));
Chris@1511 1142
Chris@1511 1143 for (EventVector::const_iterator i = points.begin();
Chris@1511 1144 i != points.end(); ++i) {
Chris@1511 1145
Chris@1511 1146 sv_frame_t frame = 0;
Chris@1511 1147
Chris@1511 1148 if (!realign) {
Chris@1511 1149
Chris@1511 1150 frame = i->getFrame();
Chris@1511 1151
Chris@1511 1152 } else {
Chris@1511 1153
Chris@1511 1154 if (i->hasReferenceFrame()) {
Chris@1511 1155 frame = i->getReferenceFrame();
Chris@1511 1156 frame = alignFromReference(v, frame);
Chris@1511 1157 } else {
Chris@1511 1158 frame = i->getFrame();
Chris@1511 1159 }
Chris@1511 1160 }
Chris@1511 1161
Chris@1533 1162 Event p = i->withFrame(frame);
Chris@1533 1163
Chris@1511 1164 Event newPoint = p;
Chris@1511 1165 if (!p.hasValue()) {
Chris@1518 1166 newPoint = newPoint.withValue((model->getValueMinimum() +
Chris@1518 1167 model->getValueMaximum()) / 2);
Chris@1511 1168 }
Chris@1511 1169 if (!p.hasDuration()) {
Chris@1511 1170 sv_frame_t nextFrame = frame;
Chris@1511 1171 EventVector::const_iterator j = i;
Chris@1511 1172 for (; j != points.end(); ++j) {
Chris@1511 1173 if (j != i) break;
Chris@1511 1174 }
Chris@1511 1175 if (j != points.end()) {
Chris@1511 1176 nextFrame = j->getFrame();
Chris@1511 1177 }
Chris@1511 1178 if (nextFrame == frame) {
Chris@1511 1179 newPoint = newPoint.withDuration(model->getResolution());
Chris@1511 1180 } else {
Chris@1511 1181 newPoint = newPoint.withDuration(nextFrame - frame);
Chris@1511 1182 }
Chris@1511 1183 }
Chris@1511 1184
Chris@1511 1185 command->add(newPoint);
Chris@1511 1186 }
Chris@1511 1187
Chris@1511 1188 finish(command);
Chris@1511 1189 return true;
Chris@1511 1190 }
Chris@1511 1191
Chris@1511 1192 void
Chris@1518 1193 BoxLayer::toXml(QTextStream &stream,
Chris@1515 1194 QString indent, QString extraAttributes) const
Chris@1511 1195 {
Chris@1511 1196 QString s;
Chris@1511 1197
Chris@1515 1198 s += QString("verticalScale=\"%1\" ").arg(m_verticalScale);
Chris@1511 1199
Chris@1511 1200 SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s);
Chris@1511 1201 }
Chris@1511 1202
Chris@1511 1203 void
Chris@1518 1204 BoxLayer::setProperties(const QXmlAttributes &attributes)
Chris@1511 1205 {
Chris@1511 1206 SingleColourLayer::setProperties(attributes);
Chris@1511 1207
Chris@1511 1208 bool ok;
Chris@1511 1209 VerticalScale scale = (VerticalScale)
Chris@1511 1210 attributes.value("verticalScale").toInt(&ok);
Chris@1511 1211 if (ok) setVerticalScale(scale);
Chris@1511 1212 }
Chris@1511 1213
Chris@1511 1214