Chris@60: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@60: Chris@60: /* Chris@60: Sonic Visualiser Chris@60: An audio file viewer and annotation editor. Chris@60: Centre for Digital Music, Queen Mary, University of London. Chris@60: This file copyright 2006 Chris Cannam. Chris@60: Chris@60: This program is free software; you can redistribute it and/or Chris@60: modify it under the terms of the GNU General Public License as Chris@60: published by the Free Software Foundation; either version 2 of the Chris@60: License, or (at your option) any later version. See the file Chris@60: COPYING included with this distribution for more information. Chris@60: */ Chris@60: Chris@60: #include "PluginParameterBox.h" Chris@60: Chris@60: #include "AudioDial.h" Chris@60: Chris@60: #include Chris@60: #include Chris@60: #include Chris@60: #include Chris@60: Chris@60: #include Chris@60: #include Chris@60: Chris@60: PluginParameterBox::PluginParameterBox(PluginInstance *plugin) : Chris@60: m_plugin(plugin) Chris@60: { Chris@60: m_layout = new QGridLayout; Chris@60: setLayout(m_layout); Chris@60: populate(); Chris@60: } Chris@60: Chris@60: PluginParameterBox::~PluginParameterBox() Chris@60: { Chris@60: } Chris@60: Chris@60: void Chris@60: PluginParameterBox::populate() Chris@60: { Chris@60: PluginInstance::ParameterList params = m_plugin->getParameterDescriptors(); Chris@60: Chris@60: m_params.clear(); Chris@60: Chris@60: for (size_t i = 0; i < params.size(); ++i) { Chris@60: Chris@60: QString name = params[i].name.c_str(); Chris@60: QString description = params[i].description.c_str(); Chris@60: QString unit = params[i].unit.c_str(); Chris@60: Chris@60: float min = params[i].minValue; Chris@60: float max = params[i].maxValue; Chris@60: float deft = params[i].defaultValue; Chris@60: Chris@60: float qtz = 0.0; Chris@60: if (params[i].isQuantized) qtz = params[i].quantizeStep; Chris@60: Chris@60: // construct an integer range Chris@60: Chris@60: int imin = 0, imax = 100; Chris@60: Chris@60: if (qtz > 0.0) { Chris@60: imax = int((max - min) / qtz); Chris@60: } else { Chris@60: qtz = (max - min) / 100.0; Chris@60: } Chris@60: Chris@60: //!!! would be nice to ensure the default value corresponds to Chris@60: // an integer! Chris@60: Chris@60: QLabel *label = new QLabel(description); Chris@60: m_layout->addWidget(label, i, 0); Chris@60: Chris@60: AudioDial *dial = new AudioDial; Chris@60: dial->setObjectName(name); Chris@60: dial->setMinimum(imin); Chris@60: dial->setMaximum(imax); Chris@60: dial->setPageStep(1); Chris@60: dial->setNotchesVisible((imax - imin) <= 12); Chris@60: dial->setDefaultValue(int((deft - min) / qtz)); Chris@60: connect(dial, SIGNAL(valueChanged(int)), Chris@60: this, SLOT(dialChanged(int))); Chris@60: m_layout->addWidget(dial, i, 1); Chris@60: Chris@60: QDoubleSpinBox *spinbox = new QDoubleSpinBox; Chris@60: spinbox->setObjectName(name); Chris@60: spinbox->setMinimum(min); Chris@60: spinbox->setMaximum(max); Chris@60: spinbox->setSuffix(unit); Chris@60: spinbox->setSingleStep(qtz); Chris@60: spinbox->setValue(deft); Chris@60: connect(spinbox, SIGNAL(valueChanged(double)), Chris@60: this, SLOT(spinBoxChanged(double))); Chris@60: m_layout->addWidget(spinbox, i, 2); Chris@60: Chris@60: ParamRec rec; Chris@60: rec.dial = dial; Chris@60: rec.spin = spinbox; Chris@60: rec.param = params[i]; Chris@60: m_params[name] = rec; Chris@60: } Chris@60: } Chris@60: Chris@60: void Chris@60: PluginParameterBox::dialChanged(int ival) Chris@60: { Chris@60: QObject *obj = sender(); Chris@60: QString name = obj->objectName(); Chris@60: Chris@60: if (m_params.find(name) == m_params.end()) { Chris@60: std::cerr << "WARNING: PluginParameterBox::dialChanged: Unknown parameter \"" << name.toStdString() << "\"" << std::endl; Chris@60: return; Chris@60: } Chris@60: Chris@60: PluginInstance::ParameterDescriptor params = m_params[name].param; Chris@60: Chris@60: float min = params.minValue; Chris@60: float max = params.maxValue; Chris@60: Chris@60: float qtz = 0.0; Chris@60: if (params.isQuantized) qtz = params.quantizeStep; Chris@60: Chris@60: if (qtz == 0.0) { Chris@60: qtz = (max - min) / 100.0; Chris@60: } Chris@60: Chris@60: float newValue = min + ival * qtz; Chris@60: Chris@60: QDoubleSpinBox *spin = m_params[name].spin; Chris@60: spin->blockSignals(true); Chris@60: spin->setValue(newValue); Chris@60: spin->blockSignals(false); Chris@60: Chris@60: m_plugin->setParameter(name.toStdString(), newValue); Chris@60: } Chris@60: Chris@60: void Chris@60: PluginParameterBox::spinBoxChanged(double value) Chris@60: { Chris@60: QObject *obj = sender(); Chris@60: QString name = obj->objectName(); Chris@60: Chris@60: if (m_params.find(name) == m_params.end()) { Chris@60: std::cerr << "WARNING: PluginParameterBox::spinBoxChanged: Unknown parameter \"" << name.toStdString() << "\"" << std::endl; Chris@60: return; Chris@60: } Chris@60: Chris@60: PluginInstance::ParameterDescriptor params = m_params[name].param; Chris@60: Chris@60: float min = params.minValue; Chris@60: float max = params.maxValue; Chris@60: Chris@60: float qtz = 0.0; Chris@60: if (params.isQuantized) qtz = params.quantizeStep; Chris@60: Chris@60: if (qtz > 0.0) { Chris@60: int step = int((value - min) / qtz); Chris@60: value = min + step * qtz; Chris@60: } Chris@60: Chris@60: int imin = 0, imax = 100; Chris@60: Chris@60: if (qtz > 0.0) { Chris@60: imax = int((max - min) / qtz); Chris@60: } else { Chris@60: qtz = (max - min) / 100.0; Chris@60: } Chris@60: Chris@60: int ival = (value - min) / qtz; Chris@60: Chris@60: AudioDial *dial = m_params[name].dial; Chris@60: dial->blockSignals(true); Chris@60: dial->setValue(ival); Chris@60: dial->blockSignals(false); Chris@60: Chris@60: m_plugin->setParameter(name.toStdString(), value); Chris@60: } Chris@60: Chris@60: