comparison widgets/UnitConverter.cpp @ 889:cd59f6e1aa26

hz -> freq, and some layout rearrangement
author Chris Cannam
date Tue, 09 Dec 2014 11:45:34 +0000
parents cb3a3e20634d
children 1f2dc7fcad92
comparison
equal deleted inserted replaced
888:cb3a3e20634d 889:cd59f6e1aa26
18 #include <QComboBox> 18 #include <QComboBox>
19 #include <QDoubleSpinBox> 19 #include <QDoubleSpinBox>
20 #include <QLabel> 20 #include <QLabel>
21 #include <QDialogButtonBox> 21 #include <QDialogButtonBox>
22 #include <QGridLayout> 22 #include <QGridLayout>
23 #include <QTabWidget>
23 24
24 #include "base/Debug.h" 25 #include "base/Debug.h"
25 #include "base/Pitch.h" 26 #include "base/Pitch.h"
26 #include "base/Preferences.h" 27 #include "base/Preferences.h"
27 28
33 }; 34 };
34 35
35 UnitConverter::UnitConverter(QWidget *parent) : 36 UnitConverter::UnitConverter(QWidget *parent) :
36 QDialog(parent) 37 QDialog(parent)
37 { 38 {
39 QGridLayout *maingrid = new QGridLayout;
40 setLayout(maingrid);
41
42 QTabWidget *tabs = new QTabWidget;
43 maingrid->addWidget(tabs, 0, 0);
44
45 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
46 maingrid->addWidget(bb, 1, 0);
47 connect(bb, SIGNAL(rejected()), this, SLOT(close()));
48
49 QFrame *frame = new QFrame;
50 tabs->addTab(frame, tr("Pitch"));
51
38 QGridLayout *grid = new QGridLayout; 52 QGridLayout *grid = new QGridLayout;
39 setLayout(grid); 53 frame->setLayout(grid);
40 54
41 m_hz = new QDoubleSpinBox; 55 m_freq = new QDoubleSpinBox;
42 m_hz->setSuffix(QString(" Hz")); 56 m_freq->setSuffix(QString(" Hz"));
43 m_hz->setDecimals(6); 57 m_freq->setDecimals(6);
44 m_hz->setMinimum(1e-3); 58 m_freq->setMinimum(1e-3);
45 m_hz->setMaximum(1e6); 59 m_freq->setMaximum(1e6);
46 m_hz->setValue(440); 60 m_freq->setValue(440);
47 connect(m_hz, SIGNAL(valueChanged(double)), 61 connect(m_freq, SIGNAL(valueChanged(double)),
48 this, SLOT(hzChanged(double))); 62 this, SLOT(freqChanged(double)));
49 63
50 // The min and max range values for all the remaining controls are 64 // The min and max range values for all the remaining controls are
51 // determined by the min and max Hz above 65 // determined by the min and max Hz above
52 66
53 m_midi = new QSpinBox; 67 m_midi = new QSpinBox;
80 m_piano = new QSpinBox; 94 m_piano = new QSpinBox;
81 //!!! 95 //!!!
82 96
83 int row = 1; 97 int row = 1;
84 98
85 grid->addWidget(m_hz, row, 0); 99 grid->addWidget(m_freq, row, 0, 2, 1);
86 grid->addWidget(new QLabel(tr("=")), row, 1); 100 grid->addWidget(new QLabel(tr("=")), row, 1, 2, 1);
87 101
88 grid->addWidget(new QLabel(tr("+")), row, 7); 102 grid->addWidget(new QLabel(tr("+")), row, 7, 2, 1);
89 grid->addWidget(m_cents, row, 8); 103 grid->addWidget(m_cents, row, 8, 2, 1);
90 104
91 grid->addWidget(new QLabel(tr("Piano note")), row, 2, 1, 2); 105 grid->addWidget(new QLabel(tr("Piano note")), row, 2, 1, 2);
92 grid->addWidget(m_note, row, 4); 106 grid->addWidget(m_note, row, 4);
93 grid->addWidget(new QLabel(tr("in octave")), row, 5); 107 grid->addWidget(new QLabel(tr("in octave")), row, 5);
94 grid->addWidget(m_octave, row, 6); 108 grid->addWidget(m_octave, row, 6);
115 "in the MIDI protocol.")), 129 "in the MIDI protocol.")),
116 row, 0, 1, 9); 130 row, 0, 1, 9);
117 131
118 ++row; 132 ++row;
119 133
120 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close); 134 QFrame *frame = new QFrame;
121 grid->addWidget(bb, row, 0, 1, 9); 135 tabs->addTab(frame, tr("Pitch"));
122 connect(bb, SIGNAL(rejected()), this, SLOT(close())); 136
123 137 QGridLayout *grid = new QGridLayout;
124 updateAllFromHz(); 138 frame->setLayout(grid);
139
140 updatePitchesFromFreq();
125 } 141 }
126 142
127 UnitConverter::~UnitConverter() 143 UnitConverter::~UnitConverter()
128 { 144 {
129 } 145 }
130 146
131 void 147 void
132 UnitConverter::hzChanged(double hz) 148 UnitConverter::freqChanged(double freq)
133 { 149 {
134 cerr << "hzChanged: " << hz << endl; 150 cerr << "freqChanged: " << freq << endl;
135 updateAllFromHz(); 151 updatePitchesFromFreq();
136 } 152 }
137 153
138 void 154 void
139 UnitConverter::midiChanged(int midi) 155 UnitConverter::midiChanged(int midi)
140 { 156 {
141 cerr << "midiChanged: " << midi << endl; 157 cerr << "midiChanged: " << midi << endl;
142 double hz = Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value()); 158 double freq = Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value());
143 cerr << "hz -> " << hz << endl; 159 cerr << "freq -> " << freq << endl;
144 m_hz->setValue(hz); 160 m_freq->setValue(freq);
145 } 161 }
146 162
147 void 163 void
148 UnitConverter::noteChanged(int note) 164 UnitConverter::noteChanged(int note)
149 { 165 {
150 cerr << "noteChanged: " << note << endl; 166 cerr << "noteChanged: " << note << endl;
151 int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(), 167 int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(),
152 m_octave->value()); 168 m_octave->value());
153 double hz = Pitch::getFrequencyForPitch(pitch, m_cents->value()); 169 double freq = Pitch::getFrequencyForPitch(pitch, m_cents->value());
154 cerr << "hz -> " << hz << endl; 170 cerr << "freq -> " << freq << endl;
155 m_hz->setValue(hz); 171 m_freq->setValue(freq);
156 } 172 }
157 173
158 void 174 void
159 UnitConverter::octaveChanged(int oct) 175 UnitConverter::octaveChanged(int oct)
160 { 176 {
161 cerr << "octaveChanged: " << oct << endl; 177 cerr << "octaveChanged: " << oct << endl;
162 int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(), 178 int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(),
163 m_octave->value()); 179 m_octave->value());
164 double hz = Pitch::getFrequencyForPitch(pitch, m_cents->value()); 180 double freq = Pitch::getFrequencyForPitch(pitch, m_cents->value());
165 cerr << "hz -> " << hz << endl; 181 cerr << "freq -> " << freq << endl;
166 m_hz->setValue(hz); 182 m_freq->setValue(freq);
167 } 183 }
168 184
169 void 185 void
170 UnitConverter::centsChanged(double cents) 186 UnitConverter::centsChanged(double cents)
171 { 187 {
172 cerr << "centsChanged: " << cents << endl; 188 cerr << "centsChanged: " << cents << endl;
173 double hz = Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value()); 189 double freq = Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value());
174 cerr << "hz -> " << hz << endl; 190 cerr << "freq -> " << freq << endl;
175 m_hz->setValue(hz); 191 m_freq->setValue(freq);
176 } 192 }
177 193
178 void 194 void
179 UnitConverter::pianoChanged(int piano) 195 UnitConverter::pianoChanged(int piano)
180 { 196 {
181 cerr << "pianoChanged: " << piano << endl; 197 cerr << "pianoChanged: " << piano << endl;
182 } 198 }
183 199
184 void 200 void
185 UnitConverter::updateAllFromHz() 201 UnitConverter::updatePitchesFromFreq()
186 { 202 {
187 float cents = 0; 203 float cents = 0;
188 int pitch = Pitch::getPitchForFrequency(m_hz->value(), &cents); 204 int pitch = Pitch::getPitchForFrequency(m_freq->value(), &cents);
189 int note, octave; 205 int note, octave;
190 Pitch::getNoteAndOctaveForPitch(pitch, note, octave); 206 Pitch::getNoteAndOctaveForPitch(pitch, note, octave);
191 207
192 cerr << "pitch " << pitch << " note " << note << " octave " << octave << " cents " << cents << endl; 208 cerr << "pitch " << pitch << " note " << note << " octave " << octave << " cents " << cents << endl;
193 209