annotate layer/TimeInstantLayer.cpp @ 18:7c767d41bcee

* Add basic drag editing of time instants. Plenty of tuning to do for both this and drawing.
author Chris Cannam
date Thu, 26 Jan 2006 18:01:38 +0000
parents 0183ebb725ca
children 1deb5f87a18c
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 A waveform viewer and audio annotation editor.
Chris@5 5 Chris Cannam, Queen Mary University of London, 2005-2006
Chris@0 6
Chris@0 7 This is experimental software. Not for distribution.
Chris@0 8 */
Chris@0 9
Chris@0 10 #include "TimeInstantLayer.h"
Chris@0 11
Chris@0 12 #include "base/Model.h"
Chris@0 13 #include "base/RealTime.h"
Chris@0 14 #include "base/View.h"
Chris@0 15 #include "base/Profiler.h"
Chris@0 16
Chris@0 17 #include "model/SparseOneDimensionalModel.h"
Chris@0 18
Chris@0 19 #include <QPainter>
Chris@17 20 #include <QMouseEvent>
Chris@0 21
Chris@0 22 #include <iostream>
Chris@0 23
Chris@0 24 TimeInstantLayer::TimeInstantLayer(View *w) :
Chris@0 25 Layer(w),
Chris@0 26 m_model(0),
Chris@18 27 m_editing(false),
Chris@17 28 m_editingPoint(0, tr("New Point")),
Chris@0 29 m_colour(QColor(200, 50, 255))
Chris@0 30 {
Chris@0 31 m_view->addLayer(this);
Chris@0 32 }
Chris@0 33
Chris@0 34 void
Chris@0 35 TimeInstantLayer::setModel(SparseOneDimensionalModel *model)
Chris@0 36 {
Chris@0 37 if (m_model == model) return;
Chris@0 38 m_model = model;
Chris@0 39
Chris@0 40 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@0 41 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 42 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@0 43
Chris@0 44 connect(m_model, SIGNAL(completionChanged()),
Chris@0 45 this, SIGNAL(modelCompletionChanged()));
Chris@0 46
Chris@0 47 std::cerr << "TimeInstantLayer::setModel(" << model << ")" << std::endl;
Chris@0 48
Chris@0 49 emit modelReplaced();
Chris@0 50 }
Chris@0 51
Chris@0 52 Layer::PropertyList
Chris@0 53 TimeInstantLayer::getProperties() const
Chris@0 54 {
Chris@0 55 PropertyList list;
Chris@0 56 list.push_back(tr("Colour"));
Chris@0 57 return list;
Chris@0 58 }
Chris@0 59
Chris@0 60 Layer::PropertyType
Chris@0 61 TimeInstantLayer::getPropertyType(const PropertyName &name) const
Chris@0 62 {
Chris@0 63 return ValueProperty;
Chris@0 64 }
Chris@0 65
Chris@0 66 int
Chris@0 67 TimeInstantLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@0 68 int *min, int *max) const
Chris@0 69 {
Chris@0 70 int deft = 0;
Chris@0 71
Chris@0 72 if (name == tr("Colour")) {
Chris@0 73
Chris@10 74 if (min) *min = 0;
Chris@10 75 if (max) *max = 5;
Chris@0 76
Chris@0 77 if (m_colour == Qt::black) deft = 0;
Chris@0 78 else if (m_colour == Qt::darkRed) deft = 1;
Chris@0 79 else if (m_colour == Qt::darkBlue) deft = 2;
Chris@0 80 else if (m_colour == Qt::darkGreen) deft = 3;
Chris@0 81 else if (m_colour == QColor(200, 50, 255)) deft = 4;
Chris@0 82 else if (m_colour == QColor(255, 150, 50)) deft = 5;
Chris@0 83
Chris@0 84 } else {
Chris@0 85
Chris@0 86 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@0 87 }
Chris@0 88
Chris@0 89 return deft;
Chris@0 90 }
Chris@0 91
Chris@0 92 QString
Chris@0 93 TimeInstantLayer::getPropertyValueLabel(const PropertyName &name,
Chris@0 94 int value) const
Chris@0 95 {
Chris@0 96 if (name == tr("Colour")) {
Chris@0 97 switch (value) {
Chris@0 98 default:
Chris@0 99 case 0: return tr("Black");
Chris@0 100 case 1: return tr("Red");
Chris@0 101 case 2: return tr("Blue");
Chris@0 102 case 3: return tr("Green");
Chris@0 103 case 4: return tr("Purple");
Chris@0 104 case 5: return tr("Orange");
Chris@0 105 }
Chris@0 106 }
Chris@0 107 return tr("<unknown>");
Chris@0 108 }
Chris@0 109
Chris@0 110 void
Chris@0 111 TimeInstantLayer::setProperty(const PropertyName &name, int value)
Chris@0 112 {
Chris@0 113 if (name == tr("Colour")) {
Chris@0 114 switch (value) {
Chris@0 115 default:
Chris@0 116 case 0: setBaseColour(Qt::black); break;
Chris@0 117 case 1: setBaseColour(Qt::darkRed); break;
Chris@0 118 case 2: setBaseColour(Qt::darkBlue); break;
Chris@0 119 case 3: setBaseColour(Qt::darkGreen); break;
Chris@0 120 case 4: setBaseColour(QColor(200, 50, 255)); break;
Chris@0 121 case 5: setBaseColour(QColor(255, 150, 50)); break;
Chris@0 122 }
Chris@0 123 }
Chris@0 124 }
Chris@0 125
Chris@0 126 void
Chris@0 127 TimeInstantLayer::setBaseColour(QColor colour)
Chris@0 128 {
Chris@0 129 if (m_colour == colour) return;
Chris@0 130 m_colour = colour;
Chris@0 131 emit layerParametersChanged();
Chris@0 132 }
Chris@0 133
Chris@0 134 bool
Chris@0 135 TimeInstantLayer::isLayerScrollable() const
Chris@0 136 {
Chris@0 137 QPoint discard;
Chris@0 138 return !m_view->shouldIlluminateLocalFeatures(this, discard);
Chris@0 139 }
Chris@0 140
Chris@0 141 QRect
Chris@0 142 TimeInstantLayer::getFeatureDescriptionRect(QPainter &paint, QPoint pos) const
Chris@0 143 {
Chris@0 144 return QRect(0, 0,
Chris@0 145 std::max(100, paint.fontMetrics().width(tr("No local points"))),
Chris@0 146 50); //!!! cruddy
Chris@0 147 }
Chris@0 148
Chris@0 149 SparseOneDimensionalModel::PointList
Chris@0 150 TimeInstantLayer::getLocalPoints(int x) const
Chris@0 151 {
Chris@0 152 if (!m_model) return SparseOneDimensionalModel::PointList();
Chris@0 153
Chris@0 154 long startFrame = m_view->getStartFrame();
Chris@0 155 long endFrame = m_view->getEndFrame();
Chris@0 156 int zoomLevel = m_view->getZoomLevel();
Chris@0 157 long frame = startFrame + x * zoomLevel;
Chris@0 158
Chris@0 159 SparseOneDimensionalModel::PointList onPoints =
Chris@0 160 m_model->getPoints(frame);
Chris@0 161
Chris@0 162 if (!onPoints.empty()) {
Chris@0 163 return onPoints;
Chris@0 164 }
Chris@0 165
Chris@0 166 SparseOneDimensionalModel::PointList prevPoints =
Chris@0 167 m_model->getPreviousPoints(frame);
Chris@0 168 SparseOneDimensionalModel::PointList nextPoints =
Chris@0 169 m_model->getNextPoints(frame);
Chris@0 170
Chris@0 171 SparseOneDimensionalModel::PointList usePoints = prevPoints;
Chris@0 172
Chris@0 173 if (prevPoints.empty()) {
Chris@0 174 usePoints = nextPoints;
Chris@0 175 } else if (prevPoints.begin()->frame < startFrame &&
Chris@0 176 !(nextPoints.begin()->frame > endFrame)) {
Chris@0 177 usePoints = nextPoints;
Chris@0 178 } else if (nextPoints.begin()->frame - frame <
Chris@0 179 frame - prevPoints.begin()->frame) {
Chris@0 180 usePoints = nextPoints;
Chris@0 181 }
Chris@0 182
Chris@0 183 return usePoints;
Chris@0 184 }
Chris@0 185
Chris@0 186 void
Chris@0 187 TimeInstantLayer::paintLocalFeatureDescription(QPainter &paint, QRect rect,
Chris@0 188 QPoint pos) const
Chris@0 189 {
Chris@0 190 //!!! bleagh
Chris@0 191
Chris@0 192 int x = pos.x();
Chris@0 193
Chris@0 194 if (!m_model || !m_model->getSampleRate()) return;
Chris@0 195
Chris@0 196 SparseOneDimensionalModel::PointList points = getLocalPoints(x);
Chris@0 197
Chris@0 198 QFontMetrics metrics = paint.fontMetrics();
Chris@0 199 int xbase = rect.x() + 5;
Chris@0 200 int ybase = rect.y() + 5;
Chris@0 201
Chris@0 202 if (points.empty()) {
Chris@0 203 QString label = tr("No local points");
Chris@0 204 if (!m_model->isReady()) {
Chris@0 205 label = tr("In progress");
Chris@0 206 }
Chris@0 207 paint.drawText(xbase + 5, ybase + 5 + metrics.ascent(), label);
Chris@0 208 return;
Chris@0 209 }
Chris@0 210
Chris@0 211 long useFrame = points.begin()->frame;
Chris@0 212
Chris@0 213 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
Chris@2 214 QString timeText = QString(tr("Time %1")).arg(rt.toText(true).c_str());
Chris@0 215
Chris@0 216 int timewidth = metrics.width(timeText);
Chris@0 217 int labelwidth = metrics.width(points.begin()->label);
Chris@0 218
Chris@0 219 int boxheight = metrics.height() * 2 + 3;
Chris@0 220 int boxwidth = std::max(timewidth, labelwidth);
Chris@0 221
Chris@0 222 paint.drawRect(xbase, ybase, boxwidth + 10,
Chris@0 223 boxheight + 10 - metrics.descent() + 1);
Chris@0 224
Chris@0 225 paint.drawText(xbase + 5, ybase + 5 + metrics.ascent(), timeText);
Chris@0 226 paint.drawText(xbase + 5, ybase + 7 + metrics.ascent() + metrics.height(),
Chris@0 227 points.begin()->label);
Chris@0 228 }
Chris@0 229
Chris@13 230 int
Chris@13 231 TimeInstantLayer::getNearestFeatureFrame(int frame,
Chris@13 232 size_t &resolution,
Chris@13 233 bool snapRight) const
Chris@13 234 {
Chris@13 235 if (!m_model) {
Chris@13 236 return Layer::getNearestFeatureFrame(frame, resolution, snapRight);
Chris@13 237 }
Chris@13 238
Chris@13 239 resolution = m_model->getResolution();
Chris@13 240 SparseOneDimensionalModel::PointList points(m_model->getPoints(frame, frame));
Chris@13 241
Chris@13 242 int returnFrame = frame;
Chris@13 243
Chris@13 244 for (SparseOneDimensionalModel::PointList::const_iterator i = points.begin();
Chris@13 245 i != points.end(); ++i) {
Chris@13 246
Chris@13 247 if (snapRight) {
Chris@13 248 if (i->frame > frame) {
Chris@13 249 returnFrame = i->frame;
Chris@13 250 break;
Chris@13 251 }
Chris@13 252 } else {
Chris@13 253 if (i->frame <= frame) {
Chris@13 254 returnFrame = i->frame;
Chris@13 255 }
Chris@13 256 }
Chris@13 257 }
Chris@13 258
Chris@13 259 return returnFrame;
Chris@13 260 }
Chris@13 261
Chris@0 262 void
Chris@0 263 TimeInstantLayer::paint(QPainter &paint, QRect rect) const
Chris@0 264 {
Chris@0 265 if (!m_model || !m_model->isOK()) return;
Chris@0 266
Chris@0 267 // Profiler profiler("TimeInstantLayer::paint", true);
Chris@0 268
Chris@0 269 long startFrame = m_view->getStartFrame();
Chris@0 270 int zoomLevel = m_view->getZoomLevel();
Chris@0 271
Chris@0 272 int x0 = rect.left(), x1 = rect.right();
Chris@0 273 long frame0 = startFrame + x0 * zoomLevel;
Chris@0 274 long frame1 = startFrame + x1 * zoomLevel;
Chris@0 275
Chris@0 276 SparseOneDimensionalModel::PointList points(m_model->getPoints
Chris@0 277 (frame0, frame1));
Chris@0 278
Chris@0 279 paint.setPen(m_colour);
Chris@0 280
Chris@0 281 QColor brushColour(m_colour);
Chris@0 282 brushColour.setAlpha(100);
Chris@0 283 paint.setBrush(brushColour);
Chris@0 284
Chris@0 285 // std::cerr << "TimeInstantLayer::paint: resolution is "
Chris@0 286 // << m_model->getResolution() << " frames" << std::endl;
Chris@0 287
Chris@0 288 QPoint localPos;
Chris@0 289 long illuminateFrame = -1;
Chris@0 290
Chris@0 291 if (m_view->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@0 292 SparseOneDimensionalModel::PointList localPoints =
Chris@0 293 getLocalPoints(localPos.x());
Chris@0 294 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@0 295 }
Chris@0 296
Chris@0 297 for (SparseOneDimensionalModel::PointList::const_iterator i = points.begin();
Chris@0 298 i != points.end(); ++i) {
Chris@0 299
Chris@0 300 const SparseOneDimensionalModel::Point &p(*i);
Chris@17 301 SparseOneDimensionalModel::PointList::const_iterator j = i;
Chris@17 302 ++j;
Chris@0 303
Chris@0 304 int x = (p.frame - startFrame) / zoomLevel;
Chris@16 305 float w = float(m_model->getResolution()) / zoomLevel;
Chris@16 306 int iw = w;
Chris@16 307 if (iw < 2) {
Chris@17 308 if (iw < 1) {
Chris@17 309 iw = 2;
Chris@17 310 if (j != points.end()) {
Chris@17 311 int nx = ((*j).frame - startFrame) / zoomLevel;
Chris@17 312 if (nx < x + 3) iw = 1;
Chris@17 313 }
Chris@17 314 } else {
Chris@17 315 iw = 2;
Chris@17 316 }
Chris@16 317 }
Chris@0 318
Chris@0 319 if (p.frame == illuminateFrame) {
Chris@0 320 paint.setPen(Qt::black); //!!!
Chris@0 321 } else {
Chris@0 322 paint.setPen(brushColour);
Chris@0 323 }
Chris@16 324 paint.drawRect(x, 0, iw - 1, m_view->height() - 1);
Chris@0 325 paint.setPen(m_colour);
Chris@0 326
Chris@0 327 if (p.label != "") {
Chris@0 328
Chris@0 329 // only draw if there's enough room from here to the next point
Chris@0 330
Chris@0 331 int lw = paint.fontMetrics().width(p.label);
Chris@0 332 bool good = true;
Chris@0 333
Chris@17 334 if (j != points.end()) {
Chris@0 335 int nx = (j->frame - startFrame) / zoomLevel;
Chris@0 336 if (nx >= x && nx - x - w - 3 <= lw) good = false;
Chris@0 337 }
Chris@0 338
Chris@0 339 if (good) {
Chris@0 340 paint.drawText(x + w + 2,
Chris@0 341 m_view->height() - paint.fontMetrics().height(),
Chris@0 342 p.label);
Chris@0 343 }
Chris@0 344 }
Chris@0 345 }
Chris@0 346 }
Chris@0 347
Chris@17 348 void
Chris@17 349 TimeInstantLayer::drawStart(QMouseEvent *e)
Chris@17 350 {
Chris@17 351 std::cerr << "TimeInstantLayer::drawStart(" << e->x() << ")" << std::endl;
Chris@17 352
Chris@17 353 if (!m_model) return;
Chris@17 354
Chris@17 355 long frame = e->x() * m_view->getZoomLevel() + m_view->getStartFrame();
Chris@17 356 if (frame < 0) frame = 0;
Chris@17 357 m_editingPoint = SparseOneDimensionalModel::Point(frame, tr("New Point"));
Chris@17 358 m_model->addPoint(m_editingPoint);
Chris@18 359 m_editing = true;
Chris@17 360 }
Chris@17 361
Chris@17 362 void
Chris@17 363 TimeInstantLayer::drawDrag(QMouseEvent *e)
Chris@17 364 {
Chris@17 365 std::cerr << "TimeInstantLayer::drawDrag(" << e->x() << ")" << std::endl;
Chris@17 366
Chris@18 367 if (!m_model || !m_editing) return;
Chris@17 368
Chris@17 369 long frame = e->x() * m_view->getZoomLevel() + m_view->getStartFrame();
Chris@17 370 if (frame < 0) frame = 0;
Chris@17 371 m_model->deletePoint(m_editingPoint);
Chris@17 372 m_editingPoint.frame = frame;
Chris@17 373 m_model->addPoint(m_editingPoint);
Chris@17 374 }
Chris@17 375
Chris@17 376 void
Chris@17 377 TimeInstantLayer::drawEnd(QMouseEvent *e)
Chris@17 378 {
Chris@17 379 std::cerr << "TimeInstantLayer::drawEnd(" << e->x() << ")" << std::endl;
Chris@18 380 if (!m_model || !m_editing) return;
Chris@18 381 m_editing = false;
Chris@18 382 }
Chris@18 383
Chris@18 384 void
Chris@18 385 TimeInstantLayer::editStart(QMouseEvent *e)
Chris@18 386 {
Chris@18 387 std::cerr << "TimeInstantLayer::editStart(" << e->x() << ")" << std::endl;
Chris@18 388
Chris@17 389 if (!m_model) return;
Chris@18 390
Chris@18 391 SparseOneDimensionalModel::PointList points = getLocalPoints(e->x());
Chris@18 392 if (points.empty()) return;
Chris@18 393
Chris@18 394 m_editingPoint = *points.begin();
Chris@18 395 m_editing = true;
Chris@18 396 }
Chris@18 397
Chris@18 398 void
Chris@18 399 TimeInstantLayer::editDrag(QMouseEvent *e)
Chris@18 400 {
Chris@18 401 std::cerr << "TimeInstantLayer::editDrag(" << e->x() << ")" << std::endl;
Chris@18 402
Chris@18 403 if (!m_model || !m_editing) return;
Chris@18 404
Chris@18 405 long frame = e->x() * m_view->getZoomLevel() + m_view->getStartFrame();
Chris@18 406 if (frame < 0) frame = 0;
Chris@18 407 m_model->deletePoint(m_editingPoint);
Chris@18 408 m_editingPoint.frame = frame;
Chris@18 409 m_model->addPoint(m_editingPoint);
Chris@18 410 }
Chris@18 411
Chris@18 412 void
Chris@18 413 TimeInstantLayer::editEnd(QMouseEvent *e)
Chris@18 414 {
Chris@18 415 std::cerr << "TimeInstantLayer::editEnd(" << e->x() << ")" << std::endl;
Chris@18 416 if (!m_model || !m_editing) return;
Chris@18 417 m_editing = false;
Chris@17 418 }
Chris@17 419
Chris@6 420 QString
Chris@6 421 TimeInstantLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 422 {
Chris@6 423 return Layer::toXmlString(indent, extraAttributes +
Chris@6 424 QString(" colour=\"%1\"").arg(encodeColour(m_colour)));
Chris@6 425 }
Chris@0 426
Chris@11 427 void
Chris@11 428 TimeInstantLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 429 {
Chris@11 430 QString colourSpec = attributes.value("colour");
Chris@11 431 if (colourSpec != "") {
Chris@11 432 QColor colour(colourSpec);
Chris@11 433 if (colour.isValid()) {
Chris@11 434 setBaseColour(QColor(colourSpec));
Chris@11 435 }
Chris@11 436 }
Chris@11 437 }
Chris@11 438
Chris@0 439 #ifdef INCLUDE_MOCFILES
Chris@0 440 #include "TimeInstantLayer.moc.cpp"
Chris@0 441 #endif
Chris@0 442