file(GLOB sources ../src/*${cpp_ext})

add_executable(mtfmapper_bin ${sources})
set_target_properties(mtfmapper_bin PROPERTIES CLEAN_DIRECT_OUTPUT 1 OUTPUT_NAME mtf_mapper)
set_target_properties(mtfmapper_bin PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
if (MSVC)
  set_target_properties(mtfmapper_bin PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_BINARY_DIR})
endif (MSVC)
target_link_libraries(mtfmapper_bin ${OpenCV_LIBS} ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES})

file(GLOB gen_sources ../src/generator/*${cpp_ext})
add_executable(generate_rectangle ${gen_sources})
set_target_properties(generate_rectangle PROPERTIES CLEAN_DIRECT_OUTPUT 1 OUTPUT_NAME mtf_generate_rectangle)
set_target_properties(generate_rectangle PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
if (MSVC)
  set_target_properties(generate_rectangle PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_BINARY_DIR})
endif (MSVC)
target_link_libraries(generate_rectangle ${OpenCV_LIBS} ${CMAKE_THREAD_LIBS_INIT})

file(GLOB chart_sources ../src/test_chart_generator/*${cpp_ext})
add_executable(generate_test_chart ${chart_sources})
set_target_properties(generate_test_chart PROPERTIES CLEAN_DIRECT_OUTPUT 1 OUTPUT_NAME mtf_generate_test_chart)
set_target_properties(generate_test_chart PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
if (MSVC)
  set_target_properties(generate_test_chart PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_BINARY_DIR})
endif (MSVC)
target_link_libraries(generate_test_chart ${OpenCV_LIBS} ${CMAKE_THREAD_LIBS_INIT})

# Qt5 
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Widgets)

get_target_property(q5name Qt5::Widgets LOCATION)

IF (EXISTS ${q5name})
  file(GLOB qt_SRCS ${PROJECT_SOURCE_DIR}/src/gui/*${cpp_ext} ${PROJECT_SOURCE_DIR}/src/gui/*.h)
  QT5_ADD_RESOURCES(UI_RESOURCES_RCC ${PROJECT_SOURCE_DIR}/src/gui/mtfm.qrc)
  if (MSVC)
      add_executable(mtf_mapper_gui WIN32 ${qt_SRCS} ${UI_RESOURCES_RCC} ${PROJECT_SOURCE_DIR}/src/gui/icons.rc)
  else (MSVC)
      add_executable(mtf_mapper_gui ${qt_SRCS} ${UI_RESOURCES_RCC})
  endif (MSVC)
  set_target_properties(mtf_mapper_gui PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  if (MSVC)
    set_target_properties(mtf_mapper_gui PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_BINARY_DIR})
  endif (MSVC)
  set_property(TARGET mtf_mapper_gui PROPERTY CXX_STANDARD 17)
  set_property(TARGET mtf_mapper_gui PROPERTY CXX_STANDARD_REQUIRED ON)
  qt5_use_modules(mtf_mapper_gui Widgets Charts)
  target_link_libraries(mtf_mapper_gui ${QT_LIBRARIES} ${OpenCV_LIBS} Threads::Threads)
  install(TARGETS mtf_mapper_gui RUNTIME DESTINATION bin)
ELSE (EXISTS ${q5name})
  message("QT5Widgets not found, not configuring GUI")
ENDIF (EXISTS ${q5name})

install(TARGETS mtfmapper_bin generate_rectangle generate_test_chart RUNTIME DESTINATION bin)
