Add support for reading studio state

This commit is contained in:
Gary Talent 2017-05-13 13:42:26 -05:00
parent e4337e468b
commit 90db4e5f18
4 changed files with 54 additions and 11 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ nostalgia.gba
nostalgia.sav
nostalgia_media.oxfs
media_header.txt
studio_state.json

View File

@ -55,10 +55,11 @@ ox::Error JsonWriter::op(QString fieldName, T *src) {
template<typename T>
ox::Error JsonWriter::op(QString fieldName, QVector<T> *src) {
ox::Error err = 0;
auto &a = m_dest[fieldName] = QJsonArray();
QJsonArray a;
for (int i = 0; i < src->size(); i++) {
err |= op(a[i], &src->at(i));
}
m_dest[fieldName] = a;
return err;
};

View File

@ -16,8 +16,10 @@
#include <QLineEdit>
#include <QMenuBar>
#include <QTabBar>
#include <QTextStream>
#include <QVector>
#include "lib/json.hpp"
#include "lib/wizard.hpp"
#include "lib/oxfstreeview.hpp"
#include "lib/project.hpp"
@ -26,6 +28,8 @@
namespace nostalgia {
namespace studio {
const QString MainWindow::StateFilePath = "studio_state.json";
MainWindow::MainWindow(NostalgiaStudioProfile config, QWidget *parent) {
auto screenSize = QApplication::desktop()->screenGeometry();
@ -42,6 +46,8 @@ MainWindow::MainWindow(NostalgiaStudioProfile config, QWidget *parent) {
setupMenu();
setupProjectExplorer();
readState();
}
MainWindow::~MainWindow() {
@ -151,15 +157,48 @@ QAction *MainWindow::addAction(QMenu *menu, QString text, QString toolTip,
return action;
}
void MainWindow::openProject() {
auto p = QFileDialog::getExistingDirectory(this, tr("Select Project Directory..."), QDir::homePath());
auto project = QSharedPointer<Project>(new Project(p));
int MainWindow::readState(QString path) {
int err = 0;
QString json;
QFile file(path);
err |= file.open(QIODevice::ReadOnly);
json = QTextStream(&file).readAll();
file.close();
err |= readJson(json, &m_state);
err |= openProject(m_state.projectPath);
return err;
}
int MainWindow::writeState(QString path) {
int err = 0;
QString json;
err |= writeJson(&json, &m_state);
QFile file(path);
err |= file.open(QIODevice::WriteOnly);
QTextStream(&file) << json;
file.close();
return err;
}
int MainWindow::openProject(QString projectPath) {
auto project = QSharedPointer<Project>(new Project(projectPath));
auto err = project->open();
if (err == 0) {
m_project = project;
m_projectExplorer->setModel(new OxFSModel(m_project->romFS()));
m_importAction->setEnabled(true);
m_state.projectPath = projectPath;
}
return err;
}
int MainWindow::openProject() {
auto projectPath = QFileDialog::getExistingDirectory(this, tr("Select Project Directory..."), QDir::homePath());
auto err = openProject(projectPath);
if (err == 0) {
writeState();
}
return err;
}
void MainWindow::showNewWizard() {

View File

@ -57,8 +57,10 @@ class MainWindow: public QMainWindow {
public:
static const QString EditorSettings;
static const QString AppTitle;
static const QString StateFilePath;
private:
NostalgiaStudioState m_state;
QAction *m_importAction = nullptr;
QSharedPointer<Project> m_project;
QPointer<QMenu> m_viewMenu;
@ -71,8 +73,6 @@ class MainWindow: public QMainWindow {
virtual ~MainWindow();
void openProject(QString);
private:
void setupDockWidgets();
@ -85,17 +85,19 @@ class MainWindow: public QMainWindow {
QAction *addAction(QMenu *menu, QString text, QString toolTip, const QObject *tgt, const char *cb);
QAction *addAction(QMenu *menu, QString text, QString toolTip,
QKeySequence::StandardKey key, const QObject *tgt, const char *cb);
QKeySequence::StandardKey key, const QObject *tgt, const char *cb);
QAction *addAction(QMenu *menu, QString text, QString toolTip,
QKeySequence::StandardKey key, void (*cb)());
QKeySequence::StandardKey key, void (*cb)());
int readSettings(QString path);
int readState(QString path = StateFilePath);
int writeSettings(QString path);
int writeState(QString path = StateFilePath);
int openProject(QString);
private slots:
void openProject();
int openProject();
void showNewWizard();