annotate widgets/PluginParameterBox.cpp @ 60:4df1a479bf10

* Add plugin parameter box widget
author Chris Cannam
date Mon, 20 Mar 2006 18:18:30 +0000
parents
children 50429a56680f
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@60 20 #include <QDoubleSpinBox>
Chris@60 21 #include <QGridLayout>
Chris@60 22 #include <QLayout>
Chris@60 23 #include <QLabel>
Chris@60 24
Chris@60 25 #include <iostream>
Chris@60 26 #include <string>
Chris@60 27
Chris@60 28 PluginParameterBox::PluginParameterBox(PluginInstance *plugin) :
Chris@60 29 m_plugin(plugin)
Chris@60 30 {
Chris@60 31 m_layout = new QGridLayout;
Chris@60 32 setLayout(m_layout);
Chris@60 33 populate();
Chris@60 34 }
Chris@60 35
Chris@60 36 PluginParameterBox::~PluginParameterBox()
Chris@60 37 {
Chris@60 38 }
Chris@60 39
Chris@60 40 void
Chris@60 41 PluginParameterBox::populate()
Chris@60 42 {
Chris@60 43 PluginInstance::ParameterList params = m_plugin->getParameterDescriptors();
Chris@60 44
Chris@60 45 m_params.clear();
Chris@60 46
Chris@60 47 for (size_t i = 0; i < params.size(); ++i) {
Chris@60 48
Chris@60 49 QString name = params[i].name.c_str();
Chris@60 50 QString description = params[i].description.c_str();
Chris@60 51 QString unit = params[i].unit.c_str();
Chris@60 52
Chris@60 53 float min = params[i].minValue;
Chris@60 54 float max = params[i].maxValue;
Chris@60 55 float deft = params[i].defaultValue;
Chris@60 56
Chris@60 57 float qtz = 0.0;
Chris@60 58 if (params[i].isQuantized) qtz = params[i].quantizeStep;
Chris@60 59
Chris@60 60 // construct an integer range
Chris@60 61
Chris@60 62 int imin = 0, imax = 100;
Chris@60 63
Chris@60 64 if (qtz > 0.0) {
Chris@60 65 imax = int((max - min) / qtz);
Chris@60 66 } else {
Chris@60 67 qtz = (max - min) / 100.0;
Chris@60 68 }
Chris@60 69
Chris@60 70 //!!! would be nice to ensure the default value corresponds to
Chris@60 71 // an integer!
Chris@60 72
Chris@60 73 QLabel *label = new QLabel(description);
Chris@60 74 m_layout->addWidget(label, i, 0);
Chris@60 75
Chris@60 76 AudioDial *dial = new AudioDial;
Chris@60 77 dial->setObjectName(name);
Chris@60 78 dial->setMinimum(imin);
Chris@60 79 dial->setMaximum(imax);
Chris@60 80 dial->setPageStep(1);
Chris@60 81 dial->setNotchesVisible((imax - imin) <= 12);
Chris@60 82 dial->setDefaultValue(int((deft - min) / qtz));
Chris@60 83 connect(dial, SIGNAL(valueChanged(int)),
Chris@60 84 this, SLOT(dialChanged(int)));
Chris@60 85 m_layout->addWidget(dial, i, 1);
Chris@60 86
Chris@60 87 QDoubleSpinBox *spinbox = new QDoubleSpinBox;
Chris@60 88 spinbox->setObjectName(name);
Chris@60 89 spinbox->setMinimum(min);
Chris@60 90 spinbox->setMaximum(max);
Chris@60 91 spinbox->setSuffix(unit);
Chris@60 92 spinbox->setSingleStep(qtz);
Chris@60 93 spinbox->setValue(deft);
Chris@60 94 connect(spinbox, SIGNAL(valueChanged(double)),
Chris@60 95 this, SLOT(spinBoxChanged(double)));
Chris@60 96 m_layout->addWidget(spinbox, i, 2);
Chris@60 97
Chris@60 98 ParamRec rec;
Chris@60 99 rec.dial = dial;
Chris@60 100 rec.spin = spinbox;
Chris@60 101 rec.param = params[i];
Chris@60 102 m_params[name] = rec;
Chris@60 103 }
Chris@60 104 }
Chris@60 105
Chris@60 106 void
Chris@60 107 PluginParameterBox::dialChanged(int ival)
Chris@60 108 {
Chris@60 109 QObject *obj = sender();
Chris@60 110 QString name = obj->objectName();
Chris@60 111
Chris@60 112 if (m_params.find(name) == m_params.end()) {
Chris@60 113 std::cerr << "WARNING: PluginParameterBox::dialChanged: Unknown parameter \"" << name.toStdString() << "\"" << std::endl;
Chris@60 114 return;
Chris@60 115 }
Chris@60 116
Chris@60 117 PluginInstance::ParameterDescriptor params = m_params[name].param;
Chris@60 118
Chris@60 119 float min = params.minValue;
Chris@60 120 float max = params.maxValue;
Chris@60 121
Chris@60 122 float qtz = 0.0;
Chris@60 123 if (params.isQuantized) qtz = params.quantizeStep;
Chris@60 124
Chris@60 125 if (qtz == 0.0) {
Chris@60 126 qtz = (max - min) / 100.0;
Chris@60 127 }
Chris@60 128
Chris@60 129 float newValue = min + ival * qtz;
Chris@60 130
Chris@60 131 QDoubleSpinBox *spin = m_params[name].spin;
Chris@60 132 spin->blockSignals(true);
Chris@60 133 spin->setValue(newValue);
Chris@60 134 spin->blockSignals(false);
Chris@60 135
Chris@60 136 m_plugin->setParameter(name.toStdString(), newValue);
Chris@60 137 }
Chris@60 138
Chris@60 139 void
Chris@60 140 PluginParameterBox::spinBoxChanged(double value)
Chris@60 141 {
Chris@60 142 QObject *obj = sender();
Chris@60 143 QString name = obj->objectName();
Chris@60 144
Chris@60 145 if (m_params.find(name) == m_params.end()) {
Chris@60 146 std::cerr << "WARNING: PluginParameterBox::spinBoxChanged: Unknown parameter \"" << name.toStdString() << "\"" << std::endl;
Chris@60 147 return;
Chris@60 148 }
Chris@60 149
Chris@60 150 PluginInstance::ParameterDescriptor params = m_params[name].param;
Chris@60 151
Chris@60 152 float min = params.minValue;
Chris@60 153 float max = params.maxValue;
Chris@60 154
Chris@60 155 float qtz = 0.0;
Chris@60 156 if (params.isQuantized) qtz = params.quantizeStep;
Chris@60 157
Chris@60 158 if (qtz > 0.0) {
Chris@60 159 int step = int((value - min) / qtz);
Chris@60 160 value = min + step * qtz;
Chris@60 161 }
Chris@60 162
Chris@60 163 int imin = 0, imax = 100;
Chris@60 164
Chris@60 165 if (qtz > 0.0) {
Chris@60 166 imax = int((max - min) / qtz);
Chris@60 167 } else {
Chris@60 168 qtz = (max - min) / 100.0;
Chris@60 169 }
Chris@60 170
Chris@60 171 int ival = (value - min) / qtz;
Chris@60 172
Chris@60 173 AudioDial *dial = m_params[name].dial;
Chris@60 174 dial->blockSignals(true);
Chris@60 175 dial->setValue(ival);
Chris@60 176 dial->blockSignals(false);
Chris@60 177
Chris@60 178 m_plugin->setParameter(name.toStdString(), value);
Chris@60 179 }
Chris@60 180
Chris@60 181