annotate layer/TimeValueLayer.cpp @ 66:e9eac9368e29

* basics of selectable vertical scale in time value layer * clear selection when closing session
author Chris Cannam
date Mon, 27 Mar 2006 16:44:12 +0000
parents 705f05ab42e3
children c4fff27cd651
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 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@0 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@0 14 */
Chris@0 15
Chris@0 16 #include "TimeValueLayer.h"
Chris@0 17
Chris@0 18 #include "base/Model.h"
Chris@0 19 #include "base/RealTime.h"
Chris@0 20 #include "base/Profiler.h"
Chris@0 21 #include "base/View.h"
Chris@0 22
Chris@0 23 #include "model/SparseTimeValueModel.h"
Chris@0 24
Chris@66 25 #include "SpectrogramLayer.h" // for optional frequency alignment
Chris@66 26
Chris@0 27 #include <QPainter>
Chris@6 28 #include <QPainterPath>
Chris@21 29 #include <QMouseEvent>
Chris@0 30
Chris@0 31 #include <iostream>
Chris@0 32 #include <cmath>
Chris@0 33
Chris@44 34 TimeValueLayer::TimeValueLayer() :
Chris@44 35 Layer(),
Chris@0 36 m_model(0),
Chris@21 37 m_editing(false),
Chris@23 38 m_originalPoint(0, 0.0, tr("New Point")),
Chris@21 39 m_editingPoint(0, 0.0, tr("New Point")),
Chris@22 40 m_editingCommand(0),
Chris@0 41 m_colour(Qt::black),
Chris@66 42 m_plotStyle(PlotConnectedPoints),
Chris@66 43 m_verticalScale(LinearScale)
Chris@0 44 {
Chris@44 45
Chris@0 46 }
Chris@0 47
Chris@0 48 void
Chris@0 49 TimeValueLayer::setModel(SparseTimeValueModel *model)
Chris@0 50 {
Chris@0 51 if (m_model == model) return;
Chris@0 52 m_model = model;
Chris@0 53
Chris@0 54 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@0 55 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 56 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@0 57
Chris@0 58 connect(m_model, SIGNAL(completionChanged()),
Chris@0 59 this, SIGNAL(modelCompletionChanged()));
Chris@0 60
Chris@0 61 std::cerr << "TimeValueLayer::setModel(" << model << ")" << std::endl;
Chris@0 62
Chris@0 63 emit modelReplaced();
Chris@0 64 }
Chris@0 65
Chris@0 66 Layer::PropertyList
Chris@0 67 TimeValueLayer::getProperties() const
Chris@0 68 {
Chris@0 69 PropertyList list;
Chris@0 70 list.push_back(tr("Colour"));
Chris@0 71 list.push_back(tr("Plot Type"));
Chris@66 72 list.push_back(tr("Vertical Scale"));
Chris@0 73 return list;
Chris@0 74 }
Chris@0 75
Chris@0 76 Layer::PropertyType
Chris@0 77 TimeValueLayer::getPropertyType(const PropertyName &name) const
Chris@0 78 {
Chris@0 79 return ValueProperty;
Chris@0 80 }
Chris@0 81
Chris@0 82 int
Chris@0 83 TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@0 84 int *min, int *max) const
Chris@0 85 {
Chris@0 86 //!!! factor this colour handling stuff out into a colour manager class
Chris@0 87
Chris@0 88 int deft = 0;
Chris@0 89
Chris@0 90 if (name == tr("Colour")) {
Chris@0 91
Chris@10 92 if (min) *min = 0;
Chris@10 93 if (max) *max = 5;
Chris@0 94
Chris@0 95 if (m_colour == Qt::black) deft = 0;
Chris@0 96 else if (m_colour == Qt::darkRed) deft = 1;
Chris@0 97 else if (m_colour == Qt::darkBlue) deft = 2;
Chris@0 98 else if (m_colour == Qt::darkGreen) deft = 3;
Chris@0 99 else if (m_colour == QColor(200, 50, 255)) deft = 4;
Chris@0 100 else if (m_colour == QColor(255, 150, 50)) deft = 5;
Chris@0 101
Chris@0 102 } else if (name == tr("Plot Type")) {
Chris@0 103
Chris@10 104 if (min) *min = 0;
Chris@26 105 if (max) *max = 5;
Chris@0 106
Chris@0 107 deft = int(m_plotStyle);
Chris@0 108
Chris@66 109 } else if (name == tr("Vertical Scale")) {
Chris@66 110
Chris@66 111 if (min) *min = 0;
Chris@66 112 if (max) *max = 3;
Chris@66 113
Chris@66 114 deft = int(m_verticalScale);
Chris@66 115
Chris@0 116 } else {
Chris@0 117
Chris@0 118 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@0 119 }
Chris@0 120
Chris@0 121 return deft;
Chris@0 122 }
Chris@0 123
Chris@0 124 QString
Chris@0 125 TimeValueLayer::getPropertyValueLabel(const PropertyName &name,
Chris@0 126 int value) const
Chris@0 127 {
Chris@0 128 if (name == tr("Colour")) {
Chris@0 129 switch (value) {
Chris@0 130 default:
Chris@0 131 case 0: return tr("Black");
Chris@0 132 case 1: return tr("Red");
Chris@0 133 case 2: return tr("Blue");
Chris@0 134 case 3: return tr("Green");
Chris@0 135 case 4: return tr("Purple");
Chris@0 136 case 5: return tr("Orange");
Chris@0 137 }
Chris@0 138 } else if (name == tr("Plot Type")) {
Chris@0 139 switch (value) {
Chris@0 140 default:
Chris@0 141 case 0: return tr("Points");
Chris@0 142 case 1: return tr("Stems");
Chris@6 143 case 2: return tr("Connected Points");
Chris@6 144 case 3: return tr("Lines");
Chris@6 145 case 4: return tr("Curve");
Chris@26 146 case 5: return tr("Segmentation");
Chris@0 147 }
Chris@66 148 } else if (name == tr("Vertical Scale")) {
Chris@66 149 switch (value) {
Chris@66 150 default:
Chris@66 151 case 0: return tr("Linear Scale");
Chris@66 152 case 1: return tr("Log Scale");
Chris@66 153 case 2: return tr("+/-1 Scale");
Chris@66 154 case 3: return tr("Frequency Scale");
Chris@66 155 }
Chris@0 156 }
Chris@0 157 return tr("<unknown>");
Chris@0 158 }
Chris@0 159
Chris@0 160 void
Chris@0 161 TimeValueLayer::setProperty(const PropertyName &name, int value)
Chris@0 162 {
Chris@0 163 if (name == tr("Colour")) {
Chris@0 164 switch (value) {
Chris@0 165 default:
Chris@0 166 case 0: setBaseColour(Qt::black); break;
Chris@0 167 case 1: setBaseColour(Qt::darkRed); break;
Chris@0 168 case 2: setBaseColour(Qt::darkBlue); break;
Chris@0 169 case 3: setBaseColour(Qt::darkGreen); break;
Chris@0 170 case 4: setBaseColour(QColor(200, 50, 255)); break;
Chris@0 171 case 5: setBaseColour(QColor(255, 150, 50)); break;
Chris@0 172 }
Chris@0 173 } else if (name == tr("Plot Type")) {
Chris@0 174 setPlotStyle(PlotStyle(value));
Chris@66 175 } else if (name == tr("Vertical Scale")) {
Chris@66 176 setVerticalScale(VerticalScale(value));
Chris@0 177 }
Chris@0 178 }
Chris@0 179
Chris@0 180 void
Chris@0 181 TimeValueLayer::setBaseColour(QColor colour)
Chris@0 182 {
Chris@0 183 if (m_colour == colour) return;
Chris@0 184 m_colour = colour;
Chris@0 185 emit layerParametersChanged();
Chris@0 186 }
Chris@0 187
Chris@0 188 void
Chris@0 189 TimeValueLayer::setPlotStyle(PlotStyle style)
Chris@0 190 {
Chris@0 191 if (m_plotStyle == style) return;
Chris@0 192 m_plotStyle = style;
Chris@0 193 emit layerParametersChanged();
Chris@0 194 }
Chris@0 195
Chris@66 196 void
Chris@66 197 TimeValueLayer::setVerticalScale(VerticalScale scale)
Chris@66 198 {
Chris@66 199 if (m_verticalScale == scale) return;
Chris@66 200 m_verticalScale = scale;
Chris@66 201 emit layerParametersChanged();
Chris@66 202 }
Chris@66 203
Chris@0 204 bool
Chris@44 205 TimeValueLayer::isLayerScrollable(const View *v) const
Chris@0 206 {
Chris@6 207 // We don't illuminate sections in the line or curve modes, so
Chris@6 208 // they're always scrollable
Chris@6 209
Chris@6 210 if (m_plotStyle == PlotLines ||
Chris@6 211 m_plotStyle == PlotCurve) return true;
Chris@6 212
Chris@0 213 QPoint discard;
Chris@44 214 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@0 215 }
Chris@0 216
Chris@0 217 SparseTimeValueModel::PointList
Chris@44 218 TimeValueLayer::getLocalPoints(View *v, int x) const
Chris@0 219 {
Chris@0 220 if (!m_model) return SparseTimeValueModel::PointList();
Chris@0 221
Chris@44 222 long frame = v->getFrameForX(x);
Chris@0 223
Chris@0 224 SparseTimeValueModel::PointList onPoints =
Chris@0 225 m_model->getPoints(frame);
Chris@0 226
Chris@0 227 if (!onPoints.empty()) {
Chris@0 228 return onPoints;
Chris@0 229 }
Chris@0 230
Chris@0 231 SparseTimeValueModel::PointList prevPoints =
Chris@0 232 m_model->getPreviousPoints(frame);
Chris@0 233 SparseTimeValueModel::PointList nextPoints =
Chris@0 234 m_model->getNextPoints(frame);
Chris@0 235
Chris@0 236 SparseTimeValueModel::PointList usePoints = prevPoints;
Chris@0 237
Chris@0 238 if (prevPoints.empty()) {
Chris@0 239 usePoints = nextPoints;
Chris@44 240 } else if (prevPoints.begin()->frame < v->getStartFrame() &&
Chris@44 241 !(nextPoints.begin()->frame > v->getEndFrame())) {
Chris@0 242 usePoints = nextPoints;
Chris@0 243 } else if (nextPoints.begin()->frame - frame <
Chris@0 244 frame - prevPoints.begin()->frame) {
Chris@0 245 usePoints = nextPoints;
Chris@0 246 }
Chris@0 247
Chris@28 248 if (!usePoints.empty()) {
Chris@28 249 int fuzz = 2;
Chris@44 250 int px = v->getXForFrame(usePoints.begin()->frame);
Chris@28 251 if ((px > x && px - x > fuzz) ||
Chris@28 252 (px < x && x - px > fuzz + 1)) {
Chris@28 253 usePoints.clear();
Chris@28 254 }
Chris@28 255 }
Chris@28 256
Chris@0 257 return usePoints;
Chris@0 258 }
Chris@0 259
Chris@25 260 QString
Chris@44 261 TimeValueLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@0 262 {
Chris@25 263 int x = pos.x();
Chris@0 264
Chris@25 265 if (!m_model || !m_model->getSampleRate()) return "";
Chris@0 266
Chris@44 267 SparseTimeValueModel::PointList points = getLocalPoints(v, x);
Chris@0 268
Chris@0 269 if (points.empty()) {
Chris@0 270 if (!m_model->isReady()) {
Chris@25 271 return tr("In progress");
Chris@25 272 } else {
Chris@25 273 return tr("No local points");
Chris@0 274 }
Chris@0 275 }
Chris@0 276
Chris@0 277 long useFrame = points.begin()->frame;
Chris@0 278
Chris@0 279 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
Chris@25 280
Chris@25 281 QString text;
Chris@0 282
Chris@25 283 if (points.begin()->label == "") {
Chris@25 284 text = QString(tr("Time:\t%1\nValue:\t%2\nNo label"))
Chris@25 285 .arg(rt.toText(true).c_str())
Chris@25 286 .arg(points.begin()->value);
Chris@25 287 } else {
Chris@25 288 text = QString(tr("Time:\t%1\nValue:\t%2\nLabel:\t%3"))
Chris@25 289 .arg(rt.toText(true).c_str())
Chris@25 290 .arg(points.begin()->value)
Chris@25 291 .arg(points.begin()->label);
Chris@25 292 }
Chris@0 293
Chris@44 294 pos = QPoint(v->getXForFrame(useFrame),
Chris@44 295 getYForValue(v, points.begin()->value));
Chris@25 296 return text;
Chris@0 297 }
Chris@0 298
Chris@28 299 bool
Chris@44 300 TimeValueLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 301 size_t &resolution,
Chris@28 302 SnapType snap) const
Chris@13 303 {
Chris@13 304 if (!m_model) {
Chris@44 305 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@13 306 }
Chris@13 307
Chris@13 308 resolution = m_model->getResolution();
Chris@28 309 SparseTimeValueModel::PointList points;
Chris@13 310
Chris@28 311 if (snap == SnapNeighbouring) {
Chris@28 312
Chris@44 313 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@28 314 if (points.empty()) return false;
Chris@28 315 frame = points.begin()->frame;
Chris@28 316 return true;
Chris@28 317 }
Chris@28 318
Chris@28 319 points = m_model->getPoints(frame, frame);
Chris@28 320 int snapped = frame;
Chris@28 321 bool found = false;
Chris@13 322
Chris@13 323 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@13 324 i != points.end(); ++i) {
Chris@13 325
Chris@28 326 if (snap == SnapRight) {
Chris@28 327
Chris@13 328 if (i->frame > frame) {
Chris@28 329 snapped = i->frame;
Chris@28 330 found = true;
Chris@13 331 break;
Chris@13 332 }
Chris@28 333
Chris@28 334 } else if (snap == SnapLeft) {
Chris@28 335
Chris@13 336 if (i->frame <= frame) {
Chris@28 337 snapped = i->frame;
Chris@28 338 found = true; // don't break, as the next may be better
Chris@28 339 } else {
Chris@28 340 break;
Chris@28 341 }
Chris@28 342
Chris@28 343 } else { // nearest
Chris@28 344
Chris@28 345 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@28 346 ++j;
Chris@28 347
Chris@28 348 if (j == points.end()) {
Chris@28 349
Chris@28 350 snapped = i->frame;
Chris@28 351 found = true;
Chris@28 352 break;
Chris@28 353
Chris@28 354 } else if (j->frame >= frame) {
Chris@28 355
Chris@28 356 if (j->frame - frame < frame - i->frame) {
Chris@28 357 snapped = j->frame;
Chris@28 358 } else {
Chris@28 359 snapped = i->frame;
Chris@28 360 }
Chris@28 361 found = true;
Chris@28 362 break;
Chris@13 363 }
Chris@13 364 }
Chris@13 365 }
Chris@13 366
Chris@28 367 frame = snapped;
Chris@28 368 return found;
Chris@13 369 }
Chris@13 370
Chris@21 371 int
Chris@66 372 TimeValueLayer::getYForValue(View *v, float val) const
Chris@21 373 {
Chris@21 374 float min = m_model->getValueMinimum();
Chris@21 375 float max = m_model->getValueMaximum();
Chris@21 376 if (max == min) max = min + 1.0;
Chris@21 377
Chris@44 378 int h = v->height();
Chris@21 379
Chris@66 380 if (m_verticalScale == FrequencyScale || m_verticalScale == LogScale) {
Chris@66 381
Chris@66 382 if (m_verticalScale == FrequencyScale) {
Chris@66 383 // If we have a spectrogram layer on the same view as us, align
Chris@66 384 // ourselves with it...
Chris@66 385 for (int i = 0; i < v->getLayerCount(); ++i) {
Chris@66 386 SpectrogramLayer *spectrogram = dynamic_cast<SpectrogramLayer *>
Chris@66 387 (v->getLayer(i));
Chris@66 388 if (spectrogram) {
Chris@66 389 return spectrogram->getYForFrequency(v, val);
Chris@66 390 }
Chris@66 391 }
Chris@66 392 }
Chris@66 393
Chris@66 394 min = (min < 0.0) ? -log10(-min) : (min == 0.0) ? 0.0 : log10(min);
Chris@66 395 max = (max < 0.0) ? -log10(-max) : (max == 0.0) ? 0.0 : log10(max);
Chris@66 396 val = (val < 0.0) ? -log10(-val) : (val == 0.0) ? 0.0 : log10(val);
Chris@66 397
Chris@66 398 } else if (m_verticalScale == PlusMinusOneScale) {
Chris@66 399 min = -1.0;
Chris@66 400 max = 1.0;
Chris@66 401 }
Chris@66 402
Chris@66 403 return int(h - ((val - min) * h) / (max - min));
Chris@21 404 }
Chris@21 405
Chris@21 406 float
Chris@44 407 TimeValueLayer::getValueForY(View *v, int y) const
Chris@21 408 {
Chris@66 409 //!!!
Chris@66 410
Chris@21 411 float min = m_model->getValueMinimum();
Chris@21 412 float max = m_model->getValueMaximum();
Chris@21 413 if (max == min) max = min + 1.0;
Chris@21 414
Chris@44 415 int h = v->height();
Chris@21 416
Chris@21 417 return min + (float(h - y) * float(max - min)) / h;
Chris@21 418 }
Chris@21 419
Chris@0 420 void
Chris@44 421 TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 422 {
Chris@0 423 if (!m_model || !m_model->isOK()) return;
Chris@0 424
Chris@0 425 int sampleRate = m_model->getSampleRate();
Chris@0 426 if (!sampleRate) return;
Chris@0 427
Chris@0 428 // Profiler profiler("TimeValueLayer::paint", true);
Chris@0 429
Chris@0 430 int x0 = rect.left(), x1 = rect.right();
Chris@44 431 long frame0 = v->getFrameForX(x0);
Chris@44 432 long frame1 = v->getFrameForX(x1);
Chris@0 433
Chris@0 434 SparseTimeValueModel::PointList points(m_model->getPoints
Chris@0 435 (frame0, frame1));
Chris@11 436 if (points.empty()) return;
Chris@0 437
Chris@0 438 paint.setPen(m_colour);
Chris@0 439
Chris@0 440 QColor brushColour(m_colour);
Chris@0 441 brushColour.setAlpha(80);
Chris@0 442 paint.setBrush(brushColour);
Chris@0 443
Chris@0 444 // std::cerr << "TimeValueLayer::paint: resolution is "
Chris@0 445 // << m_model->getResolution() << " frames" << std::endl;
Chris@0 446
Chris@0 447 float min = m_model->getValueMinimum();
Chris@0 448 float max = m_model->getValueMaximum();
Chris@0 449 if (max == min) max = min + 1.0;
Chris@0 450
Chris@44 451 int origin = int(nearbyint(v->height() -
Chris@44 452 (-min * v->height()) / (max - min)));
Chris@0 453
Chris@0 454 QPoint localPos;
Chris@0 455 long illuminateFrame = -1;
Chris@0 456
Chris@44 457 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@0 458 SparseTimeValueModel::PointList localPoints =
Chris@44 459 getLocalPoints(v, localPos.x());
Chris@0 460 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@0 461 }
Chris@6 462
Chris@20 463 int w =
Chris@44 464 v->getXForFrame(frame0 + m_model->getResolution()) -
Chris@44 465 v->getXForFrame(frame0);
Chris@7 466
Chris@6 467 paint.save();
Chris@6 468
Chris@6 469 QPainterPath path;
Chris@55 470 int pointCount = 0;
Chris@6 471
Chris@0 472 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@0 473 i != points.end(); ++i) {
Chris@0 474
Chris@0 475 const SparseTimeValueModel::Point &p(*i);
Chris@0 476
Chris@44 477 int x = v->getXForFrame(p.frame);
Chris@44 478 int y = getYForValue(v, p.value);
Chris@0 479
Chris@34 480 bool haveNext = false;
Chris@44 481 int nx = v->getXForFrame(m_model->getEndFrame());
Chris@34 482 int ny = y;
Chris@34 483
Chris@34 484 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@34 485 ++j;
Chris@34 486
Chris@34 487 if (j != points.end()) {
Chris@34 488 const SparseTimeValueModel::Point &q(*j);
Chris@44 489 nx = v->getXForFrame(q.frame);
Chris@44 490 ny = getYForValue(v, q.value);
Chris@34 491 haveNext = true;
Chris@34 492 }
Chris@34 493
Chris@34 494 int labelY = y;
Chris@34 495
Chris@0 496 if (w < 1) w = 1;
Chris@26 497 paint.setPen(m_colour);
Chris@0 498
Chris@26 499 if (m_plotStyle == PlotSegmentation) {
Chris@26 500 int value = ((p.value - min) / (max - min)) * 255.999;
Chris@26 501 QColor colour = QColor::fromHsv(256 - value, value / 2 + 128, value);
Chris@26 502 paint.setBrush(QColor(colour.red(), colour.green(), colour.blue(),
Chris@26 503 120));
Chris@44 504 labelY = v->height();
Chris@26 505 } else if (m_plotStyle == PlotLines ||
Chris@26 506 m_plotStyle == PlotCurve) {
Chris@6 507 paint.setBrush(Qt::NoBrush);
Chris@3 508 } else {
Chris@6 509 paint.setBrush(brushColour);
Chris@3 510 }
Chris@0 511
Chris@0 512 if (m_plotStyle == PlotStems) {
Chris@0 513 paint.setPen(brushColour);
Chris@0 514 if (y < origin - 1) {
Chris@0 515 paint.drawRect(x + w/2, y + 1, 1, origin - y);
Chris@0 516 } else if (y > origin + 1) {
Chris@0 517 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
Chris@0 518 }
Chris@0 519 paint.setPen(m_colour);
Chris@0 520 }
Chris@0 521
Chris@0 522 if (illuminateFrame == p.frame) {
Chris@6 523
Chris@0 524 //!!! aside from the problem of choosing a colour, it'd be
Chris@0 525 //better to save the highlighted rects and draw them at
Chris@0 526 //the end perhaps
Chris@6 527
Chris@6 528 //!!! not equipped to illuminate the right section in line
Chris@6 529 //or curve mode
Chris@6 530
Chris@6 531 if (m_plotStyle != PlotCurve &&
Chris@6 532 m_plotStyle != PlotLines) {
Chris@6 533 paint.setPen(Qt::black);//!!!
Chris@26 534 if (m_plotStyle != PlotSegmentation) {
Chris@26 535 paint.setBrush(Qt::black);//!!!
Chris@26 536 }
Chris@6 537 }
Chris@0 538 }
Chris@0 539
Chris@6 540 if (m_plotStyle != PlotLines &&
Chris@26 541 m_plotStyle != PlotCurve &&
Chris@26 542 m_plotStyle != PlotSegmentation) {
Chris@3 543 paint.drawRect(x, y - 1, w, 2);
Chris@3 544 }
Chris@0 545
Chris@6 546 if (m_plotStyle == PlotConnectedPoints ||
Chris@6 547 m_plotStyle == PlotLines ||
Chris@6 548 m_plotStyle == PlotCurve) {
Chris@0 549
Chris@34 550 if (haveNext) {
Chris@3 551
Chris@6 552 if (m_plotStyle == PlotConnectedPoints) {
Chris@34 553
Chris@3 554 paint.setPen(brushColour);
Chris@3 555 paint.drawLine(x + w, y, nx, ny);
Chris@6 556
Chris@6 557 } else if (m_plotStyle == PlotLines) {
Chris@6 558
Chris@6 559 paint.drawLine(x + w/2, y, nx + w/2, ny);
Chris@6 560
Chris@3 561 } else {
Chris@6 562
Chris@55 563 float x0 = x + float(w)/2;
Chris@55 564 float x1 = nx + float(w)/2;
Chris@55 565
Chris@55 566 float y0 = y;
Chris@55 567 float y1 = ny;
Chris@55 568
Chris@55 569 if (pointCount == 0) {
Chris@55 570 path.moveTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 571 }
Chris@55 572 ++pointCount;
Chris@6 573
Chris@6 574 if (nx - x > 5) {
Chris@55 575 path.cubicTo(x0, y0,
Chris@55 576 x0, y0,
Chris@55 577 (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 578
Chris@55 579 // // or
Chris@55 580 // path.quadTo(x0, y0, (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 581
Chris@6 582 } else {
Chris@55 583 path.lineTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 584 }
Chris@3 585 }
Chris@0 586 }
Chris@0 587 }
Chris@0 588
Chris@26 589 if (m_plotStyle == PlotSegmentation) {
Chris@26 590
Chris@27 591 if (nx <= x) continue;
Chris@26 592
Chris@28 593 if (illuminateFrame != p.frame &&
Chris@44 594 (nx < x + 5 || x >= v->width() - 1)) {
Chris@27 595 paint.setPen(Qt::NoPen);
Chris@27 596 }
Chris@26 597
Chris@44 598 paint.drawRect(x, -1, nx - x, v->height() + 1);
Chris@26 599 }
Chris@26 600
Chris@55 601 if (p.label != "") {
Chris@55 602 paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label);
Chris@55 603 }
Chris@0 604 }
Chris@6 605
Chris@6 606 if (m_plotStyle == PlotCurve && !path.isEmpty()) {
Chris@55 607 paint.setRenderHint(QPainter::Antialiasing, pointCount <= v->width());
Chris@6 608 paint.drawPath(path);
Chris@6 609 }
Chris@6 610
Chris@6 611 paint.restore();
Chris@6 612
Chris@6 613 // looks like save/restore doesn't deal with this:
Chris@6 614 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@6 615 }
Chris@6 616
Chris@42 617 int
Chris@44 618 TimeValueLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
Chris@42 619 {
Chris@55 620 if (m_plotStyle == PlotSegmentation) return 0;
Chris@55 621 return paint.fontMetrics().width("+0.000e+00") + 15;
Chris@42 622 }
Chris@42 623
Chris@42 624 void
Chris@44 625 TimeValueLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@42 626 {
Chris@42 627 if (!m_model) return;
Chris@55 628 if (m_plotStyle == PlotSegmentation) return;
Chris@42 629
Chris@44 630 float val = m_model->getValueMinimum();
Chris@44 631 float inc = (m_model->getValueMaximum() - val) / 10;
Chris@42 632
Chris@55 633 char buffer[40];
Chris@55 634
Chris@55 635 int w = getVerticalScaleWidth(v, paint);
Chris@55 636
Chris@44 637 while (val < m_model->getValueMaximum()) {
Chris@44 638 int y = getYForValue(v, val);
Chris@55 639 // QString label = QString("%1").arg(val);
Chris@55 640 sprintf(buffer, "%+.3e", val);
Chris@55 641 QString label = QString(buffer);
Chris@55 642 paint.drawLine(w - 5, y, w, y);// 100 - 10, y, 100, y);
Chris@55 643 paint.drawText(5, // 100 - 15 - paint.fontMetrics().width(label),
Chris@55 644 y - paint.fontMetrics().height() + paint.fontMetrics().ascent(),
Chris@42 645 label);
Chris@44 646 val += inc;
Chris@42 647 }
Chris@42 648
Chris@42 649 }
Chris@42 650
Chris@21 651 void
Chris@44 652 TimeValueLayer::drawStart(View *v, QMouseEvent *e)
Chris@21 653 {
Chris@21 654 std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 655
Chris@21 656 if (!m_model) return;
Chris@21 657
Chris@44 658 long frame = v->getFrameForX(e->x());
Chris@21 659 if (frame < 0) frame = 0;
Chris@21 660 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 661
Chris@44 662 float value = getValueForY(v, e->y());
Chris@21 663
Chris@21 664 m_editingPoint = SparseTimeValueModel::Point(frame, value, tr("New Point"));
Chris@23 665 m_originalPoint = m_editingPoint;
Chris@22 666
Chris@22 667 if (m_editingCommand) m_editingCommand->finish();
Chris@22 668 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 669 tr("Draw Point"));
Chris@22 670 m_editingCommand->addPoint(m_editingPoint);
Chris@22 671
Chris@21 672 m_editing = true;
Chris@21 673 }
Chris@21 674
Chris@21 675 void
Chris@44 676 TimeValueLayer::drawDrag(View *v, QMouseEvent *e)
Chris@21 677 {
Chris@21 678 std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 679
Chris@21 680 if (!m_model || !m_editing) return;
Chris@21 681
Chris@44 682 long frame = v->getFrameForX(e->x());
Chris@21 683 if (frame < 0) frame = 0;
Chris@21 684 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 685
Chris@44 686 float value = getValueForY(v, e->y());
Chris@21 687
Chris@22 688 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 689 m_editingPoint.frame = frame;
Chris@21 690 m_editingPoint.value = value;
Chris@22 691 m_editingCommand->addPoint(m_editingPoint);
Chris@21 692 }
Chris@21 693
Chris@21 694 void
Chris@44 695 TimeValueLayer::drawEnd(View *v, QMouseEvent *e)
Chris@21 696 {
Chris@21 697 std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 698 if (!m_model || !m_editing) return;
Chris@22 699 m_editingCommand->finish();
Chris@22 700 m_editingCommand = 0;
Chris@21 701 m_editing = false;
Chris@21 702 }
Chris@21 703
Chris@21 704 void
Chris@44 705 TimeValueLayer::editStart(View *v, QMouseEvent *e)
Chris@21 706 {
Chris@21 707 std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 708
Chris@21 709 if (!m_model) return;
Chris@21 710
Chris@44 711 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@21 712 if (points.empty()) return;
Chris@21 713
Chris@21 714 m_editingPoint = *points.begin();
Chris@23 715 m_originalPoint = m_editingPoint;
Chris@22 716
Chris@22 717 if (m_editingCommand) {
Chris@22 718 m_editingCommand->finish();
Chris@22 719 m_editingCommand = 0;
Chris@22 720 }
Chris@22 721
Chris@21 722 m_editing = true;
Chris@21 723 }
Chris@21 724
Chris@21 725 void
Chris@44 726 TimeValueLayer::editDrag(View *v, QMouseEvent *e)
Chris@21 727 {
Chris@21 728 std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 729
Chris@21 730 if (!m_model || !m_editing) return;
Chris@21 731
Chris@44 732 long frame = v->getFrameForX(e->x());
Chris@21 733 if (frame < 0) frame = 0;
Chris@21 734 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 735
Chris@44 736 float value = getValueForY(v, e->y());
Chris@21 737
Chris@22 738 if (!m_editingCommand) {
Chris@22 739 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 740 tr("Drag Point"));
Chris@22 741 }
Chris@22 742
Chris@22 743 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 744 m_editingPoint.frame = frame;
Chris@21 745 m_editingPoint.value = value;
Chris@22 746 m_editingCommand->addPoint(m_editingPoint);
Chris@21 747 }
Chris@21 748
Chris@21 749 void
Chris@44 750 TimeValueLayer::editEnd(View *v, QMouseEvent *e)
Chris@21 751 {
Chris@21 752 std::cerr << "TimeValueLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 753 if (!m_model || !m_editing) return;
Chris@23 754
Chris@23 755 if (m_editingCommand) {
Chris@23 756
Chris@23 757 QString newName = m_editingCommand->getName();
Chris@23 758
Chris@23 759 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@23 760 if (m_editingPoint.value != m_originalPoint.value) {
Chris@23 761 newName = tr("Edit Point");
Chris@23 762 } else {
Chris@23 763 newName = tr("Relocate Point");
Chris@23 764 }
Chris@23 765 } else {
Chris@23 766 newName = tr("Change Point Value");
Chris@23 767 }
Chris@23 768
Chris@23 769 m_editingCommand->setName(newName);
Chris@23 770 m_editingCommand->finish();
Chris@23 771 }
Chris@23 772
Chris@22 773 m_editingCommand = 0;
Chris@21 774 m_editing = false;
Chris@21 775 }
Chris@21 776
Chris@43 777 void
Chris@43 778 TimeValueLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 779 {
Chris@43 780 SparseTimeValueModel::EditCommand *command =
Chris@43 781 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 782 tr("Drag Selection"));
Chris@43 783
Chris@43 784 SparseTimeValueModel::PointList points =
Chris@43 785 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 786
Chris@43 787 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 788 i != points.end(); ++i) {
Chris@43 789
Chris@43 790 if (s.contains(i->frame)) {
Chris@43 791 SparseTimeValueModel::Point newPoint(*i);
Chris@43 792 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 793 command->deletePoint(*i);
Chris@43 794 command->addPoint(newPoint);
Chris@43 795 }
Chris@43 796 }
Chris@43 797
Chris@43 798 command->finish();
Chris@43 799 }
Chris@43 800
Chris@43 801 void
Chris@43 802 TimeValueLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 803 {
Chris@43 804 SparseTimeValueModel::EditCommand *command =
Chris@43 805 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 806 tr("Resize Selection"));
Chris@43 807
Chris@43 808 SparseTimeValueModel::PointList points =
Chris@43 809 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 810
Chris@43 811 double ratio =
Chris@43 812 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 813 double(s.getEndFrame() - s.getStartFrame());
Chris@43 814
Chris@43 815 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 816 i != points.end(); ++i) {
Chris@43 817
Chris@43 818 if (s.contains(i->frame)) {
Chris@43 819
Chris@43 820 double target = i->frame;
Chris@43 821 target = newSize.getStartFrame() +
Chris@43 822 double(target - s.getStartFrame()) * ratio;
Chris@43 823
Chris@43 824 SparseTimeValueModel::Point newPoint(*i);
Chris@43 825 newPoint.frame = lrint(target);
Chris@43 826 command->deletePoint(*i);
Chris@43 827 command->addPoint(newPoint);
Chris@43 828 }
Chris@43 829 }
Chris@43 830
Chris@43 831 command->finish();
Chris@43 832 }
Chris@43 833
Chris@6 834 QString
Chris@6 835 TimeValueLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 836 {
Chris@6 837 return Layer::toXmlString(indent, extraAttributes +
Chris@6 838 QString(" colour=\"%1\" plotStyle=\"%2\"")
Chris@6 839 .arg(encodeColour(m_colour)).arg(m_plotStyle));
Chris@0 840 }
Chris@0 841
Chris@11 842 void
Chris@11 843 TimeValueLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 844 {
Chris@11 845 QString colourSpec = attributes.value("colour");
Chris@11 846 if (colourSpec != "") {
Chris@11 847 QColor colour(colourSpec);
Chris@11 848 if (colour.isValid()) {
Chris@11 849 setBaseColour(QColor(colourSpec));
Chris@11 850 }
Chris@11 851 }
Chris@11 852
Chris@11 853 bool ok;
Chris@11 854 PlotStyle style = (PlotStyle)
Chris@11 855 attributes.value("plotStyle").toInt(&ok);
Chris@11 856 if (ok) setPlotStyle(style);
Chris@11 857 }
Chris@11 858
Chris@0 859
Chris@0 860 #ifdef INCLUDE_MOCFILES
Chris@0 861 #include "TimeValueLayer.moc.cpp"
Chris@0 862 #endif
Chris@0 863