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(size_t sampleRate, int options,
00032                                QString valueUnits, QWidget *parent) :
00033     QDialog(parent),
00034     m_sampleRate(sampleRate),
00035     m_frameTimeSpinBox(0),
00036     m_realTimeSecsSpinBox(0),
00037     m_realTimeUSecsSpinBox(0),
00038     m_frameDurationSpinBox(0),
00039     m_realDurationSecsSpinBox(0),
00040     m_realDurationUSecsSpinBox(0),
00041     m_valueSpinBox(0),
00042     m_textField(0)
00043 {
00044     QGridLayout *grid = new QGridLayout;
00045     setLayout(grid);
00046 
00047     QGroupBox *timeBox = 0;
00048     QGroupBox *valueBox = 0;
00049     QGridLayout *subgrid = 0;
00050 
00051     int row = 0, subrow = 0;
00052 
00053     size_t singleStep = RealTime::frame2RealTime(2, sampleRate).usec() - 1;
00054 
00055     if ((options & ShowTime) || (options & ShowDuration)) {
00056 
00057         timeBox = new QGroupBox;
00058         timeBox->setTitle(tr("Timing"));
00059         grid->addWidget(timeBox, row, 0);
00060 
00061         subgrid = new QGridLayout;
00062         timeBox->setLayout(subgrid);
00063 
00064         ++row;
00065     }
00066 
00067     if (options & ShowTime) {
00068 
00069         subgrid->addWidget(new QLabel(tr("Time:")), subrow, 0);
00070 
00071         m_frameTimeSpinBox = new QSpinBox;
00072         m_frameTimeSpinBox->setMaximum(INT_MAX);
00073         m_frameTimeSpinBox->setSuffix(tr(" frames"));
00074         subgrid->addWidget(m_frameTimeSpinBox, subrow, 1, 1, 2);
00075         connect(m_frameTimeSpinBox, SIGNAL(valueChanged(int)),
00076                 this, SLOT(frameTimeChanged(int)));
00077 
00078         ++subrow;
00079 
00080         m_realTimeSecsSpinBox = new QSpinBox;
00081         m_realTimeSecsSpinBox->setMaximum(999999);
00082         m_realTimeSecsSpinBox->setSuffix(tr(" sec"));
00083         subgrid->addWidget(m_realTimeSecsSpinBox, subrow, 1);
00084         connect(m_realTimeSecsSpinBox, SIGNAL(valueChanged(int)),
00085                 this, SLOT(realTimeSecsChanged(int)));
00086 
00087         m_realTimeUSecsSpinBox = new QSpinBox;
00088         m_realTimeUSecsSpinBox->setMaximum(999999);
00089         m_realTimeUSecsSpinBox->setSuffix(tr(" usec"));
00090         m_realTimeUSecsSpinBox->setSingleStep(singleStep);
00091         subgrid->addWidget(m_realTimeUSecsSpinBox, subrow, 2);
00092         connect(m_realTimeUSecsSpinBox, SIGNAL(valueChanged(int)),
00093                 this, SLOT(realTimeUSecsChanged(int)));
00094 
00095         ++subrow;
00096     }
00097 
00098     if (options & ShowDuration) {
00099 
00100         subgrid->addWidget(new QLabel(tr("Duration:")), subrow, 0);
00101 
00102         m_frameDurationSpinBox = new QSpinBox;
00103         m_frameDurationSpinBox->setMaximum(INT_MAX);
00104         m_frameDurationSpinBox->setSuffix(tr(" frames"));
00105         subgrid->addWidget(m_frameDurationSpinBox, subrow, 1, 1, 2);
00106         connect(m_frameDurationSpinBox, SIGNAL(valueChanged(int)),
00107                 this, SLOT(frameDurationChanged(int)));
00108 
00109         ++subrow;
00110 
00111         m_realDurationSecsSpinBox = new QSpinBox;
00112         m_realDurationSecsSpinBox->setMaximum(999999);
00113         m_realDurationSecsSpinBox->setSuffix(tr(" sec"));
00114         subgrid->addWidget(m_realDurationSecsSpinBox, subrow, 1);
00115         connect(m_realDurationSecsSpinBox, SIGNAL(valueChanged(int)),
00116                 this, SLOT(realDurationSecsChanged(int)));
00117 
00118         m_realDurationUSecsSpinBox = new QSpinBox;
00119         m_realDurationUSecsSpinBox->setMaximum(999999);
00120         m_realDurationUSecsSpinBox->setSuffix(tr(" usec"));
00121         m_realDurationUSecsSpinBox->setSingleStep(singleStep);
00122         subgrid->addWidget(m_realDurationUSecsSpinBox, subrow, 2);
00123         connect(m_realDurationUSecsSpinBox, SIGNAL(valueChanged(int)),
00124                 this, SLOT(realDurationUSecsChanged(int)));
00125 
00126         ++subrow;
00127     }
00128 
00129     if ((options & ShowValue) || (options & ShowText)) {
00130 
00131         valueBox = new QGroupBox;
00132         valueBox->setTitle(tr("Properties"));
00133         grid->addWidget(valueBox, row, 0);
00134 
00135         subgrid = new QGridLayout;
00136         valueBox->setLayout(subgrid);
00137 
00138         ++row;
00139     }
00140 
00141     subrow = 0;
00142 
00143     if (options & ShowValue) {
00144         
00145         subgrid->addWidget(new QLabel(tr("Value:")), subrow, 0);
00146 
00147         m_valueSpinBox = new QDoubleSpinBox;
00148         m_valueSpinBox->setSuffix(QString(" %1").arg(valueUnits));
00149         m_valueSpinBox->setDecimals(10);
00150         m_valueSpinBox->setMinimum(-1e100);
00151         m_valueSpinBox->setMaximum(1e100);
00152         connect(m_valueSpinBox, SIGNAL(valueChanged(double)),
00153                 this, SLOT(valueChanged(double)));
00154         subgrid->addWidget(m_valueSpinBox, subrow, 1);
00155 
00156         ++subrow;
00157     }
00158 
00159     if (options & ShowText) {
00160         
00161         subgrid->addWidget(new QLabel(tr("Text:")), subrow, 0);
00162 
00163         m_textField = new QLineEdit;
00164         connect(m_textField, SIGNAL(textChanged(QString)),
00165                 this, SLOT(textChanged(QString)));
00166         subgrid->addWidget(m_textField, subrow, 1);
00167 
00168         ++subrow;
00169     }
00170 
00171     if (options & ShowText) {
00172         m_textField->setFocus(Qt::OtherFocusReason);
00173     } else if (options & ShowValue) {
00174         m_valueSpinBox->setFocus(Qt::OtherFocusReason);
00175     }
00176     
00177     QDialogButtonBox *bb = new QDialogButtonBox(Qt::Horizontal);
00178     grid->addWidget(bb, row, 0, 1, 2);
00179     
00180     QPushButton *ok = new QPushButton(tr("OK"));
00181     m_resetButton = new QPushButton(tr("Reset"));
00182     QPushButton *cancel = new QPushButton(tr("Cancel"));
00183     bb->addButton(ok, QDialogButtonBox::AcceptRole);
00184     bb->addButton(m_resetButton, QDialogButtonBox::ResetRole);
00185     bb->addButton(cancel, QDialogButtonBox::RejectRole);
00186     connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
00187     connect(m_resetButton, SIGNAL(clicked()), this, SLOT(reset()));
00188     connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
00189     m_resetButton->setEnabled(false);
00190 }
00191 
00192 void
00193 ItemEditDialog::setFrameTime(long frame)
00194 {
00195     if (!m_frameTimeSpinBox) return;
00196 
00197     RealTime rt(RealTime::frame2RealTime(frame, m_sampleRate));
00198     m_realTimeSecsSpinBox->setValue(rt.sec);
00199     m_realTimeUSecsSpinBox->setValue(rt.usec());
00200     m_frameTimeSpinBox->setValue(frame);
00201     m_defaultFrame = frame;
00202     m_resetButton->setEnabled(false);
00203 }
00204 
00205 long
00206 ItemEditDialog::getFrameTime() const
00207 {
00208     return m_frameTimeSpinBox->value();
00209 }
00210 
00211 void
00212 ItemEditDialog::setRealTime(RealTime rt)
00213 {
00214     setFrameTime(RealTime::realTime2Frame(rt, m_sampleRate));
00215 }
00216 
00217 RealTime
00218 ItemEditDialog::getRealTime() const
00219 {
00220     return RealTime::frame2RealTime(getFrameTime(), m_sampleRate);
00221 }
00222 
00223 void
00224 ItemEditDialog::setFrameDuration(long duration)
00225 {
00226     if (!m_frameDurationSpinBox) return;
00227 
00228     RealTime rt(RealTime::frame2RealTime(duration, m_sampleRate));
00229     m_realDurationSecsSpinBox->setValue(rt.sec);
00230     m_realDurationUSecsSpinBox->setValue(rt.usec());
00231     m_frameDurationSpinBox->setValue(duration);
00232     m_defaultDuration = duration;
00233     m_resetButton->setEnabled(false);
00234 }
00235 
00236 long
00237 ItemEditDialog::getFrameDuration() const
00238 {
00239     return m_frameDurationSpinBox->value();
00240 }
00241 
00242 void
00243 ItemEditDialog::setRealDuration(RealTime rt)
00244 {
00245     setFrameDuration(RealTime::realTime2Frame(rt, m_sampleRate));
00246 }
00247 
00248 RealTime
00249 ItemEditDialog::getRealDuration() const
00250 {
00251     return RealTime::frame2RealTime(getFrameDuration(), m_sampleRate);
00252 }
00253 
00254 void
00255 ItemEditDialog::setValue(float v)
00256 {
00257     if (!m_valueSpinBox) return;
00258 
00259     m_valueSpinBox->setValue(v);
00260     m_defaultValue = v;
00261     m_resetButton->setEnabled(false);
00262 }
00263 
00264 float
00265 ItemEditDialog::getValue() const
00266 {
00267     return m_valueSpinBox->value();
00268 }
00269 
00270 void
00271 ItemEditDialog::setText(QString text)
00272 {
00273     if (!m_textField) return;
00274 
00275     m_textField->setText(text);
00276     m_defaultText = text;
00277     m_resetButton->setEnabled(false);
00278 }
00279 
00280 QString
00281 ItemEditDialog::getText() const
00282 {
00283     return m_textField->text();
00284 }
00285 
00286 void
00287 ItemEditDialog::frameTimeChanged(int i)
00288 {
00289     m_realTimeSecsSpinBox->blockSignals(true);
00290     m_realTimeUSecsSpinBox->blockSignals(true);
00291 
00292     RealTime rt(RealTime::frame2RealTime(i, m_sampleRate));
00293     m_realTimeSecsSpinBox->setValue(rt.sec);
00294     m_realTimeUSecsSpinBox->setValue(rt.usec());
00295 
00296     m_realTimeSecsSpinBox->blockSignals(false);
00297     m_realTimeUSecsSpinBox->blockSignals(false);
00298     m_resetButton->setEnabled(true);
00299 }
00300 
00301 void
00302 ItemEditDialog::realTimeSecsChanged(int i)
00303 {
00304     RealTime rt = getRealTime();
00305     rt.sec = i;
00306     size_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
00307     m_frameTimeSpinBox->setValue(frame);
00308     m_resetButton->setEnabled(true);
00309 }
00310 
00311 void
00312 ItemEditDialog::realTimeUSecsChanged(int i)
00313 {
00314     RealTime rt = getRealTime();
00315     rt.nsec = i * 1000;
00316     size_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
00317     m_frameTimeSpinBox->setValue(frame);
00318     m_resetButton->setEnabled(true);
00319 }
00320 
00321 void
00322 ItemEditDialog::frameDurationChanged(int i)
00323 {
00324     m_realDurationSecsSpinBox->blockSignals(true);
00325     m_realDurationUSecsSpinBox->blockSignals(true);
00326 
00327     RealTime rt(RealTime::frame2RealTime(i, m_sampleRate));
00328     m_realDurationSecsSpinBox->setValue(rt.sec);
00329     m_realDurationUSecsSpinBox->setValue(rt.usec());
00330 
00331     m_realDurationSecsSpinBox->blockSignals(false);
00332     m_realDurationUSecsSpinBox->blockSignals(false);
00333     m_resetButton->setEnabled(true);
00334 }
00335 
00336 void
00337 ItemEditDialog::realDurationSecsChanged(int i)
00338 {
00339     RealTime rt = getRealDuration();
00340     rt.sec = i;
00341     size_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
00342     m_frameDurationSpinBox->setValue(frame);
00343     m_resetButton->setEnabled(true);
00344 }
00345 
00346 void
00347 ItemEditDialog::realDurationUSecsChanged(int i)
00348 {
00349     RealTime rt = getRealDuration();
00350     rt.nsec = i * 1000;
00351     size_t frame = RealTime::realTime2Frame(rt, m_sampleRate);
00352     m_frameDurationSpinBox->setValue(frame);
00353     m_resetButton->setEnabled(true);
00354 }
00355 
00356 void
00357 ItemEditDialog::valueChanged(double)
00358 {
00359     m_resetButton->setEnabled(true);
00360 }
00361 
00362 void
00363 ItemEditDialog::textChanged(QString)
00364 {
00365     m_resetButton->setEnabled(true);
00366 }
00367 
00368 void
00369 ItemEditDialog::reset()
00370 {
00371     setFrameTime(m_defaultFrame);
00372     setFrameDuration(m_defaultDuration);
00373     setValue(m_defaultValue);
00374     setText(m_defaultText);
00375     m_resetButton->setEnabled(false);
00376 }
00377 

Generated on Wed Feb 20 15:45:26 2008 for SonicVisualiser by  doxygen 1.5.1