diff --git a/src/nostalgia/studio/lib/wizard.cpp b/src/nostalgia/studio/lib/wizard.cpp index c44186ae..8079f72a 100644 --- a/src/nostalgia/studio/lib/wizard.cpp +++ b/src/nostalgia/studio/lib/wizard.cpp @@ -6,13 +6,14 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include +#include #include #include #include #include #include #include + #include "wizard.hpp" namespace nostalgia { @@ -97,6 +98,28 @@ void WizardConclusionPage::initializePage() { } +void WizardFormPage::Field::setDisplayText(QString text) { + auto le = dynamic_cast(this->valueControl); + auto cb = dynamic_cast(this->valueControl); + if (le) { + le->setText(text); + } else if (cb) { + cb->setCurrentText(text); + } +} + +QString WizardFormPage::Field::getDisplayText() { + auto le = dynamic_cast(this->valueControl); + auto cb = dynamic_cast(this->valueControl); + if (le) { + return le->text(); + } else if (cb) { + return cb->currentText(); + } else { + return ""; + } +} + WizardFormPage::WizardFormPage() { m_layout = new QGridLayout(this); m_layout->setColumnMinimumWidth(0, 20); @@ -112,11 +135,8 @@ WizardFormPage::~WizardFormPage() { void WizardFormPage::initializePage() { for (auto it = m_fields.begin(); it != m_fields.end(); it++) { auto key = it.key(); - auto le = m_fields[key].lineEdit; auto defaultVal = it.value().defaultValue; - if (le) { - le->setText(defaultVal); - } + m_fields[key].setDisplayText(defaultVal); } } @@ -126,7 +146,7 @@ bool WizardFormPage::validatePage() { // check validators for (auto f : m_fields) { if (f.validator != nullptr) { - if (f.validator(f.lineEdit->text()) != 0) { + if (f.validator(f.getDisplayText()) != 0) { retval = false; break; } @@ -141,6 +161,38 @@ bool WizardFormPage::validatePage() { return retval; } +void WizardFormPage::addComboBox(QString displayName, QString fieldName, QVector options) { + auto lbl = new QLabel(displayName, this); + auto cb = new QComboBox(this); + lbl->setBuddy(cb); + + m_layout->addWidget(lbl, m_currentLine, 0); + m_layout->addWidget(cb, m_currentLine, 1); + + auto field = &m_fields[fieldName]; + + field->valueControl = cb; + + registerField(fieldName, cb); + + for (auto o : options) { + cb->addItem(o); + } + + connect(cb, &QComboBox::currentTextChanged, [this, fieldName, cb, field](QString txt) { + if (field->value == "" && txt != "") { + m_validFields++; + } else if (field->value != "" && txt == "") { + m_validFields--; + } + field->value = txt; + emit completeChanged(); + } + ); + + m_currentLine++; +} + void WizardFormPage::addLineEdit(QString displayName, QString fieldName, QString defaultVal, function validator) { auto lbl = new QLabel(displayName, this); auto le = new QLineEdit(this); @@ -152,7 +204,7 @@ void WizardFormPage::addLineEdit(QString displayName, QString fieldName, QString auto field = &m_fields[fieldName]; field->defaultValue = defaultVal; - field->lineEdit = le; + field->valueControl = le; field->validator = validator; registerField(fieldName, le); @@ -171,7 +223,8 @@ void WizardFormPage::addLineEdit(QString displayName, QString fieldName, QString m_currentLine++; } -void WizardFormPage::addDirBrowse(QString displayName, QString fieldName, QString defaultVal) { +void WizardFormPage::addPathBrowse(QString displayName, QString fieldName, + QString defaultVal, QFileDialog::FileMode fileMode) { auto layout = new QHBoxLayout(); auto lbl = new QLabel(displayName, this); auto le = new QLineEdit("", this); @@ -185,13 +238,24 @@ void WizardFormPage::addDirBrowse(QString displayName, QString fieldName, QStrin m_subLayout.push_back(layout); m_fields[fieldName].defaultValue = defaultVal; - m_fields[fieldName].lineEdit = le; - m_fields[fieldName].validator = [this](QString path) { - if (!QDir(path).exists()) { - showValidationError(tr("Specified Project Path directory does not exist.")); - return 1; + m_fields[fieldName].valueControl = le; + m_fields[fieldName].validator = [this, fileMode](QString path) { + if (fileMode == QFileDialog::Directory) { + if (!QDir(path).exists()) { + showValidationError(tr("Specified directory path does not exist.")); + return 1; + } else { + return 0; + } + } else if (fileMode == QFileDialog::ExistingFile) { + if (!QFile(path).exists()) { + showValidationError(tr("Specified directory path does not exist.")); + return 1; + } else { + return 0; + } } else { - return 0; + return 2; } }; @@ -208,10 +272,17 @@ void WizardFormPage::addDirBrowse(QString displayName, QString fieldName, QStrin } ); - connect(btn, &QPushButton::clicked, [this, defaultVal, le]() { - auto p = QFileDialog::getExistingDirectory(this, tr("Select Directory..."), defaultVal); - if (p != "") { - le->setText(p); + connect(btn, &QPushButton::clicked, [this, defaultVal, le, fileMode]() { + if (fileMode == QFileDialog::Directory) { + auto p = QFileDialog::getExistingDirectory(this, tr("Select Directory..."), defaultVal); + if (p != "") { + le->setText(p); + } + } else if (fileMode == QFileDialog::ExistingFile) { + auto p = QFileDialog::getOpenFileName(this, tr("Select File..."), defaultVal); + if (p != "") { + le->setText(p); + } } } ); diff --git a/src/nostalgia/studio/lib/wizard.hpp b/src/nostalgia/studio/lib/wizard.hpp index 888581b3..33629fe4 100644 --- a/src/nostalgia/studio/lib/wizard.hpp +++ b/src/nostalgia/studio/lib/wizard.hpp @@ -9,7 +9,9 @@ #pragma once #include + #include +#include #include #include #include @@ -48,8 +50,12 @@ class WizardFormPage: public QWizardPage { struct Field { QString defaultValue = ""; QString value = ""; - QLineEdit *lineEdit = nullptr; + QWidget *valueControl = nullptr; std::function validator; + + void setDisplayText(QString text); + + QString getDisplayText(); }; QLabel *m_errorMsg = nullptr; QGridLayout *m_layout = nullptr; @@ -67,11 +73,14 @@ class WizardFormPage: public QWizardPage { bool validatePage() override; + void addComboBox(QString displayName, QString fieldName, QVector options); + void addLineEdit(QString displayName, QString fieldName, QString defaultVal = "", std::function validator = [](QString) { return 0; }); - void addDirBrowse(QString displayName, QString fieldName, QString defaultVal = QDir::homePath()); + void addPathBrowse(QString displayName, QString fieldName, QString defaultVal = QDir::homePath(), + QFileDialog::FileMode fileMode = QFileDialog::AnyFile); void showValidationError(QString msg); }; diff --git a/src/nostalgia/studio/mainwindow.cpp b/src/nostalgia/studio/mainwindow.cpp index 8685cfbd..d2941443 100644 --- a/src/nostalgia/studio/mainwindow.cpp +++ b/src/nostalgia/studio/mainwindow.cpp @@ -51,9 +51,7 @@ MainWindow::MainWindow(NostalgiaStudioProfile config, QWidget *parent) { } MainWindow::~MainWindow() { - if (m_projectExplorer->model()) { - delete m_projectExplorer->model(); - } + closeProject(); for (auto f : m_cleanupTasks) { f(); } @@ -161,7 +159,7 @@ int MainWindow::readState(QString path) { int err = 0; QString json; QFile file(path); - err |= file.open(QIODevice::ReadOnly); + err |= !file.open(QIODevice::ReadOnly); json = QTextStream(&file).readAll(); file.close(); err |= readJson(json, &m_state); @@ -174,15 +172,16 @@ int MainWindow::writeState(QString path) { QString json; err |= writeJson(&json, &m_state); QFile file(path); - err |= file.open(QIODevice::WriteOnly); + err |= !file.open(QIODevice::WriteOnly); QTextStream(&file) << json; file.close(); return err; } int MainWindow::openProject(QString projectPath) { + auto err = closeProject(); auto project = QSharedPointer(new Project(projectPath)); - auto err = project->open(); + err |= project->open(); if (err == 0) { m_project = project; m_projectExplorer->setModel(new OxFSModel(m_project->romFS())); @@ -192,11 +191,31 @@ int MainWindow::openProject(QString projectPath) { return err; } +int MainWindow::closeProject() { + auto err = 0; + m_project = QSharedPointer(nullptr); + if (m_projectExplorer->model()) { + delete m_projectExplorer->model(); + } + m_projectExplorer->setModel(nullptr); + + m_importAction->setEnabled(false); + + m_state.projectPath = ""; + return err; +} + + +// private slots + int MainWindow::openProject() { auto projectPath = QFileDialog::getExistingDirectory(this, tr("Select Project Directory..."), QDir::homePath()); - auto err = openProject(projectPath); - if (err == 0) { - writeState(); + int err = 0; + if (projectPath != "") { + err |= openProject(projectPath); + if (err == 0) { + err |= writeState(); + } } return err; } @@ -208,7 +227,7 @@ void MainWindow::showNewWizard() { auto ws = new WizardSelect(); wizard.addPage(ws); ws->addOption(tr("Project"), - [&wizard, PROJECT_NAME, PROJECT_PATH]() { + [&wizard, PROJECT_NAME, PROJECT_PATH]() { QVector pgs; auto pg = new WizardFormPage(); pg->addLineEdit(tr("Project &Name:"), PROJECT_NAME + "*", "", [PROJECT_PATH, pg, &wizard](QString projectName) { @@ -222,7 +241,7 @@ void MainWindow::showNewWizard() { } } ); - pg->addDirBrowse(tr("Project &Path:"), PROJECT_PATH + "*"); + 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})); @@ -247,12 +266,13 @@ void MainWindow::showNewWizard() { void MainWindow::showImportWizard() { const QString TILESHEET_NAME = "projectName"; const QString IMPORT_PATH = "projectPath"; + const QString BPP = "bpp"; Wizard wizard(tr("Import...")); auto ws = new WizardSelect(); wizard.addPage(ws); ws->addOption(tr("Tile Sheet"), - [&wizard, TILESHEET_NAME, IMPORT_PATH]() { + [&wizard, TILESHEET_NAME, IMPORT_PATH, BPP]() { QVector pgs; auto pg = new WizardFormPage(); pg->addLineEdit(tr("Tile Sheet &Name:"), TILESHEET_NAME + "*", "", [IMPORT_PATH, pg, &wizard](QString projectName) { @@ -266,20 +286,18 @@ void MainWindow::showImportWizard() { } } ); - pg->addDirBrowse(tr("Project &Path:"), IMPORT_PATH + "*"); + pg->addPathBrowse(tr("Tile Sheet &Path:"), IMPORT_PATH + "*", QDir::homePath(), QFileDialog::ExistingFile); + pg->addComboBox(tr("Bits Per Pixe&l:"), BPP, {"4", "8"}); pgs.push_back(pg); - pgs.push_back(new WizardConclusionPage(tr("Importing tile sheet: ") + "%1/%2", {IMPORT_PATH})); + pgs.push_back(new WizardConclusionPage(tr("Importing tile sheet: %1 as %2"), {IMPORT_PATH, TILESHEET_NAME})); return pgs; } ); wizard.setAccept([&wizard, ws, TILESHEET_NAME, IMPORT_PATH]() { - auto projectName = wizard.field(TILESHEET_NAME).toString(); - auto projectPath = wizard.field(IMPORT_PATH).toString(); - if (QDir(projectPath).exists()) { - auto path = projectPath + "/" + projectName; - if (QDir(path).exists()) { - } + auto tilesheetName = wizard.field(TILESHEET_NAME).toString(); + auto importPath = wizard.field(IMPORT_PATH).toString(); + if (QFile(importPath).exists()) { } } ); diff --git a/src/nostalgia/studio/mainwindow.hpp b/src/nostalgia/studio/mainwindow.hpp index 15023e4b..87acc6ef 100644 --- a/src/nostalgia/studio/mainwindow.hpp +++ b/src/nostalgia/studio/mainwindow.hpp @@ -8,6 +8,8 @@ #pragma once +#include + #include #include #include @@ -17,7 +19,6 @@ #include #include #include -#include #include @@ -96,6 +97,8 @@ class MainWindow: public QMainWindow { int openProject(QString); + int closeProject(); + private slots: int openProject();