annotate widgets/PropertyBox.cpp @ 47:2e2ad8510e52

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