annotate widgets/ItemEditDialog.cpp @ 127:89c625dda204

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