Chris@35
|
1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@35
|
2
|
Chris@35
|
3 /*
|
Chris@35
|
4 A waveform viewer and audio annotation editor.
|
Chris@35
|
5 Chris Cannam, Queen Mary University of London, 2005-2006
|
Chris@35
|
6
|
Chris@35
|
7 This is experimental software. Not for distribution.
|
Chris@35
|
8 */
|
Chris@35
|
9
|
Chris@35
|
10 #include "TextLayer.h"
|
Chris@35
|
11
|
Chris@35
|
12 #include "base/Model.h"
|
Chris@35
|
13 #include "base/RealTime.h"
|
Chris@35
|
14 #include "base/Profiler.h"
|
Chris@35
|
15 #include "base/View.h"
|
Chris@35
|
16
|
Chris@35
|
17 #include "model/TextModel.h"
|
Chris@35
|
18
|
Chris@35
|
19 #include <QPainter>
|
Chris@35
|
20 #include <QMouseEvent>
|
Chris@35
|
21
|
Chris@35
|
22 #include <iostream>
|
Chris@35
|
23 #include <cmath>
|
Chris@35
|
24
|
Chris@35
|
25 TextLayer::TextLayer(View *w) :
|
Chris@35
|
26 Layer(w),
|
Chris@35
|
27 m_model(0),
|
Chris@35
|
28 m_editing(false),
|
Chris@35
|
29 m_originalPoint(0, 0.0, tr("Empty Label")),
|
Chris@35
|
30 m_editingPoint(0, 0.0, tr("Empty Label")),
|
Chris@35
|
31 m_editingCommand(0),
|
Chris@35
|
32 m_colour(255, 150, 50) // orange
|
Chris@35
|
33 {
|
Chris@35
|
34 m_view->addLayer(this);
|
Chris@35
|
35 }
|
Chris@35
|
36
|
Chris@35
|
37 void
|
Chris@35
|
38 TextLayer::setModel(TextModel *model)
|
Chris@35
|
39 {
|
Chris@35
|
40 if (m_model == model) return;
|
Chris@35
|
41 m_model = model;
|
Chris@35
|
42
|
Chris@35
|
43 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
|
Chris@35
|
44 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
|
Chris@35
|
45 this, SIGNAL(modelChanged(size_t, size_t)));
|
Chris@35
|
46
|
Chris@35
|
47 connect(m_model, SIGNAL(completionChanged()),
|
Chris@35
|
48 this, SIGNAL(modelCompletionChanged()));
|
Chris@35
|
49
|
Chris@35
|
50 std::cerr << "TextLayer::setModel(" << model << ")" << std::endl;
|
Chris@35
|
51
|
Chris@35
|
52 emit modelReplaced();
|
Chris@35
|
53 }
|
Chris@35
|
54
|
Chris@35
|
55 Layer::PropertyList
|
Chris@35
|
56 TextLayer::getProperties() const
|
Chris@35
|
57 {
|
Chris@35
|
58 PropertyList list;
|
Chris@35
|
59 list.push_back(tr("Colour"));
|
Chris@35
|
60 return list;
|
Chris@35
|
61 }
|
Chris@35
|
62
|
Chris@35
|
63 Layer::PropertyType
|
Chris@35
|
64 TextLayer::getPropertyType(const PropertyName &name) const
|
Chris@35
|
65 {
|
Chris@35
|
66 return ValueProperty;
|
Chris@35
|
67 }
|
Chris@35
|
68
|
Chris@35
|
69 int
|
Chris@35
|
70 TextLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@35
|
71 int *min, int *max) const
|
Chris@35
|
72 {
|
Chris@35
|
73 //!!! factor this colour handling stuff out into a colour manager class
|
Chris@35
|
74
|
Chris@35
|
75 int deft = 0;
|
Chris@35
|
76
|
Chris@35
|
77 if (name == tr("Colour")) {
|
Chris@35
|
78
|
Chris@35
|
79 if (min) *min = 0;
|
Chris@35
|
80 if (max) *max = 5;
|
Chris@35
|
81
|
Chris@35
|
82 if (m_colour == Qt::black) deft = 0;
|
Chris@35
|
83 else if (m_colour == Qt::darkRed) deft = 1;
|
Chris@35
|
84 else if (m_colour == Qt::darkBlue) deft = 2;
|
Chris@35
|
85 else if (m_colour == Qt::darkGreen) deft = 3;
|
Chris@35
|
86 else if (m_colour == QColor(200, 50, 255)) deft = 4;
|
Chris@35
|
87 else if (m_colour == QColor(255, 150, 50)) deft = 5;
|
Chris@35
|
88
|
Chris@35
|
89 } else {
|
Chris@35
|
90
|
Chris@35
|
91 deft = Layer::getPropertyRangeAndValue(name, min, max);
|
Chris@35
|
92 }
|
Chris@35
|
93
|
Chris@35
|
94 return deft;
|
Chris@35
|
95 }
|
Chris@35
|
96
|
Chris@35
|
97 QString
|
Chris@35
|
98 TextLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@35
|
99 int value) const
|
Chris@35
|
100 {
|
Chris@35
|
101 if (name == tr("Colour")) {
|
Chris@35
|
102 switch (value) {
|
Chris@35
|
103 default:
|
Chris@35
|
104 case 0: return tr("Black");
|
Chris@35
|
105 case 1: return tr("Red");
|
Chris@35
|
106 case 2: return tr("Blue");
|
Chris@35
|
107 case 3: return tr("Green");
|
Chris@35
|
108 case 4: return tr("Purple");
|
Chris@35
|
109 case 5: return tr("Orange");
|
Chris@35
|
110 }
|
Chris@35
|
111 }
|
Chris@35
|
112 return tr("<unknown>");
|
Chris@35
|
113 }
|
Chris@35
|
114
|
Chris@35
|
115 void
|
Chris@35
|
116 TextLayer::setProperty(const PropertyName &name, int value)
|
Chris@35
|
117 {
|
Chris@35
|
118 if (name == tr("Colour")) {
|
Chris@35
|
119 switch (value) {
|
Chris@35
|
120 default:
|
Chris@35
|
121 case 0: setBaseColour(Qt::black); break;
|
Chris@35
|
122 case 1: setBaseColour(Qt::darkRed); break;
|
Chris@35
|
123 case 2: setBaseColour(Qt::darkBlue); break;
|
Chris@35
|
124 case 3: setBaseColour(Qt::darkGreen); break;
|
Chris@35
|
125 case 4: setBaseColour(QColor(200, 50, 255)); break;
|
Chris@35
|
126 case 5: setBaseColour(QColor(255, 150, 50)); break;
|
Chris@35
|
127 }
|
Chris@35
|
128 }
|
Chris@35
|
129 }
|
Chris@35
|
130
|
Chris@35
|
131 void
|
Chris@35
|
132 TextLayer::setBaseColour(QColor colour)
|
Chris@35
|
133 {
|
Chris@35
|
134 if (m_colour == colour) return;
|
Chris@35
|
135 m_colour = colour;
|
Chris@35
|
136 emit layerParametersChanged();
|
Chris@35
|
137 }
|
Chris@35
|
138
|
Chris@35
|
139 bool
|
Chris@35
|
140 TextLayer::isLayerScrollable() const
|
Chris@35
|
141 {
|
Chris@35
|
142 QPoint discard;
|
Chris@35
|
143 return !m_view->shouldIlluminateLocalFeatures(this, discard);
|
Chris@35
|
144 }
|
Chris@35
|
145
|
Chris@35
|
146
|
Chris@35
|
147 TextModel::PointList
|
Chris@35
|
148 TextLayer::getLocalPoints(int x, int y) const
|
Chris@35
|
149 {
|
Chris@35
|
150 if (!m_model) return TextModel::PointList();
|
Chris@35
|
151
|
Chris@35
|
152 long frame0 = getFrameForX(-150);
|
Chris@35
|
153 long frame1 = getFrameForX(m_view->width() + 150);
|
Chris@35
|
154
|
Chris@35
|
155 TextModel::PointList points(m_model->getPoints(frame0, frame1));
|
Chris@35
|
156
|
Chris@35
|
157 TextModel::PointList rv;
|
Chris@35
|
158 QFontMetrics metrics = QPainter().fontMetrics();
|
Chris@35
|
159
|
Chris@35
|
160 for (TextModel::PointList::iterator i = points.begin();
|
Chris@35
|
161 i != points.end(); ++i) {
|
Chris@35
|
162
|
Chris@35
|
163 const TextModel::Point &p(*i);
|
Chris@35
|
164
|
Chris@35
|
165 int px = getXForFrame(p.frame);
|
Chris@35
|
166 int py = getYForHeight(p.height);
|
Chris@35
|
167
|
Chris@35
|
168 QString label = p.label;
|
Chris@35
|
169 if (label == "") {
|
Chris@35
|
170 label = tr("<no text>");
|
Chris@35
|
171 }
|
Chris@35
|
172
|
Chris@35
|
173 QRect rect = metrics.boundingRect
|
Chris@35
|
174 (QRect(0, 0, 150, 200),
|
Chris@35
|
175 Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, label);
|
Chris@35
|
176
|
Chris@35
|
177 if (py + rect.height() > m_view->height()) {
|
Chris@35
|
178 if (rect.height() > m_view->height()) py = 0;
|
Chris@35
|
179 else py = m_view->height() - rect.height() - 1;
|
Chris@35
|
180 }
|
Chris@35
|
181
|
Chris@35
|
182 if (x >= px && x < px + rect.width() &&
|
Chris@35
|
183 y >= py && y < py + rect.height()) {
|
Chris@35
|
184 rv.insert(p);
|
Chris@35
|
185 }
|
Chris@35
|
186 }
|
Chris@35
|
187
|
Chris@35
|
188 return rv;
|
Chris@35
|
189 }
|
Chris@35
|
190
|
Chris@35
|
191 QString
|
Chris@35
|
192 TextLayer::getFeatureDescription(QPoint &pos) const
|
Chris@35
|
193 {
|
Chris@35
|
194 int x = pos.x();
|
Chris@35
|
195
|
Chris@35
|
196 if (!m_model || !m_model->getSampleRate()) return "";
|
Chris@35
|
197
|
Chris@35
|
198 TextModel::PointList points = getLocalPoints(x, pos.y());
|
Chris@35
|
199
|
Chris@35
|
200 if (points.empty()) {
|
Chris@35
|
201 if (!m_model->isReady()) {
|
Chris@35
|
202 return tr("In progress");
|
Chris@35
|
203 } else {
|
Chris@35
|
204 return "";
|
Chris@35
|
205 }
|
Chris@35
|
206 }
|
Chris@35
|
207
|
Chris@35
|
208 long useFrame = points.begin()->frame;
|
Chris@35
|
209
|
Chris@35
|
210 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
|
Chris@35
|
211
|
Chris@35
|
212 QString text;
|
Chris@35
|
213
|
Chris@35
|
214 if (points.begin()->label == "") {
|
Chris@35
|
215 text = QString(tr("Time:\t%1\nHeight:\t%2\nLabel:\t%3"))
|
Chris@35
|
216 .arg(rt.toText(true).c_str())
|
Chris@35
|
217 .arg(points.begin()->height)
|
Chris@35
|
218 .arg(points.begin()->label);
|
Chris@35
|
219 }
|
Chris@35
|
220
|
Chris@35
|
221 pos = QPoint(getXForFrame(useFrame), getYForHeight(points.begin()->height));
|
Chris@35
|
222 return text;
|
Chris@35
|
223 }
|
Chris@35
|
224
|
Chris@35
|
225
|
Chris@35
|
226 //!!! too much overlap with TimeValueLayer/TimeInstantLayer
|
Chris@35
|
227
|
Chris@35
|
228 bool
|
Chris@35
|
229 TextLayer::snapToFeatureFrame(int &frame,
|
Chris@35
|
230 size_t &resolution,
|
Chris@35
|
231 SnapType snap) const
|
Chris@35
|
232 {
|
Chris@35
|
233 if (!m_model) {
|
Chris@35
|
234 return Layer::snapToFeatureFrame(frame, resolution, snap);
|
Chris@35
|
235 }
|
Chris@35
|
236
|
Chris@35
|
237 resolution = m_model->getResolution();
|
Chris@35
|
238 TextModel::PointList points;
|
Chris@35
|
239
|
Chris@35
|
240 if (snap == SnapNeighbouring) {
|
Chris@35
|
241
|
Chris@35
|
242 points = getLocalPoints(getXForFrame(frame), -1);
|
Chris@35
|
243 if (points.empty()) return false;
|
Chris@35
|
244 frame = points.begin()->frame;
|
Chris@35
|
245 return true;
|
Chris@35
|
246 }
|
Chris@35
|
247
|
Chris@35
|
248 points = m_model->getPoints(frame, frame);
|
Chris@35
|
249 int snapped = frame;
|
Chris@35
|
250 bool found = false;
|
Chris@35
|
251
|
Chris@35
|
252 for (TextModel::PointList::const_iterator i = points.begin();
|
Chris@35
|
253 i != points.end(); ++i) {
|
Chris@35
|
254
|
Chris@35
|
255 if (snap == SnapRight) {
|
Chris@35
|
256
|
Chris@35
|
257 if (i->frame > frame) {
|
Chris@35
|
258 snapped = i->frame;
|
Chris@35
|
259 found = true;
|
Chris@35
|
260 break;
|
Chris@35
|
261 }
|
Chris@35
|
262
|
Chris@35
|
263 } else if (snap == SnapLeft) {
|
Chris@35
|
264
|
Chris@35
|
265 if (i->frame <= frame) {
|
Chris@35
|
266 snapped = i->frame;
|
Chris@35
|
267 found = true; // don't break, as the next may be better
|
Chris@35
|
268 } else {
|
Chris@35
|
269 break;
|
Chris@35
|
270 }
|
Chris@35
|
271
|
Chris@35
|
272 } else { // nearest
|
Chris@35
|
273
|
Chris@35
|
274 TextModel::PointList::const_iterator j = i;
|
Chris@35
|
275 ++j;
|
Chris@35
|
276
|
Chris@35
|
277 if (j == points.end()) {
|
Chris@35
|
278
|
Chris@35
|
279 snapped = i->frame;
|
Chris@35
|
280 found = true;
|
Chris@35
|
281 break;
|
Chris@35
|
282
|
Chris@35
|
283 } else if (j->frame >= frame) {
|
Chris@35
|
284
|
Chris@35
|
285 if (j->frame - frame < frame - i->frame) {
|
Chris@35
|
286 snapped = j->frame;
|
Chris@35
|
287 } else {
|
Chris@35
|
288 snapped = i->frame;
|
Chris@35
|
289 }
|
Chris@35
|
290 found = true;
|
Chris@35
|
291 break;
|
Chris@35
|
292 }
|
Chris@35
|
293 }
|
Chris@35
|
294 }
|
Chris@35
|
295
|
Chris@35
|
296 frame = snapped;
|
Chris@35
|
297 return found;
|
Chris@35
|
298 }
|
Chris@35
|
299
|
Chris@35
|
300 int
|
Chris@35
|
301 TextLayer::getYForHeight(float height) const
|
Chris@35
|
302 {
|
Chris@35
|
303 int h = m_view->height();
|
Chris@35
|
304 return h - int(height * h);
|
Chris@35
|
305 }
|
Chris@35
|
306
|
Chris@35
|
307 float
|
Chris@35
|
308 TextLayer::getHeightForY(int y) const
|
Chris@35
|
309 {
|
Chris@35
|
310 int h = m_view->height();
|
Chris@35
|
311 return float(h - y) / h;
|
Chris@35
|
312 }
|
Chris@35
|
313
|
Chris@35
|
314 void
|
Chris@35
|
315 TextLayer::paint(QPainter &paint, QRect rect) const
|
Chris@35
|
316 {
|
Chris@35
|
317 if (!m_model || !m_model->isOK()) return;
|
Chris@35
|
318
|
Chris@35
|
319 int sampleRate = m_model->getSampleRate();
|
Chris@35
|
320 if (!sampleRate) return;
|
Chris@35
|
321
|
Chris@35
|
322 // Profiler profiler("TextLayer::paint", true);
|
Chris@35
|
323
|
Chris@35
|
324 int x0 = rect.left(), x1 = rect.right();
|
Chris@35
|
325 long frame0 = getFrameForX(x0);
|
Chris@35
|
326 long frame1 = getFrameForX(x1);
|
Chris@35
|
327
|
Chris@35
|
328 TextModel::PointList points(m_model->getPoints(frame0, frame1));
|
Chris@35
|
329 if (points.empty()) return;
|
Chris@35
|
330
|
Chris@35
|
331 QColor brushColour(m_colour);
|
Chris@35
|
332 brushColour.setAlpha(80);
|
Chris@35
|
333 paint.setBrush(brushColour);
|
Chris@35
|
334
|
Chris@35
|
335 if (m_view->hasLightBackground()) {
|
Chris@35
|
336 paint.setPen(Qt::black);
|
Chris@35
|
337 } else {
|
Chris@35
|
338 paint.setPen(Qt::white);
|
Chris@35
|
339 }
|
Chris@35
|
340
|
Chris@35
|
341 // std::cerr << "TextLayer::paint: resolution is "
|
Chris@35
|
342 // << m_model->getResolution() << " frames" << std::endl;
|
Chris@35
|
343
|
Chris@35
|
344 QPoint localPos;
|
Chris@35
|
345 long illuminateFrame = -1;
|
Chris@35
|
346
|
Chris@35
|
347 if (m_view->shouldIlluminateLocalFeatures(this, localPos)) {
|
Chris@35
|
348 TextModel::PointList localPoints = getLocalPoints(localPos.x(),
|
Chris@35
|
349 localPos.y());
|
Chris@35
|
350 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
|
Chris@35
|
351 }
|
Chris@35
|
352
|
Chris@35
|
353 int boxMaxWidth = 150;
|
Chris@35
|
354 int boxMaxHeight = 200;
|
Chris@35
|
355
|
Chris@35
|
356 paint.save();
|
Chris@35
|
357 paint.setClipRect(rect.x(), 0, rect.width() + boxMaxWidth, m_view->height());
|
Chris@35
|
358
|
Chris@35
|
359 for (TextModel::PointList::const_iterator i = points.begin();
|
Chris@35
|
360 i != points.end(); ++i) {
|
Chris@35
|
361
|
Chris@35
|
362 const TextModel::Point &p(*i);
|
Chris@35
|
363
|
Chris@35
|
364 int x = getXForFrame(p.frame);
|
Chris@35
|
365 int y = getYForHeight(p.height);
|
Chris@35
|
366
|
Chris@35
|
367 if (illuminateFrame == p.frame) {
|
Chris@35
|
368 /*
|
Chris@35
|
369 //!!! aside from the problem of choosing a colour, it'd be
|
Chris@35
|
370 //better to save the highlighted rects and draw them at
|
Chris@35
|
371 //the end perhaps
|
Chris@35
|
372
|
Chris@35
|
373 //!!! not equipped to illuminate the right section in line
|
Chris@35
|
374 //or curve mode
|
Chris@35
|
375
|
Chris@35
|
376 if (m_plotStyle != PlotCurve &&
|
Chris@35
|
377 m_plotStyle != PlotLines) {
|
Chris@35
|
378 paint.setPen(Qt::black);//!!!
|
Chris@35
|
379 if (m_plotStyle != PlotSegmentation) {
|
Chris@35
|
380 paint.setBrush(Qt::black);//!!!
|
Chris@35
|
381 }
|
Chris@35
|
382 }
|
Chris@35
|
383 */
|
Chris@35
|
384 }
|
Chris@35
|
385
|
Chris@35
|
386 QString label = p.label;
|
Chris@35
|
387 if (label == "") {
|
Chris@35
|
388 label = tr("<no text>");
|
Chris@35
|
389 }
|
Chris@35
|
390
|
Chris@35
|
391 QRect boxRect = paint.fontMetrics().boundingRect
|
Chris@35
|
392 (QRect(0, 0, boxMaxWidth, boxMaxHeight),
|
Chris@35
|
393 Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, label);
|
Chris@35
|
394
|
Chris@35
|
395 QRect textRect = QRect(3, 2, boxRect.width(), boxRect.height());
|
Chris@35
|
396 boxRect = QRect(0, 0, boxRect.width() + 6, boxRect.height() + 2);
|
Chris@35
|
397
|
Chris@35
|
398 if (y + boxRect.height() > m_view->height()) {
|
Chris@35
|
399 if (boxRect.height() > m_view->height()) y = 0;
|
Chris@35
|
400 else y = m_view->height() - boxRect.height() - 1;
|
Chris@35
|
401 }
|
Chris@35
|
402
|
Chris@35
|
403 boxRect = QRect(x, y, boxRect.width(), boxRect.height());
|
Chris@35
|
404 textRect = QRect(x + 3, y + 2, textRect.width(), textRect.height());
|
Chris@35
|
405
|
Chris@35
|
406 // boxRect = QRect(x, y, boxRect.width(), boxRect.height());
|
Chris@35
|
407 // textRect = QRect(x + 3, y + 2, textRect.width(), textRect.height());
|
Chris@35
|
408
|
Chris@35
|
409 paint.setRenderHint(QPainter::Antialiasing, false);
|
Chris@35
|
410 paint.drawRect(boxRect);
|
Chris@35
|
411
|
Chris@35
|
412 paint.setRenderHint(QPainter::Antialiasing, true);
|
Chris@35
|
413 paint.drawText(textRect,
|
Chris@35
|
414 Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap,
|
Chris@35
|
415 label);
|
Chris@35
|
416
|
Chris@35
|
417 /// if (p.label != "") {
|
Chris@35
|
418 /// paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label);
|
Chris@35
|
419 /// }
|
Chris@35
|
420 }
|
Chris@35
|
421
|
Chris@35
|
422 paint.restore();
|
Chris@35
|
423
|
Chris@35
|
424 // looks like save/restore doesn't deal with this:
|
Chris@35
|
425 paint.setRenderHint(QPainter::Antialiasing, false);
|
Chris@35
|
426 }
|
Chris@35
|
427
|
Chris@35
|
428 void
|
Chris@35
|
429 TextLayer::drawStart(QMouseEvent *e)
|
Chris@35
|
430 {
|
Chris@35
|
431 std::cerr << "TextLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@35
|
432
|
Chris@35
|
433 if (!m_model) {
|
Chris@35
|
434 std::cerr << "TextLayer::drawStart: no model" << std::endl;
|
Chris@35
|
435 return;
|
Chris@35
|
436 }
|
Chris@35
|
437
|
Chris@35
|
438 long frame = getFrameForX(e->x());
|
Chris@35
|
439 if (frame < 0) frame = 0;
|
Chris@35
|
440 frame = frame / m_model->getResolution() * m_model->getResolution();
|
Chris@35
|
441
|
Chris@35
|
442 float height = getHeightForY(e->y());
|
Chris@35
|
443
|
Chris@35
|
444 m_editingPoint = TextModel::Point(frame, height, "");
|
Chris@35
|
445 m_originalPoint = m_editingPoint;
|
Chris@35
|
446
|
Chris@35
|
447 if (m_editingCommand) m_editingCommand->finish();
|
Chris@35
|
448 m_editingCommand = new TextModel::EditCommand(m_model, "Add Label");
|
Chris@35
|
449 m_editingCommand->addPoint(m_editingPoint);
|
Chris@35
|
450
|
Chris@35
|
451 m_editing = true;
|
Chris@35
|
452 }
|
Chris@35
|
453
|
Chris@35
|
454 void
|
Chris@35
|
455 TextLayer::drawDrag(QMouseEvent *e)
|
Chris@35
|
456 {
|
Chris@35
|
457 std::cerr << "TextLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@35
|
458
|
Chris@35
|
459 if (!m_model || !m_editing) return;
|
Chris@35
|
460
|
Chris@35
|
461 long frame = getFrameForX(e->x());
|
Chris@35
|
462 if (frame < 0) frame = 0;
|
Chris@35
|
463 frame = frame / m_model->getResolution() * m_model->getResolution();
|
Chris@35
|
464
|
Chris@35
|
465 float height = getHeightForY(e->y());
|
Chris@35
|
466
|
Chris@35
|
467 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@35
|
468 m_editingPoint.frame = frame;
|
Chris@35
|
469 m_editingPoint.height = height;
|
Chris@35
|
470 m_editingCommand->addPoint(m_editingPoint);
|
Chris@35
|
471 }
|
Chris@35
|
472
|
Chris@35
|
473 void
|
Chris@35
|
474 TextLayer::drawEnd(QMouseEvent *e)
|
Chris@35
|
475 {
|
Chris@35
|
476 std::cerr << "TextLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@35
|
477 if (!m_model || !m_editing) return;
|
Chris@35
|
478 m_editingCommand->finish();
|
Chris@35
|
479 m_editingCommand = 0;
|
Chris@35
|
480 m_editing = false;
|
Chris@35
|
481 }
|
Chris@35
|
482
|
Chris@35
|
483 void
|
Chris@35
|
484 TextLayer::editStart(QMouseEvent *e)
|
Chris@35
|
485 {
|
Chris@35
|
486 std::cerr << "TextLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@35
|
487
|
Chris@35
|
488 if (!m_model) return;
|
Chris@35
|
489
|
Chris@35
|
490 TextModel::PointList points = getLocalPoints(e->x(), e->y());
|
Chris@35
|
491 if (points.empty()) return;
|
Chris@35
|
492
|
Chris@35
|
493 m_editingPoint = *points.begin();
|
Chris@35
|
494 m_originalPoint = m_editingPoint;
|
Chris@35
|
495
|
Chris@35
|
496 if (m_editingCommand) {
|
Chris@35
|
497 m_editingCommand->finish();
|
Chris@35
|
498 m_editingCommand = 0;
|
Chris@35
|
499 }
|
Chris@35
|
500
|
Chris@35
|
501 m_editing = true;
|
Chris@35
|
502 }
|
Chris@35
|
503
|
Chris@35
|
504 void
|
Chris@35
|
505 TextLayer::editDrag(QMouseEvent *e)
|
Chris@35
|
506 {
|
Chris@35
|
507 if (!m_model || !m_editing) return;
|
Chris@35
|
508
|
Chris@35
|
509 long frame = getFrameForX(e->x());
|
Chris@35
|
510 if (frame < 0) frame = 0;
|
Chris@35
|
511 frame = frame / m_model->getResolution() * m_model->getResolution();
|
Chris@35
|
512
|
Chris@35
|
513 float height = getHeightForY(e->y());
|
Chris@35
|
514
|
Chris@35
|
515 if (!m_editingCommand) {
|
Chris@35
|
516 m_editingCommand = new TextModel::EditCommand(m_model, tr("Drag Label"));
|
Chris@35
|
517 }
|
Chris@35
|
518
|
Chris@35
|
519 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@35
|
520 m_editingPoint.frame = frame;
|
Chris@35
|
521 m_editingPoint.height = height;
|
Chris@35
|
522 m_editingCommand->addPoint(m_editingPoint);
|
Chris@35
|
523 }
|
Chris@35
|
524
|
Chris@35
|
525 void
|
Chris@35
|
526 TextLayer::editEnd(QMouseEvent *e)
|
Chris@35
|
527 {
|
Chris@35
|
528 std::cerr << "TextLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@35
|
529 if (!m_model || !m_editing) return;
|
Chris@35
|
530
|
Chris@35
|
531 if (m_editingCommand) {
|
Chris@35
|
532
|
Chris@35
|
533 QString newName = m_editingCommand->getName();
|
Chris@35
|
534
|
Chris@35
|
535 if (m_editingPoint.frame != m_originalPoint.frame) {
|
Chris@35
|
536 if (m_editingPoint.height != m_originalPoint.height) {
|
Chris@35
|
537 newName = tr("Move Label");
|
Chris@35
|
538 } else {
|
Chris@35
|
539 newName = tr("Relocate Label");
|
Chris@35
|
540 }
|
Chris@35
|
541 } else {
|
Chris@35
|
542 newName = tr("Change Point Height");
|
Chris@35
|
543 }
|
Chris@35
|
544
|
Chris@35
|
545 m_editingCommand->setName(newName);
|
Chris@35
|
546 m_editingCommand->finish();
|
Chris@35
|
547 }
|
Chris@35
|
548
|
Chris@35
|
549 m_editingCommand = 0;
|
Chris@35
|
550 m_editing = false;
|
Chris@35
|
551 }
|
Chris@35
|
552
|
Chris@35
|
553 QString
|
Chris@35
|
554 TextLayer::toXmlString(QString indent, QString extraAttributes) const
|
Chris@35
|
555 {
|
Chris@35
|
556 return Layer::toXmlString(indent, extraAttributes +
|
Chris@35
|
557 QString(" colour=\"%1\"")
|
Chris@35
|
558 .arg(encodeColour(m_colour)));
|
Chris@35
|
559 }
|
Chris@35
|
560
|
Chris@35
|
561 void
|
Chris@35
|
562 TextLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@35
|
563 {
|
Chris@35
|
564 QString colourSpec = attributes.value("colour");
|
Chris@35
|
565 if (colourSpec != "") {
|
Chris@35
|
566 QColor colour(colourSpec);
|
Chris@35
|
567 if (colour.isValid()) {
|
Chris@35
|
568 setBaseColour(QColor(colourSpec));
|
Chris@35
|
569 }
|
Chris@35
|
570 }
|
Chris@35
|
571 }
|
Chris@35
|
572
|
Chris@35
|
573
|
Chris@35
|
574 #ifdef INCLUDE_MOCFILES
|
Chris@35
|
575 #include "TextLayer.moc.cpp"
|
Chris@35
|
576 #endif
|
Chris@35
|
577
|