annotate layer/BoxLayer.cpp @ 1546:ec837d223bd9

Update getPointToDrag to prefer boxes that are containing the mouse position in height as well as width
author Chris Cannam
date Thu, 17 Oct 2019 10:26:21 +0100
parents 4f8c72adbf43
children e6362cf5ff1d
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@1546 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@1546 278 Event bestContaining;
Chris@1511 279 for (const auto &p: onPoints) {
Chris@1546 280 auto r = getRange(p);
Chris@1546 281 if (y > getYForValue(v, r.first) || y < getYForValue(v, r.second)) {
Chris@1546 282 SVCERR << "inPoints: rejecting " << p.toXmlString() << endl;
Chris@1546 283 continue;
Chris@1546 284 }
Chris@1546 285 SVCERR << "inPoints: looking at " << p.toXmlString() << endl;
Chris@1546 286 if (bestContaining == Event()) {
Chris@1546 287 bestContaining = p;
Chris@1546 288 continue;
Chris@1546 289 }
Chris@1546 290 auto br = getRange(bestContaining);
Chris@1546 291 if (r.first < br.first && r.second > br.second) {
Chris@1546 292 continue;
Chris@1546 293 }
Chris@1546 294 if (r.first > br.first && r.second < br.second) {
Chris@1546 295 bestContaining = p;
Chris@1546 296 continue;
Chris@1546 297 }
Chris@1546 298 if (p.getFrame() > bestContaining.getFrame() &&
Chris@1546 299 p.getFrame() + p.getDuration() <
Chris@1546 300 bestContaining.getFrame() + bestContaining.getDuration()) {
Chris@1546 301 bestContaining = p;
Chris@1546 302 continue;
Chris@1546 303 }
Chris@1546 304 }
Chris@1546 305
Chris@1546 306 if (bestContaining != Event()) {
Chris@1546 307 point = bestContaining;
Chris@1546 308 } else {
Chris@1546 309 int nearestDistance = -1;
Chris@1546 310 for (const auto &p: onPoints) {
Chris@1546 311 int distance = std::min
Chris@1546 312 (getYForValue(v, p.getValue()) - y,
Chris@1546 313 getYForValue(v, p.getValue() + fabsf(p.getLevel())) - y);
Chris@1546 314 if (distance < 0) distance = -distance;
Chris@1546 315 if (nearestDistance == -1 || distance < nearestDistance) {
Chris@1546 316 nearestDistance = distance;
Chris@1546 317 point = p;
Chris@1546 318 }
Chris@1511 319 }
Chris@1511 320 }
Chris@1511 321
Chris@1511 322 return true;
Chris@1511 323 }
Chris@1511 324
Chris@1511 325 QString
Chris@1518 326 BoxLayer::getLabelPreceding(sv_frame_t frame) const
Chris@1511 327 {
Chris@1518 328 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 329 if (!model) return "";
Chris@1511 330 EventVector points = model->getEventsStartingWithin
Chris@1511 331 (model->getStartFrame(), frame - model->getStartFrame());
Chris@1511 332 if (!points.empty()) {
Chris@1511 333 for (auto i = points.rbegin(); i != points.rend(); ++i) {
Chris@1511 334 if (i->getLabel() != QString()) {
Chris@1511 335 return i->getLabel();
Chris@1511 336 }
Chris@1511 337 }
Chris@1511 338 }
Chris@1511 339 return QString();
Chris@1511 340 }
Chris@1511 341
Chris@1511 342 QString
Chris@1518 343 BoxLayer::getFeatureDescription(LayerGeometryProvider *v,
Chris@1546 344 QPoint &pos) const
Chris@1511 345 {
Chris@1511 346 int x = pos.x();
Chris@1511 347
Chris@1518 348 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 349 if (!model || !model->getSampleRate()) return "";
Chris@1511 350
Chris@1511 351 EventVector points = getLocalPoints(v, x);
Chris@1511 352
Chris@1511 353 if (points.empty()) {
Chris@1511 354 if (!model->isReady()) {
Chris@1511 355 return tr("In progress");
Chris@1511 356 } else {
Chris@1511 357 return tr("No local points");
Chris@1511 358 }
Chris@1511 359 }
Chris@1511 360
Chris@1511 361 Event box;
Chris@1511 362 EventVector::iterator i;
Chris@1511 363
Chris@1511 364 for (i = points.begin(); i != points.end(); ++i) {
Chris@1511 365
Chris@1511 366 int y0 = getYForValue(v, i->getValue());
Chris@1511 367 int y1 = getYForValue(v, i->getValue() + fabsf(i->getLevel()));
Chris@1511 368
Chris@1511 369 if (pos.y() >= y0 && pos.y() <= y1) {
Chris@1511 370 box = *i;
Chris@1511 371 break;
Chris@1511 372 }
Chris@1511 373 }
Chris@1511 374
Chris@1511 375 if (i == points.end()) return tr("No local points");
Chris@1511 376
Chris@1511 377 RealTime rt = RealTime::frame2RealTime(box.getFrame(),
Chris@1511 378 model->getSampleRate());
Chris@1511 379 RealTime rd = RealTime::frame2RealTime(box.getDuration(),
Chris@1511 380 model->getSampleRate());
Chris@1511 381
Chris@1511 382 QString rangeText;
Chris@1511 383
Chris@1511 384 rangeText = tr("%1 %2 - %3 %4")
Chris@1511 385 .arg(box.getValue()).arg(getScaleUnits())
Chris@1511 386 .arg(box.getValue() + fabsf(box.getLevel())).arg(getScaleUnits());
Chris@1511 387
Chris@1511 388 QString text;
Chris@1511 389
Chris@1511 390 if (box.getLabel() == "") {
Chris@1518 391 text = QString(tr("Time:\t%1\nDuration:\t%2\nValue:\t%3\nNo label"))
Chris@1511 392 .arg(rt.toText(true).c_str())
Chris@1511 393 .arg(rd.toText(true).c_str())
Chris@1511 394 .arg(rangeText);
Chris@1511 395 } else {
Chris@1518 396 text = QString(tr("Time:\t%1\nDuration:\t%2\nValue:\t%3\nLabel:\t%4"))
Chris@1511 397 .arg(rt.toText(true).c_str())
Chris@1511 398 .arg(rd.toText(true).c_str())
Chris@1511 399 .arg(rangeText)
Chris@1511 400 .arg(box.getLabel());
Chris@1511 401 }
Chris@1511 402
Chris@1511 403 pos = QPoint(v->getXForFrame(box.getFrame()),
Chris@1511 404 getYForValue(v, box.getValue()));
Chris@1511 405 return text;
Chris@1511 406 }
Chris@1511 407
Chris@1511 408 bool
Chris@1518 409 BoxLayer::snapToFeatureFrame(LayerGeometryProvider *v,
Chris@1546 410 sv_frame_t &frame,
Chris@1546 411 int &resolution,
Chris@1546 412 SnapType snap) const
Chris@1511 413 {
Chris@1518 414 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 415 if (!model) {
Chris@1511 416 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@1511 417 }
Chris@1511 418
Chris@1511 419 // SnapLeft / SnapRight: return frame of nearest feature in that
Chris@1511 420 // direction no matter how far away
Chris@1511 421 //
Chris@1511 422 // SnapNeighbouring: return frame of feature that would be used in
Chris@1511 423 // an editing operation, i.e. closest feature in either direction
Chris@1511 424 // but only if it is "close enough"
Chris@1511 425
Chris@1511 426 resolution = model->getResolution();
Chris@1511 427
Chris@1511 428 if (snap == SnapNeighbouring) {
Chris@1511 429 EventVector points = getLocalPoints(v, v->getXForFrame(frame));
Chris@1511 430 if (points.empty()) return false;
Chris@1511 431 frame = points.begin()->getFrame();
Chris@1511 432 return true;
Chris@1511 433 }
Chris@1511 434
Chris@1511 435 // Normally we snap to the start frame of whichever event we
Chris@1511 436 // find. However here, for SnapRight only, if the end frame of
Chris@1511 437 // whichever event we would have snapped to had we been snapping
Chris@1511 438 // left is closer than the start frame of the next event to the
Chris@1511 439 // right, then we snap to that frame instead. Clear?
Chris@1511 440
Chris@1511 441 Event left;
Chris@1511 442 bool haveLeft = false;
Chris@1511 443 if (model->getNearestEventMatching
Chris@1511 444 (frame, [](Event) { return true; }, EventSeries::Backward, left)) {
Chris@1511 445 haveLeft = true;
Chris@1511 446 }
Chris@1511 447
Chris@1511 448 if (snap == SnapLeft) {
Chris@1511 449 frame = left.getFrame();
Chris@1511 450 return haveLeft;
Chris@1511 451 }
Chris@1511 452
Chris@1511 453 Event right;
Chris@1511 454 bool haveRight = false;
Chris@1511 455 if (model->getNearestEventMatching
Chris@1511 456 (frame, [](Event) { return true; }, EventSeries::Forward, right)) {
Chris@1511 457 haveRight = true;
Chris@1511 458 }
Chris@1511 459
Chris@1511 460 if (haveLeft) {
Chris@1511 461 sv_frame_t leftEnd = left.getFrame() + left.getDuration();
Chris@1511 462 if (leftEnd > frame) {
Chris@1511 463 if (haveRight) {
Chris@1511 464 if (leftEnd - frame < right.getFrame() - frame) {
Chris@1511 465 frame = leftEnd;
Chris@1511 466 } else {
Chris@1511 467 frame = right.getFrame();
Chris@1511 468 }
Chris@1511 469 } else {
Chris@1511 470 frame = leftEnd;
Chris@1511 471 }
Chris@1511 472 return true;
Chris@1511 473 }
Chris@1511 474 }
Chris@1511 475
Chris@1511 476 if (haveRight) {
Chris@1511 477 frame = right.getFrame();
Chris@1511 478 return true;
Chris@1511 479 }
Chris@1511 480
Chris@1511 481 return false;
Chris@1511 482 }
Chris@1511 483
Chris@1511 484 QString
Chris@1518 485 BoxLayer::getScaleUnits() const
Chris@1511 486 {
Chris@1518 487 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 488 if (model) return model->getScaleUnits();
Chris@1511 489 else return "";
Chris@1511 490 }
Chris@1511 491
Chris@1511 492 void
Chris@1518 493 BoxLayer::getScaleExtents(LayerGeometryProvider *v,
Chris@1511 494 double &min, double &max,
Chris@1511 495 bool &log) const
Chris@1511 496 {
Chris@1511 497 min = 0.0;
Chris@1511 498 max = 0.0;
Chris@1511 499 log = false;
Chris@1511 500
Chris@1518 501 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 502 if (!model) return;
Chris@1511 503
Chris@1511 504 QString queryUnits;
Chris@1511 505 queryUnits = getScaleUnits();
Chris@1511 506
Chris@1511 507 if (m_verticalScale == AutoAlignScale) {
Chris@1511 508
Chris@1537 509 if (!v->getVisibleExtentsForUnit(queryUnits, min, max, log)) {
Chris@1511 510
Chris@1518 511 min = model->getValueMinimum();
Chris@1518 512 max = model->getValueMaximum();
Chris@1511 513
Chris@1518 514 // cerr << "BoxLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl;
Chris@1511 515
Chris@1511 516 } else if (log) {
Chris@1511 517
Chris@1511 518 LogRange::mapRange(min, max);
Chris@1511 519
Chris@1518 520 // cerr << "BoxLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl;
Chris@1511 521
Chris@1511 522 }
Chris@1511 523
Chris@1511 524 } else {
Chris@1511 525
Chris@1518 526 min = model->getValueMinimum();
Chris@1518 527 max = model->getValueMaximum();
Chris@1511 528
Chris@1511 529 if (m_verticalScale == LogScale) {
Chris@1511 530 LogRange::mapRange(min, max);
Chris@1511 531 log = true;
Chris@1511 532 }
Chris@1511 533 }
Chris@1511 534
Chris@1511 535 if (max == min) max = min + 1.0;
Chris@1511 536 }
Chris@1511 537
Chris@1511 538 int
Chris@1518 539 BoxLayer::getYForValue(LayerGeometryProvider *v, double val) const
Chris@1511 540 {
Chris@1511 541 double min = 0.0, max = 0.0;
Chris@1511 542 bool logarithmic = false;
Chris@1511 543 int h = v->getPaintHeight();
Chris@1511 544
Chris@1511 545 getScaleExtents(v, min, max, logarithmic);
Chris@1511 546
Chris@1518 547 // cerr << "BoxLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << endl;
Chris@1511 548 // cerr << "h = " << h << ", margin = " << margin << endl;
Chris@1511 549
Chris@1511 550 if (logarithmic) {
Chris@1511 551 val = LogRange::map(val);
Chris@1511 552 }
Chris@1511 553
Chris@1511 554 return int(h - ((val - min) * h) / (max - min));
Chris@1511 555 }
Chris@1511 556
Chris@1511 557 double
Chris@1518 558 BoxLayer::getValueForY(LayerGeometryProvider *v, int y) const
Chris@1511 559 {
Chris@1511 560 double min = 0.0, max = 0.0;
Chris@1511 561 bool logarithmic = false;
Chris@1511 562 int h = v->getPaintHeight();
Chris@1511 563
Chris@1511 564 getScaleExtents(v, min, max, logarithmic);
Chris@1511 565
Chris@1511 566 double val = min + (double(h - y) * double(max - min)) / h;
Chris@1511 567
Chris@1511 568 if (logarithmic) {
Chris@1511 569 val = pow(10.0, val);
Chris@1511 570 }
Chris@1511 571
Chris@1511 572 return val;
Chris@1511 573 }
Chris@1511 574
Chris@1511 575 void
Chris@1518 576 BoxLayer::paint(LayerGeometryProvider *v, QPainter &paint,
Chris@1511 577 QRect rect) const
Chris@1511 578 {
Chris@1518 579 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 580 if (!model || !model->isOK()) return;
Chris@1511 581
Chris@1511 582 sv_samplerate_t sampleRate = model->getSampleRate();
Chris@1511 583 if (!sampleRate) return;
Chris@1511 584
Chris@1518 585 // Profiler profiler("BoxLayer::paint", true);
Chris@1511 586
Chris@1511 587 int x0 = rect.left() - 40, x1 = rect.right();
Chris@1511 588
Chris@1511 589 sv_frame_t wholeFrame0 = v->getFrameForX(0);
Chris@1511 590 sv_frame_t wholeFrame1 = v->getFrameForX(v->getPaintWidth());
Chris@1511 591
Chris@1511 592 EventVector points(model->getEventsSpanning(wholeFrame0,
Chris@1511 593 wholeFrame1 - wholeFrame0));
Chris@1511 594 if (points.empty()) return;
Chris@1511 595
Chris@1511 596 paint.setPen(getBaseQColor());
Chris@1511 597
Chris@1518 598 // SVDEBUG << "BoxLayer::paint: resolution is "
Chris@1511 599 // << model->getResolution() << " frames" << endl;
Chris@1511 600
Chris@1518 601 double min = model->getValueMinimum();
Chris@1518 602 double max = model->getValueMaximum();
Chris@1511 603 if (max == min) max = min + 1.0;
Chris@1511 604
Chris@1511 605 QPoint localPos;
Chris@1511 606 Event illuminatePoint(0);
Chris@1511 607 bool shouldIlluminate = false;
Chris@1511 608
Chris@1511 609 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@1511 610 shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(),
Chris@1511 611 illuminatePoint);
Chris@1511 612 }
Chris@1511 613
Chris@1511 614 paint.save();
Chris@1511 615 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@1511 616
Chris@1514 617 QFontMetrics fm = paint.fontMetrics();
Chris@1514 618
Chris@1511 619 for (EventVector::const_iterator i = points.begin();
Chris@1511 620 i != points.end(); ++i) {
Chris@1511 621
Chris@1511 622 const Event &p(*i);
Chris@1511 623
Chris@1511 624 int x = v->getXForFrame(p.getFrame());
Chris@1511 625 int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x;
Chris@1511 626 int y = getYForValue(v, p.getValue());
Chris@1514 627 int h = getYForValue(v, p.getValue() + fabsf(p.getLevel())) - y;
Chris@1511 628 int ex = x + w;
Chris@1514 629 int gap = v->scalePixelSize(2);
Chris@1511 630
Chris@1511 631 EventVector::const_iterator j = i;
Chris@1511 632 ++j;
Chris@1511 633
Chris@1511 634 if (j != points.end()) {
Chris@1511 635 const Event &q(*j);
Chris@1511 636 int nx = v->getXForFrame(q.getFrame());
Chris@1511 637 if (nx < ex) ex = nx;
Chris@1511 638 }
Chris@1511 639
Chris@1511 640 if (w < 1) w = 1;
Chris@1511 641
Chris@1511 642 paint.setPen(getBaseQColor());
Chris@1514 643 paint.setBrush(Qt::NoBrush);
Chris@1511 644
Chris@1514 645 if ((shouldIlluminate && illuminatePoint == p) ||
Chris@1514 646 (m_editing && m_editingPoint == p)) {
Chris@1511 647
Chris@1514 648 paint.setPen(QPen(getBaseQColor(), v->scalePixelSize(2)));
Chris@1511 649
Chris@1511 650 // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
Chris@1511 651 // replacement (horizontalAdvance) was only added in Qt 5.11
Chris@1511 652 // which is too new for us
Chris@1511 653 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Chris@1511 654
Chris@1514 655 if (abs(h) > 2 * fm.height()) {
Chris@1514 656
Chris@1514 657 QString y0label = QString("%1 %2")
Chris@1514 658 .arg(p.getValue())
Chris@1514 659 .arg(getScaleUnits());
Chris@1514 660
Chris@1514 661 QString y1label = QString("%1 %2")
Chris@1514 662 .arg(p.getValue() + fabsf(p.getLevel()))
Chris@1514 663 .arg(getScaleUnits());
Chris@1514 664
Chris@1514 665 PaintAssistant::drawVisibleText
Chris@1514 666 (v, paint,
Chris@1514 667 x - fm.width(y0label) - gap,
Chris@1514 668 y - fm.descent(),
Chris@1514 669 y0label, PaintAssistant::OutlinedText);
Chris@1514 670
Chris@1514 671 PaintAssistant::drawVisibleText
Chris@1514 672 (v, paint,
Chris@1514 673 x - fm.width(y1label) - gap,
Chris@1514 674 y + h + fm.ascent(),
Chris@1514 675 y1label, PaintAssistant::OutlinedText);
Chris@1514 676
Chris@1514 677 } else {
Chris@1514 678
Chris@1514 679 QString ylabel = QString("%1 %2 - %3 %4")
Chris@1514 680 .arg(p.getValue())
Chris@1514 681 .arg(getScaleUnits())
Chris@1514 682 .arg(p.getValue() + fabsf(p.getLevel()))
Chris@1514 683 .arg(getScaleUnits());
Chris@1514 684
Chris@1514 685 PaintAssistant::drawVisibleText
Chris@1514 686 (v, paint,
Chris@1514 687 x - fm.width(ylabel) - gap,
Chris@1514 688 y - fm.descent(),
Chris@1514 689 ylabel, PaintAssistant::OutlinedText);
Chris@1514 690 }
Chris@1514 691
Chris@1514 692 QString t0label = RealTime::frame2RealTime
Chris@1514 693 (p.getFrame(), model->getSampleRate()).toText(true).c_str();
Chris@1514 694
Chris@1514 695 QString t1label = RealTime::frame2RealTime
Chris@1514 696 (p.getFrame() + p.getDuration(), model->getSampleRate())
Chris@1514 697 .toText(true).c_str();
Chris@1514 698
Chris@1511 699 PaintAssistant::drawVisibleText
Chris@1514 700 (v, paint, x, y + fm.ascent() + gap,
Chris@1514 701 t0label, PaintAssistant::OutlinedText);
Chris@1514 702
Chris@1514 703 if (w > fm.width(t0label) + fm.width(t1label) + gap * 3) {
Chris@1514 704
Chris@1514 705 PaintAssistant::drawVisibleText
Chris@1514 706 (v, paint,
Chris@1514 707 x + w - fm.width(t1label),
Chris@1514 708 y + fm.ascent() + gap,
Chris@1514 709 t1label, PaintAssistant::OutlinedText);
Chris@1514 710
Chris@1514 711 } else {
Chris@1514 712
Chris@1514 713 PaintAssistant::drawVisibleText
Chris@1514 714 (v, paint,
Chris@1514 715 x + w - fm.width(t1label),
Chris@1514 716 y + fm.ascent() + fm.height() + gap,
Chris@1514 717 t1label, PaintAssistant::OutlinedText);
Chris@1514 718 }
Chris@1511 719 }
Chris@1511 720
Chris@1511 721 paint.drawRect(x, y, w, h);
Chris@1511 722 }
Chris@1511 723
Chris@1511 724 for (EventVector::const_iterator i = points.begin();
Chris@1511 725 i != points.end(); ++i) {
Chris@1511 726
Chris@1511 727 const Event &p(*i);
Chris@1511 728
Chris@1514 729 QString label = p.getLabel();
Chris@1514 730 if (label == "") continue;
Chris@1514 731
Chris@1514 732 if (shouldIlluminate && illuminatePoint == p) {
Chris@1514 733 continue; // already handled this in illumination special case
Chris@1514 734 }
Chris@1514 735
Chris@1511 736 int x = v->getXForFrame(p.getFrame());
Chris@1511 737 int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x;
Chris@1511 738 int y = getYForValue(v, p.getValue());
Chris@1511 739
Chris@1514 740 int labelWidth = fm.width(label);
Chris@1511 741
Chris@1511 742 int gap = v->scalePixelSize(2);
Chris@1511 743
Chris@1511 744 if (x + w < x0 || x - labelWidth - gap > x1) {
Chris@1511 745 continue;
Chris@1511 746 }
Chris@1511 747
Chris@1514 748 int labelX, labelY;
Chris@1511 749
Chris@1514 750 labelX = x - labelWidth - gap;
Chris@1514 751 labelY = y - fm.descent();
Chris@1511 752
Chris@1514 753 PaintAssistant::drawVisibleText(v, paint, labelX, labelY, label,
Chris@1514 754 PaintAssistant::OutlinedText);
Chris@1511 755 }
Chris@1511 756
Chris@1511 757 paint.restore();
Chris@1511 758 }
Chris@1511 759
Chris@1511 760 int
Chris@1518 761 BoxLayer::getVerticalScaleWidth(LayerGeometryProvider *v,
Chris@1511 762 bool, QPainter &paint) const
Chris@1511 763 {
Chris@1518 764 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 765 if (!model || m_verticalScale == AutoAlignScale) {
Chris@1511 766 return 0;
Chris@1511 767 } else {
Chris@1511 768 if (m_verticalScale == LogScale) {
Chris@1511 769 return LogNumericalScale().getWidth(v, paint);
Chris@1511 770 } else {
Chris@1511 771 return LinearNumericalScale().getWidth(v, paint);
Chris@1511 772 }
Chris@1511 773 }
Chris@1511 774 }
Chris@1511 775
Chris@1511 776 void
Chris@1518 777 BoxLayer::paintVerticalScale(LayerGeometryProvider *v,
Chris@1511 778 bool, QPainter &paint, QRect) const
Chris@1511 779 {
Chris@1518 780 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 781 if (!model || model->isEmpty()) return;
Chris@1511 782
Chris@1511 783 QString unit;
Chris@1511 784 double min, max;
Chris@1511 785 bool logarithmic;
Chris@1511 786
Chris@1511 787 int w = getVerticalScaleWidth(v, false, paint);
Chris@1511 788
Chris@1511 789 getScaleExtents(v, min, max, logarithmic);
Chris@1511 790
Chris@1511 791 if (logarithmic) {
Chris@1511 792 LogNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@1511 793 } else {
Chris@1511 794 LinearNumericalScale().paintVertical(v, this, paint, 0, min, max);
Chris@1511 795 }
Chris@1511 796
Chris@1511 797 if (getScaleUnits() != "") {
Chris@1511 798 int mw = w - 5;
Chris@1511 799 paint.drawText(5,
Chris@1511 800 5 + paint.fontMetrics().ascent(),
Chris@1511 801 TextAbbrev::abbreviate(getScaleUnits(),
Chris@1511 802 paint.fontMetrics(),
Chris@1511 803 mw));
Chris@1511 804 }
Chris@1511 805 }
Chris@1511 806
Chris@1511 807 void
Chris@1518 808 BoxLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 809 {
Chris@1518 810 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 811 if (!model) return;
Chris@1511 812
Chris@1511 813 sv_frame_t frame = v->getFrameForX(e->x());
Chris@1511 814 if (frame < 0) frame = 0;
Chris@1511 815 frame = frame / model->getResolution() * model->getResolution();
Chris@1511 816
Chris@1518 817 double value = getValueForY(v, e->y());
Chris@1511 818
Chris@1518 819 m_editingPoint = Event(frame, float(value), 0, "");
Chris@1511 820 m_originalPoint = m_editingPoint;
Chris@1511 821
Chris@1511 822 if (m_editingCommand) finish(m_editingCommand);
Chris@1511 823 m_editingCommand = new ChangeEventsCommand(m_model.untyped,
Chris@1518 824 tr("Draw Box"));
Chris@1511 825 m_editingCommand->add(m_editingPoint);
Chris@1511 826
Chris@1511 827 m_editing = true;
Chris@1511 828 }
Chris@1511 829
Chris@1511 830 void
Chris@1518 831 BoxLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 832 {
Chris@1518 833 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 834 if (!model || !m_editing) return;
Chris@1511 835
Chris@1514 836 sv_frame_t dragFrame = v->getFrameForX(e->x());
Chris@1514 837 if (dragFrame < 0) dragFrame = 0;
Chris@1514 838 dragFrame = dragFrame / model->getResolution() * model->getResolution();
Chris@1511 839
Chris@1514 840 sv_frame_t eventFrame = m_originalPoint.getFrame();
Chris@1514 841 sv_frame_t eventDuration = dragFrame - eventFrame;
Chris@1514 842 if (eventDuration < 0) {
Chris@1514 843 eventFrame = eventFrame + eventDuration;
Chris@1514 844 eventDuration = -eventDuration;
Chris@1514 845 } else if (eventDuration == 0) {
Chris@1514 846 eventDuration = model->getResolution();
Chris@1514 847 }
Chris@1511 848
Chris@1518 849 double dragValue = getValueForY(v, e->y());
Chris@1514 850
Chris@1518 851 double eventValue = m_originalPoint.getValue();
Chris@1518 852 double eventFreqDiff = dragValue - eventValue;
Chris@1514 853 if (eventFreqDiff < 0) {
Chris@1518 854 eventValue = eventValue + eventFreqDiff;
Chris@1514 855 eventFreqDiff = -eventFreqDiff;
Chris@1511 856 }
Chris@1511 857
Chris@1511 858 m_editingCommand->remove(m_editingPoint);
Chris@1511 859 m_editingPoint = m_editingPoint
Chris@1514 860 .withFrame(eventFrame)
Chris@1514 861 .withDuration(eventDuration)
Chris@1518 862 .withValue(float(eventValue))
Chris@1514 863 .withLevel(float(eventFreqDiff));
Chris@1511 864 m_editingCommand->add(m_editingPoint);
Chris@1511 865 }
Chris@1511 866
Chris@1511 867 void
Chris@1518 868 BoxLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *)
Chris@1511 869 {
Chris@1518 870 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 871 if (!model || !m_editing) return;
Chris@1511 872 finish(m_editingCommand);
Chris@1511 873 m_editingCommand = nullptr;
Chris@1511 874 m_editing = false;
Chris@1511 875 }
Chris@1511 876
Chris@1511 877 void
Chris@1518 878 BoxLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 879 {
Chris@1518 880 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 881 if (!model) return;
Chris@1511 882
Chris@1511 883 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@1511 884
Chris@1511 885 if (m_editingCommand) {
Chris@1511 886 finish(m_editingCommand);
Chris@1511 887 m_editingCommand = nullptr;
Chris@1511 888 }
Chris@1511 889
Chris@1511 890 m_editing = true;
Chris@1511 891 }
Chris@1511 892
Chris@1511 893 void
Chris@1518 894 BoxLayer::eraseDrag(LayerGeometryProvider *, QMouseEvent *)
Chris@1511 895 {
Chris@1511 896 }
Chris@1511 897
Chris@1511 898 void
Chris@1518 899 BoxLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 900 {
Chris@1518 901 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 902 if (!model || !m_editing) return;
Chris@1511 903
Chris@1511 904 m_editing = false;
Chris@1511 905
Chris@1511 906 Event p(0);
Chris@1511 907 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
Chris@1511 908 if (p.getFrame() != m_editingPoint.getFrame() ||
Chris@1511 909 p.getValue() != m_editingPoint.getValue()) return;
Chris@1511 910
Chris@1511 911 m_editingCommand = new ChangeEventsCommand
Chris@1518 912 (m_model.untyped, tr("Erase Box"));
Chris@1511 913
Chris@1511 914 m_editingCommand->remove(m_editingPoint);
Chris@1511 915
Chris@1511 916 finish(m_editingCommand);
Chris@1511 917 m_editingCommand = nullptr;
Chris@1511 918 m_editing = false;
Chris@1511 919 }
Chris@1511 920
Chris@1511 921 void
Chris@1518 922 BoxLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 923 {
Chris@1518 924 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 925 if (!model) return;
Chris@1511 926
Chris@1511 927 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) {
Chris@1511 928 return;
Chris@1511 929 }
Chris@1511 930
Chris@1511 931 m_dragPointX = v->getXForFrame(m_editingPoint.getFrame());
Chris@1511 932 m_dragPointY = getYForValue(v, m_editingPoint.getValue());
Chris@1511 933
Chris@1511 934 m_originalPoint = m_editingPoint;
Chris@1511 935
Chris@1511 936 if (m_editingCommand) {
Chris@1511 937 finish(m_editingCommand);
Chris@1511 938 m_editingCommand = nullptr;
Chris@1511 939 }
Chris@1511 940
Chris@1511 941 m_editing = true;
Chris@1511 942 m_dragStartX = e->x();
Chris@1511 943 m_dragStartY = e->y();
Chris@1511 944 }
Chris@1511 945
Chris@1511 946 void
Chris@1518 947 BoxLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 948 {
Chris@1518 949 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 950 if (!model || !m_editing) return;
Chris@1511 951
Chris@1511 952 int xdist = e->x() - m_dragStartX;
Chris@1511 953 int ydist = e->y() - m_dragStartY;
Chris@1511 954 int newx = m_dragPointX + xdist;
Chris@1511 955 int newy = m_dragPointY + ydist;
Chris@1511 956
Chris@1511 957 sv_frame_t frame = v->getFrameForX(newx);
Chris@1511 958 if (frame < 0) frame = 0;
Chris@1511 959 frame = frame / model->getResolution() * model->getResolution();
Chris@1511 960
Chris@1511 961 double value = getValueForY(v, newy);
Chris@1511 962
Chris@1511 963 if (!m_editingCommand) {
Chris@1511 964 m_editingCommand = new ChangeEventsCommand
Chris@1511 965 (m_model.untyped,
Chris@1518 966 tr("Drag Box"));
Chris@1511 967 }
Chris@1511 968
Chris@1511 969 m_editingCommand->remove(m_editingPoint);
Chris@1511 970 m_editingPoint = m_editingPoint
Chris@1511 971 .withFrame(frame)
Chris@1511 972 .withValue(float(value));
Chris@1511 973 m_editingCommand->add(m_editingPoint);
Chris@1511 974 }
Chris@1511 975
Chris@1511 976 void
Chris@1518 977 BoxLayer::editEnd(LayerGeometryProvider *, QMouseEvent *)
Chris@1511 978 {
Chris@1518 979 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 980 if (!model || !m_editing) return;
Chris@1511 981
Chris@1511 982 if (m_editingCommand) {
Chris@1511 983
Chris@1511 984 QString newName = m_editingCommand->getName();
Chris@1511 985
Chris@1511 986 if (m_editingPoint.getFrame() != m_originalPoint.getFrame()) {
Chris@1511 987 if (m_editingPoint.getValue() != m_originalPoint.getValue()) {
Chris@1518 988 newName = tr("Edit Box");
Chris@1511 989 } else {
Chris@1518 990 newName = tr("Relocate Box");
Chris@1511 991 }
Chris@1511 992 } else {
Chris@1511 993 newName = tr("Change Point Value");
Chris@1511 994 }
Chris@1511 995
Chris@1511 996 m_editingCommand->setName(newName);
Chris@1511 997 finish(m_editingCommand);
Chris@1511 998 }
Chris@1511 999
Chris@1511 1000 m_editingCommand = nullptr;
Chris@1511 1001 m_editing = false;
Chris@1511 1002 }
Chris@1511 1003
Chris@1511 1004 bool
Chris@1518 1005 BoxLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e)
Chris@1511 1006 {
Chris@1518 1007 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 1008 if (!model) return false;
Chris@1511 1009
Chris@1511 1010 Event region(0);
Chris@1511 1011 if (!getPointToDrag(v, e->x(), e->y(), region)) return false;
Chris@1511 1012
Chris@1515 1013 ItemEditDialog::LabelOptions labelOptions;
Chris@1518 1014 labelOptions.valueLabel = tr("Minimum Value");
Chris@1518 1015 labelOptions.levelLabel = tr("Value Extent");
Chris@1515 1016 labelOptions.valueUnits = getScaleUnits();
Chris@1515 1017 labelOptions.levelUnits = getScaleUnits();
Chris@1515 1018
Chris@1511 1019 ItemEditDialog *dialog = new ItemEditDialog
Chris@1511 1020 (model->getSampleRate(),
Chris@1511 1021 ItemEditDialog::ShowTime |
Chris@1511 1022 ItemEditDialog::ShowDuration |
Chris@1511 1023 ItemEditDialog::ShowValue |
Chris@1515 1024 ItemEditDialog::ShowLevel |
Chris@1511 1025 ItemEditDialog::ShowText,
Chris@1515 1026 labelOptions);
Chris@1511 1027
Chris@1511 1028 dialog->setFrameTime(region.getFrame());
Chris@1511 1029 dialog->setValue(region.getValue());
Chris@1515 1030 dialog->setLevel(region.getLevel());
Chris@1511 1031 dialog->setFrameDuration(region.getDuration());
Chris@1511 1032 dialog->setText(region.getLabel());
Chris@1511 1033
Chris@1511 1034 if (dialog->exec() == QDialog::Accepted) {
Chris@1511 1035
Chris@1518 1036 Event newBox = region
Chris@1511 1037 .withFrame(dialog->getFrameTime())
Chris@1511 1038 .withValue(dialog->getValue())
Chris@1515 1039 .withLevel(dialog->getLevel())
Chris@1511 1040 .withDuration(dialog->getFrameDuration())
Chris@1511 1041 .withLabel(dialog->getText());
Chris@1511 1042
Chris@1511 1043 ChangeEventsCommand *command = new ChangeEventsCommand
Chris@1518 1044 (m_model.untyped, tr("Edit Box"));
Chris@1511 1045 command->remove(region);
Chris@1518 1046 command->add(newBox);
Chris@1511 1047 finish(command);
Chris@1511 1048 }
Chris@1511 1049
Chris@1511 1050 delete dialog;
Chris@1511 1051 return true;
Chris@1511 1052 }
Chris@1511 1053
Chris@1511 1054 void
Chris@1518 1055 BoxLayer::moveSelection(Selection s, sv_frame_t newStartFrame)
Chris@1511 1056 {
Chris@1518 1057 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 1058 if (!model) return;
Chris@1511 1059
Chris@1511 1060 ChangeEventsCommand *command =
Chris@1511 1061 new ChangeEventsCommand(m_model.untyped, tr("Drag Selection"));
Chris@1511 1062
Chris@1511 1063 EventVector points =
Chris@1511 1064 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@1511 1065
Chris@1511 1066 for (EventVector::iterator i = points.begin();
Chris@1511 1067 i != points.end(); ++i) {
Chris@1511 1068
Chris@1511 1069 Event newPoint = (*i)
Chris@1511 1070 .withFrame(i->getFrame() + newStartFrame - s.getStartFrame());
Chris@1511 1071 command->remove(*i);
Chris@1511 1072 command->add(newPoint);
Chris@1511 1073 }
Chris@1511 1074
Chris@1511 1075 finish(command);
Chris@1511 1076 }
Chris@1511 1077
Chris@1511 1078 void
Chris@1518 1079 BoxLayer::resizeSelection(Selection s, Selection newSize)
Chris@1511 1080 {
Chris@1518 1081 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 1082 if (!model || !s.getDuration()) return;
Chris@1511 1083
Chris@1511 1084 ChangeEventsCommand *command =
Chris@1511 1085 new ChangeEventsCommand(m_model.untyped, tr("Resize Selection"));
Chris@1511 1086
Chris@1511 1087 EventVector points =
Chris@1511 1088 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@1511 1089
Chris@1511 1090 double ratio = double(newSize.getDuration()) / double(s.getDuration());
Chris@1511 1091 double oldStart = double(s.getStartFrame());
Chris@1511 1092 double newStart = double(newSize.getStartFrame());
Chris@1511 1093
Chris@1511 1094 for (Event p: points) {
Chris@1511 1095
Chris@1511 1096 double newFrame = (double(p.getFrame()) - oldStart) * ratio + newStart;
Chris@1511 1097 double newDuration = double(p.getDuration()) * ratio;
Chris@1511 1098
Chris@1511 1099 Event newPoint = p
Chris@1511 1100 .withFrame(lrint(newFrame))
Chris@1511 1101 .withDuration(lrint(newDuration));
Chris@1511 1102 command->remove(p);
Chris@1511 1103 command->add(newPoint);
Chris@1511 1104 }
Chris@1511 1105
Chris@1511 1106 finish(command);
Chris@1511 1107 }
Chris@1511 1108
Chris@1511 1109 void
Chris@1518 1110 BoxLayer::deleteSelection(Selection s)
Chris@1511 1111 {
Chris@1518 1112 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 1113 if (!model) return;
Chris@1511 1114
Chris@1511 1115 ChangeEventsCommand *command =
Chris@1511 1116 new ChangeEventsCommand(m_model.untyped, tr("Delete Selected Points"));
Chris@1511 1117
Chris@1511 1118 EventVector points =
Chris@1511 1119 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@1511 1120
Chris@1511 1121 for (EventVector::iterator i = points.begin();
Chris@1511 1122 i != points.end(); ++i) {
Chris@1511 1123
Chris@1511 1124 if (s.contains(i->getFrame())) {
Chris@1511 1125 command->remove(*i);
Chris@1511 1126 }
Chris@1511 1127 }
Chris@1511 1128
Chris@1511 1129 finish(command);
Chris@1511 1130 }
Chris@1511 1131
Chris@1511 1132 void
Chris@1518 1133 BoxLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to)
Chris@1511 1134 {
Chris@1518 1135 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 1136 if (!model) return;
Chris@1511 1137
Chris@1511 1138 EventVector points =
Chris@1511 1139 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@1511 1140
Chris@1511 1141 for (Event p: points) {
Chris@1511 1142 to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame())));
Chris@1511 1143 }
Chris@1511 1144 }
Chris@1511 1145
Chris@1511 1146 bool
Chris@1533 1147 BoxLayer::paste(LayerGeometryProvider *v, const Clipboard &from,
Chris@1533 1148 sv_frame_t /* frameOffset */, bool /* interactive */)
Chris@1511 1149 {
Chris@1518 1150 auto model = ModelById::getAs<BoxModel>(m_model);
Chris@1511 1151 if (!model) return false;
Chris@1511 1152
Chris@1511 1153 const EventVector &points = from.getPoints();
Chris@1511 1154
Chris@1511 1155 bool realign = false;
Chris@1511 1156
Chris@1511 1157 if (clipboardHasDifferentAlignment(v, from)) {
Chris@1511 1158
Chris@1511 1159 QMessageBox::StandardButton button =
Chris@1511 1160 QMessageBox::question(v->getView(), tr("Re-align pasted items?"),
Chris@1511 1161 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 1162 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@1511 1163 QMessageBox::Yes);
Chris@1511 1164
Chris@1511 1165 if (button == QMessageBox::Cancel) {
Chris@1511 1166 return false;
Chris@1511 1167 }
Chris@1511 1168
Chris@1511 1169 if (button == QMessageBox::Yes) {
Chris@1511 1170 realign = true;
Chris@1511 1171 }
Chris@1511 1172 }
Chris@1511 1173
Chris@1511 1174 ChangeEventsCommand *command =
Chris@1511 1175 new ChangeEventsCommand(m_model.untyped, tr("Paste"));
Chris@1511 1176
Chris@1511 1177 for (EventVector::const_iterator i = points.begin();
Chris@1511 1178 i != points.end(); ++i) {
Chris@1511 1179
Chris@1511 1180 sv_frame_t frame = 0;
Chris@1511 1181
Chris@1511 1182 if (!realign) {
Chris@1511 1183
Chris@1511 1184 frame = i->getFrame();
Chris@1511 1185
Chris@1511 1186 } else {
Chris@1511 1187
Chris@1511 1188 if (i->hasReferenceFrame()) {
Chris@1511 1189 frame = i->getReferenceFrame();
Chris@1511 1190 frame = alignFromReference(v, frame);
Chris@1511 1191 } else {
Chris@1511 1192 frame = i->getFrame();
Chris@1511 1193 }
Chris@1511 1194 }
Chris@1511 1195
Chris@1533 1196 Event p = i->withFrame(frame);
Chris@1533 1197
Chris@1511 1198 Event newPoint = p;
Chris@1511 1199 if (!p.hasValue()) {
Chris@1518 1200 newPoint = newPoint.withValue((model->getValueMinimum() +
Chris@1518 1201 model->getValueMaximum()) / 2);
Chris@1511 1202 }
Chris@1511 1203 if (!p.hasDuration()) {
Chris@1511 1204 sv_frame_t nextFrame = frame;
Chris@1511 1205 EventVector::const_iterator j = i;
Chris@1511 1206 for (; j != points.end(); ++j) {
Chris@1511 1207 if (j != i) break;
Chris@1511 1208 }
Chris@1511 1209 if (j != points.end()) {
Chris@1511 1210 nextFrame = j->getFrame();
Chris@1511 1211 }
Chris@1511 1212 if (nextFrame == frame) {
Chris@1511 1213 newPoint = newPoint.withDuration(model->getResolution());
Chris@1511 1214 } else {
Chris@1511 1215 newPoint = newPoint.withDuration(nextFrame - frame);
Chris@1511 1216 }
Chris@1511 1217 }
Chris@1511 1218
Chris@1511 1219 command->add(newPoint);
Chris@1511 1220 }
Chris@1511 1221
Chris@1511 1222 finish(command);
Chris@1511 1223 return true;
Chris@1511 1224 }
Chris@1511 1225
Chris@1511 1226 void
Chris@1518 1227 BoxLayer::toXml(QTextStream &stream,
Chris@1515 1228 QString indent, QString extraAttributes) const
Chris@1511 1229 {
Chris@1511 1230 QString s;
Chris@1511 1231
Chris@1515 1232 s += QString("verticalScale=\"%1\" ").arg(m_verticalScale);
Chris@1511 1233
Chris@1511 1234 SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s);
Chris@1511 1235 }
Chris@1511 1236
Chris@1511 1237 void
Chris@1518 1238 BoxLayer::setProperties(const QXmlAttributes &attributes)
Chris@1511 1239 {
Chris@1511 1240 SingleColourLayer::setProperties(attributes);
Chris@1511 1241
Chris@1511 1242 bool ok;
Chris@1511 1243 VerticalScale scale = (VerticalScale)
Chris@1511 1244 attributes.value("verticalScale").toInt(&ok);
Chris@1511 1245 if (ok) setVerticalScale(scale);
Chris@1511 1246 }
Chris@1511 1247
Chris@1511 1248