feat: Added explorer view to editor
This commit is contained in:
parent
6522a887f9
commit
1f6ba1752f
|
@ -25,6 +25,8 @@ set(PROJECT_SOURCES
|
||||||
mainwindow.ui
|
mainwindow.ui
|
||||||
mainglwidget.h
|
mainglwidget.h
|
||||||
mainglwidget.cpp
|
mainglwidget.cpp
|
||||||
|
explorermodel.h
|
||||||
|
explorermodel.cpp
|
||||||
${TS_FILES}
|
${TS_FILES}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
81
editor/explorermodel.cpp
Normal file
81
editor/explorermodel.cpp
Normal file
|
@ -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<Instance*>(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<Instance*>(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<Instance*>(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<const Instance*>(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);
|
||||||
|
}
|
34
editor/explorermodel.h
Normal file
34
editor/explorermodel.h
Normal file
|
@ -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 <QOpenGLWidget>
|
||||||
|
#include <QWidget>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
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
|
|
@ -8,10 +8,12 @@
|
||||||
#include <QTimerEvent>
|
#include <QTimerEvent>
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "physics/simulation.h"
|
#include "physics/simulation.h"
|
||||||
#include "objects/part.h"
|
#include "objects/part.h"
|
||||||
|
#include "explorermodel.h"
|
||||||
|
|
||||||
#include "wayland-pointer-constraints-unstable-v1-client-protocol.h"
|
#include "wayland-pointer-constraints-unstable-v1-client-protocol.h"
|
||||||
|
|
||||||
|
@ -23,6 +25,8 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
timer.start(33, this);
|
timer.start(33, this);
|
||||||
setMouseTracking(true);
|
setMouseTracking(true);
|
||||||
|
|
||||||
|
ui->explorerView->setModel(new ExplorerModel(std::dynamic_pointer_cast<Instance>(workspace)));
|
||||||
|
|
||||||
simulationInit();
|
simulationInit();
|
||||||
|
|
||||||
// Baseplate
|
// Baseplate
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>800</width>
|
<width>1027</width>
|
||||||
<height>600</height>
|
<height>600</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
|
@ -40,12 +40,27 @@
|
||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>800</width>
|
<width>1027</width>
|
||||||
<height>29</height>
|
<height>29</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="QStatusBar" name="statusbar"/>
|
<widget class="QStatusBar" name="statusbar"/>
|
||||||
|
<widget class="QDockWidget" name="explorerWidget">
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>Explorer</string>
|
||||||
|
</property>
|
||||||
|
<attribute name="dockWidgetArea">
|
||||||
|
<number>2</number>
|
||||||
|
</attribute>
|
||||||
|
<widget class="QWidget" name="dockWidgetContents">
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||||
|
<item>
|
||||||
|
<widget class="QTreeView" name="explorerView"/>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</widget>
|
||||||
</widget>
|
</widget>
|
||||||
<customwidgets>
|
<customwidgets>
|
||||||
<customwidget>
|
<customwidget>
|
||||||
|
|
Loading…
Reference in a new issue