C

Indicators: Creating Safety-Critical UI

// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial

// This file is part of the Qt Safe Renderer module
#include <QtSafeRenderer/qsafelayout.h>
#include <QtSafeRenderer/qsafelayoutresourcereader.h>
#include <QtSafeRenderer/statemanager.h>

#if defined(HOST_BUILD)
#include <QQuickView>
#include <QWindow>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#endif
#if defined(USE_OUTPUTVERIFIER)
#include <outputverifier.h>
#endif

#include <QtSafeGraphicsAdaptation/safewindow.h>
#include <QtSafeEventHandlerAdaptation/eventhandler.h>

using namespace SafeRenderer;

int main(int argc, char **argv)
{
#ifdef HOST_BUILD
    // In host build when using QtGUI, we need to initialize QApplication before SafeWindow.
    QGuiApplication app(argc, argv);
#endif

    (void)argc;
    (void)argv;
    static QSafeLayoutResourceReader layout;
    layout.readLayout("/layoutData/MainForm/MainForm.ui.srl");

#if defined(USE_OUTPUTVERIFIER)
    try {
#endif

    SafeWindow telltaleWindow(layout.size(), QSafePoint(0U, 0U));

    static SafeRenderer::StateManager stateManager(telltaleWindow, layout);
    telltaleWindow.requestUpdate(); //Request is required because eventHandler is not running yet.

#if defined(USE_OUTPUTVERIFIER)
    EventHandler msgHandler(stateManager, telltaleWindow, outputVerifier);
#else
    EventHandler msgHandler(stateManager, telltaleWindow);
#endif

#if defined(HOST_BUILD)
    // Mixing the Qt and Qt Safe Renderer here is done to demonstrate the integration of both frameworks.
    // This setup allows developers to test, visualize, and verify that the Safe Renderer output is valid and correct alongside the standard Qt UI components.
    // Note: This approach is intended for demonstration purposes only and should not be used in production environments.
    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/ControlUI.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app,
                     [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            qDebug() << "Failed to start the ControlUI.qml";
    }, Qt::QueuedConnection);
    engine.addImportPath(":/imports");
    engine.load(url);
#endif

    msgHandler.handleEvents();

#if defined(USE_OUTPUTVERIFIER)
    }
    catch (const OutputVerifierException &e) {
        std::cout << e.what();
    }
#endif

    return 0;
}