comparison layer/TimeValueLayer.cpp @ 526:e60e6fccfe4e

* fix incorrect default scale extents when loading layer from a file in which both min and max have been saved as 0
author Chris Cannam
date Thu, 19 Mar 2009 15:48:19 +0000
parents 1c6439ef99d6
children 2e8194a30f40
comparison
equal deleted inserted replaced
525:e6e38632e8ea 526:e60e6fccfe4e
39 #include <QMessageBox> 39 #include <QMessageBox>
40 #include <QInputDialog> 40 #include <QInputDialog>
41 41
42 #include <iostream> 42 #include <iostream>
43 #include <cmath> 43 #include <cmath>
44
45 //#define DEBUG_TIME_VALUE_LAYER 1
44 46
45 TimeValueLayer::TimeValueLayer() : 47 TimeValueLayer::TimeValueLayer() :
46 SingleColourLayer(), 48 SingleColourLayer(),
47 m_model(0), 49 m_model(0),
48 m_editing(false), 50 m_editing(false),
75 } 77 }
76 if (m_model && m_model->getRDFTypeURI().endsWith("Change")) { 78 if (m_model && m_model->getRDFTypeURI().endsWith("Change")) {
77 setPlotStyle(PlotSegmentation); 79 setPlotStyle(PlotSegmentation);
78 } 80 }
79 81
80 // std::cerr << "TimeValueLayer::setModel(" << model << ")" << std::endl; 82 #ifdef DEBUG_TIME_VALUE_LAYER
83 std::cerr << "TimeValueLayer::setModel(" << model << ")" << std::endl;
84 #endif
81 85
82 emit modelReplaced(); 86 emit modelReplaced();
83 } 87 }
84 88
85 Layer::PropertyList 89 Layer::PropertyList
310 } 314 }
311 315
312 min = m_scaleMinimum; 316 min = m_scaleMinimum;
313 max = m_scaleMaximum; 317 max = m_scaleMaximum;
314 318
315 // std::cerr << "TimeValueLayer::getDisplayExtents: min = " << min << ", max = " << max << std::endl; 319 #ifdef DEBUG_TIME_VALUE_LAYER
320 std::cerr << "TimeValueLayer::getDisplayExtents: min = " << min << ", max = " << max << std::endl;
321 #endif
316 322
317 return true; 323 return true;
318 } 324 }
319 325
320 bool 326 bool
331 } 337 }
332 338
333 m_scaleMinimum = min; 339 m_scaleMinimum = min;
334 m_scaleMaximum = max; 340 m_scaleMaximum = max;
335 341
336 // std::cerr << "TimeValueLayer::setDisplayExtents: min = " << min << ", max = " << max << std::endl; 342 #ifdef DEBUG_TIME_VALUE_LAYER
343 std::cerr << "TimeValueLayer::setDisplayExtents: min = " << min << ", max = " << max << std::endl;
344 #endif
337 345
338 emit layerParametersChanged(); 346 emit layerParametersChanged();
339 return true; 347 return true;
340 } 348 }
341 349
360 368
361 float dmin, dmax; 369 float dmin, dmax;
362 getDisplayExtents(dmin, dmax); 370 getDisplayExtents(dmin, dmax);
363 371
364 int nr = mapper->getPositionForValue(dmax - dmin); 372 int nr = mapper->getPositionForValue(dmax - dmin);
365 /* 373
374 #ifdef DEBUG_TIME_VALUE_LAYER
366 int n0 = mapper->getPositionForValue(dmax); 375 int n0 = mapper->getPositionForValue(dmax);
367 int n1 = mapper->getPositionForValue(dmin); 376 int n1 = mapper->getPositionForValue(dmin);
368 int nr = n1 - n0; 377 int nr = n1 - n0;
369 if (nr < 0) nr = -nr; 378 if (nr < 0) nr = -nr;
370 379
371 std::cerr << "TimeValueLayer::getCurrentVerticalZoomStep: dmin = " << dmin << ", dmax = " << dmax << ", n0 = " << n0 << ", n1 = " << n1 << ", nr = " << nr << std::endl; 380 std::cerr << "TimeValueLayer::getCurrentVerticalZoomStep: dmin = " << dmin << ", dmax = " << dmax << ", n0 = " << n0 << ", n1 = " << n1 << ", nr = " << nr << std::endl;
372 */ 381 #endif
382
373 delete mapper; 383 delete mapper;
374 384
375 return 100 - nr; 385 return 100 - nr;
376 } 386 }
377 387
401 // see SpectrogramLayer::setVerticalZoomStep 411 // see SpectrogramLayer::setVerticalZoomStep
402 412
403 newmax = (newdist + sqrtf(newdist*newdist + 4*dmin*dmax)) / 2; 413 newmax = (newdist + sqrtf(newdist*newdist + 4*dmin*dmax)) / 2;
404 newmin = newmax - newdist; 414 newmin = newmax - newdist;
405 415
406 // std::cerr << "newmin = " << newmin << ", newmax = " << newmax << std::endl; 416 #ifdef DEBUG_TIME_VALUE_LAYER
417 std::cerr << "newmin = " << newmin << ", newmax = " << newmax << std::endl;
418 #endif
407 419
408 } else { 420 } else {
409 float dmid = (dmax + dmin) / 2; 421 float dmid = (dmax + dmin) / 2;
410 newmin = dmid - newdist / 2; 422 newmin = dmid - newdist / 2;
411 newmax = dmid + newdist / 2; 423 newmax = dmid + newdist / 2;
417 } 429 }
418 if (newmax > max) { 430 if (newmax > max) {
419 newmax = max; 431 newmax = max;
420 } 432 }
421 433
434 #ifdef DEBUG_TIME_VALUE_LAYER
422 std::cerr << "TimeValueLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << std::endl; 435 std::cerr << "TimeValueLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << std::endl;
436 #endif
423 437
424 setDisplayExtents(newmin, newmax); 438 setDisplayExtents(newmin, newmax);
425 } 439 }
426 440
427 RangeMapper * 441 RangeMapper *
719 bool logarithmic = false; 733 bool logarithmic = false;
720 int h = v->height(); 734 int h = v->height();
721 735
722 getScaleExtents(v, min, max, logarithmic); 736 getScaleExtents(v, min, max, logarithmic);
723 737
724 // std::cerr << "getYForValue(" << val << "): min " << min << ", max " 738 #ifdef DEBUG_TIME_VALUE_LAYER
725 // << max << ", log " << logarithmic << std::endl; 739 std::cerr << "getYForValue(" << val << "): min " << min << ", max "
740 << max << ", log " << logarithmic << std::endl;
741 #endif
726 742
727 if (logarithmic) { 743 if (logarithmic) {
728 val = LogRange::map(val); 744 val = LogRange::map(val);
729 } 745 }
730 746
769 785
770 if (log) { 786 if (log) {
771 val = LogRange::map(val); 787 val = LogRange::map(val);
772 } 788 }
773 789
774 // std::cerr << "TimeValueLayer::getColourForValue: min " << min << ", max " 790 #ifdef DEBUG_TIME_VALUE_LAYER
775 // << max << ", log " << log << ", value " << val << std::endl; 791 std::cerr << "TimeValueLayer::getColourForValue: min " << min << ", max "
792 << max << ", log " << log << ", value " << val << std::endl;
793 #endif
776 794
777 QColor solid = ColourMapper(m_colourMap, min, max).map(val); 795 QColor solid = ColourMapper(m_colourMap, min, max).map(val);
778 return QColor(solid.red(), solid.green(), solid.blue(), 120); 796 return QColor(solid.red(), solid.green(), solid.blue(), 120);
779 } 797 }
780 798
810 828
811 QColor brushColour(getBaseQColor()); 829 QColor brushColour(getBaseQColor());
812 brushColour.setAlpha(80); 830 brushColour.setAlpha(80);
813 paint.setBrush(brushColour); 831 paint.setBrush(brushColour);
814 832
815 // std::cerr << "TimeValueLayer::paint: resolution is " 833 #ifdef DEBUG_TIME_VALUE_LAYER
816 // << m_model->getResolution() << " frames" << std::endl; 834 std::cerr << "TimeValueLayer::paint: resolution is "
835 << m_model->getResolution() << " frames" << std::endl;
836 #endif
817 837
818 float min = m_model->getValueMinimum(); 838 float min = m_model->getValueMinimum();
819 float max = m_model->getValueMaximum(); 839 float max = m_model->getValueMaximum();
820 if (max == min) max = min + 1.0; 840 if (max == min) max = min + 1.0;
821 841
826 long illuminateFrame = -1; 846 long illuminateFrame = -1;
827 847
828 if (v->shouldIlluminateLocalFeatures(this, localPos)) { 848 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
829 SparseTimeValueModel::PointList localPoints = 849 SparseTimeValueModel::PointList localPoints =
830 getLocalPoints(v, localPos.x()); 850 getLocalPoints(v, localPos.x());
831 // std::cerr << "TimeValueLayer: " << localPoints.size() << " local points" << std::endl; 851 #ifdef DEBUG_TIME_VALUE_LAYER
852 std::cerr << "TimeValueLayer: " << localPoints.size() << " local points" << std::endl;
853 #endif
832 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame; 854 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
833 } 855 }
834 856
835 int w = 857 int w =
836 v->getXForFrame(frame0 + m_model->getResolution()) - 858 v->getXForFrame(frame0 + m_model->getResolution()) -
984 } 1006 }
985 } 1007 }
986 1008
987 if (m_plotStyle == PlotSegmentation) { 1009 if (m_plotStyle == PlotSegmentation) {
988 1010
989 // std::cerr << "drawing rect" << std::endl; 1011 #ifdef DEBUG_TIME_VALUE_LAYER
1012 std::cerr << "drawing rect" << std::endl;
1013 #endif
990 1014
991 if (nx <= x) continue; 1015 if (nx <= x) continue;
992 1016
993 if (illuminateFrame != p.frame) { 1017 if (illuminateFrame != p.frame) {
994 if (!m_drawSegmentDivisions || 1018 if (!m_drawSegmentDivisions ||
1088 if (inc > 0) { 1112 if (inc > 0) {
1089 int prec = trunc(log10f(inc)); 1113 int prec = trunc(log10f(inc));
1090 prec -= 1; 1114 prec -= 1;
1091 if (prec < 0) dp = -prec; 1115 if (prec < 0) dp = -prec;
1092 round = powf(10.f, prec); 1116 round = powf(10.f, prec);
1093 // std::cerr << "inc = " << inc << ", round = " << round << std::endl; 1117 #ifdef DEBUG_TIME_VALUE_LAYER
1118 std::cerr << "inc = " << inc << ", round = " << round << std::endl;
1119 #endif
1094 } 1120 }
1095 1121
1096 int prevy = -1; 1122 int prevy = -1;
1097 1123
1098 for (int i = 0; i < n; ++i) { 1124 for (int i = 0; i < n; ++i) {
1109 if (i == n-1 && 1135 if (i == n-1 &&
1110 v->height() < paint.fontMetrics().height() * (n*2)) { 1136 v->height() < paint.fontMetrics().height() * (n*2)) {
1111 if (m_model->getScaleUnits() != "") drawText = false; 1137 if (m_model->getScaleUnits() != "") drawText = false;
1112 } 1138 }
1113 dispval = lrintf(val / round) * round; 1139 dispval = lrintf(val / round) * round;
1114 // std::cerr << "val = " << val << ", dispval = " << dispval << std::endl; 1140 #ifdef DEBUG_TIME_VALUE_LAYER
1141 std::cerr << "val = " << val << ", dispval = " << dispval << std::endl;
1142 #endif
1115 if (logarithmic) { 1143 if (logarithmic) {
1116 y = getYForValue(v, LogRange::unmap(dispval)); 1144 y = getYForValue(v, LogRange::unmap(dispval));
1117 } else { 1145 } else {
1118 y = getYForValue(v, dispval); 1146 y = getYForValue(v, dispval);
1119 } 1147 }
1159 } 1187 }
1160 1188
1161 void 1189 void
1162 TimeValueLayer::drawStart(View *v, QMouseEvent *e) 1190 TimeValueLayer::drawStart(View *v, QMouseEvent *e)
1163 { 1191 {
1164 // std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl; 1192 #ifdef DEBUG_TIME_VALUE_LAYER
1193 std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
1194 #endif
1165 1195
1166 if (!m_model) return; 1196 if (!m_model) return;
1167 1197
1168 long frame = v->getFrameForX(e->x()); 1198 long frame = v->getFrameForX(e->x());
1169 long resolution = m_model->getResolution(); 1199 long resolution = m_model->getResolution();
1177 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x()); 1207 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
1178 if (!points.empty()) { 1208 if (!points.empty()) {
1179 for (SparseTimeValueModel::PointList::iterator i = points.begin(); 1209 for (SparseTimeValueModel::PointList::iterator i = points.begin();
1180 i != points.end(); ++i) { 1210 i != points.end(); ++i) {
1181 if (((i->frame / resolution) * resolution) != frame) { 1211 if (((i->frame / resolution) * resolution) != frame) {
1182 // std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl; 1212 #ifdef DEBUG_TIME_VALUE_LAYER
1213 std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
1214 #endif
1183 continue; 1215 continue;
1184 } 1216 }
1185 m_editingPoint = *i; 1217 m_editingPoint = *i;
1186 havePoint = true; 1218 havePoint = true;
1187 } 1219 }
1205 } 1237 }
1206 1238
1207 void 1239 void
1208 TimeValueLayer::drawDrag(View *v, QMouseEvent *e) 1240 TimeValueLayer::drawDrag(View *v, QMouseEvent *e)
1209 { 1241 {
1210 // std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl; 1242 #ifdef DEBUG_TIME_VALUE_LAYER
1243 std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
1244 #endif
1211 1245
1212 if (!m_model || !m_editing) return; 1246 if (!m_model || !m_editing) return;
1213 1247
1214 long frame = v->getFrameForX(e->x()); 1248 long frame = v->getFrameForX(e->x());
1215 long resolution = m_model->getResolution(); 1249 long resolution = m_model->getResolution();
1218 1252
1219 float value = getValueForY(v, e->y()); 1253 float value = getValueForY(v, e->y());
1220 1254
1221 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x()); 1255 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
1222 1256
1223 // std::cerr << points.size() << " points" << std::endl; 1257 #ifdef DEBUG_TIME_VALUE_LAYER
1258 std::cerr << points.size() << " points" << std::endl;
1259 #endif
1224 1260
1225 bool havePoint = false; 1261 bool havePoint = false;
1226 1262
1227 if (!points.empty()) { 1263 if (!points.empty()) {
1228 for (SparseTimeValueModel::PointList::iterator i = points.begin(); 1264 for (SparseTimeValueModel::PointList::iterator i = points.begin();
1229 i != points.end(); ++i) { 1265 i != points.end(); ++i) {
1230 if (i->frame == m_editingPoint.frame && 1266 if (i->frame == m_editingPoint.frame &&
1231 i->value == m_editingPoint.value) { 1267 i->value == m_editingPoint.value) {
1232 // std::cerr << "ignoring current editing point at " << i->frame << ", " << i->value << std::endl; 1268 #ifdef DEBUG_TIME_VALUE_LAYER
1269 std::cerr << "ignoring current editing point at " << i->frame << ", " << i->value << std::endl;
1270 #endif
1233 continue; 1271 continue;
1234 } 1272 }
1235 if (((i->frame / resolution) * resolution) != frame) { 1273 if (((i->frame / resolution) * resolution) != frame) {
1236 // std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl; 1274 #ifdef DEBUG_TIME_VALUE_LAYER
1275 std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
1276 #endif
1237 continue; 1277 continue;
1238 } 1278 }
1239 // std::cerr << "adjusting to new point at " << i->frame << ", " << i->value << std::endl; 1279 #ifdef DEBUG_TIME_VALUE_LAYER
1280 std::cerr << "adjusting to new point at " << i->frame << ", " << i->value << std::endl;
1281 #endif
1240 m_editingPoint = *i; 1282 m_editingPoint = *i;
1241 m_originalPoint = m_editingPoint; 1283 m_originalPoint = m_editingPoint;
1242 m_editingCommand->deletePoint(m_editingPoint); 1284 m_editingCommand->deletePoint(m_editingPoint);
1243 havePoint = true; 1285 havePoint = true;
1244 } 1286 }
1257 } 1299 }
1258 1300
1259 void 1301 void
1260 TimeValueLayer::drawEnd(View *, QMouseEvent *) 1302 TimeValueLayer::drawEnd(View *, QMouseEvent *)
1261 { 1303 {
1262 // std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl; 1304 #ifdef DEBUG_TIME_VALUE_LAYER
1305 std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
1306 #endif
1263 if (!m_model || !m_editing) return; 1307 if (!m_model || !m_editing) return;
1264 finish(m_editingCommand); 1308 finish(m_editingCommand);
1265 m_editingCommand = 0; 1309 m_editingCommand = 0;
1266 m_editing = false; 1310 m_editing = false;
1267 } 1311 }
1312 } 1356 }
1313 1357
1314 void 1358 void
1315 TimeValueLayer::editStart(View *v, QMouseEvent *e) 1359 TimeValueLayer::editStart(View *v, QMouseEvent *e)
1316 { 1360 {
1317 // std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl; 1361 #ifdef DEBUG_TIME_VALUE_LAYER
1362 std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
1363 #endif
1318 1364
1319 if (!m_model) return; 1365 if (!m_model) return;
1320 1366
1321 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x()); 1367 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
1322 if (points.empty()) return; 1368 if (points.empty()) return;
1333 } 1379 }
1334 1380
1335 void 1381 void
1336 TimeValueLayer::editDrag(View *v, QMouseEvent *e) 1382 TimeValueLayer::editDrag(View *v, QMouseEvent *e)
1337 { 1383 {
1338 // std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl; 1384 #ifdef DEBUG_TIME_VALUE_LAYER
1385 std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
1386 #endif
1339 1387
1340 if (!m_model || !m_editing) return; 1388 if (!m_model || !m_editing) return;
1341 1389
1342 long frame = v->getFrameForX(e->x()); 1390 long frame = v->getFrameForX(e->x());
1343 if (frame < 0) frame = 0; 1391 if (frame < 0) frame = 0;
1357 } 1405 }
1358 1406
1359 void 1407 void
1360 TimeValueLayer::editEnd(View *, QMouseEvent *) 1408 TimeValueLayer::editEnd(View *, QMouseEvent *)
1361 { 1409 {
1362 // std::cerr << "TimeValueLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl; 1410 #ifdef DEBUG_TIME_VALUE_LAYER
1411 std::cerr << "TimeValueLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
1412 #endif
1363 if (!m_model || !m_editing) return; 1413 if (!m_model || !m_editing) return;
1364 1414
1365 if (m_editingCommand) { 1415 if (m_editingCommand) {
1366 1416
1367 QString newName = m_editingCommand->getName(); 1417 QString newName = m_editingCommand->getName();
1695 SparseTimeValueModel::Point formerPrevPoint = prevPoint; 1745 SparseTimeValueModel::Point formerPrevPoint = prevPoint;
1696 1746
1697 if (i->haveValue()) { 1747 if (i->haveValue()) {
1698 newPoint.value = i->getValue(); 1748 newPoint.value = i->getValue();
1699 } else { 1749 } else {
1700 // std::cerr << "Setting value on point at " << newPoint.frame << " from labeller"; 1750 #ifdef DEBUG_TIME_VALUE_LAYER
1701 // if (i == points.begin()) { 1751 std::cerr << "Setting value on point at " << newPoint.frame << " from labeller";
1702 // std::cerr << ", no prev point" << std::endl; 1752 if (i == points.begin()) {
1703 // } else { 1753 std::cerr << ", no prev point" << std::endl;
1704 // std::cerr << ", prev point is at " << prevPoint.frame << std::endl; 1754 } else {
1705 // } 1755 std::cerr << ", prev point is at " << prevPoint.frame << std::endl;
1756 }
1757 #endif
1706 labeller.setValue<SparseTimeValueModel::Point> 1758 labeller.setValue<SparseTimeValueModel::Point>
1707 (newPoint, (i == points.begin()) ? 0 : &prevPoint); 1759 (newPoint, (i == points.begin()) ? 0 : &prevPoint);
1708 // std::cerr << "New point value = " << newPoint.value << std::endl; 1760 #ifdef DEBUG_TIME_VALUE_LAYER
1761 std::cerr << "New point value = " << newPoint.value << std::endl;
1762 #endif
1709 if (labeller.actingOnPrevPoint() && i != points.begin()) { 1763 if (labeller.actingOnPrevPoint() && i != points.begin()) {
1710 usePrev = true; 1764 usePrev = true;
1711 } 1765 }
1712 } 1766 }
1713 1767
1760 bool draw = (attributes.value("drawDivisions").trimmed() == "true"); 1814 bool draw = (attributes.value("drawDivisions").trimmed() == "true");
1761 setDrawSegmentDivisions(draw); 1815 setDrawSegmentDivisions(draw);
1762 1816
1763 float min = attributes.value("scaleMinimum").toFloat(&ok); 1817 float min = attributes.value("scaleMinimum").toFloat(&ok);
1764 float max = attributes.value("scaleMaximum").toFloat(&alsoOk); 1818 float max = attributes.value("scaleMaximum").toFloat(&alsoOk);
1765 if (ok && alsoOk) setDisplayExtents(min, max); 1819 #ifdef DEBUG_TIME_VALUE_LAYER
1766 } 1820 std::cerr << "from properties: min = " << min << ", max = " << max << std::endl;
1767 1821 #endif
1822 if (ok && alsoOk && min != max) setDisplayExtents(min, max);
1823 }
1824