svgui  1.9
ItemEditDialog.cpp
Go to the documentation of this file.
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 #include <QDialogButtonBox>
27 
28 #include <float.h> // for FLT_MIN/MAX
29 
30 
31 ItemEditDialog::ItemEditDialog(int sampleRate, int options,
32  QString valueUnits, QWidget *parent) :
33  QDialog(parent),
34  m_sampleRate(sampleRate),
35  m_defaultFrame(0),
36  m_defaultDuration(0),
37  m_defaultValue(0),
38  m_frameTimeSpinBox(0),
39  m_realTimeSecsSpinBox(0),
40  m_realTimeUSecsSpinBox(0),
41  m_frameDurationSpinBox(0),
42  m_realDurationSecsSpinBox(0),
43  m_realDurationUSecsSpinBox(0),
44  m_valueSpinBox(0),
45  m_textField(0)
46 {
47  QGridLayout *grid = new QGridLayout;
48  setLayout(grid);
49 
50  QGroupBox *timeBox = 0;
51  QGroupBox *valueBox = 0;
52  QGridLayout *subgrid = 0;
53 
54  int row = 0, subrow = 0;
55 
56  int singleStep = RealTime::frame2RealTime(2, sampleRate).usec() - 1;
57 
58  if ((options & ShowTime) || (options & ShowDuration)) {
59 
60  timeBox = new QGroupBox;
61  timeBox->setTitle(tr("Timing"));
62  grid->addWidget(timeBox, row, 0);
63 
64  subgrid = new QGridLayout;
65  timeBox->setLayout(subgrid);
66 
67  ++row;
68  }
69 
70  if (options & ShowTime) {
71 
72  subgrid->addWidget(new QLabel(tr("Time:")), subrow, 0);
73 
74  m_frameTimeSpinBox = new QSpinBox;
75  m_frameTimeSpinBox->setMaximum(INT_MAX);
76  m_frameTimeSpinBox->setSuffix(tr(" frames"));
77  subgrid->addWidget(m_frameTimeSpinBox, subrow, 1, 1, 2);
78  connect(m_frameTimeSpinBox, SIGNAL(valueChanged(int)),
79  this, SLOT(frameTimeChanged(int)));
80 
81  ++subrow;
82 
83  m_realTimeSecsSpinBox = new QSpinBox;
84  m_realTimeSecsSpinBox->setMaximum(999999);
85  m_realTimeSecsSpinBox->setSuffix(tr(" sec"));
86  subgrid->addWidget(m_realTimeSecsSpinBox, subrow, 1);
87  connect(m_realTimeSecsSpinBox, SIGNAL(valueChanged(int)),
88  this, SLOT(realTimeSecsChanged(int)));
89 
90  m_realTimeUSecsSpinBox = new QSpinBox;
91  m_realTimeUSecsSpinBox->setMaximum(999999);
92  m_realTimeUSecsSpinBox->setSuffix(tr(" usec"));
93  m_realTimeUSecsSpinBox->setSingleStep(singleStep);
94  subgrid->addWidget(m_realTimeUSecsSpinBox, subrow, 2);
95  connect(m_realTimeUSecsSpinBox, SIGNAL(valueChanged(int)),
96  this, SLOT(realTimeUSecsChanged(int)));
97 
98  ++subrow;
99  }
100 
101  if (options & ShowDuration) {
102 
103  subgrid->addWidget(new QLabel(tr("Duration:")), subrow, 0);
104 
105  m_frameDurationSpinBox = new QSpinBox;
106  m_frameDurationSpinBox->setMaximum(INT_MAX);
107  m_frameDurationSpinBox->setSuffix(tr(" frames"));
108  subgrid->addWidget(m_frameDurationSpinBox, subrow, 1, 1, 2);
109  connect(m_frameDurationSpinBox, SIGNAL(valueChanged(int)),
110  this, SLOT(frameDurationChanged(int)));
111 
112  ++subrow;
113 
114  m_realDurationSecsSpinBox = new QSpinBox;
115  m_realDurationSecsSpinBox->setMaximum(999999);
116  m_realDurationSecsSpinBox->setSuffix(tr(" sec"));
117  subgrid->addWidget(m_realDurationSecsSpinBox, subrow, 1);
118  connect(m_realDurationSecsSpinBox, SIGNAL(valueChanged(int)),
119  this, SLOT(realDurationSecsChanged(int)));
120 
121  m_realDurationUSecsSpinBox = new QSpinBox;
122  m_realDurationUSecsSpinBox->setMaximum(999999);
123  m_realDurationUSecsSpinBox->setSuffix(tr(" usec"));
124  m_realDurationUSecsSpinBox->setSingleStep(singleStep);
125  subgrid->addWidget(m_realDurationUSecsSpinBox, subrow, 2);
126  connect(m_realDurationUSecsSpinBox, SIGNAL(valueChanged(int)),
127  this, SLOT(realDurationUSecsChanged(int)));
128 
129  ++subrow;
130  }
131 
132  if ((options & ShowValue) || (options & ShowText)) {
133 
134  valueBox = new QGroupBox;
135  valueBox->setTitle(tr("Properties"));
136  grid->addWidget(valueBox, row, 0);
137 
138  subgrid = new QGridLayout;
139  valueBox->setLayout(subgrid);
140 
141  ++row;
142  }
143 
144  subrow = 0;
145 
146  if (options & ShowValue) {
147 
148  subgrid->addWidget(new QLabel(tr("Value:")), subrow, 0);
149 
150  m_valueSpinBox = new QDoubleSpinBox;
151  m_valueSpinBox->setSuffix(QString(" %1").arg(valueUnits));
152  m_valueSpinBox->setDecimals(10);
153  m_valueSpinBox->setMinimum(-1e10);
154  m_valueSpinBox->setMaximum(1e10);
155  connect(m_valueSpinBox, SIGNAL(valueChanged(double)),
156  this, SLOT(valueChanged(double)));
157  subgrid->addWidget(m_valueSpinBox, subrow, 1);
158 
159  ++subrow;
160  }
161 
162  if (options & ShowText) {
163 
164  subgrid->addWidget(new QLabel(tr("Text:")), subrow, 0);
165 
166  m_textField = new QLineEdit;
167  connect(m_textField, SIGNAL(textChanged(QString)),
168  this, SLOT(textChanged(QString)));
169  subgrid->addWidget(m_textField, subrow, 1);
170 
171  ++subrow;
172  }
173 
174  if (options & ShowText) {
175  m_textField->setFocus(Qt::OtherFocusReason);
176  } else if (options & ShowValue) {
177  m_valueSpinBox->setFocus(Qt::OtherFocusReason);
178  }
179 
180  QDialogButtonBox *bb = new QDialogButtonBox(Qt::Horizontal);
181  grid->addWidget(bb, row, 0, 1, 2);
182 
183  QPushButton *ok = new QPushButton(tr("OK"));
184  m_resetButton = new QPushButton(tr("Reset"));
185  QPushButton *cancel = new QPushButton(tr("Cancel"));
186  bb->addButton(ok, QDialogButtonBox::AcceptRole);
187  bb->addButton(m_resetButton, QDialogButtonBox::ResetRole);
188  bb->addButton(cancel, QDialogButtonBox::RejectRole);
189  connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
190  connect(m_resetButton, SIGNAL(clicked()), this, SLOT(reset()));
191  connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
192  m_resetButton->setEnabled(false);
193 }
194 
195 void
197 {
198  if (!m_frameTimeSpinBox) return;
199 
200  RealTime rt(RealTime::frame2RealTime(frame, m_sampleRate));
201  m_realTimeSecsSpinBox->setValue(rt.sec);
202  m_realTimeUSecsSpinBox->setValue(rt.usec());
203  m_frameTimeSpinBox->setValue(frame);
204  m_defaultFrame = frame;
205  m_resetButton->setEnabled(false);
206 }
207 
208 int
210 {
211  return m_frameTimeSpinBox->value();
212 }
213 
214 void
216 {
217  setFrameTime(RealTime::realTime2Frame(rt, m_sampleRate));
218 }
219 
220 RealTime
222 {
223  return RealTime::frame2RealTime(getFrameTime(), m_sampleRate);
224 }
225 
226 void
228 {
229  if (!m_frameDurationSpinBox) return;
230 
231  RealTime rt(RealTime::frame2RealTime(duration, m_sampleRate));
232  m_realDurationSecsSpinBox->setValue(rt.sec);
233  m_realDurationUSecsSpinBox->setValue(rt.usec());
234  m_frameDurationSpinBox->setValue(duration);
235  m_defaultDuration = duration;
236  m_resetButton->setEnabled(false);
237 }
238 
239 int
241 {
242  return m_frameDurationSpinBox->value();
243 }
244 
245 void
247 {
248  setFrameDuration(RealTime::realTime2Frame(rt, m_sampleRate));
249 }
250 
251 RealTime
253 {
254  return RealTime::frame2RealTime(getFrameDuration(), m_sampleRate);
255 }
256 
257 void
259 {
260  if (!m_valueSpinBox) return;
261 
262  m_valueSpinBox->setValue(v);
263  m_defaultValue = v;
264  m_resetButton->setEnabled(false);
265 }
266 
267 float
269 {
270  return m_valueSpinBox->value();
271 }
272 
273 void
275 {
276  if (!m_textField) return;
277 
278  m_textField->setText(text);
279  m_defaultText = text;
280  m_resetButton->setEnabled(false);
281 }
282 
283 QString
285 {
286  return m_textField->text();
287 }
288 
289 void
291 {
292  m_realTimeSecsSpinBox->blockSignals(true);
293  m_realTimeUSecsSpinBox->blockSignals(true);
294 
295  RealTime rt(RealTime::frame2RealTime(i, m_sampleRate));
296  m_realTimeSecsSpinBox->setValue(rt.sec);
297  m_realTimeUSecsSpinBox->setValue(rt.usec());
298 
299  m_realTimeSecsSpinBox->blockSignals(false);
300  m_realTimeUSecsSpinBox->blockSignals(false);
301  m_resetButton->setEnabled(true);
302 }
303 
304 void
306 {
307  RealTime rt = getRealTime();
308  rt.sec = i;
309  int frame = RealTime::realTime2Frame(rt, m_sampleRate);
310  m_frameTimeSpinBox->setValue(frame);
311  m_resetButton->setEnabled(true);
312 }
313 
314 void
316 {
317  RealTime rt = getRealTime();
318  rt.nsec = i * 1000;
319  int frame = RealTime::realTime2Frame(rt, m_sampleRate);
320  m_frameTimeSpinBox->setValue(frame);
321  m_resetButton->setEnabled(true);
322 }
323 
324 void
326 {
327  m_realDurationSecsSpinBox->blockSignals(true);
328  m_realDurationUSecsSpinBox->blockSignals(true);
329 
330  RealTime rt(RealTime::frame2RealTime(i, m_sampleRate));
331  m_realDurationSecsSpinBox->setValue(rt.sec);
332  m_realDurationUSecsSpinBox->setValue(rt.usec());
333 
334  m_realDurationSecsSpinBox->blockSignals(false);
335  m_realDurationUSecsSpinBox->blockSignals(false);
336  m_resetButton->setEnabled(true);
337 }
338 
339 void
341 {
342  RealTime rt = getRealDuration();
343  rt.sec = i;
344  int frame = RealTime::realTime2Frame(rt, m_sampleRate);
345  m_frameDurationSpinBox->setValue(frame);
346  m_resetButton->setEnabled(true);
347 }
348 
349 void
351 {
352  RealTime rt = getRealDuration();
353  rt.nsec = i * 1000;
354  int frame = RealTime::realTime2Frame(rt, m_sampleRate);
355  m_frameDurationSpinBox->setValue(frame);
356  m_resetButton->setEnabled(true);
357 }
358 
359 void
361 {
362  m_resetButton->setEnabled(true);
363 }
364 
365 void
367 {
368  m_resetButton->setEnabled(true);
369 }
370 
371 void
373 {
378  m_resetButton->setEnabled(false);
379 }
380 
RealTime getRealDuration() const
ItemEditDialog(int sampleRate, int options, QString valueUnits="", QWidget *parent=0)
QSpinBox * m_realDurationSecsSpinBox
QSpinBox * m_frameTimeSpinBox
void valueChanged(double)
void setFrameTime(int frame)
void frameDurationChanged(int)
void realDurationUSecsChanged(int)
QString m_defaultText
QSpinBox * m_realTimeUSecsSpinBox
QSpinBox * m_realTimeSecsSpinBox
int getFrameTime() const
RealTime getRealTime() const
void setText(QString text)
QLineEdit * m_textField
void setRealDuration(RealTime rt)
void realTimeUSecsChanged(int)
QDoubleSpinBox * m_valueSpinBox
QPushButton * m_resetButton
QSpinBox * m_frameDurationSpinBox
void realDurationSecsChanged(int)
void setRealTime(RealTime rt)
void frameTimeChanged(int)
float getValue() const
void textChanged(QString)
QSpinBox * m_realDurationUSecsSpinBox
int getFrameDuration() const
void realTimeSecsChanged(int)
void setValue(float value)
void setFrameDuration(int frame)
QString getText() const