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 
32  valueLabel(tr("Value")),
33  levelLabel(tr("Level"))
34 {
35 }
36 
37 ItemEditDialog::ItemEditDialog(sv_samplerate_t sampleRate, int options,
38  QString scaleUnits, QWidget *parent) :
39  ItemEditDialog(sampleRate, options,
40  [] (QString units) {
41  ItemEditDialog::LabelOptions labelOptions;
42  labelOptions.valueUnits = units;
43  return labelOptions;
44  }(scaleUnits),
45  parent) {};
46 
47 ItemEditDialog::ItemEditDialog(sv_samplerate_t sampleRate, int options,
48  LabelOptions labelOptions, QWidget *parent) :
49  QDialog(parent),
50  m_sampleRate(sampleRate),
51  m_defaultFrame(0),
53  m_defaultValue(0),
54  m_defaultLevel(0),
55  m_frameTimeSpinBox(nullptr),
56  m_realTimeSecsSpinBox(nullptr),
57  m_realTimeUSecsSpinBox(nullptr),
58  m_frameDurationSpinBox(nullptr),
61  m_valueSpinBox(nullptr),
62  m_levelSpinBox(nullptr),
63  m_textField(nullptr)
64 {
65  QGridLayout *grid = new QGridLayout;
66  setLayout(grid);
67 
68  QGroupBox *timeBox = nullptr;
69  QGroupBox *valueBox = nullptr;
70  QGridLayout *subgrid = nullptr;
71 
72  int row = 0, subrow = 0;
73 
74  int singleStep = RealTime::frame2RealTime(2, sampleRate).usec() - 1;
75 
76  if ((options & ShowTime) || (options & ShowDuration)) {
77 
78  timeBox = new QGroupBox;
79  timeBox->setTitle(tr("Timing"));
80  grid->addWidget(timeBox, row, 0);
81 
82  subgrid = new QGridLayout;
83  timeBox->setLayout(subgrid);
84 
85  ++row;
86  }
87 
88  if (options & ShowTime) {
89 
90  subgrid->addWidget(new QLabel(tr("Time:")), subrow, 0);
91 
92  m_frameTimeSpinBox = new QSpinBox;
93  m_frameTimeSpinBox->setMaximum(INT_MAX);
94  m_frameTimeSpinBox->setSuffix(tr(" frames"));
95  subgrid->addWidget(m_frameTimeSpinBox, subrow, 1, 1, 2);
96  connect(m_frameTimeSpinBox, SIGNAL(valueChanged(int)),
97  this, SLOT(frameTimeChanged(int)));
98 
99  ++subrow;
100 
101  m_realTimeSecsSpinBox = new QSpinBox;
102  m_realTimeSecsSpinBox->setMaximum(999999);
103  m_realTimeSecsSpinBox->setSuffix(tr(" sec"));
104  subgrid->addWidget(m_realTimeSecsSpinBox, subrow, 1);
105  connect(m_realTimeSecsSpinBox, SIGNAL(valueChanged(int)),
106  this, SLOT(realTimeSecsChanged(int)));
107 
108  m_realTimeUSecsSpinBox = new QSpinBox;
109  m_realTimeUSecsSpinBox->setMaximum(999999);
110  m_realTimeUSecsSpinBox->setSuffix(tr(" usec"));
111  m_realTimeUSecsSpinBox->setSingleStep(singleStep);
112  subgrid->addWidget(m_realTimeUSecsSpinBox, subrow, 2);
113  connect(m_realTimeUSecsSpinBox, SIGNAL(valueChanged(int)),
114  this, SLOT(realTimeUSecsChanged(int)));
115 
116  ++subrow;
117  }
118 
119  if (options & ShowDuration) {
120 
121  subgrid->addWidget(new QLabel(tr("Duration:")), subrow, 0);
122 
123  m_frameDurationSpinBox = new QSpinBox;
124  m_frameDurationSpinBox->setMaximum(INT_MAX);
125  m_frameDurationSpinBox->setSuffix(tr(" frames"));
126  subgrid->addWidget(m_frameDurationSpinBox, subrow, 1, 1, 2);
127  connect(m_frameDurationSpinBox, SIGNAL(valueChanged(int)),
128  this, SLOT(frameDurationChanged(int)));
129 
130  ++subrow;
131 
132  m_realDurationSecsSpinBox = new QSpinBox;
133  m_realDurationSecsSpinBox->setMaximum(999999);
134  m_realDurationSecsSpinBox->setSuffix(tr(" sec"));
135  subgrid->addWidget(m_realDurationSecsSpinBox, subrow, 1);
136  connect(m_realDurationSecsSpinBox, SIGNAL(valueChanged(int)),
137  this, SLOT(realDurationSecsChanged(int)));
138 
139  m_realDurationUSecsSpinBox = new QSpinBox;
140  m_realDurationUSecsSpinBox->setMaximum(999999);
141  m_realDurationUSecsSpinBox->setSuffix(tr(" usec"));
142  m_realDurationUSecsSpinBox->setSingleStep(singleStep);
143  subgrid->addWidget(m_realDurationUSecsSpinBox, subrow, 2);
144  connect(m_realDurationUSecsSpinBox, SIGNAL(valueChanged(int)),
145  this, SLOT(realDurationUSecsChanged(int)));
146 
147  ++subrow;
148  }
149 
150  if ((options & ShowValue) ||
151  (options & ShowLevel) ||
152  (options & ShowText)) {
153 
154  valueBox = new QGroupBox;
155  valueBox->setTitle(tr("Properties"));
156  grid->addWidget(valueBox, row, 0);
157 
158  subgrid = new QGridLayout;
159  valueBox->setLayout(subgrid);
160 
161  ++row;
162  }
163 
164  subrow = 0;
165 
166  if (options & ShowValue) {
167 
168  subgrid->addWidget(new QLabel(tr("%1:").arg(labelOptions.valueLabel)),
169  subrow, 0);
170 
171  m_valueSpinBox = new QDoubleSpinBox;
172  m_valueSpinBox->setSuffix(QString(" %1").arg(labelOptions.valueUnits));
173  m_valueSpinBox->setDecimals(10);
174  m_valueSpinBox->setMinimum(-1e10);
175  m_valueSpinBox->setMaximum(1e10);
176  connect(m_valueSpinBox, SIGNAL(valueChanged(double)),
177  this, SLOT(valueChanged(double)));
178  subgrid->addWidget(m_valueSpinBox, subrow, 1);
179 
180  ++subrow;
181  }
182 
183  if (options & ShowLevel) {
184 
185  subgrid->addWidget(new QLabel(tr("%1:").arg(labelOptions.levelLabel)),
186  subrow, 0);
187 
188  m_levelSpinBox = new QDoubleSpinBox;
189  m_levelSpinBox->setSuffix(QString(" %1").arg(labelOptions.levelUnits));
190  m_levelSpinBox->setDecimals(10);
191  m_levelSpinBox->setMinimum(-1e10);
192  m_levelSpinBox->setMaximum(1e10);
193  connect(m_levelSpinBox, SIGNAL(levelChanged(double)),
194  this, SLOT(levelChanged(double)));
195  subgrid->addWidget(m_levelSpinBox, subrow, 1);
196 
197  ++subrow;
198  }
199 
200  if (options & ShowText) {
201 
202  subgrid->addWidget(new QLabel(tr("Text:")), subrow, 0);
203 
204  m_textField = new QLineEdit;
205  connect(m_textField, SIGNAL(textChanged(QString)),
206  this, SLOT(textChanged(QString)));
207  subgrid->addWidget(m_textField, subrow, 1);
208 
209  ++subrow;
210  }
211 
212  if (options & ShowText) {
213  m_textField->setFocus(Qt::OtherFocusReason);
214  } else if (options & ShowValue) {
215  m_valueSpinBox->setFocus(Qt::OtherFocusReason);
216  } else if (options & ShowLevel) {
217  m_levelSpinBox->setFocus(Qt::OtherFocusReason);
218  }
219 
220  QDialogButtonBox *bb = new QDialogButtonBox(Qt::Horizontal);
221  grid->addWidget(bb, row, 0, 1, 2);
222 
223  QPushButton *ok = new QPushButton(tr("OK"));
224  m_resetButton = new QPushButton(tr("Reset"));
225  QPushButton *cancel = new QPushButton(tr("Cancel"));
226  bb->addButton(ok, QDialogButtonBox::AcceptRole);
227  bb->addButton(m_resetButton, QDialogButtonBox::ResetRole);
228  bb->addButton(cancel, QDialogButtonBox::RejectRole);
229  connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
230  connect(m_resetButton, SIGNAL(clicked()), this, SLOT(reset()));
231  connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
232  m_resetButton->setEnabled(false);
233 }
234 
235 void
237 {
238  if (!m_frameTimeSpinBox) return;
239 
240  RealTime rt(RealTime::frame2RealTime(frame, m_sampleRate));
241  m_realTimeSecsSpinBox->setValue(rt.sec);
242  m_realTimeUSecsSpinBox->setValue(rt.usec());
243  m_frameTimeSpinBox->setValue(int(frame));
244  m_defaultFrame = frame;
245  m_resetButton->setEnabled(false);
246 }
247 
248 sv_frame_t
250 {
251  return m_frameTimeSpinBox->value();
252 }
253 
254 void
256 {
257  setFrameTime(RealTime::realTime2Frame(rt, m_sampleRate));
258 }
259 
260 RealTime
262 {
263  return RealTime::frame2RealTime(getFrameTime(), m_sampleRate);
264 }
265 
266 void
268 {
269  if (!m_frameDurationSpinBox) return;
270 
271  RealTime rt(RealTime::frame2RealTime(duration, m_sampleRate));
272  m_realDurationSecsSpinBox->setValue(rt.sec);
273  m_realDurationUSecsSpinBox->setValue(rt.usec());
274  m_frameDurationSpinBox->setValue(int(duration));
275  m_defaultDuration = duration;
276  m_resetButton->setEnabled(false);
277 }
278 
279 sv_frame_t
281 {
282  return m_frameDurationSpinBox->value();
283 }
284 
285 void
287 {
288  setFrameDuration(RealTime::realTime2Frame(rt, m_sampleRate));
289 }
290 
291 RealTime
293 {
294  return RealTime::frame2RealTime(getFrameDuration(), m_sampleRate);
295 }
296 
297 void
299 {
300  if (!m_valueSpinBox) return;
301 
302  m_valueSpinBox->setValue(v);
303  m_defaultValue = v;
304  m_resetButton->setEnabled(false);
305 }
306 
307 float
309 {
310  return float(m_valueSpinBox->value());
311 }
312 
313 void
315 {
316  if (!m_levelSpinBox) return;
317 
318  m_levelSpinBox->setValue(v);
319  m_defaultLevel = v;
320  m_resetButton->setEnabled(false);
321 }
322 
323 float
325 {
326  return float(m_levelSpinBox->value());
327 }
328 
329 void
331 {
332  if (!m_textField) return;
333 
334  m_textField->setText(text);
335  m_defaultText = text;
336  m_resetButton->setEnabled(false);
337 }
338 
339 QString
341 {
342  return m_textField->text();
343 }
344 
345 void
347 {
348  m_realTimeSecsSpinBox->blockSignals(true);
349  m_realTimeUSecsSpinBox->blockSignals(true);
350 
351  RealTime rt(RealTime::frame2RealTime(i, m_sampleRate));
352  m_realTimeSecsSpinBox->setValue(rt.sec);
353  m_realTimeUSecsSpinBox->setValue(rt.usec());
354 
355  m_realTimeSecsSpinBox->blockSignals(false);
356  m_realTimeUSecsSpinBox->blockSignals(false);
357  m_resetButton->setEnabled(true);
358 }
359 
360 void
362 {
363  RealTime rt = getRealTime();
364  rt.sec = i;
365  sv_frame_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
366  m_frameTimeSpinBox->setValue(int(frame));
367  m_resetButton->setEnabled(true);
368 }
369 
370 void
372 {
373  RealTime rt = getRealTime();
374  rt.nsec = i * 1000;
375  sv_frame_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
376  m_frameTimeSpinBox->setValue(int(frame));
377  m_resetButton->setEnabled(true);
378 }
379 
380 void
382 {
383  m_realDurationSecsSpinBox->blockSignals(true);
384  m_realDurationUSecsSpinBox->blockSignals(true);
385 
386  RealTime rt(RealTime::frame2RealTime(i, m_sampleRate));
387  m_realDurationSecsSpinBox->setValue(rt.sec);
388  m_realDurationUSecsSpinBox->setValue(rt.usec());
389 
390  m_realDurationSecsSpinBox->blockSignals(false);
391  m_realDurationUSecsSpinBox->blockSignals(false);
392  m_resetButton->setEnabled(true);
393 }
394 
395 void
397 {
398  RealTime rt = getRealDuration();
399  rt.sec = i;
400  sv_frame_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
401  m_frameDurationSpinBox->setValue(int(frame));
402  m_resetButton->setEnabled(true);
403 }
404 
405 void
407 {
408  RealTime rt = getRealDuration();
409  rt.nsec = i * 1000;
410  sv_frame_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
411  m_frameDurationSpinBox->setValue(int(frame));
412  m_resetButton->setEnabled(true);
413 }
414 
415 void
417 {
418  m_resetButton->setEnabled(true);
419 }
420 
421 void
423 {
424  m_resetButton->setEnabled(true);
425 }
426 
427 void
429 {
430  m_resetButton->setEnabled(true);
431 }
432 
433 void
435 {
441  m_resetButton->setEnabled(false);
442 }
443 
RealTime getRealDuration() const
float getValue() const
QSpinBox * m_realDurationSecsSpinBox
QSpinBox * m_frameTimeSpinBox
void levelChanged(double)
void valueChanged(double)
void setFrameDuration(sv_frame_t frame)
sv_samplerate_t m_sampleRate
void frameDurationChanged(int)
void realDurationUSecsChanged(int)
QString m_defaultText
QSpinBox * m_realTimeUSecsSpinBox
sv_frame_t m_defaultDuration
QSpinBox * m_realTimeSecsSpinBox
void setFrameTime(sv_frame_t frame)
sv_frame_t getFrameTime() const
void setText(QString text)
RealTime getRealTime() const
QLineEdit * m_textField
void setRealDuration(RealTime rt)
void realTimeUSecsChanged(int)
QDoubleSpinBox * m_valueSpinBox
QPushButton * m_resetButton
QSpinBox * m_frameDurationSpinBox
sv_frame_t m_defaultFrame
void realDurationSecsChanged(int)
ItemEditDialog(sv_samplerate_t sampleRate, int options, LabelOptions labelOptions={}, QWidget *parent=0)
void setRealTime(RealTime rt)
QString getText() const
void frameTimeChanged(int)
void textChanged(QString)
float getLevel() const
QSpinBox * m_realDurationUSecsSpinBox
QDoubleSpinBox * m_levelSpinBox
void setLevel(float level)
sv_frame_t getFrameDuration() const
void realTimeSecsChanged(int)
void setValue(float value)