annotate layer/TimeInstantLayer.cpp @ 17:0183ebb725ca

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