comparison layer/NoteLayer.cpp @ 702:ceb9a2992d96 tonioni

Merge from default branch
author Chris Cannam
date Wed, 04 Dec 2013 13:46:29 +0000
parents 6d9624e0ac55 084f65094203
children 137d3ff48f73
comparison
equal deleted inserted replaced
696:e77b1673e17e 702:ceb9a2992d96
21 #include "base/Pitch.h" 21 #include "base/Pitch.h"
22 #include "base/LogRange.h" 22 #include "base/LogRange.h"
23 #include "base/RangeMapper.h" 23 #include "base/RangeMapper.h"
24 #include "ColourDatabase.h" 24 #include "ColourDatabase.h"
25 #include "view/View.h" 25 #include "view/View.h"
26
26 #include "PianoScale.h" 27 #include "PianoScale.h"
28 #include "LinearNumericalScale.h"
29 #include "LogNumericalScale.h"
27 30
28 #include "data/model/NoteModel.h" 31 #include "data/model/NoteModel.h"
29 32
30 #include "widgets/ItemEditDialog.h" 33 #include "widgets/ItemEditDialog.h"
34 #include "widgets/TextAbbrev.h"
31 35
32 #include <QPainter> 36 #include <QPainter>
33 #include <QPainterPath> 37 #include <QPainterPath>
34 #include <QMouseEvent> 38 #include <QMouseEvent>
35 #include <QTextStream> 39 #include <QTextStream>
103 return tr("Scale"); 107 return tr("Scale");
104 } 108 }
105 return SingleColourLayer::getPropertyGroupName(name); 109 return SingleColourLayer::getPropertyGroupName(name);
106 } 110 }
107 111
112 QString
113 NoteLayer::getScaleUnits() const
114 {
115 if (m_model) return m_model->getScaleUnits();
116 else return "";
117 }
118
108 int 119 int
109 NoteLayer::getPropertyRangeAndValue(const PropertyName &name, 120 NoteLayer::getPropertyRangeAndValue(const PropertyName &name,
110 int *min, int *max, int *deflt) const 121 int *min, int *max, int *deflt) const
111 { 122 {
112 int val = 0; 123 int val = 0;
122 } else if (name == "Scale Units") { 133 } else if (name == "Scale Units") {
123 134
124 if (deflt) *deflt = 0; 135 if (deflt) *deflt = 0;
125 if (m_model) { 136 if (m_model) {
126 val = UnitDatabase::getInstance()->getUnitId 137 val = UnitDatabase::getInstance()->getUnitId
127 (m_model->getScaleUnits()); 138 (getScaleUnits());
128 } 139 }
129 140
130 } else { 141 } else {
131 142
132 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt); 143 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
183 } 194 }
184 195
185 bool 196 bool
186 NoteLayer::shouldConvertMIDIToHz() const 197 NoteLayer::shouldConvertMIDIToHz() const
187 { 198 {
188 QString unit = m_model->getScaleUnits(); 199 QString unit = getScaleUnits();
189 return (unit != "Hz"); 200 return (unit != "Hz");
190 // if (unit == "" || 201 // if (unit == "" ||
191 // unit.startsWith("MIDI") || 202 // unit.startsWith("MIDI") ||
192 // unit.startsWith("midi")) return true; 203 // unit.startsWith("midi")) return true;
193 // return false; 204 // return false;
203 214
204 if (shouldConvertMIDIToHz()) { 215 if (shouldConvertMIDIToHz()) {
205 unit = "Hz"; 216 unit = "Hz";
206 min = Pitch::getFrequencyForPitch(lrintf(min)); 217 min = Pitch::getFrequencyForPitch(lrintf(min));
207 max = Pitch::getFrequencyForPitch(lrintf(max + 1)); 218 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
208 } else unit = m_model->getScaleUnits(); 219 } else unit = getScaleUnits();
209 220
210 if (m_verticalScale == MIDIRangeScale || 221 if (m_verticalScale == MIDIRangeScale ||
211 m_verticalScale == LogScale) logarithmic = true; 222 m_verticalScale == LogScale) logarithmic = true;
212 223
213 return true; 224 return true;
497 pitchText = tr("%1 (%2, %3 Hz)") 508 pitchText = tr("%1 (%2, %3 Hz)")
498 .arg(Pitch::getPitchLabel(mnote, cents)) 509 .arg(Pitch::getPitchLabel(mnote, cents))
499 .arg(mnote) 510 .arg(mnote)
500 .arg(freq); 511 .arg(freq);
501 512
502 } else if (m_model->getScaleUnits() == "Hz") { 513 } else if (getScaleUnits() == "Hz") {
503 514
504 pitchText = tr("%1 Hz (%2, %3)") 515 pitchText = tr("%1 Hz (%2, %3)")
505 .arg(note.value) 516 .arg(note.value)
506 .arg(Pitch::getPitchLabelForFrequency(note.value)) 517 .arg(Pitch::getPitchLabelForFrequency(note.value))
507 .arg(Pitch::getPitchForFrequency(note.value)); 518 .arg(Pitch::getPitchForFrequency(note.value));
508 519
509 } else { 520 } else {
510 pitchText = tr("%1 %2") 521 pitchText = tr("%1 %2")
511 .arg(note.value).arg(m_model->getScaleUnits()); 522 .arg(note.value).arg(getScaleUnits());
512 } 523 }
513 524
514 QString text; 525 QString text;
515 526
516 if (note.label == "") { 527 if (note.label == "") {
610 max = 0.0; 621 max = 0.0;
611 log = false; 622 log = false;
612 623
613 QString queryUnits; 624 QString queryUnits;
614 if (shouldConvertMIDIToHz()) queryUnits = "Hz"; 625 if (shouldConvertMIDIToHz()) queryUnits = "Hz";
615 else queryUnits = m_model->getScaleUnits(); 626 else queryUnits = getScaleUnits();
616 627
617 if (shouldAutoAlign()) { 628 if (shouldAutoAlign()) {
618 629
619 if (!v->getValueExtents(queryUnits, min, max, log)) { 630 if (!v->getValueExtents(queryUnits, min, max, log)) {
620 631
791 !NoteModel::Point::Comparator()(p, illuminatePoint)) { 802 !NoteModel::Point::Comparator()(p, illuminatePoint)) {
792 803
793 paint.setPen(v->getForeground()); 804 paint.setPen(v->getForeground());
794 paint.setBrush(v->getForeground()); 805 paint.setBrush(v->getForeground());
795 806
796 QString vlabel = QString("%1%2").arg(p.value).arg(m_model->getScaleUnits()); 807 QString vlabel = QString("%1%2").arg(p.value).arg(getScaleUnits());
797 v->drawVisibleText(paint, 808 v->drawVisibleText(paint,
798 x - paint.fontMetrics().width(vlabel) - 2, 809 x - paint.fontMetrics().width(vlabel) - 2,
799 y + paint.fontMetrics().height()/2 810 y + paint.fontMetrics().height()/2
800 - paint.fontMetrics().descent(), 811 - paint.fontMetrics().descent(),
801 vlabel, View::OutlinedText); 812 vlabel, View::OutlinedText);
813 824
814 paint.restore(); 825 paint.restore();
815 } 826 }
816 827
817 int 828 int
818 NoteLayer::getVerticalScaleWidth(View *, bool, QPainter &paint) const 829 NoteLayer::getVerticalScaleWidth(View *v, bool, QPainter &paint) const
819 { 830 {
820 return 10; 831 if (!m_model || shouldAutoAlign()) {
832 return 0;
833 } else {
834 if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) {
835 return LogNumericalScale().getWidth(v, paint) + 10; // for piano
836 } else {
837 return LinearNumericalScale().getWidth(v, paint);
838 }
839 }
821 } 840 }
822 841
823 void 842 void
824 NoteLayer::paintVerticalScale(View *v, bool, QPainter &paint, QRect) const 843 NoteLayer::paintVerticalScale(View *v, bool, QPainter &paint, QRect) const
825 { 844 {
826 float fmin, fmax; 845 if (!m_model) return;
827 bool log; 846
828 QString unit; 847 QString unit;
829 if (!getValueExtents(fmin, fmax, log, unit)) return; 848 float min, max;
830 PianoScale().paintPianoVertical 849 bool logarithmic;
831 (v, paint, QRect(0, 0, 10, v->height()), fmin, fmax); 850
832 paint.drawLine(10, 0, 10, v->height()); 851 int w = getVerticalScaleWidth(v, false, paint);
852 int h = v->height();
853
854 getScaleExtents(v, min, max, logarithmic);
855
856 if (logarithmic) {
857 LogNumericalScale().paintVertical(v, this, paint, 0, min, max);
858 } else {
859 LinearNumericalScale().paintVertical(v, this, paint, 0, min, max);
860 }
861
862 if (logarithmic && (getScaleUnits() == "Hz")) {
863 PianoScale().paintPianoVertical
864 (v, paint, QRect(w - 10, 0, 10, h),
865 LogRange::unmap(min),
866 LogRange::unmap(max));
867 paint.drawLine(w, 0, w, h);
868 }
869
870 if (getScaleUnits() != "") {
871 int mw = w - 5;
872 paint.drawText(5,
873 5 + paint.fontMetrics().ascent(),
874 TextAbbrev::abbreviate(getScaleUnits(),
875 paint.fontMetrics(),
876 mw));
877 }
833 } 878 }
834 879
835 void 880 void
836 NoteLayer::drawStart(View *v, QMouseEvent *e) 881 NoteLayer::drawStart(View *v, QMouseEvent *e)
837 { 882 {
1029 (m_model->getSampleRate(), 1074 (m_model->getSampleRate(),
1030 ItemEditDialog::ShowTime | 1075 ItemEditDialog::ShowTime |
1031 ItemEditDialog::ShowDuration | 1076 ItemEditDialog::ShowDuration |
1032 ItemEditDialog::ShowValue | 1077 ItemEditDialog::ShowValue |
1033 ItemEditDialog::ShowText, 1078 ItemEditDialog::ShowText,
1034 m_model->getScaleUnits()); 1079 getScaleUnits());
1035 1080
1036 dialog->setFrameTime(note.frame); 1081 dialog->setFrameTime(note.frame);
1037 dialog->setValue(note.value); 1082 dialog->setValue(note.value);
1038 dialog->setFrameDuration(note.duration); 1083 dialog->setFrameDuration(note.duration);
1039 dialog->setText(note.label); 1084 dialog->setText(note.label);