|
From: Tapio V. <aa...@us...> - 2011-01-13 20:01:57
|
Module: editor
Branch: master
Commit: a434204f30b1ec16b31e69f6347d051c57f23664
Author: Tapio Vierros <tap...@gm...>
Date: Thu Jan 13 22:00:21 2011 +0200
File I/O to saving & loading.
Projects can now be saved and opened.
Achieved by implementing Operator serialization to QDataStream.
---
editorapp.cc | 34 +++++++++++++++++++++++++++++-----
operation.cc | 26 ++++++++++++++++++++++++++
operation.hh | 6 ++++++
3 files changed, 61 insertions(+), 5 deletions(-)
diff --git a/editorapp.cc b/editorapp.cc
index f697b4d..ed258de 100644
--- a/editorapp.cc
+++ b/editorapp.cc
@@ -138,10 +138,27 @@ void EditorApp::on_actionOpen_triggered()
QFileInfo finfo(fileName);
try {
if (finfo.suffix() == PROJECT_SAVE_FILE_EXTENSION) {
- opStack.clear();
- // TODO: Load opstack
- doOpStack();
+
+ // Project file loading
+ QFile f(fileName);
+ if (f.open(QFile::ReadOnly)) {
+ opStack.clear();
+ QDataStream in(&f);
+ while (!in.atEnd()) {
+ Operation op;
+ in >> op;
+ std::cout << "Loaded op: " << op.dump() << std::endl;
+ opStack.push(op);
+ }
+ doOpStack();
+ projectFileName = fileName;
+ updateNoteInfo(NULL); // Title bar
+ } else
+ QMessageBox::critical(this, tr("Error saving file!"), tr("Couldn't open file %1 for saving.").arg(fileName));
+
} else {
+
+ // Song import
song.reset(new Song(QString(finfo.path()+"/").toStdString(), finfo.fileName().toStdString()));
noteGraph->setLyrics(song->getVocalTrack());
updateSongMeta(true);
@@ -177,8 +194,15 @@ void EditorApp::on_actionSaveAs_triggered()
void EditorApp::saveProject(QString fileName)
{
- projectFileName = fileName;
- updateNoteInfo(NULL); // Title bar
+ QFile f(fileName);
+ if (f.open(QFile::WriteOnly)) {
+ QDataStream out(&f);
+ foreach (Operation op, opStack)
+ out << op;
+ projectFileName = fileName;
+ updateNoteInfo(NULL); // Title bar
+ } else
+ QMessageBox::critical(this, tr("Error saving file!"), tr("Couldn't open file %1 for saving.").arg(fileName));
}
void EditorApp::on_actionSingStarXML_triggered()
diff --git a/operation.cc b/operation.cc
index 6385362..e8f80a9 100644
--- a/operation.cc
+++ b/operation.cc
@@ -1,2 +1,28 @@
#include "operation.hh"
+#include <stdexcept>
+namespace {
+ static const int MAX_PARAMS = 10;
+}
+
+QDataStream& operator<<(QDataStream& stream, const Operation& op)
+{
+ if (op.m_params.size() > MAX_PARAMS)
+ throw std::runtime_error("Operation has too many parameters");
+ int i = 0;
+ for (i; i < op.m_params.size(); ++i)
+ stream << op.m_params[i];
+ for (i; i < MAX_PARAMS; ++i)
+ stream << QVariant(0);
+ return stream;
+}
+
+QDataStream& operator>>(QDataStream& stream, Operation& op)
+{
+ for (int i = 0; i < MAX_PARAMS; ++i ) {
+ QVariant qv;
+ stream >> qv;
+ op << qv;
+ }
+ return stream;
+}
diff --git a/operation.hh b/operation.hh
index 2b0769f..4bcf05b 100644
--- a/operation.hh
+++ b/operation.hh
@@ -3,6 +3,7 @@
#include <QStack>
#include <QVariant>
#include <QTextStream>
+#include <QDataStream>
#include <ostream>
struct Operation
@@ -51,9 +52,14 @@ struct Operation
return st.toStdString();
}
+ friend QDataStream& operator<<(QDataStream&, const Operation&);
+
private:
QList<QVariant> m_params;
};
typedef QStack<Operation> OperationStack;
+// Serialization operators
+QDataStream& operator<<(QDataStream& stream, const Operation& op);
+QDataStream& operator>>(QDataStream& stream, Operation& op);
|