annotate layer/TextLayer.cpp @ 675:3437e0fad7ae imaf_enc

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