annotate widgets/PluginParameterBox.cpp @ 167:53b9c7656798

* Use RangeMappers in various places in plugin parameters, layer properties, playback parameters &c
author Chris Cannam
date Mon, 16 Oct 2006 20:42:23 +0000
parents 5064eeb1c76f
children d4be66d61c04
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@60 7 This file copyright 2006 Chris Cannam.
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@60 90 QString name = params[i].name.c_str();
Chris@60 91 QString description = params[i].description.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@62 97 float value = m_plugin->getParameter(params[i].name);
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@60 117 QLabel *label = new QLabel(description);
Chris@63 118 m_layout->addWidget(label, i + offset, 0);
Chris@60 119
Chris@60 120 ParamRec rec;
Chris@60 121 rec.param = params[i];
Chris@63 122 rec.dial = 0;
Chris@63 123 rec.spin = 0;
Chris@63 124 rec.check = 0;
Chris@74 125 rec.combo = 0;
Chris@63 126
Chris@74 127 if (params[i].isQuantized && !valueNames.empty()) {
Chris@74 128
Chris@74 129 QComboBox *combobox = new QComboBox;
Chris@74 130 combobox->setObjectName(name);
Chris@74 131 for (unsigned int j = 0; j < valueNames.size(); ++j) {
Chris@74 132 combobox->addItem(valueNames[j].c_str());
Chris@74 133 if (lrintf((value - min) / qtz) == j) {
Chris@74 134 combobox->setCurrentIndex(j);
Chris@74 135 }
Chris@74 136 }
Chris@74 137 connect(combobox, SIGNAL(activated(int)),
Chris@74 138 this, SLOT(dialChanged(int)));
Chris@74 139 m_layout->addWidget(combobox, i + offset, 1, 1, 2);
Chris@74 140 rec.combo = combobox;
Chris@74 141
Chris@74 142 } else if (min == 0.0 && max == 1.0 && qtz == 1.0) {
Chris@63 143
Chris@63 144 QCheckBox *checkbox = new QCheckBox;
Chris@63 145 checkbox->setObjectName(name);
Chris@63 146 checkbox->setCheckState(value == 0.0 ? Qt::Unchecked : Qt::Checked);
Chris@63 147 connect(checkbox, SIGNAL(stateChanged(int)),
Chris@63 148 this, SLOT(checkBoxChanged(int)));
Chris@63 149 m_layout->addWidget(checkbox, i + offset, 2);
Chris@63 150 rec.check = checkbox;
Chris@63 151
Chris@63 152 } else {
Chris@63 153
Chris@63 154 AudioDial *dial = new AudioDial;
Chris@167 155 dial->setObjectName(description);
Chris@63 156 dial->setMinimum(imin);
Chris@63 157 dial->setMaximum(imax);
Chris@63 158 dial->setPageStep(1);
Chris@63 159 dial->setNotchesVisible((imax - imin) <= 12);
Chris@74 160 dial->setDefaultValue(lrintf((deft - min) / qtz));
Chris@74 161 dial->setValue(lrintf((value - min) / qtz));
Chris@63 162 dial->setFixedWidth(32);
Chris@63 163 dial->setFixedHeight(32);
Chris@167 164 dial->setRangeMapper(new LinearRangeMapper
Chris@167 165 (imin, imax, min, max, unit));
Chris@63 166 connect(dial, SIGNAL(valueChanged(int)),
Chris@63 167 this, SLOT(dialChanged(int)));
Chris@63 168 m_layout->addWidget(dial, i + offset, 1);
Chris@63 169
Chris@63 170 QDoubleSpinBox *spinbox = new QDoubleSpinBox;
Chris@63 171 spinbox->setObjectName(name);
Chris@63 172 spinbox->setMinimum(min);
Chris@63 173 spinbox->setMaximum(max);
Chris@63 174 spinbox->setSuffix(QString(" %1").arg(unit));
Chris@63 175 spinbox->setSingleStep(qtz);
Chris@63 176 spinbox->setValue(value);
Chris@103 177 spinbox->setDecimals(4);
Chris@63 178 connect(spinbox, SIGNAL(valueChanged(double)),
Chris@63 179 this, SLOT(spinBoxChanged(double)));
Chris@63 180 m_layout->addWidget(spinbox, i + offset, 2);
Chris@63 181 rec.dial = dial;
Chris@63 182 rec.spin = spinbox;
Chris@63 183 }
Chris@63 184
Chris@60 185 m_params[name] = rec;
Chris@167 186 m_descriptionMap[description] = name;
Chris@60 187 }
Chris@60 188 }
Chris@60 189
Chris@60 190 void
Chris@60 191 PluginParameterBox::dialChanged(int ival)
Chris@60 192 {
Chris@60 193 QObject *obj = sender();
Chris@60 194 QString name = obj->objectName();
Chris@60 195
Chris@167 196 if (m_params.find(name) == m_params.end() &&
Chris@167 197 m_descriptionMap.find(name) != m_descriptionMap.end()) {
Chris@167 198 name = m_descriptionMap[name];
Chris@167 199 }
Chris@167 200
Chris@60 201 if (m_params.find(name) == m_params.end()) {
Chris@60 202 std::cerr << "WARNING: PluginParameterBox::dialChanged: Unknown parameter \"" << name.toStdString() << "\"" << std::endl;
Chris@60 203 return;
Chris@60 204 }
Chris@60 205
Chris@71 206 Vamp::PluginBase::ParameterDescriptor params = m_params[name].param;
Chris@60 207
Chris@60 208 float min = params.minValue;
Chris@60 209 float max = params.maxValue;
Chris@60 210
Chris@60 211 float qtz = 0.0;
Chris@60 212 if (params.isQuantized) qtz = params.quantizeStep;
Chris@60 213
Chris@60 214 if (qtz == 0.0) {
Chris@60 215 qtz = (max - min) / 100.0;
Chris@60 216 }
Chris@60 217
Chris@60 218 float newValue = min + ival * qtz;
Chris@60 219
Chris@60 220 QDoubleSpinBox *spin = m_params[name].spin;
Chris@63 221 if (spin) {
Chris@63 222 spin->blockSignals(true);
Chris@63 223 spin->setValue(newValue);
Chris@63 224 spin->blockSignals(false);
Chris@63 225 }
Chris@60 226
Chris@60 227 m_plugin->setParameter(name.toStdString(), newValue);
Chris@64 228
Chris@71 229 emit pluginConfigurationChanged(PluginXml(m_plugin).toXmlString());
Chris@60 230 }
Chris@60 231
Chris@60 232 void
Chris@63 233 PluginParameterBox::checkBoxChanged(int state)
Chris@63 234 {
Chris@63 235 QObject *obj = sender();
Chris@63 236 QString name = obj->objectName();
Chris@63 237
Chris@167 238 if (m_params.find(name) == m_params.end() &&
Chris@167 239 m_descriptionMap.find(name) != m_descriptionMap.end()) {
Chris@167 240 name = m_descriptionMap[name];
Chris@167 241 }
Chris@167 242
Chris@63 243 if (m_params.find(name) == m_params.end()) {
Chris@63 244 std::cerr << "WARNING: PluginParameterBox::checkBoxChanged: Unknown parameter \"" << name.toStdString() << "\"" << std::endl;
Chris@63 245 return;
Chris@63 246 }
Chris@63 247
Chris@71 248 Vamp::PluginBase::ParameterDescriptor params = m_params[name].param;
Chris@63 249
Chris@63 250 if (state) m_plugin->setParameter(name.toStdString(), 1.0);
Chris@63 251 else m_plugin->setParameter(name.toStdString(), 0.0);
Chris@64 252
Chris@71 253 emit pluginConfigurationChanged(PluginXml(m_plugin).toXmlString());
Chris@63 254 }
Chris@63 255
Chris@63 256 void
Chris@60 257 PluginParameterBox::spinBoxChanged(double value)
Chris@60 258 {
Chris@60 259 QObject *obj = sender();
Chris@60 260 QString name = obj->objectName();
Chris@60 261
Chris@167 262 if (m_params.find(name) == m_params.end() &&
Chris@167 263 m_descriptionMap.find(name) != m_descriptionMap.end()) {
Chris@167 264 name = m_descriptionMap[name];
Chris@167 265 }
Chris@167 266
Chris@60 267 if (m_params.find(name) == m_params.end()) {
Chris@60 268 std::cerr << "WARNING: PluginParameterBox::spinBoxChanged: Unknown parameter \"" << name.toStdString() << "\"" << std::endl;
Chris@60 269 return;
Chris@60 270 }
Chris@60 271
Chris@71 272 Vamp::PluginBase::ParameterDescriptor params = m_params[name].param;
Chris@60 273
Chris@60 274 float min = params.minValue;
Chris@60 275 float max = params.maxValue;
Chris@60 276
Chris@60 277 float qtz = 0.0;
Chris@60 278 if (params.isQuantized) qtz = params.quantizeStep;
Chris@60 279
Chris@60 280 if (qtz > 0.0) {
Chris@60 281 int step = int((value - min) / qtz);
Chris@60 282 value = min + step * qtz;
Chris@60 283 }
Chris@60 284
Chris@60 285 int imin = 0, imax = 100;
Chris@60 286
Chris@60 287 if (qtz > 0.0) {
Chris@60 288 imax = int((max - min) / qtz);
Chris@60 289 } else {
Chris@60 290 qtz = (max - min) / 100.0;
Chris@60 291 }
Chris@60 292
Chris@60 293 int ival = (value - min) / qtz;
Chris@60 294
Chris@60 295 AudioDial *dial = m_params[name].dial;
Chris@63 296 if (dial) {
Chris@63 297 dial->blockSignals(true);
Chris@63 298 dial->setValue(ival);
Chris@63 299 dial->blockSignals(false);
Chris@63 300 }
Chris@60 301
Chris@60 302 m_plugin->setParameter(name.toStdString(), value);
Chris@64 303
Chris@71 304 emit pluginConfigurationChanged(PluginXml(m_plugin).toXmlString());
Chris@60 305 }
Chris@60 306
Chris@63 307 void
Chris@63 308 PluginParameterBox::programComboChanged(const QString &newProgram)
Chris@63 309 {
Chris@63 310 m_plugin->selectProgram(newProgram.toStdString());
Chris@60 311
Chris@63 312 for (std::map<QString, ParamRec>::iterator i = m_params.begin();
Chris@63 313 i != m_params.end(); ++i) {
Chris@63 314
Chris@71 315 Vamp::PluginBase::ParameterDescriptor &param = i->second.param;
Chris@63 316 float value = m_plugin->getParameter(param.name);
Chris@63 317
Chris@63 318 if (i->second.spin) {
Chris@63 319 i->second.spin->blockSignals(true);
Chris@63 320 i->second.spin->setValue(value);
Chris@63 321 i->second.spin->blockSignals(false);
Chris@63 322 }
Chris@63 323
Chris@63 324 if (i->second.dial) {
Chris@63 325
Chris@63 326 float min = param.minValue;
Chris@63 327 float max = param.maxValue;
Chris@63 328
Chris@63 329 float qtz = 0.0;
Chris@63 330 if (param.isQuantized) qtz = param.quantizeStep;
Chris@63 331
Chris@63 332 if (qtz == 0.0) {
Chris@63 333 qtz = (max - min) / 100.0;
Chris@63 334 }
Chris@63 335
Chris@63 336 i->second.dial->blockSignals(true);
Chris@74 337 i->second.dial->setValue(lrintf((value - min) / qtz));
Chris@63 338 i->second.dial->blockSignals(false);
Chris@63 339 }
Chris@74 340
Chris@74 341 if (i->second.combo) {
Chris@74 342 i->second.combo->blockSignals(true);
Chris@74 343 i->second.combo->setCurrentIndex(value);
Chris@74 344 i->second.combo->blockSignals(false);
Chris@74 345 }
Chris@63 346 }
Chris@64 347
Chris@71 348 emit pluginConfigurationChanged(PluginXml(m_plugin).toXmlString());
Chris@63 349 }
Chris@63 350