# The Omatrack QML module.
#
# QML documents and the C++ types they import live in this one directory, which
# is what qt_add_qml_module expects: SOURCES are scanned for QML_ELEMENT /
# QML_SINGLETON, so qmllint, qmlls, and qmlcachegen all resolve
# `import Omatrack` without any extra configuration.

if(APPLE)
  qt_add_executable(omatrack MACOSX_BUNDLE
    main.cpp)
else()
  qt_add_executable(omatrack
    main.cpp)
endif()

set_target_properties(omatrack PROPERTIES
  MACOSX_BUNDLE_BUNDLE_NAME "Omatrack"
  MACOSX_BUNDLE_BUNDLE_VERSION "${PROJECT_VERSION}"
  MACOSX_BUNDLE_GUI_IDENTIFIER "io.github.tobi.omatrack"
  MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION}")
if(APPLE)
  set_target_properties(omatrack PROPERTIES OUTPUT_NAME "Omatrack"
    MACOSX_BUNDLE_INFO_PLIST "${PROJECT_SOURCE_DIR}/packaging/macos/Info.plist.in")
endif()

if(WIN32)
  configure_file(
    ${CMAKE_SOURCE_DIR}/assets/omatrack.rc.in
    ${CMAKE_CURRENT_BINARY_DIR}/omatrack.rc
    @ONLY)
  target_sources(omatrack PRIVATE
    WindowsIntegration.h
    WindowsIntegration.cpp
    WindowsAssociations.h
    WindowsAssociations.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/omatrack.rc)
  set_target_properties(omatrack PROPERTIES WIN32_EXECUTABLE TRUE)
endif()

# `pragma Singleton` alone is not enough: the qmldir entry comes from this
# source property, and without it qmllint and qmlcachegen treat Style as an
# ordinary type.
set_source_files_properties(Style.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)

qt_add_qml_module(omatrack
  URI Omatrack
  VERSION 1.0
  QML_FILES
    Main.qml
    RoleDot.qml
    RoleActionRow.qml
    ChannelBrowserPopup.qml
    ChannelMappingField.qml
    ConnectionDialog.qml
    CompactTextField.qml
    CompactButton.qml
    CompactToolButton.qml
    DenseTwoLineRow.qml
    OverlayCard.qml
    Style.qml
    AppHeader.qml
    ChannelsWindow.qml
    LapFilmstrip.qml
    CornerFocusOverlay.qml
    CornerEditOverlay.qml
    DriverMetadataSection.qml
    FileAssociationsDialog.qml
    FileBrowserPane.qml
    FileTreeDelegate.qml
    FilterPill.qml
    PreferencesWindow.qml
    PreferencesDriversPage.qml
    PreferencesLibraryPage.qml
    PreferencesTrackDataPage.qml
    PreferencesUpdatesPage.qml
    PreferencesImageTelemetryPage.qml
    GaugeDiscoveryOverlay.qml
    TextInputDialog.qml
    SessionTreeDelegate.qml
    SpanHoverCard.qml
    TraceCornerRuler.qml
    TraceLaneChrome.qml
    TraceToolbar.qml
    TraceResizeOverlay.qml
    TraceColorButton.qml
    TrackAssignmentDialog.qml
    OperationErrorDialog.qml
    ThinScrollBar.qml
    VideoMetadataDialog.qml
    VideoTelemetryOverlay.qml
    VideoDeltaBar.qml
    VideoSessionLabel.qml
    UsbCopyOverlay.qml
    UsbSyncWindow.qml
    PreferencesEventPage.qml
    PreferencesPluginsPage.qml
    PreferencesOverlayPage.qml
  SOURCES
    AsyncJob.h
    FilterChange.h
    StoreTypes.h
    StoreModels.h StoreModels.cpp
    LibraryModel.h LibraryModel.cpp
    AppUpdate.h AppUpdate.cpp
    AppUpdater.h AppUpdater.cpp
    DamperStripView.h DamperStripView.cpp
    MpvVideoItem.h MpvVideoItem.cpp
    ImageTelemetryController.h ImageTelemetryController.cpp
    GaugeSetup.h GaugeSetup.cpp
    GaugeDetectorArtifact.h GaugeDetectorArtifact.cpp
    GaugeBundleCheck.h GaugeBundleCheck.cpp
    GaugeModelPaths.h
    ImageModelManager.h ImageModelManager.cpp
    ImageModelManifest.h ImageModelManifest.cpp
    ImageTelemetryTraces.h ImageTelemetryTraces.cpp
    ImageTelemetryTraceGeometry.h ImageTelemetryTraceGeometry.cpp
    FilmstripLayout.h
    OmarchyTheme.h OmarchyTheme.cpp
    ComparisonAlignment.h ComparisonAlignment.cpp
    TrackMetadata.h TrackMetadata.cpp
    TelemetryStore.h TelemetryStore.cpp
    OverlayManager.h OverlayManager.cpp
    PreferencesStore.h PreferencesStore.cpp
    TrackAtlasManager.h TrackAtlasManager.cpp
    LibraryLocation.h LibraryLocation.cpp
    RemoteCache.h RemoteCache.cpp
    WebDavBackend.cpp
    SigV4.h SigV4.cpp
    S3Backend.cpp
    TrackAtlasSpatial.h TrackAtlasSpatial.cpp
    ChannelAppearance.h
    TraceDecimator.h TraceDecimator.cpp
    TraceSceneBuilder.h TraceSceneBuilder.cpp
    TraceTextCache.h TraceTextCache.cpp
    UsbMedia.h UsbMedia.cpp
    UsbCopy.h UsbCopy.cpp
    OverlayResample.h
    PluginHost.h PluginHost.cpp
    ScrollAnchor.h ScrollAnchor.cpp
    PathJail.h PathJail.cpp
    IndexCache.h IndexCache.cpp
    LuaRename.h LuaRename.cpp
    ModelDiff.h
    VerboseLog.h VerboseLog.cpp
    SingleInstance.h SingleInstance.cpp
    FileOpenEvents.h
    TraceSnapshot.h
    TraceInteraction.h TraceInteraction.cpp
    TraceLaneSizing.h
    TraceLaneLayout.h TraceLaneLayout.cpp
    TraceView.h TraceView.cpp
    VideoTelemetryHud.h VideoTelemetryHud.cpp
    VideoSyncController.h VideoSyncController.cpp
    DeadlineQueue.h DeadlineQueue.cpp
    YamlConfig.h YamlConfig.cpp
)
if(OMATRACK_ENABLE_AUTOTEST_HARNESS)
  target_sources(omatrack PRIVATE AutotestHarness.h AutotestHarness.cpp
    FilmstripAutotest.cpp
    TraceRenderingAutotest.cpp
    CornerNavigationAutotest.cpp
    TraceResizeAutotest.cpp
    SessionChromeAutotest.cpp
    UsbCopyAutotest.cpp
    PluginAutotest.cpp
    GaugeDiscoveryAutotest.h GaugeDiscoveryAutotest.cpp
    ImageTelemetryScanAutotest.h ImageTelemetryScanAutotest.cpp
    ImageModelManagementAutotest.h ImageModelManagementAutotest.cpp)
  target_compile_definitions(omatrack PRIVATE
    OMATRACK_ENABLE_AUTOTEST_HARNESS=1)
endif()

# Fonts and Qt Quick Controls configuration are bundled application resources.
qt_add_resources(omatrack "omatrack_assets"
  PREFIX "/"
  BASE assets
  FILES
    assets/fonts/Geist-Variable.ttf
    assets/fonts/GeistMono-Variable.ttf
    assets/fonts/OFL.txt
    assets/qtquickcontrols2.conf
)
qt_add_resources(omatrack "omatrack_branding"
  PREFIX "/"
  BASE ${CMAKE_SOURCE_DIR}
  FILES
    ${CMAKE_SOURCE_DIR}/assets/omatrack.svg
    ${CMAKE_SOURCE_DIR}/assets/omatrack.ico)
# Example plugins ship inside the binary so Preferences → Plugins can install
# them into the user's plugin folder from an AppImage or installer.
qt_add_resources(omatrack "omatrack_plugin_examples"
  PREFIX "/"
  BASE ${CMAKE_SOURCE_DIR}
  FILES
    ${CMAKE_SOURCE_DIR}/plugins/weather/plugin.lua)

qt_add_resources(omatrack "omatrack_model_manifest"
  PREFIX "/omatrack-models"
  BASE "${PROJECT_SOURCE_DIR}/models"
  FILES "${PROJECT_SOURCE_DIR}/models/bundle.json")

target_link_libraries(omatrack PRIVATE
  lua_sandbox
  sol2_headers
  omatrack_core
  omatrack_headless
  omatrack_portable_runtime
  omatrack_warnings
  omatrack_yaml
  omatrack_mpv
  omatrack_inference
  omatrack_image_cache
  Qt6::Concurrent
  Qt6::Quick
  Qt6::QuickControls2
  Qt6::Qml
  Qt6::Network)
if(WIN32)
  # Qt6::GuiPrivate exposes <QtGui/qpa/qplatformwindow_p.h>, the native
  # interface WindowsIntegration.cpp uses to keep fullscreen composited.
  target_link_libraries(omatrack PRIVATE Qt6::GuiPrivate dwmapi shell32)
endif()

# After link libraries so the PCH sees the same include dirs as the TUs.
if(OMATRACK_ENABLE_PCH)
  target_precompile_headers(omatrack PRIVATE omatrack_pch.h)
endif()

# qmlprofiler and qmlpreview can only attach to a QML-debug-enabled binary.
target_compile_definitions(omatrack PRIVATE
  OMATRACK_VERSION="${PROJECT_VERSION}"
  $<$<NOT:$<CONFIG:Release>>:QT_QML_DEBUG>)

install(TARGETS omatrack
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  BUNDLE DESTINATION .)
include(${PROJECT_SOURCE_DIR}/cmake/GaugeModelPackage.cmake)

if(UNIX AND NOT APPLE)
  install(FILES
    ${CMAKE_SOURCE_DIR}/packaging/linux/io.github.tobi.omatrack.desktop
    DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)
  install(FILES ${CMAKE_SOURCE_DIR}/assets/omatrack.svg
    DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/scalable/apps
    RENAME io.github.tobi.omatrack.svg)
  install(FILES ${CMAKE_SOURCE_DIR}/packaging/linux/omatrack.xml
    DESTINATION ${CMAKE_INSTALL_DATADIR}/mime/packages)
  install(FILES
    ${CMAKE_SOURCE_DIR}/packaging/linux/io.github.tobi.omatrack.metainfo.xml
    DESTINATION ${CMAKE_INSTALL_DATADIR}/metainfo)
endif()

# Windows and macOS releases are self-contained application bundles. Linux
# distribution installs deliberately retain their system Qt dependencies; the
# release workflow turns that install tree into a portable AppImage.
if(WIN32)
  # Qt does not deploy translations that omatrack never loads, and keeping
  # them out of the tree keeps the qt.conf written below fully consistent.
  qt_generate_deploy_qml_app_script(
    TARGET omatrack
    OUTPUT_SCRIPT omatrack_deploy_script
    NO_TRANSLATIONS)
  # Flat Windows release layout. Runtime DLLs are deployed next to the exe at
  # the install root (QT_DEPLOY_BIN_DIR "."), while Qt plugins and QML modules
  # land under lib/. The deploy support script leaves these alone because of
  # its `if(NOT ...)` guards, so they must be set before the generated script
  # is included. Generating a qt.conf is how the deploy step reconciles the
  # binaries-with-bundled-DLLs root with the lib/ plugin and QML tree.
  file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/omatrack_deploy_flat.cmake
    "set(QT_DEPLOY_BIN_DIR \".\")\n"
    "set(QT_DEPLOY_LIB_DIR \"lib\")\n"
    "set(QT_DEPLOY_PLUGINS_DIR \"lib/plugins\")\n"
    "set(QT_DEPLOY_QML_DIR \"lib/qml\")\n"
    "include(\"${omatrack_deploy_script}\")\n")
  install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/omatrack_deploy_flat.cmake)
elseif(APPLE)
  qt_generate_deploy_qml_app_script(
    TARGET omatrack
    OUTPUT_SCRIPT omatrack_deploy_script)
  install(SCRIPT ${omatrack_deploy_script})
endif()
