changeset 314:70a232b1f12a

* Make XmlExportable::toXml the function that is universally overridden (and pure virtual) instead of toXmlString. Tidies up some classes, notably the model classes, significantly. Closes #1794561.
author Chris Cannam
date Thu, 18 Oct 2007 10:15:07 +0000
parents 29485aa03da4
children 96ef9746c560
files base/PlayParameters.cpp base/PlayParameters.h base/Selection.cpp base/Selection.h base/XmlExportable.cpp base/XmlExportable.h data/model/AggregateWaveModel.cpp data/model/EditableDenseThreeDimensionalModel.cpp data/model/EditableDenseThreeDimensionalModel.h data/model/ImageModel.h data/model/Model.cpp data/model/Model.h data/model/NoteModel.h data/model/SparseModel.h data/model/SparseOneDimensionalModel.h data/model/SparseTimeValueModel.h data/model/SparseValueModel.h data/model/TextModel.h data/model/WaveFileModel.cpp data/model/WritableWaveFileModel.cpp plugin/PluginXml.cpp plugin/PluginXml.h
diffstat 22 files changed, 93 insertions(+), 126 deletions(-) [+]
line wrap: on
line diff
--- a/base/PlayParameters.cpp	Wed Oct 17 12:58:45 2007 +0000
+++ b/base/PlayParameters.cpp	Thu Oct 18 10:15:07 2007 +0000
@@ -17,6 +17,8 @@
 
 #include <iostream>
 
+#include <QTextStream>
+
 void
 PlayParameters::copyFrom(const PlayParameters *pp)
 {
@@ -27,25 +29,24 @@
     m_playPluginConfiguration = pp->getPlayPluginConfiguration();
 }
 
-QString
-PlayParameters::toXmlString(QString indent,
-                            QString extraAttributes) const
+void
+PlayParameters::toXml(QTextStream &stream,
+                      QString indent,
+                      QString extraAttributes) const
 {
-    QString s;
-    s += indent;
-    s += QString("<playparameters mute=\"%1\" pan=\"%2\" gain=\"%3\" pluginId=\"%4\" %6")
+    stream << indent;
+    stream << QString("<playparameters mute=\"%1\" pan=\"%2\" gain=\"%3\" pluginId=\"%4\" %6")
         .arg(m_playMuted ? "true" : "false")
         .arg(m_playPan)
         .arg(m_playGain)
         .arg(m_playPluginId)
         .arg(extraAttributes);
     if (m_playPluginConfiguration != "") {
-        s += ">\n  " + indent + m_playPluginConfiguration
-            + "\n" + indent + "</playparameters>\n";
+        stream << ">\n  " << indent << m_playPluginConfiguration
+               << "\n" << indent << "</playparameters>\n";
     } else {
-        s += "/>\n";
+        stream << "/>\n";
     }
-    return s;
 }
 
 void
--- a/base/PlayParameters.h	Wed Oct 17 12:58:45 2007 +0000
+++ b/base/PlayParameters.h	Thu Oct 18 10:15:07 2007 +0000
@@ -36,8 +36,9 @@
 
     virtual void copyFrom(const PlayParameters *);
 
-    virtual QString toXmlString(QString indent = "",
-                                QString extraAttributes = "") const;
+    virtual void toXml(QTextStream &stream,
+                       QString indent = "",
+                       QString extraAttributes = "") const;
 
 public slots:
     virtual void setPlayMuted(bool muted);
--- a/base/Selection.cpp	Wed Oct 17 12:58:45 2007 +0000
+++ b/base/Selection.cpp	Thu Oct 18 10:15:07 2007 +0000
@@ -14,6 +14,7 @@
 */
 
 #include "Selection.h"
+#include <QTextStream>
 
 Selection::Selection() :
     m_startFrame(0),
@@ -212,18 +213,17 @@
     return Selection();
 }
 
-QString
-MultiSelection::toXmlString(QString indent,
-			    QString extraAttributes) const
+void
+MultiSelection::toXml(QTextStream &stream, QString indent,
+                      QString extraAttributes) const
 {
-    QString s;
-    s += indent + QString("<selections %1>\n").arg(extraAttributes);
+    stream << indent << QString("<selections %1>\n").arg(extraAttributes);
     for (SelectionList::iterator i = m_selections.begin();
 	 i != m_selections.end(); ++i) {
-	s += indent + QString("  <selection start=\"%1\" end=\"%2\"/>\n")
+	stream << indent
+               << QString("  <selection start=\"%1\" end=\"%2\"/>\n")
 	    .arg(i->getStartFrame()).arg(i->getEndFrame());
     }
-    s += indent + "</selections>\n";
-    return s;
+    stream << indent << "</selections>\n";
 }
 
--- a/base/Selection.h	Wed Oct 17 12:58:45 2007 +0000
+++ b/base/Selection.h	Thu Oct 18 10:15:07 2007 +0000
@@ -67,8 +67,8 @@
      */
     Selection getContainingSelection(size_t frame, bool defaultToFollowing) const;
 
-    virtual QString toXmlString(QString indent = "",
-				QString extraAttributes = "") const;
+    virtual void toXml(QTextStream &stream, QString indent = "",
+                       QString extraAttributes = "") const;
 
 protected:
     SelectionList m_selections;
--- a/base/XmlExportable.cpp	Wed Oct 17 12:58:45 2007 +0000
+++ b/base/XmlExportable.cpp	Thu Oct 18 10:15:07 2007 +0000
@@ -19,11 +19,18 @@
 #include <QMutexLocker>
 #include <QTextStream>
 
-void
-XmlExportable::toXml(QTextStream &stream, QString indent,
-                     QString extraAttributes) const
+QString
+XmlExportable::toXmlString(QString indent,
+                           QString extraAttributes) const
 {
-    stream << toXmlString(indent, extraAttributes);
+    QString s;
+
+    {
+        QTextStream out(&s);
+        toXml(out, indent, extraAttributes);
+    }
+
+    return s;
 }
 
 QString
--- a/base/XmlExportable.h	Wed Oct 17 12:58:45 2007 +0000
+++ b/base/XmlExportable.h	Thu Oct 18 10:15:07 2007 +0000
@@ -28,23 +28,18 @@
 
     /**
      * Stream this exportable object out to XML on a text stream.
-     * 
-     * The default implementation calls toXmlString and streams the
-     * resulting string.  This is only appropriate for objects with
-     * short representations.  Bigger objects should override this
-     * method so as to write to the stream directly and override
-     * toXmlString with a method that calls this one, so that the
-     * direct streaming method can be used when appropriate.
      */
     virtual void toXml(QTextStream &stream,
                        QString indent = "",
-                       QString extraAttributes = "") const;
+                       QString extraAttributes = "") const = 0;
 
     /**
-     * Convert this exportable object to XML in a string.
+     * Convert this exportable object to XML in a string.  The default
+     * implementation calls toXml and returns the result as a string.
+     * Do not override this unless you really know what you're doing.
      */
     virtual QString toXmlString(QString indent = "",
-				QString extraAttributes = "") const = 0;
+				QString extraAttributes = "") const;
 
     static QString encodeEntities(QString);
 
--- a/data/model/AggregateWaveModel.cpp	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/AggregateWaveModel.cpp	Thu Oct 18 10:15:07 2007 +0000
@@ -17,6 +17,8 @@
 
 #include <iostream>
 
+#include <QTextStream>
+
 PowerOfSqrtTwoZoomConstraint
 AggregateWaveModel::m_zoomConstraint;
 
--- a/data/model/EditableDenseThreeDimensionalModel.cpp	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/EditableDenseThreeDimensionalModel.cpp	Thu Oct 18 10:15:07 2007 +0000
@@ -272,13 +272,14 @@
 
 void
 EditableDenseThreeDimensionalModel::toXml(QTextStream &out,
-                                  QString indent,
-                                  QString extraAttributes) const
+                                          QString indent,
+                                          QString extraAttributes) const
 {
     // For historical reasons we read and write "resolution" as "windowSize"
 
-    out << Model::toXmlString
-	(indent, QString("type=\"dense\" dimensions=\"3\" windowSize=\"%1\" yBinCount=\"%2\" minimum=\"%3\" maximum=\"%4\" dataset=\"%5\" %6")
+    Model::toXml
+	(out, indent,
+         QString("type=\"dense\" dimensions=\"3\" windowSize=\"%1\" yBinCount=\"%2\" minimum=\"%3\" maximum=\"%4\" dataset=\"%5\" %6")
 	 .arg(m_resolution)
 	 .arg(m_yBinCount)
 	 .arg(m_minimum)
@@ -311,17 +312,4 @@
     out << indent + "</dataset>\n";
 }
 
-QString
-EditableDenseThreeDimensionalModel::toXmlString(QString indent,
-					QString extraAttributes) const
-{
-    QString s;
 
-    {
-        QTextStream out(&s);
-        toXml(out, indent, extraAttributes);
-    }
-
-    return s;
-}
-
--- a/data/model/EditableDenseThreeDimensionalModel.h	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/EditableDenseThreeDimensionalModel.h	Thu Oct 18 10:15:07 2007 +0000
@@ -113,9 +113,6 @@
                        QString indent = "",
                        QString extraAttributes = "") const;
 
-    virtual QString toXmlString(QString indent = "",
-				QString extraAttributes = "") const;
-
 protected:
     typedef std::vector<Column> ValueMatrix;
     ValueMatrix m_data;
--- a/data/model/ImageModel.h	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/ImageModel.h	Thu Oct 18 10:15:07 2007 +0000
@@ -39,10 +39,12 @@
     QString image;
     QString label;
     
-    QString toXmlString(QString indent = "",
-			QString extraAttributes = "") const
+    void toXml(QTextStream &stream,
+               QString indent = "",
+               QString extraAttributes = "") const
     {
-	return QString("%1<point frame=\"%2\" image=\"%3\" label=\"%4\" %5/>\n")
+	stream <<
+            QString("%1<point frame=\"%2\" image=\"%3\" label=\"%4\" %5/>\n")
 	    .arg(indent).arg(frame)
             .arg(encodeEntities(image))
             .arg(encodeEntities(label))
--- a/data/model/Model.cpp	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/Model.cpp	Thu Oct 18 10:15:07 2007 +0000
@@ -47,16 +47,4 @@
 	.arg(extraAttributes);
 }
 
-QString
-Model::toXmlString(QString indent, QString extraAttributes) const
-{
-    QString s;
 
-    {
-        QTextStream out(&s);
-        toXml(out, indent, extraAttributes);
-    }
-
-    return s;
-}
-
--- a/data/model/Model.h	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/Model.h	Thu Oct 18 10:15:07 2007 +0000
@@ -132,9 +132,6 @@
                        QString indent = "",
                        QString extraAttributes = "") const;
 
-    virtual QString toXmlString(QString indent = "",
-				QString extraAttributes = "") const;
-
     virtual QString toDelimitedDataString(QString) const { return ""; }
 
 signals:
--- a/data/model/NoteModel.h	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/NoteModel.h	Thu Oct 18 10:15:07 2007 +0000
@@ -43,10 +43,12 @@
     size_t duration;
     QString label;
 
-    QString toXmlString(QString indent = "",
-			QString extraAttributes = "") const
+    void toXml(QTextStream &stream,
+               QString indent = "",
+               QString extraAttributes = "") const
     {
-	return QString("%1<point frame=\"%2\" value=\"%3\" duration=\"%4\" label=\"%5\" %6/>\n")
+	stream <<
+            QString("%1<point frame=\"%2\" value=\"%3\" duration=\"%4\" label=\"%5\" %6/>\n")
 	    .arg(indent).arg(frame).arg(value).arg(duration).arg(label).arg(extraAttributes);
     }
 
@@ -124,7 +126,7 @@
                        QString indent = "",
                        QString extraAttributes = "") const
     {
-	return SparseValueModel<Note>::toXml
+        SparseValueModel<Note>::toXml
 	    (out,
              indent,
 	     QString("%1 valueQuantization=\"%2\"")
--- a/data/model/SparseModel.h	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/SparseModel.h	Thu Oct 18 10:15:07 2007 +0000
@@ -136,9 +136,6 @@
                        QString indent = "",
                        QString extraAttributes = "") const;
 
-    virtual QString toXmlString(QString indent = "",
-				QString extraAttributes = "") const;
-
     virtual QString toDelimitedDataString(QString delimiter) const
     { 
         QString s;
@@ -567,7 +564,7 @@
 	.arg(PointType(0).getDimensions());
 
     for (PointListIterator i = m_points.begin(); i != m_points.end(); ++i) {
-	out << i->toXmlString(indent + "  ");
+        i->toXml(out, indent + "  ");
     }
 
     out << indent;
@@ -575,21 +572,6 @@
 }
 
 template <typename PointType>
-QString
-SparseModel<PointType>::toXmlString(QString indent,
-				    QString extraAttributes) const
-{
-    QString s;
-
-    {
-        QTextStream out(&s);
-        toXml(out, indent, extraAttributes);
-    }
-
-    return s;
-}
-    
-template <typename PointType>
 SparseModel<PointType>::EditCommand::EditCommand(SparseModel *model,
                                                  QString commandName) :
     MacroCommand(commandName),
--- a/data/model/SparseOneDimensionalModel.h	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/SparseOneDimensionalModel.h	Thu Oct 18 10:15:07 2007 +0000
@@ -31,10 +31,11 @@
     long frame;
     QString label;
     
-    QString toXmlString(QString indent = "",
-			QString extraAttributes = "") const
+    void toXml(QTextStream &stream,
+               QString indent = "",
+               QString extraAttributes = "") const
     {
-	return QString("%1<point frame=\"%2\" label=\"%3\" %4/>\n")
+        stream << QString("%1<point frame=\"%2\" label=\"%3\" %4/>\n")
 	    .arg(indent).arg(frame).arg(label).arg(extraAttributes);
     }
 
--- a/data/model/SparseTimeValueModel.h	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/SparseTimeValueModel.h	Thu Oct 18 10:15:07 2007 +0000
@@ -39,10 +39,10 @@
     float value;
     QString label;
     
-    QString toXmlString(QString indent = "",
-			QString extraAttributes = "") const
+    void toXml(QTextStream &stream, QString indent = "",
+               QString extraAttributes = "") const
     {
-	return QString("%1<point frame=\"%2\" value=\"%3\" label=\"%4\" %5/>\n")
+        stream << QString("%1<point frame=\"%2\" value=\"%3\" label=\"%4\" %5/>\n")
 	    .arg(indent).arg(frame).arg(value).arg(label).arg(extraAttributes);
     }
 
--- a/data/model/SparseValueModel.h	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/SparseValueModel.h	Thu Oct 18 10:15:07 2007 +0000
@@ -110,11 +110,11 @@
 	}
     }
 
-    virtual QString toXmlString(QString indent = "",
-				QString extraAttributes = "") const
+    virtual void toXml(QTextStream &stream, QString indent = "",
+                       QString extraAttributes = "") const
     {
-	return SparseModel<PointType>::toXmlString
-	    (indent,
+	SparseModel<PointType>::toXml
+	    (stream, indent,
 	     QString("%1 minimum=\"%2\" maximum=\"%3\" units=\"%4\"")
 	     .arg(extraAttributes).arg(m_valueMinimum).arg(m_valueMaximum)
              .arg(this->encodeEntities(m_units)));
--- a/data/model/TextModel.h	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/TextModel.h	Thu Oct 18 10:15:07 2007 +0000
@@ -39,10 +39,10 @@
     float height;
     QString label;
     
-    QString toXmlString(QString indent = "",
-			QString extraAttributes = "") const
+    void toXml(QTextStream &stream, QString indent = "",
+               QString extraAttributes = "") const
     {
-	return QString("%1<point frame=\"%2\" height=\"%3\" label=\"%4\" %5/>\n")
+	stream << QString("%1<point frame=\"%2\" height=\"%3\" label=\"%4\" %5/>\n")
 	    .arg(indent).arg(frame).arg(height)
             .arg(encodeEntities(label)).arg(extraAttributes);
     }
--- a/data/model/WaveFileModel.cpp	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/WaveFileModel.cpp	Thu Oct 18 10:15:07 2007 +0000
@@ -22,6 +22,7 @@
 
 #include <QMessageBox>
 #include <QFileInfo>
+#include <QTextStream>
 
 #include <iostream>
 #include <unistd.h>
--- a/data/model/WritableWaveFileModel.cpp	Wed Oct 17 12:58:45 2007 +0000
+++ b/data/model/WritableWaveFileModel.cpp	Thu Oct 18 10:15:07 2007 +0000
@@ -22,6 +22,7 @@
 #include "fileio/WavFileReader.h"
 
 #include <QDir>
+#include <QTextStream>
 
 #include <cassert>
 #include <iostream>
--- a/plugin/PluginXml.cpp	Wed Oct 17 12:58:45 2007 +0000
+++ b/plugin/PluginXml.cpp	Thu Oct 18 10:15:07 2007 +0000
@@ -23,6 +23,8 @@
 #include <QDomNamedNodeMap>
 #include <QDomAttr>
 
+#include <QTextStream>
+
 #include "vamp-sdk/PluginBase.h"
 #include "RealTimePluginInstance.h"
 
@@ -53,13 +55,13 @@
     return rv;
 }
     
-QString
-PluginXml::toXmlString(QString indent, QString extraAttributes) const
+void
+PluginXml::toXml(QTextStream &stream,
+                 QString indent, QString extraAttributes) const
 {
-    QString s;
-    s += indent;
+    stream << indent;
 
-    s += QString("<plugin identifier=\"%1\" name=\"%2\" description=\"%3\" maker=\"%4\" version=\"%5\" copyright=\"%6\" %7 ")
+    stream << QString("<plugin identifier=\"%1\" name=\"%2\" description=\"%3\" maker=\"%4\" version=\"%5\" copyright=\"%6\" %7 ")
         .arg(encodeEntities(QString(m_plugin->getIdentifier().c_str())))
         .arg(encodeEntities(QString(m_plugin->getName().c_str())))
         .arg(encodeEntities(QString(m_plugin->getDescription().c_str())))
@@ -69,7 +71,7 @@
         .arg(extraAttributes);
 
     if (!m_plugin->getPrograms().empty()) {
-        s += QString("program=\"%1\" ")
+        stream << QString("program=\"%1\" ")
             .arg(encodeEntities(m_plugin->getCurrentProgram().c_str()));
     }
 
@@ -79,11 +81,11 @@
     for (Vamp::PluginBase::ParameterList::const_iterator i = parameters.begin();
          i != parameters.end(); ++i) {
 
-//        std::cerr << "PluginXml::toXmlString: parameter name \""
+//        std::cerr << "PluginXml::toXml: parameter name \""
 //                  << i->name.c_str() << "\" has value "
 //                  << m_plugin->getParameter(i->name) << std::endl;
 
-        s += QString("param-%1=\"%2\" ")
+        stream << QString("param-%1=\"%2\" ")
             .arg(stripInvalidParameterNameCharacters(QString(i->identifier.c_str())))
             .arg(m_plugin->getParameter(i->identifier));
     }
@@ -104,13 +106,12 @@
             config += QString("%1=%2").arg(key).arg(value);
         }
         if (config != "") {
-            s += QString("configuration=\"%1\" ")
+            stream << QString("configuration=\"%1\" ")
                 .arg(encodeEntities(config));
         }
     }
 
-    s += "/>\n";
-    return s;
+    stream << "/>\n";
 }
 
 #define CHECK_ATTRIBUTE(ATTRIBUTE, ACCESSOR) \
--- a/plugin/PluginXml.h	Wed Oct 17 12:58:45 2007 +0000
+++ b/plugin/PluginXml.h	Thu Oct 18 10:15:07 2007 +0000
@@ -31,19 +31,20 @@
     /**
      * Export plugin settings to XML.
      */
-    virtual QString toXmlString(QString indent = "",
-                                QString extraAttributes = "") const;
+    virtual void toXml(QTextStream &stream,
+                       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.
+     * attributes.  This is a partial inverse of toXml.
      */
     virtual void setParameters(const QXmlAttributes &);
 
     /**
      * Set the parameters and program of a plugin from an XML plugin
-     * element as returned by toXmlString.  This is a partial inverse
-     * of toXmlString.
+     * element as returned by toXml.  This is a partial inverse of
+     * toXml.
      */
     virtual void setParametersFromXml(QString xml);