diff --git a/src/Console/TclConsoleBuilder.cpp b/src/Console/TclConsoleBuilder.cpp index f9fc1d5c8..bb91275ee 100644 --- a/src/Console/TclConsoleBuilder.cpp +++ b/src/Console/TclConsoleBuilder.cpp @@ -9,7 +9,7 @@ QWidget *createConsole(TclInterp *interp, QWidget *w = new QWidget{parent}; w->setLayout(new QGridLayout); TclConsoleWidget *console = - new TclConsoleWidget{interp, std::move(iConsole), buffer}; + new TclConsoleWidget{interp, std::move(iConsole), buffer, w}; SearchWidget *search = new SearchWidget{console}; QObject::connect(console, &TclConsoleWidget::searchEnable, search, diff --git a/src/Main/registerTclCommands.cpp b/src/Main/registerTclCommands.cpp index f01dc035c..dc7778cd4 100644 --- a/src/Main/registerTclCommands.cpp +++ b/src/Main/registerTclCommands.cpp @@ -103,6 +103,94 @@ void registerBasicGuiCommands(FOEDAG::Session* session) { }; session->TclInterp()->registerCmd("process_qt_events", process_qt_events, 0, 0); + + auto qt_getWidget = [](void* clientData, Tcl_Interp* interp, int argc, + const char* argv[]) -> int { + if (argc < 2) return TCL_ERROR; + + const QString widgetName{argv[1]}; + QWidget* w = static_cast(clientData); + QWidget* topWidget = QApplication::topLevelAt(w->mapToGlobal(QPoint())); + if (!topWidget) topWidget = w; + if (!topWidget) { + Tcl_AppendResult(interp, qPrintable("topWidget == nullptr"), (char*)NULL); + return TCL_ERROR; + } + + QWidget* widget = topWidget->findChild(widgetName); + if (!widget) { + Tcl_AppendResult(interp, qPrintable("No such widget"), (char*)NULL); + return TCL_ERROR; + } + QString result = + QString("QWidget(0x%1)") + .arg(QString::number(reinterpret_cast(widget), 16)); + Tcl_AppendResult(interp, qPrintable(result), (char*)NULL); + + return TCL_OK; + }; + session->TclInterp()->registerCmd("qt_getWidget", qt_getWidget, + GlobalSession->MainWindow(), nullptr); + + auto qt_showAllQtObjects = [](void* clientData, Tcl_Interp* interp, int argc, + const char* argv[]) -> int { + QWidget* w = static_cast(clientData); + QWidget* topWidget = QApplication::topLevelAt(w->mapToGlobal(QPoint())); + if (topWidget) { + auto children = topWidget->findChildren(); + QStringList objectsNames; + for (auto child : children) { + if (!child->objectName().isEmpty()) objectsNames += child->objectName(); + } + Tcl_AppendResult(interp, qPrintable(objectsNames.join(" ")), nullptr); + } + return TCL_OK; + }; + session->TclInterp()->registerCmd("qt_showAllQtObjects", qt_showAllQtObjects, + GlobalSession->MainWindow(), nullptr); + + auto qt_testWidget = [](void* clientData, Tcl_Interp* interp, int argc, + const char* argv[]) -> int { + if (argc < 2) { + Tcl_AppendResult(interp, qPrintable("Usage: qt_testWidget ?widget?"), + nullptr); + return TCL_ERROR; + } + QString widgetStr{argv[1]}; + widgetStr.remove("QWidget("); + widgetStr.remove(")"); + bool ok{false}; + ulong widgetPtr = widgetStr.toULong(&ok, 16); + if (!ok || (widgetPtr == 0)) { + Tcl_AppendResult( + interp, qPrintable("Wrong format. Expetced: QWidget(0x?number?)"), + nullptr); + return TCL_ERROR; + } + QWidget* widget = reinterpret_cast(widgetPtr); + + QWidget* w = static_cast(clientData); + QWidget* topWidget = QApplication::topLevelAt(w->mapToGlobal(QPoint())); + if (!topWidget) topWidget = w; + if (!topWidget) { + Tcl_AppendResult(interp, qPrintable("topWidget == nullptr"), nullptr); + return TCL_ERROR; + } + auto children = topWidget->findChildren(); + if (!children.contains(widget)) { + Tcl_AppendResult(interp, qPrintable("Unknown widget"), nullptr); + return TCL_ERROR; + } + + Tcl_AppendResult( + interp, + qPrintable( + QString("QWidget object name = %1").arg(widget->objectName())), + nullptr); + return TCL_OK; + }; + session->TclInterp()->registerCmd("qt_testWidget", qt_testWidget, + GlobalSession->MainWindow(), nullptr); } void registerBasicBatchCommands(FOEDAG::Session* session) { diff --git a/tests/TestGui/gui_console.tcl b/tests/TestGui/gui_console.tcl index 0ec1065d8..c8375ee3f 100644 --- a/tests/TestGui/gui_console.tcl +++ b/tests/TestGui/gui_console.tcl @@ -22,3 +22,4 @@ puts "CONSOLE GUI: console_proc" ; flush stdout ; console_proc tests/TestGu puts "CONSOLE GUI: console_multiline" ; flush stdout ; console_multiline puts "CONSOLE GUI: console_cancel" ; flush stdout ; console_cancel puts "CONSOLE GUI: console_history" ; flush stdout ; console_history +puts "CONSOLE GUI: qt_getWidget" ; flush stdout ; qt_getWidget TclConsole