svgui  1.9
View.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef _VIEW_H_
17 #define _VIEW_H_
18 
19 #include <QFrame>
20 #include <QProgressBar>
21 
22 #include "base/ZoomConstraint.h"
23 #include "base/PropertyContainer.h"
24 #include "ViewManager.h"
25 #include "base/XmlExportable.h"
26 
27 // #define DEBUG_VIEW_WIDGET_PAINT 1
28 
29 class Layer;
31 
32 class QPushButton;
33 
34 #include <map>
35 #include <set>
36 
50 class View : public QFrame,
51  public XmlExportable
52 {
53  Q_OBJECT
54 
55 public:
60  virtual ~View();
61 
67  int getStartFrame() const;
68 
73  void setStartFrame(int);
74 
81  int getCentreFrame() const { return m_centreFrame; }
82 
86  void setCentreFrame(int f) { setCentreFrame(f, true); }
87 
93  int getEndFrame() const;
94 
99  int getXForFrame(int frame) const;
100 
104  int getFrameForX(int x) const;
105 
114  float getYForFrequency(float frequency, float minFreq, float maxFreq,
115  bool logarithmic) const;
116 
123  float getFrequencyForY(int y, float minFreq, float maxFreq,
124  bool logarithmic) const;
125 
129  int getZoomLevel() const;
130 
136  virtual void setZoomLevel(int z);
137 
141  virtual void zoom(bool in);
142 
146  virtual void scroll(bool right, bool lots, bool doEmit = true);
147 
153  virtual void addLayer(Layer *v);
154 
160  virtual void removeLayer(Layer *v);
161 
166  virtual int getLayerCount() const { return m_layerStack.size(); }
167 
174  virtual Layer *getLayer(int n) {
175  if (n < int(m_layerStack.size())) return m_layerStack[n];
176  else return 0;
177  }
178 
186  virtual Layer *getFixedOrderLayer(int n) {
187  if (n < int(m_fixedOrderLayers.size())) return m_fixedOrderLayers[n];
188  else return 0;
189  }
190 
196  virtual Layer *getInteractionLayer();
197 
198  virtual const Layer *getInteractionLayer() const;
199 
214  virtual Layer *getSelectedLayer();
215 
216  virtual const Layer *getSelectedLayer() const;
217 
227  virtual Layer *getTopLayer() {
228  return m_layerStack.empty() ? 0 : m_layerStack[m_layerStack.size()-1];
229  }
230 
231  virtual void setViewManager(ViewManager *m);
232  virtual void setViewManager(ViewManager *m, int initialFrame);
233  virtual ViewManager *getViewManager() const { return m_manager; }
234 
235  virtual void setFollowGlobalPan(bool f);
236  virtual bool getFollowGlobalPan() const { return m_followPan; }
237 
238  virtual void setFollowGlobalZoom(bool f);
239  virtual bool getFollowGlobalZoom() const { return m_followZoom; }
240 
241  virtual bool hasLightBackground() const;
242  virtual QColor getForeground() const;
243  virtual QColor getBackground() const;
244 
245  enum TextStyle {
249  };
250 
251  virtual void drawVisibleText(QPainter &p, int x, int y,
252  QString text, TextStyle style) const;
253 
254  virtual void drawMeasurementRect(QPainter &p, const Layer *,
255  QRect rect, bool focus) const;
256 
257  virtual bool shouldShowFeatureLabels() const {
259  }
260  virtual bool shouldIlluminateLocalFeatures(const Layer *, QPoint &) const {
261  return false;
262  }
263  virtual bool shouldIlluminateLocalSelection(QPoint &, bool &, bool &) const {
264  return false;
265  }
266 
267  virtual void setPlaybackFollow(PlaybackFollowMode m);
269 
270  typedef PropertyContainer::PropertyName PropertyName;
271 
272  // We implement the PropertyContainer API, although we don't
273  // actually subclass PropertyContainer. We have our own
274  // PropertyContainer that we can return on request that just
275  // delegates back to us.
276  virtual PropertyContainer::PropertyList getProperties() const;
277  virtual QString getPropertyLabel(const PropertyName &) const;
278  virtual PropertyContainer::PropertyType getPropertyType(const PropertyName &) const;
279  virtual int getPropertyRangeAndValue(const PropertyName &,
280  int *min, int *max, int *deflt) const;
281  virtual QString getPropertyValueLabel(const PropertyName &,
282  int value) const;
283  virtual void setProperty(const PropertyName &, int value);
284  virtual QString getPropertyContainerName() const {
285  return objectName();
286  }
287  virtual QString getPropertyContainerIconName() const = 0;
288 
289  virtual int getPropertyContainerCount() const;
290 
291  // The 0th property container is the view's own; the rest are the
292  // layers in fixed-order series
293  virtual const PropertyContainer *getPropertyContainer(int i) const;
294  virtual PropertyContainer *getPropertyContainer(int i);
295 
296  // Render the contents on a wide canvas
297  virtual QImage *toNewImage(int f0, int f1);
298  virtual QImage *toNewImage();
299  virtual QSize getImageSize(int f0, int f1);
300  virtual QSize getImageSize();
301 
302  virtual int getTextLabelHeight(const Layer *layer, QPainter &) const;
303 
304  virtual bool getValueExtents(QString unit, float &min, float &max,
305  bool &log) const;
306 
307  virtual void toXml(QTextStream &stream, QString indent = "",
308  QString extraAttributes = "") const;
309 
310  // First frame actually in model, to right of scale, if present
311  virtual int getFirstVisibleFrame() const;
312  virtual int getLastVisibleFrame() const;
313 
314  int getModelsStartFrame() const;
315  int getModelsEndFrame() const;
316 
317  typedef std::set<Model *> ModelSet;
319 
321  Model *getAligningModel() const;
322  int alignFromReference(int) const;
323  int alignToReference(int) const;
324  int getAlignedPlaybackFrame() const;
325 
326 signals:
327  void propertyContainerAdded(PropertyContainer *pc);
328  void propertyContainerRemoved(PropertyContainer *pc);
329  void propertyContainerPropertyChanged(PropertyContainer *pc);
330  void propertyContainerPropertyRangeChanged(PropertyContainer *pc);
331  void propertyContainerNameChanged(PropertyContainer *pc);
332  void propertyContainerSelected(PropertyContainer *pc);
333  void propertyChanged(PropertyContainer::PropertyName);
334 
335  void layerModelChanged();
336 
337  void centreFrameChanged(int frame,
338  bool globalScroll,
339  PlaybackFollowMode followMode);
340 
341  void zoomLevelChanged(int, bool);
342 
343  void contextHelpChanged(const QString &);
344 
345 public slots:
346  virtual void modelChanged();
347  virtual void modelChangedWithin(int startFrame, int endFrame);
348  virtual void modelCompletionChanged();
349  virtual void modelAlignmentCompletionChanged();
350  virtual void modelReplaced();
351  virtual void layerParametersChanged();
352  virtual void layerParameterRangesChanged();
353  virtual void layerMeasurementRectsChanged();
354  virtual void layerNameChanged();
355 
356  virtual void globalCentreFrameChanged(int);
357  virtual void viewCentreFrameChanged(View *, int);
358  virtual void viewManagerPlaybackFrameChanged(int);
359  virtual void viewZoomLevelChanged(View *, int, bool);
360 
361  virtual void propertyContainerSelected(View *, PropertyContainer *pc);
362 
363  virtual void selectionChanged();
364  virtual void toolModeChanged();
365  virtual void overlayModeChanged();
366  virtual void zoomWheelsEnabledChanged();
367 
368  virtual void cancelClicked();
369 
370  virtual void progressCheckStalledTimerElapsed();
371 
372 protected:
373  View(QWidget *, bool showProgress);
374  virtual void paintEvent(QPaintEvent *e);
375  virtual void drawSelections(QPainter &);
376  virtual bool shouldLabelSelections() const { return true; }
377  virtual bool render(QPainter &paint, int x0, int f0, int f1);
378  virtual void setPaintFont(QPainter &paint);
379 
380  typedef std::vector<Layer *> LayerList;
381 
382  int getModelsSampleRate() const;
383  bool areLayersScrollable() const;
384  LayerList getScrollableBackLayers(bool testChanged, bool &changed) const;
385  LayerList getNonScrollableFrontLayers(bool testChanged, bool &changed) const;
386  int getZoomConstraintBlockSize(int blockSize,
387  ZoomConstraint::RoundingDirection dir =
388  ZoomConstraint::RoundNearest) const;
389 
390  // True if the top layer(s) use colours for meaningful things. If
391  // this is the case, selections will be shown using unfilled boxes
392  // rather than with a translucent fill.
393  bool areLayerColoursSignificant() const;
394 
395  // True if the top layer has a time axis on the x coordinate (this
396  // is generally the case except for spectrum/slice layers). It
397  // will not be possible to make or display selections if this is
398  // false.
399  bool hasTopLayerTimeXAxis() const;
400 
401  bool setCentreFrame(int f, bool doEmit);
402 
403  void movePlayPointer(int f);
404 
405  void checkProgress(void *object);
406  int getProgressBarWidth() const; // if visible
407 
417 
418  QPixmap *m_cache;
422 
424 
425  LayerList m_layerStack; // I don't own these, but see dtor note above
428 
429  QString m_lastError;
430 
431  // caches for use in getScrollableBackLayers, getNonScrollableFrontLayers
434 
435  struct ProgressBarRec {
436  QPushButton *cancel;
437  QProgressBar *bar;
439  QTimer *checkTimer;
440  };
441  typedef std::map<Layer *, ProgressBarRec> ProgressMap;
442  ProgressMap m_progressBars; // I own the ProgressBars
443 
444  ViewManager *m_manager; // I don't own this
446 };
447 
448 
449 // Use this for delegation, because we can't subclass from
450 // PropertyContainer (which is a QObject) ourselves because of
451 // ambiguity with QFrame parent
452 
453 class ViewPropertyContainer : public PropertyContainer
454 {
455  Q_OBJECT
456 
457 public:
459  virtual ~ViewPropertyContainer();
460 
461  PropertyList getProperties() const { return m_v->getProperties(); }
462  QString getPropertyLabel(const PropertyName &n) const {
463  return m_v->getPropertyLabel(n);
464  }
465  PropertyType getPropertyType(const PropertyName &n) const {
466  return m_v->getPropertyType(n);
467  }
468  int getPropertyRangeAndValue(const PropertyName &n, int *min, int *max,
469  int *deflt) const {
470  return m_v->getPropertyRangeAndValue(n, min, max, deflt);
471  }
472  QString getPropertyValueLabel(const PropertyName &n, int value) const {
473  return m_v->getPropertyValueLabel(n, value);
474  }
475  QString getPropertyContainerName() const {
476  return m_v->getPropertyContainerName();
477  }
480  }
481 
482 public slots:
483  virtual void setProperty(const PropertyName &n, int value) {
484  m_v->setProperty(n, value);
485  }
486 
487 protected:
489 };
490 
491 #endif
492 
int getFrameForX(int x) const
Return the closest frame to the given pixel x-coordinate.
Definition: View.cpp:363
virtual void globalCentreFrameChanged(int)
Definition: View.cpp:998
virtual const PropertyContainer * getPropertyContainer(int i) const
Definition: View.cpp:171
QString getPropertyLabel(const PropertyName &n) const
Definition: View.h:462
int getModelsSampleRate() const
Definition: View.cpp:1234
void propertyContainerNameChanged(PropertyContainer *pc)
The base class for visual representations of the data found in a Model.
Definition: Layer.h:52
virtual QString getPropertyContainerName() const
Definition: View.h:284
int getZoomConstraintBlockSize(int blockSize, ZoomConstraint::RoundingDirection dir=ZoomConstraint::RoundNearest) const
Definition: View.cpp:1424
bool m_lightBackground
Definition: View.h:415
void checkProgress(void *object)
Definition: View.cpp:1532
LayerList m_fixedOrderLayers
Definition: View.h:426
int m_zoomLevel
Definition: View.h:409
void propertyContainerSelected(PropertyContainer *pc)
std::set< Model * > ModelSet
Definition: View.h:317
QString m_lastError
Definition: View.h:429
bool m_followPlayIsDetached
Definition: View.h:413
bool m_followZoom
Definition: View.h:411
bool m_deleting
Definition: View.h:423
virtual void setProperty(const PropertyName &, int value)
Definition: View.cpp:148
void propertyContainerRemoved(PropertyContainer *pc)
virtual QColor getForeground() const
Definition: View.cpp:513
float getYForFrequency(float frequency, float minFreq, float maxFreq, bool logarithmic) const
Return the pixel y-coordinate corresponding to a given frequency, if the frequency range is as specif...
Definition: View.cpp:377
virtual bool getValueExtents(QString unit, float &min, float &max, bool &log) const
Definition: View.cpp:185
virtual int getLayerCount() const
Return the number of layers, regardless of whether visible or dormant, i.e.
Definition: View.h:166
int getModelsStartFrame() const
Definition: View.cpp:1190
virtual PropertyContainer::PropertyType getPropertyType(const PropertyName &) const
Definition: View.cpp:101
virtual void modelReplaced()
Definition: View.cpp:947
QString getPropertyValueLabel(const PropertyName &n, int value) const
Definition: View.h:472
virtual bool shouldShowFeatureLabels() const
Definition: View.h:257
LayerList getNonScrollableFrontLayers(bool testChanged, bool &changed) const
Definition: View.cpp:1392
virtual void zoom(bool in)
Zoom in or out.
Definition: View.cpp:1475
virtual ~View()
Deleting a View does not delete any of its layers.
Definition: View.cpp:73
ProgressMap m_progressBars
Definition: View.h:442
virtual void viewCentreFrameChanged(View *, int)
Definition: View.cpp:1011
virtual ViewManager * getViewManager() const
Definition: View.h:233
virtual int getPropertyRangeAndValue(const PropertyName &, int *min, int *max, int *deflt) const
Definition: View.cpp:110
int getAlignedPlaybackFrame() const
Definition: View.cpp:1331
QTimer * checkTimer
Definition: View.h:439
virtual void selectionChanged()
Definition: View.cpp:1161
std::vector< Layer * > LayerList
Definition: View.h:380
virtual QImage * toNewImage()
Definition: View.cpp:2441
void zoomLevelChanged(int, bool)
void propertyContainerPropertyChanged(PropertyContainer *pc)
virtual void paintEvent(QPaintEvent *e)
Definition: View.cpp:1662
LayerList getScrollableBackLayers(bool testChanged, bool &changed) const
Definition: View.cpp:1356
virtual bool shouldIlluminateLocalFeatures(const Layer *, QPoint &) const
Definition: View.h:260
bool shouldShowFeatureLabels() const
Definition: ViewManager.h:226
virtual void layerParameterRangesChanged()
Definition: View.cpp:977
virtual bool hasLightBackground() const
Definition: View.cpp:463
virtual void addLayer(Layer *v)
Add a layer to the view.
Definition: View.cpp:527
LayerList m_layerStack
Definition: View.h:425
virtual PlaybackFollowMode getPlaybackFollow() const
Definition: View.h:268
virtual Layer * getTopLayer()
Return the "top" layer in the view, whether visible or dormant.
Definition: View.h:227
virtual int getTextLabelHeight(const Layer *layer, QPainter &) const
Definition: View.cpp:222
virtual void setViewManager(ViewManager *m)
Definition: View.cpp:692
int getZoomLevel() const
Return the zoom level, i.e.
Definition: View.cpp:443
void propertyContainerAdded(PropertyContainer *pc)
bool hasTopLayerTimeXAxis() const
Definition: View.cpp:1466
QString getPropertyContainerIconName() const
Definition: View.h:478
virtual void setPaintFont(QPainter &paint)
Definition: View.cpp:1654
ViewManager * m_manager
Definition: View.h:444
virtual int getFirstVisibleFrame() const
Definition: View.cpp:1172
int alignToReference(int) const
Definition: View.cpp:1322
virtual void modelAlignmentCompletionChanged()
Definition: View.cpp:938
virtual bool shouldIlluminateLocalSelection(QPoint &, bool &, bool &) const
Definition: View.h:263
PropertyList getProperties() const
Definition: View.h:461
Model * getAligningModel() const
!!
Definition: View.cpp:1273
void movePlayPointer(int f)
Definition: View.cpp:1037
virtual void cancelClicked()
Definition: View.cpp:1513
void propertyChanged(PropertyContainer::PropertyName)
virtual QSize getImageSize()
Definition: View.cpp:2469
virtual bool getFollowGlobalPan() const
Definition: View.h:236
int getStartFrame() const
Retrieve the first visible sample frame on the widget.
Definition: View.cpp:302
int alignFromReference(int) const
Definition: View.cpp:1313
bool m_selectionCached
Definition: View.h:421
PlaybackFollowMode
Definition: ViewManager.h:33
virtual int getPropertyContainerCount() const
Definition: View.cpp:165
bool areLayersScrollable() const
Definition: View.cpp:1346
virtual void modelCompletionChanged()
Definition: View.cpp:929
virtual void layerParametersChanged()
Definition: View.cpp:959
virtual void modelChanged()
Definition: View.cpp:844
virtual void modelChangedWithin(int startFrame, int endFrame)
Definition: View.cpp:880
int m_centreFrame
Definition: View.h:408
virtual ~ViewPropertyContainer()
Definition: View.cpp:2527
int getEndFrame() const
Retrieve the last visible sample frame on the widget.
Definition: View.cpp:308
virtual Layer * getFixedOrderLayer(int n)
Return the nth layer, counted in the order they were added.
Definition: View.h:186
virtual QString getPropertyValueLabel(const PropertyName &, int value) const
Definition: View.cpp:133
QPushButton * cancel
Definition: View.h:436
PropertyContainer::PropertyName PropertyName
Definition: View.h:270
QProgressBar * bar
Definition: View.h:437
bool m_haveSelectedLayer
Definition: View.h:427
virtual void drawSelections(QPainter &)
Definition: View.cpp:1950
int getProgressBarWidth() const
Definition: View.cpp:1641
virtual void setProperty(const PropertyName &n, int value)
Definition: View.h:483
virtual void layerNameChanged()
Definition: View.cpp:991
virtual Layer * getLayer(int n)
Return the nth layer, counted in stacking order.
Definition: View.h:174
PlaybackFollowMode m_followPlay
Definition: View.h:412
void contextHelpChanged(const QString &)
virtual void progressCheckStalledTimerElapsed()
Definition: View.cpp:1622
int m_playPointerFrame
Definition: View.h:414
LayerList m_lastScrollableBackLayers
Definition: View.h:432
virtual void setFollowGlobalPan(bool f)
Definition: View.cpp:773
QString getPropertyContainerName() const
Definition: View.h:475
virtual bool shouldLabelSelections() const
Definition: View.h:376
int getModelsEndFrame() const
Definition: View.cpp:1211
int m_cacheCentreFrame
Definition: View.h:419
void propertyContainerPropertyRangeChanged(PropertyContainer *pc)
virtual void setPlaybackFollow(PlaybackFollowMode m)
Definition: View.cpp:837
bool m_followPan
Definition: View.h:410
View is the base class of widgets that display one or more overlaid views of data against a horizonta...
Definition: View.h:50
int m_cacheZoomLevel
Definition: View.h:420
virtual void removeLayer(Layer *v)
Remove a layer from the view.
Definition: View.cpp:594
The ViewManager manages properties that may need to be synchronised between separate Views.
Definition: ViewManager.h:73
virtual void toolModeChanged()
Definition: View.cpp:282
virtual void setZoomLevel(int z)
Set the zoom level, i.e.
Definition: View.cpp:452
virtual void zoomWheelsEnabledChanged()
Definition: View.cpp:296
View(QWidget *, bool showProgress)
Definition: View.cpp:51
virtual QString getPropertyLabel(const PropertyName &) const
Definition: View.cpp:92
std::map< Layer *, ProgressBarRec > ProgressMap
Definition: View.h:441
bool m_showProgress
Definition: View.h:416
virtual void viewZoomLevelChanged(View *, int, bool)
Definition: View.cpp:1150
virtual void drawVisibleText(QPainter &p, int x, int y, QString text, TextStyle style) const
Definition: View.cpp:787
LayerList m_lastNonScrollableBackLayers
Definition: View.h:433
int getCentreFrame() const
Return the centre frame of the visible widget.
Definition: View.h:81
virtual bool getFollowGlobalZoom() const
Definition: View.h:239
virtual void viewManagerPlaybackFrameChanged(int)
Definition: View.cpp:1017
virtual Layer * getSelectedLayer()
Return the layer most recently selected by the user.
Definition: View.cpp:676
virtual void overlayModeChanged()
Definition: View.cpp:288
virtual void scroll(bool right, bool lots, bool doEmit=true)
Scroll left or right by a smallish or largish amount.
Definition: View.cpp:1493
virtual int getLastVisibleFrame() const
Definition: View.cpp:1181
virtual void setFollowGlobalZoom(bool f)
Definition: View.cpp:780
void setCentreFrame(int f)
Set the centre frame of the visible widget.
Definition: View.h:86
float getFrequencyForY(int y, float minFreq, float maxFreq, bool logarithmic) const
Return the closest frequency to the given pixel y-coordinate, if the frequency range is as specified.
Definition: View.cpp:411
TextStyle
Definition: View.h:245
virtual bool render(QPainter &paint, int x0, int f0, int f1)
Definition: View.cpp:2336
virtual void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
Definition: View.cpp:2487
virtual PropertyContainer::PropertyList getProperties() const
Definition: View.cpp:82
void setStartFrame(int)
Set the widget pan based on the given first visible frame.
Definition: View.cpp:314
void layerModelChanged()
virtual QColor getBackground() const
Definition: View.cpp:493
bool areLayerColoursSignificant() const
Definition: View.cpp:1455
virtual Layer * getInteractionLayer()
Return the layer currently active for tool interaction.
Definition: View.cpp:650
ModelSet getModels()
Definition: View.cpp:1251
virtual QString getPropertyContainerIconName() const =0
virtual void layerMeasurementRectsChanged()
Definition: View.cpp:984
int getPropertyRangeAndValue(const PropertyName &n, int *min, int *max, int *deflt) const
Definition: View.h:468
QPixmap * m_cache
Definition: View.h:418
int getXForFrame(int frame) const
Return the pixel x-coordinate corresponding to a given sample frame (which may be negative).
Definition: View.cpp:357
virtual void drawMeasurementRect(QPainter &p, const Layer *, QRect rect, bool focus) const
Definition: View.cpp:2097
ViewPropertyContainer * m_propertyContainer
Definition: View.h:445
ViewPropertyContainer(View *v)
Definition: View.cpp:2519
PropertyType getPropertyType(const PropertyName &n) const
Definition: View.h:465
void centreFrameChanged(int frame, bool globalScroll, PlaybackFollowMode followMode)