comparison view/View.cpp @ 373:0895517bb2d1 1.2-stable

* merge from trunk (1.2 ended up being tracked from trunk, but we may want this branch for fixes later)
author Chris Cannam
date Wed, 27 Feb 2008 10:32:45 +0000
parents 4a542ba875c2
children
comparison
equal deleted inserted replaced
337:813170c57b13 373:0895517bb2d1
17 #include "layer/Layer.h" 17 #include "layer/Layer.h"
18 #include "data/model/Model.h" 18 #include "data/model/Model.h"
19 #include "base/ZoomConstraint.h" 19 #include "base/ZoomConstraint.h"
20 #include "base/Profiler.h" 20 #include "base/Profiler.h"
21 #include "base/Pitch.h" 21 #include "base/Pitch.h"
22 #include "base/Preferences.h"
22 23
23 #include "layer/TimeRulerLayer.h" 24 #include "layer/TimeRulerLayer.h"
24 #include "layer/SingleColourLayer.h" 25 #include "layer/SingleColourLayer.h"
25 #include "data/model/PowerOfSqrtTwoZoomConstraint.h" 26 #include "data/model/PowerOfSqrtTwoZoomConstraint.h"
26 #include "data/model/RangeSummarisableTimeValueModel.h" 27 #include "data/model/RangeSummarisableTimeValueModel.h"
29 #include <QPaintEvent> 30 #include <QPaintEvent>
30 #include <QRect> 31 #include <QRect>
31 #include <QApplication> 32 #include <QApplication>
32 #include <QProgressDialog> 33 #include <QProgressDialog>
33 #include <QTextStream> 34 #include <QTextStream>
35 #include <QFont>
34 36
35 #include <iostream> 37 #include <iostream>
36 #include <cassert> 38 #include <cassert>
37 #include <math.h> 39 #include <math.h>
38 40
326 changeVisible = true; 328 changeVisible = true;
327 } 329 }
328 330
329 if (e) { 331 if (e) {
330 size_t rf = alignToReference(f); 332 size_t rf = alignToReference(f);
333 #ifdef DEBUG_VIEW_WIDGET_PAINT
331 std::cerr << "View[" << this << "]::setCentreFrame(" << f 334 std::cerr << "View[" << this << "]::setCentreFrame(" << f
332 << "): emitting centreFrameChanged(" 335 << "): emitting centreFrameChanged("
333 << rf << ")" << std::endl; 336 << rf << ")" << std::endl;
337 #endif
334 emit centreFrameChanged(rf, m_followPan, m_followPlay); 338 emit centreFrameChanged(rf, m_followPan, m_followPlay);
335 } 339 }
336 } 340 }
337 341
338 return changeVisible; 342 return changeVisible;
347 long 351 long
348 View::getFrameForX(int x) const 352 View::getFrameForX(int x) const
349 { 353 {
350 long z = (long)m_zoomLevel; 354 long z = (long)m_zoomLevel;
351 long frame = m_centreFrame - (width()/2) * z; 355 long frame = m_centreFrame - (width()/2) * z;
356
357 #ifdef DEBUG_VIEW_WIDGET_PAINT
358 std::cerr << "View::getFrameForX(" << x << "): z = " << z << ", m_centreFrame = " << m_centreFrame << ", width() = " << width() << ", frame = " << frame << std::endl;
359 #endif
360
352 frame = (frame / z) * z; // this is start frame 361 frame = (frame / z) * z; // this is start frame
353 return frame + x * z; 362 return frame + x * z;
354 } 363 }
355 364
356 float 365 float
501 } 510 }
502 511
503 View::LayerProgressBar::LayerProgressBar(QWidget *parent) : 512 View::LayerProgressBar::LayerProgressBar(QWidget *parent) :
504 QProgressBar(parent) 513 QProgressBar(parent)
505 { 514 {
506 QFont f(font());
507 f.setPointSize(f.pointSize() * 8 / 10);
508 setFont(f);
509 } 515 }
510 516
511 void 517 void
512 View::addLayer(Layer *layer) 518 View::addLayer(Layer *layer)
513 { 519 {
521 527
522 m_progressBars[layer] = new LayerProgressBar(this); 528 m_progressBars[layer] = new LayerProgressBar(this);
523 m_progressBars[layer]->setMinimum(0); 529 m_progressBars[layer]->setMinimum(0);
524 m_progressBars[layer]->setMaximum(100); 530 m_progressBars[layer]->setMaximum(100);
525 m_progressBars[layer]->setMinimumWidth(80); 531 m_progressBars[layer]->setMinimumWidth(80);
532
533 QFont f(m_progressBars[layer]->font());
534 int fs = Preferences::getInstance()->getViewFontSize();
535 f.setPointSize(std::min(fs, int(ceil(fs * 0.85))));
536
537 m_progressBars[layer]->setFont(f);
526 m_progressBars[layer]->hide(); 538 m_progressBars[layer]->hide();
527 539
528 connect(layer, SIGNAL(layerParametersChanged()), 540 connect(layer, SIGNAL(layerParametersChanged()),
529 this, SLOT(layerParametersChanged())); 541 this, SLOT(layerParametersChanged()));
530 connect(layer, SIGNAL(layerParameterRangesChanged()), 542 connect(layer, SIGNAL(layerParameterRangesChanged()),
653 PlaybackFollowMode))); 665 PlaybackFollowMode)));
654 666
655 connect(this, SIGNAL(zoomLevelChanged(unsigned long, bool)), 667 connect(this, SIGNAL(zoomLevelChanged(unsigned long, bool)),
656 m_manager, SLOT(viewZoomLevelChanged(unsigned long, bool))); 668 m_manager, SLOT(viewZoomLevelChanged(unsigned long, bool)));
657 669
658 if (m_followPlay != PlaybackIgnore) { 670 if (m_followPlay == PlaybackScrollPage) {
671 // std::cerr << "View::setViewManager: setting centre frame to global centre frame: " << m_manager->getGlobalCentreFrame() << std::endl;
672 setCentreFrame(m_manager->getGlobalCentreFrame(), false);
673 } else if (m_followPlay == PlaybackScrollContinuous) {
659 // std::cerr << "View::setViewManager: setting centre frame to playback frame: " << m_manager->getPlaybackFrame() << std::endl; 674 // std::cerr << "View::setViewManager: setting centre frame to playback frame: " << m_manager->getPlaybackFrame() << std::endl;
660 setCentreFrame(m_manager->getPlaybackFrame(), false); 675 setCentreFrame(m_manager->getPlaybackFrame(), false);
661 } else if (m_followPan) { 676 } else if (m_followPan) {
662 // std::cerr << "View::setViewManager: setting centre frame to global centre frame: " << m_manager->getGlobalCentreFrame() << std::endl; 677 // std::cerr << "View::setViewManager: setting centre frame to global centre frame: " << m_manager->getGlobalCentreFrame() << std::endl;
663 setCentreFrame(m_manager->getGlobalCentreFrame(), false); 678 setCentreFrame(m_manager->getGlobalCentreFrame(), false);
878 void 893 void
879 View::globalCentreFrameChanged(unsigned long rf) 894 View::globalCentreFrameChanged(unsigned long rf)
880 { 895 {
881 if (m_followPan) { 896 if (m_followPan) {
882 size_t f = alignFromReference(rf); 897 size_t f = alignFromReference(rf);
898 #ifdef DEBUG_VIEW_WIDGET_PAINT
883 std::cerr << "View[" << this << "]::globalCentreFrameChanged(" << rf 899 std::cerr << "View[" << this << "]::globalCentreFrameChanged(" << rf
884 << "): setting centre frame to " << f << std::endl; 900 << "): setting centre frame to " << f << std::endl;
901 #endif
885 setCentreFrame(f, false); 902 setCentreFrame(f, false);
886 } 903 }
887 } 904 }
888 905
889 void 906 void
1106 !m_manager->getPlaybackModel()) { 1123 !m_manager->getPlaybackModel()) {
1107 return 0; 1124 return 0;
1108 } 1125 }
1109 1126
1110 Model *anyModel = 0; 1127 Model *anyModel = 0;
1128 Model *alignedModel = 0;
1111 Model *goodModel = 0; 1129 Model *goodModel = 0;
1112 1130
1113 for (LayerList::const_iterator i = m_layers.begin(); 1131 for (LayerList::const_iterator i = m_layers.begin();
1114 i != m_layers.end(); ++i) { 1132 i != m_layers.end(); ++i) {
1115 1133
1119 if (dynamic_cast<TimeRulerLayer *>(layer)) continue; 1137 if (dynamic_cast<TimeRulerLayer *>(layer)) continue;
1120 1138
1121 Model *model = (*i)->getModel(); 1139 Model *model = (*i)->getModel();
1122 if (!model) continue; 1140 if (!model) continue;
1123 1141
1142 anyModel = model;
1143
1124 if (model->getAlignmentReference()) { 1144 if (model->getAlignmentReference()) {
1125 anyModel = model; 1145 alignedModel = model;
1126 if (layer->isLayerOpaque() || 1146 if (layer->isLayerOpaque() ||
1127 dynamic_cast<RangeSummarisableTimeValueModel *>(model)) { 1147 dynamic_cast<RangeSummarisableTimeValueModel *>(model)) {
1128 goodModel = model; 1148 goodModel = model;
1129 } 1149 }
1130 } 1150 }
1131 } 1151 }
1132 1152
1133 if (goodModel) return goodModel; 1153 if (goodModel) return goodModel;
1154 else if (alignedModel) return alignedModel;
1134 else return anyModel; 1155 else return anyModel;
1135 } 1156 }
1136 1157
1137 size_t 1158 size_t
1138 View::alignFromReference(size_t f) const 1159 View::alignFromReference(size_t f) const
1394 if (i->second->isVisible()) { 1415 if (i->second->isVisible()) {
1395 ph -= i->second->height(); 1416 ph -= i->second->height();
1396 } 1417 }
1397 } 1418 }
1398 } 1419 }
1420 }
1421
1422 void
1423 View::setPaintFont(QPainter &paint)
1424 {
1425 QFont font(paint.font());
1426 font.setPointSize(Preferences::getInstance()->getViewFontSize());
1427 paint.setFont(font);
1399 } 1428 }
1400 1429
1401 void 1430 void
1402 View::paintEvent(QPaintEvent *e) 1431 View::paintEvent(QPaintEvent *e)
1403 { 1432 {
1581 1610
1582 if (!paintedCacheRect) { 1611 if (!paintedCacheRect) {
1583 1612
1584 if (repaintCache) paint.begin(m_cache); 1613 if (repaintCache) paint.begin(m_cache);
1585 else paint.begin(this); 1614 else paint.begin(this);
1586 1615 setPaintFont(paint);
1587 paint.setClipRect(cacheRect); 1616 paint.setClipRect(cacheRect);
1588 1617
1589 paint.setPen(getBackground()); 1618 paint.setPen(getBackground());
1590 paint.setBrush(getBackground()); 1619 paint.setBrush(getBackground());
1591 paint.drawRect(cacheRect); 1620 paint.drawRect(cacheRect);
1622 1651
1623 nonCacheRect |= cacheRect; 1652 nonCacheRect |= cacheRect;
1624 1653
1625 paint.begin(this); 1654 paint.begin(this);
1626 paint.setClipRect(nonCacheRect); 1655 paint.setClipRect(nonCacheRect);
1627 1656 setPaintFont(paint);
1628 if (scrollables.empty()) { 1657 if (scrollables.empty()) {
1629 paint.setPen(getBackground()); 1658 paint.setPen(getBackground());
1630 paint.setBrush(getBackground()); 1659 paint.setBrush(getBackground());
1631 paint.drawRect(nonCacheRect); 1660 paint.drawRect(nonCacheRect);
1632 } 1661 }
1640 } 1669 }
1641 1670
1642 paint.end(); 1671 paint.end();
1643 1672
1644 paint.begin(this); 1673 paint.begin(this);
1674 setPaintFont(paint);
1645 if (e) paint.setClipRect(e->rect()); 1675 if (e) paint.setClipRect(e->rect());
1646 if (!m_selectionCached) { 1676 if (!m_selectionCached) {
1647 drawSelections(paint); 1677 drawSelections(paint);
1648 } 1678 }
1649 paint.end(); 1679 paint.end();
1883 int axx, axy, bxx, bxy, dxx, dxy; 1913 int axx, axy, bxx, bxy, dxx, dxy;
1884 int aw = 0, bw = 0, dw = 0; 1914 int aw = 0, bw = 0, dw = 0;
1885 1915
1886 int labelCount = 0; 1916 int labelCount = 0;
1887 1917
1918 // top-left point, x-coord
1919
1888 if ((b0 = topLayer->getXScaleValue(this, r.x(), v0, u0))) { 1920 if ((b0 = topLayer->getXScaleValue(this, r.x(), v0, u0))) {
1889 axs = QString("%1 %2").arg(v0).arg(u0); 1921 axs = QString("%1 %2").arg(v0).arg(u0);
1890 if (u0 == "Hz" && Pitch::isFrequencyInMidiRange(v0)) { 1922 if (u0 == "Hz" && Pitch::isFrequencyInMidiRange(v0)) {
1891 axs = QString("%1 (%2)").arg(axs) 1923 axs = QString("%1 (%2)").arg(axs)
1892 .arg(Pitch::getPitchLabelForFrequency(v0)); 1924 .arg(Pitch::getPitchLabelForFrequency(v0));
1893 } 1925 }
1894 aw = paint.fontMetrics().width(axs); 1926 aw = paint.fontMetrics().width(axs);
1895 ++labelCount; 1927 ++labelCount;
1896 } 1928 }
1929
1930 // bottom-right point, x-coord
1897 1931
1898 if (r.width() > 0) { 1932 if (r.width() > 0) {
1899 if ((b1 = topLayer->getXScaleValue(this, r.x() + r.width(), v1, u1))) { 1933 if ((b1 = topLayer->getXScaleValue(this, r.x() + r.width(), v1, u1))) {
1900 bxs = QString("%1 %2").arg(v1).arg(u1); 1934 bxs = QString("%1 %2").arg(v1).arg(u1);
1901 if (u1 == "Hz" && Pitch::isFrequencyInMidiRange(v1)) { 1935 if (u1 == "Hz" && Pitch::isFrequencyInMidiRange(v1)) {
1903 .arg(Pitch::getPitchLabelForFrequency(v1)); 1937 .arg(Pitch::getPitchLabelForFrequency(v1));
1904 } 1938 }
1905 bw = paint.fontMetrics().width(bxs); 1939 bw = paint.fontMetrics().width(bxs);
1906 } 1940 }
1907 } 1941 }
1942
1943 // dimension, width
1908 1944
1909 if (b0 && b1 && v1 != v0 && u0 == u1) { 1945 if (b0 && b1 && v1 != v0 && u0 == u1) {
1910 dxs = QString("(%1 %2)").arg(fabs(v1 - v0)).arg(u1); 1946 dxs = QString("[%1 %2]").arg(fabs(v1 - v0)).arg(u1);
1911 dw = paint.fontMetrics().width(dxs); 1947 dw = paint.fontMetrics().width(dxs);
1912 } 1948 }
1913 1949
1914 b0 = false; 1950 b0 = false;
1915 b1 = false; 1951 b1 = false;
1952
1953 // top-left point, y-coord
1916 1954
1917 if ((b0 = topLayer->getYScaleValue(this, r.y(), v0, u0))) { 1955 if ((b0 = topLayer->getYScaleValue(this, r.y(), v0, u0))) {
1918 ays = QString("%1 %2").arg(v0).arg(u0); 1956 ays = QString("%1 %2").arg(v0).arg(u0);
1919 if (u0 == "Hz" && Pitch::isFrequencyInMidiRange(v0)) { 1957 if (u0 == "Hz" && Pitch::isFrequencyInMidiRange(v0)) {
1920 ays = QString("%1 (%2)").arg(ays) 1958 ays = QString("%1 (%2)").arg(ays)
1921 .arg(Pitch::getPitchLabelForFrequency(v0)); 1959 .arg(Pitch::getPitchLabelForFrequency(v0));
1922 } 1960 }
1923 aw = std::max(aw, paint.fontMetrics().width(ays)); 1961 aw = std::max(aw, paint.fontMetrics().width(ays));
1924 ++labelCount; 1962 ++labelCount;
1925 } 1963 }
1964
1965 // bottom-right point, y-coord
1926 1966
1927 if (r.height() > 0) { 1967 if (r.height() > 0) {
1928 if ((b1 = topLayer->getYScaleValue(this, r.y() + r.height(), v1, u1))) { 1968 if ((b1 = topLayer->getYScaleValue(this, r.y() + r.height(), v1, u1))) {
1929 bys = QString("%1 %2").arg(v1).arg(u1); 1969 bys = QString("%1 %2").arg(v1).arg(u1);
1930 if (u1 == "Hz" && Pitch::isFrequencyInMidiRange(v1)) { 1970 if (u1 == "Hz" && Pitch::isFrequencyInMidiRange(v1)) {
1937 1977
1938 bool bd = false; 1978 bool bd = false;
1939 float dy = 0.f; 1979 float dy = 0.f;
1940 QString du; 1980 QString du;
1941 1981
1982 // dimension, height
1983
1942 if ((bd = topLayer->getYScaleDifference(this, r.y(), r.y() + r.height(), 1984 if ((bd = topLayer->getYScaleDifference(this, r.y(), r.y() + r.height(),
1943 dy, du)) && 1985 dy, du)) &&
1944 dy != 0) { 1986 dy != 0) {
1945 if (du != "") { 1987 if (du != "") {
1946 dys = QString("(%1 %2)").arg(dy).arg(du); 1988 if (du == "Hz") {
1989 int semis;
1990 float cents;
1991 semis = Pitch::getPitchForFrequencyDifference(v0, v1, &cents);
1992 dys = QString("[%1 %2 (%3)]")
1993 .arg(dy).arg(du)
1994 .arg(Pitch::getLabelForPitchRange(semis, cents));
1995 } else {
1996 dys = QString("[%1 %2]").arg(dy).arg(du);
1997 }
1947 } else { 1998 } else {
1948 dys = QString("(%1)").arg(dy); 1999 dys = QString("[%1]").arg(dy);
1949 } 2000 }
1950 dw = std::max(dw, paint.fontMetrics().width(dys)); 2001 dw = std::max(dw, paint.fontMetrics().width(dys));
1951 } 2002 }
1952 2003
1953 int mw = r.width(); 2004 int mw = r.width();