comparison layer/ImageLayer.cpp @ 1469:11a150e65ee1 by-id

Some work on updating layers for ModelId bits
author Chris Cannam
date Thu, 27 Jun 2019 13:16:25 +0100
parents 09d008b5c8f4
children 696e569ff21b
comparison
equal deleted inserted replaced
1468:de41a11cabc2 1469:11a150e65ee1
41 41
42 QMutex 42 QMutex
43 ImageLayer::m_imageMapMutex; 43 ImageLayer::m_imageMapMutex;
44 44
45 ImageLayer::ImageLayer() : 45 ImageLayer::ImageLayer() :
46 Layer(),
47 m_model(nullptr),
48 m_editing(false), 46 m_editing(false),
49 m_editingCommand(nullptr) 47 m_editingCommand(nullptr)
50 { 48 {
51 } 49 }
52 50
56 i != m_fileSources.end(); ++i) { 54 i != m_fileSources.end(); ++i) {
57 delete i->second; 55 delete i->second;
58 } 56 }
59 } 57 }
60 58
61 void 59 int
62 ImageLayer::setModel(ImageModel *model) 60 ImageLayer::getCompletion(LayerGeometryProvider *) const
63 { 61 {
64 if (m_model == model) return; 62 auto model = ModelById::get(m_model);
65 m_model = model; 63 if (model) return model->getCompletion();
64 else return 0;
65 }
66
67 void
68 ImageLayer::setModel(ModelId modelId)
69 {
70 if (m_model == modelId) return;
71
72 auto model = ModelById::getAs<ImageModel>(modelId);
73 if (!model) throw std::logic_error("Not an ImageModel");
74
75 m_model = modelId;
66 76
67 connectSignals(m_model); 77 connectSignals(m_model);
68 78
69 emit modelReplaced(); 79 emit modelReplaced();
70 } 80 }
120 } 130 }
121 131
122 EventVector 132 EventVector
123 ImageLayer::getLocalPoints(LayerGeometryProvider *v, int x, int ) const 133 ImageLayer::getLocalPoints(LayerGeometryProvider *v, int x, int ) const
124 { 134 {
125 if (!m_model) return {}; 135 auto model = ModelById::getAs<ImageModel>(m_model);
136 if (!model) return {};
126 137
127 // SVDEBUG << "ImageLayer::getLocalPoints(" << x << "," << y << "):"; 138 // SVDEBUG << "ImageLayer::getLocalPoints(" << x << "," << y << "):";
128 EventVector points(m_model->getAllEvents()); 139 EventVector points(model->getAllEvents());
129 140
130 EventVector rv; 141 EventVector rv;
131 142
132 for (EventVector::const_iterator i = points.begin(); i != points.end(); ) { 143 for (EventVector::const_iterator i = points.begin(); i != points.end(); ) {
133 144
167 QString 178 QString
168 ImageLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const 179 ImageLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const
169 { 180 {
170 int x = pos.x(); 181 int x = pos.x();
171 182
172 if (!m_model || !m_model->getSampleRate()) return ""; 183 auto model = ModelById::getAs<ImageModel>(m_model);
184 if (!model || !model->getSampleRate()) return "";
173 185
174 EventVector points = getLocalPoints(v, x, pos.y()); 186 EventVector points = getLocalPoints(v, x, pos.y());
175 187
176 if (points.empty()) { 188 if (points.empty()) {
177 if (!m_model->isReady()) { 189 if (!model->isReady()) {
178 return tr("In progress"); 190 return tr("In progress");
179 } else { 191 } else {
180 return ""; 192 return "";
181 } 193 }
182 } 194 }
183 195
184 // int useFrame = points.begin()->frame; 196 // int useFrame = points.begin()->frame;
185 197
186 // RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate()); 198 // RealTime rt = RealTime::frame2RealTime(useFrame, model->getSampleRate());
187 199
188 QString text; 200 QString text;
189 /* 201 /*
190 if (points.begin()->label == "") { 202 if (points.begin()->label == "") {
191 text = QString(tr("Time:\t%1\nHeight:\t%2\nLabel:\t%3")) 203 text = QString(tr("Time:\t%1\nHeight:\t%2\nLabel:\t%3"))
206 bool 218 bool
207 ImageLayer::snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame, 219 ImageLayer::snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame,
208 int &resolution, 220 int &resolution,
209 SnapType snap) const 221 SnapType snap) const
210 { 222 {
211 if (!m_model) { 223 auto model = ModelById::getAs<ImageModel>(m_model);
224 if (!model) {
212 return Layer::snapToFeatureFrame(v, frame, resolution, snap); 225 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
213 } 226 }
214 227
215 resolution = m_model->getResolution(); 228 resolution = model->getResolution();
216 229
217 if (snap == SnapNeighbouring) { 230 if (snap == SnapNeighbouring) {
218 EventVector points = getLocalPoints(v, v->getXForFrame(frame), -1); 231 EventVector points = getLocalPoints(v, v->getXForFrame(frame), -1);
219 if (points.empty()) return false; 232 if (points.empty()) return false;
220 frame = points.begin()->getFrame(); 233 frame = points.begin()->getFrame();
221 return true; 234 return true;
222 } 235 }
223 236
224 Event e; 237 Event e;
225 if (m_model->getNearestEventMatching 238 if (model->getNearestEventMatching
226 (frame, 239 (frame,
227 [](Event) { return true; }, 240 [](Event) { return true; },
228 snap == SnapLeft ? EventSeries::Backward : EventSeries::Forward, 241 snap == SnapLeft ? EventSeries::Backward : EventSeries::Forward,
229 e)) { 242 e)) {
230 frame = e.getFrame(); 243 frame = e.getFrame();
235 } 248 }
236 249
237 void 250 void
238 ImageLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const 251 ImageLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
239 { 252 {
240 if (!m_model || !m_model->isOK()) return; 253 auto model = ModelById::getAs<ImageModel>(m_model);
241 254 if (!model || !model->isOK()) return;
242 sv_samplerate_t sampleRate = m_model->getSampleRate(); 255
256 sv_samplerate_t sampleRate = model->getSampleRate();
243 if (!sampleRate) return; 257 if (!sampleRate) return;
244 258
245 // Profiler profiler("ImageLayer::paint", true); 259 // Profiler profiler("ImageLayer::paint", true);
246 260
247 // int x0 = rect.left(), x1 = rect.right(); 261 // int x0 = rect.left(), x1 = rect.right();
248 int x0 = 0, x1 = v->getPaintWidth(); 262 int x0 = 0, x1 = v->getPaintWidth();
249 263
250 sv_frame_t frame0 = v->getFrameForX(x0); 264 sv_frame_t frame0 = v->getFrameForX(x0);
251 sv_frame_t frame1 = v->getFrameForX(x1); 265 sv_frame_t frame1 = v->getFrameForX(x1);
252 266
253 EventVector points(m_model->getEventsWithin(frame0, frame1 - frame0, 2)); 267 EventVector points(model->getEventsWithin(frame0, frame1 - frame0, 2));
254 if (points.empty()) return; 268 if (points.empty()) return;
255 269
256 paint.save(); 270 paint.save();
257 paint.setClipRect(rect.x(), 0, rect.width(), v->getPaintHeight()); 271 paint.setClipRect(rect.x(), 0, rect.width(), v->getPaintHeight());
258 272
511 void 525 void
512 ImageLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e) 526 ImageLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e)
513 { 527 {
514 // SVDEBUG << "ImageLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl; 528 // SVDEBUG << "ImageLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl;
515 529
516 if (!m_model) { 530 auto model = ModelById::getAs<ImageModel>(m_model);
531 if (!model) {
517 SVDEBUG << "ImageLayer::drawStart: no model" << endl; 532 SVDEBUG << "ImageLayer::drawStart: no model" << endl;
518 return; 533 return;
519 } 534 }
520 535
521 sv_frame_t frame = v->getFrameForX(e->x()); 536 sv_frame_t frame = v->getFrameForX(e->x());
522 if (frame < 0) frame = 0; 537 if (frame < 0) frame = 0;
523 frame = frame / m_model->getResolution() * m_model->getResolution(); 538 frame = frame / model->getResolution() * model->getResolution();
524 539
525 m_editingPoint = Event(frame); 540 m_editingPoint = Event(frame);
526 m_originalPoint = m_editingPoint; 541 m_originalPoint = m_editingPoint;
527 542
528 if (m_editingCommand) finish(m_editingCommand); 543 if (m_editingCommand) finish(m_editingCommand);
529 m_editingCommand = new ChangeEventsCommand(m_model, "Add Image"); 544 m_editingCommand = new ChangeEventsCommand<Model>(m_model, "Add Image");
530 m_editingCommand->add(m_editingPoint); 545 m_editingCommand->add(m_editingPoint);
531 546
532 m_editing = true; 547 m_editing = true;
533 } 548 }
534 549
535 void 550 void
536 ImageLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e) 551 ImageLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e)
537 { 552 {
538 // SVDEBUG << "ImageLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl; 553 // SVDEBUG << "ImageLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl;
539 554
540 if (!m_model || !m_editing) return; 555 auto model = ModelById::getAs<ImageModel>(m_model);
556 if (!model || !m_editing) return;
541 557
542 sv_frame_t frame = v->getFrameForX(e->x()); 558 sv_frame_t frame = v->getFrameForX(e->x());
543 if (frame < 0) frame = 0; 559 if (frame < 0) frame = 0;
544 frame = frame / m_model->getResolution() * m_model->getResolution(); 560 frame = frame / model->getResolution() * model->getResolution();
545 561
546 m_editingCommand->remove(m_editingPoint); 562 m_editingCommand->remove(m_editingPoint);
547 m_editingPoint = m_editingPoint 563 m_editingPoint = m_editingPoint
548 .withFrame(frame); 564 .withFrame(frame);
549 m_editingCommand->add(m_editingPoint); 565 m_editingCommand->add(m_editingPoint);
551 567
552 void 568 void
553 ImageLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *) 569 ImageLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *)
554 { 570 {
555 // SVDEBUG << "ImageLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl; 571 // SVDEBUG << "ImageLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl;
556 if (!m_model || !m_editing) return; 572 auto model = ModelById::getAs<ImageModel>(m_model);
573 if (!model || !m_editing) return;
557 574
558 ImageDialog dialog(tr("Select image"), "", ""); 575 ImageDialog dialog(tr("Select image"), "", "");
559 576
560 m_editingCommand->remove(m_editingPoint); 577 m_editingCommand->remove(m_editingPoint);
561 578
584 m_fileSources.erase(url); 601 m_fileSources.erase(url);
585 return false; 602 return false;
586 } 603 }
587 604
588 Event point = Event(frame).withURI(url); 605 Event point = Event(frame).withURI(url);
589 ChangeEventsCommand *command = 606 auto command =
590 new ChangeEventsCommand(m_model, "Add Image"); 607 new ChangeEventsCommand<Model>(m_model, "Add Image");
591 command->add(point); 608 command->add(point);
592 finish(command); 609 finish(command);
593 return true; 610 return true;
594 } 611 }
595 612
596 void 613 void
597 ImageLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e) 614 ImageLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e)
598 { 615 {
599 // SVDEBUG << "ImageLayer::editStart(" << e->x() << "," << e->y() << ")" << endl; 616 // SVDEBUG << "ImageLayer::editStart(" << e->x() << "," << e->y() << ")" << endl;
600 617
601 if (!m_model) return; 618 auto model = ModelById::getAs<ImageModel>(m_model);
619 if (!model) return;
602 620
603 EventVector points = getLocalPoints(v, e->x(), e->y()); 621 EventVector points = getLocalPoints(v, e->x(), e->y());
604 if (points.empty()) return; 622 if (points.empty()) return;
605 623
606 m_editOrigin = e->pos(); 624 m_editOrigin = e->pos();
616 } 634 }
617 635
618 void 636 void
619 ImageLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e) 637 ImageLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e)
620 { 638 {
621 if (!m_model || !m_editing) return; 639 auto model = ModelById::getAs<ImageModel>(m_model);
640 if (!model || !m_editing) return;
622 641
623 sv_frame_t frameDiff = v->getFrameForX(e->x()) - v->getFrameForX(m_editOrigin.x()); 642 sv_frame_t frameDiff = v->getFrameForX(e->x()) - v->getFrameForX(m_editOrigin.x());
624 sv_frame_t frame = m_originalPoint.getFrame() + frameDiff; 643 sv_frame_t frame = m_originalPoint.getFrame() + frameDiff;
625 644
626 if (frame < 0) frame = 0; 645 if (frame < 0) frame = 0;
627 frame = (frame / m_model->getResolution()) * m_model->getResolution(); 646 frame = (frame / model->getResolution()) * model->getResolution();
628 647
629 if (!m_editingCommand) { 648 if (!m_editingCommand) {
630 m_editingCommand = new ChangeEventsCommand(m_model, tr("Move Image")); 649 m_editingCommand = new ChangeEventsCommand<Model>(m_model, tr("Move Image"));
631 } 650 }
632 651
633 m_editingCommand->remove(m_editingPoint); 652 m_editingCommand->remove(m_editingPoint);
634 m_editingPoint = m_editingPoint 653 m_editingPoint = m_editingPoint
635 .withFrame(frame); 654 .withFrame(frame);
638 657
639 void 658 void
640 ImageLayer::editEnd(LayerGeometryProvider *, QMouseEvent *) 659 ImageLayer::editEnd(LayerGeometryProvider *, QMouseEvent *)
641 { 660 {
642 // SVDEBUG << "ImageLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl; 661 // SVDEBUG << "ImageLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl;
643 if (!m_model || !m_editing) return; 662 auto model = ModelById::getAs<ImageModel>(m_model);
663 if (!model || !m_editing) return;
644 664
645 if (m_editingCommand) { 665 if (m_editingCommand) {
646 finish(m_editingCommand); 666 finish(m_editingCommand);
647 } 667 }
648 668
651 } 671 }
652 672
653 bool 673 bool
654 ImageLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e) 674 ImageLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e)
655 { 675 {
656 if (!m_model) return false; 676 auto model = ModelById::getAs<ImageModel>(m_model);
677 if (!model) return false;
657 678
658 EventVector points = getLocalPoints(v, e->x(), e->y()); 679 EventVector points = getLocalPoints(v, e->x(), e->y());
659 if (points.empty()) return false; 680 if (points.empty()) return false;
660 681
661 QString image = points.begin()->getURI(); 682 QString image = points.begin()->getURI();
667 688
668 if (dialog.exec() == QDialog::Accepted) { 689 if (dialog.exec() == QDialog::Accepted) {
669 690
670 checkAddSource(dialog.getImage()); 691 checkAddSource(dialog.getImage());
671 692
672 ChangeEventsCommand *command = 693 auto command =
673 new ChangeEventsCommand(m_model, tr("Edit Image")); 694 new ChangeEventsCommand<Model>(m_model, tr("Edit Image"));
674 command->remove(*points.begin()); 695 command->remove(*points.begin());
675 command->add(points.begin()-> 696 command->add(points.begin()->
676 withURI(dialog.getImage()).withLabel(dialog.getLabel())); 697 withURI(dialog.getImage()).withLabel(dialog.getLabel()));
677 finish(command); 698 finish(command);
678 } 699 }
681 } 702 }
682 703
683 void 704 void
684 ImageLayer::moveSelection(Selection s, sv_frame_t newStartFrame) 705 ImageLayer::moveSelection(Selection s, sv_frame_t newStartFrame)
685 { 706 {
686 if (!m_model) return; 707 auto model = ModelById::getAs<ImageModel>(m_model);
687 708 if (!model) return;
688 ChangeEventsCommand *command = 709
689 new ChangeEventsCommand(m_model, tr("Drag Selection")); 710 auto command =
711 new ChangeEventsCommand<Model>(m_model, tr("Drag Selection"));
690 712
691 EventVector points = 713 EventVector points =
692 m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); 714 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
693 715
694 for (Event p: points) { 716 for (Event p: points) {
695 command->remove(p); 717 command->remove(p);
696 Event moved = p.withFrame(p.getFrame() + 718 Event moved = p.withFrame(p.getFrame() +
697 newStartFrame - s.getStartFrame()); 719 newStartFrame - s.getStartFrame());
702 } 724 }
703 725
704 void 726 void
705 ImageLayer::resizeSelection(Selection s, Selection newSize) 727 ImageLayer::resizeSelection(Selection s, Selection newSize)
706 { 728 {
707 if (!m_model) return; 729 auto model = ModelById::getAs<ImageModel>(m_model);
708 730 if (!model) return;
709 ChangeEventsCommand *command = 731
710 new ChangeEventsCommand(m_model, tr("Resize Selection")); 732 auto command =
733 new ChangeEventsCommand<Model>(m_model, tr("Resize Selection"));
711 734
712 EventVector points = 735 EventVector points =
713 m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); 736 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
714 737
715 double ratio = double(newSize.getDuration()) / double(s.getDuration()); 738 double ratio = double(newSize.getDuration()) / double(s.getDuration());
716 double oldStart = double(s.getStartFrame()); 739 double oldStart = double(s.getStartFrame());
717 double newStart = double(newSize.getStartFrame()); 740 double newStart = double(newSize.getStartFrame());
718 741
730 } 753 }
731 754
732 void 755 void
733 ImageLayer::deleteSelection(Selection s) 756 ImageLayer::deleteSelection(Selection s)
734 { 757 {
735 if (!m_model) return; 758 auto model = ModelById::getAs<ImageModel>(m_model);
736 759 if (!model) return;
737 ChangeEventsCommand *command = 760
738 new ChangeEventsCommand(m_model, tr("Delete Selection")); 761 auto command =
762 new ChangeEventsCommand<Model>(m_model, tr("Delete Selection"));
739 763
740 EventVector points = 764 EventVector points =
741 m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); 765 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
742 766
743 for (Event p: points) { 767 for (Event p: points) {
744 command->remove(p); 768 command->remove(p);
745 } 769 }
746 770
748 } 772 }
749 773
750 void 774 void
751 ImageLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to) 775 ImageLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to)
752 { 776 {
753 if (!m_model) return; 777 auto model = ModelById::getAs<ImageModel>(m_model);
778 if (!model) return;
754 779
755 EventVector points = 780 EventVector points =
756 m_model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); 781 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
757 782
758 for (Event p: points) { 783 for (Event p: points) {
759 to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame()))); 784 to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame())));
760 } 785 }
761 } 786 }
762 787
763 bool 788 bool
764 ImageLayer::paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t /* frameOffset */, bool /* interactive */) 789 ImageLayer::paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t /* frameOffset */, bool /* interactive */)
765 { 790 {
766 if (!m_model) return false; 791 auto model = ModelById::getAs<ImageModel>(m_model);
792 if (!model) return false;
767 793
768 const EventVector &points = from.getPoints(); 794 const EventVector &points = from.getPoints();
769 795
770 bool realign = false; 796 bool realign = false;
771 797
784 if (button == QMessageBox::Yes) { 810 if (button == QMessageBox::Yes) {
785 realign = true; 811 realign = true;
786 } 812 }
787 } 813 }
788 814
789 ChangeEventsCommand *command = 815 auto command = new ChangeEventsCommand<Model>(m_model, tr("Paste"));
790 new ChangeEventsCommand(m_model, tr("Paste"));
791 816
792 for (EventVector::const_iterator i = points.begin(); 817 for (EventVector::const_iterator i = points.begin();
793 i != points.end(); ++i) { 818 i != points.end(); ++i) {
794 819
795 sv_frame_t frame = 0; 820 sv_frame_t frame = 0;
861 } 886 }
862 887
863 void 888 void
864 ImageLayer::checkAddSources() 889 ImageLayer::checkAddSources()
865 { 890 {
866 const EventVector &points(m_model->getAllEvents()); 891 auto model = ModelById::getAs<ImageModel>(m_model);
892 const EventVector &points(model->getAllEvents());
867 893
868 for (EventVector::const_iterator i = points.begin(); 894 for (EventVector::const_iterator i = points.begin();
869 i != points.end(); ++i) { 895 i != points.end(); ++i) {
870 896
871 checkAddSource((*i).getURI()); 897 checkAddSource((*i).getURI());