comparison layer/TimeRulerLayer.cpp @ 44:ad214997dddb

* Refactor Layer classes so as no longer to store a single View pointer; instead they need to be able to draw themselves on any View on demand. Layers with caches (e.g. spectrogram) will need to be further refactored so as to maintain a per-View cache * Begin refactoring MainWindow by pulling out the document stuff (set of layers, models etc) into a Document class. Not yet in use. This revision is fairly unstable.
author Chris Cannam
date Thu, 02 Mar 2006 16:58:49 +0000
parents ea6fe8cfcdd5
children 128ebfeeebee
comparison
equal deleted inserted replaced
43:78515b1e29eb 44:ad214997dddb
18 #include <iostream> 18 #include <iostream>
19 19
20 using std::cerr; 20 using std::cerr;
21 using std::endl; 21 using std::endl;
22 22
23 TimeRulerLayer::TimeRulerLayer(View *w) : 23 TimeRulerLayer::TimeRulerLayer() :
24 Layer(w), 24 Layer(),
25 m_model(0), 25 m_model(0),
26 m_colour(Qt::black), 26 m_colour(Qt::black),
27 m_labelHeight(LabelTop) 27 m_labelHeight(LabelTop)
28 { 28 {
29 m_view->addLayer(this); 29
30 } 30 }
31 31
32 void 32 void
33 TimeRulerLayer::setModel(Model *model) 33 TimeRulerLayer::setModel(Model *model)
34 { 34 {
118 } 118 }
119 } 119 }
120 } 120 }
121 121
122 void 122 void
123 TimeRulerLayer::paint(QPainter &paint, QRect rect) const 123 TimeRulerLayer::paint(View *v, QPainter &paint, QRect rect) const
124 { 124 {
125 // std::cerr << "TimeRulerLayer::paint (" << rect.x() << "," << rect.y() 125 // std::cerr << "TimeRulerLayer::paint (" << rect.x() << "," << rect.y()
126 // << ") [" << rect.width() << "x" << rect.height() << "]" << std::endl; 126 // << ") [" << rect.width() << "x" << rect.height() << "]" << std::endl;
127 127
128 if (!m_model || !m_model->isOK()) return; 128 if (!m_model || !m_model->isOK()) return;
129 129
130 int sampleRate = m_model->getSampleRate(); 130 int sampleRate = m_model->getSampleRate();
131 if (!sampleRate) return; 131 if (!sampleRate) return;
132 132
133 long startFrame = m_view->getStartFrame(); 133 long startFrame = v->getStartFrame();
134 long endFrame = m_view->getEndFrame(); 134 long endFrame = v->getEndFrame();
135 135
136 int zoomLevel = m_view->getZoomLevel(); 136 int zoomLevel = v->getZoomLevel();
137 137
138 long rectStart = startFrame + (rect.x() - 100) * zoomLevel; 138 long rectStart = startFrame + (rect.x() - 100) * zoomLevel;
139 long rectEnd = startFrame + (rect.x() + rect.width() + 100) * zoomLevel; 139 long rectEnd = startFrame + (rect.x() + rect.width() + 100) * zoomLevel;
140 if (rectStart < startFrame) rectStart = startFrame; 140 if (rectStart < startFrame) rectStart = startFrame;
141 if (rectEnd > endFrame) rectEnd = endFrame; 141 if (rectEnd > endFrame) rectEnd = endFrame;
142 142
143 // std::cerr << "TimeRulerLayer::paint: calling paint.save()" << std::endl; 143 // std::cerr << "TimeRulerLayer::paint: calling paint.save()" << std::endl;
144 paint.save(); 144 paint.save();
145 //!!! paint.setClipRect(m_view->rect()); 145 //!!! paint.setClipRect(v->rect());
146 146
147 int minPixelSpacing = 50; 147 int minPixelSpacing = 50;
148 148
149 RealTime rtStart = RealTime::frame2RealTime(startFrame, sampleRate); 149 RealTime rtStart = RealTime::frame2RealTime(startFrame, sampleRate);
150 RealTime rtEnd = RealTime::frame2RealTime(endFrame, sampleRate); 150 RealTime rtEnd = RealTime::frame2RealTime(endFrame, sampleRate);
151 // cerr << "startFrame " << startFrame << ", endFrame " << m_view->getEndFrame() << ", rtStart " << rtStart << ", rtEnd " << rtEnd << endl; 151 // cerr << "startFrame " << startFrame << ", endFrame " << v->getEndFrame() << ", rtStart " << rtStart << ", rtEnd " << rtEnd << endl;
152 int count = m_view->width() / minPixelSpacing; 152 int count = v->width() / minPixelSpacing;
153 if (count < 1) count = 1; 153 if (count < 1) count = 1;
154 RealTime rtGap = (rtEnd - rtStart) / count; 154 RealTime rtGap = (rtEnd - rtStart) / count;
155 // cerr << "rtGap is " << rtGap << endl; 155 // cerr << "rtGap is " << rtGap << endl;
156 156
157 int incms; 157 int incms;
215 215
216 int x = (frame - startFrame) / zoomLevel; 216 int x = (frame - startFrame) / zoomLevel;
217 if (x < rect.x() || x >= rect.x() + rect.width()) continue; 217 if (x < rect.x() || x >= rect.x() + rect.width()) continue;
218 218
219 paint.setPen(greyColour); 219 paint.setPen(greyColour);
220 paint.drawLine(x, 0, x, m_view->height()); 220 paint.drawLine(x, 0, x, v->height());
221 221
222 paint.setPen(m_colour); 222 paint.setPen(m_colour);
223 paint.drawLine(x, 0, x, 5); 223 paint.drawLine(x, 0, x, 5);
224 paint.drawLine(x, m_view->height() - 6, x, m_view->height() - 1); 224 paint.drawLine(x, v->height() - 6, x, v->height() - 1);
225 225
226 QString text(QString::fromStdString(rt.toText())); 226 QString text(QString::fromStdString(rt.toText()));
227 227
228 int y; 228 int y;
229 QFontMetrics metrics = paint.fontMetrics(); 229 QFontMetrics metrics = paint.fontMetrics();
231 default: 231 default:
232 case LabelTop: 232 case LabelTop:
233 y = 6 + metrics.ascent(); 233 y = 6 + metrics.ascent();
234 break; 234 break;
235 case LabelMiddle: 235 case LabelMiddle:
236 y = m_view->height() / 2 - metrics.height() / 2 + metrics.ascent(); 236 y = v->height() / 2 - metrics.height() / 2 + metrics.ascent();
237 break; 237 break;
238 case LabelBottom: 238 case LabelBottom:
239 y = m_view->height() - metrics.height() + metrics.ascent() - 6; 239 y = v->height() - metrics.height() + metrics.ascent() - 6;
240 } 240 }
241 241
242 int tw = metrics.width(text); 242 int tw = metrics.width(text);
243 243
244 paint.setPen(m_view->palette().background().color()); 244 paint.setPen(v->palette().background().color());
245 245
246 //!!! simple drawing function for this please 246 //!!! simple drawing function for this please
247 //!!! and need getContrastingColour() in widget, or use the 247 //!!! and need getContrastingColour() in widget, or use the
248 //palette properly -- get the base class able to draw text 248 //palette properly -- get the base class able to draw text
249 //using the proper colour (or this technique) automatically 249 //using the proper colour (or this technique) automatically
265 x = (frame - startFrame) / zoomLevel; 265 x = (frame - startFrame) / zoomLevel;
266 int sz = 5; 266 int sz = 5;
267 if (ticks == 10) { 267 if (ticks == 10) {
268 if ((i % 2) == 1) { 268 if ((i % 2) == 1) {
269 if (i == 5) { 269 if (i == 5) {
270 paint.drawLine(x, 0, x, m_view->height()); 270 paint.drawLine(x, 0, x, v->height());
271 } else sz = 3; 271 } else sz = 3;
272 } else { 272 } else {
273 sz = 7; 273 sz = 7;
274 } 274 }
275 } 275 }
276 paint.drawLine(x, 0, x, sz); 276 paint.drawLine(x, 0, x, sz);
277 paint.drawLine(x, m_view->height() - sz - 1, x, m_view->height() - 1); 277 paint.drawLine(x, v->height() - sz - 1, x, v->height() - 1);
278 } 278 }
279 } 279 }
280 280
281 paint.restore(); 281 paint.restore();
282 } 282 }