summaryrefslogtreecommitdiffstats
path: root/src/gui/studio/SynthPluginManagerDialog.cpp
blob: fb3f77b3927bf659d39adab4c36b98a1b89ebf51 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <richard.bown@ferventsoftware.com>
 
    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 "SynthPluginManagerDialog.h"
#include <tqlayout.h>

#include <tdelocale.h>
#include "misc/Debug.h"
#include "AudioPlugin.h"
#include "AudioPluginManager.h"
#include "AudioPluginOSCGUIManager.h"
#include "base/AudioPluginInstance.h"
#include "base/Instrument.h"
#include "base/MidiProgram.h"
#include "base/Studio.h"
#include "document/RosegardenGUIDoc.h"
#include "document/ConfigGroups.h"
#include <tdeaction.h>
#include <kcombobox.h>
#include <tdemainwindow.h>
#include <kstdaction.h>
#include <tqframe.h>
#include <tqgroupbox.h>
#include <tqlabel.h>
#include <tqobject.h>
#include <tqpushbutton.h>
#include <tqsizepolicy.h>
#include <tqstring.h>
#include <tqwidget.h>


namespace Rosegarden
{

SynthPluginManagerDialog::SynthPluginManagerDialog(TQWidget *parent,
            RosegardenGUIDoc *doc
#ifdef HAVE_LIBLO
            , AudioPluginOSCGUIManager *guiManager
#endif
                                                      ) :
            TDEMainWindow(parent, "synthpluginmanagerdialog"),
            m_document(doc),
            m_studio(&doc->getStudio()),
            m_pluginManager(doc->getPluginManager())
#ifdef HAVE_LIBLO
            , m_guiManager(guiManager)
#endif
    {
        setCaption(i18n("Manage Synth Plugins"));

        TQFrame *mainBox = new TQFrame(this);
        setCentralWidget(mainBox);

        TQVBoxLayout *mainLayout = new TQVBoxLayout(mainBox, 10, 10);

        TQGroupBox *groupBox = new TQGroupBox(1, TQt::Horizontal, i18n("Synth plugins"), mainBox);
        mainLayout->addWidget(groupBox);

        TQFrame *pluginFrame = new TQFrame(groupBox);
        TQGridLayout *pluginLayout = new TQGridLayout(pluginFrame, 1, 4, 3, 3);

        m_synthPlugins.clear();
        m_synthPlugins.push_back( -1);

        int count = 0;

        for (PluginIterator itr = m_pluginManager->begin();
                itr != m_pluginManager->end(); ++itr) {

            if ((*itr)->isSynth()) {
                m_synthPlugins.push_back(count);
            }

            ++count;
        }

        for (int i = 0; i < SoftSynthInstrumentCount; ++i) {

            InstrumentId id = SoftSynthInstrumentBase + i;
            Instrument *instrument = m_studio->getInstrumentById(id);
            if (!instrument)
                continue;

            //	pluginLayout->addWidget(new TQLabel(instrument->getPresentationName().c_str(),
            //					   pluginFrame), i, 0);
            pluginLayout->addWidget(new TQLabel(TQString("%1").arg(i + 1),
                                               pluginFrame), i, 0);

            AudioPluginInstance *plugin = instrument->getPlugin
                                          (Instrument::SYNTH_PLUGIN_POSITION);

            std::string identifier;
            if (plugin)
                identifier = plugin->getIdentifier();

            int currentItem = 0;

            KComboBox *pluginCombo = new KComboBox(pluginFrame);
            pluginCombo->insertItem(i18n("<none>"));

            for (size_t j = 0; j < m_synthPlugins.size(); ++j) {

                if (m_synthPlugins[j] == -1)
                    continue;

                AudioPlugin *plugin =
                    m_pluginManager->getPlugin(m_synthPlugins[j]);

                pluginCombo->insertItem(plugin->getName());

                if (plugin->getIdentifier() == identifier.c_str()) {
                    pluginCombo->setCurrentItem(pluginCombo->count() - 1);
                }
            }

            connect(pluginCombo, TQT_SIGNAL(activated(int)),
                    this, TQT_SLOT(slotPluginChanged(int)));

            pluginLayout->addWidget(pluginCombo, i, 1);

            m_synthCombos.push_back(pluginCombo);

            TQPushButton *controlsButton = new TQPushButton(i18n("Controls"), pluginFrame);
            pluginLayout->addWidget(controlsButton, i, 2);
            connect(controlsButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotControlsButtonClicked()));
            m_controlsButtons.push_back(controlsButton);

#ifdef HAVE_LIBLO

            TQPushButton *guiButton = new TQPushButton(i18n("Editor >>"), pluginFrame);
            pluginLayout->addWidget(guiButton, i, 3);
            guiButton->setEnabled(m_guiManager->hasGUI
                                  (id, Instrument::SYNTH_PLUGIN_POSITION));
            connect(guiButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotGUIButtonClicked()));
            m_guiButtons.push_back(guiButton);
#endif

        }

        TQFrame* btnBox = new TQFrame(mainBox);

        btnBox->setSizePolicy(
            TQSizePolicy(TQSizePolicy::Minimum, TQSizePolicy::Fixed));

        TQPushButton *closeButton = new TQPushButton(i18n("Close"), btnBox);

        TQHBoxLayout* layout = new TQHBoxLayout(btnBox, 0, 10);
        layout->addStretch(10);
        layout->addWidget(closeButton);
        layout->addSpacing(5);

        TDEAction* close = KStdAction::close(TQT_TQOBJECT(this),
                                           TQT_SLOT(slotClose()),
                                           actionCollection());

        closeButton->setText(close->text());
        connect(closeButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotClose()));

        mainLayout->addWidget(btnBox);

        createGUI("synthpluginmanager.rc");

        setAutoSaveSettings(SynthPluginManagerConfigGroup, true);
    }

    SynthPluginManagerDialog::~SynthPluginManagerDialog()
    {
        RG_DEBUG << "\n*** SynthPluginManagerDialog::~SynthPluginManagerDialog()"
        << endl;
    }

    void
    SynthPluginManagerDialog::updatePlugin(InstrumentId id, int plugin)
    {
        if (id < SoftSynthInstrumentBase)
            return ;
        int row = id - SoftSynthInstrumentBase;
        if (row >= m_synthCombos.size())
            return ;

        KComboBox *comboBox = m_synthCombos[row];

        for (unsigned int i = 0; i < m_synthPlugins.size(); ++i) {
            if (m_synthPlugins[i] == plugin) {
                blockSignals(true);
                comboBox->setCurrentItem(i);
                blockSignals(false);
                return ;
            }
        }

        blockSignals(true);
        comboBox->setCurrentItem(0);
        blockSignals(false);
        return ;
    }

    void
    SynthPluginManagerDialog::slotClose()
    {
        close();
    }

    void
    SynthPluginManagerDialog::closeEvent(TQCloseEvent *e)
    {
        emit closing();
        TDEMainWindow::closeEvent(e);
    }

    void
    SynthPluginManagerDialog::slotGUIButtonClicked()
    {
        const TQObject *s = sender();

        int instrumentNo = -1;

        for (unsigned int i = 0; i < m_guiButtons.size(); ++i) {
            if (s == m_guiButtons[i])
                instrumentNo = i;
        }

        if (instrumentNo == -1) {
            RG_DEBUG << "WARNING: SynthPluginManagerDialog::slotGUIButtonClicked: unknown sender" << endl;
            return ;
        }

        InstrumentId id = SoftSynthInstrumentBase + instrumentNo;

        emit showPluginGUI(id, Instrument::SYNTH_PLUGIN_POSITION);
    }

    void
    SynthPluginManagerDialog::slotControlsButtonClicked()
    {
        const TQObject *s = sender();

        int instrumentNo = -1;

        for (unsigned int i = 0; i < m_controlsButtons.size(); ++i) {
            if (s == m_controlsButtons[i])
                instrumentNo = i;
        }

        if (instrumentNo == -1) {
            RG_DEBUG << "WARNING: SynthPluginManagerDialog::slotControlsButtonClicked: unknown sender" << endl;
            return ;
        }

        InstrumentId id = SoftSynthInstrumentBase + instrumentNo;

        emit showPluginDialog(this, id, Instrument::SYNTH_PLUGIN_POSITION);
    }

    void
    SynthPluginManagerDialog::slotPluginChanged(int index)
    {
        const TQObject *s = sender();

        RG_DEBUG << "SynthPluginManagerDialog::slotPluginChanged(" << index
        << ")" << endl;

        int instrumentNo = -1;

        for (unsigned int i = 0; i < m_synthCombos.size(); ++i) {
            if (s == m_synthCombos[i])
                instrumentNo = i;
        }

        if (instrumentNo == -1) {
            RG_DEBUG << "WARNING: SynthPluginManagerDialog::slotValueChanged: unknown sender" << endl;
            return ;
        }

        InstrumentId id = SoftSynthInstrumentBase + instrumentNo;

        if (index >= int(m_synthPlugins.size())) {
            RG_DEBUG << "WARNING: SynthPluginManagerDialog::slotValueChanged: synth "
            << index << " out of range" << endl;
            return ;
        }

        // NB m_synthPlugins[0] is -1 to represent the <none> item

        AudioPlugin *plugin = m_pluginManager->getPlugin(m_synthPlugins[index]);
        Instrument *instrument = m_studio->getInstrumentById(id);

        if (instrument) {

            AudioPluginInstance *pluginInstance = instrument->getPlugin
                                                  (Instrument::SYNTH_PLUGIN_POSITION);

            if (pluginInstance) {

                if (plugin) {
                    RG_DEBUG << "plugin is " << plugin->getIdentifier() << endl;
                    pluginInstance->setIdentifier(plugin->getIdentifier().ascii());

                    // set ports to defaults

                    AudioPlugin::PortIterator it = plugin->begin();
                    int count = 0;

                    for (; it != plugin->end(); ++it) {

                        if (((*it)->getType() & PluginPort::Control) &&
                                ((*it)->getType() & PluginPort::Input)) {

                            if (pluginInstance->getPort(count) == 0) {
                                pluginInstance->addPort(count, (float)(*it)->getDefaultValue());
                            } else {
                                pluginInstance->getPort(count)->value = (*it)->getDefaultValue();
                            }
                        }

                        ++count;
                    }

                } else {
                    pluginInstance->setIdentifier("");
                }
            }
        }

#ifdef HAVE_LIBLO
        if (instrumentNo < m_guiButtons.size()) {
            m_guiButtons[instrumentNo]->setEnabled
            (m_guiManager->hasGUI
             (id, Instrument::SYNTH_PLUGIN_POSITION));
        }
#endif

        emit pluginSelected(id, Instrument::SYNTH_PLUGIN_POSITION,
                            m_synthPlugins[index]);
    }

    }
#include "SynthPluginManagerDialog.moc"