From 7a6174953dfc43b4901d73504fd9dea7675b4860 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 19 Aug 2017 12:31:53 -0500 Subject: [PATCH] Fix OxFSTreeView to properly list added files --- .../core/studio/import_tilesheet_wizard.cpp | 3 +- .../core/studio/import_tilesheet_wizard.hpp | 2 +- src/nostalgia/studio/lib/oxfstreeview.cpp | 70 ++++++++++++++----- src/nostalgia/studio/lib/oxfstreeview.hpp | 10 ++- src/nostalgia/studio/lib/plugin.hpp | 2 +- src/nostalgia/studio/lib/project.cpp | 8 ++- src/nostalgia/studio/lib/project.hpp | 3 + src/nostalgia/studio/mainwindow.cpp | 23 ++++-- src/nostalgia/studio/mainwindow.hpp | 4 ++ 9 files changed, 95 insertions(+), 30 deletions(-) diff --git a/src/nostalgia/core/studio/import_tilesheet_wizard.cpp b/src/nostalgia/core/studio/import_tilesheet_wizard.cpp index 83ba8e68..26ca2abd 100644 --- a/src/nostalgia/core/studio/import_tilesheet_wizard.cpp +++ b/src/nostalgia/core/studio/import_tilesheet_wizard.cpp @@ -17,8 +17,7 @@ const QString ImportTilesheetWizardPage::TILESHEET_NAME = "projectName"; const QString ImportTilesheetWizardPage::IMPORT_PATH = "projectPath"; const QString ImportTilesheetWizardPage::BPP = "bpp"; -ImportTilesheetWizardPage::ImportTilesheetWizardPage(studio::PluginArgs args) { - m_project = args.project; +ImportTilesheetWizardPage::ImportTilesheetWizardPage(studio::PluginArgs args): m_project(args.project) { addLineEdit(tr("&Tile Sheet Name:"), TILESHEET_NAME + "*", "", [this](QString) { auto importPath = field(IMPORT_PATH).toString(); if (QFile(importPath).exists()) { diff --git a/src/nostalgia/core/studio/import_tilesheet_wizard.hpp b/src/nostalgia/core/studio/import_tilesheet_wizard.hpp index 76891216..32cc4525 100644 --- a/src/nostalgia/core/studio/import_tilesheet_wizard.hpp +++ b/src/nostalgia/core/studio/import_tilesheet_wizard.hpp @@ -18,7 +18,7 @@ class ImportTilesheetWizardPage: public studio::WizardFormPage { static const QString TILESHEET_NAME; static const QString IMPORT_PATH; static const QString BPP; - studio::Project *m_project = nullptr; + studio::Project *&m_project; public: ImportTilesheetWizardPage(studio::PluginArgs args); diff --git a/src/nostalgia/studio/lib/oxfstreeview.cpp b/src/nostalgia/studio/lib/oxfstreeview.cpp index b2fe3f37..b29a96fb 100644 --- a/src/nostalgia/studio/lib/oxfstreeview.cpp +++ b/src/nostalgia/studio/lib/oxfstreeview.cpp @@ -16,40 +16,64 @@ namespace studio { using namespace ox; OxFSFile::OxFSFile(FileSystem *fs, QString path, OxFSFile *parentItem) { - m_fs = fs; m_path = path; m_parentItem = parentItem; + + // find children + if (fs) { + QVector> ls; + if (fs->stat((const char*) m_path.toUtf8()).fileType == FileType_Directory) { + fs->ls(m_path.toUtf8(), &ls); + qSort(ls); + } + for (auto v : ls) { + auto ch = new OxFSFile(fs, m_path + "/" + v.name, this); + m_childItems.push_back(ch); + } + } } OxFSFile::~OxFSFile() { qDeleteAll(m_childItems); } -void OxFSFile::appendChild(OxFSFile*) { +void OxFSFile::appendChild(OxFSModel *model, QStringList pathItems, QString currentPath) { + if (pathItems.size()) { + auto target = pathItems[0]; + currentPath += "/" + target; + OxFSFile *nextItem = nullptr; + int index = m_childItems.size(); + for (int i = 0; i < m_childItems.size(); i++) { + if (m_childItems[i]->name() >= target) { + index = i; + break; + } + } + + if (m_childItems.size() == index || m_childItems[index]->name() != target) { + auto idx = model->createIndex(row(), 0, this); + model->beginInsertRows(idx, index, index); + nextItem = new OxFSFile(nullptr, currentPath, this); + m_childItems.insert(index, nextItem); + model->endInsertRows(); + } + + nextItem = m_childItems[index]; + + nextItem->appendChild(model, pathItems.mid(1), currentPath); + } } OxFSFile *OxFSFile::child(int row) { - if (m_fs) { - QVector> ls; - m_fs->ls(m_path.toUtf8(), &ls); - auto ch = new OxFSFile(m_fs, m_path + "/" + ls[row].name, this); - m_childItems.push_back(ch); - return ch; + if (row < m_childItems.size()) { + return m_childItems[row]; } else { return nullptr; } } int OxFSFile::childCount() const { - if (m_fs) { - QVector> ls; - if (m_fs->stat((const char*) m_path.toUtf8()).fileType == FileType_Directory) { - m_fs->ls(m_path.toUtf8(), &ls); - } - return ls.size(); - } else { - return 0; - } + return m_childItems.size(); } int OxFSFile::columnCount() const { @@ -57,7 +81,7 @@ int OxFSFile::columnCount() const { } QVariant OxFSFile::data(int) const { - return m_path.mid(m_path.lastIndexOf('/') + 1); + return name(); } int OxFSFile::row() const { @@ -72,6 +96,10 @@ OxFSFile *OxFSFile::parentItem() { return m_parentItem; } +QString OxFSFile::name() const { + return m_path.mid(m_path.lastIndexOf("/") + 1); +} + // OxFSModel @@ -82,6 +110,7 @@ OxFSModel::OxFSModel(FileSystem *fs, QObject *parent) { OxFSModel::~OxFSModel() { if (m_rootItem) { delete m_rootItem; + m_rootItem = nullptr; } } @@ -167,6 +196,11 @@ int OxFSModel::columnCount(const QModelIndex &parent) const { } } +void OxFSModel::updateFile(QString path) { + auto pathItems = path.split("/").mid(1); + m_rootItem->appendChild(this, pathItems, ""); +} + void OxFSModel::setupModelData(const QStringList &lines, OxFSFile *parent) { } diff --git a/src/nostalgia/studio/lib/oxfstreeview.hpp b/src/nostalgia/studio/lib/oxfstreeview.hpp index de9f21c3..91ccd2eb 100644 --- a/src/nostalgia/studio/lib/oxfstreeview.hpp +++ b/src/nostalgia/studio/lib/oxfstreeview.hpp @@ -19,7 +19,6 @@ namespace studio { class OxFSFile { private: - ox::FileSystem *m_fs = nullptr; OxFSFile *m_parentItem = nullptr; QString m_path; QVector m_childItems; @@ -29,7 +28,7 @@ class OxFSFile { ~OxFSFile(); - void appendChild(OxFSFile *child); + void appendChild(class OxFSModel *model, QStringList pathItems, QString fullPath); OxFSFile *child(int row); @@ -42,11 +41,15 @@ class OxFSFile { int row() const; OxFSFile *parentItem(); + + QString name() const; }; class OxFSModel: public QAbstractItemModel { Q_OBJECT + friend OxFSFile; + private: OxFSFile *m_rootItem = nullptr; @@ -71,6 +74,9 @@ class OxFSModel: public QAbstractItemModel { int columnCount(const QModelIndex &parent = QModelIndex()) const override; + public slots: + void updateFile(QString path); + private: void setupModelData(const QStringList &lines, OxFSFile *parent); }; diff --git a/src/nostalgia/studio/lib/plugin.hpp b/src/nostalgia/studio/lib/plugin.hpp index fd0a2792..ba76d076 100644 --- a/src/nostalgia/studio/lib/plugin.hpp +++ b/src/nostalgia/studio/lib/plugin.hpp @@ -22,7 +22,7 @@ namespace nostalgia { namespace studio { struct PluginArgs { - Project *project = nullptr; + Project *&project; }; struct WizardMaker { diff --git a/src/nostalgia/studio/lib/project.cpp b/src/nostalgia/studio/lib/project.cpp index e6be36c4..eac65663 100644 --- a/src/nostalgia/studio/lib/project.cpp +++ b/src/nostalgia/studio/lib/project.cpp @@ -79,11 +79,15 @@ FileSystem *Project::romFs() { } int Project::mkdir(QString path) { - return m_fs->mkdir(path.toUtf8().data()); + auto err = m_fs->mkdir(path.toUtf8().data()); + emit updated(path); + return err; } int Project::write(QString path, uint8_t *buff, size_t buffLen) { - return m_fs->write(path.toUtf8().data(), buff, buffLen); + auto err = m_fs->write(path.toUtf8().data(), buff, buffLen); + emit updated(path); + return err; } } diff --git a/src/nostalgia/studio/lib/project.hpp b/src/nostalgia/studio/lib/project.hpp index 283f543b..6badbcf8 100644 --- a/src/nostalgia/studio/lib/project.hpp +++ b/src/nostalgia/studio/lib/project.hpp @@ -40,6 +40,9 @@ class Project: public QObject { int mkdir(QString path); int write(QString path, uint8_t *buff, size_t buffLen); + + signals: + void updated(QString path); }; } diff --git a/src/nostalgia/studio/mainwindow.cpp b/src/nostalgia/studio/mainwindow.cpp index aafc8d6e..638b7eaa 100644 --- a/src/nostalgia/studio/mainwindow.cpp +++ b/src/nostalgia/studio/mainwindow.cpp @@ -7,8 +7,8 @@ */ #include -#include #include +#include #include #include #include @@ -226,7 +226,9 @@ int MainWindow::openProject(QString projectPath) { m_project = nullptr; } m_project = project; - m_projectExplorer->setModel(new OxFSModel(m_project->romFs())); + m_oxfsView = new OxFSModel(m_project->romFs()); + m_projectExplorer->setModel(m_oxfsView); + connect(m_project, SIGNAL(updated(QString)), m_oxfsView, SLOT(updateFile(QString))); m_importAction->setEnabled(true); m_state.projectPath = projectPath; } @@ -236,8 +238,13 @@ 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))); + delete m_project; m_project = nullptr; + + delete m_oxfsView; + m_oxfsView = nullptr; } if (m_projectExplorer->model()) { delete m_projectExplorer->model(); @@ -322,8 +329,9 @@ void MainWindow::showImportWizard() { auto ws = new WizardSelect(); wizard.addPage(ws); - PluginArgs args; - args.project = m_project; + PluginArgs args { + .project = m_project + }; for (auto p : m_plugins) { for (auto w : p->importWizards(args)) { ws->addOption(w.name, w.make); @@ -334,5 +342,12 @@ void MainWindow::showImportWizard() { wizard.exec(); } +void MainWindow::refreshProjectExplorer(QString path) { + if (m_oxfsView) { + m_oxfsView->updateFile(path); + qInfo() << "asdf\n"; + } +} + } } diff --git a/src/nostalgia/studio/mainwindow.hpp b/src/nostalgia/studio/mainwindow.hpp index a373a631..72f29268 100644 --- a/src/nostalgia/studio/mainwindow.hpp +++ b/src/nostalgia/studio/mainwindow.hpp @@ -22,6 +22,7 @@ #include +#include "lib/oxfstreeview.hpp" #include "lib/plugin.hpp" #include "lib/project.hpp" @@ -88,6 +89,7 @@ class MainWindow: public QMainWindow { QVector> m_dockWidgets; QTreeView *m_projectExplorer = nullptr; QVector m_plugins; + QPointer m_oxfsView = nullptr; public: MainWindow(QString profilePath); @@ -127,6 +129,8 @@ class MainWindow: public QMainWindow { void showNewWizard(); void showImportWizard(); + + void refreshProjectExplorer(QString path); }; }