Add new World wizard

This commit is contained in:
2017-12-20 22:41:14 -06:00
parent 2edee450aa
commit 4e50d80f5f
31 changed files with 415 additions and 94 deletions

View File

@@ -3,7 +3,6 @@ cmake_minimum_required(VERSION 2.8.11)
project(nostalgia-studio)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
add_executable(
nostalgia-studio

View File

@@ -11,17 +11,17 @@
namespace nostalgia {
namespace studio {
void Plugin::addNewWizard(QString name, std::function<QVector<QWizardPage*>()> make) {
m_newWizards.push_back({name, make});
}
QVector<WizardMaker> Plugin::newWizards(PluginArgs) {
QVector<WizardMaker> Plugin::newWizards(const Context *ctx) {
return {};
}
QVector<WizardMaker> Plugin::importWizards(PluginArgs) {
QVector<WizardMaker> Plugin::importWizards(const Context *ctx) {
return {};
}
QWidget *Plugin::makeEditor(QString path, const Context *ctx) {
return nullptr;
}
}
}

View File

@@ -21,27 +21,30 @@
namespace nostalgia {
namespace studio {
struct PluginArgs {
Project *&project;
struct Context {
QWidget *tabParent = nullptr;
const Project *project = nullptr;
};
struct WizardMaker {
QString name;
std::function<QVector<QWizardPage*>()> make;
std::function<int(QWizard*)> onAccept;
};
struct EditorMaker {
virtual QWidget *make(QString path, const Context *ctx) = 0;
};
class Plugin {
private:
QVector<WizardMaker> m_newWizards;
public:
void addNewWizard(QString name, std::function<QVector<QWizardPage*>()> make);
virtual QVector<WizardMaker> newWizards(const Context *ctx);
void addImportWizard(QString name, std::function<QVector<QWizardPage*>()> make);
virtual QVector<WizardMaker> importWizards(const Context *ctx);
virtual QVector<WizardMaker> newWizards(PluginArgs);
virtual QWidget *makeEditor(QString path, const Context *ctx);
virtual QVector<WizardMaker> importWizards(PluginArgs);
};
}

View File

@@ -65,7 +65,7 @@ int Project::openRomFs() {
}
}
int Project::saveRomFs() {
int Project::saveRomFs() const {
int err = 0;
QFile file(m_path + ROM_FILE);
err |= file.open(QIODevice::WriteOnly) == false;
@@ -78,17 +78,21 @@ FileSystem *Project::romFs() {
return m_fs;
}
int Project::mkdir(QString path) {
int Project::mkdir(QString path) const {
auto err = m_fs->mkdir(path.toUtf8().data());
emit updated(path);
return err;
}
int Project::write(QString path, uint8_t *buff, size_t buffLen) {
int Project::write(QString path, uint8_t *buff, size_t buffLen) const {
auto err = m_fs->write(path.toUtf8().data(), buff, buffLen);
emit updated(path);
return err;
}
ox::FileStat Project::stat(QString path) const {
return m_fs->stat(path.toUtf8().data());
}
}
}

View File

@@ -33,16 +33,19 @@ class Project: public QObject {
int openRomFs();
int saveRomFs();
int saveRomFs() const;
ox::FileSystem *romFs();
int mkdir(QString path);
int mkdir(QString path) const;
int write(QString path, uint8_t *buff, size_t buffLen);
int write(QString path, uint8_t *buff, size_t buffLen) const;
ox::FileStat stat(QString path) const;
signals:
void updated(QString path);
void updated(QString path) const;
};
}

View File

@@ -7,6 +7,7 @@
*/
#include <QComboBox>
#include <QDebug>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
@@ -14,6 +15,7 @@
#include <QPushButton>
#include <QVBoxLayout>
#include "plugin.hpp"
#include "wizard.hpp"
namespace nostalgia {
@@ -43,9 +45,9 @@ void WizardSelect::initializePage() {
emit completeChanged();
}
void WizardSelect::addOption(QString name, function<QVector<QWizardPage*>()> makePage) {
m_options[name] = makePage;
m_listWidget->addItem(name);
void WizardSelect::addOption(WizardMaker wm) {
m_options[wm.name] = {wm.make, wm.onAccept};
m_listWidget->addItem(wm.name);
}
bool WizardSelect::isComplete() const {
@@ -53,18 +55,21 @@ bool WizardSelect::isComplete() const {
}
void WizardSelect::itemSelected(int row) {
if (row > -1) {
auto w = wizard();
if (nextId() > -1) {
auto w = dynamic_cast<Wizard*>(wizard());
if (w and row > -1) {
// remove other pages
while (nextId() > -1) {
w->removePage(nextId());
}
auto selected = m_listWidget->currentItem()->text();
if (m_options.contains(selected)) {
for (auto p : m_options[selected]()) {
auto &o = m_options[selected];
for (auto p : o.make()) {
w->addPage(p);
}
w->setAccept(o.onAccept);
// for some reason the continue button only appears correctly after remove runs
w->removePage(w->addPage(new QWizardPage()));
}
@@ -313,7 +318,7 @@ void WizardFormPage::showValidationError(QString msg) {
// set message
if (msg != "") {
m_errorMsg->setText(tr("Error: ") + msg);
m_errorMsg->setText(msg);
} else {
m_errorMsg->setText("");
}
@@ -325,7 +330,7 @@ Wizard::Wizard(QString windowTitle, QWidget *parent): QWizard(parent) {
setModal(true);
}
void Wizard::setAccept(std::function<int()> acceptFunc) {
void Wizard::setAccept(std::function<int(QWizard*)> acceptFunc) {
m_acceptFunc = acceptFunc;
}
@@ -333,7 +338,7 @@ void Wizard::accept() {
auto page = dynamic_cast<WizardFormPage*>(currentPage());
if (page != nullptr && page->accept() == 0) {
QDialog::accept();
} else if (m_acceptFunc != nullptr && m_acceptFunc() == 0) {
} else if (m_acceptFunc != nullptr && m_acceptFunc(this) == 0) {
QDialog::accept();
}
}

View File

@@ -22,18 +22,25 @@
namespace nostalgia {
namespace studio {
class WizardMaker;
class WizardSelect: public QWizardPage {
Q_OBJECT
private:
QMap<QString, std::function<QVector<QWizardPage*>()>> m_options;
struct WizardFuncs {
std::function<QVector<QWizardPage*>()> make;
std::function<int(QWizard*)> onAccept;
};
QHash<QString, WizardFuncs> m_options;
QListWidget *m_listWidget = nullptr;
bool m_complete = false;
public:
WizardSelect();
void addOption(QString name, std::function<QVector<QWizardPage*>()> makePage);
void addOption(WizardMaker wm);
void initializePage() override;
@@ -109,12 +116,12 @@ class WizardConclusionPage: public QWizardPage {
class Wizard: public QWizard {
Q_OBJECT
private:
std::function<int()> m_acceptFunc;
std::function<int(QWizard*)> m_acceptFunc;
public:
Wizard(QString windowTitle, QWidget *parent = 0);
void setAccept(std::function<int()> acceptFunc);
void setAccept(std::function<int(QWizard*)> acceptFunc);
void accept();
};

View File

@@ -91,6 +91,8 @@ void MainWindow::loadPlugins() {
delete loader;
});
m_plugins.push_back(plugin);
} else {
qInfo() << loader->errorString();
}
}
}
@@ -232,14 +234,14 @@ int MainWindow::openProject(QString projectPath) {
auto project = new Project(projectPath);
err |= project->openRomFs();
if (err == 0) {
if (m_project) {
delete m_project;
m_project = nullptr;
if (m_ctx.project) {
delete m_ctx.project;
m_ctx.project = nullptr;
}
m_project = project;
m_oxfsView = new OxFSModel(m_project->romFs());
m_ctx.project = project;
m_oxfsView = new OxFSModel(project->romFs());
m_projectExplorer->setModel(m_oxfsView);
connect(m_project, SIGNAL(updated(QString)), m_oxfsView, SLOT(updateFile(QString)));
connect(m_ctx.project, SIGNAL(updated(QString)), m_oxfsView, SLOT(updateFile(QString)));
m_importAction->setEnabled(true);
m_state.projectPath = projectPath;
}
@@ -248,11 +250,11 @@ int MainWindow::openProject(QString projectPath) {
int MainWindow::closeProject() {
auto err = 0;
if (m_project) {
disconnect(m_project, SIGNAL(updated(QString)), m_oxfsView, SLOT(updateFile(QString)));
if (m_ctx.project) {
disconnect(m_ctx.project, SIGNAL(updated(QString)), m_oxfsView, SLOT(updateFile(QString)));
delete m_project;
m_project = nullptr;
delete m_ctx.project;
m_ctx.project = nullptr;
delete m_oxfsView;
m_oxfsView = nullptr;
@@ -293,45 +295,58 @@ void MainWindow::showNewWizard() {
Wizard wizard(tr("New..."));
auto ws = new WizardSelect();
wizard.addPage(ws);
ws->addOption(tr("Project"),
[&wizard, PROJECT_NAME, PROJECT_PATH]() {
QVector<QWizardPage*> pgs;
auto pg = new WizardFormPage();
pg->addLineEdit(tr("Project &Name:"), PROJECT_NAME + "*", "", [PROJECT_PATH, pg, &wizard](QString projectName) {
auto projectPath = wizard.field(PROJECT_PATH).toString();
// add new project wizard
ws->addOption(
WizardMaker {
tr("Project"),
[&wizard, PROJECT_NAME, PROJECT_PATH]() {
QVector<QWizardPage*> pgs;
auto pg = new WizardFormPage();
pg->addLineEdit(tr("Project &Name:"), PROJECT_NAME + "*", "", [PROJECT_PATH, pg, &wizard](QString projectName) {
auto projectPath = wizard.field(PROJECT_PATH).toString();
auto path = projectPath + "/" + projectName;
if (!QDir(path).exists()) {
return 0;
} else {
pg->showValidationError(tr("This project directory already exists."));
return 1;
}
}
);
pg->addPathBrowse(tr("Project &Path:"), PROJECT_PATH + "*", QDir::homePath(), QFileDialog::Directory);
pgs.push_back(pg);
pgs.push_back(new WizardConclusionPage(tr("Creating project: %1/%2"), {PROJECT_PATH, PROJECT_NAME}));
return pgs;
},
[this, PROJECT_NAME, PROJECT_PATH](QWizard *wizard) {
auto projectName = wizard->field(PROJECT_NAME).toString();
auto projectPath = wizard->field(PROJECT_PATH).toString();
if (QDir(projectPath).exists()) {
auto path = projectPath + "/" + projectName;
if (!QDir(path).exists()) {
Project(path).create();
openProject(path);
return 0;
} else {
pg->showValidationError(tr("This project directory already exists."));
return 1;
}
}
);
pg->addPathBrowse(tr("Project &Path:"), PROJECT_PATH + "*", QDir::homePath(), QFileDialog::Directory);
pgs.push_back(pg);
pgs.push_back(new WizardConclusionPage(tr("Creating project: ") + "%1/%2", {PROJECT_PATH, PROJECT_NAME}));
return pgs;
}
);
wizard.setAccept([this, &wizard, ws, PROJECT_NAME, PROJECT_PATH]() -> int {
auto projectName = wizard.field(PROJECT_NAME).toString();
auto projectPath = wizard.field(PROJECT_PATH).toString();
if (QDir(projectPath).exists()) {
auto path = projectPath + "/" + projectName;
if (!QDir(path).exists()) {
Project(path).create();
openProject(path);
return 0;
} else {
return 1;
return 2;
}
} else {
return 2;
}
}
);
// add plugin options
for (auto p : m_plugins) {
for (auto w : p->newWizards(&m_ctx)) {
ws->addOption(w);
}
}
wizard.show();
wizard.exec();
}
@@ -344,12 +359,9 @@ void MainWindow::showImportWizard() {
auto ws = new WizardSelect();
wizard.addPage(ws);
PluginArgs args {
.project = m_project
};
for (auto p : m_plugins) {
for (auto w : p->importWizards(args)) {
ws->addOption(w.name, w.make);
for (auto w : p->importWizards(&m_ctx)) {
ws->addOption(w);
}
}

View File

@@ -87,14 +87,14 @@ class MainWindow: public QMainWindow {
NostalgiaStudioProfile m_profile;
NostalgiaStudioState m_state;
QAction *m_importAction = nullptr;
Project *m_project = nullptr;
Context m_ctx;
QPointer<QMenu> m_viewMenu;
QVector<std::function<void()>> m_cleanupTasks;
QVector<QPointer<QDockWidget>> m_dockWidgets;
QTreeView *m_projectExplorer = nullptr;
QVector<Plugin*> m_plugins;
QPointer<OxFSModel> m_oxfsView = nullptr;
QTabBar *m_tabbar;
QTabBar *m_tabbar = nullptr;
public:
MainWindow(QString profilePath);

View File

@@ -5,6 +5,10 @@
{
"dir": "../lib/nostalgia",
"lib_name": "NostalgiaCore-Studio"
},
{
"dir": "../lib/nostalgia",
"lib_name": "NostalgiaWorld-Studio"
}
]
}