attach vtk context to gcompose
This commit is contained in:
@@ -4,8 +4,16 @@
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QTreeView>
|
||||
#include <QSplitter>
|
||||
#include <QList>
|
||||
#include <QShortcut>
|
||||
#include <QItemSelectionModel>
|
||||
#include <Vtk/vtkQViewport.h>
|
||||
#include <Vtk/vtkObjectsContext.h>
|
||||
|
||||
ContextPanel::ContextPanel(QWidget* parent) : QWidget(parent) {
|
||||
ContextPanel::ContextPanel(QWidget* parent)
|
||||
: QWidget(parent)
|
||||
, m_vtkContext(nullptr) {
|
||||
m_layout = new QVBoxLayout(this);
|
||||
m_layout->setContentsMargins(0, 0, 0, 0);
|
||||
m_layout->setSpacing(0);
|
||||
@@ -32,12 +40,92 @@ ContextPanel::ContextPanel(QWidget* parent) : QWidget(parent) {
|
||||
m_model = new ContextModel(this);
|
||||
m_treeView->setModel(m_model);
|
||||
|
||||
m_layout->addWidget(m_treeView);
|
||||
m_splitter = new QSplitter(Qt::Vertical, this);
|
||||
m_splitter->addWidget(m_treeView);
|
||||
|
||||
m_vtkView = new uLib::Vtk::QViewport(m_splitter);
|
||||
m_splitter->addWidget(m_vtkView);
|
||||
|
||||
QList<int> sizes;
|
||||
sizes << 400 << 200;
|
||||
m_splitter->setSizes(sizes);
|
||||
|
||||
m_layout->addWidget(m_splitter);
|
||||
|
||||
connect(m_treeView->selectionModel(), &QItemSelectionModel::selectionChanged,
|
||||
this, &ContextPanel::onSelectionChanged);
|
||||
|
||||
auto* deleteShortcut = new QShortcut(QKeySequence::Delete, this);
|
||||
connect(deleteShortcut, &QShortcut::activated, [this]() {
|
||||
auto selectedIndexes = m_treeView->selectionModel()->selectedIndexes();
|
||||
if (selectedIndexes.isEmpty() || !m_context) return;
|
||||
|
||||
// Collect objects to remove to avoid iterator invalidation issues if context signal emits during removal
|
||||
std::vector<uLib::Object*> toRemove;
|
||||
for (const auto& idx : selectedIndexes) {
|
||||
if (idx.column() == 0) {
|
||||
toRemove.push_back(static_cast<uLib::Object*>(idx.internalPointer()));
|
||||
}
|
||||
}
|
||||
|
||||
for (auto* obj : toRemove) {
|
||||
m_context->RemoveObject(obj);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
ContextPanel::~ContextPanel() {}
|
||||
|
||||
void ContextPanel::setContext(uLib::ObjectsContext* context) {
|
||||
m_context = context;
|
||||
m_model->setContext(context);
|
||||
m_treeView->expandAll();
|
||||
|
||||
if (m_vtkContext) {
|
||||
m_vtkView->RemovePuppet(*m_vtkContext);
|
||||
delete m_vtkContext;
|
||||
}
|
||||
m_vtkContext = new uLib::Vtk::vtkObjectsContext(context);
|
||||
m_vtkView->AddPuppet(*m_vtkContext);
|
||||
|
||||
// Render viewport and add child puppets when context is updated
|
||||
if (context) {
|
||||
uLib::Object::connect(m_vtkContext, &uLib::Vtk::vtkObjectsContext::PuppetAdded, [this](uLib::Vtk::Puppet* p) {
|
||||
if (this->m_vtkView && p) {
|
||||
this->m_vtkView->AddPuppet(*p);
|
||||
this->m_vtkView->ZoomAuto();
|
||||
this->m_vtkView->Render();
|
||||
}
|
||||
});
|
||||
|
||||
// Add any puppets that were created during m_vtkContext's construction
|
||||
for (auto* obj : context->GetObjects()) {
|
||||
if (auto* p = m_vtkContext->GetPuppet(obj)) {
|
||||
this->m_vtkView->AddPuppet(*p);
|
||||
}
|
||||
}
|
||||
|
||||
uLib::Object::connect(context, &uLib::Object::Updated, [this]() {
|
||||
if (this->m_vtkView) {
|
||||
this->m_vtkView->ZoomAuto();
|
||||
this->m_vtkView->Render();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
m_vtkView->Render();
|
||||
}
|
||||
|
||||
void ContextPanel::onSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected) {
|
||||
uLib::Object* target = nullptr;
|
||||
if (!selected.indexes().isEmpty()) {
|
||||
target = static_cast<uLib::Object*>(selected.indexes().first().internalPointer());
|
||||
}
|
||||
|
||||
emit objectSelected(target);
|
||||
|
||||
if (m_vtkContext) {
|
||||
auto* puppet = m_vtkContext->GetPuppet(target);
|
||||
m_vtkView->SelectPuppet(puppet);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user