# HG changeset patch # User Chris Cannam # Date 1553702795 0 # Node ID 69ab62d378bf14fc13a443a874d2d6e51a84e507 # Parent dfcd05e8bd2fde2308a46e8cd60699548daa3f29 Ensure image & text models get the proper attribute names (which are not the same as the default event ones) diff -r dfcd05e8bd2f -r 69ab62d378bf base/Event.h --- 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("\n"; diff -r dfcd05e8bd2f -r 69ab62d378bf base/EventSeries.cpp --- 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("\n") .arg(getObjectExportId(this)) .arg(extraAttributes); for (const auto &p: m_events) { - p.toXml(out, indent + " "); + p.toXml(out, indent + " ", "", options); } out << indent << "\n"; diff -r dfcd05e8bd2f -r 69ab62d378bf base/EventSeries.h --- 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: /** diff -r dfcd05e8bd2f -r 69ab62d378bf data/model/ImageModel.h --- 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: diff -r dfcd05e8bd2f -r 69ab62d378bf data/model/TextModel.h --- 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: diff -r dfcd05e8bd2f -r 69ab62d378bf data/model/test/TestSparseModels.h --- 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 #include @@ -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 = "\n" "\n" - " \n" + " \n" " \n" - " \n" + " \n" "\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 = + "\n" + "\n" + " \n" + "\n"; + expected.replace("\'", "\""); + if (xml != expected) { + cerr << "Obtained xml:\n" << xml + << "\nExpected:\n" << expected << endl; + } + QCOMPARE(xml, expected); + } + }; #endif