comparison layer/TextLayer.cpp @ 946:36cddc3de023 alignment_view

Merge from default branch
author Chris Cannam
date Mon, 20 Apr 2015 09:19:52 +0100
parents 4a578a360011
children 94e4952a6774
comparison
equal deleted inserted replaced
897:499b637f2a26 946:36cddc3de023
94 { 94 {
95 SingleColourLayer::setProperty(name, value); 95 SingleColourLayer::setProperty(name, value);
96 } 96 }
97 97
98 bool 98 bool
99 TextLayer::getValueExtents(float &, float &, bool &, QString &) const 99 TextLayer::getValueExtents(double &, double &, bool &, QString &) const
100 { 100 {
101 return false; 101 return false;
102 } 102 }
103 103
104 bool 104 bool
112 TextModel::PointList 112 TextModel::PointList
113 TextLayer::getLocalPoints(View *v, int x, int y) const 113 TextLayer::getLocalPoints(View *v, int x, int y) const
114 { 114 {
115 if (!m_model) return TextModel::PointList(); 115 if (!m_model) return TextModel::PointList();
116 116
117 long frame0 = v->getFrameForX(-150); 117 sv_frame_t frame0 = v->getFrameForX(-150);
118 long frame1 = v->getFrameForX(v->width() + 150); 118 sv_frame_t frame1 = v->getFrameForX(v->width() + 150);
119 119
120 TextModel::PointList points(m_model->getPoints(frame0, frame1)); 120 TextModel::PointList points(m_model->getPoints(frame0, frame1));
121 121
122 TextModel::PointList rv; 122 TextModel::PointList rv;
123 QFontMetrics metrics = QFontMetrics(QFont()); 123 QFontMetrics metrics = QFontMetrics(QFont());
156 bool 156 bool
157 TextLayer::getPointToDrag(View *v, int x, int y, TextModel::Point &p) const 157 TextLayer::getPointToDrag(View *v, int x, int y, TextModel::Point &p) const
158 { 158 {
159 if (!m_model) return false; 159 if (!m_model) return false;
160 160
161 long a = v->getFrameForX(x - 120); 161 sv_frame_t a = v->getFrameForX(x - 120);
162 long b = v->getFrameForX(x + 10); 162 sv_frame_t b = v->getFrameForX(x + 10);
163 TextModel::PointList onPoints = m_model->getPoints(a, b); 163 TextModel::PointList onPoints = m_model->getPoints(a, b);
164 if (onPoints.empty()) return false; 164 if (onPoints.empty()) return false;
165 165
166 float nearestDistance = -1; 166 double nearestDistance = -1;
167 167
168 for (TextModel::PointList::const_iterator i = onPoints.begin(); 168 for (TextModel::PointList::const_iterator i = onPoints.begin();
169 i != onPoints.end(); ++i) { 169 i != onPoints.end(); ++i) {
170 170
171 int yd = getYForHeight(v, (*i).height) - y; 171 double yd = getYForHeight(v, (*i).height) - y;
172 int xd = v->getXForFrame((*i).frame) - x; 172 double xd = v->getXForFrame((*i).frame) - x;
173 float distance = sqrtf(yd*yd + xd*xd); 173 double distance = sqrt(yd*yd + xd*xd);
174 174
175 if (nearestDistance == -1 || distance < nearestDistance) { 175 if (nearestDistance == -1 || distance < nearestDistance) {
176 nearestDistance = distance; 176 nearestDistance = distance;
177 p = *i; 177 p = *i;
178 } 178 }
196 } else { 196 } else {
197 return ""; 197 return "";
198 } 198 }
199 } 199 }
200 200
201 long useFrame = points.begin()->frame; 201 sv_frame_t useFrame = points.begin()->frame;
202 202
203 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate()); 203 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
204 204
205 QString text; 205 QString text;
206 206
218 218
219 219
220 //!!! too much overlap with TimeValueLayer/TimeInstantLayer 220 //!!! too much overlap with TimeValueLayer/TimeInstantLayer
221 221
222 bool 222 bool
223 TextLayer::snapToFeatureFrame(View *v, int &frame, 223 TextLayer::snapToFeatureFrame(View *v, sv_frame_t &frame,
224 int &resolution, 224 int &resolution,
225 SnapType snap) const 225 SnapType snap) const
226 { 226 {
227 if (!m_model) { 227 if (!m_model) {
228 return Layer::snapToFeatureFrame(v, frame, resolution, snap); 228 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
238 frame = points.begin()->frame; 238 frame = points.begin()->frame;
239 return true; 239 return true;
240 } 240 }
241 241
242 points = m_model->getPoints(frame, frame); 242 points = m_model->getPoints(frame, frame);
243 int snapped = frame; 243 sv_frame_t snapped = frame;
244 bool found = false; 244 bool found = false;
245 245
246 for (TextModel::PointList::const_iterator i = points.begin(); 246 for (TextModel::PointList::const_iterator i = points.begin();
247 i != points.end(); ++i) { 247 i != points.end(); ++i) {
248 248
290 frame = snapped; 290 frame = snapped;
291 return found; 291 return found;
292 } 292 }
293 293
294 int 294 int
295 TextLayer::getYForHeight(View *v, float height) const 295 TextLayer::getYForHeight(View *v, double height) const
296 { 296 {
297 int h = v->height(); 297 int h = v->height();
298 return h - int(height * h); 298 return h - int(height * h);
299 } 299 }
300 300
301 float 301 double
302 TextLayer::getHeightForY(View *v, int y) const 302 TextLayer::getHeightForY(View *v, int y) const
303 { 303 {
304 int h = v->height(); 304 int h = v->height();
305 return float(h - y) / h; 305 return double(h - y) / h;
306 } 306 }
307 307
308 void 308 void
309 TextLayer::paint(View *v, QPainter &paint, QRect rect) const 309 TextLayer::paint(View *v, QPainter &paint, QRect rect) const
310 { 310 {
311 if (!m_model || !m_model->isOK()) return; 311 if (!m_model || !m_model->isOK()) return;
312 312
313 int sampleRate = m_model->getSampleRate(); 313 sv_samplerate_t sampleRate = m_model->getSampleRate();
314 if (!sampleRate) return; 314 if (!sampleRate) return;
315 315
316 // Profiler profiler("TextLayer::paint", true); 316 // Profiler profiler("TextLayer::paint", true);
317 317
318 int x0 = rect.left(), x1 = rect.right(); 318 int x0 = rect.left(), x1 = rect.right();
319 long frame0 = v->getFrameForX(x0); 319 sv_frame_t frame0 = v->getFrameForX(x0);
320 long frame1 = v->getFrameForX(x1); 320 sv_frame_t frame1 = v->getFrameForX(x1);
321 321
322 TextModel::PointList points(m_model->getPoints(frame0, frame1)); 322 TextModel::PointList points(m_model->getPoints(frame0, frame1));
323 if (points.empty()) return; 323 if (points.empty()) return;
324 324
325 QColor brushColour(getBaseQColor()); 325 QColor brushColour(getBaseQColor());
418 if (!m_model) { 418 if (!m_model) {
419 SVDEBUG << "TextLayer::drawStart: no model" << endl; 419 SVDEBUG << "TextLayer::drawStart: no model" << endl;
420 return; 420 return;
421 } 421 }
422 422
423 long frame = v->getFrameForX(e->x()); 423 sv_frame_t frame = v->getFrameForX(e->x());
424 if (frame < 0) frame = 0; 424 if (frame < 0) frame = 0;
425 frame = frame / m_model->getResolution() * m_model->getResolution(); 425 frame = frame / m_model->getResolution() * m_model->getResolution();
426 426
427 float height = getHeightForY(v, e->y()); 427 double height = getHeightForY(v, e->y());
428 428
429 m_editingPoint = TextModel::Point(frame, height, ""); 429 m_editingPoint = TextModel::Point(frame, float(height), "");
430 m_originalPoint = m_editingPoint; 430 m_originalPoint = m_editingPoint;
431 431
432 if (m_editingCommand) finish(m_editingCommand); 432 if (m_editingCommand) finish(m_editingCommand);
433 m_editingCommand = new TextModel::EditCommand(m_model, "Add Label"); 433 m_editingCommand = new TextModel::EditCommand(m_model, "Add Label");
434 m_editingCommand->addPoint(m_editingPoint); 434 m_editingCommand->addPoint(m_editingPoint);
441 { 441 {
442 // SVDEBUG << "TextLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl; 442 // SVDEBUG << "TextLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl;
443 443
444 if (!m_model || !m_editing) return; 444 if (!m_model || !m_editing) return;
445 445
446 long frame = v->getFrameForX(e->x()); 446 sv_frame_t frame = v->getFrameForX(e->x());
447 if (frame < 0) frame = 0; 447 if (frame < 0) frame = 0;
448 frame = frame / m_model->getResolution() * m_model->getResolution(); 448 frame = frame / m_model->getResolution() * m_model->getResolution();
449 449
450 float height = getHeightForY(v, e->y()); 450 double height = getHeightForY(v, e->y());
451 451
452 m_editingCommand->deletePoint(m_editingPoint); 452 m_editingCommand->deletePoint(m_editingPoint);
453 m_editingPoint.frame = frame; 453 m_editingPoint.frame = frame;
454 m_editingPoint.height = height; 454 m_editingPoint.height = float(height);
455 m_editingCommand->addPoint(m_editingPoint); 455 m_editingCommand->addPoint(m_editingPoint);
456 } 456 }
457 457
458 void 458 void
459 TextLayer::drawEnd(View *v, QMouseEvent *) 459 TextLayer::drawEnd(View *v, QMouseEvent *)
545 void 545 void
546 TextLayer::editDrag(View *v, QMouseEvent *e) 546 TextLayer::editDrag(View *v, QMouseEvent *e)
547 { 547 {
548 if (!m_model || !m_editing) return; 548 if (!m_model || !m_editing) return;
549 549
550 long frameDiff = v->getFrameForX(e->x()) - v->getFrameForX(m_editOrigin.x()); 550 sv_frame_t frameDiff = v->getFrameForX(e->x()) - v->getFrameForX(m_editOrigin.x());
551 float heightDiff = getHeightForY(v, e->y()) - getHeightForY(v, m_editOrigin.y()); 551 double heightDiff = getHeightForY(v, e->y()) - getHeightForY(v, m_editOrigin.y());
552 552
553 long frame = m_originalPoint.frame + frameDiff; 553 sv_frame_t frame = m_originalPoint.frame + frameDiff;
554 float height = m_originalPoint.height + heightDiff; 554 double height = m_originalPoint.height + heightDiff;
555 555
556 // long frame = v->getFrameForX(e->x()); 556 // sv_frame_t frame = v->getFrameForX(e->x());
557 if (frame < 0) frame = 0; 557 if (frame < 0) frame = 0;
558 frame = (frame / m_model->getResolution()) * m_model->getResolution(); 558 frame = (frame / m_model->getResolution()) * m_model->getResolution();
559 559
560 // float height = getHeightForY(v, e->y()); 560 // double height = getHeightForY(v, e->y());
561 561
562 if (!m_editingCommand) { 562 if (!m_editingCommand) {
563 m_editingCommand = new TextModel::EditCommand(m_model, tr("Drag Label")); 563 m_editingCommand = new TextModel::EditCommand(m_model, tr("Drag Label"));
564 } 564 }
565 565
566 m_editingCommand->deletePoint(m_editingPoint); 566 m_editingCommand->deletePoint(m_editingPoint);
567 m_editingPoint.frame = frame; 567 m_editingPoint.frame = frame;
568 m_editingPoint.height = height; 568 m_editingPoint.height = float(height);
569 m_editingCommand->addPoint(m_editingPoint); 569 m_editingCommand->addPoint(m_editingPoint);
570 } 570 }
571 571
572 void 572 void
573 TextLayer::editEnd(View *, QMouseEvent *) 573 TextLayer::editEnd(View *, QMouseEvent *)
619 619
620 return true; 620 return true;
621 } 621 }
622 622
623 void 623 void
624 TextLayer::moveSelection(Selection s, int newStartFrame) 624 TextLayer::moveSelection(Selection s, sv_frame_t newStartFrame)
625 { 625 {
626 if (!m_model) return; 626 if (!m_model) return;
627 627
628 TextModel::EditCommand *command = 628 TextModel::EditCommand *command =
629 new TextModel::EditCommand(m_model, tr("Drag Selection")); 629 new TextModel::EditCommand(m_model, tr("Drag Selection"));
663 for (TextModel::PointList::iterator i = points.begin(); 663 for (TextModel::PointList::iterator i = points.begin();
664 i != points.end(); ++i) { 664 i != points.end(); ++i) {
665 665
666 if (s.contains(i->frame)) { 666 if (s.contains(i->frame)) {
667 667
668 double target = i->frame; 668 double target = double(i->frame);
669 target = newSize.getStartFrame() + 669 target = double(newSize.getStartFrame()) +
670 double(target - s.getStartFrame()) * ratio; 670 target - double(s.getStartFrame()) * ratio;
671 671
672 TextModel::Point newPoint(*i); 672 TextModel::Point newPoint(*i);
673 newPoint.frame = lrint(target); 673 newPoint.frame = lrint(target);
674 command->deletePoint(*i); 674 command->deletePoint(*i);
675 command->addPoint(newPoint); 675 command->addPoint(newPoint);
715 } 715 }
716 } 716 }
717 } 717 }
718 718
719 bool 719 bool
720 TextLayer::paste(View *v, const Clipboard &from, int /* frameOffset */, bool /* interactive */) 720 TextLayer::paste(View *v, const Clipboard &from, sv_frame_t /* frameOffset */, bool /* interactive */)
721 { 721 {
722 if (!m_model) return false; 722 if (!m_model) return false;
723 723
724 const Clipboard::PointList &points = from.getPoints(); 724 const Clipboard::PointList &points = from.getPoints();
725 725
743 } 743 }
744 744
745 TextModel::EditCommand *command = 745 TextModel::EditCommand *command =
746 new TextModel::EditCommand(m_model, tr("Paste")); 746 new TextModel::EditCommand(m_model, tr("Paste"));
747 747
748 float valueMin = 0.0, valueMax = 1.0; 748 double valueMin = 0.0, valueMax = 1.0;
749 for (Clipboard::PointList::const_iterator i = points.begin(); 749 for (Clipboard::PointList::const_iterator i = points.begin();
750 i != points.end(); ++i) { 750 i != points.end(); ++i) {
751 if (i->haveValue()) { 751 if (i->haveValue()) {
752 if (i->getValue() < valueMin) valueMin = i->getValue(); 752 if (i->getValue() < valueMin) valueMin = i->getValue();
753 if (i->getValue() > valueMax) valueMax = i->getValue(); 753 if (i->getValue() > valueMax) valueMax = i->getValue();
757 757
758 for (Clipboard::PointList::const_iterator i = points.begin(); 758 for (Clipboard::PointList::const_iterator i = points.begin();
759 i != points.end(); ++i) { 759 i != points.end(); ++i) {
760 760
761 if (!i->haveFrame()) continue; 761 if (!i->haveFrame()) continue;
762 int frame = 0; 762 sv_frame_t frame = 0;
763 763
764 if (!realign) { 764 if (!realign) {
765 765
766 frame = i->getFrame(); 766 frame = i->getFrame();
767 767
776 } 776 }
777 777
778 TextModel::Point newPoint(frame); 778 TextModel::Point newPoint(frame);
779 779
780 if (i->haveValue()) { 780 if (i->haveValue()) {
781 newPoint.height = (i->getValue() - valueMin) / (valueMax - valueMin); 781 newPoint.height = float((i->getValue() - valueMin) / (valueMax - valueMin));
782 } else { 782 } else {
783 newPoint.height = 0.5; 783 newPoint.height = 0.5f;
784 } 784 }
785 785
786 if (i->haveLabel()) { 786 if (i->haveLabel()) {
787 newPoint.label = i->getLabel(); 787 newPoint.label = i->getLabel();
788 } else if (i->haveValue()) { 788 } else if (i->haveValue()) {