svgui  1.9
TimeRulerLayer.cpp
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 #include "TimeRulerLayer.h"
17 
18 #include "LayerFactory.h"
19 
20 #include "data/model/Model.h"
21 #include "base/RealTime.h"
22 #include "ColourDatabase.h"
23 #include "view/View.h"
24 
25 #include <QPainter>
26 
27 #include <iostream>
28 #include <cmath>
29 
30 //#define DEBUG_TIME_RULER_LAYER 1
31 
32 
33 
34 
37  m_model(0),
38  m_labelHeight(LabelTop)
39 {
40 
41 }
42 
43 void
45 {
46  if (m_model == model) return;
47  m_model = model;
48  emit modelReplaced();
49 }
50 
51 bool
53  int &resolution, SnapType snap) const
54 {
55  if (!m_model) {
56  resolution = 1;
57  return false;
58  }
59 
60  bool q;
61  int tick = getMajorTickSpacing(v, q);
62  RealTime rtick = RealTime::fromMilliseconds(tick);
63  int rate = m_model->getSampleRate();
64 
65  RealTime rt = RealTime::frame2RealTime(frame, rate);
66  double ratio = rt / rtick;
67 
68  int rounded = int(ratio);
69  RealTime rdrt = rtick * rounded;
70 
71  int left = RealTime::realTime2Frame(rdrt, rate);
72  resolution = RealTime::realTime2Frame(rtick, rate);
73  int right = left + resolution;
74 
75 // SVDEBUG << "TimeRulerLayer::snapToFeatureFrame: type "
76 // << int(snap) << ", frame " << frame << " (time "
77 // << rt << ", tick " << rtick << ", rounded " << rdrt << ") ";
78 
79  switch (snap) {
80 
81  case SnapLeft:
82  frame = left;
83  break;
84 
85  case SnapRight:
86  frame = right;
87  break;
88 
89  case SnapNearest:
90  {
91  if (abs(frame - left) > abs(right - frame)) {
92  frame = right;
93  } else {
94  frame = left;
95  }
96  break;
97  }
98 
99  case SnapNeighbouring:
100  {
101  int dl = -1, dr = -1;
102  int x = v->getXForFrame(frame);
103 
104  if (left > v->getStartFrame() &&
105  left < v->getEndFrame()) {
106  dl = abs(v->getXForFrame(left) - x);
107  }
108 
109  if (right > v->getStartFrame() &&
110  right < v->getEndFrame()) {
111  dr = abs(v->getXForFrame(right) - x);
112  }
113 
114  int fuzz = 2;
115 
116  if (dl >= 0 && dr >= 0) {
117  if (dl < dr) {
118  if (dl <= fuzz) {
119  frame = left;
120  }
121  } else {
122  if (dr < fuzz) {
123  frame = right;
124  }
125  }
126  } else if (dl >= 0) {
127  if (dl <= fuzz) {
128  frame = left;
129  }
130  } else if (dr >= 0) {
131  if (dr <= fuzz) {
132  frame = right;
133  }
134  }
135  }
136  }
137 
138 // SVDEBUG << " -> " << frame << " (resolution = " << resolution << ")" << endl;
139 
140  return true;
141 }
142 
143 int
144 TimeRulerLayer::getMajorTickSpacing(View *v, bool &quarterTicks) const
145 {
146  // return value is in milliseconds
147 
148  if (!m_model || !v) return 1000;
149 
150  int sampleRate = m_model->getSampleRate();
151  if (!sampleRate) return 1000;
152 
153  long startFrame = v->getStartFrame();
154  long endFrame = v->getEndFrame();
155 
156  int minPixelSpacing = 50;
157 
158  RealTime rtStart = RealTime::frame2RealTime(startFrame, sampleRate);
159  RealTime rtEnd = RealTime::frame2RealTime(endFrame, sampleRate);
160 
161  int count = v->width() / minPixelSpacing;
162  if (count < 1) count = 1;
163  RealTime rtGap = (rtEnd - rtStart) / count;
164 
165  int incms;
166  quarterTicks = false;
167 
168  if (rtGap.sec > 0) {
169  incms = 1000;
170  int s = rtGap.sec;
171  if (s > 0) { incms *= 5; s /= 5; }
172  if (s > 0) { incms *= 2; s /= 2; }
173  if (s > 0) { incms *= 6; s /= 6; quarterTicks = true; }
174  if (s > 0) { incms *= 5; s /= 5; quarterTicks = false; }
175  if (s > 0) { incms *= 2; s /= 2; }
176  if (s > 0) { incms *= 6; s /= 6; quarterTicks = true; }
177  while (s > 0) {
178  incms *= 10;
179  s /= 10;
180  quarterTicks = false;
181  }
182  } else {
183  incms = 1;
184  int ms = rtGap.msec();
185  if (ms > 0) { incms *= 10; ms /= 10; }
186  if (ms > 0) { incms *= 10; ms /= 10; }
187  if (ms > 0) { incms *= 5; ms /= 5; }
188  if (ms > 0) { incms *= 2; ms /= 2; }
189  }
190 
191  return incms;
192 }
193 
194 void
195 TimeRulerLayer::paint(View *v, QPainter &paint, QRect rect) const
196 {
197 #ifdef DEBUG_TIME_RULER_LAYER
198  SVDEBUG << "TimeRulerLayer::paint (" << rect.x() << "," << rect.y()
199  << ") [" << rect.width() << "x" << rect.height() << "]" << endl;
200 #endif
201 
202  if (!m_model || !m_model->isOK()) return;
203 
204  int sampleRate = m_model->getSampleRate();
205  if (!sampleRate) return;
206 
207  long startFrame = v->getFrameForX(rect.x() - 50);
208 
209 #ifdef DEBUG_TIME_RULER_LAYER
210  cerr << "start frame = " << startFrame << endl;
211 #endif
212 
213  bool quarter = false;
214  int incms = getMajorTickSpacing(v, quarter);
215 
216  int ms = lrint(1000.0 * (double(startFrame) / double(sampleRate)));
217  ms = (ms / incms) * incms - incms;
218 
219 #ifdef DEBUG_TIME_RULER_LAYER
220  cerr << "start ms = " << ms << " at step " << incms << endl;
221 #endif
222 
223  // Calculate the number of ticks per increment -- approximate
224  // values for x and frame counts here will do, no rounding issue.
225  // We always use the exact incms in our calculations for where to
226  // draw the actual ticks or lines.
227 
228  int minPixelSpacing = 50;
229  long incFrame = (incms * sampleRate) / 1000;
230  int incX = incFrame / v->getZoomLevel();
231  int ticks = 10;
232  if (incX < minPixelSpacing * 2) {
233  ticks = quarter ? 4 : 5;
234  }
235 
236  QColor greyColour = getPartialShades(v)[1];
237 
238  paint.save();
239 
240  // Do not label time zero - we now overlay an opaque area over
241  // time < 0 which would cut it in half
242  int minlabel = 1; // ms
243 
244  while (1) {
245 
246  // frame is used to determine where to draw the lines, so it
247  // needs to correspond to an exact pixel (so that we don't get
248  // a different pixel when scrolling a small amount and
249  // re-drawing with a different start frame).
250 
251  double dms = ms;
252  long frame = lrint((dms * sampleRate) / 1000.0);
253  frame /= v->getZoomLevel();
254  frame *= v->getZoomLevel(); // so frame corresponds to an exact pixel
255 
256  int x = v->getXForFrame(frame);
257 
258 #ifdef DEBUG_TIME_RULER_LAYER
259  SVDEBUG << "Considering frame = " << frame << ", x = " << x << endl;
260 #endif
261 
262  if (x >= rect.x() + rect.width() + 50) {
263 #ifdef DEBUG_TIME_RULER_LAYER
264  cerr << "X well out of range, ending here" << endl;
265 #endif
266  break;
267  }
268 
269  if (x >= rect.x() - 50 && ms >= minlabel) {
270 
271  RealTime rt = RealTime::fromMilliseconds(ms);
272 
273 #ifdef DEBUG_TIME_RULER_LAYER
274  cerr << "X in range, drawing line here for time " << rt.toText() << endl;
275 #endif
276 
277  QString text(QString::fromStdString(rt.toText()));
278  QFontMetrics metrics = paint.fontMetrics();
279  int tw = metrics.width(text);
280 
281  if (tw < 50 &&
282  (x < rect.x() - tw/2 ||
283  x >= rect.x() + rect.width() + tw/2)) {
284 #ifdef DEBUG_TIME_RULER_LAYER
285  cerr << "hm, maybe X isn't in range after all (x = " << x << ", tw = " << tw << ", rect.x() = " << rect.x() << ", rect.width() = " << rect.width() << ")" << endl;
286 #endif
287  }
288 
289  paint.setPen(greyColour);
290  paint.drawLine(x, 0, x, v->height());
291 
292  paint.setPen(getBaseQColor());
293  paint.drawLine(x, 0, x, 5);
294  paint.drawLine(x, v->height() - 6, x, v->height() - 1);
295 
296  int y;
297  switch (m_labelHeight) {
298  default:
299  case LabelTop:
300  y = 6 + metrics.ascent();
301  break;
302  case LabelMiddle:
303  y = v->height() / 2 - metrics.height() / 2 + metrics.ascent();
304  break;
305  case LabelBottom:
306  y = v->height() - metrics.height() + metrics.ascent() - 6;
307  }
308 
309  if (v->getViewManager() && v->getViewManager()->getOverlayMode() !=
311 
312  if (v->getLayer(0) == this) {
313  // backmost layer, don't worry about outlining the text
314  paint.drawText(x+2 - tw/2, y, text);
315  } else {
316  v->drawVisibleText(paint, x+2 - tw/2, y, text, View::OutlinedText);
317  }
318  }
319  }
320 
321  paint.setPen(greyColour);
322 
323  for (int i = 1; i < ticks; ++i) {
324 
325  dms = ms + (i * double(incms)) / ticks;
326  frame = lrint((dms * sampleRate) / 1000.0);
327  frame /= v->getZoomLevel();
328  frame *= v->getZoomLevel(); // exact pixel as above
329 
330  x = v->getXForFrame(frame);
331 
332  if (x < rect.x() || x >= rect.x() + rect.width()) {
333 #ifdef DEBUG_TIME_RULER_LAYER
334 // cerr << "tick " << i << ": X out of range, going on to next tick" << endl;
335 #endif
336  continue;
337  }
338 
339 #ifdef DEBUG_TIME_RULER_LAYER
340  cerr << "tick " << i << " in range, drawing at " << x << endl;
341 #endif
342 
343  int sz = 5;
344  if (ticks == 10) {
345  if ((i % 2) == 1) {
346  if (i == 5) {
347  paint.drawLine(x, 0, x, v->height());
348  } else sz = 3;
349  } else {
350  sz = 7;
351  }
352  }
353  paint.drawLine(x, 0, x, sz);
354  paint.drawLine(x, v->height() - sz - 1, x, v->height() - 1);
355  }
356 
357  ms += incms;
358  }
359 
360  paint.restore();
361 }
362 
363 int
364 TimeRulerLayer::getDefaultColourHint(bool darkbg, bool &impose)
365 {
366  impose = true;
368  (QString(darkbg ? "White" : "Black"));
369 }
370 
372 {
374  QString layerName = factory->getLayerPresentationName
375  (factory->getLayerType(this));
376  return layerName;
377 }
378 
379 void
380 TimeRulerLayer::toXml(QTextStream &stream,
381  QString indent, QString extraAttributes) const
382 {
383  SingleColourLayer::toXml(stream, indent, extraAttributes);
384 }
385 
386 void
387 TimeRulerLayer::setProperties(const QXmlAttributes &attributes)
388 {
390 }
391 
static LayerFactory * getInstance()
int getFrameForX(int x) const
Return the closest frame to the given pixel x-coordinate.
Definition: View.cpp:363
virtual int getDefaultColourHint(bool dark, bool &impose)
void modelReplaced()
void setProperties(const QXmlAttributes &attributes)
Set the particular properties of a layer (those specific to the subclass) from a set of XML attribute...
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.
virtual ViewManager * getViewManager() const
Definition: View.h:233
void setModel(Model *)
int getZoomLevel() const
Return the zoom level, i.e.
Definition: View.cpp:443
virtual void paint(View *v, QPainter &paint, QRect rect) const
Paint the given rectangle of this layer onto the given view using the given painter,...
virtual QColor getBaseQColor() const
virtual bool snapToFeatureFrame(View *, int &, int &, SnapType) const
Adjust the given frame to snap to the nearest feature, if possible.
std::vector< QColor > getPartialShades(View *v) const
int getColourIndex(QString name) const
QString getLayerPresentationName(LayerType type)
int getStartFrame() const
Retrieve the first visible sample frame on the widget.
Definition: View.cpp:302
virtual void setProperties(const QXmlAttributes &attributes)
Set the particular properties of a layer (those specific to the subclass) from a set of XML attribute...
int getEndFrame() const
Retrieve the last visible sample frame on the widget.
Definition: View.cpp:308
SnapType
Definition: Layer.h:157
virtual Layer * getLayer(int n)
Return the nth layer, counted in stacking order.
Definition: View.h:174
LayerType getLayerType(const Layer *)
View is the base class of widgets that display one or more overlaid views of data against a horizonta...
Definition: View.h:50
LabelHeight m_labelHeight
virtual void drawVisibleText(QPainter &p, int x, int y, QString text, TextStyle style) const
Definition: View.cpp:787
virtual QString getLayerPresentationName() const
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.
OverlayMode getOverlayMode() const
Definition: ViewManager.h:194
int getMajorTickSpacing(View *, bool &quarterTicks) const
int getXForFrame(int frame) const
Return the pixel x-coordinate corresponding to a given sample frame (which may be negative).
Definition: View.cpp:357
static ColourDatabase * getInstance()