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

Further layer updates for ById
author Chris Cannam
date Fri, 28 Jun 2019 17:37:22 +0100
parents e2b6a13a1f69
children f2525e6cbdf1
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@35 2
Chris@35 3 /*
Chris@59 4 Sonic Visualiser
Chris@59 5 An audio file viewer and annotation editor.
Chris@59 6 Centre for Digital Music, Queen Mary, University of London.
Chris@59 7 This file copyright 2006 Chris Cannam.
Chris@35 8
Chris@59 9 This program is free software; you can redistribute it and/or
Chris@59 10 modify it under the terms of the GNU General Public License as
Chris@59 11 published by the Free Software Foundation; either version 2 of the
Chris@59 12 License, or (at your option) any later version. See the file
Chris@59 13 COPYING included with this distribution for more information.
Chris@35 14 */
Chris@35 15
Chris@35 16 #include "TextLayer.h"
Chris@35 17
Chris@128 18 #include "data/model/Model.h"
Chris@35 19 #include "base/RealTime.h"
Chris@35 20 #include "base/Profiler.h"
Chris@376 21 #include "ColourDatabase.h"
Chris@128 22 #include "view/View.h"
Chris@35 23
Chris@128 24 #include "data/model/TextModel.h"
Chris@35 25
Chris@35 26 #include <QPainter>
Chris@35 27 #include <QMouseEvent>
Chris@36 28 #include <QInputDialog>
Chris@316 29 #include <QTextStream>
Chris@360 30 #include <QMessageBox>
Chris@35 31
Chris@35 32 #include <iostream>
Chris@35 33 #include <cmath>
Chris@35 34
Chris@44 35 TextLayer::TextLayer() :
Chris@287 36 SingleColourLayer(),
Chris@1408 37 m_model(nullptr),
Chris@35 38 m_editing(false),
Chris@35 39 m_originalPoint(0, 0.0, tr("Empty Label")),
Chris@35 40 m_editingPoint(0, 0.0, tr("Empty Label")),
Chris@1408 41 m_editingCommand(nullptr)
Chris@35 42 {
Chris@44 43
Chris@35 44 }
Chris@35 45
Chris@1470 46 int
Chris@1470 47 TextLayer::getCompletion(LayerGeometryProvider *) const
Chris@1470 48 {
Chris@1470 49 auto model = ModelById::get(m_model);
Chris@1470 50 if (model) return model->getCompletion();
Chris@1470 51 else return 0;
Chris@1470 52 }
Chris@1470 53
Chris@35 54 void
Chris@1470 55 TextLayer::setModel(ModelId model)
Chris@35 56 {
Chris@35 57 if (m_model == model) return;
Chris@35 58 m_model = model;
Chris@35 59
Chris@320 60 connectSignals(m_model);
Chris@35 61
Chris@587 62 // SVDEBUG << "TextLayer::setModel(" << model << ")" << endl;
Chris@35 63
Chris@35 64 emit modelReplaced();
Chris@35 65 }
Chris@35 66
Chris@35 67 Layer::PropertyList
Chris@35 68 TextLayer::getProperties() const
Chris@35 69 {
Chris@287 70 PropertyList list = SingleColourLayer::getProperties();
Chris@35 71 return list;
Chris@35 72 }
Chris@35 73
Chris@87 74 QString
Chris@87 75 TextLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 76 {
Chris@287 77 return SingleColourLayer::getPropertyLabel(name);
Chris@87 78 }
Chris@87 79
Chris@35 80 Layer::PropertyType
Chris@287 81 TextLayer::getPropertyType(const PropertyName &name) const
Chris@35 82 {
Chris@287 83 return SingleColourLayer::getPropertyType(name);
Chris@35 84 }
Chris@35 85
Chris@35 86 int
Chris@35 87 TextLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@1266 88 int *min, int *max, int *deflt) const
Chris@35 89 {
Chris@287 90 return SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@35 91 }
Chris@35 92
Chris@35 93 QString
Chris@35 94 TextLayer::getPropertyValueLabel(const PropertyName &name,
Chris@1266 95 int value) const
Chris@35 96 {
Chris@287 97 return SingleColourLayer::getPropertyValueLabel(name, value);
Chris@35 98 }
Chris@35 99
Chris@35 100 void
Chris@35 101 TextLayer::setProperty(const PropertyName &name, int value)
Chris@35 102 {
Chris@287 103 SingleColourLayer::setProperty(name, value);
Chris@35 104 }
Chris@35 105
Chris@79 106 bool
Chris@908 107 TextLayer::getValueExtents(double &, double &, bool &, QString &) const
Chris@79 108 {
Chris@79 109 return false;
Chris@79 110 }
Chris@79 111
Chris@35 112 bool
Chris@918 113 TextLayer::isLayerScrollable(const LayerGeometryProvider *v) const
Chris@35 114 {
Chris@35 115 QPoint discard;
Chris@44 116 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@35 117 }
Chris@35 118
Chris@1437 119 EventVector
Chris@918 120 TextLayer::getLocalPoints(LayerGeometryProvider *v, int x, int y) const
Chris@35 121 {
Chris@1437 122 if (!m_model) return {};
Chris@35 123
Chris@1437 124 int overlap = ViewManager::scalePixelSize(150);
Chris@35 125
Chris@1437 126 sv_frame_t frame0 = v->getFrameForX(-overlap);
Chris@1437 127 sv_frame_t frame1 = v->getFrameForX(v->getPaintWidth() + overlap);
Chris@1437 128
Chris@1437 129 EventVector points(m_model->getEventsSpanning(frame0, frame1 - frame0));
Chris@35 130
Chris@1437 131 EventVector rv;
Chris@552 132 QFontMetrics metrics = QFontMetrics(QFont());
Chris@35 133
Chris@1437 134 for (EventVector::iterator i = points.begin(); i != points.end(); ++i) {
Chris@35 135
Chris@1437 136 Event p(*i);
Chris@35 137
Chris@1437 138 int px = v->getXForFrame(p.getFrame());
Chris@1437 139 int py = getYForHeight(v, p.getValue());
Chris@35 140
Chris@1437 141 QString label = p.getLabel();
Chris@1266 142 if (label == "") {
Chris@1266 143 label = tr("<no text>");
Chris@1266 144 }
Chris@35 145
Chris@1266 146 QRect rect = metrics.boundingRect
Chris@1266 147 (QRect(0, 0, 150, 200),
Chris@1266 148 Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, label);
Chris@35 149
Chris@1266 150 if (py + rect.height() > v->getPaintHeight()) {
Chris@1266 151 if (rect.height() > v->getPaintHeight()) py = 0;
Chris@1266 152 else py = v->getPaintHeight() - rect.height() - 1;
Chris@1266 153 }
Chris@35 154
Chris@1266 155 if (x >= px && x < px + rect.width() &&
Chris@1266 156 y >= py && y < py + rect.height()) {
Chris@1437 157 rv.push_back(p);
Chris@1266 158 }
Chris@35 159 }
Chris@35 160
Chris@35 161 return rv;
Chris@35 162 }
Chris@35 163
Chris@552 164 bool
Chris@1437 165 TextLayer::getPointToDrag(LayerGeometryProvider *v, int x, int y, Event &p) const
Chris@552 166 {
Chris@552 167 if (!m_model) return false;
Chris@552 168
Chris@1437 169 sv_frame_t a = v->getFrameForX(x - ViewManager::scalePixelSize(120));
Chris@1437 170 sv_frame_t b = v->getFrameForX(x + ViewManager::scalePixelSize(10));
Chris@1437 171 EventVector onPoints = m_model->getEventsWithin(a, b);
Chris@552 172 if (onPoints.empty()) return false;
Chris@552 173
Chris@908 174 double nearestDistance = -1;
Chris@552 175
Chris@1437 176 for (EventVector::const_iterator i = onPoints.begin();
Chris@552 177 i != onPoints.end(); ++i) {
Chris@552 178
Chris@1437 179 double yd = getYForHeight(v, i->getValue()) - y;
Chris@1437 180 double xd = v->getXForFrame(i->getFrame()) - x;
Chris@908 181 double distance = sqrt(yd*yd + xd*xd);
Chris@552 182
Chris@552 183 if (nearestDistance == -1 || distance < nearestDistance) {
Chris@552 184 nearestDistance = distance;
Chris@552 185 p = *i;
Chris@552 186 }
Chris@552 187 }
Chris@552 188
Chris@552 189 return true;
Chris@552 190 }
Chris@552 191
Chris@35 192 QString
Chris@918 193 TextLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const
Chris@35 194 {
Chris@35 195 int x = pos.x();
Chris@35 196
Chris@35 197 if (!m_model || !m_model->getSampleRate()) return "";
Chris@35 198
Chris@1437 199 EventVector points = getLocalPoints(v, x, pos.y());
Chris@35 200
Chris@35 201 if (points.empty()) {
Chris@1266 202 if (!m_model->isReady()) {
Chris@1266 203 return tr("In progress");
Chris@1266 204 } else {
Chris@1266 205 return "";
Chris@1266 206 }
Chris@35 207 }
Chris@35 208
Chris@1437 209 sv_frame_t useFrame = points.begin()->getFrame();
Chris@35 210
Chris@35 211 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
Chris@35 212
Chris@35 213 QString text;
Chris@35 214
Chris@1437 215 if (points.begin()->getLabel() == "") {
Chris@1266 216 text = QString(tr("Time:\t%1\nHeight:\t%2\nLabel:\t%3"))
Chris@1266 217 .arg(rt.toText(true).c_str())
Chris@1437 218 .arg(points.begin()->getValue())
Chris@1437 219 .arg(points.begin()->getLabel());
Chris@35 220 }
Chris@35 221
Chris@44 222 pos = QPoint(v->getXForFrame(useFrame),
Chris@1437 223 getYForHeight(v, points.begin()->getValue()));
Chris@35 224 return text;
Chris@35 225 }
Chris@35 226
Chris@35 227
Chris@35 228 //!!! too much overlap with TimeValueLayer/TimeInstantLayer
Chris@35 229
Chris@35 230 bool
Chris@918 231 TextLayer::snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame,
Chris@1266 232 int &resolution,
Chris@1266 233 SnapType snap) const
Chris@35 234 {
Chris@35 235 if (!m_model) {
Chris@1266 236 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@35 237 }
Chris@35 238
Chris@1437 239 // SnapLeft / SnapRight: return frame of nearest feature in that
Chris@1437 240 // direction no matter how far away
Chris@1437 241 //
Chris@1437 242 // SnapNeighbouring: return frame of feature that would be used in
Chris@1437 243 // an editing operation, i.e. closest feature in either direction
Chris@1437 244 // but only if it is "close enough"
Chris@1437 245
Chris@35 246 resolution = m_model->getResolution();
Chris@35 247
Chris@35 248 if (snap == SnapNeighbouring) {
Chris@1437 249 EventVector points = getLocalPoints(v, v->getXForFrame(frame), -1);
Chris@1266 250 if (points.empty()) return false;
Chris@1437 251 frame = points.begin()->getFrame();
Chris@1266 252 return true;
Chris@35 253 }
Chris@35 254
Chris@1437 255 Event e;
Chris@1437 256 if (m_model->getNearestEventMatching
Chris@1437 257 (frame,
Chris@1437 258 [](Event) { return true; },
Chris@1437 259 snap == SnapLeft ? EventSeries::Backward : EventSeries::Forward,
Chris@1437 260 e)) {
Chris@1437 261 frame = e.getFrame();
Chris@1437 262 return true;
Chris@35 263 }
Chris@35 264
Chris@1437 265 return false;
Chris@35 266 }
Chris@35 267
Chris@35 268 int
Chris@918 269 TextLayer::getYForHeight(LayerGeometryProvider *v, double height) const
Chris@35 270 {
Chris@918 271 int h = v->getPaintHeight();
Chris@35 272 return h - int(height * h);
Chris@35 273 }
Chris@35 274
Chris@908 275 double
Chris@918 276 TextLayer::getHeightForY(LayerGeometryProvider *v, int y) const
Chris@35 277 {
Chris@918 278 int h = v->getPaintHeight();
Chris@908 279 return double(h - y) / h;
Chris@35 280 }
Chris@35 281
Chris@35 282 void
Chris@916 283 TextLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@35 284 {
Chris@35 285 if (!m_model || !m_model->isOK()) return;
Chris@35 286
Chris@908 287 sv_samplerate_t sampleRate = m_model->getSampleRate();
Chris@35 288 if (!sampleRate) return;
Chris@35 289
Chris@35 290 // Profiler profiler("TextLayer::paint", true);
Chris@35 291
Chris@35 292 int x0 = rect.left(), x1 = rect.right();
Chris@1437 293 int overlap = ViewManager::scalePixelSize(150);
Chris@1437 294 sv_frame_t frame0 = v->getFrameForX(x0 - overlap);
Chris@1437 295 sv_frame_t frame1 = v->getFrameForX(x1 + overlap);
Chris@35 296
Chris@1437 297 EventVector points(m_model->getEventsWithin(frame0, frame1 - frame0, 2));
Chris@35 298 if (points.empty()) return;
Chris@35 299
Chris@287 300 QColor brushColour(getBaseQColor());
Chris@35 301
Chris@44 302 int h, s, val;
Chris@44 303 brushColour.getHsv(&h, &s, &val);
Chris@36 304 brushColour.setHsv(h, s, 255, 100);
Chris@36 305
Chris@36 306 QColor penColour;
Chris@287 307 penColour = v->getForeground();
Chris@35 308
Chris@587 309 // SVDEBUG << "TextLayer::paint: resolution is "
Chris@1266 310 // << m_model->getResolution() << " frames" << endl;
Chris@35 311
Chris@35 312 QPoint localPos;
Chris@1437 313 Event illuminatePoint(0);
Chris@850 314 bool shouldIlluminate = false;
Chris@35 315
Chris@44 316 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@552 317 shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(),
Chris@552 318 illuminatePoint);
Chris@35 319 }
Chris@35 320
Chris@35 321 int boxMaxWidth = 150;
Chris@35 322 int boxMaxHeight = 200;
Chris@35 323
Chris@35 324 paint.save();
Chris@918 325 paint.setClipRect(rect.x(), 0, rect.width() + boxMaxWidth, v->getPaintHeight());
Chris@35 326
Chris@1437 327 for (EventVector::const_iterator i = points.begin();
Chris@1266 328 i != points.end(); ++i) {
Chris@35 329
Chris@1437 330 Event p(*i);
Chris@35 331
Chris@1437 332 int x = v->getXForFrame(p.getFrame());
Chris@1437 333 int y = getYForHeight(v, p.getValue());
Chris@35 334
Chris@1437 335 if (!shouldIlluminate || illuminatePoint != p) {
Chris@1266 336 paint.setPen(penColour);
Chris@1266 337 paint.setBrush(brushColour);
Chris@552 338 } else {
Chris@1266 339 paint.setBrush(penColour);
Chris@287 340 paint.setPen(v->getBackground());
Chris@1266 341 }
Chris@35 342
Chris@1437 343 QString label = p.getLabel();
Chris@1266 344 if (label == "") {
Chris@1266 345 label = tr("<no text>");
Chris@1266 346 }
Chris@35 347
Chris@1266 348 QRect boxRect = paint.fontMetrics().boundingRect
Chris@1266 349 (QRect(0, 0, boxMaxWidth, boxMaxHeight),
Chris@1266 350 Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, label);
Chris@35 351
Chris@1266 352 QRect textRect = QRect(3, 2, boxRect.width(), boxRect.height());
Chris@1266 353 boxRect = QRect(0, 0, boxRect.width() + 6, boxRect.height() + 2);
Chris@35 354
Chris@1266 355 if (y + boxRect.height() > v->getPaintHeight()) {
Chris@1266 356 if (boxRect.height() > v->getPaintHeight()) y = 0;
Chris@1266 357 else y = v->getPaintHeight() - boxRect.height() - 1;
Chris@1266 358 }
Chris@35 359
Chris@1266 360 boxRect = QRect(x, y, boxRect.width(), boxRect.height());
Chris@1266 361 textRect = QRect(x + 3, y + 2, textRect.width(), textRect.height());
Chris@35 362
Chris@1266 363 // boxRect = QRect(x, y, boxRect.width(), boxRect.height());
Chris@1266 364 // textRect = QRect(x + 3, y + 2, textRect.width(), textRect.height());
Chris@35 365
Chris@1266 366 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@1266 367 paint.drawRect(boxRect);
Chris@35 368
Chris@1266 369 paint.setRenderHint(QPainter::Antialiasing, true);
Chris@1266 370 paint.drawText(textRect,
Chris@1266 371 Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap,
Chris@1266 372 label);
Chris@35 373
Chris@1437 374 /// if (p.getLabel() != "") {
Chris@1437 375 /// paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.getLabel());
Chris@1266 376 /// }
Chris@35 377 }
Chris@35 378
Chris@35 379 paint.restore();
Chris@35 380
Chris@35 381 // looks like save/restore doesn't deal with this:
Chris@35 382 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@35 383 }
Chris@35 384
Chris@35 385 void
Chris@918 386 TextLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@35 387 {
Chris@587 388 // SVDEBUG << "TextLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@35 389
Chris@35 390 if (!m_model) {
Chris@1266 391 SVDEBUG << "TextLayer::drawStart: no model" << endl;
Chris@1266 392 return;
Chris@35 393 }
Chris@35 394
Chris@908 395 sv_frame_t frame = v->getFrameForX(e->x());
Chris@35 396 if (frame < 0) frame = 0;
Chris@35 397 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@35 398
Chris@908 399 double height = getHeightForY(v, e->y());
Chris@35 400
Chris@1437 401 m_editingPoint = Event(frame, float(height), "");
Chris@35 402 m_originalPoint = m_editingPoint;
Chris@35 403
Chris@376 404 if (m_editingCommand) finish(m_editingCommand);
Chris@1437 405 m_editingCommand = new ChangeEventsCommand(m_model, "Add Label");
Chris@1437 406 m_editingCommand->add(m_editingPoint);
Chris@35 407
Chris@35 408 m_editing = true;
Chris@35 409 }
Chris@35 410
Chris@35 411 void
Chris@918 412 TextLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e)
Chris@35 413 {
Chris@587 414 // SVDEBUG << "TextLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl;
Chris@35 415
Chris@35 416 if (!m_model || !m_editing) return;
Chris@35 417
Chris@908 418 sv_frame_t frame = v->getFrameForX(e->x());
Chris@35 419 if (frame < 0) frame = 0;
Chris@35 420 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@35 421
Chris@908 422 double height = getHeightForY(v, e->y());
Chris@35 423
Chris@1437 424 m_editingCommand->remove(m_editingPoint);
Chris@1437 425 m_editingPoint = m_editingPoint
Chris@1437 426 .withFrame(frame)
Chris@1437 427 .withValue(float(height));
Chris@1437 428 m_editingCommand->add(m_editingPoint);
Chris@35 429 }
Chris@35 430
Chris@35 431 void
Chris@918 432 TextLayer::drawEnd(LayerGeometryProvider *v, QMouseEvent *)
Chris@35 433 {
Chris@587 434 // SVDEBUG << "TextLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl;
Chris@35 435 if (!m_model || !m_editing) return;
Chris@36 436
Chris@36 437 bool ok = false;
Chris@918 438 QString label = QInputDialog::getText(v->getView(), tr("Enter label"),
Chris@1266 439 tr("Please enter a new label:"),
Chris@1266 440 QLineEdit::Normal, "", &ok);
Chris@36 441
Chris@1437 442 m_editingCommand->remove(m_editingPoint);
Chris@1437 443
Chris@36 444 if (ok) {
Chris@1437 445 m_editingPoint = m_editingPoint
Chris@1437 446 .withLabel(label);
Chris@1437 447 m_editingCommand->add(m_editingPoint);
Chris@36 448 }
Chris@36 449
Chris@376 450 finish(m_editingCommand);
Chris@1408 451 m_editingCommand = nullptr;
Chris@35 452 m_editing = false;
Chris@35 453 }
Chris@35 454
Chris@35 455 void
Chris@918 456 TextLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@335 457 {
Chris@335 458 if (!m_model) return;
Chris@335 459
Chris@552 460 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
Chris@335 461
Chris@335 462 if (m_editingCommand) {
Chris@1266 463 finish(m_editingCommand);
Chris@1408 464 m_editingCommand = nullptr;
Chris@335 465 }
Chris@335 466
Chris@335 467 m_editing = true;
Chris@335 468 }
Chris@335 469
Chris@335 470 void
Chris@918 471 TextLayer::eraseDrag(LayerGeometryProvider *, QMouseEvent *)
Chris@335 472 {
Chris@335 473 }
Chris@335 474
Chris@335 475 void
Chris@918 476 TextLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e)
Chris@335 477 {
Chris@335 478 if (!m_model || !m_editing) return;
Chris@335 479
Chris@335 480 m_editing = false;
Chris@335 481
Chris@1437 482 Event p;
Chris@552 483 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
Chris@1437 484 if (p.getFrame() != m_editingPoint.getFrame() ||
Chris@1437 485 p.getValue() != m_editingPoint.getValue()) return;
Chris@335 486
Chris@1437 487 m_editingCommand = new ChangeEventsCommand(m_model, tr("Erase Point"));
Chris@1437 488 m_editingCommand->remove(m_editingPoint);
Chris@376 489 finish(m_editingCommand);
Chris@1408 490 m_editingCommand = nullptr;
Chris@335 491 m_editing = false;
Chris@335 492 }
Chris@335 493
Chris@335 494 void
Chris@918 495 TextLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e)
Chris@35 496 {
Chris@587 497 // SVDEBUG << "TextLayer::editStart(" << e->x() << "," << e->y() << ")" << endl;
Chris@35 498
Chris@35 499 if (!m_model) return;
Chris@35 500
Chris@552 501 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) {
Chris@552 502 return;
Chris@552 503 }
Chris@35 504
Chris@36 505 m_editOrigin = e->pos();
Chris@35 506 m_originalPoint = m_editingPoint;
Chris@35 507
Chris@35 508 if (m_editingCommand) {
Chris@1266 509 finish(m_editingCommand);
Chris@1408 510 m_editingCommand = nullptr;
Chris@35 511 }
Chris@35 512
Chris@35 513 m_editing = true;
Chris@35 514 }
Chris@35 515
Chris@35 516 void
Chris@918 517 TextLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e)
Chris@35 518 {
Chris@35 519 if (!m_model || !m_editing) return;
Chris@35 520
Chris@1437 521 sv_frame_t frameDiff =
Chris@1437 522 v->getFrameForX(e->x()) - v->getFrameForX(m_editOrigin.x());
Chris@1437 523 double heightDiff =
Chris@1437 524 getHeightForY(v, e->y()) - getHeightForY(v, m_editOrigin.y());
Chris@36 525
Chris@1437 526 sv_frame_t frame = m_originalPoint.getFrame() + frameDiff;
Chris@1437 527 double height = m_originalPoint.getValue() + heightDiff;
Chris@36 528
Chris@35 529 if (frame < 0) frame = 0;
Chris@36 530 frame = (frame / m_model->getResolution()) * m_model->getResolution();
Chris@35 531
Chris@35 532 if (!m_editingCommand) {
Chris@1437 533 m_editingCommand = new ChangeEventsCommand(m_model, tr("Drag Label"));
Chris@35 534 }
Chris@35 535
Chris@1437 536 m_editingCommand->remove(m_editingPoint);
Chris@1437 537 m_editingPoint = m_editingPoint
Chris@1437 538 .withFrame(frame)
Chris@1437 539 .withValue(float(height));
Chris@1437 540 m_editingCommand->add(m_editingPoint);
Chris@35 541 }
Chris@35 542
Chris@35 543 void
Chris@918 544 TextLayer::editEnd(LayerGeometryProvider *, QMouseEvent *)
Chris@35 545 {
Chris@587 546 // SVDEBUG << "TextLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl;
Chris@35 547 if (!m_model || !m_editing) return;
Chris@35 548
Chris@35 549 if (m_editingCommand) {
Chris@35 550
Chris@1266 551 QString newName = m_editingCommand->getName();
Chris@35 552
Chris@1437 553 if (m_editingPoint.getFrame() != m_originalPoint.getFrame()) {
Chris@1437 554 if (m_editingPoint.getValue() != m_originalPoint.getValue()) {
Chris@1266 555 newName = tr("Move Label");
Chris@1266 556 } else {
Chris@1266 557 newName = tr("Move Label Horizontally");
Chris@1266 558 }
Chris@1266 559 } else {
Chris@1266 560 newName = tr("Move Label Vertically");
Chris@1266 561 }
Chris@35 562
Chris@1266 563 m_editingCommand->setName(newName);
Chris@1266 564 finish(m_editingCommand);
Chris@35 565 }
Chris@35 566
Chris@1408 567 m_editingCommand = nullptr;
Chris@35 568 m_editing = false;
Chris@35 569 }
Chris@35 570
Chris@255 571 bool
Chris@918 572 TextLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e)
Chris@36 573 {
Chris@255 574 if (!m_model) return false;
Chris@36 575
Chris@1437 576 Event text;
Chris@552 577 if (!getPointToDrag(v, e->x(), e->y(), text)) return false;
Chris@36 578
Chris@1437 579 QString label = text.getLabel();
Chris@36 580
Chris@36 581 bool ok = false;
Chris@918 582 label = QInputDialog::getText(v->getView(), tr("Enter label"),
Chris@1266 583 tr("Please enter a new label:"),
Chris@1266 584 QLineEdit::Normal, label, &ok);
Chris@1437 585 if (ok && label != text.getLabel()) {
Chris@1437 586 ChangeEventsCommand *command =
Chris@1437 587 new ChangeEventsCommand(m_model, tr("Re-Label Point"));
Chris@1437 588 command->remove(text);
Chris@1437 589 command->add(text.withLabel(label));
Chris@1437 590 finish(command);
Chris@36 591 }
Chris@255 592
Chris@255 593 return true;
Chris@36 594 }
Chris@36 595
Chris@43 596 void
Chris@908 597 TextLayer::moveSelection(Selection s, sv_frame_t newStartFrame)
Chris@43 598 {
Chris@99 599 if (!m_model) return;
Chris@99 600
Chris@1437 601 ChangeEventsCommand *command =
Chris@1437 602 new ChangeEventsCommand(m_model, tr("Drag Selection"));
Chris@43 603
Chris@1437 604 EventVector points =
Chris@1437 605 m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@43 606
Chris@1437 607 for (Event p: points) {
Chris@1437 608 command->remove(p);
Chris@1437 609 Event moved = p.withFrame(p.getFrame() +
Chris@1437 610 newStartFrame - s.getStartFrame());
Chris@1437 611 command->add(moved);
Chris@43 612 }
Chris@43 613
Chris@376 614 finish(command);
Chris@43 615 }
Chris@43 616
Chris@43 617 void
Chris@43 618 TextLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 619 {
Chris@99 620 if (!m_model) return;
Chris@99 621
Chris@1437 622 ChangeEventsCommand *command =
Chris@1437 623 new ChangeEventsCommand(m_model, tr("Resize Selection"));
Chris@43 624
Chris@1437 625 EventVector points =
Chris@1437 626 m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@43 627
Chris@1437 628 double ratio = double(newSize.getDuration()) / double(s.getDuration());
Chris@1437 629 double oldStart = double(s.getStartFrame());
Chris@1437 630 double newStart = double(newSize.getStartFrame());
Chris@1437 631
Chris@1437 632 for (Event p: points) {
Chris@43 633
Chris@1437 634 double newFrame = (double(p.getFrame()) - oldStart) * ratio + newStart;
Chris@43 635
Chris@1437 636 Event newPoint = p
Chris@1437 637 .withFrame(lrint(newFrame));
Chris@1437 638 command->remove(p);
Chris@1437 639 command->add(newPoint);
Chris@43 640 }
Chris@43 641
Chris@376 642 finish(command);
Chris@43 643 }
Chris@43 644
Chris@76 645 void
Chris@76 646 TextLayer::deleteSelection(Selection s)
Chris@76 647 {
Chris@99 648 if (!m_model) return;
Chris@99 649
Chris@1437 650 ChangeEventsCommand *command =
Chris@1437 651 new ChangeEventsCommand(m_model, tr("Delete Selection"));
Chris@76 652
Chris@1437 653 EventVector points =
Chris@1437 654 m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@76 655
Chris@1437 656 for (Event p: points) {
Chris@1437 657 command->remove(p);
Chris@76 658 }
Chris@76 659
Chris@376 660 finish(command);
Chris@76 661 }
Chris@76 662
Chris@76 663 void
Chris@918 664 TextLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to)
Chris@76 665 {
Chris@99 666 if (!m_model) return;
Chris@99 667
Chris@1437 668 EventVector points =
Chris@1437 669 m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
Chris@76 670
Chris@1437 671 for (Event p: points) {
Chris@1437 672 to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame())));
Chris@76 673 }
Chris@76 674 }
Chris@76 675
Chris@125 676 bool
Chris@918 677 TextLayer::paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t /* frameOffset */, bool /* interactive */)
Chris@76 678 {
Chris@125 679 if (!m_model) return false;
Chris@99 680
Chris@1423 681 const EventVector &points = from.getPoints();
Chris@76 682
Chris@360 683 bool realign = false;
Chris@360 684
Chris@360 685 if (clipboardHasDifferentAlignment(v, from)) {
Chris@360 686
Chris@360 687 QMessageBox::StandardButton button =
Chris@918 688 QMessageBox::question(v->getView(), tr("Re-align pasted items?"),
Chris@360 689 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@360 690 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@360 691 QMessageBox::Yes);
Chris@360 692
Chris@360 693 if (button == QMessageBox::Cancel) {
Chris@360 694 return false;
Chris@360 695 }
Chris@360 696
Chris@360 697 if (button == QMessageBox::Yes) {
Chris@360 698 realign = true;
Chris@360 699 }
Chris@360 700 }
Chris@360 701
Chris@1437 702 ChangeEventsCommand *command =
Chris@1437 703 new ChangeEventsCommand(m_model, tr("Paste"));
Chris@76 704
Chris@908 705 double valueMin = 0.0, valueMax = 1.0;
Chris@1423 706 for (EventVector::const_iterator i = points.begin();
Chris@125 707 i != points.end(); ++i) {
Chris@1423 708 if (i->hasValue()) {
Chris@125 709 if (i->getValue() < valueMin) valueMin = i->getValue();
Chris@125 710 if (i->getValue() > valueMax) valueMax = i->getValue();
Chris@125 711 }
Chris@125 712 }
Chris@125 713 if (valueMax < valueMin + 1.0) valueMax = valueMin + 1.0;
Chris@125 714
Chris@1423 715 for (EventVector::const_iterator i = points.begin();
Chris@76 716 i != points.end(); ++i) {
Chris@76 717
Chris@908 718 sv_frame_t frame = 0;
Chris@360 719
Chris@360 720 if (!realign) {
Chris@360 721
Chris@360 722 frame = i->getFrame();
Chris@360 723
Chris@360 724 } else {
Chris@360 725
Chris@1423 726 if (i->hasReferenceFrame()) {
Chris@360 727 frame = i->getReferenceFrame();
Chris@360 728 frame = alignFromReference(v, frame);
Chris@360 729 } else {
Chris@360 730 frame = i->getFrame();
Chris@360 731 }
Chris@76 732 }
Chris@360 733
Chris@1437 734 Event p = *i;
Chris@1437 735 Event newPoint = p;
Chris@1437 736 if (p.hasValue()) {
Chris@1437 737 newPoint = newPoint.withValue(float((i->getValue() - valueMin) /
Chris@1437 738 (valueMax - valueMin)));
Chris@125 739 } else {
Chris@1437 740 newPoint = newPoint.withValue(0.5f);
Chris@125 741 }
Chris@125 742
Chris@1437 743 if (!p.hasLabel()) {
Chris@1437 744 if (p.hasValue()) {
Chris@1437 745 newPoint = newPoint.withLabel(QString("%1").arg(p.getValue()));
Chris@1437 746 } else {
Chris@1437 747 newPoint = newPoint.withLabel(tr("New Point"));
Chris@1437 748 }
Chris@125 749 }
Chris@76 750
Chris@1437 751 command->add(newPoint);
Chris@76 752 }
Chris@76 753
Chris@376 754 finish(command);
Chris@125 755 return true;
Chris@76 756 }
Chris@76 757
Chris@287 758 int
Chris@287 759 TextLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 760 {
Chris@287 761 impose = false;
Chris@287 762 return ColourDatabase::getInstance()->getColourIndex
Chris@287 763 (QString(darkbg ? "Bright Orange" : "Orange"));
Chris@287 764 }
Chris@287 765
Chris@316 766 void
Chris@316 767 TextLayer::toXml(QTextStream &stream,
Chris@316 768 QString indent, QString extraAttributes) const
Chris@35 769 {
Chris@316 770 SingleColourLayer::toXml(stream, indent, extraAttributes);
Chris@35 771 }
Chris@35 772
Chris@35 773 void
Chris@35 774 TextLayer::setProperties(const QXmlAttributes &attributes)
Chris@35 775 {
Chris@287 776 SingleColourLayer::setProperties(attributes);
Chris@35 777 }
Chris@35 778