annotate widgets/PluginParameterBox.cpp @ 230:e52ed907cc42

* Add missing include
author Chris Cannam
date Thu, 15 Mar 2007 11:34:20 +0000
parents 8495187c13ce
children e6d0b097d102
rev   line source
Chris@60 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@60 2
Chris@60 3 /*
Chris@60 4 Sonic Visualiser
Chris@60 5 An audio file viewer and annotation editor.
Chris@60 6 Centre for Digital Music, Queen Mary, University of London.
Chris@182 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@60 8
Chris@60 9 This program is free software; you can redistribute it and/or
Chris@60 10 modify it under the terms of the GNU General Public License as
Chris@60 11 published by the Free Software Foundation; either version 2 of the
Chris@60 12 License, or (at your option) any later version. See the file
Chris@60 13 COPYING included with this distribution for more information.
Chris@60 14 */
Chris@60 15
Chris@60 16 #include "PluginParameterBox.h"
Chris@60 17
Chris@60 18 #include "AudioDial.h"
Chris@60 19
Chris@71 20 #include "plugin/PluginXml.h"
Chris@71 21
Chris@167 22 #include "base/RangeMapper.h"
Chris@167 23
Chris@60 24 #include <QDoubleSpinBox>
Chris@60 25 #include <QGridLayout>
Chris@63 26 #include <QComboBox>
Chris@63 27 #include <QCheckBox>
Chris@60 28 #include <QLayout>
Chris@60 29 #include <QLabel>
Chris@60 30
Chris@60 31 #include <iostream>
Chris@60 32 #include <string>
Chris@60 33
Chris@78 34 #include <cmath>
Chris@78 35
Chris@71 36 PluginParameterBox::PluginParameterBox(Vamp::PluginBase *plugin, QWidget *parent) :
Chris@62 37 QFrame(parent),
Chris@60 38 m_plugin(plugin)
Chris@60 39 {
Chris@60 40 m_layout = new QGridLayout;
Chris@60 41 setLayout(m_layout);
Chris@60 42 populate();
Chris@60 43 }
Chris@60 44
Chris@60 45 PluginParameterBox::~PluginParameterBox()
Chris@60 46 {
Chris@60 47 }
Chris@60 48
Chris@60 49 void
Chris@60 50 PluginParameterBox::populate()
Chris@60 51 {
Chris@71 52 Vamp::PluginBase::ParameterList params = m_plugin->getParameterDescriptors();
Chris@71 53 Vamp::PluginBase::ProgramList programs = m_plugin->getPrograms();
Chris@60 54
Chris@60 55 m_params.clear();
Chris@60 56
Chris@63 57 if (params.empty() && programs.empty()) {
Chris@62 58 m_layout->addWidget
Chris@62 59 (new QLabel(tr("This plugin has no adjustable parameters.")),
Chris@62 60 0, 0);
Chris@62 61 }
Chris@62 62
Chris@63 63 int offset = 0;
Chris@63 64
Chris@63 65 if (!programs.empty()) {
Chris@63 66
Chris@63 67 std::string currentProgram = m_plugin->getCurrentProgram();
Chris@63 68
Chris@63 69 QComboBox *programCombo = new QComboBox;
Chris@63 70 programCombo->setMaxVisibleItems(20);
Chris@63 71
Chris@63 72 for (int i = 0; i < programs.size(); ++i) {
Chris@63 73 programCombo->addItem(programs[i].c_str());
Chris@63 74 if (programs[i] == currentProgram) {
Chris@63 75 programCombo->setCurrentIndex(i);
Chris@63 76 }
Chris@63 77 }
Chris@63 78
Chris@63 79 m_layout->addWidget(new QLabel(tr("Program")), 0, 0);
Chris@63 80 m_layout->addWidget(programCombo, 0, 1, 1, 2);
Chris@63 81
Chris@63 82 connect(programCombo, SIGNAL(currentIndexChanged(const QString &)),
Chris@63 83 this, SLOT(programComboChanged(const QString &)));
Chris@63 84
Chris@63 85 offset = 1;
Chris@63 86 }
Chris@63 87
Chris@60 88 for (size_t i = 0; i < params.size(); ++i) {
Chris@60 89
Chris@207 90 QString identifier = params[i].identifier.c_str();
Chris@60 91 QString name = params[i].name.c_str();
Chris@60 92 QString unit = params[i].unit.c_str();
Chris@60 93
Chris@60 94 float min = params[i].minValue;
Chris@60 95 float max = params[i].maxValue;
Chris@60 96 float deft = params[i].defaultValue;
Chris@207 97 float value = m_plugin->getParameter(params[i].identifier);
Chris@60 98
Chris@60 99 float qtz = 0.0;
Chris@60 100 if (params[i].isQuantized) qtz = params[i].quantizeStep;
Chris@60 101
Chris@74 102 std::vector<std::string> valueNames = params[i].valueNames;
Chris@74 103
Chris@60 104 // construct an integer range
Chris@60 105
Chris@60 106 int imin = 0, imax = 100;
Chris@60 107
Chris@60 108 if (qtz > 0.0) {
Chris@60 109 imax = int((max - min) / qtz);
Chris@60 110 } else {
Chris@60 111 qtz = (max - min) / 100.0;
Chris@60 112 }
Chris@60 113
Chris@60 114 //!!! would be nice to ensure the default value corresponds to
Chris@60 115 // an integer!
Chris@60 116
Chris@207 117 QLabel *label = new QLabel(name);
Chris@208 118 if (params[i].description != "") {
Chris@208 119 label->setToolTip(params[i].description.c_str());
Chris@208 120 }
Chris@63 121 m_layout->addWidget(label, i + offset, 0);
Chris@60 122
Chris@60 123 ParamRec rec;
Chris@60 124 rec.param = params[i];
Chris@63 125 rec.dial = 0;
Chris@63 126 rec.spin = 0;
Chris@63 127 rec.check = 0;
Chris@74 128 rec.combo = 0;
Chris@63 129
Chris@74 130 if (params[i].isQuantized && !valueNames.empty()) {
Chris@74 131
Chris@74 132 QComboBox *combobox = new QComboBox;
Chris@207 133 combobox->setObjectName(identifier);
Chris@74 134 for (unsigned int j = 0; j < valueNames.size(); ++j) {
Chris@74 135 combobox->addItem(valueNames[j].c_str());
Chris@74 136 if (lrintf((value - min) / qtz) == j) {
Chris@74 137 combobox->setCurrentIndex(j);
Chris@74 138 }
Chris@74 139 }
Chris@74 140 connect(combobox, SIGNAL(activated(int)),
Chris@74 141 this, SLOT(dialChanged(int)));
Chris@74 142 m_layout->addWidget(combobox, i + offset, 1, 1, 2);
Chris@74 143 rec.combo = combobox;
Chris@74 144
Chris@74 145 } else if (min == 0.0 && max == 1.0 && qtz == 1.0) {
Chris@63 146
Chris@63 147 QCheckBox *checkbox = new QCheckBox;
Chris@207 148 checkbox->setObjectName(identifier);
Chris@63 149 checkbox->setCheckState(value == 0.0 ? Qt::Unchecked : Qt::Checked);
Chris@63 150 connect(checkbox, SIGNAL(stateChanged(int)),
Chris@63 151 this, SLOT(checkBoxChanged(int)));
Chris@63 152 m_layout->addWidget(checkbox, i + offset, 2);
Chris@63 153 rec.check = checkbox;
Chris@63 154
Chris@63 155 } else {
Chris@63 156
Chris@63 157 AudioDial *dial = new AudioDial;
Chris@207 158 dial->setObjectName(name);
Chris@63 159 dial->setMinimum(imin);
Chris@63 160 dial->setMaximum(imax);
Chris@63 161 dial->setPageStep(1);
Chris@63 162 dial->setNotchesVisible((imax - imin) <= 12);
Chris@74 163 dial->setDefaultValue(lrintf((deft - min) / qtz));
Chris@74 164 dial->setValue(lrintf((value - min) / qtz));
Chris@63 165 dial->setFixedWidth(32);
Chris@63 166 dial->setFixedHeight(32);
Chris@167 167 dial->setRangeMapper(new LinearRangeMapper
Chris@167 168 (imin, imax, min, max, unit));
Chris@168 169 dial->setShowToolTip(true);
Chris@63 170 connect(dial, SIGNAL(valueChanged(int)),
Chris@63 171 this, SLOT(dialChanged(int)));
Chris@63 172 m_layout->addWidget(dial, i + offset, 1);
Chris@63 173
Chris@63 174 QDoubleSpinBox *spinbox = new QDoubleSpinBox;
Chris@207 175 spinbox->setObjectName(identifier);
Chris@63 176 spinbox->setMinimum(min);
Chris@63 177 spinbox->setMaximum(max);
Chris@63 178 spinbox->setSuffix(QString(" %1").arg(unit));
Chris@63 179 spinbox->setSingleStep(qtz);
Chris@63 180 spinbox->setValue(value);
Chris@103 181 spinbox->setDecimals(4);
Chris@63 182 connect(spinbox, SIGNAL(valueChanged(double)),
Chris@63 183 this, SLOT(spinBoxChanged(double)));
Chris@63 184 m_layout->addWidget(spinbox, i + offset, 2);
Chris@63 185 rec.dial = dial;
Chris@63 186 rec.spin = spinbox;
Chris@63 187 }
Chris@63 188
Chris@207 189 m_params[identifier] = rec;
Chris@207 190 m_nameMap[name] = identifier;
Chris@60 191 }
Chris@60 192 }
Chris@60 193
Chris@60 194 void
Chris@60 195 PluginParameterBox::dialChanged(int ival)
Chris@60 196 {
Chris@60 197 QObject *obj = sender();
Chris@207 198 QString identifier = obj->objectName();
Chris@60 199
Chris@207 200 if (m_params.find(identifier) == m_params.end() &&
Chris@207 201 m_nameMap.find(identifier) != m_nameMap.end()) {
Chris@207 202 identifier = m_nameMap[identifier];
Chris@167 203 }
Chris@167 204
Chris@207 205 if (m_params.find(identifier) == m_params.end()) {
Chris@207 206 std::cerr << "WARNING: PluginParameterBox::dialChanged: Unknown parameter \"" << identifier.toStdString() << "\"" << std::endl;
Chris@60 207 return;
Chris@60 208 }
Chris@60 209
Chris@207 210 Vamp::PluginBase::ParameterDescriptor params = m_params[identifier].param;
Chris@60 211
Chris@60 212 float min = params.minValue;
Chris@60 213 float max = params.maxValue;
Chris@60 214
Chris@168 215 float newValue;
Chris@168 216
Chris@60 217 float qtz = 0.0;
Chris@60 218 if (params.isQuantized) qtz = params.quantizeStep;
Chris@168 219
Chris@168 220 AudioDial *ad = dynamic_cast<AudioDial *>(obj);
Chris@60 221
Chris@168 222 if (ad && ad->rangeMapper()) {
Chris@168 223
Chris@168 224 newValue = ad->mappedValue();
Chris@168 225 if (newValue < min) newValue = min;
Chris@168 226 if (newValue > max) newValue = max;
Chris@168 227 if (qtz != 0.0) {
Chris@168 228 ival = lrintf((newValue - min) / qtz);
Chris@168 229 newValue = min + ival * qtz;
Chris@168 230 }
Chris@168 231
Chris@168 232 } else {
Chris@168 233 if (qtz == 0.0) {
Chris@168 234 qtz = (max - min) / 100.0;
Chris@168 235 }
Chris@168 236 newValue = min + ival * qtz;
Chris@60 237 }
Chris@60 238
Chris@207 239 QDoubleSpinBox *spin = m_params[identifier].spin;
Chris@63 240 if (spin) {
Chris@63 241 spin->blockSignals(true);
Chris@63 242 spin->setValue(newValue);
Chris@63 243 spin->blockSignals(false);
Chris@63 244 }
Chris@60 245
Chris@207 246 m_plugin->setParameter(identifier.toStdString(), newValue);
Chris@64 247
Chris@71 248 emit pluginConfigurationChanged(PluginXml(m_plugin).toXmlString());
Chris@60 249 }
Chris@60 250
Chris@60 251 void
Chris@63 252 PluginParameterBox::checkBoxChanged(int state)
Chris@63 253 {
Chris@63 254 QObject *obj = sender();
Chris@207 255 QString identifier = obj->objectName();
Chris@63 256
Chris@207 257 if (m_params.find(identifier) == m_params.end() &&
Chris@207 258 m_nameMap.find(identifier) != m_nameMap.end()) {
Chris@207 259 identifier = m_nameMap[identifier];
Chris@167 260 }
Chris@167 261
Chris@207 262 if (m_params.find(identifier) == m_params.end()) {
Chris@207 263 std::cerr << "WARNING: PluginParameterBox::checkBoxChanged: Unknown parameter \"" << identifier.toStdString() << "\"" << std::endl;
Chris@63 264 return;
Chris@63 265 }
Chris@63 266
Chris@207 267 Vamp::PluginBase::ParameterDescriptor params = m_params[identifier].param;
Chris@63 268
Chris@207 269 if (state) m_plugin->setParameter(identifier.toStdString(), 1.0);
Chris@207 270 else m_plugin->setParameter(identifier.toStdString(), 0.0);
Chris@64 271
Chris@71 272 emit pluginConfigurationChanged(PluginXml(m_plugin).toXmlString());
Chris@63 273 }
Chris@63 274
Chris@63 275 void
Chris@60 276 PluginParameterBox::spinBoxChanged(double value)
Chris@60 277 {
Chris@60 278 QObject *obj = sender();
Chris@207 279 QString identifier = obj->objectName();
Chris@60 280
Chris@207 281 if (m_params.find(identifier) == m_params.end() &&
Chris@207 282 m_nameMap.find(identifier) != m_nameMap.end()) {
Chris@207 283 identifier = m_nameMap[identifier];
Chris@167 284 }
Chris@167 285
Chris@207 286 if (m_params.find(identifier) == m_params.end()) {
Chris@207 287 std::cerr << "WARNING: PluginParameterBox::spinBoxChanged: Unknown parameter \"" << identifier.toStdString() << "\"" << std::endl;
Chris@60 288 return;
Chris@60 289 }
Chris@60 290
Chris@207 291 Vamp::PluginBase::ParameterDescriptor params = m_params[identifier].param;
Chris@60 292
Chris@60 293 float min = params.minValue;
Chris@60 294 float max = params.maxValue;
Chris@60 295
Chris@60 296 float qtz = 0.0;
Chris@60 297 if (params.isQuantized) qtz = params.quantizeStep;
Chris@60 298
Chris@60 299 if (qtz > 0.0) {
Chris@60 300 int step = int((value - min) / qtz);
Chris@60 301 value = min + step * qtz;
Chris@60 302 }
Chris@60 303
Chris@60 304 int imin = 0, imax = 100;
Chris@60 305
Chris@60 306 if (qtz > 0.0) {
Chris@60 307 imax = int((max - min) / qtz);
Chris@60 308 } else {
Chris@60 309 qtz = (max - min) / 100.0;
Chris@60 310 }
Chris@60 311
Chris@60 312 int ival = (value - min) / qtz;
Chris@60 313
Chris@207 314 AudioDial *dial = m_params[identifier].dial;
Chris@63 315 if (dial) {
Chris@63 316 dial->blockSignals(true);
Chris@63 317 dial->setValue(ival);
Chris@63 318 dial->blockSignals(false);
Chris@63 319 }
Chris@60 320
Chris@207 321 m_plugin->setParameter(identifier.toStdString(), value);
Chris@64 322
Chris@71 323 emit pluginConfigurationChanged(PluginXml(m_plugin).toXmlString());
Chris@60 324 }
Chris@60 325
Chris@63 326 void
Chris@63 327 PluginParameterBox::programComboChanged(const QString &newProgram)
Chris@63 328 {
Chris@63 329 m_plugin->selectProgram(newProgram.toStdString());
Chris@60 330
Chris@63 331 for (std::map<QString, ParamRec>::iterator i = m_params.begin();
Chris@63 332 i != m_params.end(); ++i) {
Chris@63 333
Chris@71 334 Vamp::PluginBase::ParameterDescriptor &param = i->second.param;
Chris@207 335 float value = m_plugin->getParameter(param.identifier);
Chris@63 336
Chris@63 337 if (i->second.spin) {
Chris@63 338 i->second.spin->blockSignals(true);
Chris@63 339 i->second.spin->setValue(value);
Chris@63 340 i->second.spin->blockSignals(false);
Chris@63 341 }
Chris@63 342
Chris@63 343 if (i->second.dial) {
Chris@63 344
Chris@63 345 float min = param.minValue;
Chris@63 346 float max = param.maxValue;
Chris@63 347
Chris@63 348 float qtz = 0.0;
Chris@63 349 if (param.isQuantized) qtz = param.quantizeStep;
Chris@63 350
Chris@63 351 if (qtz == 0.0) {
Chris@63 352 qtz = (max - min) / 100.0;
Chris@63 353 }
Chris@63 354
Chris@63 355 i->second.dial->blockSignals(true);
Chris@74 356 i->second.dial->setValue(lrintf((value - min) / qtz));
Chris@63 357 i->second.dial->blockSignals(false);
Chris@63 358 }
Chris@74 359
Chris@74 360 if (i->second.combo) {
Chris@74 361 i->second.combo->blockSignals(true);
Chris@74 362 i->second.combo->setCurrentIndex(value);
Chris@74 363 i->second.combo->blockSignals(false);
Chris@74 364 }
Chris@63 365 }
Chris@64 366
Chris@71 367 emit pluginConfigurationChanged(PluginXml(m_plugin).toXmlString());
Chris@63 368 }
Chris@63 369