comparison layer/TimeValueLayer.cpp @ 698:ad7623c39396

Pull out log and linear vertical scales into their own classes, make some improvements to log numbering
author Chris Cannam
date Wed, 04 Dec 2013 11:35:08 +0000
parents 26b1ffe412f9
children 1a1448f7beb2
comparison
equal deleted inserted replaced
697:26b1ffe412f9 698:ad7623c39396
29 #include "widgets/ItemEditDialog.h" 29 #include "widgets/ItemEditDialog.h"
30 #include "widgets/ListInputDialog.h" 30 #include "widgets/ListInputDialog.h"
31 31
32 #include "ColourMapper.h" 32 #include "ColourMapper.h"
33 #include "PianoScale.h" 33 #include "PianoScale.h"
34 #include "LinearNumericalScale.h"
35 #include "LogNumericalScale.h"
34 36
35 #include <QPainter> 37 #include <QPainter>
36 #include <QPainterPath> 38 #include <QPainterPath>
37 #include <QMouseEvent> 39 #include <QMouseEvent>
38 #include <QRegExp> 40 #include <QRegExp>
142 return tr("Plot Type"); 144 return tr("Plot Type");
143 } 145 }
144 return SingleColourLayer::getPropertyGroupName(name); 146 return SingleColourLayer::getPropertyGroupName(name);
145 } 147 }
146 148
149 QString
150 TimeValueLayer::getScaleUnits() const
151 {
152 if (m_model) return m_model->getScaleUnits();
153 else return "";
154 }
155
147 int 156 int
148 TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name, 157 TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name,
149 int *min, int *max, int *deflt) const 158 int *min, int *max, int *deflt) const
150 { 159 {
151 int val = 0; 160 int val = 0;
177 } else if (name == "Scale Units") { 186 } else if (name == "Scale Units") {
178 187
179 if (deflt) *deflt = 0; 188 if (deflt) *deflt = 0;
180 if (m_model) { 189 if (m_model) {
181 val = UnitDatabase::getInstance()->getUnitId 190 val = UnitDatabase::getInstance()->getUnitId
182 (m_model->getScaleUnits()); 191 (getScaleUnits());
183 } 192 }
184 193
185 } else if (name == "Draw Segment Division Lines") { 194 } else if (name == "Draw Segment Division Lines") {
186 195
187 if (min) *min = 0; 196 if (min) *min = 0;
325 min = m_model->getValueMinimum(); 334 min = m_model->getValueMinimum();
326 max = m_model->getValueMaximum(); 335 max = m_model->getValueMaximum();
327 336
328 logarithmic = (m_verticalScale == LogScale); 337 logarithmic = (m_verticalScale == LogScale);
329 338
330 unit = m_model->getScaleUnits(); 339 unit = getScaleUnits();
331 340
332 if (m_derivative) { 341 if (m_derivative) {
333 max = std::max(fabsf(min), fabsf(max)); 342 max = std::max(fabsf(min), fabsf(max));
334 min = -max; 343 min = -max;
335 } 344 }
593 long useFrame = points.begin()->frame; 602 long useFrame = points.begin()->frame;
594 603
595 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate()); 604 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
596 605
597 QString text; 606 QString text;
598 QString unit = m_model->getScaleUnits(); 607 QString unit = getScaleUnits();
599 if (unit != "") unit = " " + unit; 608 if (unit != "") unit = " " + unit;
600 609
601 if (points.begin()->label == "") { 610 if (points.begin()->label == "") {
602 text = QString(tr("Time:\t%1\nValue:\t%2%3\nNo label")) 611 text = QString(tr("Time:\t%1\nValue:\t%2%3\nNo label"))
603 .arg(rt.toText(true).c_str()) 612 .arg(rt.toText(true).c_str())
768 max = 0.0; 777 max = 0.0;
769 log = false; 778 log = false;
770 779
771 if (shouldAutoAlign()) { 780 if (shouldAutoAlign()) {
772 781
773 if (!v->getValueExtents(m_model->getScaleUnits(), min, max, log)) { 782 if (!v->getValueExtents(getScaleUnits(), min, max, log)) {
774 min = m_model->getValueMinimum(); 783 min = m_model->getValueMinimum();
775 max = m_model->getValueMaximum(); 784 max = m_model->getValueMaximum();
776 } else if (log) { 785 } else if (log) {
777 LogRange::mapRange(min, max); 786 LogRange::mapRange(min, max);
778 } 787 }
838 847
839 bool 848 bool
840 TimeValueLayer::shouldAutoAlign() const 849 TimeValueLayer::shouldAutoAlign() const
841 { 850 {
842 if (!m_model) return false; 851 if (!m_model) return false;
843 QString unit = m_model->getScaleUnits(); 852 QString unit = getScaleUnits();
844 return (m_verticalScale == AutoAlignScale && unit != ""); 853 return (m_verticalScale == AutoAlignScale && unit != "");
845 } 854 }
846 855
847 QColor 856 QColor
848 TimeValueLayer::getColourForValue(View *v, float val) const 857 TimeValueLayer::getColourForValue(View *v, float val) const
1194 // looks like save/restore doesn't deal with this: 1203 // looks like save/restore doesn't deal with this:
1195 paint.setRenderHint(QPainter::Antialiasing, false); 1204 paint.setRenderHint(QPainter::Antialiasing, false);
1196 } 1205 }
1197 1206
1198 int 1207 int
1199 TimeValueLayer::getVerticalScaleWidth(View *, bool, QPainter &paint) const 1208 TimeValueLayer::getVerticalScaleWidth(View *v, bool, QPainter &paint) const
1200 { 1209 {
1201 if (!m_model || shouldAutoAlign()) return 0; 1210 if (!m_model || shouldAutoAlign()) return 0;
1202 int w = paint.fontMetrics().width("-000.000"); 1211 int w = 0;
1212 if (m_verticalScale == LogScale) {
1213 w = LogNumericalScale().getWidth(v, paint);
1214 } else {
1215 w = LinearNumericalScale().getWidth(v, paint);
1216 }
1203 if (m_plotStyle == PlotSegmentation) return w + 20; 1217 if (m_plotStyle == PlotSegmentation) return w + 20;
1204 else return w + 10; 1218 else return w + 10;
1205 } 1219 }
1206 1220
1207 void 1221 void
1208 TimeValueLayer::paintVerticalScale(View *v, bool, QPainter &paint, QRect) const 1222 TimeValueLayer::paintVerticalScale(View *v, bool, QPainter &paint, QRect) const
1209 { 1223 {
1210 if (!m_model) return; 1224 if (!m_model) return;
1211 1225
1226 /*
1212 int h = v->height(); 1227 int h = v->height();
1213 1228
1214 int n = 10; 1229 int n = 10;
1215 1230
1216 float min, max; 1231 float min, max;
1233 int w = getVerticalScaleWidth(v, false, paint); 1248 int w = getVerticalScaleWidth(v, false, paint);
1234 1249
1235 int tx = 5; 1250 int tx = 5;
1236 1251
1237 int boxx = 5, boxy = 5; 1252 int boxx = 5, boxy = 5;
1238 if (m_model->getScaleUnits() != "") { 1253 if (getScaleUnits() != "") {
1239 boxy += paint.fontMetrics().height(); 1254 boxy += paint.fontMetrics().height();
1240 } 1255 }
1241 int boxw = 10, boxh = h - boxy - 5; 1256 int boxw = 10, boxh = h - boxy - 5;
1242 1257
1243 if (m_plotStyle == PlotSegmentation) { 1258 if (m_plotStyle == PlotSegmentation) {
1284 y = boxy + int(boxh - ((val - min) * boxh) / (max - min)); 1299 y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
1285 ty = y; 1300 ty = y;
1286 } else { 1301 } else {
1287 if (i == n-1 && 1302 if (i == n-1 &&
1288 v->height() < paint.fontMetrics().height() * (n*2)) { 1303 v->height() < paint.fontMetrics().height() * (n*2)) {
1289 if (m_model->getScaleUnits() != "") drawText = false; 1304 if (getScaleUnits() != "") drawText = false;
1290 } 1305 }
1291 dispval = lrintf(val / round) * round; 1306 dispval = lrintf(val / round) * round;
1292 #ifdef DEBUG_TIME_VALUE_LAYER 1307 #ifdef DEBUG_TIME_VALUE_LAYER
1293 cerr << "val = " << val << ", dispval = " << dispval << endl; 1308 cerr << "val = " << val << ", dispval = " << dispval << endl;
1294 #endif 1309 #endif
1333 } 1348 }
1334 1349
1335 prevy = y; 1350 prevy = y;
1336 val += inc; 1351 val += inc;
1337 } 1352 }
1338 1353 */
1339 if (m_model->getScaleUnits() != "") { 1354 float min, max;
1355 bool logarithmic;
1356 getScaleExtents(v, min, max, logarithmic);
1357
1358 int w = getVerticalScaleWidth(v, false, paint);
1359 int h = v->height();
1360
1361 if (m_plotStyle == PlotSegmentation) {
1362
1363 //!!! todo!
1364
1365 } else {
1366
1367 if (logarithmic) {
1368 LogNumericalScale().paintVertical(v, this, paint, 0, min, max);
1369 } else {
1370 LinearNumericalScale().paintVertical(v, this, paint, 0, min, max);
1371 }
1372
1373 if (logarithmic && (getScaleUnits() == "Hz")) {
1374 PianoScale().paintPianoVertical
1375 (v, paint, QRect(w - 10, 0, 10, h),
1376 LogRange::unmap(min),
1377 LogRange::unmap(max));
1378 paint.drawLine(w, 0, w, h);
1379 }
1380 }
1381
1382 if (getScaleUnits() != "") {
1340 paint.drawText(5, 5 + paint.fontMetrics().ascent(), 1383 paint.drawText(5, 5 + paint.fontMetrics().ascent(),
1341 m_model->getScaleUnits()); 1384 getScaleUnits());
1342 }
1343
1344 if (logarithmic &&
1345 (m_model->getScaleUnits() == "Hz") &&
1346 (m_plotStyle != PlotSegmentation)) {
1347 float fmin, fmax;
1348 getDisplayExtents(fmin, fmax);
1349 PianoScale().paintPianoVertical
1350 (v, paint, QRect(w, 0, 10, h), fmin, fmax);
1351 paint.drawLine(w + 10, 0, w + 10, h);
1352 } 1385 }
1353 } 1386 }
1354 1387
1355 void 1388 void
1356 TimeValueLayer::drawStart(View *v, QMouseEvent *e) 1389 TimeValueLayer::drawStart(View *v, QMouseEvent *e)
1613 ItemEditDialog *dialog = new ItemEditDialog 1646 ItemEditDialog *dialog = new ItemEditDialog
1614 (m_model->getSampleRate(), 1647 (m_model->getSampleRate(),
1615 ItemEditDialog::ShowTime | 1648 ItemEditDialog::ShowTime |
1616 ItemEditDialog::ShowValue | 1649 ItemEditDialog::ShowValue |
1617 ItemEditDialog::ShowText, 1650 ItemEditDialog::ShowText,
1618 m_model->getScaleUnits()); 1651 getScaleUnits());
1619 1652
1620 dialog->setFrameTime(point.frame); 1653 dialog->setFrameTime(point.frame);
1621 dialog->setValue(point.value); 1654 dialog->setValue(point.value);
1622 dialog->setText(point.label); 1655 dialog->setText(point.label);
1623 1656