diff --git a/src/studio/CMakeLists.txt b/src/studio/CMakeLists.txt index dfa25328..659da2a9 100644 --- a/src/studio/CMakeLists.txt +++ b/src/studio/CMakeLists.txt @@ -20,7 +20,7 @@ target_link_libraries( OxStd NostalgiaCommon NostalgiaCore - NostalgiaStudioStatic + NostalgiaStudio NostalgiaStudioJson ) diff --git a/src/studio/lib/CMakeLists.txt b/src/studio/lib/CMakeLists.txt index 2de10374..5d7ccf79 100644 --- a/src/studio/lib/CMakeLists.txt +++ b/src/studio/lib/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) add_library( - NostalgiaStudioStatic + NostalgiaStudio newwizard.cpp oxfstreeview.cpp project.cpp @@ -14,27 +14,11 @@ add_library( set_property( TARGET - NostalgiaStudioStatic + NostalgiaStudio PROPERTY POSITION_INDEPENDENT_CODE ON ) -target_link_libraries( - NostalgiaStudioStatic - Qt5::Core - Qt5::Widgets - OxFS - OxStd -) - -add_library( - NostalgiaStudio - SHARED - newwizard.cpp - oxfstreeview.cpp - project.cpp -) - target_link_libraries( NostalgiaStudio Qt5::Core diff --git a/src/studio/lib/oxfstreeview.cpp b/src/studio/lib/oxfstreeview.cpp index 157abd7c..0e433af3 100644 --- a/src/studio/lib/oxfstreeview.cpp +++ b/src/studio/lib/oxfstreeview.cpp @@ -6,12 +6,168 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include + #include "oxfstreeview.hpp" namespace nostalgia { namespace studio { +using namespace ox; +OxFSFile::OxFSFile(FileSystem *fs, QString path, OxFSFile *parentItem) { + m_fs = fs; + m_path = path; + m_parentItem = parentItem; +} + +OxFSFile::~OxFSFile() { + qDeleteAll(m_childItems); +} + +void OxFSFile::appendChild(OxFSFile*) { +} + +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; + } else { + return nullptr; + } +} + +int OxFSFile::childCount() const { + if (m_fs) { + QVector> ls; + m_fs->ls(m_path.toUtf8(), &ls); + return ls.size(); + } else { + return 0; + } +} + +int OxFSFile::columnCount() const { + return 1; +} + +QVariant OxFSFile::data(int) const { + return m_path.mid(m_path.lastIndexOf('/') + 1); +} + +int OxFSFile::row() const { + if (m_parentItem) { + return m_parentItem->m_childItems.indexOf(const_cast(this)); + } else { + return 0; + } +} + +OxFSFile *OxFSFile::parentItem() { + return m_parentItem; +} + + +// OxFSModel + +OxFSModel::OxFSModel(FileSystem *fs, QObject *parent) { + m_rootItem = new OxFSFile(fs, ""); +} + +OxFSModel::~OxFSModel() { + if (m_rootItem) { + delete m_rootItem; + } +} + +QVariant OxFSModel::data(const QModelIndex &index, int role) const { + if (!index.isValid() || role != Qt::DisplayRole) { + return QVariant(); + } else { + OxFSFile *item = static_cast(index.internalPointer()); + return item->data(index.column()); + } +} + +Qt::ItemFlags OxFSModel::flags(const QModelIndex &index) const { + if (!index.isValid()) { + return 0; + } else { + return QAbstractItemModel::flags(index); + } +} + +QVariant OxFSModel::headerData(int section, Qt::Orientation orientation, int role) const { + return QVariant(); + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { + return m_rootItem->data(section); + } else { + return QVariant(); + } +} + +QModelIndex OxFSModel::index(int row, int column, const QModelIndex &parent) const { + if (!hasIndex(row, column, parent)) { + return QModelIndex(); + } + + OxFSFile *parentItem; + if (!parent.isValid()) { + parentItem = m_rootItem; + } else { + parentItem = static_cast(parent.internalPointer()); + } + + OxFSFile *childItem = parentItem->child(row); + if (childItem) { + return createIndex(row, column, childItem); + } else { + return QModelIndex(); + } +} + +QModelIndex OxFSModel::parent(const QModelIndex &index) const { + if (!index.isValid()) { + return QModelIndex(); + } + + OxFSFile *childItem = static_cast(index.internalPointer()); + OxFSFile *parentItem = childItem->parentItem(); + if (parentItem == m_rootItem) { + return QModelIndex(); + } + + return createIndex(parentItem->row(), 0, parentItem); +} + +int OxFSModel::rowCount(const QModelIndex &parent) const { + if (parent.column() > 0) { + return 0; + } + + OxFSFile *parentItem; + if (!parent.isValid()) { + parentItem = m_rootItem; + } else { + parentItem = static_cast(parent.internalPointer()); + } + + return parentItem->childCount(); +} + +int OxFSModel::columnCount(const QModelIndex &parent) const { + if (parent.isValid()) { + return static_cast(parent.internalPointer())->columnCount(); + } else { + return m_rootItem->columnCount(); + } +} + +void OxFSModel::setupModelData(const QStringList &lines, OxFSFile *parent) { +} } } diff --git a/src/studio/lib/oxfstreeview.hpp b/src/studio/lib/oxfstreeview.hpp index 4254b009..de9f21c3 100644 --- a/src/studio/lib/oxfstreeview.hpp +++ b/src/studio/lib/oxfstreeview.hpp @@ -8,31 +8,71 @@ #pragma once -#include -#include +#include +#include #include +#include + namespace nostalgia { namespace studio { class OxFSFile { private: - QList m_childItems; - QList m_itemData; - OxFSFile *m_parentItem; + ox::FileSystem *m_fs = nullptr; + OxFSFile *m_parentItem = nullptr; + QString m_path; + QVector m_childItems; public: - explicit OxFSFile(const QList &data, OxFSFile *parentItem = 0); - ~OxFSFile(); + OxFSFile(ox::FileSystem *fs, QString path, OxFSFile *parentItem = nullptr); - void appendChild(OxFSFile *child); + ~OxFSFile(); - OxFSFile *child(int row); - int childCount() const; - int columnCount() const; - QVariant data(int column) const; - int row() const; - OxFSFile *parentItem(); + void appendChild(OxFSFile *child); + + OxFSFile *child(int row); + + int childCount() const; + + int columnCount() const; + + QVariant data(int column) const; + + int row() const; + + OxFSFile *parentItem(); +}; + +class OxFSModel: public QAbstractItemModel { + Q_OBJECT + + private: + OxFSFile *m_rootItem = nullptr; + + public: + explicit OxFSModel(ox::FileSystem *fs, QObject *parent = 0); + + ~OxFSModel(); + + QVariant data(const QModelIndex &index, int role) const override; + + Qt::ItemFlags flags(const QModelIndex &index) const override; + + QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const override; + + QModelIndex index(int row, int column, + const QModelIndex &parent = QModelIndex()) const override; + + QModelIndex parent(const QModelIndex &index) const override; + + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + + private: + void setupModelData(const QStringList &lines, OxFSFile *parent); }; } diff --git a/src/studio/lib/project.cpp b/src/studio/lib/project.cpp index fc81eaa5..c8222ee3 100644 --- a/src/studio/lib/project.cpp +++ b/src/studio/lib/project.cpp @@ -22,6 +22,9 @@ Project::Project(QString path) { } Project::~Project() { + if (m_fs) { + delete m_fs; + } } void Project::create() { @@ -29,7 +32,7 @@ void Project::create() { m_romBuff = QSharedPointer(new QByteArray(1024, 0)); FileSystem32::format(m_romBuff->data(), m_romBuff->size(), true); - m_fs = QSharedPointer(createFileSystem(m_romBuff->data(), m_romBuff->size())); + m_fs = createFileSystem(m_romBuff->data(), m_romBuff->size()); m_fs->mkdir("/Tilesets"); @@ -45,7 +48,9 @@ int Project::open() { if (file.exists()) { file.open(QIODevice::ReadOnly); if (file.read(m_romBuff->data(), file.size()) > 0) { - m_fs = QSharedPointer(createFileSystem(m_romBuff->data(), m_romBuff->size())); + m_fs = createFileSystem(m_romBuff->data(), m_romBuff->size()); + + m_fs->mkdir("/Tilesets"); return 0; } else { return 1; @@ -62,5 +67,9 @@ void Project::save() { file.close(); } +FileSystem *Project::romFS() { + return m_fs; +} + } } diff --git a/src/studio/lib/project.hpp b/src/studio/lib/project.hpp index 3d616e0a..93368577 100644 --- a/src/studio/lib/project.hpp +++ b/src/studio/lib/project.hpp @@ -23,7 +23,7 @@ class Project: public QObject { QString m_path = ""; QSharedPointer m_romBuff; - QSharedPointer m_fs; + ox::FileSystem *m_fs = nullptr; public: Project(QString path); @@ -35,6 +35,8 @@ class Project: public QObject { int open(); void save(); + + ox::FileSystem *romFS(); }; } diff --git a/src/studio/mainwindow.cpp b/src/studio/mainwindow.cpp index 09569a8b..37d1a1a2 100644 --- a/src/studio/mainwindow.cpp +++ b/src/studio/mainwindow.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -18,6 +19,7 @@ #include #include "lib/newwizard.hpp" +#include "lib/oxfstreeview.hpp" #include "lib/project.hpp" #include "mainwindow.hpp" @@ -43,6 +45,9 @@ MainWindow::MainWindow(NostalgiaStudioProfile config, QWidget *parent) { } MainWindow::~MainWindow() { + if (m_projectExplorer->model()) { + delete m_projectExplorer->model(); + } for (auto f : m_cleanupTasks) { f(); } @@ -91,6 +96,9 @@ void MainWindow::setupProjectExplorer() { resizeDocks({dock}, {(int) (width() * 0.25)}, Qt::Horizontal); // setup tree view + m_projectExplorer = new QTreeView(dock); + m_projectExplorer->header()->hide(); + dock->setWidget(m_projectExplorer); } void MainWindow::addDockWidget(Qt::DockWidgetArea area, QDockWidget *dockWidget) { @@ -127,6 +135,7 @@ void MainWindow::openProject() { auto err = project->open(); if (err == 0) { m_project = project; + m_projectExplorer->setModel(new OxFSModel(m_project->romFS())); } } diff --git a/src/studio/mainwindow.hpp b/src/studio/mainwindow.hpp index 19616ee0..2c5ff03a 100644 --- a/src/studio/mainwindow.hpp +++ b/src/studio/mainwindow.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -60,6 +61,7 @@ class MainWindow: public QMainWindow { QPointer m_viewMenu; QVector> m_cleanupTasks; QVector> m_dockWidgets; + QTreeView *m_projectExplorer = nullptr; public: MainWindow(NostalgiaStudioProfile config, QWidget *parent = 0);