#-----------------------------------------------------------------------------
#
# Testmanager - Graphical Automation and Visualisation Tool
#
# Copyright (C) 2018  Florian Pose <fp@igh.de>
#               2023  Bjarne von Horn <vh@igh.de>
#
# This file is part of Testmanager.
#
# Testmanager is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Testmanager is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with Testmanager. If not, see <http://www.gnu.org/licenses/>.
#
#-----------------------------------------------------------------------------

cmake_minimum_required(VERSION 3.10)

project(testmanager)

option(MODEL_TEST "enable model tests" OFF)

# load Qt
find_package(Qt5 REQUIRED COMPONENTS Gui Network Quick Svg Widgets Xml LinguistTools)
if (MODEL_TEST)
    find_package(Qt5 REQUIRED COMPONENTS Test)
endif()

# load PdWidgets
find_package(QtPdCom1 REQUIRED)
find_package(QtPdWidgets2 REQUIRED)
find_package(PythonLibs REQUIRED)

if (NOT VERSION)
    execute_process(COMMAND sh revision.sh .
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        OUTPUT_VARIABLE VERSION
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
endif()

include(GNUInstallDirs)

add_compile_options(-Wall -Wextra)

add_subdirectory(plugins)

set(TS_FILES
    testmanager_de.ts
)

configure_file(testmanager-lang.qrc "${CMAKE_CURRENT_BINARY_DIR}" COPYONLY)
qt5_add_translation(QM_FILES ${TS_FILES})

add_executable(${PROJECT_NAME}

    $<TARGET_OBJECTS:plugins>

    AboutDialog.h
    BroadcastDialog.h
    ColorDelegate.h
    ConnectDialog.h
    DataModel.h
    DataNode.h
    DataSlot.h
    DataSource.h
    HandleWidget.h
    Legend.h
    LoginDialog.h
    MainWindow.h
    MessageDialog.h
    MessageWidget.h
    Parameter.h
    ParameterModel.h
    ParameterTableModel.h
    ParameterSet.h
    ParameterSetModel.h
    ParameterDialog.h
    Plugin.h
    PropertyDelegate.h
    PropertyFlag.h
    PropertyGroup.h
    Property.h
    PropertyModel.h
    PropertyNode.h
    PropertyTree.h
    ReplaceDialog.h
    ScaleDelegate.h
    ScriptVariable.h
    ScriptVariableModel.h
    SlotDialog.h
    SlotModel.h
    SlotNode.h
    SourceDelegate.h
    StyleDialog.h
    TabDialog.h
    TabPage.h
    TooltipMatrix.h
    WidgetContainer.h

    AboutDialog.cpp
    BroadcastDialog.cpp
    ColorDelegate.cpp
    ConnectDialog.cpp
    DataModel.cpp
    DataNode.cpp
    DataSlot.cpp
    DataSource.cpp
    HandleWidget.cpp
    Legend.cpp
    LoginDialog.cpp
    main.cpp
    MainWindow.cpp
    MessageDialog.cpp
    MessageWidget.cpp
    Parameter.cpp
    ParameterModel.cpp
    ParameterTableModel.cpp
    ParameterSet.cpp
    ParameterSetModel.cpp
    ParameterDialog.cpp
    Plugin.cpp
    Property.cpp
    PropertyDelegate.cpp
    PropertyFlag.cpp
    PropertyGroup.cpp
    PropertyModel.cpp
    PropertyNode.cpp
    PropertyTree.cpp
    ReplaceDialog.cpp
    ScaleDelegate.cpp
    ScriptVariable.cpp
    ScriptVariableModel.cpp
    SlotDialog.cpp
    SlotModel.cpp
    SlotNode.cpp
    SourceDelegate.cpp
    StyleDialog.cpp
    TabDialog.cpp
    TabPage.cpp
    TooltipMatrix.cpp
    WidgetContainer.cpp

    AboutDialog.ui
    BroadcastDialog.ui
    ConnectDialog.ui
    LoginDialog.ui
    MainWindow.ui
    MessageDialog.ui
    MessageWidget.ui
    ParameterDialog.ui
    ReplaceDialog.ui
    SlotDialog.ui
    StyleDialog.ui
    TabDialog.ui

    testmanager.qrc
    testmanager.rc
    "${CMAKE_CURRENT_BINARY_DIR}/testmanager-lang.qrc"
    ${QM_FILES}

    $<$<BOOL:${UNIX}>:SignalReceiver.cpp;SignalReceiver.h>
    $<$<BOOL:${MODEL_TEST}>:modeltest.h;modeltest.cpp>
)

# enable moc, uic and rcc
set_target_properties(${PROJECT_NAME} PROPERTIES
    AUTOUIC 1
    AUTOMOC 1
    AUTORCC 1
)

# link against Qt and PdWidgets
target_link_libraries(${PROJECT_NAME} PUBLIC
    EtherLab::QtPdCom1
    EtherLab::QtPdWidgets2
    Qt5::Gui
    Qt5::Network
    Qt5::Svg
    Qt5::Widgets
    ${PYTHON_LIBRARIES}
)

target_include_directories(${PROJECT_NAME} PUBLIC
    "${CMAKE_CURRENT_SOURCE_DIR}"
    ${PYTHON_INCLUDE_DIRS}
)

install(TARGETS ${PROJECT_NAME}
    ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    RUNTIME DESTINATION  "${CMAKE_INSTALL_BINDIR}"
)

if(COMMIT_DATE)
    target_compile_definitions(${PROJECT_NAME} PRIVATE COMMIT_DATE=${COMMIT_DATE})
endif()

target_compile_definitions(${PROJECT_NAME} PRIVATE VERSION=${VERSION})

if (MODEL_TEST)
    target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Test)
    target_compile_definitions(${PROJECT_NAME} PRIVATE TM_MODEL_TEST)
endif()
