annotate layer/Layer.h @ 282:d9319859a4cf tip

(none)
author benoitrigolleau
date Fri, 31 Oct 2008 11:00:24 +0000
parents 0387f53242b2
children
rev   line source
lbajardsilogic@0 1
lbajardsilogic@0 2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
lbajardsilogic@0 3
lbajardsilogic@0 4 /*
lbajardsilogic@0 5 Sonic Visualiser
lbajardsilogic@0 6 An audio file viewer and annotation editor.
lbajardsilogic@0 7 Centre for Digital Music, Queen Mary, University of London.
lbajardsilogic@0 8 This file copyright 2006 Chris Cannam and QMUL.
lbajardsilogic@0 9
lbajardsilogic@0 10 This program is free software; you can redistribute it and/or
lbajardsilogic@0 11 modify it under the terms of the GNU General Public License as
lbajardsilogic@0 12 published by the Free Software Foundation; either version 2 of the
lbajardsilogic@0 13 License, or (at your option) any later version. See the file
lbajardsilogic@0 14 COPYING included with this distribution for more information.
lbajardsilogic@0 15 */
lbajardsilogic@0 16
lbajardsilogic@0 17 #ifndef _LAYER_H_
lbajardsilogic@0 18 #define _LAYER_H_
lbajardsilogic@0 19
lbajardsilogic@0 20 #include "base/PropertyContainer.h"
lbajardsilogic@0 21 #include "base/XmlExportable.h"
lbajardsilogic@0 22 #include "base/Selection.h"
lbajardsilogic@0 23
lbajardsilogic@0 24 #include <QObject>
lbajardsilogic@0 25 #include <QRect>
lbajardsilogic@0 26 #include <QXmlAttributes>
lbajardsilogic@0 27 #include <QMutex>
lbajardsilogic@0 28
benoitrigolleau@61 29
lbajardsilogic@0 30 #include <map>
lbajardsilogic@0 31
lbajardsilogic@0 32 class ZoomConstraint;
lbajardsilogic@0 33 class Model;
lbajardsilogic@0 34 class QPainter;
lbajardsilogic@0 35 class View;
lbajardsilogic@0 36 class QMouseEvent;
lbajardsilogic@0 37 class Clipboard;
lbajardsilogic@0 38 class RangeMapper;
lbajardsilogic@0 39
lbajardsilogic@0 40 /**
lbajardsilogic@0 41 * The base class for visual representations of the data found in a
lbajardsilogic@0 42 * Model. Layers are expected to be able to draw themselves onto a
lbajardsilogic@0 43 * View, and may also be editable.
lbajardsilogic@0 44 */
lbajardsilogic@0 45
lbajardsilogic@0 46 class Layer : public PropertyContainer,
lbajardsilogic@0 47 public XmlExportable
lbajardsilogic@0 48 {
lbajardsilogic@0 49 Q_OBJECT
lbajardsilogic@0 50
lbajardsilogic@0 51 public:
lbajardsilogic@0 52 Layer();
lbajardsilogic@0 53 virtual ~Layer();
lbajardsilogic@0 54
lbajardsilogic@18 55 inline QString getModelName() const {return m_modelName;}
lbajardsilogic@18 56 inline int getModelId() const {return m_modelId;}
lbajardsilogic@18 57
lbajardsilogic@18 58 inline void setModelName(QString& name) {m_modelName = name;}
lbajardsilogic@18 59 inline void setModelId(int& id){m_modelId = id;}
lbajardsilogic@18 60
lbajardsilogic@0 61 virtual const Model *getModel() const = 0;
lbajardsilogic@0 62 virtual Model *getModel() {
lbajardsilogic@0 63 return const_cast<Model *>(const_cast<const Layer *>(this)->getModel());
lbajardsilogic@0 64 }
lbajardsilogic@0 65
lbajardsilogic@0 66 /**
lbajardsilogic@0 67 * Return a zoom constraint object defining the supported zoom
lbajardsilogic@0 68 * levels for this layer. If this returns zero, the layer will
lbajardsilogic@0 69 * support any integer zoom level.
lbajardsilogic@0 70 */
lbajardsilogic@0 71 virtual const ZoomConstraint *getZoomConstraint() const { return 0; }
lbajardsilogic@0 72
lbajardsilogic@0 73 /**
lbajardsilogic@0 74 * Return true if this layer can handle zoom levels other than
lbajardsilogic@0 75 * those supported by its zoom constraint (presumably less
lbajardsilogic@0 76 * efficiently or accurately than the officially supported zoom
lbajardsilogic@0 77 * levels). If true, the layer will unenthusistically accept any
lbajardsilogic@0 78 * integer zoom level from 1 to the maximum returned by its zoom
lbajardsilogic@0 79 * constraint.
lbajardsilogic@0 80 */
lbajardsilogic@0 81 virtual bool supportsOtherZoomLevels() const { return true; }
lbajardsilogic@0 82
lbajardsilogic@0 83 virtual void paint(View *, QPainter &, QRect) const = 0;
lbajardsilogic@0 84
lbajardsilogic@0 85 enum VerticalPosition {
lbajardsilogic@0 86 PositionTop, PositionMiddle, PositionBottom
lbajardsilogic@0 87 };
lbajardsilogic@0 88 virtual VerticalPosition getPreferredTimeRulerPosition() const {
lbajardsilogic@0 89 return PositionMiddle;
lbajardsilogic@0 90 }
lbajardsilogic@0 91 virtual VerticalPosition getPreferredFrameCountPosition() const {
lbajardsilogic@0 92 return PositionBottom;
lbajardsilogic@0 93 }
lbajardsilogic@0 94 virtual bool hasLightBackground() const {
lbajardsilogic@0 95 return true;
lbajardsilogic@0 96 }
lbajardsilogic@0 97
lbajardsilogic@0 98 virtual QString getPropertyContainerIconName() const;
lbajardsilogic@0 99
lbajardsilogic@0 100 virtual QString getPropertyContainerName() const {
lbajardsilogic@0 101 return objectName();
lbajardsilogic@0 102 }
lbajardsilogic@0 103
lbajardsilogic@0 104 virtual QString getLayerPresentationName() const;
lbajardsilogic@0 105
lbajardsilogic@0 106 virtual int getVerticalScaleWidth(View *, QPainter &) const { return 0; }
lbajardsilogic@0 107 virtual void paintVerticalScale(View *, QPainter &, QRect) const { }
lbajardsilogic@0 108
lbajardsilogic@0 109 virtual bool getCrosshairExtents(View *, QPainter &, QPoint /* cursorPos */,
lbajardsilogic@0 110 std::vector<QRect> &) const {
lbajardsilogic@0 111 return false;
lbajardsilogic@0 112 }
lbajardsilogic@0 113 virtual void paintCrosshairs(View *, QPainter &, QPoint) const { }
lbajardsilogic@0 114
lbajardsilogic@0 115 virtual QString getFeatureDescription(View *, QPoint &) const {
lbajardsilogic@0 116 return "";
lbajardsilogic@0 117 }
lbajardsilogic@0 118
lbajardsilogic@0 119 enum SnapType {
lbajardsilogic@0 120 SnapLeft,
lbajardsilogic@0 121 SnapRight,
lbajardsilogic@0 122 SnapNearest,
lbajardsilogic@0 123 SnapNeighbouring
lbajardsilogic@0 124 };
lbajardsilogic@0 125
lbajardsilogic@0 126 /**
lbajardsilogic@0 127 * Adjust the given frame to snap to the nearest feature, if
lbajardsilogic@0 128 * possible.
lbajardsilogic@0 129 *
lbajardsilogic@0 130 * If snap is SnapLeft or SnapRight, adjust the frame to match
lbajardsilogic@0 131 * that of the nearest feature in the given direction regardless
lbajardsilogic@0 132 * of how far away it is. If snap is SnapNearest, adjust the
lbajardsilogic@0 133 * frame to that of the nearest feature in either direction. If
lbajardsilogic@0 134 * snap is SnapNeighbouring, adjust the frame to that of the
lbajardsilogic@0 135 * nearest feature if it is close, and leave it alone (returning
lbajardsilogic@0 136 * false) otherwise. SnapNeighbouring should always choose the
lbajardsilogic@0 137 * same feature that would be used in an editing operation through
lbajardsilogic@0 138 * calls to editStart etc.
lbajardsilogic@0 139 *
lbajardsilogic@0 140 * Return true if a suitable feature was found and frame adjusted
lbajardsilogic@0 141 * accordingly. Return false if no suitable feature was available
lbajardsilogic@0 142 * (and leave frame unmodified). Also return the resolution of
lbajardsilogic@0 143 * the model in this layer in sample frames.
lbajardsilogic@0 144 */
lbajardsilogic@0 145 virtual bool snapToFeatureFrame(View * /* v */,
lbajardsilogic@0 146 int & /* frame */,
lbajardsilogic@0 147 size_t &resolution,
lbajardsilogic@0 148 SnapType /* snap */) const {
lbajardsilogic@0 149 resolution = 1;
lbajardsilogic@0 150 return false;
lbajardsilogic@0 151 }
lbajardsilogic@0 152
lbajardsilogic@0 153 // Draw and edit modes:
lbajardsilogic@0 154 //
lbajardsilogic@0 155 // Layer needs to get actual mouse events, I guess. Draw mode is
lbajardsilogic@0 156 // probably the easier.
lbajardsilogic@0 157
lbajardsilogic@0 158 virtual void drawStart(View *, QMouseEvent *) { }
lbajardsilogic@0 159 virtual void drawDrag(View *, QMouseEvent *) { }
lbajardsilogic@0 160 virtual void drawEnd(View *, QMouseEvent *) { }
lbajardsilogic@0 161
lbajardsilogic@0 162 virtual void editStart(View *, QMouseEvent *) { }
lbajardsilogic@0 163 virtual void editDrag(View *, QMouseEvent *) { }
lbajardsilogic@0 164 virtual void editEnd(View *, QMouseEvent *) { }
lbajardsilogic@0 165
lbajardsilogic@0 166 virtual void editOpen(View *, QMouseEvent *) { } // on double-click
lbajardsilogic@0 167
lbajardsilogic@0 168 virtual void moveSelection(Selection, size_t /* newStartFrame */) { }
lbajardsilogic@0 169 virtual void resizeSelection(Selection, Selection /* newSize */) { }
lbajardsilogic@0 170 virtual void deleteSelection(Selection) { }
lbajardsilogic@0 171
lbajardsilogic@0 172 virtual void copy(Selection, Clipboard & /* to */) { }
lbajardsilogic@0 173
lbajardsilogic@0 174 /**
lbajardsilogic@0 175 * Paste from the given clipboard onto the layer at the given
lbajardsilogic@0 176 * frame offset. If interactive is true, the layer may ask the
lbajardsilogic@0 177 * user about paste options through a dialog if desired, and may
lbajardsilogic@0 178 * return false if the user cancelled the paste operation. This
lbajardsilogic@0 179 * function should return true if a paste actually occurred.
lbajardsilogic@0 180 */
lbajardsilogic@0 181 virtual bool paste(const Clipboard & /* from */,
lbajardsilogic@0 182 int /* frameOffset */,
lbajardsilogic@0 183 bool /* interactive */) { return false; }
lbajardsilogic@0 184
lbajardsilogic@0 185 // Text mode:
lbajardsilogic@0 186 //
lbajardsilogic@0 187 // Label nearest feature. We need to get the feature coordinates
lbajardsilogic@0 188 // and current label from the layer, and then the pane can pop up
lbajardsilogic@0 189 // a little text entry dialog at the right location. Or we edit
lbajardsilogic@0 190 // in place? Probably the dialog is easier.
lbajardsilogic@0 191
lbajardsilogic@0 192 /**
lbajardsilogic@0 193 * This should return true if the layer can safely be scrolled
lbajardsilogic@0 194 * automatically by a given view (simply copying the existing data
lbajardsilogic@0 195 * and then refreshing the exposed area) without altering its
lbajardsilogic@0 196 * meaning. For the view widget as a whole this is usually not
lbajardsilogic@0 197 * possible because of invariant (non-scrolling) material
lbajardsilogic@0 198 * displayed over the top, but the widget may be able to optimise
lbajardsilogic@0 199 * scrolling better if it is known that individual views can be
lbajardsilogic@0 200 * scrolled safely in this way.
lbajardsilogic@0 201 */
lbajardsilogic@0 202 virtual bool isLayerScrollable(const View *) const { return true; }
lbajardsilogic@0 203
lbajardsilogic@0 204 /**
lbajardsilogic@0 205 * This should return true if the layer completely obscures any
lbajardsilogic@0 206 * underlying layers. It's used to determine whether the view can
lbajardsilogic@0 207 * safely draw any selection rectangles under the layer instead of
lbajardsilogic@0 208 * over it, in the case where the layer is not scrollable and
lbajardsilogic@0 209 * therefore needs to be redrawn each time (so that the selection
lbajardsilogic@0 210 * rectangle can be cached).
lbajardsilogic@0 211 */
lbajardsilogic@0 212 virtual bool isLayerOpaque() const { return false; }
lbajardsilogic@0 213
lbajardsilogic@0 214 /**
lbajardsilogic@0 215 * This should return true if the layer uses colours to indicate
lbajardsilogic@0 216 * meaningful information (as opposed to just using a single
lbajardsilogic@0 217 * colour of the user's choice). If this is the case, the view
lbajardsilogic@0 218 * will show selections using unfilled rectangles instead of
lbajardsilogic@0 219 * translucent filled rectangles, so as not to disturb the colours
lbajardsilogic@0 220 * underneath.
lbajardsilogic@0 221 */
lbajardsilogic@0 222 virtual bool isLayerColourSignificant() const { return false; }
lbajardsilogic@0 223
lbajardsilogic@0 224 /**
lbajardsilogic@0 225 * This should return true if the layer can be edited by the user.
lbajardsilogic@0 226 * If this is the case, the appropriate edit tools may be made
lbajardsilogic@0 227 * available by the application and the layer's drawStart/Drag/End
lbajardsilogic@0 228 * and editStart/Drag/End methods should be implemented.
lbajardsilogic@0 229 */
lbajardsilogic@0 230 virtual bool isLayerEditable() const { return false; }
lbajardsilogic@0 231
lbajardsilogic@0 232 /**
lbajardsilogic@0 233 * Return the proportion of background work complete in drawing
lbajardsilogic@0 234 * this view, as a percentage -- in most cases this will be the
lbajardsilogic@0 235 * value returned by pointer from a call to the underlying model's
lbajardsilogic@0 236 * isReady(int *) call. The view may choose to show a progress
lbajardsilogic@0 237 * meter if it finds that this returns < 100 at any given moment.
lbajardsilogic@0 238 */
lbajardsilogic@0 239 virtual int getCompletion(View *) const { return 100; }
lbajardsilogic@0 240
lbajardsilogic@0 241 virtual void setObjectName(const QString &name);
lbajardsilogic@0 242
lbajardsilogic@0 243 /**
lbajardsilogic@0 244 * Convert the layer's data (though not those of the model it
lbajardsilogic@0 245 * refers to) into an XML string for file output. This class
lbajardsilogic@0 246 * implements the basic name/type/model-id output; subclasses will
lbajardsilogic@0 247 * typically call this superclass implementation with extra
lbajardsilogic@0 248 * attributes describing their particular properties.
lbajardsilogic@0 249 */
lbajardsilogic@0 250 virtual QString toXmlString(QString indent = "",
lbajardsilogic@0 251 QString extraAttributes = "") const;
lbajardsilogic@0 252
lbajardsilogic@18 253 virtual QString toEasaierXmlString(QString indent = "",
lbajardsilogic@18 254 QString extraAttributes = "") const;
lbajardsilogic@18 255
lbajardsilogic@0 256 /**
lbajardsilogic@0 257 * Set the particular properties of a layer (those specific to the
lbajardsilogic@0 258 * subclass) from a set of XML attributes. This is the effective
lbajardsilogic@0 259 * inverse of the toXmlString method.
lbajardsilogic@0 260 */
lbajardsilogic@0 261 virtual void setProperties(const QXmlAttributes &) = 0;
lbajardsilogic@0 262
lbajardsilogic@0 263 /**
lbajardsilogic@0 264 * Indicate that a layer is not currently visible in the given
lbajardsilogic@0 265 * view and is not expected to become visible in the near future
lbajardsilogic@0 266 * (for example because the user has explicitly removed or hidden
lbajardsilogic@0 267 * it). The layer may respond by (for example) freeing any cache
lbajardsilogic@0 268 * memory it is using, until next time its paint method is called,
lbajardsilogic@0 269 * when it should set itself un-dormant again.
lbajardsilogic@0 270 *
lbajardsilogic@0 271 * A layer class that overrides this function must also call this
lbajardsilogic@0 272 * class's implementation.
lbajardsilogic@0 273 */
lbajardsilogic@0 274 virtual void setLayerDormant(const View *v, bool dormant);
lbajardsilogic@0 275
lbajardsilogic@0 276 /**
lbajardsilogic@0 277 * Return whether the layer is dormant (i.e. hidden) in the given
lbajardsilogic@0 278 * view.
lbajardsilogic@0 279 */
lbajardsilogic@0 280 virtual bool isLayerDormant(const View *v) const;
lbajardsilogic@0 281
lbajardsilogic@0 282 virtual PlayParameters *getPlayParameters();
lbajardsilogic@0 283
lbajardsilogic@0 284 virtual bool needsTextLabelHeight() const { return false; }
lbajardsilogic@0 285
lbajardsilogic@0 286 virtual bool hasTimeXAxis() const { return true; }
lbajardsilogic@0 287
lbajardsilogic@0 288 /**
lbajardsilogic@0 289 * Return the minimum and maximum values for the y axis of the
lbajardsilogic@0 290 * model in this layer, as well as whether the layer is configured
lbajardsilogic@0 291 * to use a logarithmic y axis display. Also return the unit for
lbajardsilogic@0 292 * these values if known.
lbajardsilogic@0 293 *
lbajardsilogic@0 294 * This function returns the "normal" extents for the layer, not
lbajardsilogic@0 295 * necessarily the extents actually in use in the display.
lbajardsilogic@0 296 */
lbajardsilogic@0 297 virtual bool getValueExtents(float &min, float &max,
lbajardsilogic@0 298 bool &logarithmic, QString &unit) const = 0;
lbajardsilogic@0 299
lbajardsilogic@0 300 /**
lbajardsilogic@0 301 * Return the minimum and maximum values within the displayed
lbajardsilogic@0 302 * range for the y axis, if only a subset of the whole range of
lbajardsilogic@0 303 * the model (returned by getValueExtents) is being displayed.
lbajardsilogic@0 304 * Return false if the layer is not imposing a particular display
lbajardsilogic@0 305 * extent (using the normal layer extents or deferring to whatever
lbajardsilogic@0 306 * is in use for the same units elsewhere in the view).
lbajardsilogic@0 307 */
lbajardsilogic@0 308 virtual bool getDisplayExtents(float & /* min */,
lbajardsilogic@0 309 float & /* max */) const {
lbajardsilogic@0 310 return false;
lbajardsilogic@0 311 }
lbajardsilogic@0 312
lbajardsilogic@0 313 /**
lbajardsilogic@0 314 * Set the displayed minimum and maximum values for the y axis to
lbajardsilogic@0 315 * the given range, if supported. Return false if not supported
lbajardsilogic@0 316 * on this layer (and set nothing). In most cases, layers that
lbajardsilogic@0 317 * return false for getDisplayExtents should also return false for
lbajardsilogic@0 318 * this function.
lbajardsilogic@0 319 */
lbajardsilogic@0 320 virtual bool setDisplayExtents(float /* min */,
lbajardsilogic@0 321 float /* max */) {
lbajardsilogic@0 322 return false;
lbajardsilogic@0 323 }
lbajardsilogic@0 324
lbajardsilogic@0 325 /**
lbajardsilogic@0 326 * Get the number of vertical zoom steps available for this layer.
lbajardsilogic@0 327 * If vertical zooming is not available, return 0. The meaning of
lbajardsilogic@0 328 * "zooming" is entirely up to the layer -- changing the zoom
lbajardsilogic@0 329 * level may cause the layer to reset its display extents or
lbajardsilogic@0 330 * change another property such as display gain. However, layers
lbajardsilogic@0 331 * are advised for consistency to treat smaller zoom steps as
lbajardsilogic@0 332 * "more distant" or "zoomed out" and larger ones as "closer" or
lbajardsilogic@0 333 * "zoomed in".
lbajardsilogic@0 334 *
lbajardsilogic@0 335 * Layers that provide this facility should also emit the
lbajardsilogic@0 336 * verticalZoomChanged signal if their vertical zoom changes
lbajardsilogic@0 337 * due to factors other than setVerticalZoomStep being called.
lbajardsilogic@0 338 */
lbajardsilogic@0 339 virtual int getVerticalZoomSteps(int & /* defaultStep */) const { return 0; }
lbajardsilogic@0 340
lbajardsilogic@0 341 /**
lbajardsilogic@0 342 * Get the current vertical zoom step. A layer may support finer
lbajardsilogic@0 343 * control over ranges etc than is available through the integer
lbajardsilogic@0 344 * zoom step mechanism; if this one does, it should just return
lbajardsilogic@0 345 * the nearest of the available zoom steps to the current settings.
lbajardsilogic@0 346 */
lbajardsilogic@0 347 virtual int getCurrentVerticalZoomStep() const { return 0; }
lbajardsilogic@0 348
lbajardsilogic@0 349 /**
lbajardsilogic@0 350 * Set the vertical zoom step. The meaning of "zooming" is
lbajardsilogic@0 351 * entirely up to the layer -- changing the zoom level may cause
lbajardsilogic@0 352 * the layer to reset its display extents or change another
lbajardsilogic@0 353 * property such as display gain.
lbajardsilogic@0 354 */
lbajardsilogic@0 355 virtual void setVerticalZoomStep(int) { }
lbajardsilogic@0 356
lbajardsilogic@0 357 /**
lbajardsilogic@0 358 * Create and return a range mapper for vertical zoom step values.
lbajardsilogic@0 359 * See the RangeMapper documentation for more details. The
lbajardsilogic@0 360 * returned value is allocated on the heap and will be deleted by
lbajardsilogic@0 361 * the caller.
lbajardsilogic@0 362 */
lbajardsilogic@0 363 virtual RangeMapper *getNewVerticalZoomRangeMapper() const { return 0; }
lbajardsilogic@0 364
benoitrigolleau@61 365 /**
benoitrigolleau@61 366 * Set the basic display colour for waveforms.
benoitrigolleau@61 367 *
benoitrigolleau@61 368 */
benoitrigolleau@61 369 virtual void setBaseColour(QColor){};
benoitrigolleau@61 370 virtual QColor getBaseColour() const {return -1;};
benoitrigolleau@61 371
lbajardsilogic@0 372 public slots:
lbajardsilogic@0 373 void showLayer(View *, bool show);
lbajardsilogic@0 374
lbajardsilogic@0 375 signals:
lbajardsilogic@0 376 void modelChanged();
lbajardsilogic@0 377 void modelCompletionChanged();
lbajardsilogic@0 378 void modelChanged(size_t startFrame, size_t endFrame);
lbajardsilogic@0 379 void modelReplaced();
lbajardsilogic@0 380
lbajardsilogic@0 381 void layerParametersChanged();
lbajardsilogic@0 382 void layerParameterRangesChanged();
lbajardsilogic@0 383 void layerNameChanged();
lbajardsilogic@0 384
lbajardsilogic@0 385 void verticalZoomChanged();
lbajardsilogic@0 386
lbajardsilogic@0 387 private:
lbajardsilogic@0 388 mutable QMutex m_dormancyMutex;
lbajardsilogic@0 389 mutable std::map<const void *, bool> m_dormancy;
lbajardsilogic@18 390
lbajardsilogic@18 391 QString m_modelName;
lbajardsilogic@18 392 int m_modelId;
lbajardsilogic@0 393 };
lbajardsilogic@0 394
lbajardsilogic@0 395 #endif
lbajardsilogic@0 396