cmake_minimum_required(VERSION 3.5)
cmake_policy(SET CMP0079 NEW)
if(POLICY CMP0079)
    cmake_policy(GET CMP0079 policy_status)
    message(STATUS "CMP0079 策略状态: ${policy_status}")
else()
    message(STATUS "CMP0079 策略不可用")
endif()

project(DiagramDesigner LANGUAGES CXX VERSION 1.0)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Gui Widgets Sql Xml REQUIRED Charts)
find_package(Qt6 REQUIRED COMPONENTS SvgWidgets)
find_package(Qt6 COMPONENTS Network WebSockets REQUIRED)
find_package(PostgreSQL  REQUIRED)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
# 默认 ui 文件要和 .h 头文件在一个目录，若不在一个目录，需要指定其所在目录
set(CMAKE_AUTOUIC_SEARCH_PATHS "ui")

if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")
    set(dd_PlatformDir "x86")
else()
    if(DEFINED CMAKE_SYSTEM_PROCESSOR)
        if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
            set(dd_PlatformDir "x64")
        elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|AARCH64")
            set(dd_PlatformDir "aarch64")
        elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|ARM64")
               set(dd_PlatformDir "arm64")
        else()
            set(dd_PlatformDir "x64")
        endif()
    else()
        set(dd_PlatformDir "x64")
    endif()
endif()

set(H_HEADER_FILES
    include/mainwindow.h
    include/graphicElementsPanel.h
    include/electricElementsPanel.h
    include/electricElementsBox.h
    include/electricElementsListwidget.h
    include/operationCommand.h
    include/toolPage.h
    include/toolBox.h
    include/projectTableDelegate.h
    include/selectorDialog.h
    include/topologyView.h
    include/diagramView.h
    include/topologyTree.h
    include/monitorItemsDlg.h
    include/monitorPagesDlg.h
    include/baseDockWidget.h
    include/toolBarConfig.h
    include/enhancedToolBar.h
    include/configToolBar.h
    include/draggableToolButton.h
    include/runtimeDialog.h

    common/include/tools.h
    common/include/httpInterface.h
    common/include/baseProperty.h
    common/include/compiler.hpp
    common/include/export.hpp
    common/include/operatingSystem.hpp
    common/include/structDataSource.h
    common/include/extraPropertyManager.h

    common/include/pluginCommon/iCanvasItem.h
    common/include/pluginCommon/iPlugin.h
    common/include/pluginCommon/iShapeFactory.h

    common/core_model/types.h
    common/core_model/topology.h
    common/core_model/diagram.h
    common/backend/project_model.h
    common/backend/meta_model.h
)
set(CPP_SOURCE_FILES
    source/main.cpp
    source/mainwindow.cpp
    source/graphicElementsPanel.cpp
    source/electricElementsPanel.cpp
    source/electricElementsBox.cpp
    source/electricElementsListwidget.cpp
    source/operationCommand.cpp
    source/toolPage.cpp
    source/toolBox.cpp
    source/projectTableDelegate.cpp
    source/selectorDialog.cpp
    source/topologyView.cpp
    source/diagramView.cpp
    source/topologyTree.cpp
    source/monitorItemsDlg.cpp
    source/monitorPagesDlg.cpp
    source/baseDockWidget.cpp
    source/toolBarConfig.cpp
    source/enhancedToolBar.cpp
    source/configToolBar.cpp
    source/draggableToolButton.cpp
    source/runtimeDialog.cpp

    common/source/httpInterface.cpp
    common/source/tools.cpp
    common/source/baseProperty.cpp
    common/source/structDataSource.cpp
    common/source/extraPropertyManager.cpp

    common/core_model/types.cpp

    common/source/iPlugin.cpp
)
set(UI_FILES
    ui/mainwindow.ui
    ui/graphicElementsPanel.ui
    ui/topologyView.ui
    ui/diagramView.ui
    ui/monitorItemsDlg.ui
    ui/monitorPagesDlg.ui
)

# 包含源文件目录
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR})

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(DiagramDesigner
        MANUAL_FINALIZATION
        ${H_HEADER_FILES}
        ${CPP_SOURCE_FILES}
        ${UI_FILES}
        resource/DiagramDesigner.qrc
    )
else()
    if(ANDROID)
        add_library(DiagramDesigner SHARED
            ${H_HEADER_FILES}
            ${CPP_SOURCE_FILES}
            ${UI_FILES}
            resource/DiagramDesigner.qrc
        )
    else()
        add_executable(DiagramDesigner WIN32
            ${H_HEADER_FILES}
            ${CPP_SOURCE_FILES}
            ${UI_FILES}
            resource/DiagramDesigner.qrc
        )
    endif()
endif()

include_directories(include)
include_directories(common/include)
include_directories(${PostgreSQL_INCLUDE_DIRS})

target_include_directories(DiagramDesigner PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_include_directories(DiagramDesigner PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/common")
target_include_directories(DiagramDesigner PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/PropertyEditor/source/include)")
target_include_directories(DiagramDesigner PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/pluginManager/include)")
target_link_libraries(DiagramDesigner PUBLIC Qt${QT_VERSION_MAJOR}::Core
                                                  Qt${QT_VERSION_MAJOR}::Gui 
                                                  Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(DiagramDesigner PRIVATE Qt6::SvgWidgets)
target_link_libraries(DiagramDesigner PRIVATE Qt6::Xml)
target_link_libraries(DiagramDesigner PRIVATE Qt6::Network)
target_link_libraries(DiagramDesigner PRIVATE Qt6::Sql ${PostgreSQL_LIBRARIES})
message(STATUS "POSTGRESQL_LIBRARIES: ${PostgreSQL_LIBRARIES}")
set_target_properties(DiagramDesigner PROPERTIES
    AUTOMOC ON
    AUTORCC ON
    AUTOUIC ON
    CXX_STANDARD 14
    CXX_STANDARD_REQUIRED ON
    CXX_EXTENSIONS OFF
    VERSION 1.0
    EXPORT_NAME "DiagramDesigner with Qt Advanced Docking System"
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${dd_PlatformDir}/lib"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${dd_PlatformDir}/lib"
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${dd_PlatformDir}/bin"
)

add_subdirectory(PropertyEditor)
add_subdirectory(diagramCavas)
add_subdirectory(diagramUtils)
add_subdirectory(diagramCommunication)
add_subdirectory(pluginManager)

target_link_libraries(DiagramDesigner PUBLIC PropertyEditor)
target_link_libraries(DiagramDesigner PUBLIC diagramCavas diagramUtils diagramCommunication pluginManager)

file(COPY setting.xml DESTINATION "${CMAKE_BINARY_DIR}/${dd_PlatformDir}/bin")

add_subdirectory(customTypePlugin)
