comparison layer/RegionLayer.cpp @ 701:084f65094203

Extend numerical and colour scales (and piano where applicable) to note and region layers
author Chris Cannam
date Wed, 04 Dec 2013 13:39:41 +0000
parents 1a0dfcbffaf1
children 137d3ff48f73
comparison
equal deleted inserted replaced
700:7846175403f1 701:084f65094203
18 #include "data/model/Model.h" 18 #include "data/model/Model.h"
19 #include "base/RealTime.h" 19 #include "base/RealTime.h"
20 #include "base/Profiler.h" 20 #include "base/Profiler.h"
21 #include "base/LogRange.h" 21 #include "base/LogRange.h"
22 #include "ColourDatabase.h" 22 #include "ColourDatabase.h"
23
23 #include "ColourMapper.h" 24 #include "ColourMapper.h"
25 #include "LinearNumericalScale.h"
26 #include "LogNumericalScale.h"
27 #include "LinearColourScale.h"
28 #include "LogColourScale.h"
29
24 #include "view/View.h" 30 #include "view/View.h"
25 31
26 #include "data/model/RegionModel.h" 32 #include "data/model/RegionModel.h"
27 33
28 #include "widgets/ItemEditDialog.h" 34 #include "widgets/ItemEditDialog.h"
35 #include "widgets/TextAbbrev.h"
29 36
30 #include <QPainter> 37 #include <QPainter>
31 #include <QPainterPath> 38 #include <QPainterPath>
32 #include <QMouseEvent> 39 #include <QMouseEvent>
33 #include <QTextStream> 40 #include <QTextStream>
144 } else if (name == "Scale Units") { 151 } else if (name == "Scale Units") {
145 152
146 if (deflt) *deflt = 0; 153 if (deflt) *deflt = 0;
147 if (m_model) { 154 if (m_model) {
148 val = UnitDatabase::getInstance()->getUnitId 155 val = UnitDatabase::getInstance()->getUnitId
149 (m_model->getScaleUnits()); 156 (getScaleUnits());
150 } 157 }
151 158
152 } else { 159 } else {
153 160
154 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt); 161 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
268 bool &logarithmic, QString &unit) const 275 bool &logarithmic, QString &unit) const
269 { 276 {
270 if (!m_model) return false; 277 if (!m_model) return false;
271 min = m_model->getValueMinimum(); 278 min = m_model->getValueMinimum();
272 max = m_model->getValueMaximum(); 279 max = m_model->getValueMaximum();
273 unit = m_model->getScaleUnits(); 280 unit = getScaleUnits();
274 281
275 if (m_verticalScale == LogScale) logarithmic = true; 282 if (m_verticalScale == LogScale) logarithmic = true;
276 283
277 return true; 284 return true;
278 } 285 }
417 RealTime rd = RealTime::frame2RealTime(region.duration, 424 RealTime rd = RealTime::frame2RealTime(region.duration,
418 m_model->getSampleRate()); 425 m_model->getSampleRate());
419 426
420 QString valueText; 427 QString valueText;
421 428
422 valueText = tr("%1 %2").arg(region.value).arg(m_model->getScaleUnits()); 429 valueText = tr("%1 %2").arg(region.value).arg(getScaleUnits());
423 430
424 QString text; 431 QString text;
425 432
426 if (region.label == "") { 433 if (region.label == "") {
427 text = QString(tr("Time:\t%1\nValue:\t%2\nDuration:\t%3\nNo label")) 434 text = QString(tr("Time:\t%1\nValue:\t%2\nDuration:\t%3\nNo label"))
595 602
596 frame = snapped; 603 frame = snapped;
597 return found; 604 return found;
598 } 605 }
599 606
607 QString
608 RegionLayer::getScaleUnits() const
609 {
610 if (m_model) return m_model->getScaleUnits();
611 else return "";
612 }
613
600 void 614 void
601 RegionLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const 615 RegionLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const
602 { 616 {
603 min = 0.0; 617 min = 0.0;
604 max = 0.0; 618 max = 0.0;
605 log = false; 619 log = false;
606 620
607 QString queryUnits; 621 QString queryUnits;
608 queryUnits = m_model->getScaleUnits(); 622 queryUnits = getScaleUnits();
609 623
610 if (m_verticalScale == AutoAlignScale) { 624 if (m_verticalScale == AutoAlignScale) {
611 625
612 if (!v->getValueExtents(queryUnits, min, max, log)) { 626 if (!v->getValueExtents(queryUnits, min, max, log)) {
613 627
702 val = LogRange::map(val); 716 val = LogRange::map(val);
703 } 717 }
704 718
705 return int(h - ((val - min) * h) / (max - min)); 719 return int(h - ((val - min) * h) / (max - min));
706 } 720 }
721 }
722
723 float
724 RegionLayer::getValueForY(View *v, int y) const
725 {
726 return getValueForY(v, y, -1);
707 } 727 }
708 728
709 float 729 float
710 RegionLayer::getValueForY(View *v, int y, int avoid) const 730 RegionLayer::getValueForY(View *v, int y, int avoid) const
711 { 731 {
945 !RegionModel::Point::Comparator()(p, illuminatePoint)) { 965 !RegionModel::Point::Comparator()(p, illuminatePoint)) {
946 966
947 paint.setPen(v->getForeground()); 967 paint.setPen(v->getForeground());
948 paint.setBrush(v->getForeground()); 968 paint.setBrush(v->getForeground());
949 969
950 QString vlabel = QString("%1%2").arg(p.value).arg(m_model->getScaleUnits()); 970 QString vlabel = QString("%1%2").arg(p.value).arg(getScaleUnits());
951 v->drawVisibleText(paint, 971 v->drawVisibleText(paint,
952 x - paint.fontMetrics().width(vlabel) - 2, 972 x - paint.fontMetrics().width(vlabel) - 2,
953 y + paint.fontMetrics().height()/2 973 y + paint.fontMetrics().height()/2
954 - paint.fontMetrics().descent(), 974 - paint.fontMetrics().descent(),
955 vlabel, View::OutlinedText); 975 vlabel, View::OutlinedText);
994 } 1014 }
995 1015
996 if (!illuminated) { 1016 if (!illuminated) {
997 QString label = p.label; 1017 QString label = p.label;
998 if (label == "") { 1018 if (label == "") {
999 label = QString("%1%2").arg(p.value).arg(m_model->getScaleUnits()); 1019 label = QString("%1%2").arg(p.value).arg(getScaleUnits());
1000 } 1020 }
1001 1021
1002 int labelX, labelY; 1022 int labelX, labelY;
1003 1023
1004 if (m_plotStyle != PlotSegmentation) { 1024 if (m_plotStyle != PlotSegmentation) {
1022 } 1042 }
1023 1043
1024 paint.restore(); 1044 paint.restore();
1025 } 1045 }
1026 1046
1047 int
1048 RegionLayer::getVerticalScaleWidth(View *v, bool, QPainter &paint) const
1049 {
1050 if (!m_model ||
1051 m_verticalScale == AutoAlignScale ||
1052 m_verticalScale == EqualSpaced) {
1053 return 0;
1054 } else if (m_plotStyle == PlotSegmentation) {
1055 if (m_verticalScale == LogScale) {
1056 return LogColourScale().getWidth(v, paint);
1057 } else {
1058 return LinearColourScale().getWidth(v, paint);
1059 }
1060 } else {
1061 if (m_verticalScale == LogScale) {
1062 return LogNumericalScale().getWidth(v, paint);
1063 } else {
1064 return LinearNumericalScale().getWidth(v, paint);
1065 }
1066 }
1067 }
1068
1069 void
1070 RegionLayer::paintVerticalScale(View *v, bool, QPainter &paint, QRect) const
1071 {
1072 if (!m_model) return;
1073
1074 QString unit;
1075 float min, max;
1076 bool logarithmic;
1077
1078 int w = getVerticalScaleWidth(v, false, paint);
1079 int h = v->height();
1080
1081 if (m_plotStyle == PlotSegmentation) {
1082
1083 getValueExtents(min, max, logarithmic, unit);
1084
1085 if (logarithmic) {
1086 LogRange::mapRange(min, max);
1087 LogColourScale().paintVertical(v, this, paint, 0, min, max);
1088 } else {
1089 LinearColourScale().paintVertical(v, this, paint, 0, min, max);
1090 }
1091
1092 } else {
1093
1094 getScaleExtents(v, min, max, logarithmic);
1095
1096 if (logarithmic) {
1097 LogNumericalScale().paintVertical(v, this, paint, 0, min, max);
1098 } else {
1099 LinearNumericalScale().paintVertical(v, this, paint, 0, min, max);
1100 }
1101 }
1102
1103 if (getScaleUnits() != "") {
1104 int mw = w - 5;
1105 paint.drawText(5,
1106 5 + paint.fontMetrics().ascent(),
1107 TextAbbrev::abbreviate(getScaleUnits(),
1108 paint.fontMetrics(),
1109 mw));
1110 }
1111 }
1112
1027 void 1113 void
1028 RegionLayer::drawStart(View *v, QMouseEvent *e) 1114 RegionLayer::drawStart(View *v, QMouseEvent *e)
1029 { 1115 {
1030 if (!m_model) return; 1116 if (!m_model) return;
1031 1117
1232 (m_model->getSampleRate(), 1318 (m_model->getSampleRate(),
1233 ItemEditDialog::ShowTime | 1319 ItemEditDialog::ShowTime |
1234 ItemEditDialog::ShowDuration | 1320 ItemEditDialog::ShowDuration |
1235 ItemEditDialog::ShowValue | 1321 ItemEditDialog::ShowValue |
1236 ItemEditDialog::ShowText, 1322 ItemEditDialog::ShowText,
1237 m_model->getScaleUnits()); 1323 getScaleUnits());
1238 1324
1239 dialog->setFrameTime(region.frame); 1325 dialog->setFrameTime(region.frame);
1240 dialog->setValue(region.value); 1326 dialog->setValue(region.value);
1241 dialog->setFrameDuration(region.duration); 1327 dialog->setFrameDuration(region.duration);
1242 dialog->setText(region.label); 1328 dialog->setText(region.label);