summaryrefslogtreecommitdiffstats
path: root/src/gui/widgets/PluginControl.cpp
blob: acf33ea4bc46c72144f60a919eab62eff1ce2b88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <[email protected]>,
        Chris Cannam        <[email protected]>,
        Richard Bown        <[email protected]>
 
    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.
 
    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/


#include "PluginControl.h"
#include "Rotary.h"
#include "misc/Strings.h"
#include "base/AudioPluginInstance.h"
#include "gui/general/GUIPalette.h"
#include "gui/studio/AudioPluginManager.h"
#include "gui/widgets/Rotary.h"
#include <qfont.h>
#include <qhbox.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qobject.h>
#include <qstring.h>
#include <qwidget.h>
#include <cmath>


namespace Rosegarden
{

PluginControl::PluginControl(QWidget *parent,
                             QGridLayout *layout,
                             ControlType type,
                             PluginPort *port,
                             AudioPluginManager *aPM,
                             int index,
                             float initialValue,
                             bool showBounds,
                             bool hidden):
        QObject(parent),
        m_layout(layout),
        m_type(type),
        m_port(port),
        m_pluginManager(aPM),
        m_index(index)
{
    QFont plainFont;
    plainFont.setPointSize((plainFont.pointSize() * 9 ) / 10);

    QLabel *controlTitle =
        new QLabel(QString("%1    ").arg(strtoqstr(port->getName())), parent);
    controlTitle->setFont(plainFont);

    if (type == Rotary) {
        float lowerBound = port->getLowerBound();
        float upperBound = port->getUpperBound();
        // Default value was already handled when calling this constructor

        if (lowerBound > upperBound) {
            float swap = upperBound;
            upperBound = lowerBound;
            lowerBound = swap;
        }

        float step = (upperBound - lowerBound) / 100.0;
        float pageStep = step * 10.f;
        Rotary::TickMode ticks = Rotary::PageStepTicks;
        bool snapToTicks = false;

        if (port->getDisplayHint() & PluginPort::Integer) {
            step = 1.0;
            ticks = Rotary::StepTicks;
            if (upperBound - lowerBound > 30.0)
                pageStep = 10.0;
            snapToTicks = true;
        }
        if (port->getDisplayHint() & PluginPort::Toggled) {
            lowerBound = -0.0001;
            upperBound = 1.0001;
            step = 1.0;
            pageStep = 1.0;
            ticks = Rotary::StepTicks;
            snapToTicks = true;
        }

        float displayLower = lowerBound, displayUpper = upperBound;

        bool logarithmic = (port->getDisplayHint() & PluginPort::Logarithmic);

        if (logarithmic) {
            float logthresh = -10;
            float thresh = powf(10, logthresh);
            if (lowerBound > thresh) lowerBound = log10f(lowerBound);
            else {
                if (upperBound > 1) lowerBound = 0;
                else lowerBound = logthresh;
            }
            if (upperBound > thresh) upperBound = log10f(upperBound);
            else upperBound = logthresh;

            step = (upperBound - lowerBound) / 100.0;
            pageStep = step * 10.f;
            initialValue = log10f(initialValue);
        }

        QLabel *low;
        if (port->getDisplayHint() &
            (PluginPort::Integer | PluginPort::Toggled)) {
            low = new QLabel(QString("%1").arg(int(displayLower)), parent);
        } else {
            low = new QLabel(QString("%1").arg(displayLower), parent);
        }
        low->setFont(plainFont);

//        std::cerr << "port " << port->getName() << ": lower bound "
//                  << displayLower << ", upper bound " << displayUpper
//                  << ", logarithmic " << logarithmic << ", default "
//                  << initialValue << ", actual lower " << lowerBound
//                  << ", actual upper " << upperBound << ", step "
//                  << step << std::endl;

        m_dial = new ::Rosegarden::Rotary(parent,
                                          lowerBound,    // min
                                          upperBound,    // max
                                          step,          // step
                                          pageStep,      // page step
                                          initialValue,  // initial
                                          30,            // size
                                          ticks,
                                          snapToTicks,
                                          false,         // centred
                                          logarithmic);

        m_dial->setKnobColour(GUIPalette::getColour(GUIPalette::RotaryPlugin));

        connect(m_dial, SIGNAL(valueChanged(float)),
                this, SLOT(slotValueChanged(float)));

        QLabel *upp;
        if (port->getDisplayHint() &
            (PluginPort::Integer | PluginPort::Toggled)) {
            upp = new QLabel(QString("%1").arg(int(displayUpper)), parent);
        } else {
            upp = new QLabel(QString("%1").arg(displayUpper), parent);
        }
        upp->setFont(plainFont);

        QWidgetItem *item;

        if (!hidden) {
            controlTitle->show();
            item = new QWidgetItem(controlTitle);
            item->setAlignment(Qt::AlignRight | Qt::AlignBottom);
            m_layout->addItem(item);
        } else {
            controlTitle->hide();
        }

        if (showBounds && !hidden) {
            low->show();
            item = new QWidgetItem(low);
            item->setAlignment(Qt::AlignRight | Qt::AlignBottom);
            m_layout->addItem(item);
        } else {
            low->hide();
        }

        if (!hidden) {
            m_dial->show();
            item = new QWidgetItem(m_dial);
            item->setAlignment(Qt::AlignCenter);
            m_layout->addItem(item);
        } else {
            m_dial->hide();
        }

        if (showBounds && !hidden) {
            upp->show();
            item = new QWidgetItem(upp);
            item->setAlignment(Qt::AlignLeft | Qt::AlignBottom);
            m_layout->addItem(item);
        } else {
            upp->hide();
        }
    }
}

void
PluginControl::setValue(float value, bool emitSignals)
{
    if (!emitSignals)
        m_dial->blockSignals(true);
    m_dial->setPosition(value);
    if (!emitSignals)
        m_dial->blockSignals(false);
    else
        emit valueChanged(value);
}

float
PluginControl::getValue() const
{
    return m_dial == 0 ? 0 : m_dial->getPosition();
}

void
PluginControl::slotValueChanged(float value)
{
    emit valueChanged(value);
}

}
#include "PluginControl.moc"