From 0d035f6898ad9ef9d0188f91529ef66b79b3807f Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Fri, 29 Oct 2021 12:37:17 +0200 Subject: [PATCH] PyQt demo app (#21625) * build python helpers lib * call setMainWindow from python * put in helper lib * linter * move to scripts old-commit-hash: 25e4e94691d90f1a4496a2742c63ede108593e15 --- scripts/pyqt_demo.py | 14 ++++++++++++++ selfdrive/ui/SConscript | 4 +++- selfdrive/ui/qt/python_helpers.py | 21 +++++++++++++++++++++ selfdrive/ui/qt/qt_window.cc | 25 +++++++++++++++++++++++++ selfdrive/ui/qt/qt_window.h | 17 +---------------- 5 files changed, 64 insertions(+), 17 deletions(-) create mode 100755 scripts/pyqt_demo.py create mode 100644 selfdrive/ui/qt/python_helpers.py create mode 100644 selfdrive/ui/qt/qt_window.cc diff --git a/scripts/pyqt_demo.py b/scripts/pyqt_demo.py new file mode 100755 index 0000000000..43716fbeb2 --- /dev/null +++ b/scripts/pyqt_demo.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 + +from PyQt5.QtWidgets import QApplication, QLabel # pylint: disable=no-name-in-module, import-error +from selfdrive.ui.qt.python_helpers import set_main_window + + +if __name__ == "__main__": + app = QApplication([]) + label = QLabel('Hello World!') + + # Set full screen and rotate + set_main_window(label) + + app.exec_() diff --git a/selfdrive/ui/SConscript b/selfdrive/ui/SConscript index 6fa32a78cf..4d3ae49ba0 100644 --- a/selfdrive/ui/SConscript +++ b/selfdrive/ui/SConscript @@ -22,7 +22,7 @@ widgets_src = ["qt/util.cc", "qt/widgets/input.cc", "qt/widgets/drive_stats.cc", "qt/widgets/ssh_keys.cc", "qt/widgets/toggle.cc", "qt/widgets/controls.cc", "qt/widgets/offroad_alerts.cc", "qt/widgets/prime.cc", "qt/widgets/keyboard.cc", "qt/widgets/scrollview.cc", "qt/widgets/cameraview.cc", "#third_party/qrcode/QrCode.cc", "qt/api.cc", - "qt/request_repeater.cc"] + "qt/request_repeater.cc", "qt/qt_window.cc"] if arch != 'aarch64': widgets_src += ["qt/offroad/networking.cc", "qt/offroad/wifiManager.cc"] @@ -48,6 +48,8 @@ qt_env.Program("_soundd", "soundd.cc", LIBS=base_libs) if GetOption('test'): qt_env.Program("tests/playsound", "tests/playsound.cc", LIBS=base_libs) +qt_env.SharedLibrary("qt/python_helpers", ["qt/qt_window.cc"], LIBS=qt_libs) + # spinner and text window qt_env.Program("qt/text", ["qt/text.cc"], LIBS=qt_libs) qt_env.Program("qt/spinner", ["qt/spinner.cc"], LIBS=qt_libs) diff --git a/selfdrive/ui/qt/python_helpers.py b/selfdrive/ui/qt/python_helpers.py new file mode 100644 index 0000000000..4dd99951bf --- /dev/null +++ b/selfdrive/ui/qt/python_helpers.py @@ -0,0 +1,21 @@ + +import os +from cffi import FFI + +import sip # pylint: disable=import-error + +from common.ffi_wrapper import suffix +from common.basedir import BASEDIR + + +def get_ffi(): + lib = os.path.join(BASEDIR, "selfdrive", "ui", "qt", "libpython_helpers" + suffix()) + + ffi = FFI() + ffi.cdef("void set_main_window(void *w);") + return ffi, ffi.dlopen(lib) + + +def set_main_window(widget): + ffi, lib = get_ffi() + lib.set_main_window(ffi.cast('void*', sip.unwrapinstance(widget))) diff --git a/selfdrive/ui/qt/qt_window.cc b/selfdrive/ui/qt/qt_window.cc new file mode 100644 index 0000000000..aad21e6c06 --- /dev/null +++ b/selfdrive/ui/qt/qt_window.cc @@ -0,0 +1,25 @@ +#include "selfdrive/ui/qt/qt_window.h" + +void setMainWindow(QWidget *w) { + const bool wide = (QGuiApplication::primaryScreen()->size().width() >= WIDE_WIDTH) ^ + (getenv("INVERT_WIDTH") != NULL); + const float scale = util::getenv("SCALE", 1.0f); + + w->setFixedSize(QSize(wide ? WIDE_WIDTH : 1920, 1080) * scale); + w->show(); + +#ifdef QCOM2 + QPlatformNativeInterface *native = QGuiApplication::platformNativeInterface(); + wl_surface *s = reinterpret_cast(native->nativeResourceForWindow("surface", w->windowHandle())); + wl_surface_set_buffer_transform(s, WL_OUTPUT_TRANSFORM_270); + wl_surface_commit(s); + w->showFullScreen(); +#endif +} + + +extern "C" { + void set_main_window(void *w) { + setMainWindow((QWidget*)w); + } +} diff --git a/selfdrive/ui/qt/qt_window.h b/selfdrive/ui/qt/qt_window.h index cd1ad928d1..2c9a24e55b 100644 --- a/selfdrive/ui/qt/qt_window.h +++ b/selfdrive/ui/qt/qt_window.h @@ -18,19 +18,4 @@ const QString ASSET_PATH = ":/"; const int WIDE_WIDTH = 2160; -inline void setMainWindow(QWidget *w) { - const bool wide = (QGuiApplication::primaryScreen()->size().width() >= WIDE_WIDTH) ^ - (getenv("INVERT_WIDTH") != NULL); - const float scale = util::getenv("SCALE", 1.0f); - - w->setFixedSize(QSize(wide ? WIDE_WIDTH : 1920, 1080) * scale); - w->show(); - -#ifdef QCOM2 - QPlatformNativeInterface *native = QGuiApplication::platformNativeInterface(); - wl_surface *s = reinterpret_cast(native->nativeResourceForWindow("surface", w->windowHandle())); - wl_surface_set_buffer_transform(s, WL_OUTPUT_TRANSFORM_270); - wl_surface_commit(s); - w->showFullScreen(); -#endif -} +void setMainWindow(QWidget *w);