# SPDX-FileCopyrightText: 2026 Blagovest Petrov <blagovest@petrovs.info>
# SPDX-FileCopyrightText: 2026 Vute Tech Ltd. <https://vute.tech>
# SPDX-License-Identifier: GPL-3.0-or-later

include(ECMSetupVersion)
ecm_setup_version(${PROJECT_VERSION} VARIABLE_PREFIX KRAKOMAN VERSION_HEADER ${CMAKE_CURRENT_BINARY_DIR}/version.h)

# The C++ models and controllers behind the pages. The executable compiles
# them into its QML module; the autotests link them as a plain library.
set(krakoman_core_sources
    daemon.cpp
    daemon.h
    documentformats.cpp
    documentformats.h
    documenttranslator.cpp
    documenttranslator.h
    history.cpp
    history.h
    pairmodel.cpp
    pairmodel.h
    richtext.cpp
    richtext.h
    sentencehighlighter.cpp
    sentencehighlighter.h
    sessions.cpp
    sessions.h
    settings.cpp
    settings.h
    spelling.cpp
    spelling.h
    textrecognizer.cpp
    textrecognizer.h
    translationsession.cpp
    translationsession.h)
set(krakoman_core_libraries
    DragomanQt::DragomanQt
    Qt6::Concurrent
    Qt6::DBus
    Qt6::Gui
    Qt6::Qml
    Qt6::Quick
    KF6::SonnetCore
    KF6::ConfigCore
    KF6::CoreAddons
    KF6::I18n)

if(Tesseract_FOUND)
    list(APPEND krakoman_core_libraries PkgConfig::Tesseract)
    set(krakoman_ocr 1)
else()
    set(krakoman_ocr 0)
endif()
# Tesseract's headers use exceptions; the rest of Krakoman does not.
set_source_files_properties(textrecognizer.cpp PROPERTIES
    COMPILE_DEFINITIONS KRAKOMAN_HAVE_TESSERACT=${krakoman_ocr}
    COMPILE_OPTIONS "-fexceptions")

qt_add_executable(krakoman
    desktopintegration.cpp
    desktopintegration.h
    main.cpp)
# Resources under :/qt/qml, which the engine imports from by default, and
# no qmldir needed in the qml/ subdirectory.
qt_policy(SET QTP0001 NEW)
qt_policy(SET QTP0004 NEW)
qt_add_qml_module(krakoman
    URI dev.l10n_bg.krakoman
    VERSION 1.0
    DEPENDENCIES QtQuick
    SOURCES ${krakoman_core_sources}
    QML_FILES
        qml/Main.qml
        qml/TranslatePage.qml
        qml/TranslationPane.qml
        qml/HistoryPage.qml
        qml/SettingsPage.qml
        qml/DocumentPage.qml
        qml/SpeakButton.qml
        qml/SpellChecker.qml
        qml/LanguageComboBox.qml
        qml/PairsPage.qml
        qml/StatusPage.qml)
# Bytecode only: qmlcachegen's C++ code generation links Qt's private
# API, versioned per Qt minor release, so a package built with Qt 6.10
# would refuse to install next to 6.11. Bytecode from another Qt version
# is simply recompiled at startup.
set_target_properties(krakoman PROPERTIES QT_QMLCACHEGEN_ARGUMENTS "--only-bytecode")

# qt_add_qml_module adds Qt's private include directories, which some
# distributions (Debian) ship only in -private-dev packages; nothing here
# includes from them, so their absence is no reason to warn.
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    target_compile_options(krakoman PRIVATE -Wno-missing-include-dirs)
endif()
# Qt is shared: the QML modules are found at run time, not linked.
set_target_properties(krakoman PROPERTIES QT_QML_MODULE_NO_IMPORT_SCAN TRUE)
target_compile_definitions(krakoman PRIVATE TRANSLATION_DOMAIN="krakoman")
target_include_directories(krakoman PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(krakoman PRIVATE
    ${krakoman_core_libraries}
    Qt6::Quick
    Qt6::QuickControls2
    Qt6::Widgets
    KF6::Crash
    KF6::DBusAddons
    KF6::GlobalAccel
    KF6::GuiAddons
    KF6::I18nQml
    KF6::StatusNotifierItem)

if(BUILD_TESTING)
    add_library(krakomancore STATIC ${krakoman_core_sources})
    target_compile_definitions(krakomancore PUBLIC TRANSLATION_DOMAIN="krakoman")
    target_include_directories(krakomancore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
    target_link_libraries(krakomancore PUBLIC ${krakoman_core_libraries})
endif()

install(TARGETS krakoman ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
