#-----------------------------------------------------------------------------
#
# Copyright (C) 2022 Bjarne von Horn <vh@igh.de>
#
# This file is part of the Qt6QmlPdWidgets library.
#
# The Qt6QmlPdWidgets library is free software: you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# The Qt6QmlPdWidgets library 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 Lesser
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with the Qt6QmlPdWidgets Library. If not, see
# <http://www.gnu.org/licenses/>.
#
#-----------------------------------------------------------------------------

cmake_minimum_required(VERSION 3.19)

project(Qml6Test)
include(GNUInstallDirs)
option(USE_STATIC_QMLWIDGETS "Link in Qt6QmlPdWidgets statically" OFF)

# load Qt
find_package(Qt6 REQUIRED
    COMPONENTS Charts Gui Quick QuickControls2
)

qt_standard_project_setup()

# compile qml files (of this project) if compiler available

set(SOURCES
    src/ScalarSeries.h
    src/ScalarSeries.cpp
    src/main.cpp
)

find_package(Qt6PdCom1 1.4 REQUIRED COMPONENTS Qml)
if (NOT TARGET EtherLab::Qt6QmlPdWidgets1)
    find_package(Qt6QmlPdWidgets1 REQUIRED)
endif()

get_target_property(qtpdcom_target_type EtherLab::Qt6PdCom1 TYPE)

# fetch Path where pd qml widgets were installed/built
if (NOT EXTRA_QML_IMPORT_PATH)
    set(EXTRA_QML_IMPORT_PATH ${PD_QML_ROOT_PATH})
endif()

message(STATUS "QML import path: ${EXTRA_QML_IMPORT_PATH}")

qt_add_executable(${PROJECT_NAME} ${SOURCES})

qt_add_qml_module(${PROJECT_NAME}
    URI QmlTest
    VERSION 1.0
    IMPORTS
        de.igh.pd
        de.igh.pd.style
        QtCharts
    IMPORT_PATH "${QTPDCOM_QML_ROOT_PATH};${EXTRA_QML_IMPORT_PATH};${CMAKE_CURRENT_BINARY_DIR}/qml_out"
    RESOURCE_PREFIX "/"
    OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/qml_out/QmlTest"
    NO_RESOURCE_TARGET_PATH
    QML_FILES
        qml/ChartsPane.qml
        qml/ChannelPane.qml
        qml/ConnectionDialog.qml
        qml/main.qml
        qml/MessageTableView.qml
        qml/MessagesPane.qml
        qml/ParameterPane.qml
        qml/SvgPane.qml
    RESOURCES
        images/book-solid.svg
        images/usb.svg
        images/cog.svg
        images/switch-off.svg
        images/sign-out.svg
        images/canbus.svg
        images/arrow-left.svg
        images/chart-area.svg
        images/trash-alt.svg
        images/plug.svg
        images/sync-alt.svg
        images/bolt.svg
        images/user-check.svg
        images/cogs.svg
        images/sign-in.svg
        images/arrow-down.svg
        images/wrench.svg
        images/ambulance.svg
        images/folder-open.svg
        images/save.svg
        images/arrow-up.svg
        images/svg_example.svg
        images/power-off.svg
        images/play-circle.svg
        images/arrow-right.svg
        images/user-lock.svg
        images/edit.svg
)

# WIN32_EXECUTABLE controls whether the CMD window is hidden
set_target_properties(${PROJECT_NAME} PROPERTIES
    AUTOUIC 0
    AUTOMOC 1
    AUTORCC 1
    WIN32_EXECUTABLE true
)

# link in Qt, the PdWidgets and the PdWidgets QML files

target_link_libraries(${PROJECT_NAME} PUBLIC
    EtherLab::Qt6PdCom1
    Qt6::Charts
    Qt6::Quick
    Qt6::QuickControls2
)

if (USE_STATIC_QMLWIDGETS)
    target_link_libraries(${PROJECT_NAME} PRIVATE
        EtherLab::Qt6QmlPdWidgets1plugin
        EtherLab::Qt6QmlPdWidgets1_styleplugin
    )
    target_compile_definitions(${PROJECT_NAME} PRIVATE STATIC_QMLWIDGETS)
endif()
if (qtpdcom_target_type STREQUAL STATIC_LIBRARY)
    target_link_libraries(${PROJECT_NAME} PRIVATE
        EtherLab::Qt6PdCom1plugin
    )
    target_compile_definitions(${PROJECT_NAME} PRIVATE STATIC_QTPDCOM)
endif()

target_include_directories(${PROJECT_NAME} PUBLIC
    "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>"
    "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>"
)

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