comparison plugin/PluginInstance.cpp @ 55:6befca60ab4e

* added to- and from- XML methods to PluginInstance
author Chris Cannam
date Wed, 22 Mar 2006 13:23:50 +0000
parents
children 2157fa46c1e7
comparison
equal deleted inserted replaced
54:709d63d90028 55:6befca60ab4e
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 "PluginInstance.h"
17
18 #include <QRegExp>
19 #include <QXmlAttributes>
20
21 #include <iostream>
22
23 QString
24 PluginInstance::toXmlString(QString indent, QString extraAttributes) const
25 {
26 QString s;
27 s += indent;
28
29 s += QString("<plugin name=\"%1\" description=\"%2\" maker=\"%3\" version=\"%4\" copyright=\"%5\" %6 ")
30 .arg(encodeEntities(QString(getName().c_str())))
31 .arg(encodeEntities(QString(getDescription().c_str())))
32 .arg(encodeEntities(QString(getMaker().c_str())))
33 .arg(getPluginVersion())
34 .arg(encodeEntities(QString(getCopyright().c_str())))
35 .arg(extraAttributes);
36
37 if (!getPrograms().empty()) {
38 s += QString("program=\"%1\" ")
39 .arg(encodeEntities(getCurrentProgram().c_str()));
40 }
41
42 ParameterList parameters = getParameterDescriptors();
43
44 for (ParameterList::const_iterator i = parameters.begin();
45 i != parameters.end(); ++i) {
46 s += QString("param-%1=\"%2\" ")
47 .arg(stripInvalidParameterNameCharacters(QString(i->name.c_str())))
48 .arg(getParameter(i->name));
49 }
50
51 s += "/>\n";
52 return s;
53 }
54
55 #define CHECK_ATTRIBUTE(ATTRIBUTE, ACCESSOR) \
56 QString ATTRIBUTE = attrs.value(#ATTRIBUTE); \
57 if (ATTRIBUTE != "" && ATTRIBUTE != ACCESSOR().c_str()) { \
58 std::cerr << "WARNING: PluginInstance::setParameters: Plugin " \
59 << #ATTRIBUTE << " does not match (attributes have \"" \
60 << ATTRIBUTE.toStdString() << "\", my " \
61 << #ATTRIBUTE << " is \"" << ACCESSOR() << "\")" << std::endl; \
62 }
63
64 void
65 PluginInstance::setParameters(const QXmlAttributes &attrs)
66 {
67 CHECK_ATTRIBUTE(name, getName);
68 CHECK_ATTRIBUTE(description, getDescription);
69 CHECK_ATTRIBUTE(maker, getMaker);
70 CHECK_ATTRIBUTE(copyright, getCopyright);
71
72 bool ok;
73 int version = attrs.value("version").trimmed().toInt(&ok);
74 if (ok && version != getPluginVersion()) {
75 std::cerr << "WARNING: PluginInstance::setParameters: Plugin version does not match (attributes have " << version << ", my version is " << getPluginVersion() << ")" << std::endl;
76 }
77
78 if (!getPrograms().empty()) {
79 selectProgram(attrs.value("program").toStdString());
80 }
81
82 ParameterList parameters = getParameterDescriptors();
83
84 for (ParameterList::const_iterator i = parameters.begin();
85 i != parameters.end(); ++i) {
86 QString name = stripInvalidParameterNameCharacters
87 (QString(i->name.c_str()));
88 bool ok;
89 float value = attrs.value(name).trimmed().toFloat(&ok);
90 if (ok) {
91 setParameter(i->name, value);
92 } else {
93 std::cerr << "WARNING: PluginInstance::setParameters: Invalid value \"" << attrs.value(name).toStdString() << "\" for parameter \"" << i->name << "\"" << std::endl;
94 }
95 }
96 }
97
98 QString
99 PluginInstance::stripInvalidParameterNameCharacters(QString s) const
100 {
101 s.replace(QRegExp("[^a-zA-Z0-9_]*"), "");
102 return s;
103 }
104