annotate view/View.h @ 834:9ad718fdc369

Add getInteractionLayer() and some docs
author Chris Cannam
date Tue, 02 Sep 2014 10:31:23 +0100
parents 40c6c9344ff6
children 7792b7667f74
rev   line source
Chris@127 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@127 2
Chris@127 3 /*
Chris@127 4 Sonic Visualiser
Chris@127 5 An audio file viewer and annotation editor.
Chris@127 6 Centre for Digital Music, Queen Mary, University of London.
Chris@127 7 This file copyright 2006 Chris Cannam.
Chris@127 8
Chris@127 9 This program is free software; you can redistribute it and/or
Chris@127 10 modify it under the terms of the GNU General Public License as
Chris@127 11 published by the Free Software Foundation; either version 2 of the
Chris@127 12 License, or (at your option) any later version. See the file
Chris@127 13 COPYING included with this distribution for more information.
Chris@127 14 */
Chris@127 15
Chris@226 16 #ifndef _VIEW_H_
Chris@226 17 #define _VIEW_H_
Chris@127 18
Chris@127 19 #include <QFrame>
Chris@127 20 #include <QProgressBar>
Chris@127 21
Chris@127 22 #include "base/ZoomConstraint.h"
Chris@127 23 #include "base/PropertyContainer.h"
Chris@128 24 #include "ViewManager.h"
Chris@127 25 #include "base/XmlExportable.h"
Chris@127 26
Chris@127 27 // #define DEBUG_VIEW_WIDGET_PAINT 1
Chris@127 28
Chris@127 29 class Layer;
Chris@127 30 class ViewPropertyContainer;
Chris@127 31
Chris@797 32 class QPushButton;
Chris@797 33
Chris@127 34 #include <map>
Chris@315 35 #include <set>
Chris@127 36
Chris@127 37 /**
Chris@127 38 * View is the base class of widgets that display one or more
Chris@127 39 * overlaid views of data against a horizontal time scale.
Chris@127 40 *
Chris@127 41 * A View may have any number of attached Layers, each of which
Chris@127 42 * is expected to have one data Model (although multiple views may
Chris@127 43 * share the same model).
Chris@127 44 *
Chris@127 45 * A View may be panned in time and zoomed, although the
Chris@127 46 * mechanisms for doing so (as well as any other operations and
Chris@127 47 * properties available) depend on the subclass.
Chris@127 48 */
Chris@127 49
Chris@127 50 class View : public QFrame,
Chris@127 51 public XmlExportable
Chris@127 52 {
Chris@127 53 Q_OBJECT
Chris@127 54
Chris@127 55 public:
Chris@127 56 /**
Chris@127 57 * Deleting a View does not delete any of its layers. They should
Chris@127 58 * be managed elsewhere (e.g. by the Document).
Chris@127 59 */
Chris@127 60 virtual ~View();
Chris@127 61
Chris@127 62 /**
Chris@127 63 * Retrieve the first visible sample frame on the widget.
Chris@127 64 * This is a calculated value based on the centre-frame, widget
Chris@127 65 * width and zoom level. The result may be negative.
Chris@127 66 */
Chris@806 67 int getStartFrame() const;
Chris@127 68
Chris@127 69 /**
Chris@127 70 * Set the widget pan based on the given first visible frame. The
Chris@127 71 * frame value may be negative.
Chris@127 72 */
Chris@806 73 void setStartFrame(int);
Chris@127 74
Chris@127 75 /**
Chris@127 76 * Return the centre frame of the visible widget. This is an
Chris@127 77 * exact value that does not depend on the zoom block size. Other
Chris@127 78 * frame values (start, end) are calculated from this based on the
Chris@127 79 * zoom and other factors.
Chris@127 80 */
Chris@806 81 int getCentreFrame() const { return m_centreFrame; }
Chris@127 82
Chris@127 83 /**
Chris@127 84 * Set the centre frame of the visible widget.
Chris@127 85 */
Chris@806 86 void setCentreFrame(int f) { setCentreFrame(f, true); }
Chris@127 87
Chris@127 88 /**
Chris@127 89 * Retrieve the last visible sample frame on the widget.
Chris@127 90 * This is a calculated value based on the centre-frame, widget
Chris@127 91 * width and zoom level.
Chris@127 92 */
Chris@806 93 int getEndFrame() const;
Chris@127 94
Chris@127 95 /**
Chris@127 96 * Return the pixel x-coordinate corresponding to a given sample
Chris@127 97 * frame (which may be negative).
Chris@127 98 */
Chris@806 99 int getXForFrame(int frame) const;
Chris@127 100
Chris@127 101 /**
Chris@127 102 * Return the closest frame to the given pixel x-coordinate.
Chris@127 103 */
Chris@806 104 int getFrameForX(int x) const;
Chris@127 105
Chris@127 106 /**
Chris@127 107 * Return the pixel y-coordinate corresponding to a given
Chris@127 108 * frequency, if the frequency range is as specified. This does
Chris@127 109 * not imply any policy about layer frequency ranges, but it might
Chris@127 110 * be useful for layers to match theirs up if desired.
Chris@127 111 *
Chris@127 112 * Not thread-safe in logarithmic mode. Call only from GUI thread.
Chris@127 113 */
Chris@127 114 float getYForFrequency(float frequency, float minFreq, float maxFreq,
Chris@127 115 bool logarithmic) const;
Chris@127 116
Chris@127 117 /**
Chris@127 118 * Return the closest frequency to the given pixel y-coordinate,
Chris@127 119 * if the frequency range is as specified.
Chris@127 120 *
Chris@127 121 * Not thread-safe in logarithmic mode. Call only from GUI thread.
Chris@127 122 */
Chris@127 123 float getFrequencyForY(int y, float minFreq, float maxFreq,
Chris@127 124 bool logarithmic) const;
Chris@127 125
Chris@127 126 /**
Chris@127 127 * Return the zoom level, i.e. the number of frames per pixel
Chris@127 128 */
Chris@127 129 int getZoomLevel() const;
Chris@127 130
Chris@127 131 /**
Chris@127 132 * Set the zoom level, i.e. the number of frames per pixel. The
Chris@127 133 * centre frame will be unchanged; the start and end frames will
Chris@127 134 * change.
Chris@127 135 */
Chris@806 136 virtual void setZoomLevel(int z);
Chris@127 137
Chris@127 138 /**
Chris@127 139 * Zoom in or out.
Chris@127 140 */
Chris@127 141 virtual void zoom(bool in);
Chris@127 142
Chris@127 143 /**
Chris@127 144 * Scroll left or right by a smallish or largish amount.
Chris@127 145 */
Chris@510 146 virtual void scroll(bool right, bool lots, bool doEmit = true);
Chris@127 147
Chris@834 148 /**
Chris@834 149 * Add a layer to the view. (Normally this should be handled
Chris@834 150 * through some command abstraction instead of using this function
Chris@834 151 * directly.)
Chris@834 152 */
Chris@127 153 virtual void addLayer(Layer *v);
Chris@834 154
Chris@834 155 /**
Chris@834 156 * Remove a layer from the view. Does not delete the
Chris@834 157 * layer. (Normally this should be handled through some command
Chris@834 158 * abstraction instead of using this function directly.)
Chris@834 159 */
Chris@834 160 virtual void removeLayer(Layer *v);
Chris@834 161
Chris@834 162 /**
Chris@834 163 * Return the number of layers, regardless of whether visible or
Chris@834 164 * dormant, i.e. invisible, in this view.
Chris@834 165 */
Chris@127 166 virtual int getLayerCount() const { return m_layers.size(); }
Chris@127 167
Chris@127 168 /**
Chris@834 169 * Return the nth layer, counted in stacking order. That is,
Chris@834 170 * layer 0 is the bottom layer and layer "getLayerCount()-1" is
Chris@834 171 * the top one. The returned layer may be visible or it may be
Chris@834 172 * dormant, i.e. invisible.
Chris@127 173 */
Chris@277 174 virtual Layer *getLayer(int n) {
Chris@285 175 if (n < int(m_layers.size())) return m_layers[n]; else return 0;
Chris@277 176 }
Chris@127 177
Chris@127 178 /**
Chris@834 179 * Return the "top" layer in the view, whether visible or dormant.
Chris@834 180 * This is the same as getLayer(getLayerCount()-1) if there is at
Chris@834 181 * least one layer, and 0 otherwise.
Chris@268 182 */
Chris@268 183 virtual Layer *getTopLayer() {
Chris@268 184 return m_layers.empty() ? 0 : m_layers[m_layers.size()-1];
Chris@268 185 }
Chris@268 186
Chris@268 187 /**
Chris@834 188 * Return the layer currently active for tool interaction. This is
Chris@834 189 * the topmost non-dormant (i.e. visible) layer in the view. If
Chris@834 190 * there are no visible layers in the view, return 0.
Chris@834 191 */
Chris@834 192 virtual Layer *getInteractionLayer();
Chris@834 193
Chris@834 194 /**
Chris@834 195 * Return the layer most recently selected by the user. If the
Chris@834 196 * user has selected the pane itself more recently than any of the
Chris@834 197 * layers on it, this function will return 0. It will also return
Chris@834 198 * 0 if there are no layers in the view.
Chris@834 199 *
Chris@834 200 * Note that, unlike getInteractionLayer(), this could return an
Chris@834 201 * invisible (dormant) layer.
Chris@127 202 */
Chris@127 203 virtual Layer *getSelectedLayer();
Chris@834 204
Chris@127 205 virtual const Layer *getSelectedLayer() const;
Chris@127 206
Chris@127 207 virtual void setViewManager(ViewManager *m);
Chris@806 208 virtual void setViewManager(ViewManager *m, int initialFrame);
Chris@127 209 virtual ViewManager *getViewManager() const { return m_manager; }
Chris@127 210
Chris@127 211 virtual void setFollowGlobalPan(bool f);
Chris@127 212 virtual bool getFollowGlobalPan() const { return m_followPan; }
Chris@127 213
Chris@127 214 virtual void setFollowGlobalZoom(bool f);
Chris@127 215 virtual bool getFollowGlobalZoom() const { return m_followZoom; }
Chris@127 216
Chris@224 217 virtual bool hasLightBackground() const;
Chris@287 218 virtual QColor getForeground() const;
Chris@287 219 virtual QColor getBackground() const;
Chris@127 220
Chris@127 221 enum TextStyle {
Chris@127 222 BoxedText,
Chris@630 223 OutlinedText,
Chris@630 224 OutlinedItalicText
Chris@127 225 };
Chris@127 226
Chris@127 227 virtual void drawVisibleText(QPainter &p, int x, int y,
Chris@267 228 QString text, TextStyle style) const;
Chris@267 229
Chris@270 230 virtual void drawMeasurementRect(QPainter &p, const Layer *,
Chris@270 231 QRect rect, bool focus) const;
Chris@127 232
Chris@741 233 virtual bool shouldShowFeatureLabels() const {
Chris@741 234 return m_manager && m_manager->shouldShowFeatureLabels();
Chris@741 235 }
Chris@127 236 virtual bool shouldIlluminateLocalFeatures(const Layer *, QPoint &) const {
Chris@127 237 return false;
Chris@127 238 }
Chris@127 239 virtual bool shouldIlluminateLocalSelection(QPoint &, bool &, bool &) const {
Chris@127 240 return false;
Chris@127 241 }
Chris@127 242
Chris@127 243 virtual void setPlaybackFollow(PlaybackFollowMode m);
Chris@127 244 virtual PlaybackFollowMode getPlaybackFollow() const { return m_followPlay; }
Chris@127 245
Chris@127 246 typedef PropertyContainer::PropertyName PropertyName;
Chris@127 247
Chris@127 248 // We implement the PropertyContainer API, although we don't
Chris@127 249 // actually subclass PropertyContainer. We have our own
Chris@127 250 // PropertyContainer that we can return on request that just
Chris@127 251 // delegates back to us.
Chris@127 252 virtual PropertyContainer::PropertyList getProperties() const;
Chris@127 253 virtual QString getPropertyLabel(const PropertyName &) const;
Chris@127 254 virtual PropertyContainer::PropertyType getPropertyType(const PropertyName &) const;
Chris@127 255 virtual int getPropertyRangeAndValue(const PropertyName &,
Chris@216 256 int *min, int *max, int *deflt) const;
Chris@127 257 virtual QString getPropertyValueLabel(const PropertyName &,
Chris@127 258 int value) const;
Chris@127 259 virtual void setProperty(const PropertyName &, int value);
Chris@127 260 virtual QString getPropertyContainerName() const {
Chris@127 261 return objectName();
Chris@127 262 }
Chris@127 263 virtual QString getPropertyContainerIconName() const = 0;
Chris@127 264
Chris@806 265 virtual int getPropertyContainerCount() const;
Chris@127 266
Chris@806 267 virtual const PropertyContainer *getPropertyContainer(int i) const;
Chris@806 268 virtual PropertyContainer *getPropertyContainer(int i);
Chris@127 269
Chris@229 270 // Render the contents on a wide canvas
Chris@806 271 virtual QImage *toNewImage(int f0, int f1);
Chris@226 272 virtual QImage *toNewImage();
Chris@806 273 virtual QSize getImageSize(int f0, int f1);
Chris@229 274 virtual QSize getImageSize();
Chris@226 275
Chris@127 276 virtual int getTextLabelHeight(const Layer *layer, QPainter &) const;
Chris@127 277
Chris@127 278 virtual bool getValueExtents(QString unit, float &min, float &max,
Chris@127 279 bool &log) const;
Chris@127 280
Chris@316 281 virtual void toXml(QTextStream &stream, QString indent = "",
Chris@316 282 QString extraAttributes = "") const;
Chris@127 283
Chris@222 284 // First frame actually in model, to right of scale, if present
Chris@806 285 virtual int getFirstVisibleFrame() const;
Chris@806 286 virtual int getLastVisibleFrame() const;
Chris@222 287
Chris@806 288 int getModelsStartFrame() const;
Chris@806 289 int getModelsEndFrame() const;
Chris@127 290
Chris@315 291 typedef std::set<Model *> ModelSet;
Chris@315 292 ModelSet getModels();
Chris@315 293
Chris@301 294 //!!!
Chris@320 295 Model *getAligningModel() const;
Chris@806 296 int alignFromReference(int) const;
Chris@806 297 int alignToReference(int) const;
Chris@301 298 int getAlignedPlaybackFrame() const;
Chris@301 299
Chris@127 300 signals:
Chris@127 301 void propertyContainerAdded(PropertyContainer *pc);
Chris@127 302 void propertyContainerRemoved(PropertyContainer *pc);
Chris@127 303 void propertyContainerPropertyChanged(PropertyContainer *pc);
Chris@197 304 void propertyContainerPropertyRangeChanged(PropertyContainer *pc);
Chris@127 305 void propertyContainerNameChanged(PropertyContainer *pc);
Chris@298 306 void propertyContainerSelected(PropertyContainer *pc);
Chris@127 307 void propertyChanged(PropertyContainer::PropertyName);
Chris@127 308
Chris@336 309 void layerModelChanged();
Chris@336 310
Chris@806 311 void centreFrameChanged(int frame,
Chris@211 312 bool globalScroll,
Chris@211 313 PlaybackFollowMode followMode);
Chris@211 314
Chris@806 315 void zoomLevelChanged(int, bool);
Chris@127 316
Chris@189 317 void contextHelpChanged(const QString &);
Chris@189 318
Chris@127 319 public slots:
Chris@127 320 virtual void modelChanged();
Chris@806 321 virtual void modelChangedWithin(int startFrame, int endFrame);
Chris@127 322 virtual void modelCompletionChanged();
Chris@320 323 virtual void modelAlignmentCompletionChanged();
Chris@127 324 virtual void modelReplaced();
Chris@127 325 virtual void layerParametersChanged();
Chris@197 326 virtual void layerParameterRangesChanged();
Chris@268 327 virtual void layerMeasurementRectsChanged();
Chris@127 328 virtual void layerNameChanged();
Chris@127 329
Chris@806 330 virtual void globalCentreFrameChanged(int);
Chris@806 331 virtual void viewCentreFrameChanged(View *, int);
Chris@806 332 virtual void viewManagerPlaybackFrameChanged(int);
Chris@806 333 virtual void viewZoomLevelChanged(View *, int, bool);
Chris@127 334
Chris@127 335 virtual void propertyContainerSelected(View *, PropertyContainer *pc);
Chris@127 336
Chris@127 337 virtual void selectionChanged();
Chris@127 338 virtual void toolModeChanged();
Chris@133 339 virtual void overlayModeChanged();
Chris@133 340 virtual void zoomWheelsEnabledChanged();
Chris@127 341
Chris@797 342 virtual void cancelClicked();
Chris@797 343
Chris@555 344 virtual void progressCheckStalledTimerElapsed();
Chris@555 345
Chris@127 346 protected:
Chris@127 347 View(QWidget *, bool showProgress);
Chris@127 348 virtual void paintEvent(QPaintEvent *e);
Chris@127 349 virtual void drawSelections(QPainter &);
Chris@127 350 virtual bool shouldLabelSelections() const { return true; }
Chris@806 351 virtual bool render(QPainter &paint, int x0, int f0, int f1);
Chris@339 352 virtual void setPaintFont(QPainter &paint);
Chris@339 353
Chris@127 354 typedef std::vector<Layer *> LayerList;
Chris@127 355
Chris@127 356 int getModelsSampleRate() const;
Chris@127 357 bool areLayersScrollable() const;
Chris@127 358 LayerList getScrollableBackLayers(bool testChanged, bool &changed) const;
Chris@127 359 LayerList getNonScrollableFrontLayers(bool testChanged, bool &changed) const;
Chris@806 360 int getZoomConstraintBlockSize(int blockSize,
Chris@127 361 ZoomConstraint::RoundingDirection dir =
Chris@127 362 ZoomConstraint::RoundNearest) const;
Chris@127 363
Chris@183 364 // True if the top layer(s) use colours for meaningful things. If
Chris@183 365 // this is the case, selections will be shown using unfilled boxes
Chris@183 366 // rather than with a translucent fill.
Chris@183 367 bool areLayerColoursSignificant() const;
Chris@183 368
Chris@217 369 // True if the top layer has a time axis on the x coordinate (this
Chris@217 370 // is generally the case except for spectrum/slice layers). It
Chris@217 371 // will not be possible to make or display selections if this is
Chris@217 372 // false.
Chris@217 373 bool hasTopLayerTimeXAxis() const;
Chris@217 374
Chris@806 375 bool setCentreFrame(int f, bool doEmit);
Chris@127 376
Chris@806 377 void movePlayPointer(int f);
Chris@511 378
Chris@127 379 void checkProgress(void *object);
Chris@384 380 int getProgressBarWidth() const; // if visible
Chris@127 381
Chris@806 382 int m_centreFrame;
Chris@127 383 int m_zoomLevel;
Chris@127 384 bool m_followPan;
Chris@127 385 bool m_followZoom;
Chris@127 386 PlaybackFollowMode m_followPlay;
Chris@789 387 bool m_followPlayIsDetached;
Chris@808 388 int m_playPointerFrame;
Chris@127 389 bool m_lightBackground;
Chris@127 390 bool m_showProgress;
Chris@127 391
Chris@127 392 QPixmap *m_cache;
Chris@806 393 int m_cacheCentreFrame;
Chris@127 394 int m_cacheZoomLevel;
Chris@127 395 bool m_selectionCached;
Chris@127 396
Chris@127 397 bool m_deleting;
Chris@127 398
Chris@127 399 LayerList m_layers; // I don't own these, but see dtor note above
Chris@127 400 bool m_haveSelectedLayer;
Chris@127 401
Chris@583 402 QString m_lastError;
Chris@583 403
Chris@127 404 // caches for use in getScrollableBackLayers, getNonScrollableFrontLayers
Chris@127 405 mutable LayerList m_lastScrollableBackLayers;
Chris@127 406 mutable LayerList m_lastNonScrollableBackLayers;
Chris@127 407
Chris@555 408 struct ProgressBarRec {
Chris@797 409 QPushButton *cancel;
Chris@555 410 QProgressBar *bar;
Chris@555 411 int lastCheck;
Chris@555 412 QTimer *checkTimer;
Chris@555 413 };
Chris@555 414 typedef std::map<Layer *, ProgressBarRec> ProgressMap;
Chris@127 415 ProgressMap m_progressBars; // I own the ProgressBars
Chris@127 416
Chris@127 417 ViewManager *m_manager; // I don't own this
Chris@127 418 ViewPropertyContainer *m_propertyContainer; // I own this
Chris@127 419 };
Chris@127 420
Chris@127 421
Chris@127 422 // Use this for delegation, because we can't subclass from
Chris@127 423 // PropertyContainer (which is a QObject) ourselves because of
Chris@127 424 // ambiguity with QFrame parent
Chris@127 425
Chris@127 426 class ViewPropertyContainer : public PropertyContainer
Chris@127 427 {
Chris@127 428 Q_OBJECT
Chris@127 429
Chris@127 430 public:
Chris@127 431 ViewPropertyContainer(View *v);
Chris@728 432 virtual ~ViewPropertyContainer();
Chris@728 433
Chris@127 434 PropertyList getProperties() const { return m_v->getProperties(); }
Chris@127 435 QString getPropertyLabel(const PropertyName &n) const {
Chris@127 436 return m_v->getPropertyLabel(n);
Chris@127 437 }
Chris@127 438 PropertyType getPropertyType(const PropertyName &n) const {
Chris@127 439 return m_v->getPropertyType(n);
Chris@127 440 }
Chris@216 441 int getPropertyRangeAndValue(const PropertyName &n, int *min, int *max,
Chris@216 442 int *deflt) const {
Chris@216 443 return m_v->getPropertyRangeAndValue(n, min, max, deflt);
Chris@127 444 }
Chris@127 445 QString getPropertyValueLabel(const PropertyName &n, int value) const {
Chris@127 446 return m_v->getPropertyValueLabel(n, value);
Chris@127 447 }
Chris@127 448 QString getPropertyContainerName() const {
Chris@127 449 return m_v->getPropertyContainerName();
Chris@127 450 }
Chris@127 451 QString getPropertyContainerIconName() const {
Chris@127 452 return m_v->getPropertyContainerIconName();
Chris@127 453 }
Chris@127 454
Chris@127 455 public slots:
Chris@127 456 virtual void setProperty(const PropertyName &n, int value) {
Chris@127 457 m_v->setProperty(n, value);
Chris@127 458 }
Chris@127 459
Chris@127 460 protected:
Chris@127 461 View *m_v;
Chris@127 462 };
Chris@127 463
Chris@127 464 #endif
Chris@127 465