annotate widgets/PropertyBox.cpp @ 35:10ba9276a315

* Add TextModel and TextLayer types * Make View refresh work better when editing a model (previously edits might not be refreshed if their visible changed area extended beyond the strict frame range that was being modified in the model) * Add phase-adjusted instantaneous frequency display to spectrogram layer (still a work in progress) * Pull maths aliases out into a separate header in dsp/maths so MathUtilities can be included without introducing them
author Chris Cannam
date Mon, 20 Feb 2006 13:33:36 +0000
parents c43f2c4f66f2
children c28ebb4ba4de
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 A waveform viewer and audio annotation editor.
Chris@5 5 Chris Cannam, Queen Mary University of London, 2005-2006
Chris@0 6
Chris@0 7 This is experimental software. Not for distribution.
Chris@0 8 */
Chris@0 9
Chris@0 10 #include "PropertyBox.h"
Chris@0 11
Chris@0 12 #include "base/PropertyContainer.h"
Chris@33 13 #include "base/PlayParameters.h"
Chris@33 14 #include "base/Layer.h"
Chris@0 15
Chris@0 16 #include "AudioDial.h"
Chris@33 17 #include "LEDButton.h"
Chris@0 18
Chris@0 19 #include <QGridLayout>
Chris@0 20 #include <QHBoxLayout>
Chris@33 21 #include <QVBoxLayout>
Chris@0 22 #include <QCheckBox>
Chris@0 23 #include <QComboBox>
Chris@0 24 #include <QLabel>
Chris@33 25 #include <QFrame>
Chris@0 26
Chris@0 27 #include <cassert>
Chris@0 28 #include <iostream>
Chris@0 29
Chris@0 30 //#define DEBUG_PROPERTY_BOX 1
Chris@0 31
Chris@0 32 PropertyBox::PropertyBox(PropertyContainer *container) :
Chris@0 33 m_container(container)
Chris@0 34 {
Chris@0 35 #ifdef DEBUG_PROPERTY_BOX
Chris@0 36 std::cerr << "PropertyBox[" << this << "(\"" <<
Chris@0 37 container->getPropertyContainerName().toStdString() << "\")]::PropertyBox" << std::endl;
Chris@0 38 #endif
Chris@0 39
Chris@34 40 m_mainBox = new QVBoxLayout;
Chris@34 41 setLayout(m_mainBox);
Chris@33 42
Chris@33 43 bool needViewPlayBox = false;
Chris@33 44
Chris@34 45 m_mainWidget = new QWidget;
Chris@34 46 m_mainBox->addWidget(m_mainWidget);
Chris@34 47 m_mainBox->insertStretch(1, 10);
Chris@33 48
Chris@34 49 m_viewPlayFrame = 0;
Chris@34 50 populateViewPlayFrame();
Chris@33 51
Chris@0 52 m_layout = new QGridLayout;
Chris@34 53 m_layout->setMargin(0);
Chris@33 54 m_mainWidget->setLayout(m_layout);
Chris@0 55
Chris@34 56 PropertyContainer::PropertyList properties = m_container->getProperties();
Chris@0 57
Chris@0 58 blockSignals(true);
Chris@0 59
Chris@0 60 size_t i;
Chris@0 61
Chris@0 62 for (i = 0; i < properties.size(); ++i) {
Chris@0 63 updatePropertyEditor(properties[i]);
Chris@0 64 }
Chris@0 65
Chris@0 66 blockSignals(false);
Chris@0 67
Chris@0 68 m_layout->setRowStretch(m_layout->rowCount(), 10);
Chris@0 69
Chris@0 70 #ifdef DEBUG_PROPERTY_BOX
Chris@0 71 std::cerr << "PropertyBox[" << this << "]::PropertyBox returning" << std::endl;
Chris@0 72 #endif
Chris@0 73 }
Chris@0 74
Chris@0 75 PropertyBox::~PropertyBox()
Chris@0 76 {
Chris@0 77 #ifdef DEBUG_PROPERTY_BOX
Chris@33 78 std::cerr << "PropertyBox[" << this << "]::~PropertyBox" << std::endl;
Chris@0 79 #endif
Chris@0 80 }
Chris@0 81
Chris@0 82 void
Chris@34 83 PropertyBox::populateViewPlayFrame()
Chris@33 84 {
Chris@34 85 std::cerr << "PropertyBox(" << m_container << ")::populateViewPlayFrame" << std::endl;
Chris@34 86
Chris@34 87 if (m_viewPlayFrame) {
Chris@34 88 delete m_viewPlayFrame;
Chris@34 89 m_viewPlayFrame = 0;
Chris@34 90 }
Chris@34 91
Chris@34 92 if (!m_container) return;
Chris@34 93
Chris@34 94 Layer *layer = dynamic_cast<Layer *>(m_container);
Chris@34 95 if (layer) {
Chris@34 96 connect(layer, SIGNAL(modelReplaced()),
Chris@34 97 this, SLOT(populateViewPlayFrame()));
Chris@34 98 }
Chris@34 99
Chris@34 100 PlayParameters *params = m_container->getPlayParameters();
Chris@33 101 if (!params && !layer) return;
Chris@33 102
Chris@34 103 m_viewPlayFrame = new QFrame;
Chris@34 104 m_viewPlayFrame->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
Chris@34 105 m_mainBox->addWidget(m_viewPlayFrame);
Chris@34 106
Chris@34 107 QHBoxLayout *layout = new QHBoxLayout;
Chris@34 108 m_viewPlayFrame->setLayout(layout);
Chris@34 109
Chris@34 110 layout->setMargin(layout->margin() / 2);
Chris@34 111
Chris@34 112 std::cerr << "PropertyBox::populateViewPlayFrame: container " << m_container << " (name " << m_container->getPropertyContainerName().toStdString() << ") params " << params << std::endl;
Chris@33 113
Chris@33 114 if (layer) {
Chris@34 115 QLabel *showLabel = new QLabel(tr("Show"));
Chris@34 116 layout->addWidget(showLabel);
Chris@34 117 layout->setAlignment(showLabel, Qt::AlignVCenter);
Chris@34 118
Chris@33 119 LEDButton *showButton = new LEDButton(Qt::blue);
Chris@33 120 layout->addWidget(showButton);
Chris@33 121 connect(showButton, SIGNAL(stateChanged(bool)),
Chris@33 122 layer, SLOT(showLayer(bool)));
Chris@34 123 layout->setAlignment(showButton, Qt::AlignVCenter);
Chris@33 124 }
Chris@33 125
Chris@33 126 if (params) {
Chris@34 127
Chris@34 128 QLabel *playLabel = new QLabel(tr("Play"));
Chris@34 129 layout->addWidget(playLabel);
Chris@34 130 layout->setAlignment(playLabel, Qt::AlignVCenter);
Chris@34 131
Chris@33 132 LEDButton *playButton = new LEDButton(Qt::darkGreen);
Chris@33 133 layout->addWidget(playButton);
Chris@33 134 connect(playButton, SIGNAL(stateChanged(bool)),
Chris@33 135 params, SLOT(setPlayAudible(bool)));
Chris@34 136 connect(params, SIGNAL(playAudibleChanged(bool)),
Chris@34 137 playButton, SLOT(setState(bool)));
Chris@34 138 layout->setAlignment(playButton, Qt::AlignVCenter);
Chris@34 139
Chris@34 140 layout->insertStretch(-1, 10);
Chris@34 141
Chris@34 142 AudioDial *gainDial = new AudioDial;
Chris@34 143 layout->addWidget(gainDial);
Chris@34 144 gainDial->setMeterColor(Qt::darkRed);
Chris@34 145 gainDial->setMinimum(-50);
Chris@34 146 gainDial->setMaximum(50);
Chris@34 147 gainDial->setPageStep(1);
Chris@34 148 gainDial->setFixedWidth(24);
Chris@34 149 gainDial->setFixedHeight(24);
Chris@34 150 gainDial->setNotchesVisible(false);
Chris@35 151 gainDial->setToolTip(tr("Layer playback Level"));
Chris@34 152 gainDial->setDefaultValue(0);
Chris@34 153 connect(gainDial, SIGNAL(valueChanged(int)),
Chris@34 154 this, SLOT(playGainDialChanged(int)));
Chris@34 155 connect(params, SIGNAL(playGainChanged(float)),
Chris@34 156 this, SLOT(playGainChanged(float)));
Chris@34 157 connect(this, SIGNAL(changePlayGain(float)),
Chris@34 158 params, SLOT(setPlayGain(float)));
Chris@34 159 connect(this, SIGNAL(changePlayGainDial(int)),
Chris@34 160 gainDial, SLOT(setValue(int)));
Chris@34 161 layout->setAlignment(gainDial, Qt::AlignVCenter);
Chris@34 162
Chris@34 163 AudioDial *panDial = new AudioDial;
Chris@34 164 layout->addWidget(panDial);
Chris@34 165 panDial->setMeterColor(Qt::darkGreen);
Chris@34 166 panDial->setMinimum(-50);
Chris@34 167 panDial->setMaximum(50);
Chris@34 168 panDial->setPageStep(1);
Chris@34 169 panDial->setFixedWidth(24);
Chris@34 170 panDial->setFixedHeight(24);
Chris@34 171 panDial->setNotchesVisible(false);
Chris@35 172 panDial->setToolTip(tr("Layer playback Pan / Balance"));
Chris@34 173 panDial->setDefaultValue(0);
Chris@34 174 connect(panDial, SIGNAL(valueChanged(int)),
Chris@34 175 this, SLOT(playPanDialChanged(int)));
Chris@34 176 connect(params, SIGNAL(playPanChanged(float)),
Chris@34 177 this, SLOT(playPanChanged(float)));
Chris@34 178 connect(this, SIGNAL(changePlayPan(float)),
Chris@34 179 params, SLOT(setPlayPan(float)));
Chris@34 180 connect(this, SIGNAL(changePlayPanDial(int)),
Chris@34 181 panDial, SLOT(setValue(int)));
Chris@34 182 layout->setAlignment(panDial, Qt::AlignVCenter);
Chris@34 183
Chris@34 184 } else {
Chris@34 185
Chris@34 186 layout->insertStretch(-1, 10);
Chris@33 187 }
Chris@33 188 }
Chris@33 189
Chris@33 190 void
Chris@0 191 PropertyBox::updatePropertyEditor(PropertyContainer::PropertyName name)
Chris@0 192 {
Chris@0 193 PropertyContainer::PropertyType type = m_container->getPropertyType(name);
Chris@0 194 int row = m_layout->rowCount();
Chris@0 195
Chris@0 196 int min = 0, max = 0, value = 0;
Chris@0 197 value = m_container->getPropertyRangeAndValue(name, &min, &max);
Chris@0 198
Chris@0 199 bool have = (m_propertyControllers.find(name) !=
Chris@0 200 m_propertyControllers.end());
Chris@0 201
Chris@0 202 QString groupName = m_container->getPropertyGroupName(name);
Chris@0 203
Chris@0 204 #ifdef DEBUG_PROPERTY_BOX
Chris@0 205 std::cerr << "PropertyBox[" << this
Chris@0 206 << "(\"" << m_container->getPropertyContainerName().toStdString()
Chris@0 207 << "\")]";
Chris@0 208 std::cerr << "::updatePropertyEditor(\"" << name.toStdString() << "\"):";
Chris@0 209 std::cerr << " value " << value << ", have " << have << ", group \""
Chris@0 210 << groupName.toStdString() << "\"" << std::endl;
Chris@0 211 #endif
Chris@0 212
Chris@0 213 bool inGroup = (groupName != QString());
Chris@0 214
Chris@0 215 if (!have) {
Chris@0 216 if (inGroup) {
Chris@0 217 if (m_groupLayouts.find(groupName) == m_groupLayouts.end()) {
Chris@0 218 #ifdef DEBUG_PROPERTY_BOX
Chris@0 219 std::cerr << "PropertyBox: adding label \"" << groupName.toStdString() << "\" and frame for group for \"" << name.toStdString() << "\"" << std::endl;
Chris@0 220 #endif
Chris@33 221 m_layout->addWidget(new QLabel(groupName, m_mainWidget), row, 0);
Chris@33 222 QFrame *frame = new QFrame(m_mainWidget);
Chris@0 223 m_layout->addWidget(frame, row, 1, 1, 2);
Chris@0 224 m_groupLayouts[groupName] = new QHBoxLayout;
Chris@0 225 m_groupLayouts[groupName]->setMargin(0);
Chris@0 226 frame->setLayout(m_groupLayouts[groupName]);
Chris@0 227 }
Chris@0 228 } else {
Chris@0 229 #ifdef DEBUG_PROPERTY_BOX
Chris@0 230 std::cerr << "PropertyBox: adding label \"" << name.toStdString() << "\"" << std::endl;
Chris@0 231 #endif
Chris@33 232 m_layout->addWidget(new QLabel(name, m_mainWidget), row, 0);
Chris@0 233 }
Chris@0 234 }
Chris@0 235
Chris@0 236 switch (type) {
Chris@0 237
Chris@0 238 case PropertyContainer::ToggleProperty:
Chris@0 239 {
Chris@0 240 QCheckBox *cb;
Chris@0 241
Chris@0 242 if (have) {
Chris@0 243 cb = dynamic_cast<QCheckBox *>(m_propertyControllers[name]);
Chris@0 244 assert(cb);
Chris@0 245 } else {
Chris@0 246 #ifdef DEBUG_PROPERTY_BOX
Chris@0 247 std::cerr << "PropertyBox: creating new checkbox" << std::endl;
Chris@0 248 #endif
Chris@0 249 cb = new QCheckBox();
Chris@0 250 cb->setObjectName(name);
Chris@0 251 connect(cb, SIGNAL(stateChanged(int)),
Chris@0 252 this, SLOT(propertyControllerChanged(int)));
Chris@0 253 if (inGroup) {
Chris@0 254 cb->setToolTip(name);
Chris@0 255 m_groupLayouts[groupName]->addWidget(cb);
Chris@0 256 } else {
Chris@0 257 m_layout->addWidget(cb, row, 1, 1, 2);
Chris@0 258 }
Chris@0 259 m_propertyControllers[name] = cb;
Chris@0 260 }
Chris@0 261
Chris@0 262 if (cb->isChecked() != (value > 0)) cb->setChecked(value > 0);
Chris@0 263 break;
Chris@0 264 }
Chris@0 265
Chris@0 266 case PropertyContainer::RangeProperty:
Chris@0 267 {
Chris@0 268 AudioDial *dial;
Chris@0 269
Chris@0 270 if (have) {
Chris@0 271 dial = dynamic_cast<AudioDial *>(m_propertyControllers[name]);
Chris@0 272 assert(dial);
Chris@0 273 } else {
Chris@0 274 #ifdef DEBUG_PROPERTY_BOX
Chris@0 275 std::cerr << "PropertyBox: creating new dial" << std::endl;
Chris@0 276 #endif
Chris@0 277 dial = new AudioDial();
Chris@0 278 dial->setObjectName(name);
Chris@0 279 dial->setMinimum(min);
Chris@0 280 dial->setMaximum(max);
Chris@0 281 dial->setPageStep(1);
Chris@34 282 dial->setNotchesVisible((max - min) <= 12);
Chris@34 283 dial->setDefaultValue(value);
Chris@0 284 connect(dial, SIGNAL(valueChanged(int)),
Chris@0 285 this, SLOT(propertyControllerChanged(int)));
Chris@0 286
Chris@0 287 if (inGroup) {
Chris@0 288 dial->setFixedWidth(24);
Chris@0 289 dial->setFixedHeight(24);
Chris@0 290 dial->setToolTip(name);
Chris@0 291 m_groupLayouts[groupName]->addWidget(dial);
Chris@0 292 } else {
Chris@0 293 dial->setFixedWidth(32);
Chris@0 294 dial->setFixedHeight(32);
Chris@0 295 m_layout->addWidget(dial, row, 1);
Chris@33 296 QLabel *label = new QLabel(m_mainWidget);
Chris@0 297 connect(dial, SIGNAL(valueChanged(int)),
Chris@0 298 label, SLOT(setNum(int)));
Chris@0 299 label->setNum(value);
Chris@0 300 m_layout->addWidget(label, row, 2);
Chris@0 301 }
Chris@0 302
Chris@0 303 m_propertyControllers[name] = dial;
Chris@0 304 }
Chris@0 305
Chris@0 306 if (dial->value() != value) dial->setValue(value);
Chris@0 307 break;
Chris@0 308 }
Chris@0 309
Chris@0 310 case PropertyContainer::ValueProperty:
Chris@0 311 {
Chris@0 312 QComboBox *cb;
Chris@0 313
Chris@0 314 if (have) {
Chris@0 315 cb = dynamic_cast<QComboBox *>(m_propertyControllers[name]);
Chris@0 316 assert(cb);
Chris@0 317 } else {
Chris@0 318 #ifdef DEBUG_PROPERTY_BOX
Chris@0 319 std::cerr << "PropertyBox: creating new combobox" << std::endl;
Chris@0 320 #endif
Chris@0 321
Chris@0 322 cb = new QComboBox();
Chris@0 323 cb->setObjectName(name);
Chris@0 324 for (int i = min; i <= max; ++i) {
Chris@0 325 cb->addItem(m_container->getPropertyValueLabel(name, i));
Chris@0 326 }
Chris@0 327 connect(cb, SIGNAL(activated(int)),
Chris@0 328 this, SLOT(propertyControllerChanged(int)));
Chris@0 329 if (inGroup) {
Chris@0 330 cb->setToolTip(name);
Chris@0 331 m_groupLayouts[groupName]->addWidget(cb);
Chris@0 332 } else {
Chris@0 333 m_layout->addWidget(cb, row, 1, 1, 2);
Chris@0 334 }
Chris@0 335 m_propertyControllers[name] = cb;
Chris@0 336 }
Chris@0 337
Chris@0 338 if (cb->currentIndex() != value) cb->setCurrentIndex(value);
Chris@0 339
Chris@0 340 #ifdef Q_WS_MAC
Chris@0 341 // Crashes on startup without this, for some reason
Chris@0 342 cb->setMinimumSize(QSize(10, 10));
Chris@0 343 #endif
Chris@0 344
Chris@0 345 break;
Chris@0 346 }
Chris@0 347
Chris@0 348 default:
Chris@0 349 break;
Chris@0 350 }
Chris@0 351 }
Chris@0 352
Chris@0 353 void
Chris@0 354 PropertyBox::propertyContainerPropertyChanged(PropertyContainer *pc)
Chris@0 355 {
Chris@0 356 if (pc != m_container) return;
Chris@0 357
Chris@0 358 PropertyContainer::PropertyList properties = m_container->getProperties();
Chris@0 359 size_t i;
Chris@0 360
Chris@0 361 blockSignals(true);
Chris@0 362
Chris@0 363 for (i = 0; i < properties.size(); ++i) {
Chris@0 364 updatePropertyEditor(properties[i]);
Chris@0 365 }
Chris@0 366
Chris@0 367 blockSignals(false);
Chris@0 368 }
Chris@0 369
Chris@0 370 void
Chris@0 371 PropertyBox::propertyControllerChanged(int value)
Chris@0 372 {
Chris@0 373 QObject *obj = sender();
Chris@0 374 QString name = obj->objectName();
Chris@0 375
Chris@34 376 #ifdef DEBUG_PROPERTY_BOX
Chris@33 377 std::cerr << "PropertyBox::propertyControllerChanged(" << name.toStdString()
Chris@33 378 << ", " << value << ")" << std::endl;
Chris@34 379 #endif
Chris@0 380
Chris@0 381 PropertyContainer::PropertyType type = m_container->getPropertyType(name);
Chris@0 382
Chris@0 383 if (type != PropertyContainer::InvalidProperty) {
Chris@0 384 m_container->setProperty(name, value);
Chris@0 385 }
Chris@0 386
Chris@0 387 if (type == PropertyContainer::RangeProperty) {
Chris@0 388 AudioDial *dial = dynamic_cast<AudioDial *>(m_propertyControllers[name]);
Chris@0 389 if (dial) {
Chris@0 390 dial->setToolTip(QString("%1: %2").arg(name).arg(value));
Chris@0 391 //!!! unfortunately this doesn't update an already-visible tooltip
Chris@0 392 }
Chris@0 393 }
Chris@0 394 }
Chris@0 395
Chris@34 396 void
Chris@34 397 PropertyBox::playGainChanged(float gain)
Chris@34 398 {
Chris@34 399 int dialValue = lrint(log10(gain) * 20.0);
Chris@34 400 if (dialValue < -50) dialValue = -50;
Chris@34 401 if (dialValue > 50) dialValue = 50;
Chris@34 402 emit changePlayGainDial(dialValue);
Chris@34 403 }
Chris@34 404
Chris@34 405 void
Chris@34 406 PropertyBox::playGainDialChanged(int dialValue)
Chris@34 407 {
Chris@34 408 float gain = pow(10, float(dialValue) / 20.0);
Chris@34 409 emit changePlayGain(gain);
Chris@34 410 }
Chris@34 411
Chris@34 412 void
Chris@34 413 PropertyBox::playPanChanged(float pan)
Chris@34 414 {
Chris@34 415 int dialValue = lrint(pan * 50.0);
Chris@34 416 if (dialValue < -50) dialValue = -50;
Chris@34 417 if (dialValue > 50) dialValue = 50;
Chris@34 418 emit changePlayPanDial(dialValue);
Chris@34 419 }
Chris@34 420
Chris@34 421 void
Chris@34 422 PropertyBox::playPanDialChanged(int dialValue)
Chris@34 423 {
Chris@34 424 float pan = float(dialValue) / 50.0;
Chris@34 425 if (pan < -1.0) pan = -1.0;
Chris@34 426 if (pan > 1.0) pan = 1.0;
Chris@34 427 emit changePlayPan(pan);
Chris@34 428 }
Chris@0 429
Chris@0 430 #ifdef INCLUDE_MOCFILES
Chris@0 431 #include "PropertyBox.moc.cpp"
Chris@0 432 #endif
Chris@0 433