annotate view/View.h @ 928:3fb91da7d98d tonioni

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