From 1f6ba1752f5a5e2b39e724ce40d042a4e40f26bb Mon Sep 17 00:00:00 2001 From: maelstrom Date: Sat, 18 Jan 2025 00:13:42 +0100 Subject: [PATCH] feat: Added explorer view to editor --- editor/CMakeLists.txt | 2 + editor/explorermodel.cpp | 81 ++++++++++++++++++++++++++++++++++++++++ editor/explorermodel.h | 34 +++++++++++++++++ editor/mainwindow.cpp | 4 ++ editor/mainwindow.ui | 19 +++++++++- 5 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 editor/explorermodel.cpp create mode 100644 editor/explorermodel.h diff --git a/editor/CMakeLists.txt b/editor/CMakeLists.txt index 9818c6b..a9ee540 100644 --- a/editor/CMakeLists.txt +++ b/editor/CMakeLists.txt @@ -25,6 +25,8 @@ set(PROJECT_SOURCES mainwindow.ui mainglwidget.h mainglwidget.cpp + explorermodel.h + explorermodel.cpp ${TS_FILES} ) diff --git a/editor/explorermodel.cpp b/editor/explorermodel.cpp new file mode 100644 index 0000000..763017b --- /dev/null +++ b/editor/explorermodel.cpp @@ -0,0 +1,81 @@ +#include "explorermodel.h" +#include "objects/base/instance.h" +#include "qcontainerfwd.h" +#include "qobject.h" +#include "qwidget.h" + +// https://doc.qt.io/qt-6/qtwidgets-itemviews-simpletreemodel-example.html#testing-the-model + +ExplorerModel::ExplorerModel(InstanceRef dataRoot, QWidget *parent) + : QAbstractItemModel(parent) + , rootItem(dataRoot) { +} + +ExplorerModel::~ExplorerModel() = default; + +QModelIndex ExplorerModel::index(int row, int column, const QModelIndex &parent) const { + if (!hasIndex(row, column, parent)) + return {}; + + Instance* parentItem = parent.isValid() + ? static_cast(parent.internalPointer()) + : rootItem.get(); + + if (parentItem->GetChildren().size() >= row) + return createIndex(row, column, parentItem->GetChildren()[row].get()); + return {}; +} + +QModelIndex ExplorerModel::parent(const QModelIndex &index) const { + if (!index.isValid()) + return {}; + + Instance* childItem = static_cast(index.internalPointer()); + // NORISK: The parent must exist if the child was obtained from it during this frame + InstanceRef parentItem = childItem->GetParent().value(); + + if (parentItem == rootItem) + return {}; + + // Check above ensures this item is not root, so value() must be valid + InstanceRef parentParent = parentItem->GetParent().value(); + for (int i = 0; i < parentParent->GetChildren().size(); i++) + if (parentParent->GetChildren()[i] == parentItem) + return createIndex(i, 0, parentItem.get()); + return QModelIndex{}; +} + +int ExplorerModel::rowCount(const QModelIndex &parent) const { + if (parent.column() > 0) + return 0; + + Instance* parentItem = parent.isValid() + ? static_cast(parent.internalPointer()) + : rootItem.get(); + + return parentItem->GetChildren().size(); +} + +int ExplorerModel::columnCount(const QModelIndex &parent) const { + return 1; +} + +QVariant ExplorerModel::data(const QModelIndex &index, int role) const { + if (!index.isValid() || role != Qt::DisplayRole) + return {}; + + const Instance *item = static_cast(index.internalPointer()); + return QString::fromStdString(item->name); +} + +QVariant ExplorerModel::headerData(int section, Qt::Orientation orientation, + int role) const +{ + return QString("Idk lol \u00AF\u005C\u005F\u0028\u30C4\u0029\u005F\u002F\u00AF"); +} + +Qt::ItemFlags ExplorerModel::flags(const QModelIndex &index) const +{ + return index.isValid() + ? QAbstractItemModel::flags(index) : Qt::ItemFlags(Qt::NoItemFlags); +} \ No newline at end of file diff --git a/editor/explorermodel.h b/editor/explorermodel.h new file mode 100644 index 0000000..c7a43be --- /dev/null +++ b/editor/explorermodel.h @@ -0,0 +1,34 @@ +#ifndef EXPLORERMODEL_H +#define EXPLORERMODEL_H + +#include "objects/base/instance.h" +#include "objects/part.h" +#include "qabstractitemmodel.h" +#include "qevent.h" +#include +#include +#include + +class ExplorerModel : public QAbstractItemModel { + Q_OBJECT +public: + Q_DISABLE_COPY_MOVE(ExplorerModel) + + explicit ExplorerModel(InstanceRef dataRoot, QWidget *parent = nullptr); + ~ExplorerModel() override; + + 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 = {}) const override; + QModelIndex parent(const QModelIndex &index) const override; + int rowCount(const QModelIndex &parent = {}) const override; + int columnCount(const QModelIndex &parent = {}) const override; + +private: + InstanceRef rootItem; +}; + +#endif // EXPLORERMODEL_H diff --git a/editor/mainwindow.cpp b/editor/mainwindow.cpp index 2888898..d93b79e 100644 --- a/editor/mainwindow.cpp +++ b/editor/mainwindow.cpp @@ -8,10 +8,12 @@ #include #include #include +#include #include "common.h" #include "physics/simulation.h" #include "objects/part.h" +#include "explorermodel.h" #include "wayland-pointer-constraints-unstable-v1-client-protocol.h" @@ -23,6 +25,8 @@ MainWindow::MainWindow(QWidget *parent) timer.start(33, this); setMouseTracking(true); + ui->explorerView->setModel(new ExplorerModel(std::dynamic_pointer_cast(workspace))); + simulationInit(); // Baseplate diff --git a/editor/mainwindow.ui b/editor/mainwindow.ui index e44225f..1ec3c87 100644 --- a/editor/mainwindow.ui +++ b/editor/mainwindow.ui @@ -6,7 +6,7 @@ 0 0 - 800 + 1027 600 @@ -40,12 +40,27 @@ 0 0 - 800 + 1027 29 + + + Explorer + + + 2 + + + + + + + + +