comparison widgets/ItemEditDialog.cpp @ 0:fc9323a41f5a

start base : Sonic Visualiser sv1-1.0rc1
author lbajardsilogic
date Fri, 11 May 2007 09:08:14 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:fc9323a41f5a
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "ItemEditDialog.h"
17
18 #include <QLineEdit>
19 #include <QDoubleSpinBox>
20 #include <QSpinBox>
21 #include <QGridLayout>
22 #include <QHBoxLayout>
23 #include <QLabel>
24 #include <QPushButton>
25 #include <QGroupBox>
26
27 #include <float.h> // for FLT_MIN/MAX
28
29
30 ItemEditDialog::ItemEditDialog(size_t sampleRate, int options,
31 QString valueUnits, QWidget *parent) :
32 QDialog(parent),
33 m_sampleRate(sampleRate),
34 m_frameTimeSpinBox(0),
35 m_realTimeSecsSpinBox(0),
36 m_realTimeUSecsSpinBox(0),
37 m_frameDurationSpinBox(0),
38 m_realDurationSecsSpinBox(0),
39 m_realDurationUSecsSpinBox(0),
40 m_valueSpinBox(0),
41 m_textField(0)
42 {
43 QGridLayout *grid = new QGridLayout;
44 setLayout(grid);
45
46 QGroupBox *timeBox = 0;
47 QGroupBox *valueBox = 0;
48 QGridLayout *subgrid = 0;
49
50 int row = 0, subrow = 0;
51
52 size_t singleStep = RealTime::frame2RealTime(2, sampleRate).usec() - 1;
53
54 if ((options & ShowTime) || (options & ShowDuration)) {
55
56 timeBox = new QGroupBox;
57 timeBox->setTitle(tr("Timing"));
58 grid->addWidget(timeBox, row, 0);
59
60 subgrid = new QGridLayout;
61 timeBox->setLayout(subgrid);
62
63 ++row;
64 }
65
66 if (options & ShowTime) {
67
68 subgrid->addWidget(new QLabel(tr("Time:")), subrow, 0);
69
70 m_frameTimeSpinBox = new QSpinBox;
71 m_frameTimeSpinBox->setMaximum(INT_MAX);
72 m_frameTimeSpinBox->setSuffix(tr(" frames"));
73 subgrid->addWidget(m_frameTimeSpinBox, subrow, 1, 1, 2);
74 connect(m_frameTimeSpinBox, SIGNAL(valueChanged(int)),
75 this, SLOT(frameTimeChanged(int)));
76
77 ++subrow;
78
79 m_realTimeSecsSpinBox = new QSpinBox;
80 m_realTimeSecsSpinBox->setMaximum(999999);
81 m_realTimeSecsSpinBox->setSuffix(tr(" sec"));
82 subgrid->addWidget(m_realTimeSecsSpinBox, subrow, 1);
83 connect(m_realTimeSecsSpinBox, SIGNAL(valueChanged(int)),
84 this, SLOT(realTimeSecsChanged(int)));
85
86 m_realTimeUSecsSpinBox = new QSpinBox;
87 m_realTimeUSecsSpinBox->setMaximum(999999);
88 m_realTimeUSecsSpinBox->setSuffix(tr(" usec"));
89 m_realTimeUSecsSpinBox->setSingleStep(singleStep);
90 subgrid->addWidget(m_realTimeUSecsSpinBox, subrow, 2);
91 connect(m_realTimeUSecsSpinBox, SIGNAL(valueChanged(int)),
92 this, SLOT(realTimeUSecsChanged(int)));
93
94 ++subrow;
95 }
96
97 if (options & ShowDuration) {
98
99 subgrid->addWidget(new QLabel(tr("Duration:")), subrow, 0);
100
101 m_frameDurationSpinBox = new QSpinBox;
102 m_frameDurationSpinBox->setMaximum(INT_MAX);
103 m_frameDurationSpinBox->setSuffix(tr(" frames"));
104 subgrid->addWidget(m_frameDurationSpinBox, subrow, 1, 1, 2);
105 connect(m_frameDurationSpinBox, SIGNAL(valueChanged(int)),
106 this, SLOT(frameDurationChanged(int)));
107
108 ++subrow;
109
110 m_realDurationSecsSpinBox = new QSpinBox;
111 m_realDurationSecsSpinBox->setMaximum(999999);
112 m_realDurationSecsSpinBox->setSuffix(tr(" sec"));
113 subgrid->addWidget(m_realDurationSecsSpinBox, subrow, 1);
114 connect(m_realDurationSecsSpinBox, SIGNAL(valueChanged(int)),
115 this, SLOT(realDurationSecsChanged(int)));
116
117 m_realDurationUSecsSpinBox = new QSpinBox;
118 m_realDurationUSecsSpinBox->setMaximum(999999);
119 m_realDurationUSecsSpinBox->setSuffix(tr(" usec"));
120 m_realDurationUSecsSpinBox->setSingleStep(singleStep);
121 subgrid->addWidget(m_realDurationUSecsSpinBox, subrow, 2);
122 connect(m_realDurationUSecsSpinBox, SIGNAL(valueChanged(int)),
123 this, SLOT(realDurationUSecsChanged(int)));
124
125 ++subrow;
126 }
127
128 if ((options & ShowValue) || (options & ShowText)) {
129
130 valueBox = new QGroupBox;
131 valueBox->setTitle(tr("Properties"));
132 grid->addWidget(valueBox, row, 0);
133
134 subgrid = new QGridLayout;
135 valueBox->setLayout(subgrid);
136
137 ++row;
138 }
139
140 subrow = 0;
141
142 if (options & ShowValue) {
143
144 subgrid->addWidget(new QLabel(tr("Value:")), subrow, 0);
145
146 m_valueSpinBox = new QDoubleSpinBox;
147 m_valueSpinBox->setSuffix(QString(" %1").arg(valueUnits));
148 m_valueSpinBox->setDecimals(10);
149 m_valueSpinBox->setMinimum(-1e100);
150 m_valueSpinBox->setMaximum(1e100);
151 connect(m_valueSpinBox, SIGNAL(valueChanged(double)),
152 this, SLOT(valueChanged(double)));
153 subgrid->addWidget(m_valueSpinBox, subrow, 1);
154
155 ++subrow;
156 }
157
158 if (options & ShowText) {
159
160 subgrid->addWidget(new QLabel(tr("Text:")), subrow, 0);
161
162 m_textField = new QLineEdit;
163 connect(m_textField, SIGNAL(textChanged(QString)),
164 this, SLOT(textChanged(QString)));
165 subgrid->addWidget(m_textField, subrow, 1);
166
167 ++subrow;
168 }
169
170 if (options & ShowText) {
171 m_textField->setFocus(Qt::OtherFocusReason);
172 } else if (options & ShowValue) {
173 m_valueSpinBox->setFocus(Qt::OtherFocusReason);
174 }
175
176 QHBoxLayout *hbox = new QHBoxLayout;
177 grid->addLayout(hbox, row, 0, 1, 2);
178
179 QPushButton *ok = new QPushButton(tr("OK"));
180 m_resetButton = new QPushButton(tr("Reset"));
181 QPushButton *cancel = new QPushButton(tr("Cancel"));
182 hbox->addStretch(10);
183 hbox->addWidget(ok);
184 hbox->addWidget(m_resetButton);
185 hbox->addWidget(cancel);
186 ok->setDefault(true);
187 connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
188 connect(m_resetButton, SIGNAL(clicked()), this, SLOT(reset()));
189 connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
190 m_resetButton->setEnabled(false);
191 }
192
193 void
194 ItemEditDialog::setFrameTime(long frame)
195 {
196 if (!m_frameTimeSpinBox) return;
197
198 RealTime rt(RealTime::frame2RealTime(frame, m_sampleRate));
199 m_realTimeSecsSpinBox->setValue(rt.sec);
200 m_realTimeUSecsSpinBox->setValue(rt.usec());
201 m_frameTimeSpinBox->setValue(frame);
202 m_defaultFrame = frame;
203 m_resetButton->setEnabled(false);
204 }
205
206 long
207 ItemEditDialog::getFrameTime() const
208 {
209 return m_frameTimeSpinBox->value();
210 }
211
212 void
213 ItemEditDialog::setRealTime(RealTime rt)
214 {
215 setFrameTime(RealTime::realTime2Frame(rt, m_sampleRate));
216 }
217
218 RealTime
219 ItemEditDialog::getRealTime() const
220 {
221 return RealTime::frame2RealTime(getFrameTime(), m_sampleRate);
222 }
223
224 void
225 ItemEditDialog::setFrameDuration(long duration)
226 {
227 if (!m_frameDurationSpinBox) return;
228
229 RealTime rt(RealTime::frame2RealTime(duration, m_sampleRate));
230 m_realDurationSecsSpinBox->setValue(rt.sec);
231 m_realDurationUSecsSpinBox->setValue(rt.usec());
232 m_frameDurationSpinBox->setValue(duration);
233 m_defaultDuration = duration;
234 m_resetButton->setEnabled(false);
235 }
236
237 long
238 ItemEditDialog::getFrameDuration() const
239 {
240 return m_frameDurationSpinBox->value();
241 }
242
243 void
244 ItemEditDialog::setRealDuration(RealTime rt)
245 {
246 setFrameDuration(RealTime::realTime2Frame(rt, m_sampleRate));
247 }
248
249 RealTime
250 ItemEditDialog::getRealDuration() const
251 {
252 return RealTime::frame2RealTime(getFrameDuration(), m_sampleRate);
253 }
254
255 void
256 ItemEditDialog::setValue(float v)
257 {
258 if (!m_valueSpinBox) return;
259
260 m_valueSpinBox->setValue(v);
261 m_defaultValue = v;
262 m_resetButton->setEnabled(false);
263 }
264
265 float
266 ItemEditDialog::getValue() const
267 {
268 return m_valueSpinBox->value();
269 }
270
271 void
272 ItemEditDialog::setText(QString text)
273 {
274 if (!m_textField) return;
275
276 m_textField->setText(text);
277 m_defaultText = text;
278 m_resetButton->setEnabled(false);
279 }
280
281 QString
282 ItemEditDialog::getText() const
283 {
284 return m_textField->text();
285 }
286
287 void
288 ItemEditDialog::frameTimeChanged(int i)
289 {
290 m_realTimeSecsSpinBox->blockSignals(true);
291 m_realTimeUSecsSpinBox->blockSignals(true);
292
293 RealTime rt(RealTime::frame2RealTime(i, m_sampleRate));
294 m_realTimeSecsSpinBox->setValue(rt.sec);
295 m_realTimeUSecsSpinBox->setValue(rt.usec());
296
297 m_realTimeSecsSpinBox->blockSignals(false);
298 m_realTimeUSecsSpinBox->blockSignals(false);
299 m_resetButton->setEnabled(true);
300 }
301
302 void
303 ItemEditDialog::realTimeSecsChanged(int i)
304 {
305 RealTime rt = getRealTime();
306 rt.sec = i;
307 size_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
308 m_frameTimeSpinBox->setValue(frame);
309 m_resetButton->setEnabled(true);
310 }
311
312 void
313 ItemEditDialog::realTimeUSecsChanged(int i)
314 {
315 RealTime rt = getRealTime();
316 rt.nsec = i * 1000;
317 size_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
318 m_frameTimeSpinBox->setValue(frame);
319 m_resetButton->setEnabled(true);
320 }
321
322 void
323 ItemEditDialog::frameDurationChanged(int i)
324 {
325 m_realDurationSecsSpinBox->blockSignals(true);
326 m_realDurationUSecsSpinBox->blockSignals(true);
327
328 RealTime rt(RealTime::frame2RealTime(i, m_sampleRate));
329 m_realDurationSecsSpinBox->setValue(rt.sec);
330 m_realDurationUSecsSpinBox->setValue(rt.usec());
331
332 m_realDurationSecsSpinBox->blockSignals(false);
333 m_realDurationUSecsSpinBox->blockSignals(false);
334 m_resetButton->setEnabled(true);
335 }
336
337 void
338 ItemEditDialog::realDurationSecsChanged(int i)
339 {
340 RealTime rt = getRealDuration();
341 rt.sec = i;
342 size_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
343 m_frameDurationSpinBox->setValue(frame);
344 m_resetButton->setEnabled(true);
345 }
346
347 void
348 ItemEditDialog::realDurationUSecsChanged(int i)
349 {
350 RealTime rt = getRealDuration();
351 rt.nsec = i * 1000;
352 size_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
353 m_frameDurationSpinBox->setValue(frame);
354 m_resetButton->setEnabled(true);
355 }
356
357 void
358 ItemEditDialog::valueChanged(double)
359 {
360 m_resetButton->setEnabled(true);
361 }
362
363 void
364 ItemEditDialog::textChanged(QString)
365 {
366 m_resetButton->setEnabled(true);
367 }
368
369 void
370 ItemEditDialog::reset()
371 {
372 setFrameTime(m_defaultFrame);
373 setFrameDuration(m_defaultDuration);
374 setValue(m_defaultValue);
375 setText(m_defaultText);
376 m_resetButton->setEnabled(false);
377 }
378