add QCanvas Root and viewport pane in gcompose
This commit is contained in:
@@ -3,6 +3,10 @@ add_executable(gcompose
|
||||
src/main.cpp
|
||||
src/MainWindow.h
|
||||
src/MainWindow.cpp
|
||||
src/QViewportPane.h
|
||||
src/QViewportPane.cpp
|
||||
src/MainPanel.h
|
||||
src/MainPanel.cpp
|
||||
)
|
||||
|
||||
set_target_properties(gcompose PROPERTIES
|
||||
@@ -29,6 +33,7 @@ target_link_libraries(gcompose
|
||||
mutomMath
|
||||
mutomGeant
|
||||
mutomVtk
|
||||
mutomRoot
|
||||
${Geant4_LIBS_FILTERED}
|
||||
${VTK_LIBRARIES}
|
||||
Qt6::Widgets
|
||||
|
||||
49
app/gcompose/src/MainPanel.cpp
Normal file
49
app/gcompose/src/MainPanel.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "MainPanel.h"
|
||||
#include "QViewportPane.h"
|
||||
#include <QVBoxLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QSplitter>
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
|
||||
MainPanel::MainPanel(QWidget* parent) : QWidget(parent) {
|
||||
auto* mainLayout = new QVBoxLayout(this);
|
||||
mainLayout->setContentsMargins(0, 0, 0, 0);
|
||||
mainLayout->setSpacing(0);
|
||||
|
||||
// 1. Top Menu Panel
|
||||
auto* menuPanel = new QWidget(this);
|
||||
menuPanel->setFixedHeight(36);
|
||||
menuPanel->setStyleSheet("background-color: #2b2b2b; border-bottom: 1px solid #111;");
|
||||
|
||||
auto* menuLayout = new QHBoxLayout(menuPanel);
|
||||
menuLayout->setContentsMargins(10, 0, 10, 0);
|
||||
menuLayout->setSpacing(15);
|
||||
|
||||
auto* logo = new QLabel("G-COMPOSE", menuPanel);
|
||||
logo->setStyleSheet("font-weight: bold; color: #0078d7; font-size: 14px; letter-spacing: 1px;");
|
||||
|
||||
auto* btnOpen = new QPushButton("Open", menuPanel);
|
||||
auto* btnSave = new QPushButton("Save", menuPanel);
|
||||
|
||||
QString btnStyle = "QPushButton { background: transparent; color: #ccc; border: none; padding: 5px 10px; }"
|
||||
"QPushButton:hover { background: #3c3c3c; color: white; border-radius: 4px; }";
|
||||
btnOpen->setStyleSheet(btnStyle);
|
||||
btnSave->setStyleSheet(btnStyle);
|
||||
|
||||
menuLayout->addWidget(logo);
|
||||
menuLayout->addWidget(btnOpen);
|
||||
menuLayout->addWidget(btnSave);
|
||||
menuLayout->addStretch();
|
||||
|
||||
mainLayout->addWidget(menuPanel);
|
||||
|
||||
// 2. Central Splitter Area
|
||||
m_rootSplitter = new QSplitter(Qt::Horizontal, this);
|
||||
m_firstPane = new QViewportPane(m_rootSplitter);
|
||||
m_rootSplitter->addWidget(m_firstPane);
|
||||
|
||||
mainLayout->addWidget(m_rootSplitter, 1);
|
||||
}
|
||||
|
||||
MainPanel::~MainPanel() {}
|
||||
22
app/gcompose/src/MainPanel.h
Normal file
22
app/gcompose/src/MainPanel.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef MAINPANEL_H
|
||||
#define MAINPANEL_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class QSplitter;
|
||||
class QViewportPane;
|
||||
|
||||
class MainPanel : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit MainPanel(QWidget* parent = nullptr);
|
||||
virtual ~MainPanel();
|
||||
|
||||
QViewportPane* getFirstPane() const { return m_firstPane; }
|
||||
|
||||
private:
|
||||
QSplitter* m_rootSplitter;
|
||||
QViewportPane* m_firstPane;
|
||||
};
|
||||
|
||||
#endif // MAINPANEL_H
|
||||
@@ -1,11 +1,12 @@
|
||||
#include "MainWindow.h"
|
||||
#include <Vtk/vtkQViewport.h>
|
||||
#include <QSplitter>
|
||||
#include "MainPanel.h"
|
||||
|
||||
using namespace uLib;
|
||||
|
||||
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
|
||||
m_viewport = new Vtk::QViewport(this);
|
||||
setCentralWidget(m_viewport);
|
||||
m_panel = new MainPanel(this);
|
||||
setCentralWidget(m_panel);
|
||||
|
||||
setWindowTitle("gcompose - Qt VTK Interface");
|
||||
resize(1200, 800);
|
||||
|
||||
@@ -4,9 +4,10 @@
|
||||
#include <QMainWindow>
|
||||
#include <QVTKOpenGLNativeWidget.h>
|
||||
|
||||
class MainPanel;
|
||||
|
||||
namespace uLib {
|
||||
namespace Vtk {
|
||||
class QViewport;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +17,10 @@ public:
|
||||
MainWindow(QWidget* parent = nullptr);
|
||||
virtual ~MainWindow();
|
||||
|
||||
uLib::Vtk::QViewport* getViewport() { return m_viewport; }
|
||||
MainPanel* getPanel() { return m_panel; }
|
||||
|
||||
private:
|
||||
uLib::Vtk::QViewport* m_viewport;
|
||||
MainPanel* m_panel;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
169
app/gcompose/src/QViewportPane.cpp
Normal file
169
app/gcompose/src/QViewportPane.cpp
Normal file
@@ -0,0 +1,169 @@
|
||||
#include "QViewportPane.h"
|
||||
#include <Vtk/vtkQViewport.h>
|
||||
#include <Root/QCanvas.h>
|
||||
#include <QVBoxLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QToolButton>
|
||||
#include <QMenu>
|
||||
#include <QAction>
|
||||
#include <QSplitter>
|
||||
#include <vtkCamera.h>
|
||||
|
||||
QViewportPane::QViewportPane(QWidget* parent) : QWidget(parent), m_viewport(nullptr) {
|
||||
m_layout = new QVBoxLayout(this);
|
||||
m_layout->setContentsMargins(0, 0, 0, 0);
|
||||
m_layout->setSpacing(0);
|
||||
|
||||
// Title bar setup
|
||||
m_titleBar = new QWidget(this);
|
||||
m_titleBar->setFixedHeight(22);
|
||||
m_titleBar->setStyleSheet("background-color: #333; color: white;");
|
||||
|
||||
auto* titleLayout = new QHBoxLayout(m_titleBar);
|
||||
titleLayout->setContentsMargins(5, 0, 5, 0);
|
||||
|
||||
m_titleLabel = new QLabel("Viewport", m_titleBar);
|
||||
|
||||
auto* closeBtn = new QToolButton(m_titleBar);
|
||||
closeBtn->setText("X");
|
||||
closeBtn->setFixedSize(18, 18);
|
||||
closeBtn->setStyleSheet("QToolButton { border: none; font-weight: bold; background: transparent; color: #ccc; } "
|
||||
"QToolButton:hover { color: white; background: red; }");
|
||||
|
||||
titleLayout->addWidget(m_titleLabel);
|
||||
titleLayout->addStretch();
|
||||
titleLayout->addWidget(closeBtn);
|
||||
|
||||
m_layout->addWidget(m_titleBar);
|
||||
|
||||
m_titleBar->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
connect(m_titleBar, &QWidget::customContextMenuRequested, this, &QViewportPane::showContextMenu);
|
||||
connect(closeBtn, &QToolButton::clicked, this, &QViewportPane::onCloseRequested);
|
||||
|
||||
addVtkViewport(); // Initialize with a default VTK viewport
|
||||
}
|
||||
|
||||
QViewportPane::~QViewportPane() {}
|
||||
|
||||
void QViewportPane::setViewport(QWidget* viewport, const QString& title) {
|
||||
if (m_viewport) {
|
||||
m_layout->removeWidget(m_viewport);
|
||||
delete m_viewport;
|
||||
}
|
||||
m_viewport = viewport;
|
||||
m_titleLabel->setText(title);
|
||||
|
||||
m_viewport->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
m_layout->addWidget(m_viewport);
|
||||
}
|
||||
|
||||
void QViewportPane::addVtkViewport() {
|
||||
auto* viewport = new uLib::Vtk::QViewport(this);
|
||||
setViewport(viewport, "VTK Viewport");
|
||||
}
|
||||
|
||||
void QViewportPane::addRootCanvas() {
|
||||
auto* canvas = new uLib::Root::QCanvas(this);
|
||||
setViewport(canvas, "ROOT Canvas");
|
||||
}
|
||||
|
||||
void QViewportPane::onCloseRequested() {
|
||||
QSplitter* parentSplitter = qobject_cast<QSplitter*>(parentWidget());
|
||||
if (parentSplitter && parentSplitter->count() > 1) {
|
||||
deleteLater();
|
||||
} else {
|
||||
// Can't close the last viewport in the splitter safely. Re-initialize to default VTK canvas.
|
||||
addVtkViewport();
|
||||
}
|
||||
}
|
||||
|
||||
void QViewportPane::showContextMenu(const QPoint& pos) {
|
||||
QMenu menu(this);
|
||||
QAction* hSplit = menu.addAction("H split");
|
||||
QAction* vSplit = menu.addAction("V split");
|
||||
menu.addSeparator();
|
||||
|
||||
bool isVtk = (qobject_cast<uLib::Vtk::QViewport*>(m_viewport) != nullptr);
|
||||
|
||||
QAction* changeType = menu.addAction(isVtk ? "Change to ROOT Canvas" : "Change to VTK Viewport");
|
||||
|
||||
QAction* selected = menu.exec(m_titleBar->mapToGlobal(pos));
|
||||
|
||||
if (selected == hSplit) {
|
||||
AttemptSplit(Qt::Horizontal);
|
||||
} else if (selected == vSplit) {
|
||||
AttemptSplit(Qt::Vertical);
|
||||
} else if (selected == changeType) {
|
||||
if (isVtk) {
|
||||
addRootCanvas();
|
||||
} else {
|
||||
addVtkViewport();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void QViewportPane::AttemptSplit(Qt::Orientation orientation) {
|
||||
QWidget* p = parentWidget();
|
||||
if (!p) return;
|
||||
|
||||
QSplitter* parentSplitter = qobject_cast<QSplitter*>(p);
|
||||
if (!parentSplitter) return;
|
||||
|
||||
QViewportPane* newPane = new QViewportPane();
|
||||
|
||||
// 1. Synchronize viewport content and camera (VTK Viewport only for now)
|
||||
auto* currentVtk = qobject_cast<uLib::Vtk::QViewport*>(m_viewport);
|
||||
if (currentVtk) {
|
||||
auto* newVtk = qobject_cast<uLib::Vtk::QViewport*>(newPane->currentViewport());
|
||||
if (newVtk) {
|
||||
// Copy puppets
|
||||
for (auto* puppet : currentVtk->getPuppets()) {
|
||||
newVtk->AddPuppet(*puppet);
|
||||
}
|
||||
// Copy camera
|
||||
if (currentVtk->GetRenderer() && newVtk->GetRenderer()) {
|
||||
vtkCamera* currentCam = currentVtk->GetRenderer()->GetActiveCamera();
|
||||
vtkCamera* newCam = newVtk->GetRenderer()->GetActiveCamera();
|
||||
if (currentCam && newCam) {
|
||||
newCam->DeepCopy(currentCam);
|
||||
}
|
||||
}
|
||||
// Sync grid visible and axis
|
||||
newVtk->SetGridVisible(currentVtk->GetGridVisible());
|
||||
newVtk->SetGridAxis(currentVtk->GetGridAxis());
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Adjust for ROOT Canvas if that was the active view
|
||||
bool isRoot = (qobject_cast<uLib::Root::QCanvas*>(m_viewport) != nullptr);
|
||||
if (isRoot) {
|
||||
newPane->addRootCanvas();
|
||||
}
|
||||
|
||||
if (parentSplitter->orientation() == orientation) {
|
||||
int index = parentSplitter->indexOf(this);
|
||||
QList<int> sizes = parentSplitter->sizes();
|
||||
int currentSize = sizes.value(index, 0);
|
||||
int half = currentSize / 2;
|
||||
sizes[index] = half;
|
||||
sizes.insert(index + 1, currentSize - half);
|
||||
|
||||
parentSplitter->insertWidget(index + 1, newPane);
|
||||
parentSplitter->setSizes(sizes);
|
||||
} else {
|
||||
int index = parentSplitter->indexOf(this);
|
||||
QList<int> parentSizes = parentSplitter->sizes();
|
||||
|
||||
QSplitter* newSplitter = new QSplitter(orientation);
|
||||
newSplitter->addWidget(this);
|
||||
newSplitter->addWidget(newPane);
|
||||
|
||||
QList<int> subSizes;
|
||||
subSizes << 500 << 500;
|
||||
newSplitter->setSizes(subSizes);
|
||||
|
||||
parentSplitter->insertWidget(index, newSplitter);
|
||||
parentSplitter->setSizes(parentSizes);
|
||||
}
|
||||
}
|
||||
34
app/gcompose/src/QViewportPane.h
Normal file
34
app/gcompose/src/QViewportPane.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef QVIEWPORTPANE_H
|
||||
#define QVIEWPORTPANE_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class QVBoxLayout;
|
||||
class QLabel;
|
||||
|
||||
class QViewportPane : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit QViewportPane(QWidget* parent = nullptr);
|
||||
virtual ~QViewportPane();
|
||||
|
||||
void addVtkViewport();
|
||||
void addRootCanvas();
|
||||
|
||||
QWidget* currentViewport() const { return m_viewport; }
|
||||
|
||||
private slots:
|
||||
void onCloseRequested();
|
||||
void showContextMenu(const QPoint& pos);
|
||||
|
||||
private:
|
||||
void AttemptSplit(Qt::Orientation orientation);
|
||||
void setViewport(QWidget* viewport, const QString& title);
|
||||
|
||||
QVBoxLayout* m_layout;
|
||||
QWidget* m_titleBar;
|
||||
QLabel* m_titleLabel;
|
||||
QWidget* m_viewport;
|
||||
};
|
||||
|
||||
#endif // QVIEWPORTPANE_H
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <QApplication>
|
||||
#include "MainWindow.h"
|
||||
#include "MainPanel.h"
|
||||
#include "QViewportPane.h"
|
||||
|
||||
#include "Math/ContainerBox.h"
|
||||
#include <HEP/Geant/Scene.h>
|
||||
@@ -27,19 +29,7 @@ int main(int argc, char** argv) {
|
||||
std::cout << "Starting gcompose Qt application..." << std::endl;
|
||||
|
||||
ContainerBox world_box(Vector3f(1, 1, 1));
|
||||
world_box.Scale(Vector3f(20_mm, 20_mm, 20_mm));
|
||||
|
||||
DetectorChamber d1, d2;
|
||||
d1.SetSize(Vector3f(1, 1, 1));
|
||||
d1.SetPosition(Vector3f(0, 0, 0));
|
||||
d1.Scale(Vector3f(5, 10, 2));
|
||||
d1.Translate(Vector3f(0, 0, 0));
|
||||
|
||||
d2.SetSize(Vector3f(1, 1, 1));
|
||||
d2.SetPosition(Vector3f(0, 0, 0));
|
||||
d2.Scale(Vector3f(5, 10, 2));
|
||||
d2.Translate(Vector3f(0, 0, 10));
|
||||
|
||||
world_box.Scale(Vector3f(20_mm, 20_mm, 20_mm));
|
||||
|
||||
Geant::Scene scene;
|
||||
scene.ConstructWorldBox(world_box.GetSize(), "G4_AIR");
|
||||
@@ -47,17 +37,12 @@ int main(int argc, char** argv) {
|
||||
|
||||
// 2. Initialize MainWindow (contains embedded VTK QViewport)
|
||||
MainWindow window;
|
||||
Vtk::QViewport* viewport = window.getViewport();
|
||||
|
||||
Vtk::vtkDetectorChamber vtk_d1(&d1);
|
||||
viewport->AddPuppet(vtk_d1);
|
||||
|
||||
Vtk::vtkDetectorChamber vtk_d2(&d2);
|
||||
viewport->AddPuppet(vtk_d2);
|
||||
MainPanel* panel = window.getPanel();
|
||||
QViewportPane* pane = panel->getFirstPane();
|
||||
Vtk::QViewport* viewport = qobject_cast<Vtk::QViewport*>(pane->currentViewport());
|
||||
|
||||
Vtk::vtkContainerBox vtk_box(&world_box);
|
||||
viewport->AddPuppet(vtk_box);
|
||||
|
||||
viewport->ZoomAuto();
|
||||
|
||||
std::cout << "Geant4 and VTK scenes are ready." << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user