#-----------------------------------------------------------------------------
#
# Testmanager - Graphical Automation and Visualisation Tool
#
# Copyright (C) 2018-2025  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.13)

project(testmanager VERSION 1.2.2)  # remember to edit history in README.md

option(MODEL_TEST "enable model tests" OFF)
option(USE_QT6 "Use Qt6 instead of Qt5" OFF)
option(PUT_QT_MAJOR_INTO_LIBNAME "Put Qt Major Version into Library Name" OFF)

if(USE_QT6)
    set(Qt Qt6)
    find_package(${Qt} REQUIRED COMPONENTS Core5Compat)
else()
    set(Qt Qt5)
endif()

# load Qt
find_package(${Qt} REQUIRED
    COMPONENTS Gui Network Svg Widgets Xml LinguistTools)

if (MODEL_TEST)
    find_package(${Qt} REQUIRED COMPONENTS Test)
endif()

if (PUT_QT_MAJOR_INTO_LIBNAME)
    set(QtPdWidgets "${Qt}PdWidgets2")
    set(QtPdCom "${Qt}PdCom1")
else()
    set(QtPdWidgets "QtPdWidgets2")
    set(QtPdCom "QtPdCom1")
endif()

# load PdWidgets
find_package(${QtPdCom} REQUIRED)
find_package(${QtPdWidgets} REQUIRED)

# find numpy include path
if (NOT NUMPY_INCLUDE_DIR)
    find_package(PythonInterp REQUIRED)
    execute_process(COMMAND
        ${PYTHON_EXECUTABLE} -c "import numpy; print(numpy.get_include())"
        OUTPUT_VARIABLE NUMPY_INCLUDE_DIR
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
endif()

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()

add_compile_options(-Wall -Wextra)

include(GNUInstallDirs)

set(TS_FILES
    testmanager_de.ts
)

configure_file(testmanager-lang.qrc "${CMAKE_CURRENT_BINARY_DIR}" COPYONLY)
if (USE_QT6)
    qt6_add_translation(QM_FILES ${TS_FILES})
else()
    qt5_add_translation(QM_FILES ${TS_FILES})
endif()

set(HEADERS
    src/AboutDialog.h
    src/BroadcastDialog.h
    src/ColorDelegate.h
    src/ConnectDialog.h
    src/DataModel.h
    src/DataNode.h
    src/DataSlot.h
    src/DataSource.h
    src/DetachableTabWidget.h
    src/DetachedDockWidget.h
    src/HandleWidget.h
    src/Legend.h
    src/LoginDialog.h
    src/MainWindow.h
    src/MessageDialog.h
    src/MessageWidget.h
    src/MultiDimParameterDelegate.h
    src/MultiDimParameterDelegate_p.h
    src/Parameter.h
    src/ParameterDialog.h
    src/ParameterItemDelegate.h
    src/ParameterModel.h
    src/ParameterSaveDialog.h
    src/ParameterSet.h
    src/ParameterSetModel.h
    src/ParameterTableModel.h
    src/Plugin.h
    src/Property.h
    src/PropertyDelegate.h
    src/PropertyFlag.h
    src/PropertyGroup.h
    src/PropertyModel.h
    src/PropertyNode.h
    src/PropertyTree.h
    src/ReplaceDialog.h
    src/ScalarSubscriberPluginImpl.h
    src/ScaleDelegate.h
    src/ScriptVariable.h
    src/ScriptVariableModel.h
    src/Selector.h
    src/SelectorDelegate.h
    src/SelectorDialog.h
    src/SlotDialog.h
    src/SlotModel.h
    src/SlotModelCollection.h
    src/SlotNode.h
    src/SlotView.h
    src/SlotViewFilter.h
    src/SourceDelegate.h
    src/SourceTree.h
    src/StyleDialog.h
    src/TabDialog.h
    src/TabPage.h
    src/TooltipMatrix.h
    src/WidgetContainer.h

    plugins/BarPlugin.h
    plugins/CheckBoxPlugin.h
    plugins/DialPlugin.h
    plugins/DialPluginDialog.h
    plugins/DigitalPlugin.h
    plugins/DoubleSpinBoxPlugin.h
    plugins/GraphPlugin.h
    plugins/LedPlugin.h
    plugins/ParameterSetWidgetPlugin.h
    plugins/PushButtonPlugin.h
    plugins/QLabelPlugin.h
    plugins/RadioButtonPlugin.h
    plugins/RotorPlugin.h
    plugins/SendBroadcastPlugin.h
    plugins/TableViewPlugin.h
    plugins/TableViewPluginDialog.h
    plugins/TableViewPluginPrivate.h
    plugins/TextPlugin.h
    plugins/TextPluginDialog.h
    plugins/TouchEditPlugin.h
    plugins/XYGraphPlugin.h
)

set(SOURCES
    src/AboutDialog.cpp
    src/BroadcastDialog.cpp
    src/ColorDelegate.cpp
    src/ConnectDialog.cpp
    src/DataModel.cpp
    src/DataNode.cpp
    src/DataSlot.cpp
    src/DataSource.cpp
    src/DetachableTabWidget.cpp
    src/DetachedDockWidget.cpp
    src/HandleWidget.cpp
    src/Legend.cpp
    src/LoginDialog.cpp
    src/MainWindow.cpp
    src/MessageDialog.cpp
    src/MessageWidget.cpp
    src/MultiDimParameterDelegate.cpp
    src/Parameter.cpp
    src/ParameterDialog.cpp
    src/ParameterItemDelegate.cpp
    src/ParameterModel.cpp
    src/ParameterSaveDialog.cpp
    src/ParameterSet.cpp
    src/ParameterSetModel.cpp
    src/ParameterTableModel.cpp
    src/Plugin.cpp
    src/Property.cpp
    src/PropertyDelegate.cpp
    src/PropertyFlag.cpp
    src/PropertyGroup.cpp
    src/PropertyModel.cpp
    src/PropertyNode.cpp
    src/PropertyTree.cpp
    src/ReplaceDialog.cpp
    src/ScaleDelegate.cpp
    src/ScriptVariable.cpp
    src/ScriptVariableModel.cpp
    src/Selector.cpp
    src/SelectorDelegate.cpp
    src/SelectorDialog.cpp
    src/SlotDialog.cpp
    src/SlotModel.cpp
    src/SlotModelCollection.cpp
    src/SlotNode.cpp
    src/SlotView.cpp
    src/SlotViewFilter.cpp
    src/SourceDelegate.cpp
    src/SourceTree.cpp
    src/StyleDialog.cpp
    src/TabDialog.cpp
    src/TabPage.cpp
    src/TooltipMatrix.cpp
    src/WidgetContainer.cpp
    src/main.cpp

    plugins/BarPlugin.cpp
    plugins/CheckBoxPlugin.cpp
    plugins/DialPlugin.cpp
    plugins/DialPluginDialog.cpp
    plugins/DigitalPlugin.cpp
    plugins/DoubleSpinBoxPlugin.cpp
    plugins/GraphPlugin.cpp
    plugins/LedPlugin.cpp
    plugins/ParameterSetWidgetPlugin.cpp
    plugins/PushButtonPlugin.cpp
    plugins/QLabelPlugin.cpp
    plugins/RadioButtonPlugin.cpp
    plugins/RotorPlugin.cpp
    plugins/SendBroadcastPlugin.cpp
    plugins/TableViewPlugin.cpp
    plugins/TableViewPluginDialog.cpp
    plugins/TableViewPluginPrivate.cpp
    plugins/TextPlugin.cpp
    plugins/TextPluginDialog.cpp
    plugins/TouchEditPlugin.cpp
    plugins/XYGraphPlugin.cpp
)

set(UI_FILES
    src/AboutDialog.ui
    src/BroadcastDialog.ui
    src/LoginDialog.ui
    src/MainWindow.ui
    src/MessageDialog.ui
    src/MessageWidget.ui
    src/ParameterDialog.ui
    src/ParameterSaveDialog.ui
    src/ReplaceDialog.ui
    src/SelectorDialog.ui
    src/SlotDialog.ui
    src/StyleDialog.ui
    src/TabDialog.ui

    plugins/DialPluginDialog.ui
    plugins/TableViewPluginDialog.ui
    plugins/TextPluginDialog.ui
)

if (UNIX)
    list(APPEND HEADERS src/SignalReceiver.h)
    list(APPEND SOURCES src/SignalReceiver.cpp)
endif()

if (MODEL_TEST)
    list(APPEND HEADERS src/modeltest.h)
    list(APPEND SOURCES src/modeltest.cpp)
endif()

add_executable(${PROJECT_NAME}
    ${HEADERS}
    ${SOURCES}
    ${UI_FILES}

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

# replacement for lupdate-pro
# just run `make lupdate` in your build dir after running cmake
add_custom_target(lupdate
    ${Qt}::lupdate -locations none ${UI_FILES} ${SOURCES} ${HEADERS}
        -ts ${TS_FILES}
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

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

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

if (USE_QT6)
    target_link_libraries(${PROJECT_NAME} PUBLIC
        Qt6::Core5Compat
    )
endif()

target_include_directories(${PROJECT_NAME} PUBLIC
    "${CMAKE_CURRENT_SOURCE_DIR}"
    "${CMAKE_CURRENT_SOURCE_DIR}/src"
    ${PYTHON_INCLUDE_DIRS}
    ${NUMPY_INCLUDE_DIR}
)

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()

set_source_files_properties(AboutDialog.cpp PROPERTIES
    COMPILE_DEFINITIONS VERSION=${VERSION}
)

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

# Install example layout
install(FILES example.tml DESTINATION share/testmanager)
