refactor: Enhance VTK puppet lifecycle management by explicitly handling puppet removal and synchronizing puppets across all viewports, and remove default scene objects from startup.

This commit is contained in:
AndreaRigoni
2026-03-22 13:20:44 +00:00
parent 324aaa91b7
commit d87f3a984e
3 changed files with 47 additions and 17 deletions

View File

@@ -86,7 +86,7 @@ void ContextPanel::setContext(uLib::ObjectsContext* context) {
delete m_vtkContext; delete m_vtkContext;
} }
m_vtkContext = new uLib::Vtk::vtkObjectsContext(context); m_vtkContext = new uLib::Vtk::vtkObjectsContext(context);
m_vtkView->AddPuppet(*m_vtkContext); // m_vtkView->AddPuppet(*m_vtkContext); // redundant: child puppets are added individually
// Render viewport and add child puppets when context is updated // Render viewport and add child puppets when context is updated
if (context) { if (context) {
@@ -98,6 +98,13 @@ void ContextPanel::setContext(uLib::ObjectsContext* context) {
} }
}); });
uLib::Object::connect(m_vtkContext, &uLib::Vtk::vtkObjectsContext::PuppetRemoved, [this](uLib::Vtk::Puppet* p) {
if (this->m_vtkView && p) {
this->m_vtkView->RemovePuppet(*p);
this->m_vtkView->Render();
}
});
// Add any puppets that were created during m_vtkContext's construction // Add any puppets that were created during m_vtkContext's construction
for (auto* obj : context->GetObjects()) { for (auto* obj : context->GetObjects()) {
if (auto* p = m_vtkContext->GetPuppet(obj)) { if (auto* p = m_vtkContext->GetPuppet(obj)) {

View File

@@ -110,19 +110,42 @@ void MainPanel::setContext(uLib::ObjectsContext* context) {
if (context) { if (context) {
if (auto* viewport = qobject_cast<uLib::Vtk::QViewport*>(m_firstPane->currentViewport())) { if (auto* viewport = qobject_cast<uLib::Vtk::QViewport*>(m_firstPane->currentViewport())) {
m_mainVtkContext = new uLib::Vtk::vtkObjectsContext(context); m_mainVtkContext = new uLib::Vtk::vtkObjectsContext(context);
viewport->AddPuppet(*m_mainVtkContext); // viewport->AddPuppet(*m_mainVtkContext); // redundant
uLib::Object::connect(m_mainVtkContext, &uLib::Vtk::vtkObjectsContext::PuppetAdded, [viewport](uLib::Vtk::Puppet* p) {
if (viewport && p) { uLib::Object::connect(m_mainVtkContext, &uLib::Vtk::vtkObjectsContext::PuppetAdded, [this](uLib::Vtk::Puppet* p) {
viewport->AddPuppet(*p); if (p) {
viewport->ZoomAuto(); auto panes = this->findChildren<ViewportPane*>();
viewport->Render(); for (auto* pane : panes) {
if (auto* vp = qobject_cast<uLib::Vtk::QViewport*>(pane->currentViewport())) {
vp->AddPuppet(*p);
vp->ZoomAuto();
vp->Render();
}
}
} }
}); });
// Add any puppets that were created during m_mainVtkContext's construction uLib::Object::connect(m_mainVtkContext, &uLib::Vtk::vtkObjectsContext::PuppetRemoved, [this](uLib::Vtk::Puppet* p) {
if (p) {
auto panes = this->findChildren<ViewportPane*>();
for (auto* pane : panes) {
if (auto* vp = qobject_cast<uLib::Vtk::QViewport*>(pane->currentViewport())) {
vp->RemovePuppet(*p);
vp->Render();
}
}
}
});
// Add any puppets that were created during m_mainVtkContext's construction to all panes
auto panes = this->findChildren<ViewportPane*>();
for (auto* obj : context->GetObjects()) { for (auto* obj : context->GetObjects()) {
if (auto* p = m_mainVtkContext->GetPuppet(obj)) { if (auto* p = m_mainVtkContext->GetPuppet(obj)) {
viewport->AddPuppet(*p); for (auto* pane : panes) {
if (auto* vp = qobject_cast<uLib::Vtk::QViewport*>(pane->currentViewport())) {
vp->AddPuppet(*p);
}
}
} }
} }

View File

@@ -32,17 +32,17 @@ int main(int argc, char** argv) {
StyleManager::applyStyle(&app, "dark"); StyleManager::applyStyle(&app, "dark");
std::cout << "Starting gcompose Qt application..." << std::endl; std::cout << "Starting gcompose Qt application..." << std::endl;
ContainerBox world_box(Vector3f(1, 1, 1)); // ContainerBox world_box(Vector3f(1, 1, 1));
world_box.Scale(Vector3f(2_mm, 2_mm, 2_mm)); // world_box.Scale(Vector3f(2_mm, 2_mm, 2_mm));
world_box.SetPosition(Vector3f(-1_mm, -1_mm, -1_mm)); // world_box.SetPosition(Vector3f(-1_mm, -1_mm, -1_mm));
Geant::Scene scene; // Geant::Scene scene;
scene.ConstructWorldBox(world_box.GetSize(), "G4_AIR"); // scene.ConstructWorldBox(world_box.GetSize(), "G4_AIR");
scene.Initialize(); // scene.Initialize();
uLib::ObjectsContext globalContext; uLib::ObjectsContext globalContext;
globalContext.AddObject(&world_box); // globalContext.AddObject(&world_box);
globalContext.AddObject(&scene); // globalContext.AddObject(&scene);
// 2. Initialize MainWindow (contains embedded VTK QViewport) // 2. Initialize MainWindow (contains embedded VTK QViewport)
MainWindow window; MainWindow window;