# HG changeset patch # User Chris Cannam # Date 1143033830 0 # Node ID 6befca60ab4e614bce8fe232112821e71d770b34 # Parent 709d63d900281ed9b7d02543d944945b6777f854 * added to- and from- XML methods to PluginInstance diff -r 709d63d90028 -r 6befca60ab4e plugin/PluginInstance.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugin/PluginInstance.cpp Wed Mar 22 13:23:50 2006 +0000 @@ -0,0 +1,104 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2006 Chris Cannam. + + 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 "PluginInstance.h" + +#include +#include + +#include + +QString +PluginInstance::toXmlString(QString indent, QString extraAttributes) const +{ + QString s; + s += indent; + + s += QString("name.c_str()))) + .arg(getParameter(i->name)); + } + + s += "/>\n"; + return s; +} + +#define CHECK_ATTRIBUTE(ATTRIBUTE, ACCESSOR) \ + QString ATTRIBUTE = attrs.value(#ATTRIBUTE); \ + if (ATTRIBUTE != "" && ATTRIBUTE != ACCESSOR().c_str()) { \ + std::cerr << "WARNING: PluginInstance::setParameters: Plugin " \ + << #ATTRIBUTE << " does not match (attributes have \"" \ + << ATTRIBUTE.toStdString() << "\", my " \ + << #ATTRIBUTE << " is \"" << ACCESSOR() << "\")" << std::endl; \ + } + +void +PluginInstance::setParameters(const QXmlAttributes &attrs) +{ + CHECK_ATTRIBUTE(name, getName); + CHECK_ATTRIBUTE(description, getDescription); + CHECK_ATTRIBUTE(maker, getMaker); + CHECK_ATTRIBUTE(copyright, getCopyright); + + bool ok; + int version = attrs.value("version").trimmed().toInt(&ok); + if (ok && version != getPluginVersion()) { + std::cerr << "WARNING: PluginInstance::setParameters: Plugin version does not match (attributes have " << version << ", my version is " << getPluginVersion() << ")" << std::endl; + } + + if (!getPrograms().empty()) { + selectProgram(attrs.value("program").toStdString()); + } + + ParameterList parameters = getParameterDescriptors(); + + for (ParameterList::const_iterator i = parameters.begin(); + i != parameters.end(); ++i) { + QString name = stripInvalidParameterNameCharacters + (QString(i->name.c_str())); + bool ok; + float value = attrs.value(name).trimmed().toFloat(&ok); + if (ok) { + setParameter(i->name, value); + } else { + std::cerr << "WARNING: PluginInstance::setParameters: Invalid value \"" << attrs.value(name).toStdString() << "\" for parameter \"" << i->name << "\"" << std::endl; + } + } +} + +QString +PluginInstance::stripInvalidParameterNameCharacters(QString s) const +{ + s.replace(QRegExp("[^a-zA-Z0-9_]*"), ""); + return s; +} + diff -r 709d63d90028 -r 6befca60ab4e plugin/PluginInstance.h --- a/plugin/PluginInstance.h Tue Mar 21 18:05:28 2006 +0000 +++ b/plugin/PluginInstance.h Wed Mar 22 13:23:50 2006 +0000 @@ -19,6 +19,10 @@ #include #include +#include "base/XmlExportable.h" + +class QXmlAttributes; + /** * A base class for plugins with optional configurable parameters, * programs, etc. @@ -30,7 +34,7 @@ * controls for the plugin to the user. */ -class PluginInstance +class PluginInstance : public XmlExportable { public: /** @@ -70,7 +74,8 @@ { /** * The name of the parameter, in computer-usable form. Should - * be reasonably short and without whitespace or punctuation. + * be reasonably short, and may only contain the characters + * [a-zA-Z0-9_]. */ std::string name; @@ -153,6 +158,21 @@ * available programs, do nothing.) */ virtual void selectProgram(std::string) { } + + /** + * Export plugin settings to XML. + */ + virtual QString toXmlString(QString indent = "", + QString extraAttributes = "") const; + + /** + * Set the parameters and program of a plugin from a set of XML + * attributes. This is a partial inverse of toXmlString. + */ + virtual void setParameters(const QXmlAttributes &); + +protected: + QString stripInvalidParameterNameCharacters(QString) const; }; #endif