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