# CMake script for PAPPSOms++ library
# Author: Olivier Langella
# Created: 22/12/2017
# Rework: April 2020 (Coronavirus confinement)
# Major rework of the whole project structure, Filippo Rusconi, summer 2025

message("\n${BoldGreen}Now configuring src/gui for ${PROJECT_NAME}${ColourReset}\n")

if(NOT QCustomPlotQt6_FOUND)
    message("Still searching for QCustomPlotQt6")
endif()

########################################################
# Files
set(CPP_FILES
    graphicdevicewidget.cpp
    httpbutton/httpbutton.cpp
    precisionwidget/precisionwidget.cpp
    massspectrumwidget/qcpspectrum.cpp
    massspectrumwidget/massspectrumwidget.cpp
    obo/obochooserwidget/obochooserwidget.cpp
    obo/obolistwidget/obolistmodel.cpp
    obo/obolistwidget/obolistproxymodel.cpp
    obo/obolistwidget/obolistwidget.cpp
    obo/obotermform/obotermform.cpp
    xicwidget/qcpxic.cpp
    xicwidget/xicwidget.cpp
    plotwidget/baseplotcontext.cpp
    plotwidget/massspectraceplotcontext.cpp
    plotwidget/colormapplotconfig.cpp
    plotwidget/baseplotwidget.cpp
    plotwidget/basetraceplotwidget.cpp
    plotwidget/ticxicchromtraceplotwidget.cpp
    plotwidget/massspectraceplotwidget.cpp
    plotwidget/driftspectraceplotwidget.cpp
    plotwidget/basecolormapplotwidget.cpp
    plotwidget/ticxicchrommassspeccolormapplotwidget.cpp
    plotwidget/ticxicchromdriftspeccolormapplotwidget.cpp
    plotwidget/driftspecmassspeccolormapplotwidget.cpp
    switchbuttonwidget/switchbuttonwidget.cpp
)

set(GUI_UIS ./obo/obochooserwidget/uiobochooserwidget.ui ./obo/obolistwidget/uiobolistwidget.ui
            ./obo/obotermform/uiobotermform.ui
)

# this will run uic on .ui files:
qt6_wrap_ui(GUI_UI_HDRS ${GUI_UIS})

# For the JS reference text.
qt6_add_resources(QRC_SRCS libpappsomspp_gui_resources.qrc)

message("Generated the following resource files: ${QRC_SRCS}")

file(GLOB_RECURSE Gui_HEADERS
    LIST_DIRECTORIES false
    RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
    "*.h"
)
message(STATUS "Gui header files: ${Gui_HEADERS}")

#set(INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src)

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

#############################################################
# Build the static lib
add_library(
    Gui_static STATIC
    ${Gui_HEADERS} ${CPP_FILES} ${GUI_UI_HDRS} ${QRC_SRCS}
)

set_target_properties(
    Gui_static
    PROPERTIES EXPORT_NAME "Gui_static"
               OUTPUT_NAME pappsomspp-gui
               CLEAN_DIRECT_OUTPUT 1
               LINK_FLAGS "-Wl,--whole-archive"
)

target_compile_definitions(Gui_static PRIVATE -DPMSPP_LIBRARY)

target_link_libraries(
    Gui_static
    PUBLIC Qt6::Core
           Qt6::Widgets
           Qt6::Svg
           Qt6::PrintSupport
           Qt6::Qml
           Boost::iostreams
           Boost::filesystem
           Boost::thread
           Boost::chrono
           PwizLite::PwizLite
           liblzf::liblzf
           QCustomPlotQt6::QCustomPlotQt6
           OdsStream::Core
           Core_static
)

# FIXME: should check that this still required. It was years ago.
if(WIN32 OR _WIN32)
    # target_link_libraries(Gui_static stdc++)
endif()

# The install interface that is ${prefix}/include.
target_include_directories(
    Gui_static
    PUBLIC # These include directories are for building of this lib.
           $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
           # These include directories are for users of this lib.
           $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

get_target_property(Gui_static_INCLUDES Gui_static INCLUDE_DIRECTORIES)
message(STATUS "Gui_static_INCLUDES: ${Gui_static_INCLUDES}")

install(
    TARGETS Gui_static
    EXPORT PappsoMSppWidgetStaticTargets
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    INCLUDES
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

# Export static targets
install(
    EXPORT PappsoMSppWidgetStaticTargets
    FILE PappsoMSppWidgetStaticTargets.cmake
    NAMESPACE PappsoMSpp::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pappsomspp
)

export(
    EXPORT PappsoMSppWidgetStaticTargets
    FILE "${CMAKE_CURRENT_BINARY_DIR}/PappsoMSppWidgetStaticTargets.cmake"
    NAMESPACE PappsoMSpp::
)

#############################################################
# Build the shared lib
add_library(
    Gui SHARED
    ${Gui_HEADERS} ${CPP_FILES} ${GUI_UI_HDRS} ${QRC_SRCS}
)

set_target_properties(
    Gui
    PROPERTIES EXPORT_NAME "Gui"
               OUTPUT_NAME pappsomspp-gui
               CLEAN_DIRECT_OUTPUT 1
               VERSION ${PAPPSOMSPP_VERSION}
               SOVERSION ${PAPPSOMSPP_VERSION_MAJOR}
               LINK_FLAGS "-Wl,--no-as-needed ${LINKER_FLAGS}"
               POSITION_INDEPENDENT_CODE ON
)

target_compile_definitions(Gui PRIVATE -DPMSPP_LIBRARY)

target_link_libraries(
    Gui
    PUBLIC PwizLite::PwizLite
           Boost::iostreams
           Boost::filesystem
           Boost::thread
           Boost::chrono
           SQLite3::SQLite3
           QCustomPlotQt6::QCustomPlotQt6
           Qt6::PrintSupport
           Qt6::Gui
           Qt6::Widgets
           Qt6::Svg
           Qt6::Sql
           Qt6::Concurrent
           Qt6::Xml
           Qt6::Core5Compat
           Qt6::Core
           Qt6::Qml
           ZLIB::ZLIB
           Zstd::Zstd
           liblzf::liblzf
           OdsStream::Core
           Core
)

# Should check that this still required. It was years ago.
if(WIN32 OR _WIN32)
    #target_link_libraries(Gui stdc++)
endif()

# The INSTALL_INTERFACE that is ${prefix}/include

target_include_directories(
    Gui
    PUBLIC # These include directories are for building of this lib.
           $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
           # These include directories are for users of this lib.
           $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

get_target_property(Gui_INCLUDES Gui INCLUDE_DIRECTORIES)
message(STATUS "Gui_INCLUDES: ${Gui_INCLUDES}")

# Install libs

install(
    TARGETS Gui
    EXPORT PappsoMSppWidgetSharedTargets
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    INCLUDES
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

# Export shared targets
install(
    EXPORT PappsoMSppWidgetSharedTargets
    FILE PappsoMSppWidgetSharedTargets.cmake
    NAMESPACE PappsoMSpp::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pappsomspp
)

export(
    EXPORT PappsoMSppWidgetSharedTargets
    FILE "${CMAKE_CURRENT_BINARY_DIR}/PappsoMSppWidgetSharedTargets.cmake"
    NAMESPACE PappsoMSpp::
)


message("\n${BoldGreen}Done configuring src/widget for ${PROJECT_NAME}${ColourReset}\n")
