comparison layer/TimeValueLayer.cpp @ 76:45ba0b381c5d

* Fix long-standing off-by-1 bug in WaveFileModel that was getting us the wrong values for almost all audio data when merging channels (channel == -1) * Implement cut, copy and paste * Make draw mode work properly in time value layer * Minor fixes to CSV import
author Chris Cannam
date Fri, 07 Apr 2006 17:50:33 +0000
parents ad1fe715b480
children 19bf27e4fb29
comparison
equal deleted inserted replaced
75:dfdbf336bb37 76:45ba0b381c5d
500 500
501 int x = v->getXForFrame(p.frame); 501 int x = v->getXForFrame(p.frame);
502 int y = getYForValue(v, p.value); 502 int y = getYForValue(v, p.value);
503 503
504 bool haveNext = false; 504 bool haveNext = false;
505 int nx = v->getXForFrame(m_model->getEndFrame()); 505 int nx = v->getXForFrame(v->getModelsEndFrame());
506 // m_model->getEndFrame());
506 int ny = y; 507 int ny = y;
507 508
508 SparseTimeValueModel::PointList::const_iterator j = i; 509 SparseTimeValueModel::PointList::const_iterator j = i;
509 ++j; 510 ++j;
510 511
511 if (j != points.end()) { 512 if (j != points.end()) {
512 const SparseTimeValueModel::Point &q(*j); 513 const SparseTimeValueModel::Point &q(*j);
513 nx = v->getXForFrame(q.frame); 514 nx = v->getXForFrame(q.frame);
514 ny = getYForValue(v, q.value); 515 ny = getYForValue(v, q.value);
515 haveNext = true; 516 haveNext = true;
516 } 517 }
518
519 // std::cout << "frame = " << p.frame << ", x = " << x << ", haveNext = " << haveNext
520 // << ", nx = " << nx << std::endl;
517 521
518 int labelY = y; 522 int labelY = y;
519 523
520 if (w < 1) w = 1; 524 if (w < 1) w = 1;
521 paint.setPen(m_colour); 525 paint.setPen(m_colour);
606 } 610 }
607 } 611 }
608 } 612 }
609 613
610 if (m_plotStyle == PlotSegmentation) { 614 if (m_plotStyle == PlotSegmentation) {
615
616 // std::cerr << "drawing rect" << std::endl;
611 617
612 if (nx <= x) continue; 618 if (nx <= x) continue;
613 619
614 if (illuminateFrame != p.frame && 620 if (illuminateFrame != p.frame &&
615 (nx < x + 5 || x >= v->width() - 1)) { 621 (nx < x + 5 || x >= v->width() - 1)) {
726 std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl; 732 std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
727 733
728 if (!m_model) return; 734 if (!m_model) return;
729 735
730 long frame = v->getFrameForX(e->x()); 736 long frame = v->getFrameForX(e->x());
737 long resolution = m_model->getResolution();
731 if (frame < 0) frame = 0; 738 if (frame < 0) frame = 0;
732 frame = frame / m_model->getResolution() * m_model->getResolution(); 739 frame = (frame / resolution) * resolution;
733 740
734 float value = getValueForY(v, e->y()); 741 float value = getValueForY(v, e->y());
735 742
736 m_editingPoint = SparseTimeValueModel::Point(frame, value, tr("New Point")); 743 bool havePoint = false;
744
745 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
746 if (!points.empty()) {
747 for (SparseTimeValueModel::PointList::iterator i = points.begin();
748 i != points.end(); ++i) {
749 if (((i->frame / resolution) * resolution) != frame) {
750 std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
751 continue;
752 }
753 m_editingPoint = *i;
754 havePoint = true;
755 }
756 }
757
758 if (!havePoint) {
759 m_editingPoint = SparseTimeValueModel::Point
760 (frame, value, tr("New Point"));
761 }
762
737 m_originalPoint = m_editingPoint; 763 m_originalPoint = m_editingPoint;
738 764
739 if (m_editingCommand) m_editingCommand->finish(); 765 if (m_editingCommand) m_editingCommand->finish();
740 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model, 766 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
741 tr("Draw Point")); 767 tr("Draw Point"));
742 m_editingCommand->addPoint(m_editingPoint); 768 if (!havePoint) {
769 m_editingCommand->addPoint(m_editingPoint);
770 }
743 771
744 m_editing = true; 772 m_editing = true;
745 } 773 }
746 774
747 void 775 void
750 std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl; 778 std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
751 779
752 if (!m_model || !m_editing) return; 780 if (!m_model || !m_editing) return;
753 781
754 long frame = v->getFrameForX(e->x()); 782 long frame = v->getFrameForX(e->x());
783 long resolution = m_model->getResolution();
755 if (frame < 0) frame = 0; 784 if (frame < 0) frame = 0;
756 frame = frame / m_model->getResolution() * m_model->getResolution(); 785 frame = (frame / resolution) * resolution;
757 786
758 float value = getValueForY(v, e->y()); 787 float value = getValueForY(v, e->y());
759 788
760 m_editingCommand->deletePoint(m_editingPoint); 789 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
790
791 std::cerr << points.size() << " points" << std::endl;
792
793 bool havePoint = false;
794
795 if (!points.empty()) {
796 for (SparseTimeValueModel::PointList::iterator i = points.begin();
797 i != points.end(); ++i) {
798 if (i->frame == m_editingPoint.frame &&
799 i->value == m_editingPoint.value) {
800 std::cerr << "ignoring current editing point at " << i->frame << ", " << i->value << std::endl;
801 continue;
802 }
803 if (((i->frame / resolution) * resolution) != frame) {
804 std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
805 continue;
806 }
807 std::cerr << "adjusting to new point at " << i->frame << ", " << i->value << std::endl;
808 m_editingPoint = *i;
809 m_originalPoint = m_editingPoint;
810 m_editingCommand->deletePoint(m_editingPoint);
811 havePoint = true;
812 }
813 }
814
815 if (!havePoint) {
816 if (frame == m_editingPoint.frame) {
817 m_editingCommand->deletePoint(m_editingPoint);
818 }
819 }
820
821 // m_editingCommand->deletePoint(m_editingPoint);
761 m_editingPoint.frame = frame; 822 m_editingPoint.frame = frame;
762 m_editingPoint.value = value; 823 m_editingPoint.value = value;
763 m_editingCommand->addPoint(m_editingPoint); 824 m_editingCommand->addPoint(m_editingPoint);
764 } 825 }
765 826
939 } 1000 }
940 1001
941 command->finish(); 1002 command->finish();
942 } 1003 }
943 1004
1005 void
1006 TimeValueLayer::deleteSelection(Selection s)
1007 {
1008 SparseTimeValueModel::EditCommand *command =
1009 new SparseTimeValueModel::EditCommand(m_model,
1010 tr("Delete Selected Points"));
1011
1012 SparseTimeValueModel::PointList points =
1013 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
1014
1015 for (SparseTimeValueModel::PointList::iterator i = points.begin();
1016 i != points.end(); ++i) {
1017
1018 if (s.contains(i->frame)) {
1019 command->deletePoint(*i);
1020 }
1021 }
1022
1023 command->finish();
1024 }
1025
1026 void
1027 TimeValueLayer::copy(Selection s, Clipboard &to)
1028 {
1029 SparseTimeValueModel::PointList points =
1030 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
1031
1032 for (SparseTimeValueModel::PointList::iterator i = points.begin();
1033 i != points.end(); ++i) {
1034 if (s.contains(i->frame)) {
1035 Clipboard::Point point(i->frame, i->label);
1036 to.addPoint(point);
1037 }
1038 }
1039 }
1040
1041 void
1042 TimeValueLayer::paste(const Clipboard &from, int frameOffset)
1043 {
1044 const Clipboard::PointList &points = from.getPoints();
1045
1046 SparseTimeValueModel::EditCommand *command =
1047 new SparseTimeValueModel::EditCommand(m_model, tr("Paste"));
1048
1049 for (Clipboard::PointList::const_iterator i = points.begin();
1050 i != points.end(); ++i) {
1051
1052 if (!i->haveFrame()) continue;
1053 size_t frame = 0;
1054 if (frameOffset > 0 || -frameOffset < i->getFrame()) {
1055 frame = i->getFrame() + frameOffset;
1056 }
1057 SparseTimeValueModel::Point newPoint(frame);
1058
1059 if (i->haveLabel()) newPoint.label = i->getLabel();
1060 if (i->haveValue()) newPoint.value = i->getValue();
1061 else newPoint.value = (m_model->getValueMinimum() +
1062 m_model->getValueMaximum()) / 2;
1063
1064 command->addPoint(newPoint);
1065 }
1066
1067 command->finish();
1068 }
1069
944 QString 1070 QString
945 TimeValueLayer::toXmlString(QString indent, QString extraAttributes) const 1071 TimeValueLayer::toXmlString(QString indent, QString extraAttributes) const
946 { 1072 {
947 return Layer::toXmlString(indent, extraAttributes + 1073 return Layer::toXmlString(indent, extraAttributes +
948 QString(" colour=\"%1\" plotStyle=\"%2\"") 1074 QString(" colour=\"%1\" plotStyle=\"%2\"")