annotate layer/Layer.cpp @ 1469:11a150e65ee1 by-id

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