annotate layer/TimeValueLayer.cpp @ 359:020c485aa7e0

* More work on aligning copy/paste between layers. It's a surprisingly complicated business.
author Chris Cannam
date Wed, 06 Feb 2008 12:49:49 +0000
parents de4db9b5dcf1
children d58701996fae
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@128 18 #include "data/model/Model.h"
Chris@0 19 #include "base/RealTime.h"
Chris@0 20 #include "base/Profiler.h"
Chris@197 21 #include "base/LogRange.h"
Chris@287 22 #include "base/ColourDatabase.h"
Chris@128 23 #include "view/View.h"
Chris@0 24
Chris@128 25 #include "data/model/SparseTimeValueModel.h"
Chris@340 26 #include "data/model/Labeller.h"
Chris@0 27
Chris@70 28 #include "widgets/ItemEditDialog.h"
Chris@125 29 #include "widgets/ListInputDialog.h"
Chris@70 30
Chris@66 31 #include "SpectrogramLayer.h" // for optional frequency alignment
Chris@285 32 #include "base/ColourMapper.h"
Chris@66 33
Chris@0 34 #include <QPainter>
Chris@6 35 #include <QPainterPath>
Chris@21 36 #include <QMouseEvent>
Chris@125 37 #include <QRegExp>
Chris@316 38 #include <QTextStream>
Chris@340 39 #include <QInputDialog>
Chris@0 40
Chris@0 41 #include <iostream>
Chris@0 42 #include <cmath>
Chris@0 43
Chris@44 44 TimeValueLayer::TimeValueLayer() :
Chris@287 45 SingleColourLayer(),
Chris@0 46 m_model(0),
Chris@21 47 m_editing(false),
Chris@23 48 m_originalPoint(0, 0.0, tr("New Point")),
Chris@21 49 m_editingPoint(0, 0.0, tr("New Point")),
Chris@22 50 m_editingCommand(0),
Chris@197 51 m_colourMap(0),
Chris@66 52 m_plotStyle(PlotConnectedPoints),
Chris@101 53 m_verticalScale(AutoAlignScale)
Chris@0 54 {
Chris@44 55
Chris@0 56 }
Chris@0 57
Chris@0 58 void
Chris@0 59 TimeValueLayer::setModel(SparseTimeValueModel *model)
Chris@0 60 {
Chris@0 61 if (m_model == model) return;
Chris@0 62 m_model = model;
Chris@0 63
Chris@320 64 connectSignals(m_model);
Chris@0 65
Chris@101 66 // std::cerr << "TimeValueLayer::setModel(" << model << ")" << std::endl;
Chris@0 67
Chris@0 68 emit modelReplaced();
Chris@0 69 }
Chris@0 70
Chris@0 71 Layer::PropertyList
Chris@0 72 TimeValueLayer::getProperties() const
Chris@0 73 {
Chris@287 74 PropertyList list = SingleColourLayer::getProperties();
Chris@87 75 list.push_back("Plot Type");
Chris@87 76 list.push_back("Vertical Scale");
Chris@100 77 list.push_back("Scale Units");
Chris@0 78 return list;
Chris@0 79 }
Chris@0 80
Chris@87 81 QString
Chris@87 82 TimeValueLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 83 {
Chris@87 84 if (name == "Plot Type") return tr("Plot Type");
Chris@87 85 if (name == "Vertical Scale") return tr("Vertical Scale");
Chris@100 86 if (name == "Scale Units") return tr("Scale Units");
Chris@287 87 return SingleColourLayer::getPropertyLabel(name);
Chris@87 88 }
Chris@87 89
Chris@0 90 Layer::PropertyType
Chris@0 91 TimeValueLayer::getPropertyType(const PropertyName &name) const
Chris@0 92 {
Chris@287 93 if (name == "Plot Type") return ValueProperty;
Chris@287 94 if (name == "Vertical Scale") return ValueProperty;
Chris@100 95 if (name == "Scale Units") return UnitsProperty;
Chris@287 96 if (name == "Colour" && m_plotStyle == PlotSegmentation) return ValueProperty;
Chris@287 97 return SingleColourLayer::getPropertyType(name);
Chris@0 98 }
Chris@0 99
Chris@198 100 QString
Chris@198 101 TimeValueLayer::getPropertyGroupName(const PropertyName &name) const
Chris@198 102 {
Chris@198 103 if (name == "Vertical Scale" || name == "Scale Units") {
Chris@198 104 return tr("Scale");
Chris@198 105 }
Chris@287 106 return SingleColourLayer::getPropertyGroupName(name);
Chris@198 107 }
Chris@198 108
Chris@0 109 int
Chris@0 110 TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 111 int *min, int *max, int *deflt) const
Chris@0 112 {
Chris@216 113 int val = 0;
Chris@0 114
Chris@287 115 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
Chris@197 116
Chris@287 117 if (min) *min = 0;
Chris@287 118 if (max) *max = ColourMapper::getColourMapCount() - 1;
Chris@287 119 if (deflt) *deflt = 0;
Chris@197 120
Chris@287 121 val = m_colourMap;
Chris@0 122
Chris@87 123 } else if (name == "Plot Type") {
Chris@0 124
Chris@10 125 if (min) *min = 0;
Chris@26 126 if (max) *max = 5;
Chris@216 127 if (deflt) *deflt = int(PlotConnectedPoints);
Chris@0 128
Chris@216 129 val = int(m_plotStyle);
Chris@0 130
Chris@87 131 } else if (name == "Vertical Scale") {
Chris@66 132
Chris@66 133 if (min) *min = 0;
Chris@66 134 if (max) *max = 3;
Chris@216 135 if (deflt) *deflt = int(AutoAlignScale);
Chris@66 136
Chris@216 137 val = int(m_verticalScale);
Chris@66 138
Chris@100 139 } else if (name == "Scale Units") {
Chris@100 140
Chris@216 141 if (deflt) *deflt = 0;
Chris@100 142 if (m_model) {
Chris@216 143 val = UnitDatabase::getInstance()->getUnitId
Chris@100 144 (m_model->getScaleUnits());
Chris@100 145 }
Chris@100 146
Chris@0 147 } else {
Chris@0 148
Chris@287 149 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@0 150 }
Chris@0 151
Chris@216 152 return val;
Chris@0 153 }
Chris@0 154
Chris@0 155 QString
Chris@0 156 TimeValueLayer::getPropertyValueLabel(const PropertyName &name,
Chris@0 157 int value) const
Chris@0 158 {
Chris@287 159 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
Chris@287 160 return ColourMapper::getColourMapName(value);
Chris@87 161 } else if (name == "Plot Type") {
Chris@0 162 switch (value) {
Chris@0 163 default:
Chris@0 164 case 0: return tr("Points");
Chris@0 165 case 1: return tr("Stems");
Chris@6 166 case 2: return tr("Connected Points");
Chris@6 167 case 3: return tr("Lines");
Chris@6 168 case 4: return tr("Curve");
Chris@26 169 case 5: return tr("Segmentation");
Chris@0 170 }
Chris@87 171 } else if (name == "Vertical Scale") {
Chris@66 172 switch (value) {
Chris@66 173 default:
Chris@101 174 case 0: return tr("Auto-Align");
Chris@198 175 case 1: return tr("Linear");
Chris@198 176 case 2: return tr("Log");
Chris@198 177 case 3: return tr("+/-1");
Chris@66 178 }
Chris@0 179 }
Chris@287 180 return SingleColourLayer::getPropertyValueLabel(name, value);
Chris@0 181 }
Chris@0 182
Chris@0 183 void
Chris@0 184 TimeValueLayer::setProperty(const PropertyName &name, int value)
Chris@0 185 {
Chris@287 186 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
Chris@287 187 setFillColourMap(value);
Chris@87 188 } else if (name == "Plot Type") {
Chris@0 189 setPlotStyle(PlotStyle(value));
Chris@87 190 } else if (name == "Vertical Scale") {
Chris@66 191 setVerticalScale(VerticalScale(value));
Chris@100 192 } else if (name == "Scale Units") {
Chris@100 193 if (m_model) {
Chris@100 194 m_model->setScaleUnits
Chris@100 195 (UnitDatabase::getInstance()->getUnitById(value));
Chris@100 196 emit modelChanged();
Chris@100 197 }
Chris@287 198 } else {
Chris@287 199 SingleColourLayer::setProperty(name, value);
Chris@0 200 }
Chris@0 201 }
Chris@0 202
Chris@0 203 void
Chris@197 204 TimeValueLayer::setFillColourMap(int map)
Chris@197 205 {
Chris@197 206 if (m_colourMap == map) return;
Chris@197 207 m_colourMap = map;
Chris@197 208 emit layerParametersChanged();
Chris@197 209 }
Chris@197 210
Chris@197 211 void
Chris@0 212 TimeValueLayer::setPlotStyle(PlotStyle style)
Chris@0 213 {
Chris@0 214 if (m_plotStyle == style) return;
Chris@197 215 bool colourTypeChanged = (style == PlotSegmentation ||
Chris@197 216 m_plotStyle == PlotSegmentation);
Chris@0 217 m_plotStyle = style;
Chris@197 218 if (colourTypeChanged) {
Chris@197 219 emit layerParameterRangesChanged();
Chris@197 220 }
Chris@0 221 emit layerParametersChanged();
Chris@0 222 }
Chris@0 223
Chris@66 224 void
Chris@66 225 TimeValueLayer::setVerticalScale(VerticalScale scale)
Chris@66 226 {
Chris@66 227 if (m_verticalScale == scale) return;
Chris@66 228 m_verticalScale = scale;
Chris@66 229 emit layerParametersChanged();
Chris@66 230 }
Chris@66 231
Chris@0 232 bool
Chris@44 233 TimeValueLayer::isLayerScrollable(const View *v) const
Chris@0 234 {
Chris@6 235 // We don't illuminate sections in the line or curve modes, so
Chris@6 236 // they're always scrollable
Chris@6 237
Chris@6 238 if (m_plotStyle == PlotLines ||
Chris@6 239 m_plotStyle == PlotCurve) return true;
Chris@6 240
Chris@0 241 QPoint discard;
Chris@44 242 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@0 243 }
Chris@0 244
Chris@79 245 bool
Chris@101 246 TimeValueLayer::getValueExtents(float &min, float &max,
Chris@101 247 bool &logarithmic, QString &unit) const
Chris@79 248 {
Chris@101 249 if (!m_model) return false;
Chris@79 250 min = m_model->getValueMinimum();
Chris@79 251 max = m_model->getValueMaximum();
Chris@101 252 logarithmic = (m_verticalScale == LogScale);
Chris@79 253 unit = m_model->getScaleUnits();
Chris@79 254 return true;
Chris@79 255 }
Chris@79 256
Chris@101 257 bool
Chris@101 258 TimeValueLayer::getDisplayExtents(float &min, float &max) const
Chris@101 259 {
Chris@296 260 if (!m_model || shouldAutoAlign()) return false;
Chris@101 261
Chris@101 262 min = m_model->getValueMinimum();
Chris@101 263 max = m_model->getValueMaximum();
Chris@101 264 return true;
Chris@101 265 }
Chris@101 266
Chris@0 267 SparseTimeValueModel::PointList
Chris@44 268 TimeValueLayer::getLocalPoints(View *v, int x) const
Chris@0 269 {
Chris@0 270 if (!m_model) return SparseTimeValueModel::PointList();
Chris@0 271
Chris@44 272 long frame = v->getFrameForX(x);
Chris@0 273
Chris@0 274 SparseTimeValueModel::PointList onPoints =
Chris@0 275 m_model->getPoints(frame);
Chris@0 276
Chris@0 277 if (!onPoints.empty()) {
Chris@0 278 return onPoints;
Chris@0 279 }
Chris@0 280
Chris@0 281 SparseTimeValueModel::PointList prevPoints =
Chris@0 282 m_model->getPreviousPoints(frame);
Chris@0 283 SparseTimeValueModel::PointList nextPoints =
Chris@0 284 m_model->getNextPoints(frame);
Chris@0 285
Chris@0 286 SparseTimeValueModel::PointList usePoints = prevPoints;
Chris@0 287
Chris@0 288 if (prevPoints.empty()) {
Chris@0 289 usePoints = nextPoints;
Chris@248 290 } else if (long(prevPoints.begin()->frame) < v->getStartFrame() &&
Chris@44 291 !(nextPoints.begin()->frame > v->getEndFrame())) {
Chris@0 292 usePoints = nextPoints;
Chris@0 293 } else if (nextPoints.begin()->frame - frame <
Chris@0 294 frame - prevPoints.begin()->frame) {
Chris@0 295 usePoints = nextPoints;
Chris@0 296 }
Chris@0 297
Chris@28 298 if (!usePoints.empty()) {
Chris@28 299 int fuzz = 2;
Chris@44 300 int px = v->getXForFrame(usePoints.begin()->frame);
Chris@28 301 if ((px > x && px - x > fuzz) ||
Chris@28 302 (px < x && x - px > fuzz + 1)) {
Chris@28 303 usePoints.clear();
Chris@28 304 }
Chris@28 305 }
Chris@28 306
Chris@0 307 return usePoints;
Chris@0 308 }
Chris@0 309
Chris@25 310 QString
Chris@44 311 TimeValueLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@0 312 {
Chris@25 313 int x = pos.x();
Chris@0 314
Chris@25 315 if (!m_model || !m_model->getSampleRate()) return "";
Chris@0 316
Chris@44 317 SparseTimeValueModel::PointList points = getLocalPoints(v, x);
Chris@0 318
Chris@0 319 if (points.empty()) {
Chris@0 320 if (!m_model->isReady()) {
Chris@25 321 return tr("In progress");
Chris@25 322 } else {
Chris@25 323 return tr("No local points");
Chris@0 324 }
Chris@0 325 }
Chris@0 326
Chris@0 327 long useFrame = points.begin()->frame;
Chris@0 328
Chris@0 329 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
Chris@25 330
Chris@25 331 QString text;
Chris@101 332 QString unit = m_model->getScaleUnits();
Chris@101 333 if (unit != "") unit = " " + unit;
Chris@0 334
Chris@25 335 if (points.begin()->label == "") {
Chris@101 336 text = QString(tr("Time:\t%1\nValue:\t%2%3\nNo label"))
Chris@25 337 .arg(rt.toText(true).c_str())
Chris@25 338 .arg(points.begin()->value)
Chris@101 339 .arg(unit);
Chris@101 340 } else {
Chris@101 341 text = QString(tr("Time:\t%1\nValue:\t%2%3\nLabel:\t%4"))
Chris@101 342 .arg(rt.toText(true).c_str())
Chris@101 343 .arg(points.begin()->value)
Chris@101 344 .arg(unit)
Chris@25 345 .arg(points.begin()->label);
Chris@25 346 }
Chris@0 347
Chris@44 348 pos = QPoint(v->getXForFrame(useFrame),
Chris@44 349 getYForValue(v, points.begin()->value));
Chris@25 350 return text;
Chris@0 351 }
Chris@0 352
Chris@28 353 bool
Chris@44 354 TimeValueLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 355 size_t &resolution,
Chris@28 356 SnapType snap) const
Chris@13 357 {
Chris@13 358 if (!m_model) {
Chris@44 359 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@13 360 }
Chris@13 361
Chris@13 362 resolution = m_model->getResolution();
Chris@28 363 SparseTimeValueModel::PointList points;
Chris@13 364
Chris@28 365 if (snap == SnapNeighbouring) {
Chris@28 366
Chris@44 367 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@28 368 if (points.empty()) return false;
Chris@28 369 frame = points.begin()->frame;
Chris@28 370 return true;
Chris@28 371 }
Chris@28 372
Chris@28 373 points = m_model->getPoints(frame, frame);
Chris@28 374 int snapped = frame;
Chris@28 375 bool found = false;
Chris@13 376
Chris@13 377 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@13 378 i != points.end(); ++i) {
Chris@13 379
Chris@28 380 if (snap == SnapRight) {
Chris@28 381
Chris@13 382 if (i->frame > frame) {
Chris@28 383 snapped = i->frame;
Chris@28 384 found = true;
Chris@13 385 break;
Chris@13 386 }
Chris@28 387
Chris@28 388 } else if (snap == SnapLeft) {
Chris@28 389
Chris@13 390 if (i->frame <= frame) {
Chris@28 391 snapped = i->frame;
Chris@28 392 found = true; // don't break, as the next may be better
Chris@28 393 } else {
Chris@28 394 break;
Chris@28 395 }
Chris@28 396
Chris@28 397 } else { // nearest
Chris@28 398
Chris@28 399 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@28 400 ++j;
Chris@28 401
Chris@28 402 if (j == points.end()) {
Chris@28 403
Chris@28 404 snapped = i->frame;
Chris@28 405 found = true;
Chris@28 406 break;
Chris@28 407
Chris@28 408 } else if (j->frame >= frame) {
Chris@28 409
Chris@28 410 if (j->frame - frame < frame - i->frame) {
Chris@28 411 snapped = j->frame;
Chris@28 412 } else {
Chris@28 413 snapped = i->frame;
Chris@28 414 }
Chris@28 415 found = true;
Chris@28 416 break;
Chris@13 417 }
Chris@13 418 }
Chris@13 419 }
Chris@13 420
Chris@28 421 frame = snapped;
Chris@28 422 return found;
Chris@13 423 }
Chris@13 424
Chris@101 425 void
Chris@101 426 TimeValueLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const
Chris@101 427 {
Chris@101 428 min = 0.0;
Chris@101 429 max = 0.0;
Chris@101 430 log = false;
Chris@101 431
Chris@296 432 if (shouldAutoAlign()) {
Chris@101 433
Chris@101 434 if (!v->getValueExtents(m_model->getScaleUnits(), min, max, log)) {
Chris@101 435 min = m_model->getValueMinimum();
Chris@101 436 max = m_model->getValueMaximum();
Chris@101 437 } else if (log) {
Chris@197 438 LogRange::mapRange(min, max);
Chris@101 439 }
Chris@101 440
Chris@101 441 } else if (m_verticalScale == PlusMinusOneScale) {
Chris@101 442
Chris@101 443 min = -1.0;
Chris@101 444 max = 1.0;
Chris@101 445
Chris@101 446 } else {
Chris@101 447
Chris@101 448 min = m_model->getValueMinimum();
Chris@101 449 max = m_model->getValueMaximum();
Chris@101 450
Chris@101 451 if (m_verticalScale == LogScale) {
Chris@197 452 LogRange::mapRange(min, max);
Chris@101 453 log = true;
Chris@101 454 }
Chris@101 455 }
Chris@101 456
Chris@101 457 if (max == min) max = min + 1.0;
Chris@101 458 }
Chris@101 459
Chris@21 460 int
Chris@66 461 TimeValueLayer::getYForValue(View *v, float val) const
Chris@21 462 {
Chris@79 463 float min = 0.0, max = 0.0;
Chris@101 464 bool logarithmic = false;
Chris@79 465 int h = v->height();
Chris@79 466
Chris@101 467 getScaleExtents(v, min, max, logarithmic);
Chris@101 468
Chris@101 469 // std::cerr << "getYForValue(" << val << "): min " << min << ", max "
Chris@101 470 // << max << ", log " << logarithmic << std::endl;
Chris@101 471
Chris@101 472 if (logarithmic) {
Chris@197 473 val = LogRange::map(val);
Chris@79 474 }
Chris@79 475
Chris@66 476 return int(h - ((val - min) * h) / (max - min));
Chris@21 477 }
Chris@21 478
Chris@21 479 float
Chris@44 480 TimeValueLayer::getValueForY(View *v, int y) const
Chris@21 481 {
Chris@101 482 float min = 0.0, max = 0.0;
Chris@101 483 bool logarithmic = false;
Chris@44 484 int h = v->height();
Chris@21 485
Chris@101 486 getScaleExtents(v, min, max, logarithmic);
Chris@101 487
Chris@101 488 float val = min + (float(h - y) * float(max - min)) / h;
Chris@101 489
Chris@101 490 if (logarithmic) {
Chris@197 491 val = powf(10.f, val);
Chris@101 492 }
Chris@101 493
Chris@101 494 return val;
Chris@21 495 }
Chris@21 496
Chris@296 497 bool
Chris@296 498 TimeValueLayer::shouldAutoAlign() const
Chris@296 499 {
Chris@296 500 if (!m_model) return false;
Chris@296 501 QString unit = m_model->getScaleUnits();
Chris@296 502 return (m_verticalScale == AutoAlignScale && unit != "");
Chris@296 503 }
Chris@296 504
Chris@68 505 QColor
Chris@101 506 TimeValueLayer::getColourForValue(View *v, float val) const
Chris@68 507 {
Chris@101 508 float min, max;
Chris@101 509 bool log;
Chris@101 510 getScaleExtents(v, min, max, log);
Chris@68 511
Chris@197 512 if (min > max) std::swap(min, max);
Chris@197 513 if (max == min) max = min + 1;
Chris@197 514
Chris@101 515 if (log) {
Chris@197 516 LogRange::mapRange(min, max);
Chris@197 517 val = LogRange::map(val);
Chris@68 518 }
Chris@68 519
Chris@197 520 // std::cerr << "TimeValueLayer::getColourForValue: min " << min << ", max "
Chris@197 521 // << max << ", log " << log << ", value " << val << std::endl;
Chris@68 522
Chris@197 523 QColor solid = ColourMapper(m_colourMap, min, max).map(val);
Chris@197 524 return QColor(solid.red(), solid.green(), solid.blue(), 120);
Chris@68 525 }
Chris@68 526
Chris@287 527 int
Chris@287 528 TimeValueLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 529 {
Chris@287 530 impose = false;
Chris@287 531 return ColourDatabase::getInstance()->getColourIndex
Chris@287 532 (QString(darkbg ? "Bright Green" : "Green"));
Chris@287 533 }
Chris@287 534
Chris@0 535 void
Chris@44 536 TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 537 {
Chris@0 538 if (!m_model || !m_model->isOK()) return;
Chris@0 539
Chris@0 540 int sampleRate = m_model->getSampleRate();
Chris@0 541 if (!sampleRate) return;
Chris@0 542
Chris@353 543 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@353 544
Chris@0 545 // Profiler profiler("TimeValueLayer::paint", true);
Chris@0 546
Chris@0 547 int x0 = rect.left(), x1 = rect.right();
Chris@44 548 long frame0 = v->getFrameForX(x0);
Chris@44 549 long frame1 = v->getFrameForX(x1);
Chris@0 550
Chris@0 551 SparseTimeValueModel::PointList points(m_model->getPoints
Chris@0 552 (frame0, frame1));
Chris@11 553 if (points.empty()) return;
Chris@0 554
Chris@287 555 paint.setPen(getBaseQColor());
Chris@0 556
Chris@287 557 QColor brushColour(getBaseQColor());
Chris@0 558 brushColour.setAlpha(80);
Chris@0 559 paint.setBrush(brushColour);
Chris@0 560
Chris@0 561 // std::cerr << "TimeValueLayer::paint: resolution is "
Chris@0 562 // << m_model->getResolution() << " frames" << std::endl;
Chris@0 563
Chris@0 564 float min = m_model->getValueMinimum();
Chris@0 565 float max = m_model->getValueMaximum();
Chris@0 566 if (max == min) max = min + 1.0;
Chris@0 567
Chris@44 568 int origin = int(nearbyint(v->height() -
Chris@44 569 (-min * v->height()) / (max - min)));
Chris@0 570
Chris@0 571 QPoint localPos;
Chris@0 572 long illuminateFrame = -1;
Chris@0 573
Chris@44 574 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@0 575 SparseTimeValueModel::PointList localPoints =
Chris@44 576 getLocalPoints(v, localPos.x());
Chris@296 577 // std::cerr << "TimeValueLayer: " << localPoints.size() << " local points" << std::endl;
Chris@0 578 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@0 579 }
Chris@6 580
Chris@20 581 int w =
Chris@44 582 v->getXForFrame(frame0 + m_model->getResolution()) -
Chris@44 583 v->getXForFrame(frame0);
Chris@7 584
Chris@6 585 paint.save();
Chris@6 586
Chris@6 587 QPainterPath path;
Chris@55 588 int pointCount = 0;
Chris@79 589
Chris@79 590 int textY = 0;
Chris@79 591 if (m_plotStyle == PlotSegmentation) {
Chris@79 592 textY = v->getTextLabelHeight(this, paint);
Chris@79 593 }
Chris@6 594
Chris@0 595 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
Chris@0 596 i != points.end(); ++i) {
Chris@0 597
Chris@0 598 const SparseTimeValueModel::Point &p(*i);
Chris@0 599
Chris@44 600 int x = v->getXForFrame(p.frame);
Chris@44 601 int y = getYForValue(v, p.value);
Chris@0 602
Chris@79 603 if (m_plotStyle != PlotSegmentation) {
Chris@79 604 textY = y - paint.fontMetrics().height()
Chris@79 605 + paint.fontMetrics().ascent();
Chris@79 606 }
Chris@79 607
Chris@34 608 bool haveNext = false;
Chris@76 609 int nx = v->getXForFrame(v->getModelsEndFrame());
Chris@76 610 // m_model->getEndFrame());
Chris@34 611 int ny = y;
Chris@34 612
Chris@34 613 SparseTimeValueModel::PointList::const_iterator j = i;
Chris@34 614 ++j;
Chris@34 615
Chris@34 616 if (j != points.end()) {
Chris@34 617 const SparseTimeValueModel::Point &q(*j);
Chris@44 618 nx = v->getXForFrame(q.frame);
Chris@44 619 ny = getYForValue(v, q.value);
Chris@34 620 haveNext = true;
Chris@76 621 }
Chris@76 622
Chris@76 623 // std::cout << "frame = " << p.frame << ", x = " << x << ", haveNext = " << haveNext
Chris@76 624 // << ", nx = " << nx << std::endl;
Chris@34 625
Chris@34 626 int labelY = y;
Chris@34 627
Chris@0 628 if (w < 1) w = 1;
Chris@287 629 paint.setPen(getBaseQColor());
Chris@0 630
Chris@26 631 if (m_plotStyle == PlotSegmentation) {
Chris@287 632 paint.setPen(getForegroundQColor(v));
Chris@101 633 paint.setBrush(getColourForValue(v, p.value));
Chris@44 634 labelY = v->height();
Chris@26 635 } else if (m_plotStyle == PlotLines ||
Chris@26 636 m_plotStyle == PlotCurve) {
Chris@6 637 paint.setBrush(Qt::NoBrush);
Chris@3 638 } else {
Chris@6 639 paint.setBrush(brushColour);
Chris@3 640 }
Chris@0 641
Chris@0 642 if (m_plotStyle == PlotStems) {
Chris@0 643 paint.setPen(brushColour);
Chris@0 644 if (y < origin - 1) {
Chris@0 645 paint.drawRect(x + w/2, y + 1, 1, origin - y);
Chris@0 646 } else if (y > origin + 1) {
Chris@0 647 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
Chris@0 648 }
Chris@287 649 paint.setPen(getBaseQColor());
Chris@0 650 }
Chris@0 651
Chris@0 652 if (illuminateFrame == p.frame) {
Chris@6 653
Chris@0 654 //!!! aside from the problem of choosing a colour, it'd be
Chris@0 655 //better to save the highlighted rects and draw them at
Chris@0 656 //the end perhaps
Chris@6 657
Chris@6 658 //!!! not equipped to illuminate the right section in line
Chris@6 659 //or curve mode
Chris@6 660
Chris@6 661 if (m_plotStyle != PlotCurve &&
Chris@6 662 m_plotStyle != PlotLines) {
Chris@287 663 paint.setPen(getForegroundQColor(v));
Chris@6 664 }
Chris@0 665 }
Chris@0 666
Chris@6 667 if (m_plotStyle != PlotLines &&
Chris@26 668 m_plotStyle != PlotCurve &&
Chris@26 669 m_plotStyle != PlotSegmentation) {
Chris@326 670 if (m_plotStyle != PlotStems ||
Chris@326 671 w > 1) {
Chris@326 672 paint.drawRect(x, y - 1, w, 2);
Chris@326 673 }
Chris@3 674 }
Chris@0 675
Chris@6 676 if (m_plotStyle == PlotConnectedPoints ||
Chris@6 677 m_plotStyle == PlotLines ||
Chris@6 678 m_plotStyle == PlotCurve) {
Chris@0 679
Chris@34 680 if (haveNext) {
Chris@3 681
Chris@6 682 if (m_plotStyle == PlotConnectedPoints) {
Chris@34 683
Chris@79 684 paint.save();
Chris@3 685 paint.setPen(brushColour);
Chris@3 686 paint.drawLine(x + w, y, nx, ny);
Chris@79 687 paint.restore();
Chris@6 688
Chris@6 689 } else if (m_plotStyle == PlotLines) {
Chris@6 690
Chris@6 691 paint.drawLine(x + w/2, y, nx + w/2, ny);
Chris@6 692
Chris@3 693 } else {
Chris@6 694
Chris@55 695 float x0 = x + float(w)/2;
Chris@55 696 float x1 = nx + float(w)/2;
Chris@55 697
Chris@55 698 float y0 = y;
Chris@55 699 float y1 = ny;
Chris@55 700
Chris@55 701 if (pointCount == 0) {
Chris@55 702 path.moveTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 703 }
Chris@55 704 ++pointCount;
Chris@6 705
Chris@6 706 if (nx - x > 5) {
Chris@55 707 path.cubicTo(x0, y0,
Chris@55 708 x0, y0,
Chris@55 709 (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 710
Chris@55 711 // // or
Chris@55 712 // path.quadTo(x0, y0, (x0 + x1) / 2, (y0 + y1) / 2);
Chris@55 713
Chris@6 714 } else {
Chris@55 715 path.lineTo((x0 + x1) / 2, (y0 + y1) / 2);
Chris@6 716 }
Chris@3 717 }
Chris@0 718 }
Chris@0 719 }
Chris@0 720
Chris@26 721 if (m_plotStyle == PlotSegmentation) {
Chris@76 722
Chris@76 723 // std::cerr << "drawing rect" << std::endl;
Chris@26 724
Chris@27 725 if (nx <= x) continue;
Chris@26 726
Chris@28 727 if (illuminateFrame != p.frame &&
Chris@44 728 (nx < x + 5 || x >= v->width() - 1)) {
Chris@27 729 paint.setPen(Qt::NoPen);
Chris@27 730 }
Chris@26 731
Chris@44 732 paint.drawRect(x, -1, nx - x, v->height() + 1);
Chris@26 733 }
Chris@26 734
Chris@55 735 if (p.label != "") {
Chris@79 736 if (!haveNext || nx > x + 6 + paint.fontMetrics().width(p.label)) {
Chris@79 737 paint.drawText(x + 5, textY, p.label);
Chris@79 738 }
Chris@55 739 }
Chris@0 740 }
Chris@6 741
Chris@6 742 if (m_plotStyle == PlotCurve && !path.isEmpty()) {
Chris@55 743 paint.setRenderHint(QPainter::Antialiasing, pointCount <= v->width());
Chris@6 744 paint.drawPath(path);
Chris@6 745 }
Chris@6 746
Chris@6 747 paint.restore();
Chris@6 748
Chris@6 749 // looks like save/restore doesn't deal with this:
Chris@6 750 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@6 751 }
Chris@6 752
Chris@42 753 int
Chris@248 754 TimeValueLayer::getVerticalScaleWidth(View *, QPainter &paint) const
Chris@42 755 {
Chris@68 756 int w = paint.fontMetrics().width("-000.000");
Chris@68 757 if (m_plotStyle == PlotSegmentation) return w + 20;
Chris@68 758 else return w + 10;
Chris@42 759 }
Chris@42 760
Chris@42 761 void
Chris@248 762 TimeValueLayer::paintVerticalScale(View *v, QPainter &paint, QRect) const
Chris@42 763 {
Chris@42 764 if (!m_model) return;
Chris@42 765
Chris@68 766 int h = v->height();
Chris@68 767
Chris@68 768 int n = 10;
Chris@68 769
Chris@68 770 float max = m_model->getValueMaximum();
Chris@68 771 float min = m_model->getValueMinimum();
Chris@68 772 float val = min;
Chris@68 773 float inc = (max - val) / n;
Chris@42 774
Chris@55 775 char buffer[40];
Chris@55 776
Chris@55 777 int w = getVerticalScaleWidth(v, paint);
Chris@55 778
Chris@68 779 int tx = 5;
Chris@68 780
Chris@68 781 int boxx = 5, boxy = 5;
Chris@68 782 if (m_model->getScaleUnits() != "") {
Chris@68 783 boxy += paint.fontMetrics().height();
Chris@68 784 }
Chris@68 785 int boxw = 10, boxh = h - boxy - 5;
Chris@68 786
Chris@68 787 if (m_plotStyle == PlotSegmentation) {
Chris@68 788 tx += boxx + boxw;
Chris@68 789 paint.drawRect(boxx, boxy, boxw, boxh);
Chris@68 790 }
Chris@68 791
Chris@68 792 if (m_plotStyle == PlotSegmentation) {
Chris@68 793 paint.save();
Chris@68 794 for (int y = 0; y < boxh; ++y) {
Chris@68 795 float val = ((boxh - y) * (max - min)) / boxh + min;
Chris@101 796 paint.setPen(getColourForValue(v, val));
Chris@68 797 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
Chris@68 798 }
Chris@68 799 paint.restore();
Chris@68 800 }
Chris@68 801
Chris@68 802 for (int i = 0; i < n; ++i) {
Chris@68 803
Chris@68 804 int y, ty;
Chris@68 805 bool drawText = true;
Chris@68 806
Chris@68 807 if (m_plotStyle == PlotSegmentation) {
Chris@68 808 y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
Chris@68 809 ty = y;
Chris@68 810 } else {
Chris@68 811 if (i == n-1) {
Chris@68 812 if (m_model->getScaleUnits() != "") drawText = false;
Chris@68 813 }
Chris@68 814 y = getYForValue(v, val);
Chris@68 815 ty = y - paint.fontMetrics().height() +
Chris@68 816 paint.fontMetrics().ascent();
Chris@68 817 }
Chris@68 818
Chris@68 819 sprintf(buffer, "%.3f", val);
Chris@55 820 QString label = QString(buffer);
Chris@68 821
Chris@68 822 if (m_plotStyle != PlotSegmentation) {
Chris@68 823 paint.drawLine(w - 5, y, w, y);
Chris@68 824 } else {
Chris@68 825 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
Chris@68 826 }
Chris@68 827
Chris@68 828 if (drawText) paint.drawText(tx, ty, label);
Chris@44 829 val += inc;
Chris@42 830 }
Chris@68 831
Chris@67 832 if (m_model->getScaleUnits() != "") {
Chris@67 833 paint.drawText(5, 5 + paint.fontMetrics().ascent(),
Chris@67 834 m_model->getScaleUnits());
Chris@67 835 }
Chris@42 836 }
Chris@42 837
Chris@21 838 void
Chris@44 839 TimeValueLayer::drawStart(View *v, QMouseEvent *e)
Chris@21 840 {
Chris@101 841 // std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 842
Chris@21 843 if (!m_model) return;
Chris@21 844
Chris@44 845 long frame = v->getFrameForX(e->x());
Chris@76 846 long resolution = m_model->getResolution();
Chris@21 847 if (frame < 0) frame = 0;
Chris@76 848 frame = (frame / resolution) * resolution;
Chris@21 849
Chris@44 850 float value = getValueForY(v, e->y());
Chris@21 851
Chris@76 852 bool havePoint = false;
Chris@76 853
Chris@76 854 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@76 855 if (!points.empty()) {
Chris@76 856 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 857 i != points.end(); ++i) {
Chris@76 858 if (((i->frame / resolution) * resolution) != frame) {
Chris@101 859 // std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
Chris@76 860 continue;
Chris@76 861 }
Chris@76 862 m_editingPoint = *i;
Chris@76 863 havePoint = true;
Chris@76 864 }
Chris@76 865 }
Chris@76 866
Chris@76 867 if (!havePoint) {
Chris@76 868 m_editingPoint = SparseTimeValueModel::Point
Chris@76 869 (frame, value, tr("New Point"));
Chris@76 870 }
Chris@76 871
Chris@23 872 m_originalPoint = m_editingPoint;
Chris@22 873
Chris@22 874 if (m_editingCommand) m_editingCommand->finish();
Chris@22 875 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 876 tr("Draw Point"));
Chris@76 877 if (!havePoint) {
Chris@76 878 m_editingCommand->addPoint(m_editingPoint);
Chris@76 879 }
Chris@22 880
Chris@21 881 m_editing = true;
Chris@21 882 }
Chris@21 883
Chris@21 884 void
Chris@44 885 TimeValueLayer::drawDrag(View *v, QMouseEvent *e)
Chris@21 886 {
Chris@101 887 // std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 888
Chris@21 889 if (!m_model || !m_editing) return;
Chris@21 890
Chris@44 891 long frame = v->getFrameForX(e->x());
Chris@76 892 long resolution = m_model->getResolution();
Chris@21 893 if (frame < 0) frame = 0;
Chris@76 894 frame = (frame / resolution) * resolution;
Chris@21 895
Chris@44 896 float value = getValueForY(v, e->y());
Chris@21 897
Chris@76 898 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@76 899
Chris@101 900 // std::cerr << points.size() << " points" << std::endl;
Chris@76 901
Chris@76 902 bool havePoint = false;
Chris@76 903
Chris@76 904 if (!points.empty()) {
Chris@76 905 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 906 i != points.end(); ++i) {
Chris@76 907 if (i->frame == m_editingPoint.frame &&
Chris@76 908 i->value == m_editingPoint.value) {
Chris@101 909 // std::cerr << "ignoring current editing point at " << i->frame << ", " << i->value << std::endl;
Chris@76 910 continue;
Chris@76 911 }
Chris@76 912 if (((i->frame / resolution) * resolution) != frame) {
Chris@101 913 // std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
Chris@76 914 continue;
Chris@76 915 }
Chris@101 916 // std::cerr << "adjusting to new point at " << i->frame << ", " << i->value << std::endl;
Chris@76 917 m_editingPoint = *i;
Chris@76 918 m_originalPoint = m_editingPoint;
Chris@76 919 m_editingCommand->deletePoint(m_editingPoint);
Chris@76 920 havePoint = true;
Chris@76 921 }
Chris@76 922 }
Chris@76 923
Chris@76 924 if (!havePoint) {
Chris@76 925 if (frame == m_editingPoint.frame) {
Chris@76 926 m_editingCommand->deletePoint(m_editingPoint);
Chris@76 927 }
Chris@76 928 }
Chris@76 929
Chris@76 930 // m_editingCommand->deletePoint(m_editingPoint);
Chris@21 931 m_editingPoint.frame = frame;
Chris@21 932 m_editingPoint.value = value;
Chris@22 933 m_editingCommand->addPoint(m_editingPoint);
Chris@21 934 }
Chris@21 935
Chris@21 936 void
Chris@248 937 TimeValueLayer::drawEnd(View *, QMouseEvent *)
Chris@21 938 {
Chris@101 939 // std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 940 if (!m_model || !m_editing) return;
Chris@22 941 m_editingCommand->finish();
Chris@22 942 m_editingCommand = 0;
Chris@21 943 m_editing = false;
Chris@21 944 }
Chris@21 945
Chris@21 946 void
Chris@335 947 TimeValueLayer::eraseStart(View *v, QMouseEvent *e)
Chris@335 948 {
Chris@335 949 if (!m_model) return;
Chris@335 950
Chris@335 951 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@335 952 if (points.empty()) return;
Chris@335 953
Chris@335 954 m_editingPoint = *points.begin();
Chris@335 955
Chris@335 956 if (m_editingCommand) {
Chris@335 957 m_editingCommand->finish();
Chris@335 958 m_editingCommand = 0;
Chris@335 959 }
Chris@335 960
Chris@335 961 m_editing = true;
Chris@335 962 }
Chris@335 963
Chris@335 964 void
Chris@335 965 TimeValueLayer::eraseDrag(View *v, QMouseEvent *e)
Chris@335 966 {
Chris@335 967 }
Chris@335 968
Chris@335 969 void
Chris@335 970 TimeValueLayer::eraseEnd(View *v, QMouseEvent *e)
Chris@335 971 {
Chris@335 972 if (!m_model || !m_editing) return;
Chris@335 973
Chris@335 974 m_editing = false;
Chris@335 975
Chris@335 976 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@335 977 if (points.empty()) return;
Chris@335 978 if (points.begin()->frame != m_editingPoint.frame ||
Chris@335 979 points.begin()->value != m_editingPoint.value) return;
Chris@335 980
Chris@335 981 m_editingCommand = new SparseTimeValueModel::EditCommand
Chris@335 982 (m_model, tr("Erase Point"));
Chris@335 983
Chris@335 984 m_editingCommand->deletePoint(m_editingPoint);
Chris@335 985
Chris@335 986 m_editingCommand->finish();
Chris@335 987 m_editingCommand = 0;
Chris@335 988 m_editing = false;
Chris@335 989 }
Chris@335 990
Chris@335 991 void
Chris@44 992 TimeValueLayer::editStart(View *v, QMouseEvent *e)
Chris@21 993 {
Chris@101 994 // std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 995
Chris@21 996 if (!m_model) return;
Chris@21 997
Chris@44 998 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@21 999 if (points.empty()) return;
Chris@21 1000
Chris@21 1001 m_editingPoint = *points.begin();
Chris@23 1002 m_originalPoint = m_editingPoint;
Chris@22 1003
Chris@22 1004 if (m_editingCommand) {
Chris@22 1005 m_editingCommand->finish();
Chris@22 1006 m_editingCommand = 0;
Chris@22 1007 }
Chris@22 1008
Chris@21 1009 m_editing = true;
Chris@21 1010 }
Chris@21 1011
Chris@21 1012 void
Chris@44 1013 TimeValueLayer::editDrag(View *v, QMouseEvent *e)
Chris@21 1014 {
Chris@101 1015 // std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 1016
Chris@21 1017 if (!m_model || !m_editing) return;
Chris@21 1018
Chris@44 1019 long frame = v->getFrameForX(e->x());
Chris@21 1020 if (frame < 0) frame = 0;
Chris@21 1021 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@21 1022
Chris@44 1023 float value = getValueForY(v, e->y());
Chris@21 1024
Chris@22 1025 if (!m_editingCommand) {
Chris@22 1026 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
Chris@22 1027 tr("Drag Point"));
Chris@22 1028 }
Chris@22 1029
Chris@22 1030 m_editingCommand->deletePoint(m_editingPoint);
Chris@21 1031 m_editingPoint.frame = frame;
Chris@21 1032 m_editingPoint.value = value;
Chris@22 1033 m_editingCommand->addPoint(m_editingPoint);
Chris@21 1034 }
Chris@21 1035
Chris@21 1036 void
Chris@248 1037 TimeValueLayer::editEnd(View *, QMouseEvent *)
Chris@21 1038 {
Chris@101 1039 // std::cerr << "TimeValueLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@21 1040 if (!m_model || !m_editing) return;
Chris@23 1041
Chris@23 1042 if (m_editingCommand) {
Chris@23 1043
Chris@23 1044 QString newName = m_editingCommand->getName();
Chris@23 1045
Chris@23 1046 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@23 1047 if (m_editingPoint.value != m_originalPoint.value) {
Chris@23 1048 newName = tr("Edit Point");
Chris@23 1049 } else {
Chris@23 1050 newName = tr("Relocate Point");
Chris@23 1051 }
Chris@23 1052 } else {
Chris@23 1053 newName = tr("Change Point Value");
Chris@23 1054 }
Chris@23 1055
Chris@23 1056 m_editingCommand->setName(newName);
Chris@23 1057 m_editingCommand->finish();
Chris@23 1058 }
Chris@23 1059
Chris@22 1060 m_editingCommand = 0;
Chris@21 1061 m_editing = false;
Chris@21 1062 }
Chris@21 1063
Chris@255 1064 bool
Chris@70 1065 TimeValueLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 1066 {
Chris@255 1067 if (!m_model) return false;
Chris@70 1068
Chris@70 1069 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
Chris@255 1070 if (points.empty()) return false;
Chris@70 1071
Chris@70 1072 SparseTimeValueModel::Point point = *points.begin();
Chris@70 1073
Chris@70 1074 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 1075 (m_model->getSampleRate(),
Chris@70 1076 ItemEditDialog::ShowTime |
Chris@70 1077 ItemEditDialog::ShowValue |
Chris@73 1078 ItemEditDialog::ShowText,
Chris@73 1079 m_model->getScaleUnits());
Chris@70 1080
Chris@70 1081 dialog->setFrameTime(point.frame);
Chris@70 1082 dialog->setValue(point.value);
Chris@70 1083 dialog->setText(point.label);
Chris@70 1084
Chris@70 1085 if (dialog->exec() == QDialog::Accepted) {
Chris@70 1086
Chris@70 1087 SparseTimeValueModel::Point newPoint = point;
Chris@70 1088 newPoint.frame = dialog->getFrameTime();
Chris@70 1089 newPoint.value = dialog->getValue();
Chris@70 1090 newPoint.label = dialog->getText();
Chris@70 1091
Chris@70 1092 SparseTimeValueModel::EditCommand *command =
Chris@70 1093 new SparseTimeValueModel::EditCommand(m_model, tr("Edit Point"));
Chris@70 1094 command->deletePoint(point);
Chris@70 1095 command->addPoint(newPoint);
Chris@70 1096 command->finish();
Chris@70 1097 }
Chris@70 1098
Chris@70 1099 delete dialog;
Chris@255 1100 return true;
Chris@70 1101 }
Chris@70 1102
Chris@70 1103 void
Chris@43 1104 TimeValueLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 1105 {
Chris@99 1106 if (!m_model) return;
Chris@99 1107
Chris@43 1108 SparseTimeValueModel::EditCommand *command =
Chris@43 1109 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 1110 tr("Drag Selection"));
Chris@43 1111
Chris@43 1112 SparseTimeValueModel::PointList points =
Chris@43 1113 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1114
Chris@43 1115 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 1116 i != points.end(); ++i) {
Chris@43 1117
Chris@43 1118 if (s.contains(i->frame)) {
Chris@43 1119 SparseTimeValueModel::Point newPoint(*i);
Chris@43 1120 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 1121 command->deletePoint(*i);
Chris@43 1122 command->addPoint(newPoint);
Chris@43 1123 }
Chris@43 1124 }
Chris@43 1125
Chris@43 1126 command->finish();
Chris@43 1127 }
Chris@43 1128
Chris@43 1129 void
Chris@43 1130 TimeValueLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1131 {
Chris@99 1132 if (!m_model) return;
Chris@99 1133
Chris@43 1134 SparseTimeValueModel::EditCommand *command =
Chris@43 1135 new SparseTimeValueModel::EditCommand(m_model,
Chris@43 1136 tr("Resize Selection"));
Chris@43 1137
Chris@43 1138 SparseTimeValueModel::PointList points =
Chris@43 1139 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1140
Chris@43 1141 double ratio =
Chris@43 1142 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 1143 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1144
Chris@43 1145 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@43 1146 i != points.end(); ++i) {
Chris@43 1147
Chris@43 1148 if (s.contains(i->frame)) {
Chris@43 1149
Chris@43 1150 double target = i->frame;
Chris@43 1151 target = newSize.getStartFrame() +
Chris@43 1152 double(target - s.getStartFrame()) * ratio;
Chris@43 1153
Chris@43 1154 SparseTimeValueModel::Point newPoint(*i);
Chris@43 1155 newPoint.frame = lrint(target);
Chris@43 1156 command->deletePoint(*i);
Chris@43 1157 command->addPoint(newPoint);
Chris@43 1158 }
Chris@43 1159 }
Chris@43 1160
Chris@43 1161 command->finish();
Chris@43 1162 }
Chris@43 1163
Chris@76 1164 void
Chris@76 1165 TimeValueLayer::deleteSelection(Selection s)
Chris@76 1166 {
Chris@99 1167 if (!m_model) return;
Chris@99 1168
Chris@76 1169 SparseTimeValueModel::EditCommand *command =
Chris@76 1170 new SparseTimeValueModel::EditCommand(m_model,
Chris@76 1171 tr("Delete Selected Points"));
Chris@76 1172
Chris@76 1173 SparseTimeValueModel::PointList points =
Chris@76 1174 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1175
Chris@76 1176 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1177 i != points.end(); ++i) {
Chris@76 1178
Chris@76 1179 if (s.contains(i->frame)) {
Chris@76 1180 command->deletePoint(*i);
Chris@76 1181 }
Chris@76 1182 }
Chris@76 1183
Chris@76 1184 command->finish();
Chris@76 1185 }
Chris@76 1186
Chris@76 1187 void
Chris@359 1188 TimeValueLayer::copy(View *v, Selection s, Clipboard &to)
Chris@76 1189 {
Chris@99 1190 if (!m_model) return;
Chris@99 1191
Chris@76 1192 SparseTimeValueModel::PointList points =
Chris@76 1193 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1194
Chris@76 1195 for (SparseTimeValueModel::PointList::iterator i = points.begin();
Chris@76 1196 i != points.end(); ++i) {
Chris@76 1197 if (s.contains(i->frame)) {
Chris@83 1198 Clipboard::Point point(i->frame, i->value, i->label);
Chris@335 1199 point.setReferenceFrame(m_model->alignToReference(i->frame));
Chris@76 1200 to.addPoint(point);
Chris@76 1201 }
Chris@76 1202 }
Chris@76 1203 }
Chris@76 1204
Chris@125 1205 bool
Chris@359 1206 TimeValueLayer::paste(View *v, const Clipboard &from, int frameOffset,
Chris@125 1207 bool interactive)
Chris@76 1208 {
Chris@125 1209 if (!m_model) return false;
Chris@99 1210
Chris@76 1211 const Clipboard::PointList &points = from.getPoints();
Chris@76 1212
Chris@76 1213 SparseTimeValueModel::EditCommand *command =
Chris@76 1214 new SparseTimeValueModel::EditCommand(m_model, tr("Paste"));
Chris@76 1215
Chris@125 1216 enum ValueAvailability {
Chris@125 1217 UnknownAvailability,
Chris@125 1218 NoValues,
Chris@125 1219 SomeValues,
Chris@125 1220 AllValues
Chris@125 1221 };
Chris@125 1222
Chris@340 1223 Labeller::ValueType generation = Labeller::ValueNone;
Chris@125 1224
Chris@125 1225 bool haveUsableLabels = false;
Chris@125 1226 bool haveExistingItems = !(m_model->isEmpty());
Chris@340 1227 Labeller labeller;
Chris@340 1228 labeller.setSampleRate(m_model->getSampleRate());
Chris@125 1229
Chris@125 1230 if (interactive) {
Chris@125 1231
Chris@125 1232 ValueAvailability availability = UnknownAvailability;
Chris@125 1233
Chris@125 1234 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@125 1235 i != points.end(); ++i) {
Chris@125 1236
Chris@125 1237 if (!i->haveFrame()) continue;
Chris@125 1238
Chris@125 1239 if (availability == UnknownAvailability) {
Chris@125 1240 if (i->haveValue()) availability = AllValues;
Chris@125 1241 else availability = NoValues;
Chris@125 1242 continue;
Chris@125 1243 }
Chris@125 1244
Chris@125 1245 if (i->haveValue()) {
Chris@125 1246 if (availability == NoValues) {
Chris@125 1247 availability = SomeValues;
Chris@125 1248 }
Chris@125 1249 } else {
Chris@125 1250 if (availability == AllValues) {
Chris@125 1251 availability = SomeValues;
Chris@125 1252 }
Chris@125 1253 }
Chris@125 1254
Chris@125 1255 if (!haveUsableLabels) {
Chris@125 1256 if (i->haveLabel()) {
Chris@125 1257 if (i->getLabel().contains(QRegExp("[0-9]"))) {
Chris@125 1258 haveUsableLabels = true;
Chris@125 1259 }
Chris@125 1260 }
Chris@125 1261 }
Chris@125 1262
Chris@125 1263 if (availability == SomeValues && haveUsableLabels) break;
Chris@125 1264 }
Chris@125 1265
Chris@125 1266 if (availability == NoValues || availability == SomeValues) {
Chris@125 1267
Chris@125 1268 QString text;
Chris@125 1269 if (availability == NoValues) {
Chris@125 1270 text = tr("The items you are pasting do not have values.\nWhat values do you want to use for these items?");
Chris@125 1271 } else {
Chris@125 1272 text = tr("Some of the items you are pasting do not have values.\nWhat values do you want to use for these items?");
Chris@125 1273 }
Chris@125 1274
Chris@340 1275 Labeller::TypeNameMap names = labeller.getTypeNames();
Chris@340 1276
Chris@125 1277 QStringList options;
Chris@340 1278 std::vector<Labeller::ValueType> genopts;
Chris@125 1279
Chris@340 1280 for (Labeller::TypeNameMap::const_iterator i = names.begin();
Chris@340 1281 i != names.end(); ++i) {
Chris@340 1282 if (i->first == Labeller::ValueNone) options << tr("Zero for all items");
Chris@340 1283 else options << i->second;
Chris@340 1284 genopts.push_back(i->first);
Chris@125 1285 }
Chris@125 1286
Chris@125 1287 static int prevSelection = 0;
Chris@125 1288
Chris@125 1289 bool ok = false;
Chris@125 1290 QString selected = ListInputDialog::getItem
Chris@125 1291 (0, tr("Choose value calculation"),
Chris@125 1292 text, options, prevSelection, &ok);
Chris@125 1293
Chris@125 1294 if (!ok) return false;
Chris@125 1295 int selection = 0;
Chris@340 1296 generation = Labeller::ValueNone;
Chris@125 1297
Chris@125 1298 for (QStringList::const_iterator i = options.begin();
Chris@125 1299 i != options.end(); ++i) {
Chris@125 1300 if (selected == *i) {
Chris@340 1301 generation = genopts[selection];
Chris@125 1302 break;
Chris@125 1303 }
Chris@125 1304 ++selection;
Chris@125 1305 }
Chris@340 1306
Chris@340 1307 labeller.setType(generation);
Chris@340 1308
Chris@340 1309 if (generation == Labeller::ValueFromCyclicalCounter ||
Chris@340 1310 generation == Labeller::ValueFromTwoLevelCounter) {
Chris@340 1311 int cycleSize = QInputDialog::getInteger
Chris@340 1312 (0, tr("Select cycle size"),
Chris@340 1313 tr("Cycle size:"), 4, 2, 16, 1);
Chris@340 1314 labeller.setCounterCycleSize(cycleSize);
Chris@340 1315 }
Chris@125 1316
Chris@125 1317 prevSelection = selection;
Chris@125 1318 }
Chris@125 1319 }
Chris@125 1320
Chris@340 1321 static SparseTimeValueModel::Point prevPoint(0);
Chris@125 1322
Chris@76 1323 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 1324 i != points.end(); ++i) {
Chris@76 1325
Chris@76 1326 if (!i->haveFrame()) continue;
Chris@76 1327 size_t frame = 0;
Chris@76 1328 if (frameOffset > 0 || -frameOffset < i->getFrame()) {
Chris@76 1329 frame = i->getFrame() + frameOffset;
Chris@76 1330 }
Chris@76 1331 SparseTimeValueModel::Point newPoint(frame);
Chris@76 1332
Chris@125 1333 if (i->haveLabel()) {
Chris@125 1334 newPoint.label = i->getLabel();
Chris@125 1335 } else if (i->haveValue()) {
Chris@125 1336 newPoint.label = QString("%1").arg(i->getValue());
Chris@125 1337 }
Chris@125 1338
Chris@125 1339 if (i->haveValue()) {
Chris@125 1340 newPoint.value = i->getValue();
Chris@125 1341 } else {
Chris@340 1342 labeller.setValue<SparseTimeValueModel::Point>
Chris@340 1343 (newPoint, i == points.begin() ? 0 : &prevPoint);
Chris@340 1344 }
Chris@125 1345
Chris@340 1346 prevPoint = newPoint;
Chris@76 1347 command->addPoint(newPoint);
Chris@76 1348 }
Chris@76 1349
Chris@76 1350 command->finish();
Chris@125 1351 return true;
Chris@340 1352 }
Chris@76 1353
Chris@316 1354 void
Chris@316 1355 TimeValueLayer::toXml(QTextStream &stream,
Chris@316 1356 QString indent, QString extraAttributes) const
Chris@6 1357 {
Chris@316 1358 SingleColourLayer::toXml(stream, indent,
Chris@316 1359 extraAttributes +
Chris@316 1360 QString(" colourMap=\"%1\" plotStyle=\"%2\" verticalScale=\"%3\"")
Chris@316 1361 .arg(m_colourMap)
Chris@316 1362 .arg(m_plotStyle)
Chris@316 1363 .arg(m_verticalScale));
Chris@0 1364 }
Chris@0 1365
Chris@11 1366 void
Chris@11 1367 TimeValueLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 1368 {
Chris@287 1369 SingleColourLayer::setProperties(attributes);
Chris@11 1370
Chris@11 1371 bool ok;
Chris@287 1372
Chris@287 1373 int cmap = attributes.value("colourMap").toInt(&ok);
Chris@287 1374 if (ok) setFillColourMap(cmap);
Chris@287 1375
Chris@11 1376 PlotStyle style = (PlotStyle)
Chris@11 1377 attributes.value("plotStyle").toInt(&ok);
Chris@11 1378 if (ok) setPlotStyle(style);
Chris@286 1379
Chris@286 1380 VerticalScale scale = (VerticalScale)
Chris@286 1381 attributes.value("verticalScale").toInt(&ok);
Chris@286 1382 if (ok) setVerticalScale(scale);
Chris@11 1383 }
Chris@11 1384