svgui  1.9
ItemEditDialog.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "ItemEditDialog.h"
00017 
00018 #include <QLineEdit>
00019 #include <QDoubleSpinBox>
00020 #include <QSpinBox>
00021 #include <QGridLayout>
00022 #include <QHBoxLayout>
00023 #include <QLabel>
00024 #include <QPushButton>
00025 #include <QGroupBox>
00026 #include <QDialogButtonBox>
00027 
00028 #include <float.h> // for FLT_MIN/MAX
00029 
00030 
00031 ItemEditDialog::ItemEditDialog(int sampleRate, int options,
00032                                QString valueUnits, QWidget *parent) :
00033     QDialog(parent),
00034     m_sampleRate(sampleRate),
00035     m_defaultFrame(0),
00036     m_defaultDuration(0),
00037     m_defaultValue(0),
00038     m_frameTimeSpinBox(0),
00039     m_realTimeSecsSpinBox(0),
00040     m_realTimeUSecsSpinBox(0),
00041     m_frameDurationSpinBox(0),
00042     m_realDurationSecsSpinBox(0),
00043     m_realDurationUSecsSpinBox(0),
00044     m_valueSpinBox(0),
00045     m_textField(0)
00046 {
00047     QGridLayout *grid = new QGridLayout;
00048     setLayout(grid);
00049 
00050     QGroupBox *timeBox = 0;
00051     QGroupBox *valueBox = 0;
00052     QGridLayout *subgrid = 0;
00053 
00054     int row = 0, subrow = 0;
00055 
00056     int singleStep = RealTime::frame2RealTime(2, sampleRate).usec() - 1;
00057 
00058     if ((options & ShowTime) || (options & ShowDuration)) {
00059 
00060         timeBox = new QGroupBox;
00061         timeBox->setTitle(tr("Timing"));
00062         grid->addWidget(timeBox, row, 0);
00063 
00064         subgrid = new QGridLayout;
00065         timeBox->setLayout(subgrid);
00066 
00067         ++row;
00068     }
00069 
00070     if (options & ShowTime) {
00071 
00072         subgrid->addWidget(new QLabel(tr("Time:")), subrow, 0);
00073 
00074         m_frameTimeSpinBox = new QSpinBox;
00075         m_frameTimeSpinBox->setMaximum(INT_MAX);
00076         m_frameTimeSpinBox->setSuffix(tr(" frames"));
00077         subgrid->addWidget(m_frameTimeSpinBox, subrow, 1, 1, 2);
00078         connect(m_frameTimeSpinBox, SIGNAL(valueChanged(int)),
00079                 this, SLOT(frameTimeChanged(int)));
00080 
00081         ++subrow;
00082 
00083         m_realTimeSecsSpinBox = new QSpinBox;
00084         m_realTimeSecsSpinBox->setMaximum(999999);
00085         m_realTimeSecsSpinBox->setSuffix(tr(" sec"));
00086         subgrid->addWidget(m_realTimeSecsSpinBox, subrow, 1);
00087         connect(m_realTimeSecsSpinBox, SIGNAL(valueChanged(int)),
00088                 this, SLOT(realTimeSecsChanged(int)));
00089 
00090         m_realTimeUSecsSpinBox = new QSpinBox;
00091         m_realTimeUSecsSpinBox->setMaximum(999999);
00092         m_realTimeUSecsSpinBox->setSuffix(tr(" usec"));
00093         m_realTimeUSecsSpinBox->setSingleStep(singleStep);
00094         subgrid->addWidget(m_realTimeUSecsSpinBox, subrow, 2);
00095         connect(m_realTimeUSecsSpinBox, SIGNAL(valueChanged(int)),
00096                 this, SLOT(realTimeUSecsChanged(int)));
00097 
00098         ++subrow;
00099     }
00100 
00101     if (options & ShowDuration) {
00102 
00103         subgrid->addWidget(new QLabel(tr("Duration:")), subrow, 0);
00104 
00105         m_frameDurationSpinBox = new QSpinBox;
00106         m_frameDurationSpinBox->setMaximum(INT_MAX);
00107         m_frameDurationSpinBox->setSuffix(tr(" frames"));
00108         subgrid->addWidget(m_frameDurationSpinBox, subrow, 1, 1, 2);
00109         connect(m_frameDurationSpinBox, SIGNAL(valueChanged(int)),
00110                 this, SLOT(frameDurationChanged(int)));
00111 
00112         ++subrow;
00113 
00114         m_realDurationSecsSpinBox = new QSpinBox;
00115         m_realDurationSecsSpinBox->setMaximum(999999);
00116         m_realDurationSecsSpinBox->setSuffix(tr(" sec"));
00117         subgrid->addWidget(m_realDurationSecsSpinBox, subrow, 1);
00118         connect(m_realDurationSecsSpinBox, SIGNAL(valueChanged(int)),
00119                 this, SLOT(realDurationSecsChanged(int)));
00120 
00121         m_realDurationUSecsSpinBox = new QSpinBox;
00122         m_realDurationUSecsSpinBox->setMaximum(999999);
00123         m_realDurationUSecsSpinBox->setSuffix(tr(" usec"));
00124         m_realDurationUSecsSpinBox->setSingleStep(singleStep);
00125         subgrid->addWidget(m_realDurationUSecsSpinBox, subrow, 2);
00126         connect(m_realDurationUSecsSpinBox, SIGNAL(valueChanged(int)),
00127                 this, SLOT(realDurationUSecsChanged(int)));
00128 
00129         ++subrow;
00130     }
00131 
00132     if ((options & ShowValue) || (options & ShowText)) {
00133 
00134         valueBox = new QGroupBox;
00135         valueBox->setTitle(tr("Properties"));
00136         grid->addWidget(valueBox, row, 0);
00137 
00138         subgrid = new QGridLayout;
00139         valueBox->setLayout(subgrid);
00140 
00141         ++row;
00142     }
00143 
00144     subrow = 0;
00145 
00146     if (options & ShowValue) {
00147         
00148         subgrid->addWidget(new QLabel(tr("Value:")), subrow, 0);
00149 
00150         m_valueSpinBox = new QDoubleSpinBox;
00151         m_valueSpinBox->setSuffix(QString(" %1").arg(valueUnits));
00152         m_valueSpinBox->setDecimals(10);
00153         m_valueSpinBox->setMinimum(-1e10);
00154         m_valueSpinBox->setMaximum(1e10);
00155         connect(m_valueSpinBox, SIGNAL(valueChanged(double)),
00156                 this, SLOT(valueChanged(double)));
00157         subgrid->addWidget(m_valueSpinBox, subrow, 1);
00158 
00159         ++subrow;
00160     }
00161 
00162     if (options & ShowText) {
00163         
00164         subgrid->addWidget(new QLabel(tr("Text:")), subrow, 0);
00165 
00166         m_textField = new QLineEdit;
00167         connect(m_textField, SIGNAL(textChanged(QString)),
00168                 this, SLOT(textChanged(QString)));
00169         subgrid->addWidget(m_textField, subrow, 1);
00170 
00171         ++subrow;
00172     }
00173 
00174     if (options & ShowText) {
00175         m_textField->setFocus(Qt::OtherFocusReason);
00176     } else if (options & ShowValue) {
00177         m_valueSpinBox->setFocus(Qt::OtherFocusReason);
00178     }
00179     
00180     QDialogButtonBox *bb = new QDialogButtonBox(Qt::Horizontal);
00181     grid->addWidget(bb, row, 0, 1, 2);
00182     
00183     QPushButton *ok = new QPushButton(tr("OK"));
00184     m_resetButton = new QPushButton(tr("Reset"));
00185     QPushButton *cancel = new QPushButton(tr("Cancel"));
00186     bb->addButton(ok, QDialogButtonBox::AcceptRole);
00187     bb->addButton(m_resetButton, QDialogButtonBox::ResetRole);
00188     bb->addButton(cancel, QDialogButtonBox::RejectRole);
00189     connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
00190     connect(m_resetButton, SIGNAL(clicked()), this, SLOT(reset()));
00191     connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
00192     m_resetButton->setEnabled(false);
00193 }
00194 
00195 void
00196 ItemEditDialog::setFrameTime(int frame)
00197 {
00198     if (!m_frameTimeSpinBox) return;
00199 
00200     RealTime rt(RealTime::frame2RealTime(frame, m_sampleRate));
00201     m_realTimeSecsSpinBox->setValue(rt.sec);
00202     m_realTimeUSecsSpinBox->setValue(rt.usec());
00203     m_frameTimeSpinBox->setValue(frame);
00204     m_defaultFrame = frame;
00205     m_resetButton->setEnabled(false);
00206 }
00207 
00208 int
00209 ItemEditDialog::getFrameTime() const
00210 {
00211     return m_frameTimeSpinBox->value();
00212 }
00213 
00214 void
00215 ItemEditDialog::setRealTime(RealTime rt)
00216 {
00217     setFrameTime(RealTime::realTime2Frame(rt, m_sampleRate));
00218 }
00219 
00220 RealTime
00221 ItemEditDialog::getRealTime() const
00222 {
00223     return RealTime::frame2RealTime(getFrameTime(), m_sampleRate);
00224 }
00225 
00226 void
00227 ItemEditDialog::setFrameDuration(int duration)
00228 {
00229     if (!m_frameDurationSpinBox) return;
00230 
00231     RealTime rt(RealTime::frame2RealTime(duration, m_sampleRate));
00232     m_realDurationSecsSpinBox->setValue(rt.sec);
00233     m_realDurationUSecsSpinBox->setValue(rt.usec());
00234     m_frameDurationSpinBox->setValue(duration);
00235     m_defaultDuration = duration;
00236     m_resetButton->setEnabled(false);
00237 }
00238 
00239 int
00240 ItemEditDialog::getFrameDuration() const
00241 {
00242     return m_frameDurationSpinBox->value();
00243 }
00244 
00245 void
00246 ItemEditDialog::setRealDuration(RealTime rt)
00247 {
00248     setFrameDuration(RealTime::realTime2Frame(rt, m_sampleRate));
00249 }
00250 
00251 RealTime
00252 ItemEditDialog::getRealDuration() const
00253 {
00254     return RealTime::frame2RealTime(getFrameDuration(), m_sampleRate);
00255 }
00256 
00257 void
00258 ItemEditDialog::setValue(float v)
00259 {
00260     if (!m_valueSpinBox) return;
00261 
00262     m_valueSpinBox->setValue(v);
00263     m_defaultValue = v;
00264     m_resetButton->setEnabled(false);
00265 }
00266 
00267 float
00268 ItemEditDialog::getValue() const
00269 {
00270     return m_valueSpinBox->value();
00271 }
00272 
00273 void
00274 ItemEditDialog::setText(QString text)
00275 {
00276     if (!m_textField) return;
00277 
00278     m_textField->setText(text);
00279     m_defaultText = text;
00280     m_resetButton->setEnabled(false);
00281 }
00282 
00283 QString
00284 ItemEditDialog::getText() const
00285 {
00286     return m_textField->text();
00287 }
00288 
00289 void
00290 ItemEditDialog::frameTimeChanged(int i)
00291 {
00292     m_realTimeSecsSpinBox->blockSignals(true);
00293     m_realTimeUSecsSpinBox->blockSignals(true);
00294 
00295     RealTime rt(RealTime::frame2RealTime(i, m_sampleRate));
00296     m_realTimeSecsSpinBox->setValue(rt.sec);
00297     m_realTimeUSecsSpinBox->setValue(rt.usec());
00298 
00299     m_realTimeSecsSpinBox->blockSignals(false);
00300     m_realTimeUSecsSpinBox->blockSignals(false);
00301     m_resetButton->setEnabled(true);
00302 }
00303 
00304 void
00305 ItemEditDialog::realTimeSecsChanged(int i)
00306 {
00307     RealTime rt = getRealTime();
00308     rt.sec = i;
00309     int frame = RealTime::realTime2Frame(rt, m_sampleRate);
00310     m_frameTimeSpinBox->setValue(frame);
00311     m_resetButton->setEnabled(true);
00312 }
00313 
00314 void
00315 ItemEditDialog::realTimeUSecsChanged(int i)
00316 {
00317     RealTime rt = getRealTime();
00318     rt.nsec = i * 1000;
00319     int frame = RealTime::realTime2Frame(rt, m_sampleRate);
00320     m_frameTimeSpinBox->setValue(frame);
00321     m_resetButton->setEnabled(true);
00322 }
00323 
00324 void
00325 ItemEditDialog::frameDurationChanged(int i)
00326 {
00327     m_realDurationSecsSpinBox->blockSignals(true);
00328     m_realDurationUSecsSpinBox->blockSignals(true);
00329 
00330     RealTime rt(RealTime::frame2RealTime(i, m_sampleRate));
00331     m_realDurationSecsSpinBox->setValue(rt.sec);
00332     m_realDurationUSecsSpinBox->setValue(rt.usec());
00333 
00334     m_realDurationSecsSpinBox->blockSignals(false);
00335     m_realDurationUSecsSpinBox->blockSignals(false);
00336     m_resetButton->setEnabled(true);
00337 }
00338 
00339 void
00340 ItemEditDialog::realDurationSecsChanged(int i)
00341 {
00342     RealTime rt = getRealDuration();
00343     rt.sec = i;
00344     int frame = RealTime::realTime2Frame(rt, m_sampleRate);
00345     m_frameDurationSpinBox->setValue(frame);
00346     m_resetButton->setEnabled(true);
00347 }
00348 
00349 void
00350 ItemEditDialog::realDurationUSecsChanged(int i)
00351 {
00352     RealTime rt = getRealDuration();
00353     rt.nsec = i * 1000;
00354     int frame = RealTime::realTime2Frame(rt, m_sampleRate);
00355     m_frameDurationSpinBox->setValue(frame);
00356     m_resetButton->setEnabled(true);
00357 }
00358 
00359 void
00360 ItemEditDialog::valueChanged(double)
00361 {
00362     m_resetButton->setEnabled(true);
00363 }
00364 
00365 void
00366 ItemEditDialog::textChanged(QString)
00367 {
00368     m_resetButton->setEnabled(true);
00369 }
00370 
00371 void
00372 ItemEditDialog::reset()
00373 {
00374     setFrameTime(m_defaultFrame);
00375     setFrameDuration(m_defaultDuration);
00376     setValue(m_defaultValue);
00377     setText(m_defaultText);
00378     m_resetButton->setEnabled(false);
00379 }
00380