From d55eb62bca39a4ef408ba3aceeb6f4c24699e8e8 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 12 May 2018 12:47:58 +0200 Subject: Add initial project serialisation/deserialisation. --- src/Makefile.am | 2 + src/mainwindow.cc | 36 +++++++++++++ src/mainwindow.h | 2 + src/project.cc | 7 +++ src/project.h | 4 ++ src/projectserialiser.cc | 134 +++++++++++++++++++++++++++++++++++++++++++++++ src/projectserialiser.h | 38 ++++++++++++++ 7 files changed, 223 insertions(+) create mode 100644 src/projectserialiser.cc create mode 100644 src/projectserialiser.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 2ef9ae1..15d63d1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -30,6 +30,7 @@ dgedit_SOURCES = \ player.cc \ project.cc \ projectdialog.cc \ + projectserialiser.cc \ samplesorter.cc \ selection.cc \ selectioneditor.cc \ @@ -53,6 +54,7 @@ EXTRA_DIST = \ player.h \ project.h \ projectdialog.h \ + projectserialiser.h \ samplesorter.h \ selection.h \ selectioneditor.h \ diff --git a/src/mainwindow.cc b/src/mainwindow.cc index ea86b3c..9dabde1 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -26,6 +26,8 @@ */ #include "mainwindow.h" +#include + #include #include @@ -52,6 +54,7 @@ #include "zoomslider.h" #include "settings.h" #include "projectdialog.h" +#include "projectserialiser.h" #define MAXVAL 10000000L @@ -107,6 +110,14 @@ MainWindow::MainWindow(Settings& settings) fileMenu->addAction(act_new_project); connect(act_new_project, SIGNAL(triggered()), this, SLOT(newProject())); + QAction* act_load_project = new QAction(tr("&Load Project..."), this); + fileMenu->addAction(act_load_project); + connect(act_load_project, SIGNAL(triggered()), this, SLOT(loadProject())); + + QAction* act_save_project = new QAction(tr("&Save Project..."), this); + fileMenu->addAction(act_save_project); + connect(act_save_project, SIGNAL(triggered()), this, SLOT(saveProject())); + QAction* act_quit = new QAction(tr("&Quit"), this); fileMenu->addAction(act_quit); connect(act_quit, SIGNAL(triggered()), this, SLOT(close())); @@ -457,6 +468,31 @@ void MainWindow::newProject() dlg.exec(); } +void MainWindow::loadProject() +{ + QFile file("test.dgedit"); + if(!file.open(QIODevice::ReadOnly)) + { + return; + } + + QString xml(file.readAll()); + file.close(); + + ProjectSerialiser ser; + ser.deserialise(xml, project); + + std::cout << project.getProjectName().toStdString() << std::endl; + std::cout << project.getRawFileRoot().toStdString() << std::endl; +} + +void MainWindow::saveProject() +{ + ProjectSerialiser ser; + auto xml = ser.serialise(project); + std::cout << xml.toStdString() << std::endl; +} + void MainWindow::projectChanged() { statusBar()->showMessage(tr("Unsaved")); diff --git a/src/mainwindow.h b/src/mainwindow.h index b6c7e98..d1df144 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -69,6 +69,8 @@ public: public slots: void newProject(); + void loadProject(); + void saveProject(); void projectChanged(); void doExport(); void loadFile(QString filename); diff --git a/src/project.cc b/src/project.cc index ffa9689..072425c 100644 --- a/src/project.cc +++ b/src/project.cc @@ -79,3 +79,10 @@ void Project::setRawFileRoot(const QString& raw_file_root) this->raw_file_root = raw_file_root; } } + +void Project::reset() +{ + RAIIBulkUpdate bulkUpdate(*this); + setRawFileRoot(""); + setProjectName(""); +} diff --git a/src/project.h b/src/project.h index 02970c0..306bc17 100644 --- a/src/project.h +++ b/src/project.h @@ -60,10 +60,14 @@ public: QString getRawFileRoot() const; void setRawFileRoot(const QString& raw_file_root); + void reset(); + signals: void projectChanged(); private: + friend class ProjectSerialiser; + QString project_name; QString raw_file_root; diff --git a/src/projectserialiser.cc b/src/projectserialiser.cc new file mode 100644 index 0000000..4b8247f --- /dev/null +++ b/src/projectserialiser.cc @@ -0,0 +1,134 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * projectserialiser.cc + * + * Sat May 12 10:11:22 CEST 2018 + * Copyright 2018 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo 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. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "projectserialiser.h" + +#include +#include + +#include + +/* + + + MyProject + /some/path + +*/ + +class DomHelper +{ +public: + DomHelper(const QDomDocument& doc) : elem(doc.documentElement()) {} + DomHelper(const QDomElement& elem) : elem(elem) {} + + DomHelper operator()(const QString& name) + { + auto node = elem.namedItem(name); + if(!node.isElement()) + { + std::cout << "No such child-element: '" << name.toStdString() << "'\n"; + QDomElement e; + return DomHelper(e); + } + + return DomHelper(node.toElement()); + } + + QString operator[](const QString& name) + { + auto attrs = elem.attributes(); + auto node = attrs.namedItem(name); + if(!node.isAttr()) + { + std::cout << "No such attribute: '" << name.toStdString() << "'\n"; + return ""; + } + return node.toAttr().value(); + } + + QString text() + { + return elem.text(); + } + + std::vector children() + { + std::vector children; + return children; + } + +private: + const QDomElement& elem; +}; + +QString ProjectSerialiser::serialise(const Project& project) +{ + QDomDocument doc; + auto header = + doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); + doc.appendChild(header); + + auto dgedit = doc.createElement("dgedit"); + doc.appendChild(dgedit); + + dgedit.setAttribute("version", "1.0"); + + auto project_name = doc.createElement("project_name"); + project_name.appendChild(doc.createTextNode(project.project_name)); + dgedit.appendChild(project_name); + + auto raw_file_root = doc.createElement("raw_file_root"); + raw_file_root.appendChild(doc.createTextNode(project.raw_file_root)); + dgedit.appendChild(raw_file_root); + + return doc.toString(); +} + +bool ProjectSerialiser::deserialise(const QString& data, Project& project) +{ + QDomDocument doc; + if(!doc.setContent(data)) + { + std::cout << "Could not parse XML\n"; + return false; + } + + project.reset(); + DomHelper dom(doc); + + QString version = dom["version"]; + if(version != "1.0") + { + std::cout << "Bad version: '" << version.toStdString() << "'\n"; + return false; + } + + project.project_name = dom("project_name").text(); + project.raw_file_root = dom("raw_file_root").text(); + + return true; +} diff --git a/src/projectserialiser.h b/src/projectserialiser.h new file mode 100644 index 0000000..c4d5931 --- /dev/null +++ b/src/projectserialiser.h @@ -0,0 +1,38 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * projectserialiser.h + * + * Sat May 12 10:11:22 CEST 2018 + * Copyright 2018 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo 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. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#pragma once + +#include + +#include "project.h" + +class ProjectSerialiser +{ +public: + QString serialise(const Project& project); + bool deserialise(const QString& data, Project& project); +}; -- cgit v1.2.3