annotate widgets/PropertyBox.cpp @ 57:2b87a7bd9256

* update TODO, some tidying
author Chris Cannam
date Mon, 20 Mar 2006 11:37:45 +0000
parents 128ebfeeebee
children 01ab51f72e84
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@57 156 gainDial->setToolTip(tr("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@57 177 panDial->setToolTip(tr("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@55 267 if (cb->isChecked() != (value > 0)) {
Chris@55 268 cb->blockSignals(true);
Chris@55 269 cb->setChecked(value > 0);
Chris@55 270 cb->blockSignals(false);
Chris@55 271 }
Chris@0 272 break;
Chris@0 273 }
Chris@0 274
Chris@0 275 case PropertyContainer::RangeProperty:
Chris@0 276 {
Chris@0 277 AudioDial *dial;
Chris@0 278
Chris@0 279 if (have) {
Chris@0 280 dial = dynamic_cast<AudioDial *>(m_propertyControllers[name]);
Chris@0 281 assert(dial);
Chris@0 282 } else {
Chris@0 283 #ifdef DEBUG_PROPERTY_BOX
Chris@0 284 std::cerr << "PropertyBox: creating new dial" << std::endl;
Chris@0 285 #endif
Chris@0 286 dial = new AudioDial();
Chris@0 287 dial->setObjectName(name);
Chris@0 288 dial->setMinimum(min);
Chris@0 289 dial->setMaximum(max);
Chris@0 290 dial->setPageStep(1);
Chris@34 291 dial->setNotchesVisible((max - min) <= 12);
Chris@34 292 dial->setDefaultValue(value);
Chris@0 293 connect(dial, SIGNAL(valueChanged(int)),
Chris@0 294 this, SLOT(propertyControllerChanged(int)));
Chris@0 295
Chris@0 296 if (inGroup) {
Chris@0 297 dial->setFixedWidth(24);
Chris@0 298 dial->setFixedHeight(24);
Chris@0 299 dial->setToolTip(name);
Chris@0 300 m_groupLayouts[groupName]->addWidget(dial);
Chris@0 301 } else {
Chris@0 302 dial->setFixedWidth(32);
Chris@0 303 dial->setFixedHeight(32);
Chris@0 304 m_layout->addWidget(dial, row, 1);
Chris@33 305 QLabel *label = new QLabel(m_mainWidget);
Chris@0 306 connect(dial, SIGNAL(valueChanged(int)),
Chris@0 307 label, SLOT(setNum(int)));
Chris@0 308 label->setNum(value);
Chris@0 309 m_layout->addWidget(label, row, 2);
Chris@0 310 }
Chris@0 311
Chris@0 312 m_propertyControllers[name] = dial;
Chris@0 313 }
Chris@0 314
Chris@55 315 if (dial->value() != value) {
Chris@55 316 dial->blockSignals(true);
Chris@55 317 dial->setValue(value);
Chris@55 318 dial->blockSignals(false);
Chris@55 319 }
Chris@0 320 break;
Chris@0 321 }
Chris@0 322
Chris@0 323 case PropertyContainer::ValueProperty:
Chris@0 324 {
Chris@0 325 QComboBox *cb;
Chris@0 326
Chris@0 327 if (have) {
Chris@0 328 cb = dynamic_cast<QComboBox *>(m_propertyControllers[name]);
Chris@0 329 assert(cb);
Chris@0 330 } else {
Chris@0 331 #ifdef DEBUG_PROPERTY_BOX
Chris@0 332 std::cerr << "PropertyBox: creating new combobox" << std::endl;
Chris@0 333 #endif
Chris@0 334
Chris@0 335 cb = new QComboBox();
Chris@0 336 cb->setObjectName(name);
Chris@0 337 for (int i = min; i <= max; ++i) {
Chris@0 338 cb->addItem(m_container->getPropertyValueLabel(name, i));
Chris@0 339 }
Chris@0 340 connect(cb, SIGNAL(activated(int)),
Chris@0 341 this, SLOT(propertyControllerChanged(int)));
Chris@0 342 if (inGroup) {
Chris@0 343 cb->setToolTip(name);
Chris@0 344 m_groupLayouts[groupName]->addWidget(cb);
Chris@0 345 } else {
Chris@0 346 m_layout->addWidget(cb, row, 1, 1, 2);
Chris@0 347 }
Chris@0 348 m_propertyControllers[name] = cb;
Chris@0 349 }
Chris@0 350
Chris@55 351 if (cb->currentIndex() != value) {
Chris@55 352 cb->blockSignals(true);
Chris@55 353 cb->setCurrentIndex(value);
Chris@55 354 cb->blockSignals(false);
Chris@55 355 }
Chris@0 356
Chris@0 357 #ifdef Q_WS_MAC
Chris@0 358 // Crashes on startup without this, for some reason
Chris@0 359 cb->setMinimumSize(QSize(10, 10));
Chris@0 360 #endif
Chris@0 361
Chris@0 362 break;
Chris@0 363 }
Chris@0 364
Chris@0 365 default:
Chris@0 366 break;
Chris@0 367 }
Chris@0 368 }
Chris@0 369
Chris@0 370 void
Chris@0 371 PropertyBox::propertyContainerPropertyChanged(PropertyContainer *pc)
Chris@0 372 {
Chris@0 373 if (pc != m_container) return;
Chris@0 374
Chris@55 375 #ifdef DEBUG_PROPERTY_BOX
Chris@55 376 std::cerr << "PropertyBox::propertyContainerPropertyChanged" << std::endl;
Chris@55 377 #endif
Chris@55 378
Chris@0 379 PropertyContainer::PropertyList properties = m_container->getProperties();
Chris@0 380 size_t i;
Chris@0 381
Chris@0 382 blockSignals(true);
Chris@0 383
Chris@0 384 for (i = 0; i < properties.size(); ++i) {
Chris@0 385 updatePropertyEditor(properties[i]);
Chris@0 386 }
Chris@0 387
Chris@0 388 blockSignals(false);
Chris@0 389 }
Chris@0 390
Chris@0 391 void
Chris@0 392 PropertyBox::propertyControllerChanged(int value)
Chris@0 393 {
Chris@0 394 QObject *obj = sender();
Chris@0 395 QString name = obj->objectName();
Chris@0 396
Chris@34 397 #ifdef DEBUG_PROPERTY_BOX
Chris@33 398 std::cerr << "PropertyBox::propertyControllerChanged(" << name.toStdString()
Chris@33 399 << ", " << value << ")" << std::endl;
Chris@34 400 #endif
Chris@0 401
Chris@0 402 PropertyContainer::PropertyType type = m_container->getPropertyType(name);
Chris@0 403
Chris@0 404 if (type != PropertyContainer::InvalidProperty) {
Chris@55 405 m_container->setPropertyWithCommand(name, value);
Chris@0 406 }
Chris@0 407
Chris@0 408 if (type == PropertyContainer::RangeProperty) {
Chris@0 409 AudioDial *dial = dynamic_cast<AudioDial *>(m_propertyControllers[name]);
Chris@0 410 if (dial) {
Chris@0 411 dial->setToolTip(QString("%1: %2").arg(name).arg(value));
Chris@0 412 //!!! unfortunately this doesn't update an already-visible tooltip
Chris@0 413 }
Chris@0 414 }
Chris@0 415 }
Chris@0 416
Chris@34 417 void
Chris@34 418 PropertyBox::playGainChanged(float gain)
Chris@34 419 {
Chris@34 420 int dialValue = lrint(log10(gain) * 20.0);
Chris@34 421 if (dialValue < -50) dialValue = -50;
Chris@34 422 if (dialValue > 50) dialValue = 50;
Chris@34 423 emit changePlayGainDial(dialValue);
Chris@34 424 }
Chris@34 425
Chris@34 426 void
Chris@34 427 PropertyBox::playGainDialChanged(int dialValue)
Chris@34 428 {
Chris@34 429 float gain = pow(10, float(dialValue) / 20.0);
Chris@34 430 emit changePlayGain(gain);
Chris@34 431 }
Chris@34 432
Chris@34 433 void
Chris@34 434 PropertyBox::playPanChanged(float pan)
Chris@34 435 {
Chris@34 436 int dialValue = lrint(pan * 50.0);
Chris@34 437 if (dialValue < -50) dialValue = -50;
Chris@34 438 if (dialValue > 50) dialValue = 50;
Chris@34 439 emit changePlayPanDial(dialValue);
Chris@34 440 }
Chris@34 441
Chris@34 442 void
Chris@34 443 PropertyBox::playPanDialChanged(int dialValue)
Chris@34 444 {
Chris@34 445 float pan = float(dialValue) / 50.0;
Chris@34 446 if (pan < -1.0) pan = -1.0;
Chris@34 447 if (pan > 1.0) pan = 1.0;
Chris@34 448 emit changePlayPan(pan);
Chris@34 449 }
Chris@0 450
Chris@0 451 #ifdef INCLUDE_MOCFILES
Chris@0 452 #include "PropertyBox.moc.cpp"
Chris@0 453 #endif
Chris@0 454