cmake_minimum_required(VERSION 3.5)

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)
find_package(Qt6 REQUIRED COMPONENTS SvgWidgets)
find_package(Qt6 COMPONENTS Network 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")
        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/loadPageDlg.h

    common/include/global.h
    common/include/dataBase.h
    common/include/httpInterface.h
    common/include/compiler.hpp
    common/include/export.hpp
    common/include/operatingSystem.hpp

)
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/loadPageDlg.cpp

    common/source/dataBase.cpp
    common/source/httpInterface.cpp
)
set(UI_FILES
    ui/mainwindow.ui
    ui/graphicElementsPanel.ui
    ui/loadPageDlg.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_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})
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"
)

target_link_libraries(DiagramDesigner PRIVATE diagramCavas)
add_subdirectory(diagramCavas)

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