annotate layer/TextLayer.cpp @ 1473:886c1cd48f9d by-id

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