Finish UI for tile sheet importer

This commit is contained in:
Gary Talent 2017-05-13 22:42:56 -05:00
parent 90db4e5f18
commit a9887c0803
4 changed files with 142 additions and 41 deletions

View File

@ -6,13 +6,14 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#include <QFileDialog> #include <QComboBox>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>
#include <QLineEdit> #include <QLineEdit>
#include <QListWidget> #include <QListWidget>
#include <QPushButton> #include <QPushButton>
#include <QVBoxLayout> #include <QVBoxLayout>
#include "wizard.hpp" #include "wizard.hpp"
namespace nostalgia { namespace nostalgia {
@ -97,6 +98,28 @@ void WizardConclusionPage::initializePage() {
} }
void WizardFormPage::Field::setDisplayText(QString text) {
auto le = dynamic_cast<QLineEdit*>(this->valueControl);
auto cb = dynamic_cast<QComboBox*>(this->valueControl);
if (le) {
le->setText(text);
} else if (cb) {
cb->setCurrentText(text);
}
}
QString WizardFormPage::Field::getDisplayText() {
auto le = dynamic_cast<QLineEdit*>(this->valueControl);
auto cb = dynamic_cast<QComboBox*>(this->valueControl);
if (le) {
return le->text();
} else if (cb) {
return cb->currentText();
} else {
return "";
}
}
WizardFormPage::WizardFormPage() { WizardFormPage::WizardFormPage() {
m_layout = new QGridLayout(this); m_layout = new QGridLayout(this);
m_layout->setColumnMinimumWidth(0, 20); m_layout->setColumnMinimumWidth(0, 20);
@ -112,11 +135,8 @@ WizardFormPage::~WizardFormPage() {
void WizardFormPage::initializePage() { void WizardFormPage::initializePage() {
for (auto it = m_fields.begin(); it != m_fields.end(); it++) { for (auto it = m_fields.begin(); it != m_fields.end(); it++) {
auto key = it.key(); auto key = it.key();
auto le = m_fields[key].lineEdit;
auto defaultVal = it.value().defaultValue; auto defaultVal = it.value().defaultValue;
if (le) { m_fields[key].setDisplayText(defaultVal);
le->setText(defaultVal);
}
} }
} }
@ -126,7 +146,7 @@ bool WizardFormPage::validatePage() {
// check validators // check validators
for (auto f : m_fields) { for (auto f : m_fields) {
if (f.validator != nullptr) { if (f.validator != nullptr) {
if (f.validator(f.lineEdit->text()) != 0) { if (f.validator(f.getDisplayText()) != 0) {
retval = false; retval = false;
break; break;
} }
@ -141,6 +161,38 @@ bool WizardFormPage::validatePage() {
return retval; return retval;
} }
void WizardFormPage::addComboBox(QString displayName, QString fieldName, QVector<QString> 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<int(QString)> validator) { void WizardFormPage::addLineEdit(QString displayName, QString fieldName, QString defaultVal, function<int(QString)> validator) {
auto lbl = new QLabel(displayName, this); auto lbl = new QLabel(displayName, this);
auto le = new QLineEdit(this); auto le = new QLineEdit(this);
@ -152,7 +204,7 @@ void WizardFormPage::addLineEdit(QString displayName, QString fieldName, QString
auto field = &m_fields[fieldName]; auto field = &m_fields[fieldName];
field->defaultValue = defaultVal; field->defaultValue = defaultVal;
field->lineEdit = le; field->valueControl = le;
field->validator = validator; field->validator = validator;
registerField(fieldName, le); registerField(fieldName, le);
@ -171,7 +223,8 @@ void WizardFormPage::addLineEdit(QString displayName, QString fieldName, QString
m_currentLine++; 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 layout = new QHBoxLayout();
auto lbl = new QLabel(displayName, this); auto lbl = new QLabel(displayName, this);
auto le = new QLineEdit("", this); auto le = new QLineEdit("", this);
@ -185,13 +238,24 @@ void WizardFormPage::addDirBrowse(QString displayName, QString fieldName, QStrin
m_subLayout.push_back(layout); m_subLayout.push_back(layout);
m_fields[fieldName].defaultValue = defaultVal; m_fields[fieldName].defaultValue = defaultVal;
m_fields[fieldName].lineEdit = le; m_fields[fieldName].valueControl = le;
m_fields[fieldName].validator = [this](QString path) { m_fields[fieldName].validator = [this, fileMode](QString path) {
if (!QDir(path).exists()) { if (fileMode == QFileDialog::Directory) {
showValidationError(tr("Specified Project Path directory does not exist.")); if (!QDir(path).exists()) {
return 1; 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 { } else {
return 0; return 2;
} }
}; };
@ -208,10 +272,17 @@ void WizardFormPage::addDirBrowse(QString displayName, QString fieldName, QStrin
} }
); );
connect(btn, &QPushButton::clicked, [this, defaultVal, le]() { connect(btn, &QPushButton::clicked, [this, defaultVal, le, fileMode]() {
auto p = QFileDialog::getExistingDirectory(this, tr("Select Directory..."), defaultVal); if (fileMode == QFileDialog::Directory) {
if (p != "") { auto p = QFileDialog::getExistingDirectory(this, tr("Select Directory..."), defaultVal);
le->setText(p); if (p != "") {
le->setText(p);
}
} else if (fileMode == QFileDialog::ExistingFile) {
auto p = QFileDialog::getOpenFileName(this, tr("Select File..."), defaultVal);
if (p != "") {
le->setText(p);
}
} }
} }
); );

View File

@ -9,7 +9,9 @@
#pragma once #pragma once
#include <functional> #include <functional>
#include <QDir> #include <QDir>
#include <QFileDialog>
#include <QGridLayout> #include <QGridLayout>
#include <QLabel> #include <QLabel>
#include <QListWidget> #include <QListWidget>
@ -48,8 +50,12 @@ class WizardFormPage: public QWizardPage {
struct Field { struct Field {
QString defaultValue = ""; QString defaultValue = "";
QString value = ""; QString value = "";
QLineEdit *lineEdit = nullptr; QWidget *valueControl = nullptr;
std::function<int(QString)> validator; std::function<int(QString)> validator;
void setDisplayText(QString text);
QString getDisplayText();
}; };
QLabel *m_errorMsg = nullptr; QLabel *m_errorMsg = nullptr;
QGridLayout *m_layout = nullptr; QGridLayout *m_layout = nullptr;
@ -67,11 +73,14 @@ class WizardFormPage: public QWizardPage {
bool validatePage() override; bool validatePage() override;
void addComboBox(QString displayName, QString fieldName, QVector<QString> options);
void addLineEdit(QString displayName, QString fieldName, void addLineEdit(QString displayName, QString fieldName,
QString defaultVal = "", QString defaultVal = "",
std::function<int(QString)> validator = [](QString) { return 0; }); std::function<int(QString)> 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); void showValidationError(QString msg);
}; };

View File

@ -51,9 +51,7 @@ MainWindow::MainWindow(NostalgiaStudioProfile config, QWidget *parent) {
} }
MainWindow::~MainWindow() { MainWindow::~MainWindow() {
if (m_projectExplorer->model()) { closeProject();
delete m_projectExplorer->model();
}
for (auto f : m_cleanupTasks) { for (auto f : m_cleanupTasks) {
f(); f();
} }
@ -161,7 +159,7 @@ int MainWindow::readState(QString path) {
int err = 0; int err = 0;
QString json; QString json;
QFile file(path); QFile file(path);
err |= file.open(QIODevice::ReadOnly); err |= !file.open(QIODevice::ReadOnly);
json = QTextStream(&file).readAll(); json = QTextStream(&file).readAll();
file.close(); file.close();
err |= readJson(json, &m_state); err |= readJson(json, &m_state);
@ -174,15 +172,16 @@ int MainWindow::writeState(QString path) {
QString json; QString json;
err |= writeJson(&json, &m_state); err |= writeJson(&json, &m_state);
QFile file(path); QFile file(path);
err |= file.open(QIODevice::WriteOnly); err |= !file.open(QIODevice::WriteOnly);
QTextStream(&file) << json; QTextStream(&file) << json;
file.close(); file.close();
return err; return err;
} }
int MainWindow::openProject(QString projectPath) { int MainWindow::openProject(QString projectPath) {
auto err = closeProject();
auto project = QSharedPointer<Project>(new Project(projectPath)); auto project = QSharedPointer<Project>(new Project(projectPath));
auto err = project->open(); err |= project->open();
if (err == 0) { if (err == 0) {
m_project = project; m_project = project;
m_projectExplorer->setModel(new OxFSModel(m_project->romFS())); m_projectExplorer->setModel(new OxFSModel(m_project->romFS()));
@ -192,11 +191,31 @@ int MainWindow::openProject(QString projectPath) {
return err; return err;
} }
int MainWindow::closeProject() {
auto err = 0;
m_project = QSharedPointer<Project>(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() { int MainWindow::openProject() {
auto projectPath = QFileDialog::getExistingDirectory(this, tr("Select Project Directory..."), QDir::homePath()); auto projectPath = QFileDialog::getExistingDirectory(this, tr("Select Project Directory..."), QDir::homePath());
auto err = openProject(projectPath); int err = 0;
if (err == 0) { if (projectPath != "") {
writeState(); err |= openProject(projectPath);
if (err == 0) {
err |= writeState();
}
} }
return err; return err;
} }
@ -208,7 +227,7 @@ void MainWindow::showNewWizard() {
auto ws = new WizardSelect(); auto ws = new WizardSelect();
wizard.addPage(ws); wizard.addPage(ws);
ws->addOption(tr("Project"), ws->addOption(tr("Project"),
[&wizard, PROJECT_NAME, PROJECT_PATH]() { [&wizard, PROJECT_NAME, PROJECT_PATH]() {
QVector<QWizardPage*> pgs; QVector<QWizardPage*> pgs;
auto pg = new WizardFormPage(); auto pg = new WizardFormPage();
pg->addLineEdit(tr("Project &Name:"), PROJECT_NAME + "*", "", [PROJECT_PATH, pg, &wizard](QString projectName) { 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(pg);
pgs.push_back(new WizardConclusionPage(tr("Creating project: ") + "%1/%2", {PROJECT_PATH, PROJECT_NAME})); pgs.push_back(new WizardConclusionPage(tr("Creating project: ") + "%1/%2", {PROJECT_PATH, PROJECT_NAME}));
@ -247,12 +266,13 @@ void MainWindow::showNewWizard() {
void MainWindow::showImportWizard() { void MainWindow::showImportWizard() {
const QString TILESHEET_NAME = "projectName"; const QString TILESHEET_NAME = "projectName";
const QString IMPORT_PATH = "projectPath"; const QString IMPORT_PATH = "projectPath";
const QString BPP = "bpp";
Wizard wizard(tr("Import...")); Wizard wizard(tr("Import..."));
auto ws = new WizardSelect(); auto ws = new WizardSelect();
wizard.addPage(ws); wizard.addPage(ws);
ws->addOption(tr("Tile Sheet"), ws->addOption(tr("Tile Sheet"),
[&wizard, TILESHEET_NAME, IMPORT_PATH]() { [&wizard, TILESHEET_NAME, IMPORT_PATH, BPP]() {
QVector<QWizardPage*> pgs; QVector<QWizardPage*> pgs;
auto pg = new WizardFormPage(); auto pg = new WizardFormPage();
pg->addLineEdit(tr("Tile Sheet &Name:"), TILESHEET_NAME + "*", "", [IMPORT_PATH, pg, &wizard](QString projectName) { 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(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; return pgs;
} }
); );
wizard.setAccept([&wizard, ws, TILESHEET_NAME, IMPORT_PATH]() { wizard.setAccept([&wizard, ws, TILESHEET_NAME, IMPORT_PATH]() {
auto projectName = wizard.field(TILESHEET_NAME).toString(); auto tilesheetName = wizard.field(TILESHEET_NAME).toString();
auto projectPath = wizard.field(IMPORT_PATH).toString(); auto importPath = wizard.field(IMPORT_PATH).toString();
if (QDir(projectPath).exists()) { if (QFile(importPath).exists()) {
auto path = projectPath + "/" + projectName;
if (QDir(path).exists()) {
}
} }
} }
); );

View File

@ -8,6 +8,8 @@
#pragma once #pragma once
#include <functional>
#include <QDockWidget> #include <QDockWidget>
#include <QMainWindow> #include <QMainWindow>
#include <QModelIndex> #include <QModelIndex>
@ -17,7 +19,6 @@
#include <QString> #include <QString>
#include <QTreeView> #include <QTreeView>
#include <QVector> #include <QVector>
#include <functional>
#include <ox/std/types.hpp> #include <ox/std/types.hpp>
@ -96,6 +97,8 @@ class MainWindow: public QMainWindow {
int openProject(QString); int openProject(QString);
int closeProject();
private slots: private slots:
int openProject(); int openProject();