changeset 1674:69ab62d378bf osc-script

Ensure image & text models get the proper attribute names (which are not the same as the default event ones)
author Chris Cannam
date Wed, 27 Mar 2019 16:06:35 +0000
parents dfcd05e8bd2f
children 6804af71b7be
files base/Event.h base/EventSeries.cpp base/EventSeries.h data/model/ImageModel.h data/model/TextModel.h data/model/test/TestSparseModels.h
diffstat 6 files changed, 89 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/base/Event.h	Wed Mar 27 14:15:21 2019 +0000
+++ b/base/Event.h	Wed Mar 27 16:06:35 2019 +0000
@@ -256,21 +256,44 @@
         return m_uri < p.m_uri;
     }
 
+    struct ExportNameOptions {
+
+        ExportNameOptions() :
+            valueAtttributeName("value"),
+            uriAttributeName("uri") { }
+
+        QString valueAtttributeName;
+        QString uriAttributeName;
+    };
+    
     void toXml(QTextStream &stream,
                QString indent = "",
-               QString extraAttributes = "") const {
+               QString extraAttributes = "",
+               ExportNameOptions opts = ExportNameOptions()) const {
 
         // For I/O purposes these are points, not events
         stream << indent << QString("<point frame=\"%1\" ").arg(m_frame);
-        if (m_haveValue) stream << QString("value=\"%1\" ").arg(m_value);
-        if (m_haveDuration) stream << QString("duration=\"%1\" ").arg(m_duration);
-        if (m_haveLevel) stream << QString("level=\"%1\" ").arg(m_level);
-        if (m_haveReferenceFrame) stream << QString("referenceFrame=\"%1\" ")
-                                      .arg(m_referenceFrame);
-        stream << QString("label=\"%1\" ")
-            .arg(XmlExportable::encodeEntities(m_label));
+        if (m_haveValue) {
+            stream << QString("%1=\"%2\" ")
+                .arg(opts.valueAtttributeName).arg(m_value);
+        }
+        if (m_haveDuration) {
+            stream << QString("duration=\"%1\" ").arg(m_duration);
+        }
+        if (m_haveLevel) {
+            stream << QString("level=\"%1\" ").arg(m_level);
+        }
+        if (m_haveReferenceFrame) {
+            stream << QString("referenceFrame=\"%1\" ")
+                .arg(m_referenceFrame);
+        }
+        if (m_label != "") {
+            stream << QString("label=\"%1\" ")
+                .arg(XmlExportable::encodeEntities(m_label));
+        }
         if (m_uri != QString()) {
-            stream << QString("uri=\"%1\" ")
+            stream << QString("%1=\"%2\" ")
+                .arg(opts.uriAttributeName)
                 .arg(XmlExportable::encodeEntities(m_uri));
         }
         stream << extraAttributes << "/>\n";
--- a/base/EventSeries.cpp	Wed Mar 27 14:15:21 2019 +0000
+++ b/base/EventSeries.cpp	Wed Mar 27 16:06:35 2019 +0000
@@ -485,12 +485,21 @@
                    QString indent,
                    QString extraAttributes) const
 {
+    toXml(out, indent, extraAttributes, Event::ExportNameOptions());
+}
+
+void
+EventSeries::toXml(QTextStream &out,
+                   QString indent,
+                   QString extraAttributes,
+                   Event::ExportNameOptions options) const
+{
     out << indent << QString("<dataset id=\"%1\" %2>\n")
         .arg(getObjectExportId(this))
         .arg(extraAttributes);
     
     for (const auto &p: m_events) {
-        p.toXml(out, indent + "  ");
+        p.toXml(out, indent + "  ", "", options);
     }
     
     out << indent << "</dataset>\n";
--- a/base/EventSeries.h	Wed Mar 27 14:15:21 2019 +0000
+++ b/base/EventSeries.h	Wed Mar 27 16:06:35 2019 +0000
@@ -205,6 +205,14 @@
     void toXml(QTextStream &out,
                QString indent,
                QString extraAttributes) const override;
+
+    /**
+     * Emit to XML as a dataset element.
+     */
+    void toXml(QTextStream &out,
+               QString indent,
+               QString extraAttributes,
+               Event::ExportNameOptions) const;
     
 private:
     /**
--- a/data/model/ImageModel.h	Wed Mar 27 14:15:21 2019 +0000
+++ b/data/model/ImageModel.h	Wed Mar 27 16:06:35 2019 +0000
@@ -142,7 +142,7 @@
     void add(Event e) override {
 
         {   QMutexLocker locker(&m_mutex);
-            m_events.add(e.withoutDuration());
+            m_events.add(e.withoutDuration().withoutValue().withoutLevel());
         }
         
         m_notifier.update(e.getFrame(), m_resolution);
@@ -255,8 +255,11 @@
                           // subsequent events are always notified
              .arg(getObjectExportId(&m_events))
              .arg(extraAttributes));
+
+        Event::ExportNameOptions options;
+        options.uriAttributeName = "image";
         
-        m_events.toXml(out, indent, QString("dimensions=\"1\""));
+        m_events.toXml(out, indent, QString("dimensions=\"1\""), options);
     }
   
 protected:
--- a/data/model/TextModel.h	Wed Mar 27 14:15:21 2019 +0000
+++ b/data/model/TextModel.h	Wed Mar 27 16:06:35 2019 +0000
@@ -137,7 +137,7 @@
     void add(Event e) override {
 
         {   QMutexLocker locker(&m_mutex);
-            m_events.add(e.withoutDuration());
+            m_events.add(e.withoutDuration().withoutLevel());
         }
         
         m_notifier.update(e.getFrame(), m_resolution);
@@ -250,8 +250,11 @@
                           // subsequent events are always notified
              .arg(getObjectExportId(&m_events))
              .arg(extraAttributes));
+
+        Event::ExportNameOptions options;
+        options.valueAtttributeName = "height";
         
-        m_events.toXml(out, indent, QString("dimensions=\"2\""));
+        m_events.toXml(out, indent, QString("dimensions=\"2\""), options);
     }
   
 protected:
--- a/data/model/test/TestSparseModels.h	Wed Mar 27 14:15:21 2019 +0000
+++ b/data/model/test/TestSparseModels.h	Wed Mar 27 16:06:35 2019 +0000
@@ -19,6 +19,7 @@
 #include "../NoteModel.h"
 #include "../TextModel.h"
 #include "../PathModel.h"
+#include "../ImageModel.h"
 
 #include <QObject>
 #include <QtTest>
@@ -106,8 +107,9 @@
     void s1d_xml() {
         SparseOneDimensionalModel m(100, 10, false);
         m.setObjectName("This \"&\" that");
-        Event p1(20), p2(20), p3(50);
-        p2 = p2.withLabel("Label &'\">");
+        Event p1(20);
+        Event p2(20, "Label &'\">");
+        Event p3(50, 12.4f, 16, ""); // value + duration should not be saved
         m.add(p1);
         m.add(p2);
         m.add(p3);
@@ -118,9 +120,9 @@
         QString expected =
             "<model id='1' name='This &quot;&amp;&quot; that' sampleRate='100' start='20' end='60' type='sparse' dimensions='1' resolution='10' notifyOnAdd='true' dataset='0' />\n"
             "<dataset id='0' dimensions='1'>\n"
-            "  <point frame='20' label='' />\n"
+            "  <point frame='20' />\n"
             "  <point frame='20' label='Label &amp;&apos;&quot;&gt;' />\n"
-            "  <point frame='50' label='' />\n"
+            "  <point frame='50' />\n"
             "</dataset>\n";
         expected.replace("\'", "\"");
         if (xml != expected) {
@@ -226,7 +228,7 @@
         Event p2(20, 0.0f, "text 2");
         Event p3(50, 0.3f, "text 3");
         m.add(p1);
-        m.add(p2);
+        m.add(p2.withLevel(0.8f));
         m.add(p3);
         QString xml;
         QTextStream str(&xml, QIODevice::WriteOnly);
@@ -276,6 +278,28 @@
         QCOMPARE(xml, expected);
     }
 
+    void image_xml() {
+        ImageModel m(100, 10, false);
+        Event p1(20, 30, 40, "a label"); // value + duration should not be saved
+        m.add(p1.withURI("/path/to/thing.png").withLevel(0.8f));
+        QString xml;
+        QTextStream str(&xml, QIODevice::WriteOnly);
+        m.toXml(str);
+        str.flush();
+
+        QString expected =
+            "<model id='4' name='' sampleRate='100' start='20' end='30' type='sparse' dimensions='1' resolution='10' notifyOnAdd='true' dataset='2' subtype='image' />\n"
+            "<dataset id='2' dimensions='1'>\n"
+            "  <point frame='20' label='a label' image='/path/to/thing.png' />\n"
+            "</dataset>\n";
+        expected.replace("\'", "\"");
+        if (xml != expected) {
+            cerr << "Obtained xml:\n" << xml
+                 << "\nExpected:\n" << expected << endl;
+        }
+        QCOMPARE(xml, expected);
+    }
+
 };
 
 #endif