Skip to content

Commit

Permalink
Merge pull request #240 from KochynVolodymyr/tcl_getWidget_from_qt
Browse files Browse the repository at this point in the history
Add basic functionality for getting qt widgets in tcl (issue #236)
  • Loading branch information
alain-rs authored Mar 10, 2022
2 parents c19eecf + 28b058f commit c8493ba
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Console/TclConsoleBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
88 changes: 88 additions & 0 deletions src/Main/registerTclCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QWidget*>(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<QWidget*>(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<ulong>(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<QWidget*>(clientData);
QWidget* topWidget = QApplication::topLevelAt(w->mapToGlobal(QPoint()));
if (topWidget) {
auto children = topWidget->findChildren<QObject*>();
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<QWidget*>(widgetPtr);

QWidget* w = static_cast<QWidget*>(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<QWidget*>();
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) {
Expand Down
1 change: 1 addition & 0 deletions tests/TestGui/gui_console.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c8493ba

Please sign in to comment.