cmake_minimum_required(VERSION 3.2...3.12 FATAL_ERROR)

project ("host_auto_features_live_qtwidgets_cpp")

message (STATUS "[${PROJECT_NAME}] Processing ${CMAKE_CURRENT_LIST_FILE}")

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

if (MSVC)
    set (SAMPLE_TARGET_DEFINITION_ADDENDUM WIN32)
endif ()

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Core Gui REQUIRED
        HINTS
        "${Qt6_DIR}"
        "${Qt5_DIR}")
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Core Gui REQUIRED
        HINTS
        "${QT_DIR}")

# Setup target executable with the same name as our project
add_executable (${PROJECT_NAME} ${SAMPLE_TARGET_DEFINITION_ADDENDUM}
    acquisitionworker.cpp
    acquisitionworker.h
    autofeatures.cpp
    autofeatures.h
    device.cpp
    device.h
    display.cpp
    display.h
    main.cpp
    mainwindow.cpp
    mainwindow.h
    floatcontrol.cpp
    floatcontrol.h
)

# Find packages
if (NOT TARGET ids_peak)
    find_package (ids_peak REQUIRED)
endif ()

if (NOT TARGET ids_peak_ipl)
    find_package (ids_peak_ipl REQUIRED)
endif()

if (NOT TARGET ids_peak_afl)
    find_package (ids_peak_afl REQUIRED)
endif()

find_package (Threads REQUIRED)

set(QT_INSTALL_PREFIX "${_qt${QT_VERSION_MAJOR}Core_install_prefix}")

# Set include directories
target_include_directories (${PROJECT_NAME}
    PRIVATE ${CMAKE_CURRENT_LIST_DIR}
)

# disable the deprecated warning
target_compile_definitions (${PROJECT_NAME}
    PRIVATE
        PEAK_AFL_NO_WARN_DEPRECATED
)

# Link against libraries
target_link_libraries (${PROJECT_NAME}
    ids_peak::ids_peak
    ids_peak_ipl::ids_peak_ipl
    ids_peak_afl::ids_peak_afl
    ${CMAKE_THREAD_LIBS_INIT}
    Qt${QT_VERSION_MAJOR}::Widgets
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Gui
)

# Call deploy functions
# These functions will add a post-build steps to your target in order to copy all needed files (e.g. DLL's) to the output directory.
ids_peak_deploy(${PROJECT_NAME})
ids_peak_ipl_deploy(${PROJECT_NAME})
ids_peak_afl_deploy(${PROJECT_NAME})

# Set C++ standard to 14 (required for ids_peak)
set_target_properties(${PROJECT_NAME} PROPERTIES
    CXX_STANDARD 14
    CXX_STANDARD_REQUIRED ON
    CXX_EXTENSIONS NO
)


# Enable multiprocessing for MSVC
if (MSVC)
    target_compile_options (${PROJECT_NAME}
        PRIVATE "/MP"
    )
endif ()

if (WIN32)
    if(NOT WINDEPLOYQT_EXECUTABLE)
        get_target_property(_qmake_executable Qt${QT_VERSION_MAJOR}::qmake IMPORTED_LOCATION)
        get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)
        find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS "${_qt_bin_dir}")
    endif()

    set(winqt_deploy_arguments
            --no-translations
            --no-system-d3d-compiler
            --no-opengl-sw
            --no-svg
            --no-quick-import)

    if (QT_VERSION_MAJOR EQUAL 6)
        list(APPEND winqt_deploy_arguments
                --no-system-dxc-compiler
        )
    endif()

    # Copy Qt files to the output folder
    message (STATUS "[${PROJECT_NAME}] Add PostBuildStep for copy of the Qt libs.")
    add_custom_command (TARGET ${PROJECT_NAME} POST_BUILD
            COMMAND "${WINDEPLOYQT_EXECUTABLE}"
            ${winqt_deploy_arguments}
            --dir $<TARGET_FILE_DIR:${PROJECT_NAME}>
            $<TARGET_FILE:${PROJECT_NAME}>
            )
endif ()

target_compile_definitions(${PROJECT_NAME}
    PRIVATE QT_NO_DEPRECATED_WARNINGS
    )

# For unix Build we need the environment variable GENICAM_GENTL32_PATH respectivily GENICAM_GENTL64_PATH to find the GenTL producer libraries.
# To set these environment variables a shell script is used. This script can be automatically generated via ids_peak_generate_starter_script.
# The shell script will be saved at ${CMAKE_CURRENT_BINARY_DIR}/${targetName}.sh and automatically copied to the output directory during post-build.

# To run the samples run this script, not the binary.
if(UNIX)
    ids_peak_generate_starter_script(${PROJECT_NAME})
endif()
