annotate layer/Layer.h @ 280:3c402c6052f6

* Pull peak-picker out of SpectrumLayer and into FFTModel; use combined peak-picker and frequency estimator for SpectrogramLayer (makes the peak frequency spectrogram a bit quicker) * Add more information to spectrum and spectrogram crosshairs
author Chris Cannam
date Wed, 04 Jul 2007 15:29:16 +0000
parents b9380f679f70
children 86a112b5b319
rev   line source
Chris@127 1
Chris@127 2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@127 3
Chris@127 4 /*
Chris@127 5 Sonic Visualiser
Chris@127 6 An audio file viewer and annotation editor.
Chris@127 7 Centre for Digital Music, Queen Mary, University of London.
Chris@182 8 This file copyright 2006 Chris Cannam and QMUL.
Chris@127 9
Chris@127 10 This program is free software; you can redistribute it and/or
Chris@127 11 modify it under the terms of the GNU General Public License as
Chris@127 12 published by the Free Software Foundation; either version 2 of the
Chris@127 13 License, or (at your option) any later version. See the file
Chris@127 14 COPYING included with this distribution for more information.
Chris@127 15 */
Chris@127 16
Chris@127 17 #ifndef _LAYER_H_
Chris@127 18 #define _LAYER_H_
Chris@127 19
Chris@128 20 #include "base/PropertyContainer.h"
Chris@128 21 #include "base/XmlExportable.h"
Chris@128 22 #include "base/Selection.h"
Chris@127 23
Chris@127 24 #include <QObject>
Chris@127 25 #include <QRect>
Chris@127 26 #include <QXmlAttributes>
Chris@131 27 #include <QMutex>
Chris@127 28
Chris@127 29 #include <map>
Chris@268 30 #include <set>
Chris@127 31
Chris@127 32 class ZoomConstraint;
Chris@127 33 class Model;
Chris@127 34 class QPainter;
Chris@127 35 class View;
Chris@127 36 class QMouseEvent;
Chris@127 37 class Clipboard;
Chris@187 38 class RangeMapper;
Chris@127 39
Chris@127 40 /**
Chris@127 41 * The base class for visual representations of the data found in a
Chris@127 42 * Model. Layers are expected to be able to draw themselves onto a
Chris@127 43 * View, and may also be editable.
Chris@127 44 */
Chris@127 45
Chris@127 46 class Layer : public PropertyContainer,
Chris@127 47 public XmlExportable
Chris@127 48 {
Chris@127 49 Q_OBJECT
Chris@127 50
Chris@127 51 public:
Chris@127 52 Layer();
Chris@127 53 virtual ~Layer();
Chris@127 54
Chris@127 55 virtual const Model *getModel() const = 0;
Chris@127 56 virtual Model *getModel() {
Chris@127 57 return const_cast<Model *>(const_cast<const Layer *>(this)->getModel());
Chris@127 58 }
Chris@127 59
Chris@137 60 /**
Chris@137 61 * Return a zoom constraint object defining the supported zoom
Chris@137 62 * levels for this layer. If this returns zero, the layer will
Chris@137 63 * support any integer zoom level.
Chris@137 64 */
Chris@127 65 virtual const ZoomConstraint *getZoomConstraint() const { return 0; }
Chris@137 66
Chris@137 67 /**
Chris@137 68 * Return true if this layer can handle zoom levels other than
Chris@137 69 * those supported by its zoom constraint (presumably less
Chris@137 70 * efficiently or accurately than the officially supported zoom
Chris@137 71 * levels). If true, the layer will unenthusistically accept any
Chris@137 72 * integer zoom level from 1 to the maximum returned by its zoom
Chris@137 73 * constraint.
Chris@137 74 */
Chris@137 75 virtual bool supportsOtherZoomLevels() const { return true; }
Chris@137 76
Chris@127 77 virtual void paint(View *, QPainter &, QRect) const = 0;
Chris@127 78
Chris@127 79 enum VerticalPosition {
Chris@127 80 PositionTop, PositionMiddle, PositionBottom
Chris@127 81 };
Chris@127 82 virtual VerticalPosition getPreferredTimeRulerPosition() const {
Chris@127 83 return PositionMiddle;
Chris@127 84 }
Chris@127 85 virtual VerticalPosition getPreferredFrameCountPosition() const {
Chris@127 86 return PositionBottom;
Chris@127 87 }
Chris@224 88 virtual bool hasLightBackground() const {
Chris@224 89 return true;
Chris@224 90 }
Chris@127 91
Chris@127 92 virtual QString getPropertyContainerIconName() const;
Chris@127 93
Chris@127 94 virtual QString getPropertyContainerName() const {
Chris@127 95 return objectName();
Chris@127 96 }
Chris@127 97
Chris@127 98 virtual QString getLayerPresentationName() const;
Chris@127 99
Chris@127 100 virtual int getVerticalScaleWidth(View *, QPainter &) const { return 0; }
Chris@127 101 virtual void paintVerticalScale(View *, QPainter &, QRect) const { }
Chris@127 102
Chris@127 103 virtual bool getCrosshairExtents(View *, QPainter &, QPoint /* cursorPos */,
Chris@127 104 std::vector<QRect> &) const {
Chris@127 105 return false;
Chris@127 106 }
Chris@127 107 virtual void paintCrosshairs(View *, QPainter &, QPoint) const { }
Chris@127 108
Chris@272 109 virtual void paintMeasurementRects(View *, QPainter &,
Chris@272 110 bool showFocus, QPoint focusPoint) const;
Chris@272 111
Chris@272 112 virtual bool nearestMeasurementRectChanged(View *, QPoint prev,
Chris@272 113 QPoint now) const;
Chris@267 114
Chris@127 115 virtual QString getFeatureDescription(View *, QPoint &) const {
Chris@127 116 return "";
Chris@127 117 }
Chris@127 118
Chris@127 119 enum SnapType {
Chris@127 120 SnapLeft,
Chris@127 121 SnapRight,
Chris@127 122 SnapNearest,
Chris@127 123 SnapNeighbouring
Chris@127 124 };
Chris@127 125
Chris@127 126 /**
Chris@127 127 * Adjust the given frame to snap to the nearest feature, if
Chris@127 128 * possible.
Chris@127 129 *
Chris@127 130 * If snap is SnapLeft or SnapRight, adjust the frame to match
Chris@127 131 * that of the nearest feature in the given direction regardless
Chris@127 132 * of how far away it is. If snap is SnapNearest, adjust the
Chris@127 133 * frame to that of the nearest feature in either direction. If
Chris@127 134 * snap is SnapNeighbouring, adjust the frame to that of the
Chris@127 135 * nearest feature if it is close, and leave it alone (returning
Chris@127 136 * false) otherwise. SnapNeighbouring should always choose the
Chris@127 137 * same feature that would be used in an editing operation through
Chris@127 138 * calls to editStart etc.
Chris@127 139 *
Chris@127 140 * Return true if a suitable feature was found and frame adjusted
Chris@180 141 * accordingly. Return false if no suitable feature was available
Chris@180 142 * (and leave frame unmodified). Also return the resolution of
Chris@180 143 * the model in this layer in sample frames.
Chris@127 144 */
Chris@127 145 virtual bool snapToFeatureFrame(View * /* v */,
Chris@127 146 int & /* frame */,
Chris@127 147 size_t &resolution,
Chris@127 148 SnapType /* snap */) const {
Chris@127 149 resolution = 1;
Chris@127 150 return false;
Chris@127 151 }
Chris@127 152
Chris@127 153 // Draw and edit modes:
Chris@127 154 //
Chris@127 155 // Layer needs to get actual mouse events, I guess. Draw mode is
Chris@127 156 // probably the easier.
Chris@127 157
Chris@127 158 virtual void drawStart(View *, QMouseEvent *) { }
Chris@127 159 virtual void drawDrag(View *, QMouseEvent *) { }
Chris@127 160 virtual void drawEnd(View *, QMouseEvent *) { }
Chris@127 161
Chris@127 162 virtual void editStart(View *, QMouseEvent *) { }
Chris@127 163 virtual void editDrag(View *, QMouseEvent *) { }
Chris@127 164 virtual void editEnd(View *, QMouseEvent *) { }
Chris@127 165
Chris@267 166 // Measurement rectangle (or equivalent). Unlike draw and edit,
Chris@267 167 // the base Layer class can provide working implementations of
Chris@267 168 // these for most situations.
Chris@267 169 //
Chris@267 170 virtual void measureStart(View *, QMouseEvent *);
Chris@267 171 virtual void measureDrag(View *, QMouseEvent *);
Chris@267 172 virtual void measureEnd(View *, QMouseEvent *);
Chris@280 173 virtual void measureDoubleClick(View *, QMouseEvent *);
Chris@267 174
Chris@255 175 /**
Chris@255 176 * Open an editor on the item under the mouse (e.g. on
Chris@255 177 * double-click). If there is no item or editing is not
Chris@255 178 * supported, return false.
Chris@255 179 */
Chris@255 180 virtual bool editOpen(View *, QMouseEvent *) { return false; }
Chris@127 181
Chris@127 182 virtual void moveSelection(Selection, size_t /* newStartFrame */) { }
Chris@127 183 virtual void resizeSelection(Selection, Selection /* newSize */) { }
Chris@127 184 virtual void deleteSelection(Selection) { }
Chris@127 185
Chris@127 186 virtual void copy(Selection, Clipboard & /* to */) { }
Chris@127 187
Chris@127 188 /**
Chris@127 189 * Paste from the given clipboard onto the layer at the given
Chris@127 190 * frame offset. If interactive is true, the layer may ask the
Chris@127 191 * user about paste options through a dialog if desired, and may
Chris@127 192 * return false if the user cancelled the paste operation. This
Chris@127 193 * function should return true if a paste actually occurred.
Chris@127 194 */
Chris@127 195 virtual bool paste(const Clipboard & /* from */,
Chris@127 196 int /* frameOffset */,
Chris@127 197 bool /* interactive */) { return false; }
Chris@127 198
Chris@127 199 // Text mode:
Chris@127 200 //
Chris@127 201 // Label nearest feature. We need to get the feature coordinates
Chris@127 202 // and current label from the layer, and then the pane can pop up
Chris@127 203 // a little text entry dialog at the right location. Or we edit
Chris@127 204 // in place? Probably the dialog is easier.
Chris@127 205
Chris@127 206 /**
Chris@127 207 * This should return true if the layer can safely be scrolled
Chris@127 208 * automatically by a given view (simply copying the existing data
Chris@127 209 * and then refreshing the exposed area) without altering its
Chris@127 210 * meaning. For the view widget as a whole this is usually not
Chris@127 211 * possible because of invariant (non-scrolling) material
Chris@127 212 * displayed over the top, but the widget may be able to optimise
Chris@127 213 * scrolling better if it is known that individual views can be
Chris@127 214 * scrolled safely in this way.
Chris@127 215 */
Chris@127 216 virtual bool isLayerScrollable(const View *) const { return true; }
Chris@127 217
Chris@127 218 /**
Chris@127 219 * This should return true if the layer completely obscures any
Chris@127 220 * underlying layers. It's used to determine whether the view can
Chris@127 221 * safely draw any selection rectangles under the layer instead of
Chris@127 222 * over it, in the case where the layer is not scrollable and
Chris@127 223 * therefore needs to be redrawn each time (so that the selection
Chris@127 224 * rectangle can be cached).
Chris@127 225 */
Chris@127 226 virtual bool isLayerOpaque() const { return false; }
Chris@127 227
Chris@127 228 /**
Chris@183 229 * This should return true if the layer uses colours to indicate
Chris@183 230 * meaningful information (as opposed to just using a single
Chris@183 231 * colour of the user's choice). If this is the case, the view
Chris@183 232 * will show selections using unfilled rectangles instead of
Chris@183 233 * translucent filled rectangles, so as not to disturb the colours
Chris@183 234 * underneath.
Chris@183 235 */
Chris@183 236 virtual bool isLayerColourSignificant() const { return false; }
Chris@183 237
Chris@183 238 /**
Chris@127 239 * This should return true if the layer can be edited by the user.
Chris@127 240 * If this is the case, the appropriate edit tools may be made
Chris@127 241 * available by the application and the layer's drawStart/Drag/End
Chris@127 242 * and editStart/Drag/End methods should be implemented.
Chris@127 243 */
Chris@127 244 virtual bool isLayerEditable() const { return false; }
Chris@127 245
Chris@127 246 /**
Chris@127 247 * Return the proportion of background work complete in drawing
Chris@127 248 * this view, as a percentage -- in most cases this will be the
Chris@127 249 * value returned by pointer from a call to the underlying model's
Chris@226 250 * isReady(int *) call. The view may choose to show a progress
Chris@127 251 * meter if it finds that this returns < 100 at any given moment.
Chris@127 252 */
Chris@127 253 virtual int getCompletion(View *) const { return 100; }
Chris@127 254
Chris@127 255 virtual void setObjectName(const QString &name);
Chris@127 256
Chris@127 257 /**
Chris@127 258 * Convert the layer's data (though not those of the model it
Chris@127 259 * refers to) into an XML string for file output. This class
Chris@127 260 * implements the basic name/type/model-id output; subclasses will
Chris@127 261 * typically call this superclass implementation with extra
Chris@127 262 * attributes describing their particular properties.
Chris@127 263 */
Chris@127 264 virtual QString toXmlString(QString indent = "",
Chris@127 265 QString extraAttributes = "") const;
Chris@127 266
Chris@127 267 /**
Chris@127 268 * Set the particular properties of a layer (those specific to the
Chris@127 269 * subclass) from a set of XML attributes. This is the effective
Chris@127 270 * inverse of the toXmlString method.
Chris@127 271 */
Chris@127 272 virtual void setProperties(const QXmlAttributes &) = 0;
Chris@127 273
Chris@127 274 /**
Chris@269 275 * Produce an XML string containing the layer's ID and type. This
Chris@269 276 * is used to refer to the layer in the display section of the SV
Chris@269 277 * session file, for a layer that has already been described in
Chris@269 278 * the data section.
Chris@269 279 */
Chris@269 280 virtual QString toBriefXmlString(QString indent = "",
Chris@269 281 QString extraAttributes = "") const;
Chris@269 282
Chris@269 283 /**
Chris@269 284 * Add a measurement rectangle from the given XML attributes
Chris@269 285 * (presumably taken from a measurement element).
Chris@269 286 * Does not use a command.
Chris@269 287 */
Chris@269 288 virtual void addMeasurementRect(const QXmlAttributes &);
Chris@269 289
Chris@269 290 /**
Chris@127 291 * Indicate that a layer is not currently visible in the given
Chris@127 292 * view and is not expected to become visible in the near future
Chris@127 293 * (for example because the user has explicitly removed or hidden
Chris@127 294 * it). The layer may respond by (for example) freeing any cache
Chris@127 295 * memory it is using, until next time its paint method is called,
Chris@127 296 * when it should set itself un-dormant again.
Chris@131 297 *
Chris@131 298 * A layer class that overrides this function must also call this
Chris@131 299 * class's implementation.
Chris@127 300 */
Chris@131 301 virtual void setLayerDormant(const View *v, bool dormant);
Chris@127 302
Chris@127 303 /**
Chris@127 304 * Return whether the layer is dormant (i.e. hidden) in the given
Chris@127 305 * view.
Chris@127 306 */
Chris@131 307 virtual bool isLayerDormant(const View *v) const;
Chris@127 308
Chris@127 309 virtual PlayParameters *getPlayParameters();
Chris@127 310
Chris@127 311 virtual bool needsTextLabelHeight() const { return false; }
Chris@127 312
Chris@217 313 virtual bool hasTimeXAxis() const { return true; }
Chris@217 314
Chris@127 315 /**
Chris@127 316 * Return the minimum and maximum values for the y axis of the
Chris@127 317 * model in this layer, as well as whether the layer is configured
Chris@127 318 * to use a logarithmic y axis display. Also return the unit for
Chris@127 319 * these values if known.
Chris@127 320 *
Chris@127 321 * This function returns the "normal" extents for the layer, not
Chris@127 322 * necessarily the extents actually in use in the display.
Chris@127 323 */
Chris@127 324 virtual bool getValueExtents(float &min, float &max,
Chris@127 325 bool &logarithmic, QString &unit) const = 0;
Chris@127 326
Chris@127 327 /**
Chris@127 328 * Return the minimum and maximum values within the displayed
Chris@127 329 * range for the y axis, if only a subset of the whole range of
Chris@127 330 * the model (returned by getValueExtents) is being displayed.
Chris@127 331 * Return false if the layer is not imposing a particular display
Chris@127 332 * extent (using the normal layer extents or deferring to whatever
Chris@127 333 * is in use for the same units elsewhere in the view).
Chris@127 334 */
Chris@127 335 virtual bool getDisplayExtents(float & /* min */,
Chris@127 336 float & /* max */) const {
Chris@127 337 return false;
Chris@127 338 }
Chris@127 339
Chris@127 340 /**
Chris@127 341 * Set the displayed minimum and maximum values for the y axis to
Chris@127 342 * the given range, if supported. Return false if not supported
Chris@127 343 * on this layer (and set nothing). In most cases, layers that
Chris@127 344 * return false for getDisplayExtents should also return false for
Chris@127 345 * this function.
Chris@127 346 */
Chris@127 347 virtual bool setDisplayExtents(float /* min */,
Chris@127 348 float /* max */) {
Chris@127 349 return false;
Chris@127 350 }
Chris@127 351
Chris@133 352 /**
Chris@260 353 * Return the value and unit at the given x coordinate in the
Chris@260 354 * given view. This is for descriptive purposes using the
Chris@260 355 * measurement tool. The default implementation works correctly
Chris@260 356 * if the layer hasTimeXAxis().
Chris@260 357 */
Chris@267 358 virtual bool getXScaleValue(const View *v, int x,
Chris@260 359 float &value, QString &unit) const;
Chris@260 360
Chris@260 361 /**
Chris@260 362 * Return the value and unit at the given y coordinate in the
Chris@260 363 * given view.
Chris@260 364 */
Chris@267 365 virtual bool getYScaleValue(const View *, int /* y */,
Chris@260 366 float &/* value */, QString &/* unit */) const {
Chris@260 367 return false;
Chris@260 368 }
Chris@260 369
Chris@260 370 /**
Chris@274 371 * Return the difference between the values at the given y
Chris@274 372 * coordinates in the given view, and the unit of the difference.
Chris@274 373 * The default implementation just calls getYScaleValue twice and
Chris@274 374 * returns the difference, with the same unit.
Chris@274 375 */
Chris@274 376 virtual bool getYScaleDifference(const View *v, int y0, int y1,
Chris@274 377 float &diff, QString &unit) const;
Chris@274 378
Chris@274 379 /**
Chris@133 380 * Get the number of vertical zoom steps available for this layer.
Chris@133 381 * If vertical zooming is not available, return 0. The meaning of
Chris@133 382 * "zooming" is entirely up to the layer -- changing the zoom
Chris@133 383 * level may cause the layer to reset its display extents or
Chris@180 384 * change another property such as display gain. However, layers
Chris@180 385 * are advised for consistency to treat smaller zoom steps as
Chris@180 386 * "more distant" or "zoomed out" and larger ones as "closer" or
Chris@180 387 * "zoomed in".
Chris@180 388 *
Chris@133 389 * Layers that provide this facility should also emit the
Chris@133 390 * verticalZoomChanged signal if their vertical zoom changes
Chris@133 391 * due to factors other than setVerticalZoomStep being called.
Chris@133 392 */
Chris@248 393 virtual int getVerticalZoomSteps(int & /* defaultStep */) const { return 0; }
Chris@133 394
Chris@133 395 /**
Chris@133 396 * Get the current vertical zoom step. A layer may support finer
Chris@133 397 * control over ranges etc than is available through the integer
Chris@133 398 * zoom step mechanism; if this one does, it should just return
Chris@133 399 * the nearest of the available zoom steps to the current settings.
Chris@133 400 */
Chris@133 401 virtual int getCurrentVerticalZoomStep() const { return 0; }
Chris@133 402
Chris@133 403 /**
Chris@133 404 * Set the vertical zoom step. The meaning of "zooming" is
Chris@133 405 * entirely up to the layer -- changing the zoom level may cause
Chris@133 406 * the layer to reset its display extents or change another
Chris@133 407 * property such as display gain.
Chris@133 408 */
Chris@133 409 virtual void setVerticalZoomStep(int) { }
Chris@133 410
Chris@187 411 /**
Chris@187 412 * Create and return a range mapper for vertical zoom step values.
Chris@187 413 * See the RangeMapper documentation for more details. The
Chris@187 414 * returned value is allocated on the heap and will be deleted by
Chris@187 415 * the caller.
Chris@187 416 */
Chris@187 417 virtual RangeMapper *getNewVerticalZoomRangeMapper() const { return 0; }
Chris@187 418
Chris@127 419 public slots:
Chris@127 420 void showLayer(View *, bool show);
Chris@127 421
Chris@127 422 signals:
Chris@127 423 void modelChanged();
Chris@127 424 void modelCompletionChanged();
Chris@127 425 void modelChanged(size_t startFrame, size_t endFrame);
Chris@127 426 void modelReplaced();
Chris@127 427
Chris@127 428 void layerParametersChanged();
Chris@197 429 void layerParameterRangesChanged();
Chris@268 430 void layerMeasurementRectsChanged();
Chris@127 431 void layerNameChanged();
Chris@127 432
Chris@133 433 void verticalZoomChanged();
Chris@133 434
Chris@267 435 protected:
Chris@267 436 struct MeasureRect {
Chris@268 437
Chris@267 438 mutable QRect pixrect;
Chris@268 439 bool haveFrames;
Chris@268 440 long startFrame; // only valid if haveFrames
Chris@267 441 long endFrame; // ditto
Chris@273 442 double startY;
Chris@273 443 double endY;
Chris@268 444
Chris@268 445 bool operator<(const MeasureRect &mr) const;
Chris@269 446 QString toXmlString(QString indent) const;
Chris@267 447 };
Chris@267 448
Chris@268 449 class AddMeasurementRectCommand : public Command
Chris@268 450 {
Chris@268 451 public:
Chris@268 452 AddMeasurementRectCommand(Layer *layer, MeasureRect rect) :
Chris@268 453 m_layer(layer), m_rect(rect) { }
Chris@268 454
Chris@268 455 virtual QString getName() const;
Chris@268 456 virtual void execute();
Chris@268 457 virtual void unexecute();
Chris@268 458
Chris@268 459 private:
Chris@268 460 Layer *m_layer;
Chris@268 461 MeasureRect m_rect;
Chris@268 462 };
Chris@268 463
Chris@269 464 void addMeasureRectToSet(const MeasureRect &r) {
Chris@268 465 m_measureRects.insert(r);
Chris@268 466 emit layerMeasurementRectsChanged();
Chris@268 467 }
Chris@268 468
Chris@269 469 void deleteMeasureRectFromSet(const MeasureRect &r) {
Chris@268 470 m_measureRects.erase(r);
Chris@268 471 emit layerMeasurementRectsChanged();
Chris@268 472 }
Chris@268 473
Chris@268 474 typedef std::set<MeasureRect> MeasureRectSet;
Chris@268 475 MeasureRectSet m_measureRects;
Chris@267 476 MeasureRect m_draggingRect;
Chris@267 477 bool m_haveDraggingRect;
Chris@272 478
Chris@272 479 // Note that pixrects are only correct for a single view.
Chris@272 480 // So we should update them at the start of the paint procedure
Chris@272 481 // (painting is single threaded) and only use them after that.
Chris@273 482 void updateMeasurePixrects(View *v) const;
Chris@273 483
Chris@273 484 virtual void updateMeasureRectYCoords(View *v, const MeasureRect &r) const;
Chris@273 485 virtual void setMeasureRectYCoord(View *v, MeasureRect &r, bool start, int y) const;
Chris@272 486
Chris@272 487 // This assumes updateMeasurementPixrects has been called
Chris@272 488 MeasureRectSet::const_iterator findFocusedMeasureRect(QPoint) const;
Chris@267 489
Chris@269 490 void paintMeasurementRect(View *v, QPainter &paint,
Chris@270 491 const MeasureRect &r, bool focus) const;
Chris@268 492
Chris@131 493 private:
Chris@131 494 mutable QMutex m_dormancyMutex;
Chris@127 495 mutable std::map<const void *, bool> m_dormancy;
Chris@127 496 };
Chris@127 497
Chris@127 498 #endif
Chris@127 499