annotate layer/TextLayer.cpp @ 242:50c0a548d23e sv1-1.0pre4

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