Chris@55
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@55
|
2
|
Chris@55
|
3 /*
|
Chris@55
|
4 Sonic Visualiser
|
Chris@55
|
5 An audio file viewer and annotation editor.
|
Chris@55
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@55
|
7 This file copyright 2006 Chris Cannam.
|
Chris@55
|
8
|
Chris@55
|
9 This program is free software; you can redistribute it and/or
|
Chris@55
|
10 modify it under the terms of the GNU General Public License as
|
Chris@55
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@55
|
12 License, or (at your option) any later version. See the file
|
Chris@55
|
13 COPYING included with this distribution for more information.
|
Chris@55
|
14 */
|
Chris@55
|
15
|
Chris@55
|
16 #include "PluginInstance.h"
|
Chris@55
|
17
|
Chris@55
|
18 #include <QRegExp>
|
Chris@55
|
19 #include <QXmlAttributes>
|
Chris@55
|
20
|
Chris@55
|
21 #include <iostream>
|
Chris@55
|
22
|
Chris@55
|
23 QString
|
Chris@55
|
24 PluginInstance::toXmlString(QString indent, QString extraAttributes) const
|
Chris@55
|
25 {
|
Chris@55
|
26 QString s;
|
Chris@55
|
27 s += indent;
|
Chris@55
|
28
|
Chris@55
|
29 s += QString("<plugin name=\"%1\" description=\"%2\" maker=\"%3\" version=\"%4\" copyright=\"%5\" %6 ")
|
Chris@55
|
30 .arg(encodeEntities(QString(getName().c_str())))
|
Chris@55
|
31 .arg(encodeEntities(QString(getDescription().c_str())))
|
Chris@55
|
32 .arg(encodeEntities(QString(getMaker().c_str())))
|
Chris@55
|
33 .arg(getPluginVersion())
|
Chris@55
|
34 .arg(encodeEntities(QString(getCopyright().c_str())))
|
Chris@55
|
35 .arg(extraAttributes);
|
Chris@55
|
36
|
Chris@55
|
37 if (!getPrograms().empty()) {
|
Chris@55
|
38 s += QString("program=\"%1\" ")
|
Chris@55
|
39 .arg(encodeEntities(getCurrentProgram().c_str()));
|
Chris@55
|
40 }
|
Chris@55
|
41
|
Chris@55
|
42 ParameterList parameters = getParameterDescriptors();
|
Chris@55
|
43
|
Chris@55
|
44 for (ParameterList::const_iterator i = parameters.begin();
|
Chris@55
|
45 i != parameters.end(); ++i) {
|
Chris@55
|
46 s += QString("param-%1=\"%2\" ")
|
Chris@55
|
47 .arg(stripInvalidParameterNameCharacters(QString(i->name.c_str())))
|
Chris@55
|
48 .arg(getParameter(i->name));
|
Chris@55
|
49 }
|
Chris@55
|
50
|
Chris@55
|
51 s += "/>\n";
|
Chris@55
|
52 return s;
|
Chris@55
|
53 }
|
Chris@55
|
54
|
Chris@55
|
55 #define CHECK_ATTRIBUTE(ATTRIBUTE, ACCESSOR) \
|
Chris@55
|
56 QString ATTRIBUTE = attrs.value(#ATTRIBUTE); \
|
Chris@55
|
57 if (ATTRIBUTE != "" && ATTRIBUTE != ACCESSOR().c_str()) { \
|
Chris@55
|
58 std::cerr << "WARNING: PluginInstance::setParameters: Plugin " \
|
Chris@55
|
59 << #ATTRIBUTE << " does not match (attributes have \"" \
|
Chris@55
|
60 << ATTRIBUTE.toStdString() << "\", my " \
|
Chris@55
|
61 << #ATTRIBUTE << " is \"" << ACCESSOR() << "\")" << std::endl; \
|
Chris@55
|
62 }
|
Chris@55
|
63
|
Chris@55
|
64 void
|
Chris@55
|
65 PluginInstance::setParameters(const QXmlAttributes &attrs)
|
Chris@55
|
66 {
|
Chris@55
|
67 CHECK_ATTRIBUTE(name, getName);
|
Chris@55
|
68 CHECK_ATTRIBUTE(description, getDescription);
|
Chris@55
|
69 CHECK_ATTRIBUTE(maker, getMaker);
|
Chris@55
|
70 CHECK_ATTRIBUTE(copyright, getCopyright);
|
Chris@55
|
71
|
Chris@55
|
72 bool ok;
|
Chris@55
|
73 int version = attrs.value("version").trimmed().toInt(&ok);
|
Chris@55
|
74 if (ok && version != getPluginVersion()) {
|
Chris@55
|
75 std::cerr << "WARNING: PluginInstance::setParameters: Plugin version does not match (attributes have " << version << ", my version is " << getPluginVersion() << ")" << std::endl;
|
Chris@55
|
76 }
|
Chris@55
|
77
|
Chris@55
|
78 if (!getPrograms().empty()) {
|
Chris@55
|
79 selectProgram(attrs.value("program").toStdString());
|
Chris@55
|
80 }
|
Chris@55
|
81
|
Chris@55
|
82 ParameterList parameters = getParameterDescriptors();
|
Chris@55
|
83
|
Chris@55
|
84 for (ParameterList::const_iterator i = parameters.begin();
|
Chris@55
|
85 i != parameters.end(); ++i) {
|
Chris@55
|
86 QString name = stripInvalidParameterNameCharacters
|
Chris@55
|
87 (QString(i->name.c_str()));
|
Chris@55
|
88 bool ok;
|
Chris@55
|
89 float value = attrs.value(name).trimmed().toFloat(&ok);
|
Chris@55
|
90 if (ok) {
|
Chris@55
|
91 setParameter(i->name, value);
|
Chris@55
|
92 } else {
|
Chris@55
|
93 std::cerr << "WARNING: PluginInstance::setParameters: Invalid value \"" << attrs.value(name).toStdString() << "\" for parameter \"" << i->name << "\"" << std::endl;
|
Chris@55
|
94 }
|
Chris@55
|
95 }
|
Chris@55
|
96 }
|
Chris@55
|
97
|
Chris@55
|
98 QString
|
Chris@55
|
99 PluginInstance::stripInvalidParameterNameCharacters(QString s) const
|
Chris@55
|
100 {
|
Chris@55
|
101 s.replace(QRegExp("[^a-zA-Z0-9_]*"), "");
|
Chris@55
|
102 return s;
|
Chris@55
|
103 }
|
Chris@55
|
104
|