annotate layer/Layer.cpp @ 1485:0e971e3d93e2 by-id

Tidy; also don't show cancel buttons for alignments
author Chris Cannam
date Wed, 17 Jul 2019 13:12:56 +0100
parents e540aa5d89cd
children b402121d8f5f
rev   line source
Chris@127 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@127 2
Chris@127 3 /*
Chris@127 4 Sonic Visualiser
Chris@127 5 An audio file viewer and annotation editor.
Chris@127 6 Centre for Digital Music, Queen Mary, University of London.
Chris@182 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@127 8
Chris@127 9 This program is free software; you can redistribute it and/or
Chris@127 10 modify it under the terms of the GNU General Public License as
Chris@127 11 published by the Free Software Foundation; either version 2 of the
Chris@127 12 License, or (at your option) any later version. See the file
Chris@127 13 COPYING included with this distribution for more information.
Chris@127 14 */
Chris@127 15
Chris@127 16 #include "Layer.h"
Chris@128 17 #include "view/View.h"
Chris@128 18 #include "data/model/Model.h"
Chris@376 19 #include "widgets/CommandHistory.h"
Chris@127 20
Chris@127 21 #include <iostream>
Chris@127 22
Chris@131 23 #include <QMutexLocker>
Chris@267 24 #include <QMouseEvent>
Chris@316 25 #include <QTextStream>
Chris@131 26
Chris@326 27 #include <QDomDocument>
Chris@326 28 #include <QDomElement>
Chris@326 29 #include <QDomNamedNodeMap>
Chris@326 30 #include <QDomAttr>
Chris@326 31
Chris@131 32 #include "LayerFactory.h"
Chris@128 33 #include "base/PlayParameterRepository.h"
Chris@127 34
Chris@272 35 #include <cmath>
Chris@272 36
Chris@267 37 Layer::Layer() :
Chris@283 38 m_haveDraggingRect(false),
Chris@283 39 m_haveCurrentMeasureRect(false)
Chris@127 40 {
Chris@127 41 }
Chris@127 42
Chris@127 43 Layer::~Layer()
Chris@127 44 {
Chris@587 45 // SVDEBUG << "Layer::~Layer(" << this << ")" << endl;
Chris@127 46 }
Chris@127 47
Chris@320 48 void
Chris@1469 49 Layer::connectSignals(ModelId modelId)
Chris@320 50 {
Chris@1469 51 auto model = ModelById::get(modelId);
Chris@1469 52 if (!model) return;
Chris@1469 53
Chris@1481 54 connect(model.get(), SIGNAL(modelChanged(ModelId)),
Chris@1481 55 this, SIGNAL(modelChanged(ModelId)));
Chris@320 56
Chris@1481 57 connect(model.get(), SIGNAL(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)),
Chris@1481 58 this, SIGNAL(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)));
Chris@320 59
Chris@1481 60 connect(model.get(), SIGNAL(completionChanged(ModelId)),
Chris@1481 61 this, SIGNAL(modelCompletionChanged(ModelId)));
Chris@320 62
Chris@1481 63 connect(model.get(), SIGNAL(alignmentCompletionChanged(ModelId)),
Chris@1481 64 this, SIGNAL(modelAlignmentCompletionChanged(ModelId)));
Chris@320 65 }
Chris@320 66
Chris@127 67 QString
Chris@127 68 Layer::getPropertyContainerIconName() const
Chris@127 69 {
Chris@127 70 return LayerFactory::getInstance()->getLayerIconName
Chris@1266 71 (LayerFactory::getInstance()->getLayerType(this));
Chris@127 72 }
Chris@127 73
Chris@363 74 void
Chris@363 75 Layer::setPresentationName(QString name)
Chris@363 76 {
Chris@363 77 m_presentationName = name;
Chris@363 78 }
Chris@363 79
Chris@127 80 QString
Chris@127 81 Layer::getLayerPresentationName() const
Chris@127 82 {
Chris@363 83 if (m_presentationName != "") return m_presentationName;
Chris@203 84
Chris@203 85 LayerFactory *factory = LayerFactory::getInstance();
Chris@203 86 QString layerName = factory->getLayerPresentationName
Chris@203 87 (factory->getLayerType(this));
Chris@203 88
Chris@127 89 QString modelName;
Chris@1469 90 auto model = ModelById::get(getModel());
Chris@1469 91 if (model) modelName = model->objectName();
Chris@1266 92
Chris@127 93 QString text;
Chris@127 94 if (modelName != "") {
Chris@1266 95 text = QString("%1: %2").arg(modelName).arg(layerName);
Chris@127 96 } else {
Chris@1266 97 text = layerName;
Chris@127 98 }
Chris@1266 99
Chris@127 100 return text;
Chris@127 101 }
Chris@127 102
Chris@127 103 void
Chris@127 104 Layer::setObjectName(const QString &name)
Chris@127 105 {
Chris@127 106 QObject::setObjectName(name);
Chris@127 107 emit layerNameChanged();
Chris@127 108 }
Chris@127 109
Chris@1480 110 std::shared_ptr<PlayParameters>
Chris@127 111 Layer::getPlayParameters()
Chris@127 112 {
Chris@1470 113 return PlayParameterRepository::getInstance()->getPlayParameters
Chris@1470 114 (getModel().untyped);
Chris@127 115 }
Chris@127 116
Chris@127 117 void
Chris@918 118 Layer::setLayerDormant(const LayerGeometryProvider *v, bool dormant)
Chris@131 119 {
Chris@131 120 const void *vv = (const void *)v;
Chris@131 121 QMutexLocker locker(&m_dormancyMutex);
Chris@131 122 m_dormancy[vv] = dormant;
Chris@131 123 }
Chris@131 124
Chris@131 125 bool
Chris@918 126 Layer::isLayerDormant(const LayerGeometryProvider *v) const
Chris@131 127 {
Chris@131 128 const void *vv = (const void *)v;
Chris@131 129 QMutexLocker locker(&m_dormancyMutex);
Chris@131 130 if (m_dormancy.find(vv) == m_dormancy.end()) return false;
Chris@131 131 return m_dormancy.find(vv)->second;
Chris@131 132 }
Chris@131 133
Chris@131 134 void
Chris@918 135 Layer::showLayer(LayerGeometryProvider *view, bool show)
Chris@127 136 {
Chris@127 137 setLayerDormant(view, !show);
Chris@127 138 emit layerParametersChanged();
Chris@127 139 }
Chris@127 140
Chris@260 141 bool
Chris@918 142 Layer::getXScaleValue(const LayerGeometryProvider *v, int x, double &value, QString &unit) const
Chris@260 143 {
Chris@260 144 if (!hasTimeXAxis()) return false;
Chris@260 145
Chris@1469 146 auto model = ModelById::get(getModel());
Chris@1469 147 if (!model) return false;
Chris@260 148
Chris@1469 149 value = double(v->getFrameForX(x)) / model->getSampleRate();
Chris@260 150 unit = "s";
Chris@260 151 return true;
Chris@260 152 }
Chris@260 153
Chris@268 154 bool
Chris@918 155 Layer::getYScaleDifference(const LayerGeometryProvider *v, int y0, int y1,
Chris@904 156 double &diff, QString &unit) const
Chris@274 157 {
Chris@904 158 double v0, v1;
Chris@274 159 if (!getYScaleValue(v, y0, v0, unit) ||
Chris@274 160 !getYScaleValue(v, y1, v1, unit)) {
Chris@274 161 diff = 0.f;
Chris@274 162 return false;
Chris@274 163 }
Chris@904 164 diff = fabs(v1 - v0);
Chris@274 165 return true;
Chris@274 166 }
Chris@274 167
Chris@904 168 sv_frame_t
Chris@918 169 Layer::alignToReference(LayerGeometryProvider *v, sv_frame_t frame) const
Chris@359 170 {
Chris@1469 171 auto model = ModelById::get(getModel());
Chris@1469 172 if (model && !model->getAlignmentReference().isNone()) {
Chris@1469 173 return model->alignToReference(frame);
Chris@359 174 } else {
Chris@918 175 return v->getView()->alignToReference(frame);
Chris@359 176 }
Chris@359 177 }
Chris@359 178
Chris@904 179 sv_frame_t
Chris@918 180 Layer::alignFromReference(LayerGeometryProvider *v, sv_frame_t frame) const
Chris@359 181 {
Chris@1469 182 auto model = ModelById::get(getModel());
Chris@1469 183 if (model && !model->getAlignmentReference().isNone()) {
Chris@1469 184 return model->alignFromReference(frame);
Chris@359 185 } else {
Chris@918 186 return v->getView()->alignFromReference(frame);
Chris@359 187 }
Chris@359 188 }
Chris@359 189
Chris@274 190 bool
Chris@918 191 Layer::clipboardHasDifferentAlignment(LayerGeometryProvider *v, const Clipboard &clip) const
Chris@360 192 {
Chris@360 193 // Notes on pasting to an aligned layer:
Chris@360 194 //
Chris@360 195 // Each point may have a reference frame that may differ from the
Chris@360 196 // point's given frame (in its source model). If it has no
Chris@360 197 // reference frame, we have to assume the source model was not
Chris@360 198 // aligned or was the reference model: when cutting or copying
Chris@360 199 // points from a layer, we must always set their reference frame
Chris@360 200 // correctly if we are aligned.
Chris@360 201 //
Chris@360 202 // When pasting:
Chris@360 203 // - if point's reference and aligned frames differ:
Chris@360 204 // - if this layer is aligned:
Chris@360 205 // - if point's aligned frame matches this layer's aligned version
Chris@360 206 // of point's reference frame:
Chris@360 207 // - we can paste at reference frame or our frame
Chris@360 208 // - else
Chris@360 209 // - we can paste at reference frame, result of aligning reference
Chris@360 210 // frame in our model, or literal source frame
Chris@360 211 // - else
Chris@360 212 // - we can paste at reference (our) frame, or literal source frame
Chris@360 213 // - else
Chris@360 214 // - if this layer is aligned:
Chris@360 215 // - we can paste at reference (point's only available) frame,
Chris@360 216 // or result of aligning reference frame in our model
Chris@360 217 // - else
Chris@360 218 // - we can only paste at reference frame
Chris@360 219 //
Chris@360 220 // Which of these alternatives are useful?
Chris@360 221 //
Chris@360 222 // Example: we paste between two tracks that are aligned to the
Chris@360 223 // same reference, and the points are at 10s and 20s in the source
Chris@360 224 // track, corresponding to 5s and 10s in the reference but 20s and
Chris@360 225 // 30s in the target track.
Chris@360 226 //
Chris@360 227 // The obvious default is to paste at 20s and 30s; if we aren't
Chris@360 228 // doing that, would it be better to paste at 5s and 10s or at 10s
Chris@360 229 // and 20s? We probably don't ever want to do the former, do we?
Chris@360 230 // We either want to be literal all the way through, or aligned
Chris@360 231 // all the way through.
Chris@360 232
Chris@1423 233 for (EventVector::const_iterator i = clip.getPoints().begin();
Chris@360 234 i != clip.getPoints().end(); ++i) {
Chris@360 235
Chris@360 236 // In principle, we want to know whether the aligned version
Chris@360 237 // of the reference frame in our layer is the same as the
Chris@360 238 // source frame contained in the clipboard point. However,
Chris@360 239 // because of rounding during alignment, that won't
Chris@360 240 // necessarily be the case even if the clipboard point came
Chris@360 241 // from our layer! What we need to check is whether, if we
Chris@360 242 // aligned the clipboard point's frame back to the reference
Chris@360 243 // using this layer's alignment, we would obtain the same
Chris@360 244 // reference frame as that for the clipboard point.
Chris@360 245
Chris@360 246 // What if the clipboard point has no reference frame? Then
Chris@360 247 // we have to treat it as having its own frame as the
Chris@360 248 // reference (i.e. having been copied from the reference
Chris@360 249 // model).
Chris@360 250
Chris@905 251 sv_frame_t sourceFrame = i->getFrame();
Chris@905 252 sv_frame_t referenceFrame = sourceFrame;
Chris@1423 253 if (i->hasReferenceFrame()) {
Chris@360 254 referenceFrame = i->getReferenceFrame();
Chris@360 255 }
Chris@905 256 sv_frame_t myMappedFrame = alignToReference(v, sourceFrame);
Chris@360 257
Chris@1423 258 // cerr << "sourceFrame = " << sourceFrame << ", referenceFrame = " << referenceFrame << " (have = " << i->hasReferenceFrame() << "), myMappedFrame = " << myMappedFrame << endl;
Chris@360 259
Chris@360 260 if (myMappedFrame != referenceFrame) return true;
Chris@360 261 }
Chris@360 262
Chris@360 263 return false;
Chris@360 264 }
Chris@360 265
Chris@360 266 bool
Chris@268 267 Layer::MeasureRect::operator<(const MeasureRect &mr) const
Chris@268 268 {
Chris@268 269 if (haveFrames) {
Chris@268 270 if (startFrame == mr.startFrame) {
Chris@268 271 if (endFrame != mr.endFrame) {
Chris@268 272 return endFrame < mr.endFrame;
Chris@268 273 }
Chris@268 274 } else {
Chris@268 275 return startFrame < mr.startFrame;
Chris@268 276 }
Chris@268 277 } else {
Chris@268 278 if (pixrect.x() == mr.pixrect.x()) {
Chris@268 279 if (pixrect.width() != mr.pixrect.width()) {
Chris@268 280 return pixrect.width() < mr.pixrect.width();
Chris@268 281 }
Chris@268 282 } else {
Chris@268 283 return pixrect.x() < mr.pixrect.x();
Chris@268 284 }
Chris@268 285 }
Chris@268 286
Chris@268 287 // the two rects are equal in x and width
Chris@268 288
Chris@268 289 if (pixrect.y() == mr.pixrect.y()) {
Chris@268 290 return pixrect.height() < mr.pixrect.height();
Chris@268 291 } else {
Chris@268 292 return pixrect.y() < mr.pixrect.y();
Chris@268 293 }
Chris@268 294 }
Chris@268 295
Chris@316 296 void
Chris@316 297 Layer::MeasureRect::toXml(QTextStream &stream, QString indent) const
Chris@269 298 {
Chris@316 299 stream << indent;
Chris@316 300 stream << QString("<measurement ");
Chris@269 301
Chris@269 302 if (haveFrames) {
Chris@316 303 stream << QString("startFrame=\"%1\" endFrame=\"%2\" ")
Chris@269 304 .arg(startFrame).arg(endFrame);
Chris@269 305 } else {
Chris@316 306 stream << QString("startX=\"%1\" endX=\"%2\" ")
Chris@316 307 .arg(pixrect.x()).arg(pixrect.x() << pixrect.width());
Chris@269 308 }
Chris@269 309
Chris@316 310 stream << QString("startY=\"%1\" endY=\"%2\"/>\n")
Chris@273 311 .arg(startY).arg(endY);
Chris@269 312 }
Chris@269 313
Chris@269 314 void
Chris@269 315 Layer::addMeasurementRect(const QXmlAttributes &attributes)
Chris@269 316 {
Chris@269 317 MeasureRect rect;
Chris@269 318 QString fs = attributes.value("startFrame");
Chris@273 319 int x0 = 0, x1 = 0;
Chris@269 320 if (fs != "") {
Chris@806 321 rect.startFrame = fs.toInt();
Chris@806 322 rect.endFrame = attributes.value("endFrame").toInt();
Chris@269 323 rect.haveFrames = true;
Chris@269 324 } else {
Chris@269 325 x0 = attributes.value("startX").toInt();
Chris@269 326 x1 = attributes.value("endX").toInt();
Chris@269 327 rect.haveFrames = false;
Chris@269 328 }
Chris@273 329 rect.startY = attributes.value("startY").toDouble();
Chris@273 330 rect.endY = attributes.value("endY").toDouble();
Chris@273 331 rect.pixrect = QRect(x0, 0, x1 - x0, 0);
Chris@269 332 addMeasureRectToSet(rect);
Chris@269 333 }
Chris@269 334
Chris@269 335 QString
Chris@268 336 Layer::AddMeasurementRectCommand::getName() const
Chris@268 337 {
Chris@268 338 return tr("Make Measurement");
Chris@268 339 }
Chris@268 340
Chris@268 341 void
Chris@268 342 Layer::AddMeasurementRectCommand::execute()
Chris@268 343 {
Chris@269 344 m_layer->addMeasureRectToSet(m_rect);
Chris@268 345 }
Chris@268 346
Chris@268 347 void
Chris@268 348 Layer::AddMeasurementRectCommand::unexecute()
Chris@268 349 {
Chris@269 350 m_layer->deleteMeasureRectFromSet(m_rect);
Chris@268 351 }
Chris@268 352
Chris@283 353 QString
Chris@283 354 Layer::DeleteMeasurementRectCommand::getName() const
Chris@283 355 {
Chris@283 356 return tr("Delete Measurement");
Chris@283 357 }
Chris@283 358
Chris@283 359 void
Chris@283 360 Layer::DeleteMeasurementRectCommand::execute()
Chris@283 361 {
Chris@283 362 m_layer->deleteMeasureRectFromSet(m_rect);
Chris@283 363 }
Chris@283 364
Chris@283 365 void
Chris@283 366 Layer::DeleteMeasurementRectCommand::unexecute()
Chris@283 367 {
Chris@283 368 m_layer->addMeasureRectToSet(m_rect);
Chris@283 369 }
Chris@283 370
Chris@267 371 void
Chris@918 372 Layer::measureStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@267 373 {
Chris@1434 374 m_draggingRect.haveFrames = hasTimeXAxis();
Chris@1434 375
Chris@1434 376 // NB if haveFrames, then pixrect x and width will be rewritten on
Chris@1434 377 // every paint according to the current locations of the
Chris@1434 378 // definitive frame values. So we should set the start frame value
Chris@1434 379 // once on measureStart, and then not modify it on drag (to avoid
Chris@1434 380 // drift from repeated conversion back and forth).
Chris@1434 381
Chris@1434 382 m_draggingRect.pixrect = QRect(e->x(), e->y(), 0, 0);
Chris@1434 383
Chris@1434 384 if (m_draggingRect.haveFrames) {
Chris@1434 385 m_draggingRect.startFrame = v->getFrameForX(e->x());
Chris@1434 386 m_draggingRect.endFrame = v->getFrameForX(e->x());
Chris@1434 387 }
Chris@1434 388
Chris@1434 389 setMeasureRectYCoord(v, m_draggingRect, true, e->y());
Chris@1434 390 setMeasureRectYCoord(v, m_draggingRect, false, e->y());
Chris@1434 391
Chris@267 392 m_haveDraggingRect = true;
Chris@267 393 }
Chris@267 394
Chris@267 395 void
Chris@918 396 Layer::measureDrag(LayerGeometryProvider *v, QMouseEvent *e)
Chris@267 397 {
Chris@267 398 if (!m_haveDraggingRect) return;
Chris@268 399
Chris@1434 400 m_draggingRect.pixrect.setHeight(e->y() - m_draggingRect.pixrect.y());
Chris@1434 401
Chris@1434 402 if (m_draggingRect.haveFrames) {
Chris@1434 403 m_draggingRect.endFrame = v->getFrameForX(e->x());
Chris@1434 404 } else {
Chris@1434 405 m_draggingRect.pixrect.setWidth(e->x() - m_draggingRect.pixrect.x());
Chris@1434 406 }
Chris@1434 407
Chris@1434 408 setMeasureRectYCoord(v, m_draggingRect, false, e->y());
Chris@267 409 }
Chris@267 410
Chris@267 411 void
Chris@918 412 Layer::measureEnd(LayerGeometryProvider *v, QMouseEvent *e)
Chris@267 413 {
Chris@267 414 if (!m_haveDraggingRect) return;
Chris@267 415 measureDrag(v, e);
Chris@283 416
Chris@283 417 if (!m_draggingRect.pixrect.isNull()) {
Chris@283 418 CommandHistory::getInstance()->addCommand
Chris@283 419 (new AddMeasurementRectCommand(this, m_draggingRect));
Chris@283 420 }
Chris@268 421
Chris@267 422 m_haveDraggingRect = false;
Chris@267 423 }
Chris@267 424
Chris@267 425 void
Chris@918 426 Layer::measureDoubleClick(LayerGeometryProvider *, QMouseEvent *)
Chris@280 427 {
Chris@283 428 // nothing, in the base class
Chris@283 429 }
Chris@283 430
Chris@283 431 void
Chris@283 432 Layer::deleteCurrentMeasureRect()
Chris@283 433 {
Chris@283 434 if (!m_haveCurrentMeasureRect) return;
Chris@283 435
Chris@283 436 MeasureRectSet::const_iterator focusRectItr =
Chris@283 437 findFocusedMeasureRect(m_currentMeasureRectPoint);
Chris@283 438
Chris@283 439 if (focusRectItr == m_measureRects.end()) return;
Chris@283 440
Chris@283 441 CommandHistory::getInstance()->addCommand
Chris@283 442 (new DeleteMeasurementRectCommand(this, *focusRectItr));
Chris@280 443 }
Chris@280 444
Chris@280 445 void
Chris@918 446 Layer::paintMeasurementRects(LayerGeometryProvider *v, QPainter &paint,
Chris@272 447 bool showFocus, QPoint focusPoint) const
Chris@267 448 {
Chris@273 449 updateMeasurePixrects(v);
Chris@272 450
Chris@272 451 MeasureRectSet::const_iterator focusRectItr = m_measureRects.end();
Chris@272 452
Chris@267 453 if (m_haveDraggingRect) {
Chris@272 454
Chris@270 455 paintMeasurementRect(v, paint, m_draggingRect, true);
Chris@272 456
Chris@272 457 } else if (showFocus) {
Chris@272 458
Chris@272 459 focusRectItr = findFocusedMeasureRect(focusPoint);
Chris@267 460 }
Chris@267 461
Chris@283 462 m_haveCurrentMeasureRect = false;
Chris@283 463
Chris@268 464 for (MeasureRectSet::const_iterator i = m_measureRects.begin();
Chris@268 465 i != m_measureRects.end(); ++i) {
Chris@283 466
Chris@283 467 bool focused = (i == focusRectItr);
Chris@283 468 paintMeasurementRect(v, paint, *i, focused);
Chris@283 469
Chris@283 470 if (focused) {
Chris@283 471 m_haveCurrentMeasureRect = true;
Chris@283 472 m_currentMeasureRectPoint = focusPoint;
Chris@283 473 }
Chris@267 474 }
Chris@267 475 }
Chris@267 476
Chris@272 477 bool
Chris@918 478 Layer::nearestMeasurementRectChanged(LayerGeometryProvider *v, QPoint prev, QPoint now) const
Chris@272 479 {
Chris@273 480 updateMeasurePixrects(v);
Chris@272 481
Chris@272 482 MeasureRectSet::const_iterator i0 = findFocusedMeasureRect(prev);
Chris@272 483 MeasureRectSet::const_iterator i1 = findFocusedMeasureRect(now);
Chris@272 484
Chris@272 485 return (i0 != i1);
Chris@272 486 }
Chris@272 487
Chris@272 488 void
Chris@918 489 Layer::updateMeasurePixrects(LayerGeometryProvider *v) const
Chris@272 490 {
Chris@905 491 sv_frame_t sf = v->getStartFrame();
Chris@905 492 sv_frame_t ef = v->getEndFrame();
Chris@272 493
Chris@272 494 for (MeasureRectSet::const_iterator i = m_measureRects.begin();
Chris@272 495 i != m_measureRects.end(); ++i) {
Chris@272 496
Chris@273 497 // This logic depends on the fact that if one measure rect in
Chris@273 498 // a layer has frame values, they all will. That is in fact
Chris@273 499 // the case, because haveFrames is based on whether the layer
Chris@273 500 // hasTimeXAxis() or not. Measure rect ordering in the rect
Chris@273 501 // set wouldn't work correctly either, if haveFrames could
Chris@273 502 // vary.
Chris@272 503
Chris@273 504 if (i->haveFrames) {
Chris@273 505 if (i->startFrame >= ef) break;
Chris@273 506 if (i->endFrame <= sf) continue;
Chris@273 507 }
Chris@272 508
Chris@273 509 int x0 = i->pixrect.x();
Chris@273 510 int x1 = x0 + i->pixrect.width();
Chris@273 511
Chris@273 512 if (i->haveFrames) {
Chris@273 513 if (i->startFrame >= v->getStartFrame()) {
Chris@273 514 x0 = v->getXForFrame(i->startFrame);
Chris@273 515 }
Chris@806 516 if (i->endFrame <= int(v->getEndFrame())) {
Chris@273 517 x1 = v->getXForFrame(i->endFrame);
Chris@273 518 }
Chris@272 519 }
Chris@272 520
Chris@273 521 i->pixrect = QRect(x0, i->pixrect.y(), x1 - x0, i->pixrect.height());
Chris@273 522
Chris@273 523 updateMeasureRectYCoords(v, *i);
Chris@273 524 }
Chris@273 525 }
Chris@273 526
Chris@273 527 void
Chris@918 528 Layer::updateMeasureRectYCoords(LayerGeometryProvider *v, const MeasureRect &r) const
Chris@273 529 {
Chris@918 530 int y0 = int(lrint(r.startY * v->getPaintHeight()));
Chris@918 531 int y1 = int(lrint(r.endY * v->getPaintHeight()));
Chris@273 532 r.pixrect = QRect(r.pixrect.x(), y0, r.pixrect.width(), y1 - y0);
Chris@273 533 }
Chris@273 534
Chris@273 535 void
Chris@918 536 Layer::setMeasureRectYCoord(LayerGeometryProvider *v, MeasureRect &r, bool start, int y) const
Chris@273 537 {
Chris@273 538 if (start) {
Chris@918 539 r.startY = double(y) / double(v->getPaintHeight());
Chris@273 540 r.endY = r.startY;
Chris@273 541 } else {
Chris@918 542 r.endY = double(y) / double(v->getPaintHeight());
Chris@272 543 }
Chris@272 544 }
Chris@272 545
Chris@283 546 void
Chris@918 547 Layer::setMeasureRectFromPixrect(LayerGeometryProvider *v, MeasureRect &r, QRect pixrect) const
Chris@283 548 {
Chris@283 549 r.pixrect = pixrect;
Chris@283 550 r.haveFrames = hasTimeXAxis();
Chris@283 551 if (r.haveFrames) {
Chris@283 552 r.startFrame = v->getFrameForX(pixrect.x());
Chris@283 553 r.endFrame = v->getFrameForX(pixrect.x() + pixrect.width());
Chris@283 554 }
Chris@283 555 setMeasureRectYCoord(v, r, true, pixrect.y());
Chris@283 556 setMeasureRectYCoord(v, r, false, pixrect.y() + pixrect.height());
Chris@283 557 }
Chris@283 558
Chris@272 559 Layer::MeasureRectSet::const_iterator
Chris@272 560 Layer::findFocusedMeasureRect(QPoint focusPoint) const
Chris@272 561 {
Chris@904 562 double frDist = 0;
Chris@272 563 MeasureRectSet::const_iterator focusRectItr = m_measureRects.end();
Chris@272 564
Chris@272 565 for (MeasureRectSet::const_iterator i = m_measureRects.begin();
Chris@272 566 i != m_measureRects.end(); ++i) {
Chris@272 567
Chris@272 568 if (!i->pixrect.adjusted(-2, -2, 2, 2).contains(focusPoint)) continue;
Chris@272 569
Chris@272 570 int cx = i->pixrect.x() + i->pixrect.width()/2;
Chris@272 571 int cy = i->pixrect.y() + i->pixrect.height()/2;
Chris@272 572 int xd = focusPoint.x() - cx;
Chris@272 573 int yd = focusPoint.y() - cy;
Chris@272 574
Chris@904 575 double d = sqrt(double(xd * xd + yd * yd));
Chris@272 576
Chris@272 577 if (focusRectItr == m_measureRects.end() || d < frDist) {
Chris@272 578 focusRectItr = i;
Chris@272 579 frDist = d;
Chris@272 580 }
Chris@272 581 }
Chris@272 582
Chris@272 583 return focusRectItr;
Chris@272 584 }
Chris@272 585
Chris@268 586 void
Chris@918 587 Layer::paintMeasurementRect(LayerGeometryProvider *v, QPainter &paint,
Chris@270 588 const MeasureRect &r, bool focus) const
Chris@268 589 {
Chris@268 590 if (r.haveFrames) {
Chris@268 591
Chris@268 592 int x0 = -1;
Chris@918 593 int x1 = v->getPaintWidth() + 1;
Chris@268 594
Chris@268 595 if (r.startFrame >= v->getStartFrame()) {
Chris@268 596 x0 = v->getXForFrame(r.startFrame);
Chris@268 597 }
Chris@806 598 if (r.endFrame <= v->getEndFrame()) {
Chris@268 599 x1 = v->getXForFrame(r.endFrame);
Chris@268 600 }
Chris@268 601
Chris@272 602 QRect pr = QRect(x0, r.pixrect.y(), x1 - x0, r.pixrect.height());
Chris@268 603 r.pixrect = pr;
Chris@268 604 }
Chris@274 605
Chris@274 606 v->drawMeasurementRect(paint, this, r.pixrect.normalized(), focus);
Chris@268 607 }
Chris@268 608
Chris@1315 609 bool
Chris@1315 610 Layer::valueExtentsMatchMine(LayerGeometryProvider *v) const
Chris@1315 611 {
Chris@1315 612 double min, min_;
Chris@1315 613 double max, max_;
Chris@1315 614 bool logarithmic, logarithmic_;
Chris@1315 615 QString unit;
Chris@1315 616
Chris@1315 617 if (!getValueExtents(min_, max_, logarithmic_, unit)) {
Chris@1315 618 return false;
Chris@1315 619 }
Chris@1315 620
Chris@1315 621 if (!v->getValueExtents(unit, min, max, logarithmic)) {
Chris@1315 622 return false;
Chris@1315 623 }
Chris@1315 624
Chris@1315 625 if (min != min_ ||
Chris@1315 626 max != max_ ||
Chris@1315 627 logarithmic != logarithmic_) {
Chris@1315 628 return false;
Chris@1315 629 }
Chris@1315 630
Chris@1315 631 return true;
Chris@1315 632 }
Chris@1315 633
Chris@316 634 void
Chris@316 635 Layer::toXml(QTextStream &stream,
Chris@316 636 QString indent, QString extraAttributes) const
Chris@268 637 {
Chris@316 638 stream << indent;
Chris@268 639
Chris@363 640 if (m_presentationName != "") {
Chris@363 641 extraAttributes = QString("%1 presentationName=\"%2\"")
Chris@363 642 .arg(extraAttributes).arg(encodeEntities(m_presentationName));
Chris@363 643 }
Chris@363 644
Chris@1469 645 int modelExportId = -1;
Chris@1469 646 auto model = ModelById::get(getModel());
Chris@1469 647 if (model) modelExportId = model->getExportId();
Chris@1469 648
Chris@316 649 stream << QString("<layer id=\"%2\" type=\"%1\" name=\"%3\" model=\"%4\" %5")
Chris@1266 650 .arg(encodeEntities(LayerFactory::getInstance()->getLayerTypeName
Chris@268 651 (LayerFactory::getInstance()->getLayerType(this))))
Chris@1439 652 .arg(getExportId())
Chris@1266 653 .arg(encodeEntities(objectName()))
Chris@1469 654 .arg(modelExportId)
Chris@1266 655 .arg(extraAttributes);
Chris@268 656
Chris@269 657 if (m_measureRects.empty()) {
Chris@316 658 stream << QString("/>\n");
Chris@316 659 return;
Chris@269 660 }
Chris@269 661
Chris@316 662 stream << QString(">\n");
Chris@269 663
Chris@269 664 for (MeasureRectSet::const_iterator i = m_measureRects.begin();
Chris@269 665 i != m_measureRects.end(); ++i) {
Chris@316 666 i->toXml(stream, indent + " ");
Chris@269 667 }
Chris@269 668
Chris@316 669 stream << QString("</layer>\n");
Chris@268 670 }
Chris@269 671
Chris@316 672 void
Chris@316 673 Layer::toBriefXml(QTextStream &stream,
Chris@316 674 QString indent, QString extraAttributes) const
Chris@269 675 {
Chris@316 676 stream << indent;
Chris@269 677
Chris@363 678 if (m_presentationName != "") {
Chris@363 679 extraAttributes = QString("%1 presentationName=\"%2\"")
Chris@363 680 .arg(extraAttributes).arg(encodeEntities(m_presentationName));
Chris@363 681 }
Chris@363 682
Chris@1469 683 int modelExportId = -1;
Chris@1469 684 auto model = ModelById::get(getModel());
Chris@1469 685 if (model) modelExportId = model->getExportId();
Chris@1469 686
Chris@316 687 stream << QString("<layer id=\"%2\" type=\"%1\" name=\"%3\" model=\"%4\" %5/>\n")
Chris@1266 688 .arg(encodeEntities(LayerFactory::getInstance()->getLayerTypeName
Chris@269 689 (LayerFactory::getInstance()->getLayerType(this))))
Chris@1439 690 .arg(getExportId())
Chris@1266 691 .arg(encodeEntities(objectName()))
Chris@1469 692 .arg(modelExportId)
Chris@269 693 .arg(extraAttributes);
Chris@269 694 }
Chris@269 695