Chris@127
|
1
|
Chris@127
|
2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@127
|
3
|
Chris@127
|
4 /*
|
Chris@127
|
5 Sonic Visualiser
|
Chris@127
|
6 An audio file viewer and annotation editor.
|
Chris@127
|
7 Centre for Digital Music, Queen Mary, University of London.
|
Chris@127
|
8 This file copyright 2006 Chris Cannam.
|
Chris@127
|
9
|
Chris@127
|
10 This program is free software; you can redistribute it and/or
|
Chris@127
|
11 modify it under the terms of the GNU General Public License as
|
Chris@127
|
12 published by the Free Software Foundation; either version 2 of the
|
Chris@127
|
13 License, or (at your option) any later version. See the file
|
Chris@127
|
14 COPYING included with this distribution for more information.
|
Chris@127
|
15 */
|
Chris@127
|
16
|
Chris@127
|
17 #ifndef _LAYER_H_
|
Chris@127
|
18 #define _LAYER_H_
|
Chris@127
|
19
|
Chris@127
|
20 #include "PropertyContainer.h"
|
Chris@127
|
21 #include "XmlExportable.h"
|
Chris@127
|
22 #include "Selection.h"
|
Chris@127
|
23
|
Chris@127
|
24 #include <QObject>
|
Chris@127
|
25 #include <QRect>
|
Chris@127
|
26 #include <QXmlAttributes>
|
Chris@127
|
27
|
Chris@127
|
28 #include <map>
|
Chris@127
|
29
|
Chris@127
|
30 class ZoomConstraint;
|
Chris@127
|
31 class Model;
|
Chris@127
|
32 class QPainter;
|
Chris@127
|
33 class View;
|
Chris@127
|
34 class QMouseEvent;
|
Chris@127
|
35 class Clipboard;
|
Chris@127
|
36
|
Chris@127
|
37 /**
|
Chris@127
|
38 * The base class for visual representations of the data found in a
|
Chris@127
|
39 * Model. Layers are expected to be able to draw themselves onto a
|
Chris@127
|
40 * View, and may also be editable.
|
Chris@127
|
41 */
|
Chris@127
|
42
|
Chris@127
|
43 class Layer : public PropertyContainer,
|
Chris@127
|
44 public XmlExportable
|
Chris@127
|
45 {
|
Chris@127
|
46 Q_OBJECT
|
Chris@127
|
47
|
Chris@127
|
48 public:
|
Chris@127
|
49 Layer();
|
Chris@127
|
50 virtual ~Layer();
|
Chris@127
|
51
|
Chris@127
|
52 virtual const Model *getModel() const = 0;
|
Chris@127
|
53 virtual Model *getModel() {
|
Chris@127
|
54 return const_cast<Model *>(const_cast<const Layer *>(this)->getModel());
|
Chris@127
|
55 }
|
Chris@127
|
56
|
Chris@127
|
57 virtual const ZoomConstraint *getZoomConstraint() const { return 0; }
|
Chris@127
|
58 virtual void paint(View *, QPainter &, QRect) const = 0;
|
Chris@127
|
59
|
Chris@127
|
60 enum VerticalPosition {
|
Chris@127
|
61 PositionTop, PositionMiddle, PositionBottom
|
Chris@127
|
62 };
|
Chris@127
|
63 virtual VerticalPosition getPreferredTimeRulerPosition() const {
|
Chris@127
|
64 return PositionMiddle;
|
Chris@127
|
65 }
|
Chris@127
|
66 virtual VerticalPosition getPreferredFrameCountPosition() const {
|
Chris@127
|
67 return PositionBottom;
|
Chris@127
|
68 }
|
Chris@127
|
69
|
Chris@127
|
70 virtual QString getPropertyContainerIconName() const;
|
Chris@127
|
71
|
Chris@127
|
72 virtual QString getPropertyContainerName() const {
|
Chris@127
|
73 return objectName();
|
Chris@127
|
74 }
|
Chris@127
|
75
|
Chris@127
|
76 virtual QString getLayerPresentationName() const;
|
Chris@127
|
77
|
Chris@127
|
78 virtual int getVerticalScaleWidth(View *, QPainter &) const { return 0; }
|
Chris@127
|
79 virtual void paintVerticalScale(View *, QPainter &, QRect) const { }
|
Chris@127
|
80
|
Chris@127
|
81 virtual bool getCrosshairExtents(View *, QPainter &, QPoint /* cursorPos */,
|
Chris@127
|
82 std::vector<QRect> &) const {
|
Chris@127
|
83 return false;
|
Chris@127
|
84 }
|
Chris@127
|
85 virtual void paintCrosshairs(View *, QPainter &, QPoint) const { }
|
Chris@127
|
86
|
Chris@127
|
87 virtual QString getFeatureDescription(View *, QPoint &) const {
|
Chris@127
|
88 return "";
|
Chris@127
|
89 }
|
Chris@127
|
90
|
Chris@127
|
91 enum SnapType {
|
Chris@127
|
92 SnapLeft,
|
Chris@127
|
93 SnapRight,
|
Chris@127
|
94 SnapNearest,
|
Chris@127
|
95 SnapNeighbouring
|
Chris@127
|
96 };
|
Chris@127
|
97
|
Chris@127
|
98 /**
|
Chris@127
|
99 * Adjust the given frame to snap to the nearest feature, if
|
Chris@127
|
100 * possible.
|
Chris@127
|
101 *
|
Chris@127
|
102 * If snap is SnapLeft or SnapRight, adjust the frame to match
|
Chris@127
|
103 * that of the nearest feature in the given direction regardless
|
Chris@127
|
104 * of how far away it is. If snap is SnapNearest, adjust the
|
Chris@127
|
105 * frame to that of the nearest feature in either direction. If
|
Chris@127
|
106 * snap is SnapNeighbouring, adjust the frame to that of the
|
Chris@127
|
107 * nearest feature if it is close, and leave it alone (returning
|
Chris@127
|
108 * false) otherwise. SnapNeighbouring should always choose the
|
Chris@127
|
109 * same feature that would be used in an editing operation through
|
Chris@127
|
110 * calls to editStart etc.
|
Chris@127
|
111 *
|
Chris@127
|
112 * Return true if a suitable feature was found and frame adjusted
|
Chris@127
|
113 * accordingly. Return false if no suitable feature was
|
Chris@127
|
114 * available. Also return the resolution of the model in this
|
Chris@127
|
115 * layer in sample frames.
|
Chris@127
|
116 */
|
Chris@127
|
117 virtual bool snapToFeatureFrame(View * /* v */,
|
Chris@127
|
118 int & /* frame */,
|
Chris@127
|
119 size_t &resolution,
|
Chris@127
|
120 SnapType /* snap */) const {
|
Chris@127
|
121 resolution = 1;
|
Chris@127
|
122 return false;
|
Chris@127
|
123 }
|
Chris@127
|
124
|
Chris@127
|
125 // Draw and edit modes:
|
Chris@127
|
126 //
|
Chris@127
|
127 // Layer needs to get actual mouse events, I guess. Draw mode is
|
Chris@127
|
128 // probably the easier.
|
Chris@127
|
129
|
Chris@127
|
130 virtual void drawStart(View *, QMouseEvent *) { }
|
Chris@127
|
131 virtual void drawDrag(View *, QMouseEvent *) { }
|
Chris@127
|
132 virtual void drawEnd(View *, QMouseEvent *) { }
|
Chris@127
|
133
|
Chris@127
|
134 virtual void editStart(View *, QMouseEvent *) { }
|
Chris@127
|
135 virtual void editDrag(View *, QMouseEvent *) { }
|
Chris@127
|
136 virtual void editEnd(View *, QMouseEvent *) { }
|
Chris@127
|
137
|
Chris@127
|
138 virtual void editOpen(View *, QMouseEvent *) { } // on double-click
|
Chris@127
|
139
|
Chris@127
|
140 virtual void moveSelection(Selection, size_t /* newStartFrame */) { }
|
Chris@127
|
141 virtual void resizeSelection(Selection, Selection /* newSize */) { }
|
Chris@127
|
142 virtual void deleteSelection(Selection) { }
|
Chris@127
|
143
|
Chris@127
|
144 virtual void copy(Selection, Clipboard & /* to */) { }
|
Chris@127
|
145
|
Chris@127
|
146 /**
|
Chris@127
|
147 * Paste from the given clipboard onto the layer at the given
|
Chris@127
|
148 * frame offset. If interactive is true, the layer may ask the
|
Chris@127
|
149 * user about paste options through a dialog if desired, and may
|
Chris@127
|
150 * return false if the user cancelled the paste operation. This
|
Chris@127
|
151 * function should return true if a paste actually occurred.
|
Chris@127
|
152 */
|
Chris@127
|
153 virtual bool paste(const Clipboard & /* from */,
|
Chris@127
|
154 int /* frameOffset */,
|
Chris@127
|
155 bool /* interactive */) { return false; }
|
Chris@127
|
156
|
Chris@127
|
157 // Text mode:
|
Chris@127
|
158 //
|
Chris@127
|
159 // Label nearest feature. We need to get the feature coordinates
|
Chris@127
|
160 // and current label from the layer, and then the pane can pop up
|
Chris@127
|
161 // a little text entry dialog at the right location. Or we edit
|
Chris@127
|
162 // in place? Probably the dialog is easier.
|
Chris@127
|
163
|
Chris@127
|
164 /**
|
Chris@127
|
165 * This should return true if the layer can safely be scrolled
|
Chris@127
|
166 * automatically by a given view (simply copying the existing data
|
Chris@127
|
167 * and then refreshing the exposed area) without altering its
|
Chris@127
|
168 * meaning. For the view widget as a whole this is usually not
|
Chris@127
|
169 * possible because of invariant (non-scrolling) material
|
Chris@127
|
170 * displayed over the top, but the widget may be able to optimise
|
Chris@127
|
171 * scrolling better if it is known that individual views can be
|
Chris@127
|
172 * scrolled safely in this way.
|
Chris@127
|
173 */
|
Chris@127
|
174 virtual bool isLayerScrollable(const View *) const { return true; }
|
Chris@127
|
175
|
Chris@127
|
176 /**
|
Chris@127
|
177 * This should return true if the layer completely obscures any
|
Chris@127
|
178 * underlying layers. It's used to determine whether the view can
|
Chris@127
|
179 * safely draw any selection rectangles under the layer instead of
|
Chris@127
|
180 * over it, in the case where the layer is not scrollable and
|
Chris@127
|
181 * therefore needs to be redrawn each time (so that the selection
|
Chris@127
|
182 * rectangle can be cached).
|
Chris@127
|
183 */
|
Chris@127
|
184 virtual bool isLayerOpaque() const { return false; }
|
Chris@127
|
185
|
Chris@127
|
186 /**
|
Chris@127
|
187 * This should return true if the layer can be edited by the user.
|
Chris@127
|
188 * If this is the case, the appropriate edit tools may be made
|
Chris@127
|
189 * available by the application and the layer's drawStart/Drag/End
|
Chris@127
|
190 * and editStart/Drag/End methods should be implemented.
|
Chris@127
|
191 */
|
Chris@127
|
192 virtual bool isLayerEditable() const { return false; }
|
Chris@127
|
193
|
Chris@127
|
194 /**
|
Chris@127
|
195 * Return the proportion of background work complete in drawing
|
Chris@127
|
196 * this view, as a percentage -- in most cases this will be the
|
Chris@127
|
197 * value returned by pointer from a call to the underlying model's
|
Chris@127
|
198 * isReady(int *) call. The widget may choose to show a progress
|
Chris@127
|
199 * meter if it finds that this returns < 100 at any given moment.
|
Chris@127
|
200 */
|
Chris@127
|
201 virtual int getCompletion(View *) const { return 100; }
|
Chris@127
|
202
|
Chris@127
|
203 virtual void setObjectName(const QString &name);
|
Chris@127
|
204
|
Chris@127
|
205 /**
|
Chris@127
|
206 * Convert the layer's data (though not those of the model it
|
Chris@127
|
207 * refers to) into an XML string for file output. This class
|
Chris@127
|
208 * implements the basic name/type/model-id output; subclasses will
|
Chris@127
|
209 * typically call this superclass implementation with extra
|
Chris@127
|
210 * attributes describing their particular properties.
|
Chris@127
|
211 */
|
Chris@127
|
212 virtual QString toXmlString(QString indent = "",
|
Chris@127
|
213 QString extraAttributes = "") const;
|
Chris@127
|
214
|
Chris@127
|
215 /**
|
Chris@127
|
216 * Set the particular properties of a layer (those specific to the
|
Chris@127
|
217 * subclass) from a set of XML attributes. This is the effective
|
Chris@127
|
218 * inverse of the toXmlString method.
|
Chris@127
|
219 */
|
Chris@127
|
220 virtual void setProperties(const QXmlAttributes &) = 0;
|
Chris@127
|
221
|
Chris@127
|
222 /**
|
Chris@127
|
223 * Indicate that a layer is not currently visible in the given
|
Chris@127
|
224 * view and is not expected to become visible in the near future
|
Chris@127
|
225 * (for example because the user has explicitly removed or hidden
|
Chris@127
|
226 * it). The layer may respond by (for example) freeing any cache
|
Chris@127
|
227 * memory it is using, until next time its paint method is called,
|
Chris@127
|
228 * when it should set itself un-dormant again.
|
Chris@127
|
229 */
|
Chris@127
|
230 virtual void setLayerDormant(const View *v, bool dormant) {
|
Chris@127
|
231 m_dormancy[v] = dormant;
|
Chris@127
|
232 }
|
Chris@127
|
233
|
Chris@127
|
234 /**
|
Chris@127
|
235 * Return whether the layer is dormant (i.e. hidden) in the given
|
Chris@127
|
236 * view.
|
Chris@127
|
237 */
|
Chris@127
|
238 virtual bool isLayerDormant(const View *v) const {
|
Chris@127
|
239 if (m_dormancy.find(v) == m_dormancy.end()) return false;
|
Chris@127
|
240 return m_dormancy.find(v)->second;
|
Chris@127
|
241 }
|
Chris@127
|
242
|
Chris@127
|
243 virtual PlayParameters *getPlayParameters();
|
Chris@127
|
244
|
Chris@127
|
245 virtual bool needsTextLabelHeight() const { return false; }
|
Chris@127
|
246
|
Chris@127
|
247 /**
|
Chris@127
|
248 * Return the minimum and maximum values for the y axis of the
|
Chris@127
|
249 * model in this layer, as well as whether the layer is configured
|
Chris@127
|
250 * to use a logarithmic y axis display. Also return the unit for
|
Chris@127
|
251 * these values if known.
|
Chris@127
|
252 *
|
Chris@127
|
253 * This function returns the "normal" extents for the layer, not
|
Chris@127
|
254 * necessarily the extents actually in use in the display.
|
Chris@127
|
255 */
|
Chris@127
|
256 virtual bool getValueExtents(float &min, float &max,
|
Chris@127
|
257 bool &logarithmic, QString &unit) const = 0;
|
Chris@127
|
258
|
Chris@127
|
259 /**
|
Chris@127
|
260 * Return the minimum and maximum values within the displayed
|
Chris@127
|
261 * range for the y axis, if only a subset of the whole range of
|
Chris@127
|
262 * the model (returned by getValueExtents) is being displayed.
|
Chris@127
|
263 * Return false if the layer is not imposing a particular display
|
Chris@127
|
264 * extent (using the normal layer extents or deferring to whatever
|
Chris@127
|
265 * is in use for the same units elsewhere in the view).
|
Chris@127
|
266 */
|
Chris@127
|
267 virtual bool getDisplayExtents(float & /* min */,
|
Chris@127
|
268 float & /* max */) const {
|
Chris@127
|
269 return false;
|
Chris@127
|
270 }
|
Chris@127
|
271
|
Chris@127
|
272 /**
|
Chris@127
|
273 * Set the displayed minimum and maximum values for the y axis to
|
Chris@127
|
274 * the given range, if supported. Return false if not supported
|
Chris@127
|
275 * on this layer (and set nothing). In most cases, layers that
|
Chris@127
|
276 * return false for getDisplayExtents should also return false for
|
Chris@127
|
277 * this function.
|
Chris@127
|
278 */
|
Chris@127
|
279 virtual bool setDisplayExtents(float /* min */,
|
Chris@127
|
280 float /* max */) {
|
Chris@127
|
281 return false;
|
Chris@127
|
282 }
|
Chris@127
|
283
|
Chris@127
|
284 public slots:
|
Chris@127
|
285 void showLayer(View *, bool show);
|
Chris@127
|
286
|
Chris@127
|
287 signals:
|
Chris@127
|
288 void modelChanged();
|
Chris@127
|
289 void modelCompletionChanged();
|
Chris@127
|
290 void modelChanged(size_t startFrame, size_t endFrame);
|
Chris@127
|
291 void modelReplaced();
|
Chris@127
|
292
|
Chris@127
|
293 void layerParametersChanged();
|
Chris@127
|
294 void layerNameChanged();
|
Chris@127
|
295
|
Chris@127
|
296 protected:
|
Chris@127
|
297 mutable std::map<const void *, bool> m_dormancy;
|
Chris@127
|
298 };
|
Chris@127
|
299
|
Chris@127
|
300 #endif
|
Chris@127
|
301
|