Add open project menu item to Nostalgia Studio

This commit is contained in:
Gary Talent 2017-05-10 22:45:18 -05:00
parent c971969a73
commit 3901493274
4 changed files with 70 additions and 7 deletions

View File

@ -8,7 +8,6 @@
#include <QByteArray> #include <QByteArray>
#include <QDir> #include <QDir>
#include <ox/fs/filesystem.hpp>
#include <project.hpp> #include <project.hpp>
namespace nostalgia { namespace nostalgia {
@ -16,25 +15,55 @@ namespace studio {
using namespace ox::fs; using namespace ox::fs;
QString Project::ROM_FILE = "/ROM.oxfs";
Project::Project(QString path) { Project::Project(QString path) {
m_path = path; m_path = path;
} }
Project::~Project() {
if (m_fs) {
delete m_fs;
m_fs = nullptr;
}
if (m_romBuff) {
delete m_romBuff;
m_romBuff = nullptr;
}
}
void Project::create() { void Project::create() {
QDir().mkpath(m_path); QDir().mkpath(m_path);
m_romBuff = new QByteArray(1024, 0); m_romBuff = new QByteArray(1024, 0);
FileSystem32::format(m_romBuff->data(), m_romBuff->size(), true); FileSystem32::format(m_romBuff->data(), m_romBuff->size(), true);
auto fs = ox::fs::createFileSystem(m_romBuff->data(), m_romBuff->size()); m_fs = createFileSystem(m_romBuff->data(), m_romBuff->size());
fs->mkdir("/Tilesets"); m_fs->mkdir("/Tilesets");
QFile file(m_path + "/ROM.oxfs"); QFile file(m_path + ROM_FILE);
file.open(QIODevice::WriteOnly); file.open(QIODevice::WriteOnly);
file.write(*m_romBuff); file.write(*m_romBuff);
file.close(); file.close();
}
delete fs; int Project::open() {
QFile file(m_path + ROM_FILE);
m_romBuff = new QByteArray(file.size(), 0);
file.open(QIODevice::ReadOnly);
if (file.read(m_romBuff->data(), file.size()) > 0) {
m_fs = createFileSystem(m_romBuff->data(), m_romBuff->size());
return 0;
} else {
return 1;
}
}
void Project::save() {
QFile file(m_path + ROM_FILE);
file.open(QIODevice::WriteOnly);
file.write(*m_romBuff);
file.close();
} }
} }

View File

@ -8,6 +8,8 @@
#pragma once #pragma once
#include <ox/fs/filesystem.hpp>
namespace nostalgia { namespace nostalgia {
namespace studio { namespace studio {
@ -15,13 +17,22 @@ class Project: public QObject {
Q_OBJECT Q_OBJECT
private: private:
static QString ROM_FILE;
QString m_path = ""; QString m_path = "";
QByteArray *m_romBuff = nullptr; QByteArray *m_romBuff = nullptr;
ox::fs::FileSystem *m_fs = nullptr;
public: public:
Project(QString path); Project(QString path);
~Project();
void create(); void create();
int open();
void save();
}; };
} }

View File

@ -9,6 +9,7 @@
#include <QApplication> #include <QApplication>
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QDialog> #include <QDialog>
#include <QFileDialog>
#include <QGridLayout> #include <QGridLayout>
#include <QLabel> #include <QLabel>
#include <QLineEdit> #include <QLineEdit>
@ -63,6 +64,16 @@ void MainWindow::setupMenu() {
QKeySequence::Quit, QKeySequence::Quit,
QApplication::quit QApplication::quit
); );
addAction(
fileMenu,
tr("Open &Project"),
tr(""),
QKeySequence::Open,
this,
SLOT(openProject())
);
} }
void MainWindow::addAction(QMenu *menu, QString text, QString toolTip, void MainWindow::addAction(QMenu *menu, QString text, QString toolTip,
@ -87,6 +98,13 @@ void MainWindow::addAction(QMenu *menu, QString text, QString toolTip,
}); });
} }
void MainWindow::openProject() {
auto p = QFileDialog::getExistingDirectory(this, tr("Select Project Directory..."), QDir::homePath());
auto project = new Project(p);
project->open();
m_project = project;
}
void MainWindow::showNewWizard() { void MainWindow::showNewWizard() {
const QString PROJECT_NAME = "projectName"; const QString PROJECT_NAME = "projectName";
const QString PROJECT_PATH = "projectPath"; const QString PROJECT_PATH = "projectPath";

View File

@ -8,7 +8,6 @@
#pragma once #pragma once
#include <ox/std/types.hpp>
#include <QModelIndex> #include <QModelIndex>
#include <QMainWindow> #include <QMainWindow>
#include <QPoint> #include <QPoint>
@ -16,6 +15,10 @@
#include <QVector> #include <QVector>
#include <functional> #include <functional>
#include <ox/std/types.hpp>
#include "lib/project.hpp"
namespace nostalgia { namespace nostalgia {
namespace studio { namespace studio {
@ -38,7 +41,7 @@ class MainWindow: public QMainWindow {
static const QString AppTitle; static const QString AppTitle;
private: private:
QString m_projectPath; Project *m_project = nullptr;
QVector<std::function<void()>> m_cleanupTasks; QVector<std::function<void()>> m_cleanupTasks;
public: public:
@ -63,6 +66,8 @@ class MainWindow: public QMainWindow {
int writeSettings(QString path); int writeSettings(QString path);
private slots: private slots:
void openProject();
void showNewWizard(); void showNewWizard();
}; };