svgui  1.9
Layer.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 and QMUL.
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 _LAYER_H_
17 #define _LAYER_H_
18 
19 #include "base/PropertyContainer.h"
20 #include "base/XmlExportable.h"
21 #include "base/Selection.h"
22 
23 #include "widgets/CommandHistory.h"
24 
25 #include "system/System.h"
26 
27 #include <QObject>
28 #include <QRect>
29 #include <QXmlAttributes>
30 #include <QMutex>
31 #include <QPixmap>
32 
33 #include <map>
34 #include <set>
35 
36 #include <iostream>
37 
38 class ZoomConstraint;
39 class Model;
40 class QPainter;
41 class View;
42 class QMouseEvent;
43 class Clipboard;
44 class RangeMapper;
45 
52 class Layer : public PropertyContainer,
53  public XmlExportable
54 {
55  Q_OBJECT
56 
57 public:
58  Layer();
59  virtual ~Layer();
60 
61  virtual const Model *getModel() const = 0;
62  Model *getModel() {
63  return const_cast<Model *>(const_cast<const Layer *>(this)->getModel());
64  }
65 
71  virtual const ZoomConstraint *getZoomConstraint() const { return 0; }
72 
81  virtual bool supportsOtherZoomLevels() const { return true; }
82 
91  virtual void paint(View *, QPainter &, QRect) const = 0;
92 
104  virtual void setSynchronousPainting(bool /* synchronous */) { }
105 
108  };
110  return PositionMiddle;
111  }
113  return PositionBottom;
114  }
115  virtual bool hasLightBackground() const {
116  return true;
117  }
118 
119  virtual QString getPropertyContainerIconName() const;
120 
121  virtual QString getPropertyContainerName() const {
122  if (m_presentationName != "") return m_presentationName;
123  else return objectName();
124  }
125 
126  virtual void setPresentationName(QString name);
127 
128  virtual QString getLayerPresentationName() const;
129  virtual QPixmap getLayerPresentationPixmap(QSize) const { return QPixmap(); }
130 
131  virtual int getVerticalScaleWidth(View *, bool detailed,
132  QPainter &) const = 0;
133 
134  virtual void paintVerticalScale(View *, bool /* detailed */,
135  QPainter &, QRect) const { }
136 
137  virtual bool getCrosshairExtents(View *, QPainter &, QPoint /* cursorPos */,
138  std::vector<QRect> &) const {
139  return false;
140  }
141  virtual void paintCrosshairs(View *, QPainter &, QPoint) const { }
142 
143  virtual void paintMeasurementRects(View *, QPainter &,
144  bool showFocus, QPoint focusPoint) const;
145 
146  virtual bool nearestMeasurementRectChanged(View *, QPoint prev,
147  QPoint now) const;
148 
149  virtual QString getFeatureDescription(View *, QPoint &) const {
150  return "";
151  }
152 
153  virtual QString getLabelPreceding(int /* frame */) const {
154  return "";
155  }
156 
157  enum SnapType {
162  };
163 
183  virtual bool snapToFeatureFrame(View * /* v */,
184  int & /* frame */,
185  int &resolution,
186  SnapType /* snap */) const {
187  resolution = 1;
188  return false;
189  }
190 
207  virtual bool snapToSimilarFeature(View * /* v */,
208  int & /* source frame */,
209  int &resolution,
210  SnapType /* snap */) const {
211  resolution = 1;
212  return false;
213  }
214 
215  // Draw, erase, and edit modes:
216  //
217  // Layer needs to get actual mouse events, I guess. Draw mode is
218  // probably the easier.
219 
220  virtual void drawStart(View *, QMouseEvent *) { }
221  virtual void drawDrag(View *, QMouseEvent *) { }
222  virtual void drawEnd(View *, QMouseEvent *) { }
223 
224  virtual void eraseStart(View *, QMouseEvent *) { }
225  virtual void eraseDrag(View *, QMouseEvent *) { }
226  virtual void eraseEnd(View *, QMouseEvent *) { }
227 
228  virtual void editStart(View *, QMouseEvent *) { }
229  virtual void editDrag(View *, QMouseEvent *) { }
230  virtual void editEnd(View *, QMouseEvent *) { }
231 
232  virtual void splitStart(View *, QMouseEvent *) { }
233  virtual void splitEnd(View *, QMouseEvent *) { }
234  virtual void addNote(View *, QMouseEvent *) { };
235 
236  // Measurement rectangle (or equivalent). Unlike draw and edit,
237  // the base Layer class can provide working implementations of
238  // these for most situations.
239  //
240  virtual void measureStart(View *, QMouseEvent *);
241  virtual void measureDrag(View *, QMouseEvent *);
242  virtual void measureEnd(View *, QMouseEvent *);
243  virtual void measureDoubleClick(View *, QMouseEvent *);
244 
245  virtual bool haveCurrentMeasureRect() const {
247  }
248  virtual void deleteCurrentMeasureRect(); // using a command
249 
255  virtual bool editOpen(View *, QMouseEvent *) { return false; }
256 
257  virtual void moveSelection(Selection, int /* newStartFrame */) { }
258  virtual void resizeSelection(Selection, Selection /* newSize */) { }
259  virtual void deleteSelection(Selection) { }
260 
261  virtual void copy(View *, Selection, Clipboard & /* to */) { }
262 
270  virtual bool paste(View *,
271  const Clipboard & /* from */,
272  int /* frameOffset */,
273  bool /* interactive */) { return false; }
274 
275  // Text mode:
276  //
277  // Label nearest feature. We need to get the feature coordinates
278  // and current label from the layer, and then the pane can pop up
279  // a little text entry dialog at the right location. Or we edit
280  // in place? Probably the dialog is easier.
281 
292  virtual bool isLayerScrollable(const View *) const { return true; }
293 
302  virtual bool isLayerOpaque() const { return false; }
303 
310  };
311 
331 
338  virtual bool isLayerEditable() const { return false; }
339 
347  virtual int getCompletion(View *) const { return 100; }
348 
354  virtual QString getError(View *) const { return ""; }
355 
356  virtual void setObjectName(const QString &name);
357 
365  virtual void toXml(QTextStream &stream, QString indent = "",
366  QString extraAttributes = "") const;
367 
373  virtual void setProperties(const QXmlAttributes &) = 0;
374 
381  virtual void toBriefXml(QTextStream &stream,
382  QString indent = "",
383  QString extraAttributes = "") const;
384 
390  virtual void addMeasurementRect(const QXmlAttributes &);
391 
403  virtual void setLayerDormant(const View *v, bool dormant);
404 
409  virtual bool isLayerDormant(const View *v) const;
410 
411  virtual PlayParameters *getPlayParameters();
412 
413  virtual bool needsTextLabelHeight() const { return false; }
414 
415  virtual bool hasTimeXAxis() const { return true; }
416 
426  virtual bool getValueExtents(float &min, float &max,
427  bool &logarithmic, QString &unit) const = 0;
428 
437  virtual bool getDisplayExtents(float & /* min */,
438  float & /* max */) const {
439  return false;
440  }
441 
449  virtual bool setDisplayExtents(float /* min */,
450  float /* max */) {
451  return false;
452  }
453 
460  virtual bool getXScaleValue(const View *v, int x,
461  float &value, QString &unit) const;
462 
467  virtual bool getYScaleValue(const View *, int /* y */,
468  float &/* value */, QString &/* unit */) const {
469  return false;
470  }
471 
478  virtual bool getYScaleDifference(const View *v, int y0, int y1,
479  float &diff, QString &unit) const;
480 
495  virtual int getVerticalZoomSteps(int & /* defaultStep */) const { return 0; }
496 
503  virtual int getCurrentVerticalZoomStep() const { return 0; }
504 
511  virtual void setVerticalZoomStep(int) { }
512 
519  virtual RangeMapper *getNewVerticalZoomRangeMapper() const { return 0; }
520 
521 public slots:
522  void showLayer(View *, bool show);
523 
524 signals:
525  void modelChanged();
526  void modelCompletionChanged();
528  void modelChangedWithin(int startFrame, int endFrame);
529  void modelReplaced();
530 
531  void layerParametersChanged();
534  void layerNameChanged();
535 
536  void verticalZoomChanged();
537 
538 protected:
539  void connectSignals(const Model *);
540 
541  virtual int alignToReference(View *v, int frame) const;
542  virtual int alignFromReference(View *v, int frame) const;
543  bool clipboardHasDifferentAlignment(View *v, const Clipboard &clip) const;
544 
545  struct MeasureRect {
546 
547  mutable QRect pixrect;
549  int startFrame; // only valid if haveFrames
550  int endFrame; // ditto
551  double startY;
552  double endY;
553 
554  bool operator<(const MeasureRect &mr) const;
555  void toXml(QTextStream &stream, QString indent) const;
556  };
557 
558  class AddMeasurementRectCommand : public Command
559  {
560  public:
562  m_layer(layer), m_rect(rect) { }
563 
564  virtual QString getName() const;
565  virtual void execute();
566  virtual void unexecute();
567 
568  private:
571  };
572 
573  class DeleteMeasurementRectCommand : public Command
574  {
575  public:
577  m_layer(layer), m_rect(rect) { }
578 
579  virtual QString getName() const;
580  virtual void execute();
581  virtual void unexecute();
582 
583  private:
586  };
587 
589  m_measureRects.insert(r);
591  }
592 
594  m_measureRects.erase(r);
596  }
597 
598  typedef std::set<MeasureRect> MeasureRectSet;
604 
605  // Note that pixrects are only correct for a single view.
606  // So we should update them at the start of the paint procedure
607  // (painting is single threaded) and only use them after that.
608  void updateMeasurePixrects(View *v) const;
609 
610  virtual void updateMeasureRectYCoords(View *v, const MeasureRect &r) const;
611  virtual void setMeasureRectYCoord(View *v, MeasureRect &r, bool start, int y) const;
612  virtual void setMeasureRectFromPixrect(View *v, MeasureRect &r, QRect pixrect) const;
613 
614  // This assumes updateMeasurementPixrects has been called
615  MeasureRectSet::const_iterator findFocusedMeasureRect(QPoint) const;
616 
617  void paintMeasurementRect(View *v, QPainter &paint,
618  const MeasureRect &r, bool focus) const;
619 
621 
622 private:
623  mutable QMutex m_dormancyMutex;
624  mutable std::map<const void *, bool> m_dormancy;
625 };
626 
627 #endif
628 
MeasureRectSet m_measureRects
Definition: Layer.h:599
void toXml(QTextStream &stream, QString indent) const
Definition: Layer.cpp:299
virtual void setSynchronousPainting(bool)
Enable or disable synchronous painting.
Definition: Layer.h:104
void modelChangedWithin(int startFrame, int endFrame)
QPoint m_currentMeasureRectPoint
Definition: Layer.h:603
void deleteMeasureRectFromSet(const MeasureRect &r)
Definition: Layer.h:593
The base class for visual representations of the data found in a Model.
Definition: Layer.h:52
bool m_haveCurrentMeasureRect
Definition: Layer.h:602
virtual void paintVerticalScale(View *, bool, QPainter &, QRect) const
Definition: Layer.h:134
virtual QString getLabelPreceding(int) const
Definition: Layer.h:153
virtual void addMeasurementRect(const QXmlAttributes &)
Add a measurement rectangle from the given XML attributes (presumably taken from a measurement elemen...
Definition: Layer.cpp:317
Layer()
Definition: Layer.cpp:37
virtual bool isLayerScrollable(const View *) const
This should return true if the layer can safely be scrolled automatically by a given view (simply cop...
Definition: Layer.h:292
virtual bool hasTimeXAxis() const
Definition: Layer.h:415
virtual void setProperties(const QXmlAttributes &)=0
Set the particular properties of a layer (those specific to the subclass) from a set of XML attribute...
virtual void drawStart(View *, QMouseEvent *)
Definition: Layer.h:220
MeasureRect m_draggingRect
Definition: Layer.h:600
virtual QString getPropertyContainerName() const
Definition: Layer.h:121
AddMeasurementRectCommand(Layer *layer, MeasureRect rect)
Definition: Layer.h:561
virtual bool snapToFeatureFrame(View *, int &, int &resolution, SnapType) const
Adjust the given frame to snap to the nearest feature, if possible.
Definition: Layer.h:183
virtual void eraseDrag(View *, QMouseEvent *)
Definition: Layer.h:225
virtual void splitEnd(View *, QMouseEvent *)
Definition: Layer.h:233
void connectSignals(const Model *)
Definition: Layer.cpp:49
virtual int getCurrentVerticalZoomStep() const
Get the current vertical zoom step.
Definition: Layer.h:503
virtual QPixmap getLayerPresentationPixmap(QSize) const
Definition: Layer.h:129
void showLayer(View *, bool show)
Definition: Layer.cpp:135
virtual void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
Convert the layer's data (though not those of the model it refers to) into XML for file output.
Definition: Layer.cpp:592
void updateMeasurePixrects(View *v) const
Definition: Layer.cpp:471
virtual bool snapToSimilarFeature(View *, int &, int &resolution, SnapType) const
Adjust the given frame to snap to the next feature that has "effectively" the same value as the featu...
Definition: Layer.h:207
virtual void measureDoubleClick(View *, QMouseEvent *)
Definition: Layer.cpp:408
void modelReplaced()
DeleteMeasurementRectCommand(Layer *layer, MeasureRect rect)
Definition: Layer.h:576
QMutex m_dormancyMutex
Definition: Layer.h:623
virtual QString getName() const
Definition: Layer.cpp:356
void layerMeasurementRectsChanged()
virtual bool nearestMeasurementRectChanged(View *, QPoint prev, QPoint now) const
Definition: Layer.cpp:460
virtual void measureDrag(View *, QMouseEvent *)
Definition: Layer.cpp:382
virtual void toBriefXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
Produce XML containing the layer's ID and type.
Definition: Layer.cpp:626
void paintMeasurementRect(View *v, QPainter &paint, const MeasureRect &r, bool focus) const
Definition: Layer.cpp:569
ColourSignificance
Definition: Layer.h:304
virtual bool getCrosshairExtents(View *, QPainter &, QPoint, std::vector< QRect > &) const
Definition: Layer.h:137
virtual bool getYScaleValue(const View *, int, float &, QString &) const
Return the value and unit at the given y coordinate in the given view.
Definition: Layer.h:467
virtual ColourSignificance getLayerColourSignificance() const =0
This should return the degree of meaning associated with colour in this layer.
VerticalPosition
Definition: Layer.h:106
virtual void drawEnd(View *, QMouseEvent *)
Definition: Layer.h:222
virtual void updateMeasureRectYCoords(View *v, const MeasureRect &r) const
Definition: Layer.cpp:510
virtual VerticalPosition getPreferredFrameCountPosition() const
Definition: Layer.h:112
virtual void setLayerDormant(const View *v, bool dormant)
Indicate that a layer is not currently visible in the given view and is not expected to become visibl...
Definition: Layer.cpp:118
virtual void eraseStart(View *, QMouseEvent *)
Definition: Layer.h:224
virtual void splitStart(View *, QMouseEvent *)
Definition: Layer.h:232
void layerParameterRangesChanged()
virtual void setObjectName(const QString &name)
Definition: Layer.cpp:100
virtual bool isLayerEditable() const
This should return true if the layer can be edited by the user.
Definition: Layer.h:338
virtual void addNote(View *, QMouseEvent *)
Definition: Layer.h:234
bool operator<(const MeasureRect &mr) const
Definition: Layer.cpp:269
virtual QString getName() const
Definition: Layer.cpp:338
virtual void setMeasureRectFromPixrect(View *v, MeasureRect &r, QRect pixrect) const
Definition: Layer.cpp:529
virtual void moveSelection(Selection, int)
Definition: Layer.h:257
Model * getModel()
Definition: Layer.h:62
virtual void deleteCurrentMeasureRect()
Definition: Layer.cpp:414
virtual bool getDisplayExtents(float &, float &) const
Return the minimum and maximum values within the displayed range for the y axis, if only a subset of ...
Definition: Layer.h:437
virtual void paintMeasurementRects(View *, QPainter &, bool showFocus, QPoint focusPoint) const
Definition: Layer.cpp:428
virtual void setVerticalZoomStep(int)
Set the vertical zoom step.
Definition: Layer.h:511
virtual void setMeasureRectYCoord(View *v, MeasureRect &r, bool start, int y) const
Definition: Layer.cpp:518
void layerParametersChanged()
void verticalZoomChanged()
virtual bool setDisplayExtents(float, float)
Set the displayed minimum and maximum values for the y axis to the given range, if supported.
Definition: Layer.h:449
SnapType
Definition: Layer.h:157
virtual void deleteSelection(Selection)
Definition: Layer.h:259
virtual bool haveCurrentMeasureRect() const
Definition: Layer.h:245
virtual bool paste(View *, const Clipboard &, int, bool)
Paste from the given clipboard onto the layer at the given frame offset.
Definition: Layer.h:270
std::map< const void *, bool > m_dormancy
Definition: Layer.h:624
virtual int getCompletion(View *) const
Return the proportion of background work complete in drawing this view, as a percentage – in most cas...
Definition: Layer.h:347
bool clipboardHasDifferentAlignment(View *v, const Clipboard &clip) const
Definition: Layer.cpp:193
virtual QString getPropertyContainerIconName() const
Definition: Layer.cpp:65
virtual bool hasLightBackground() const
Definition: Layer.h:115
virtual void eraseEnd(View *, QMouseEvent *)
Definition: Layer.h:226
virtual RangeMapper * getNewVerticalZoomRangeMapper() const
Create and return a range mapper for vertical zoom step values.
Definition: Layer.h:519
virtual bool isLayerOpaque() const
This should return true if the layer completely obscures any underlying layers.
Definition: Layer.h:302
virtual bool supportsOtherZoomLevels() const
Return true if this layer can handle zoom levels other than those supported by its zoom constraint (p...
Definition: Layer.h:81
void modelChanged()
virtual void measureEnd(View *, QMouseEvent *)
Definition: Layer.cpp:394
virtual int getVerticalZoomSteps(int &) const
Get the number of vertical zoom steps available for this layer.
Definition: Layer.h:495
virtual void editDrag(View *, QMouseEvent *)
Definition: Layer.h:229
virtual bool getYScaleDifference(const View *v, int y0, int y1, float &diff, QString &unit) const
Return the difference between the values at the given y coordinates in the given view,...
Definition: Layer.cpp:155
bool m_haveDraggingRect
Definition: Layer.h:601
std::set< MeasureRect > MeasureRectSet
Definition: Layer.h:598
void modelAlignmentCompletionChanged()
virtual int alignFromReference(View *v, int frame) const
Definition: Layer.cpp:181
virtual int alignToReference(View *v, int frame) const
Definition: Layer.cpp:169
virtual bool getValueExtents(float &min, float &max, bool &logarithmic, QString &unit) const =0
Return the minimum and maximum values for the y axis of the model in this layer, as well as whether t...
virtual QString getError(View *) const
Return an error string if any errors have occurred while loading or processing data for the given vie...
Definition: Layer.h:354
virtual const Model * getModel() const =0
View is the base class of widgets that display one or more overlaid views of data against a horizonta...
Definition: View.h:50
virtual ~Layer()
Definition: Layer.cpp:43
virtual void editStart(View *, QMouseEvent *)
Definition: Layer.h:228
virtual void copy(View *, Selection, Clipboard &)
Definition: Layer.h:261
virtual QString getFeatureDescription(View *, QPoint &) const
Definition: Layer.h:149
virtual bool getXScaleValue(const View *v, int x, float &value, QString &unit) const
Return the value and unit at the given x coordinate in the given view.
Definition: Layer.cpp:142
virtual void setPresentationName(QString name)
Definition: Layer.cpp:72
double startY
Definition: Layer.h:551
void modelCompletionChanged()
QString m_presentationName
Definition: Layer.h:620
virtual void paint(View *, QPainter &, QRect) const =0
Paint the given rectangle of this layer onto the given view using the given painter,...
virtual void measureStart(View *, QMouseEvent *)
Definition: Layer.cpp:374
void addMeasureRectToSet(const MeasureRect &r)
Definition: Layer.h:588
virtual void resizeSelection(Selection, Selection)
Definition: Layer.h:258
virtual void drawDrag(View *, QMouseEvent *)
Definition: Layer.h:221
virtual void editEnd(View *, QMouseEvent *)
Definition: Layer.h:230
virtual void paintCrosshairs(View *, QPainter &, QPoint) const
Definition: Layer.h:141
virtual int getVerticalScaleWidth(View *, bool detailed, QPainter &) const =0
MeasureRectSet::const_iterator findFocusedMeasureRect(QPoint) const
Definition: Layer.cpp:542
virtual QString getLayerPresentationName() const
Definition: Layer.cpp:78
virtual bool editOpen(View *, QMouseEvent *)
Open an editor on the item under the mouse (e.g.
Definition: Layer.h:255
virtual VerticalPosition getPreferredTimeRulerPosition() const
Definition: Layer.h:109
virtual PlayParameters * getPlayParameters()
Definition: Layer.cpp:107
void layerNameChanged()
virtual bool isLayerDormant(const View *v) const
Return whether the layer is dormant (i.e.
Definition: Layer.cpp:126
virtual bool needsTextLabelHeight() const
Definition: Layer.h:413
virtual const ZoomConstraint * getZoomConstraint() const
Return a zoom constraint object defining the supported zoom levels for this layer.
Definition: Layer.h:71