Mercurial > hg > svgui
comparison 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 |
comparison
equal
deleted
inserted
replaced
59:705f05ab42e3 | 60:4df1a479bf10 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 This file copyright 2006 Chris Cannam. | |
8 | |
9 This program is free software; you can redistribute it and/or | |
10 modify it under the terms of the GNU General Public License as | |
11 published by the Free Software Foundation; either version 2 of the | |
12 License, or (at your option) any later version. See the file | |
13 COPYING included with this distribution for more information. | |
14 */ | |
15 | |
16 #include "PluginParameterBox.h" | |
17 | |
18 #include "AudioDial.h" | |
19 | |
20 #include <QDoubleSpinBox> | |
21 #include <QGridLayout> | |
22 #include <QLayout> | |
23 #include <QLabel> | |
24 | |
25 #include <iostream> | |
26 #include <string> | |
27 | |
28 PluginParameterBox::PluginParameterBox(PluginInstance *plugin) : | |
29 m_plugin(plugin) | |
30 { | |
31 m_layout = new QGridLayout; | |
32 setLayout(m_layout); | |
33 populate(); | |
34 } | |
35 | |
36 PluginParameterBox::~PluginParameterBox() | |
37 { | |
38 } | |
39 | |
40 void | |
41 PluginParameterBox::populate() | |
42 { | |
43 PluginInstance::ParameterList params = m_plugin->getParameterDescriptors(); | |
44 | |
45 m_params.clear(); | |
46 | |
47 for (size_t i = 0; i < params.size(); ++i) { | |
48 | |
49 QString name = params[i].name.c_str(); | |
50 QString description = params[i].description.c_str(); | |
51 QString unit = params[i].unit.c_str(); | |
52 | |
53 float min = params[i].minValue; | |
54 float max = params[i].maxValue; | |
55 float deft = params[i].defaultValue; | |
56 | |
57 float qtz = 0.0; | |
58 if (params[i].isQuantized) qtz = params[i].quantizeStep; | |
59 | |
60 // construct an integer range | |
61 | |
62 int imin = 0, imax = 100; | |
63 | |
64 if (qtz > 0.0) { | |
65 imax = int((max - min) / qtz); | |
66 } else { | |
67 qtz = (max - min) / 100.0; | |
68 } | |
69 | |
70 //!!! would be nice to ensure the default value corresponds to | |
71 // an integer! | |
72 | |
73 QLabel *label = new QLabel(description); | |
74 m_layout->addWidget(label, i, 0); | |
75 | |
76 AudioDial *dial = new AudioDial; | |
77 dial->setObjectName(name); | |
78 dial->setMinimum(imin); | |
79 dial->setMaximum(imax); | |
80 dial->setPageStep(1); | |
81 dial->setNotchesVisible((imax - imin) <= 12); | |
82 dial->setDefaultValue(int((deft - min) / qtz)); | |
83 connect(dial, SIGNAL(valueChanged(int)), | |
84 this, SLOT(dialChanged(int))); | |
85 m_layout->addWidget(dial, i, 1); | |
86 | |
87 QDoubleSpinBox *spinbox = new QDoubleSpinBox; | |
88 spinbox->setObjectName(name); | |
89 spinbox->setMinimum(min); | |
90 spinbox->setMaximum(max); | |
91 spinbox->setSuffix(unit); | |
92 spinbox->setSingleStep(qtz); | |
93 spinbox->setValue(deft); | |
94 connect(spinbox, SIGNAL(valueChanged(double)), | |
95 this, SLOT(spinBoxChanged(double))); | |
96 m_layout->addWidget(spinbox, i, 2); | |
97 | |
98 ParamRec rec; | |
99 rec.dial = dial; | |
100 rec.spin = spinbox; | |
101 rec.param = params[i]; | |
102 m_params[name] = rec; | |
103 } | |
104 } | |
105 | |
106 void | |
107 PluginParameterBox::dialChanged(int ival) | |
108 { | |
109 QObject *obj = sender(); | |
110 QString name = obj->objectName(); | |
111 | |
112 if (m_params.find(name) == m_params.end()) { | |
113 std::cerr << "WARNING: PluginParameterBox::dialChanged: Unknown parameter \"" << name.toStdString() << "\"" << std::endl; | |
114 return; | |
115 } | |
116 | |
117 PluginInstance::ParameterDescriptor params = m_params[name].param; | |
118 | |
119 float min = params.minValue; | |
120 float max = params.maxValue; | |
121 | |
122 float qtz = 0.0; | |
123 if (params.isQuantized) qtz = params.quantizeStep; | |
124 | |
125 if (qtz == 0.0) { | |
126 qtz = (max - min) / 100.0; | |
127 } | |
128 | |
129 float newValue = min + ival * qtz; | |
130 | |
131 QDoubleSpinBox *spin = m_params[name].spin; | |
132 spin->blockSignals(true); | |
133 spin->setValue(newValue); | |
134 spin->blockSignals(false); | |
135 | |
136 m_plugin->setParameter(name.toStdString(), newValue); | |
137 } | |
138 | |
139 void | |
140 PluginParameterBox::spinBoxChanged(double value) | |
141 { | |
142 QObject *obj = sender(); | |
143 QString name = obj->objectName(); | |
144 | |
145 if (m_params.find(name) == m_params.end()) { | |
146 std::cerr << "WARNING: PluginParameterBox::spinBoxChanged: Unknown parameter \"" << name.toStdString() << "\"" << std::endl; | |
147 return; | |
148 } | |
149 | |
150 PluginInstance::ParameterDescriptor params = m_params[name].param; | |
151 | |
152 float min = params.minValue; | |
153 float max = params.maxValue; | |
154 | |
155 float qtz = 0.0; | |
156 if (params.isQuantized) qtz = params.quantizeStep; | |
157 | |
158 if (qtz > 0.0) { | |
159 int step = int((value - min) / qtz); | |
160 value = min + step * qtz; | |
161 } | |
162 | |
163 int imin = 0, imax = 100; | |
164 | |
165 if (qtz > 0.0) { | |
166 imax = int((max - min) / qtz); | |
167 } else { | |
168 qtz = (max - min) / 100.0; | |
169 } | |
170 | |
171 int ival = (value - min) / qtz; | |
172 | |
173 AudioDial *dial = m_params[name].dial; | |
174 dial->blockSignals(true); | |
175 dial->setValue(ival); | |
176 dial->blockSignals(false); | |
177 | |
178 m_plugin->setParameter(name.toStdString(), value); | |
179 } | |
180 | |
181 |