From b52ae808b85870094a9ce475e5304be85797590d Mon Sep 17 00:00:00 2001 From: AndreaRigoni Date: Sun, 22 Mar 2026 13:55:06 +0000 Subject: [PATCH] qcanvas passage --- src/Root/QCanvas.cpp | 58 +++++++++++++++++++++++++++++- src/Root/QCanvas.h | 8 +++-- src/Root/testing/root_api_test.cpp | 33 +++++++++++++++++ 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/Root/testing/root_api_test.cpp diff --git a/src/Root/QCanvas.cpp b/src/Root/QCanvas.cpp index a18100d..a1d58b3 100644 --- a/src/Root/QCanvas.cpp +++ b/src/Root/QCanvas.cpp @@ -17,6 +17,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include namespace uLib { namespace Root { @@ -116,9 +122,14 @@ void QCanvas::mousePressEvent(QMouseEvent* event) { case Qt::LeftButton: m_canvas->HandleInput(kButton1Down, pos.x(), pos.y()); break; - case Qt::RightButton: + case Qt::RightButton: { m_canvas->HandleInput(kButton3Down, pos.x(), pos.y()); + TObject *obj = m_canvas->GetSelected(); + if (obj) { + ShowContextMenu(obj, event->globalPosition().toPoint()); + } break; + } case Qt::MiddleButton: m_canvas->HandleInput(kButton2Down, pos.x(), pos.y()); break; @@ -128,6 +139,51 @@ void QCanvas::mousePressEvent(QMouseEvent* event) { } } +void QCanvas::ShowContextMenu(TObject* obj, const QPoint& globalPos) { + if (!obj) return; + + QMenu menu(this); + menu.setTitle(obj->GetName()); + + TClass* cl = obj->IsA(); + if (!cl) return; + + TList* methods = cl->GetListOfMethods(); + if (!methods) return; + + TIter next(methods); + TObject* m_obj; + while ((m_obj = next())) { + TMethod* method = (TMethod*)m_obj; + if (method->IsMenuItem()) { + QString label = QString::fromStdString(method->GetName()); + if (strlen(method->GetTitle()) > 0) { + label += " (" + QString::fromStdString(method->GetTitle()) + ")"; + } + + QAction* action = menu.addAction(label); + connect(action, &QAction::triggered, [this, obj, method]() { + // For now, only support methods without arguments for simplicity + // or methods that can be called via TInterpreter + if (method->GetNargs() == 0) { + obj->Execute((char*)method->GetName(), (char*)""); + if (m_canvas) { + m_canvas->Modified(); + m_canvas->Update(); + } + } else { + // TODO: Handle methods with arguments if needed + std::cout << "DEBUG: Method " << method->GetName() << " requires arguments." << std::endl; + } + }); + } + } + + if (!menu.actions().isEmpty()) { + menu.exec(globalPos); + } +} + void QCanvas::mouseReleaseEvent(QMouseEvent* event) { if (m_canvas) { auto pos = event->position(); diff --git a/src/Root/QCanvas.h b/src/Root/QCanvas.h index 643ef13..bf85c31 100644 --- a/src/Root/QCanvas.h +++ b/src/Root/QCanvas.h @@ -3,10 +3,11 @@ #ifdef HAVE_QT #include -class TCanvas; -class QPaintEvent; -class QResizeEvent; class QMouseEvent; +class QPoint; + +class TObject; +class TCanvas; namespace uLib { namespace Root { @@ -21,6 +22,7 @@ public: void SetCanvas(TCanvas* c); protected: + void ShowContextMenu(TObject* obj, const QPoint& globalPos); void paintEvent(QPaintEvent* event) override; void resizeEvent(QResizeEvent* event) override; void mouseMoveEvent(QMouseEvent* event) override; diff --git a/src/Root/testing/root_api_test.cpp b/src/Root/testing/root_api_test.cpp new file mode 100644 index 0000000..8013b86 --- /dev/null +++ b/src/Root/testing/root_api_test.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include +#include + +int main() { + TH1F *h = new TH1F("h", "h", 10, 0, 10); + TClass *cl = h->IsA(); + if (!cl) { + std::cout << "No class found" << std::endl; + return 1; + } + TList *methods = cl->GetListOfMethods(); + if (!methods) { + std::cout << "No methods found" << std::endl; + return 2; + } + TIter next(methods); + TObject *obj; + int count = 0; + while ((obj = next())) { + TMethod *method = dynamic_cast(obj); + if (method && method->IsMenuItem()) { + std::cout << "Menu item: " << method->GetName() << " (" << method->GetTitle() << ")" << std::endl; + count++; + if (count > 5) break; // Just show a few + } + } + return 0; +}