From f92d78241228f6a9c0b649c8addcc7ae82071167 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sun, 13 May 2018 09:35:53 +0200 Subject: Added list of Instrument to Project including serialiser/deserialiser. --- src/project.cc | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ src/project.h | 30 +++++++++++++++++++++ src/projectserialiser.cc | 43 +++++++++++++++++++++++++++-- 3 files changed, 141 insertions(+), 2 deletions(-) diff --git a/src/project.cc b/src/project.cc index 33c3054..ba7f279 100644 --- a/src/project.cc +++ b/src/project.cc @@ -28,6 +28,35 @@ #include +Instrument::Instrument(Project& project, int id) + : id(id) + , project(project) +{ +} + +int Instrument::getId() const +{ + return id; +} + +QString Instrument::getInstrumentName() const +{ + return instrument_name; +} + +void Instrument::setInstrumentName(const QString& instrument_name) +{ + if(this->instrument_name == instrument_name) + { + return; + } + + { + Project::RAIIBulkUpdate bulkUpdate(project); + this->instrument_name = instrument_name; + } +} + void Project::bulkUpdateBegin() { ++update_count; @@ -90,9 +119,50 @@ void Project::setRawFileRoot(const QString& raw_file_root) } } +Instrument& Project::getInstrument(int id) +{ + for(auto& instrument : instruments) + { + if(instrument.getId() == id) + { + return instrument; + } + } + + Q_ASSERT(false); // No such instrument id. +} + +int Project::createInstrument() +{ + RAIIBulkUpdate bulkUpdate(*this); + + instruments.emplace_back(Instrument(*this, next_id)); + ++next_id; + + return instruments.back().getId(); +} + +void Project::deleteInstrument(int id) +{ + RAIIBulkUpdate bulkUpdate(*this); + + for(auto it = instruments.begin(); it != instruments.end(); ++it) + { + if((*it).getId() == id) + { + instruments.erase(it); + return; + } + } + + Q_ASSERT(false); // No such instrument id. +} + void Project::reset() { RAIIBulkUpdate bulkUpdate(*this); setRawFileRoot(""); setProjectName(""); + instruments.clear(); + next_id = 0; } diff --git a/src/project.h b/src/project.h index 097b7c9..2124730 100644 --- a/src/project.h +++ b/src/project.h @@ -26,9 +26,32 @@ */ #pragma once +#include + #include #include +class Project; + +class Instrument +{ +public: + Instrument(Project& project, int id); + + int getId() const; + + QString getInstrumentName() const; + void setInstrumentName(const QString& instrument_name); + +private: + friend class ProjectSerialiser; + + int id; + QString instrument_name; + + Project& project; +}; + class Project : public QObject { @@ -63,6 +86,10 @@ public: QString getRawFileRoot() const; void setRawFileRoot(const QString& raw_file_root); + Instrument& getInstrument(int id); + int createInstrument(); + void deleteInstrument(int id); + void reset(); signals: @@ -75,5 +102,8 @@ private: QString project_name; QString raw_file_root; + std::list instruments; + int next_id{0}; + int update_count{0}; }; diff --git a/src/projectserialiser.cc b/src/projectserialiser.cc index 4b8247f..707ff18 100644 --- a/src/projectserialiser.cc +++ b/src/projectserialiser.cc @@ -75,14 +75,31 @@ public: return elem.text(); } - std::vector children() + // Get child nodes with tag_name or all child nodes if tag_name == "". + std::vector children(const QString& tag_name = "") { std::vector children; + auto child_nodes = elem.childNodes(); + for(int i = 0; i < child_nodes.count(); ++i) + { + auto node = child_nodes.at(i); + if(!node.isElement()) + { + continue; + } + + auto elem = node.toElement(); + if(elem.tagName() == tag_name || tag_name == "") + { + children.emplace_back(DomHelper(elem)); + } + } + return children; } private: - const QDomElement& elem; + QDomElement elem; }; QString ProjectSerialiser::serialise(const Project& project) @@ -105,6 +122,19 @@ QString ProjectSerialiser::serialise(const Project& project) raw_file_root.appendChild(doc.createTextNode(project.raw_file_root)); dgedit.appendChild(raw_file_root); + auto instruments = doc.createElement("instruments"); + dgedit.appendChild(instruments); + + for(const auto& i : project.instruments) + { + auto instrument = doc.createElement("instrument"); + instruments.appendChild(instrument); + + auto instrument_name = doc.createElement("instrument_name"); + instrument_name.appendChild(doc.createTextNode(i.instrument_name)); + instrument.appendChild(instrument_name); + } + return doc.toString(); } @@ -130,5 +160,14 @@ bool ProjectSerialiser::deserialise(const QString& data, Project& project) project.project_name = dom("project_name").text(); project.raw_file_root = dom("raw_file_root").text(); + auto instruments = dom("instruments").children("instrument"); + for(auto& instrument : instruments) + { + project.instruments.emplace_back(Instrument(project, project.next_id)); + ++project.next_id; + auto& instr = project.instruments.back(); + instr.instrument_name = instrument("instrument_name").text(); + } + return true; } -- cgit v1.2.3