annotate widgets/ItemEditDialog.cpp @ 854:c17719e488c9

Fix some potential null-pointer derefs, and simplify some logic where loops were used with an unconditional "break" that meant they could only happen once (from coverity scan)
author Chris Cannam
date Wed, 03 Sep 2014 12:04:22 +0100
parents e4773943c9c1
children 44675ab217a3
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@282 26 #include <QDialogButtonBox>
Chris@70 27
Chris@73 28 #include <float.h> // for FLT_MIN/MAX
Chris@70 29
Chris@73 30
Chris@807 31 ItemEditDialog::ItemEditDialog(int sampleRate, int options,
Chris@73 32 QString valueUnits, QWidget *parent) :
Chris@70 33 QDialog(parent),
Chris@70 34 m_sampleRate(sampleRate),
Chris@73 35 m_frameTimeSpinBox(0),
Chris@73 36 m_realTimeSecsSpinBox(0),
Chris@73 37 m_realTimeUSecsSpinBox(0),
Chris@73 38 m_frameDurationSpinBox(0),
Chris@73 39 m_realDurationSecsSpinBox(0),
Chris@73 40 m_realDurationUSecsSpinBox(0),
Chris@73 41 m_valueSpinBox(0),
Chris@73 42 m_textField(0)
Chris@70 43 {
Chris@70 44 QGridLayout *grid = new QGridLayout;
Chris@70 45 setLayout(grid);
Chris@70 46
Chris@73 47 QGroupBox *timeBox = 0;
Chris@73 48 QGroupBox *valueBox = 0;
Chris@73 49 QGridLayout *subgrid = 0;
Chris@70 50
Chris@73 51 int row = 0, subrow = 0;
Chris@70 52
Chris@807 53 int singleStep = RealTime::frame2RealTime(2, sampleRate).usec() - 1;
Chris@70 54
Chris@73 55 if ((options & ShowTime) || (options & ShowDuration)) {
Chris@70 56
Chris@73 57 timeBox = new QGroupBox;
Chris@73 58 timeBox->setTitle(tr("Timing"));
Chris@73 59 grid->addWidget(timeBox, row, 0);
Chris@70 60
Chris@73 61 subgrid = new QGridLayout;
Chris@73 62 timeBox->setLayout(subgrid);
Chris@70 63
Chris@70 64 ++row;
Chris@70 65 }
Chris@70 66
Chris@73 67 if (options & ShowTime) {
Chris@73 68
Chris@73 69 subgrid->addWidget(new QLabel(tr("Time:")), subrow, 0);
Chris@73 70
Chris@73 71 m_frameTimeSpinBox = new QSpinBox;
Chris@73 72 m_frameTimeSpinBox->setMaximum(INT_MAX);
Chris@73 73 m_frameTimeSpinBox->setSuffix(tr(" frames"));
Chris@73 74 subgrid->addWidget(m_frameTimeSpinBox, subrow, 1, 1, 2);
Chris@73 75 connect(m_frameTimeSpinBox, SIGNAL(valueChanged(int)),
Chris@73 76 this, SLOT(frameTimeChanged(int)));
Chris@73 77
Chris@73 78 ++subrow;
Chris@73 79
Chris@73 80 m_realTimeSecsSpinBox = new QSpinBox;
Chris@73 81 m_realTimeSecsSpinBox->setMaximum(999999);
Chris@73 82 m_realTimeSecsSpinBox->setSuffix(tr(" sec"));
Chris@73 83 subgrid->addWidget(m_realTimeSecsSpinBox, subrow, 1);
Chris@73 84 connect(m_realTimeSecsSpinBox, SIGNAL(valueChanged(int)),
Chris@73 85 this, SLOT(realTimeSecsChanged(int)));
Chris@73 86
Chris@73 87 m_realTimeUSecsSpinBox = new QSpinBox;
Chris@73 88 m_realTimeUSecsSpinBox->setMaximum(999999);
Chris@73 89 m_realTimeUSecsSpinBox->setSuffix(tr(" usec"));
Chris@73 90 m_realTimeUSecsSpinBox->setSingleStep(singleStep);
Chris@73 91 subgrid->addWidget(m_realTimeUSecsSpinBox, subrow, 2);
Chris@73 92 connect(m_realTimeUSecsSpinBox, SIGNAL(valueChanged(int)),
Chris@73 93 this, SLOT(realTimeUSecsChanged(int)));
Chris@73 94
Chris@73 95 ++subrow;
Chris@73 96 }
Chris@73 97
Chris@70 98 if (options & ShowDuration) {
Chris@70 99
Chris@73 100 subgrid->addWidget(new QLabel(tr("Duration:")), subrow, 0);
Chris@70 101
Chris@73 102 m_frameDurationSpinBox = new QSpinBox;
Chris@73 103 m_frameDurationSpinBox->setMaximum(INT_MAX);
Chris@73 104 m_frameDurationSpinBox->setSuffix(tr(" frames"));
Chris@73 105 subgrid->addWidget(m_frameDurationSpinBox, subrow, 1, 1, 2);
Chris@73 106 connect(m_frameDurationSpinBox, SIGNAL(valueChanged(int)),
Chris@73 107 this, SLOT(frameDurationChanged(int)));
Chris@70 108
Chris@73 109 ++subrow;
Chris@70 110
Chris@73 111 m_realDurationSecsSpinBox = new QSpinBox;
Chris@73 112 m_realDurationSecsSpinBox->setMaximum(999999);
Chris@73 113 m_realDurationSecsSpinBox->setSuffix(tr(" sec"));
Chris@73 114 subgrid->addWidget(m_realDurationSecsSpinBox, subrow, 1);
Chris@73 115 connect(m_realDurationSecsSpinBox, SIGNAL(valueChanged(int)),
Chris@73 116 this, SLOT(realDurationSecsChanged(int)));
Chris@70 117
Chris@73 118 m_realDurationUSecsSpinBox = new QSpinBox;
Chris@73 119 m_realDurationUSecsSpinBox->setMaximum(999999);
Chris@73 120 m_realDurationUSecsSpinBox->setSuffix(tr(" usec"));
Chris@73 121 m_realDurationUSecsSpinBox->setSingleStep(singleStep);
Chris@101 122 subgrid->addWidget(m_realDurationUSecsSpinBox, subrow, 2);
Chris@73 123 connect(m_realDurationUSecsSpinBox, SIGNAL(valueChanged(int)),
Chris@73 124 this, SLOT(realDurationUSecsChanged(int)));
Chris@70 125
Chris@73 126 ++subrow;
Chris@73 127 }
Chris@70 128
Chris@73 129 if ((options & ShowValue) || (options & ShowText)) {
Chris@70 130
Chris@73 131 valueBox = new QGroupBox;
Chris@73 132 valueBox->setTitle(tr("Properties"));
Chris@73 133 grid->addWidget(valueBox, row, 0);
Chris@73 134
Chris@73 135 subgrid = new QGridLayout;
Chris@73 136 valueBox->setLayout(subgrid);
Chris@70 137
Chris@70 138 ++row;
Chris@70 139 }
Chris@70 140
Chris@73 141 subrow = 0;
Chris@73 142
Chris@70 143 if (options & ShowValue) {
Chris@70 144
Chris@73 145 subgrid->addWidget(new QLabel(tr("Value:")), subrow, 0);
Chris@70 146
Chris@73 147 m_valueSpinBox = new QDoubleSpinBox;
Chris@73 148 m_valueSpinBox->setSuffix(QString(" %1").arg(valueUnits));
Chris@73 149 m_valueSpinBox->setDecimals(10);
Chris@685 150 m_valueSpinBox->setMinimum(-1e10);
Chris@685 151 m_valueSpinBox->setMaximum(1e10);
Chris@73 152 connect(m_valueSpinBox, SIGNAL(valueChanged(double)),
Chris@70 153 this, SLOT(valueChanged(double)));
Chris@73 154 subgrid->addWidget(m_valueSpinBox, subrow, 1);
Chris@70 155
Chris@73 156 ++subrow;
Chris@70 157 }
Chris@70 158
Chris@70 159 if (options & ShowText) {
Chris@70 160
Chris@73 161 subgrid->addWidget(new QLabel(tr("Text:")), subrow, 0);
Chris@70 162
Chris@73 163 m_textField = new QLineEdit;
Chris@73 164 connect(m_textField, SIGNAL(textChanged(QString)),
Chris@70 165 this, SLOT(textChanged(QString)));
Chris@73 166 subgrid->addWidget(m_textField, subrow, 1);
Chris@70 167
Chris@73 168 ++subrow;
Chris@73 169 }
Chris@73 170
Chris@73 171 if (options & ShowText) {
Chris@73 172 m_textField->setFocus(Qt::OtherFocusReason);
Chris@73 173 } else if (options & ShowValue) {
Chris@73 174 m_valueSpinBox->setFocus(Qt::OtherFocusReason);
Chris@70 175 }
Chris@282 176
Chris@282 177 QDialogButtonBox *bb = new QDialogButtonBox(Qt::Horizontal);
Chris@282 178 grid->addWidget(bb, row, 0, 1, 2);
Chris@70 179
Chris@70 180 QPushButton *ok = new QPushButton(tr("OK"));
Chris@73 181 m_resetButton = new QPushButton(tr("Reset"));
Chris@70 182 QPushButton *cancel = new QPushButton(tr("Cancel"));
Chris@282 183 bb->addButton(ok, QDialogButtonBox::AcceptRole);
Chris@282 184 bb->addButton(m_resetButton, QDialogButtonBox::ResetRole);
Chris@282 185 bb->addButton(cancel, QDialogButtonBox::RejectRole);
Chris@70 186 connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
Chris@73 187 connect(m_resetButton, SIGNAL(clicked()), this, SLOT(reset()));
Chris@70 188 connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
Chris@73 189 m_resetButton->setEnabled(false);
Chris@70 190 }
Chris@70 191
Chris@70 192 void
Chris@807 193 ItemEditDialog::setFrameTime(int frame)
Chris@70 194 {
Chris@73 195 if (!m_frameTimeSpinBox) return;
Chris@73 196
Chris@73 197 RealTime rt(RealTime::frame2RealTime(frame, m_sampleRate));
Chris@73 198 m_realTimeSecsSpinBox->setValue(rt.sec);
Chris@73 199 m_realTimeUSecsSpinBox->setValue(rt.usec());
Chris@73 200 m_frameTimeSpinBox->setValue(frame);
Chris@73 201 m_defaultFrame = frame;
Chris@73 202 m_resetButton->setEnabled(false);
Chris@70 203 }
Chris@70 204
Chris@807 205 int
Chris@70 206 ItemEditDialog::getFrameTime() const
Chris@70 207 {
Chris@73 208 return m_frameTimeSpinBox->value();
Chris@70 209 }
Chris@70 210
Chris@70 211 void
Chris@70 212 ItemEditDialog::setRealTime(RealTime rt)
Chris@70 213 {
Chris@73 214 setFrameTime(RealTime::realTime2Frame(rt, m_sampleRate));
Chris@70 215 }
Chris@70 216
Chris@70 217 RealTime
Chris@70 218 ItemEditDialog::getRealTime() const
Chris@70 219 {
Chris@73 220 return RealTime::frame2RealTime(getFrameTime(), m_sampleRate);
Chris@70 221 }
Chris@70 222
Chris@70 223 void
Chris@807 224 ItemEditDialog::setFrameDuration(int duration)
Chris@70 225 {
Chris@73 226 if (!m_frameDurationSpinBox) return;
Chris@73 227
Chris@73 228 RealTime rt(RealTime::frame2RealTime(duration, m_sampleRate));
Chris@73 229 m_realDurationSecsSpinBox->setValue(rt.sec);
Chris@73 230 m_realDurationUSecsSpinBox->setValue(rt.usec());
Chris@73 231 m_frameDurationSpinBox->setValue(duration);
Chris@73 232 m_defaultDuration = duration;
Chris@73 233 m_resetButton->setEnabled(false);
Chris@70 234 }
Chris@70 235
Chris@807 236 int
Chris@70 237 ItemEditDialog::getFrameDuration() const
Chris@70 238 {
Chris@73 239 return m_frameDurationSpinBox->value();
Chris@70 240 }
Chris@70 241
Chris@70 242 void
Chris@70 243 ItemEditDialog::setRealDuration(RealTime rt)
Chris@70 244 {
Chris@73 245 setFrameDuration(RealTime::realTime2Frame(rt, m_sampleRate));
Chris@70 246 }
Chris@70 247
Chris@70 248 RealTime
Chris@70 249 ItemEditDialog::getRealDuration() const
Chris@70 250 {
Chris@73 251 return RealTime::frame2RealTime(getFrameDuration(), m_sampleRate);
Chris@70 252 }
Chris@70 253
Chris@70 254 void
Chris@70 255 ItemEditDialog::setValue(float v)
Chris@70 256 {
Chris@73 257 if (!m_valueSpinBox) return;
Chris@73 258
Chris@73 259 m_valueSpinBox->setValue(v);
Chris@73 260 m_defaultValue = v;
Chris@73 261 m_resetButton->setEnabled(false);
Chris@70 262 }
Chris@70 263
Chris@70 264 float
Chris@70 265 ItemEditDialog::getValue() const
Chris@70 266 {
Chris@73 267 return m_valueSpinBox->value();
Chris@70 268 }
Chris@70 269
Chris@70 270 void
Chris@70 271 ItemEditDialog::setText(QString text)
Chris@70 272 {
Chris@73 273 if (!m_textField) return;
Chris@73 274
Chris@73 275 m_textField->setText(text);
Chris@73 276 m_defaultText = text;
Chris@73 277 m_resetButton->setEnabled(false);
Chris@70 278 }
Chris@70 279
Chris@70 280 QString
Chris@70 281 ItemEditDialog::getText() const
Chris@70 282 {
Chris@73 283 return m_textField->text();
Chris@70 284 }
Chris@70 285
Chris@70 286 void
Chris@73 287 ItemEditDialog::frameTimeChanged(int i)
Chris@70 288 {
Chris@73 289 m_realTimeSecsSpinBox->blockSignals(true);
Chris@73 290 m_realTimeUSecsSpinBox->blockSignals(true);
Chris@73 291
Chris@73 292 RealTime rt(RealTime::frame2RealTime(i, m_sampleRate));
Chris@73 293 m_realTimeSecsSpinBox->setValue(rt.sec);
Chris@73 294 m_realTimeUSecsSpinBox->setValue(rt.usec());
Chris@73 295
Chris@73 296 m_realTimeSecsSpinBox->blockSignals(false);
Chris@73 297 m_realTimeUSecsSpinBox->blockSignals(false);
Chris@73 298 m_resetButton->setEnabled(true);
Chris@70 299 }
Chris@70 300
Chris@70 301 void
Chris@73 302 ItemEditDialog::realTimeSecsChanged(int i)
Chris@70 303 {
Chris@70 304 RealTime rt = getRealTime();
Chris@73 305 rt.sec = i;
Chris@807 306 int frame = RealTime::realTime2Frame(rt, m_sampleRate);
Chris@73 307 m_frameTimeSpinBox->setValue(frame);
Chris@73 308 m_resetButton->setEnabled(true);
Chris@70 309 }
Chris@70 310
Chris@70 311 void
Chris@73 312 ItemEditDialog::realTimeUSecsChanged(int i)
Chris@70 313 {
Chris@70 314 RealTime rt = getRealTime();
Chris@73 315 rt.nsec = i * 1000;
Chris@807 316 int frame = RealTime::realTime2Frame(rt, m_sampleRate);
Chris@73 317 m_frameTimeSpinBox->setValue(frame);
Chris@73 318 m_resetButton->setEnabled(true);
Chris@70 319 }
Chris@70 320
Chris@70 321 void
Chris@73 322 ItemEditDialog::frameDurationChanged(int i)
Chris@70 323 {
Chris@73 324 m_realDurationSecsSpinBox->blockSignals(true);
Chris@73 325 m_realDurationUSecsSpinBox->blockSignals(true);
Chris@73 326
Chris@73 327 RealTime rt(RealTime::frame2RealTime(i, m_sampleRate));
Chris@73 328 m_realDurationSecsSpinBox->setValue(rt.sec);
Chris@73 329 m_realDurationUSecsSpinBox->setValue(rt.usec());
Chris@73 330
Chris@73 331 m_realDurationSecsSpinBox->blockSignals(false);
Chris@73 332 m_realDurationUSecsSpinBox->blockSignals(false);
Chris@73 333 m_resetButton->setEnabled(true);
Chris@70 334 }
Chris@70 335
Chris@70 336 void
Chris@73 337 ItemEditDialog::realDurationSecsChanged(int i)
Chris@70 338 {
Chris@70 339 RealTime rt = getRealDuration();
Chris@73 340 rt.sec = i;
Chris@807 341 int frame = RealTime::realTime2Frame(rt, m_sampleRate);
Chris@73 342 m_frameDurationSpinBox->setValue(frame);
Chris@73 343 m_resetButton->setEnabled(true);
Chris@70 344 }
Chris@70 345
Chris@70 346 void
Chris@73 347 ItemEditDialog::realDurationUSecsChanged(int i)
Chris@70 348 {
Chris@70 349 RealTime rt = getRealDuration();
Chris@73 350 rt.nsec = i * 1000;
Chris@807 351 int frame = RealTime::realTime2Frame(rt, m_sampleRate);
Chris@73 352 m_frameDurationSpinBox->setValue(frame);
Chris@73 353 m_resetButton->setEnabled(true);
Chris@70 354 }
Chris@70 355
Chris@70 356 void
Chris@73 357 ItemEditDialog::valueChanged(double)
Chris@70 358 {
Chris@73 359 m_resetButton->setEnabled(true);
Chris@70 360 }
Chris@70 361
Chris@70 362 void
Chris@73 363 ItemEditDialog::textChanged(QString)
Chris@70 364 {
Chris@73 365 m_resetButton->setEnabled(true);
Chris@70 366 }
Chris@70 367
Chris@73 368 void
Chris@73 369 ItemEditDialog::reset()
Chris@73 370 {
Chris@73 371 setFrameTime(m_defaultFrame);
Chris@73 372 setFrameDuration(m_defaultDuration);
Chris@73 373 setValue(m_defaultValue);
Chris@73 374 setText(m_defaultText);
Chris@73 375 m_resetButton->setEnabled(false);
Chris@73 376 }
Chris@73 377