comparison widgets/ItemEditDialog.cpp @ 73:ad1fe715b480

* Make the item-edit dialog do something useful
author Chris Cannam
date Mon, 03 Apr 2006 17:18:27 +0000
parents bf306158803d
children 0f36cdf407a6
comparison
equal deleted inserted replaced
72:1d176af87056 73:ad1fe715b480
15 15
16 #include "ItemEditDialog.h" 16 #include "ItemEditDialog.h"
17 17
18 #include <QLineEdit> 18 #include <QLineEdit>
19 #include <QDoubleSpinBox> 19 #include <QDoubleSpinBox>
20 #include <QSpinBox>
20 #include <QGridLayout> 21 #include <QGridLayout>
21 #include <QHBoxLayout> 22 #include <QHBoxLayout>
22 #include <QLabel> 23 #include <QLabel>
23 #include <QPushButton> 24 #include <QPushButton>
24 25 #include <QGroupBox>
25 26
26 ItemEditDialog::ItemEditDialog(size_t sampleRate, int options, QWidget *parent) : 27 #include <float.h> // for FLT_MIN/MAX
28
29
30 ItemEditDialog::ItemEditDialog(size_t sampleRate, int options,
31 QString valueUnits, QWidget *parent) :
27 QDialog(parent), 32 QDialog(parent),
28 m_sampleRate(sampleRate), 33 m_sampleRate(sampleRate),
29 m_frame(0), 34 m_frameTimeSpinBox(0),
30 m_duration(0), 35 m_realTimeSecsSpinBox(0),
31 m_value(0.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)
32 { 42 {
33 QGridLayout *grid = new QGridLayout; 43 QGridLayout *grid = new QGridLayout;
34 setLayout(grid); 44 setLayout(grid);
35 45
36 int row = 0; 46 QGroupBox *timeBox = 0;
37 QLineEdit *line = 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 }
38 65
39 if (options & ShowTime) { 66 if (options & ShowTime) {
40 67
41 grid->addWidget(new QLabel(tr("Frame time:")), row, 0); 68 subgrid->addWidget(new QLabel(tr("Time:")), subrow, 0);
42 69
43 line = new QLineEdit; 70 m_frameTimeSpinBox = new QSpinBox;
44 line->setValidator(new QIntValidator(this)); 71 m_frameTimeSpinBox->setMaximum(INT_MAX);
45 grid->addWidget(line, row, 1); 72 m_frameTimeSpinBox->setSuffix(tr(" frames"));
46 connect(line, SIGNAL(textChanged(QString)), 73 subgrid->addWidget(m_frameTimeSpinBox, subrow, 1, 1, 2);
47 this, SLOT(frameTimeChanged(QString))); 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, 3);
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);
48 136
49 ++row; 137 ++row;
50 138 }
51 grid->addWidget(new QLabel(tr("Secs:")), row, 0); 139
52 140 subrow = 0;
53 line = new QLineEdit;
54 line->setValidator(new QIntValidator(this));
55 grid->addWidget(line, row, 1);
56 connect(line, SIGNAL(textChanged(QString)),
57 this, SLOT(realTimeSecsChanged(QString)));
58
59 ++row;
60
61 grid->addWidget(new QLabel(tr("Nsecs:")), row, 0);
62
63 line = new QLineEdit;
64 line->setValidator(new QIntValidator(this));
65 grid->addWidget(line, row, 1);
66 connect(line, SIGNAL(textChanged(QString)),
67 this, SLOT(realTimeSecsChanged(QString)));
68
69 ++row;
70 }
71
72 if (options & ShowDuration) {
73
74 grid->addWidget(new QLabel(tr("Frame duration:")), row, 0);
75
76 line = new QLineEdit;
77 line->setValidator(new QIntValidator(this));
78 grid->addWidget(line, row, 1);
79 connect(line, SIGNAL(textChanged(QString)),
80 this, SLOT(frameDurationChanged(QString)));
81
82 ++row;
83
84 grid->addWidget(new QLabel(tr("Secs:")), row, 0);
85
86 line = new QLineEdit;
87 line->setValidator(new QIntValidator(this));
88 grid->addWidget(line, row, 1);
89 connect(line, SIGNAL(textChanged(QString)),
90 this, SLOT(realDurationSecsChanged(QString)));
91
92 ++row;
93
94 grid->addWidget(new QLabel(tr("Nsecs:")), row, 0);
95
96 line = new QLineEdit;
97 line->setValidator(new QIntValidator(this));
98 grid->addWidget(line, row, 1);
99 connect(line, SIGNAL(textChanged(QString)),
100 this, SLOT(realDurationSecsChanged(QString)));
101
102 ++row;
103 }
104 141
105 if (options & ShowValue) { 142 if (options & ShowValue) {
106 143
107 grid->addWidget(new QLabel(tr("Value:")), row, 0); 144 subgrid->addWidget(new QLabel(tr("Value:")), subrow, 0);
108 145
109 QDoubleSpinBox *spinbox = new QDoubleSpinBox; 146 m_valueSpinBox = new QDoubleSpinBox;
110 grid->addWidget(spinbox, row, 1); 147 m_valueSpinBox->setSuffix(QString(" %1").arg(valueUnits));
111 connect(spinbox, SIGNAL(valueChanged(double)), 148 m_valueSpinBox->setDecimals(10);
149 m_valueSpinBox->setMinimum(-1e100);
150 m_valueSpinBox->setMaximum(1e100);
151 connect(m_valueSpinBox, SIGNAL(valueChanged(double)),
112 this, SLOT(valueChanged(double))); 152 this, SLOT(valueChanged(double)));
113 153 subgrid->addWidget(m_valueSpinBox, subrow, 1);
114 ++row; 154
155 ++subrow;
115 } 156 }
116 157
117 if (options & ShowText) { 158 if (options & ShowText) {
118 159
119 grid->addWidget(new QLabel(tr("Text:")), row, 0); 160 subgrid->addWidget(new QLabel(tr("Text:")), subrow, 0);
120 161
121 line = new QLineEdit; 162 m_textField = new QLineEdit;
122 grid->addWidget(line, row, 1); 163 connect(m_textField, SIGNAL(textChanged(QString)),
123 connect(line, SIGNAL(textChanged(QString)),
124 this, SLOT(textChanged(QString))); 164 this, SLOT(textChanged(QString)));
125 165 subgrid->addWidget(m_textField, subrow, 1);
126 ++row; 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);
127 } 174 }
128 175
129 QHBoxLayout *hbox = new QHBoxLayout; 176 QHBoxLayout *hbox = new QHBoxLayout;
130 grid->addLayout(hbox, row, 0, 1, 2); 177 grid->addLayout(hbox, row, 0, 1, 2);
131 178
132 QPushButton *ok = new QPushButton(tr("OK")); 179 QPushButton *ok = new QPushButton(tr("OK"));
180 m_resetButton = new QPushButton(tr("Reset"));
133 QPushButton *cancel = new QPushButton(tr("Cancel")); 181 QPushButton *cancel = new QPushButton(tr("Cancel"));
134 hbox->addStretch(10); 182 hbox->addStretch(10);
135 hbox->addWidget(ok); 183 hbox->addWidget(ok);
184 hbox->addWidget(m_resetButton);
136 hbox->addWidget(cancel); 185 hbox->addWidget(cancel);
186 ok->setDefault(true);
137 connect(ok, SIGNAL(clicked()), this, SLOT(accept())); 187 connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
188 connect(m_resetButton, SIGNAL(clicked()), this, SLOT(reset()));
138 connect(cancel, SIGNAL(clicked()), this, SLOT(reject())); 189 connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
190 m_resetButton->setEnabled(false);
139 } 191 }
140 192
141 void 193 void
142 ItemEditDialog::setFrameTime(long frame) 194 ItemEditDialog::setFrameTime(long frame)
143 { 195 {
144 m_frame = frame; 196 if (!m_frameTimeSpinBox) return;
145 //!!! 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);
146 } 204 }
147 205
148 long 206 long
149 ItemEditDialog::getFrameTime() const 207 ItemEditDialog::getFrameTime() const
150 { 208 {
151 return m_frame; 209 return m_frameTimeSpinBox->value();
152 } 210 }
153 211
154 void 212 void
155 ItemEditDialog::setRealTime(RealTime rt) 213 ItemEditDialog::setRealTime(RealTime rt)
156 { 214 {
157 m_frame = RealTime::realTime2Frame(rt, m_sampleRate); 215 setFrameTime(RealTime::realTime2Frame(rt, m_sampleRate));
158 //!!!
159 } 216 }
160 217
161 RealTime 218 RealTime
162 ItemEditDialog::getRealTime() const 219 ItemEditDialog::getRealTime() const
163 { 220 {
164 return RealTime::frame2RealTime(m_frame, m_sampleRate); 221 return RealTime::frame2RealTime(getFrameTime(), m_sampleRate);
165 } 222 }
166 223
167 void 224 void
168 ItemEditDialog::setFrameDuration(long duration) 225 ItemEditDialog::setFrameDuration(long duration)
169 { 226 {
170 m_duration = duration; 227 if (!m_frameDurationSpinBox) return;
171 //!!! 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);
172 } 235 }
173 236
174 long 237 long
175 ItemEditDialog::getFrameDuration() const 238 ItemEditDialog::getFrameDuration() const
176 { 239 {
177 return m_duration; 240 return m_frameDurationSpinBox->value();
178 } 241 }
179 242
180 void 243 void
181 ItemEditDialog::setRealDuration(RealTime rt) 244 ItemEditDialog::setRealDuration(RealTime rt)
182 { 245 {
183 m_duration = RealTime::realTime2Frame(rt, m_sampleRate); 246 setFrameDuration(RealTime::realTime2Frame(rt, m_sampleRate));
184 } 247 }
185 248
186 RealTime 249 RealTime
187 ItemEditDialog::getRealDuration() const 250 ItemEditDialog::getRealDuration() const
188 { 251 {
189 return RealTime::frame2RealTime(m_duration, m_sampleRate); 252 return RealTime::frame2RealTime(getFrameDuration(), m_sampleRate);
190 } 253 }
191 254
192 void 255 void
193 ItemEditDialog::setValue(float v) 256 ItemEditDialog::setValue(float v)
194 { 257 {
195 m_value = v; 258 if (!m_valueSpinBox) return;
196 //!!! 259
260 m_valueSpinBox->setValue(v);
261 m_defaultValue = v;
262 m_resetButton->setEnabled(false);
197 } 263 }
198 264
199 float 265 float
200 ItemEditDialog::getValue() const 266 ItemEditDialog::getValue() const
201 { 267 {
202 return m_value; 268 return m_valueSpinBox->value();
203 } 269 }
204 270
205 void 271 void
206 ItemEditDialog::setText(QString text) 272 ItemEditDialog::setText(QString text)
207 { 273 {
208 m_text = text; 274 if (!m_textField) return;
209 //!!! 275
276 m_textField->setText(text);
277 m_defaultText = text;
278 m_resetButton->setEnabled(false);
210 } 279 }
211 280
212 QString 281 QString
213 ItemEditDialog::getText() const 282 ItemEditDialog::getText() const
214 { 283 {
215 return m_text; 284 return m_textField->text();
216 } 285 }
217 286
218 void 287 void
219 ItemEditDialog::frameTimeChanged(QString s) 288 ItemEditDialog::frameTimeChanged(int i)
220 { 289 {
221 setFrameTime(s.toInt()); 290 m_realTimeSecsSpinBox->blockSignals(true);
222 } 291 m_realTimeUSecsSpinBox->blockSignals(true);
223 292
224 void 293 RealTime rt(RealTime::frame2RealTime(i, m_sampleRate));
225 ItemEditDialog::realTimeSecsChanged(QString s) 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)
226 { 304 {
227 RealTime rt = getRealTime(); 305 RealTime rt = getRealTime();
228 rt.sec = s.toInt(); 306 rt.sec = i;
229 setRealTime(rt); 307 size_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
230 } 308 m_frameTimeSpinBox->setValue(frame);
231 309 m_resetButton->setEnabled(true);
232 void 310 }
233 ItemEditDialog::realTimeNSecsChanged(QString s) 311
312 void
313 ItemEditDialog::realTimeUSecsChanged(int i)
234 { 314 {
235 RealTime rt = getRealTime(); 315 RealTime rt = getRealTime();
236 rt.nsec = s.toInt(); 316 rt.nsec = i * 1000;
237 setRealTime(rt); 317 size_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
238 } 318 m_frameTimeSpinBox->setValue(frame);
239 319 m_resetButton->setEnabled(true);
240 void 320 }
241 ItemEditDialog::frameDurationChanged(QString s) 321
242 { 322 void
243 setFrameDuration(s.toInt()); 323 ItemEditDialog::frameDurationChanged(int i)
244 } 324 {
245 325 m_realDurationSecsSpinBox->blockSignals(true);
246 void 326 m_realDurationUSecsSpinBox->blockSignals(true);
247 ItemEditDialog::realDurationSecsChanged(QString s) 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)
248 { 339 {
249 RealTime rt = getRealDuration(); 340 RealTime rt = getRealDuration();
250 rt.sec = s.toInt(); 341 rt.sec = i;
251 setRealDuration(rt); 342 size_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
252 } 343 m_frameDurationSpinBox->setValue(frame);
253 344 m_resetButton->setEnabled(true);
254 void 345 }
255 ItemEditDialog::realDurationNSecsChanged(QString s) 346
347 void
348 ItemEditDialog::realDurationUSecsChanged(int i)
256 { 349 {
257 RealTime rt = getRealDuration(); 350 RealTime rt = getRealDuration();
258 rt.nsec = s.toInt(); 351 rt.nsec = i * 1000;
259 setRealDuration(rt); 352 size_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
260 } 353 m_frameDurationSpinBox->setValue(frame);
261 354 m_resetButton->setEnabled(true);
262 void 355 }
263 ItemEditDialog::valueChanged(double v) 356
264 { 357 void
265 setValue((float)v); 358 ItemEditDialog::valueChanged(double)
266 } 359 {
267 360 m_resetButton->setEnabled(true);
268 void 361 }
269 ItemEditDialog::textChanged(QString text) 362
270 { 363 void
271 setText(text); 364 ItemEditDialog::textChanged(QString)
272 } 365 {
273 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