cmake_minimum_required(VERSION 3.16)

project(qt6-style-ukui)

# 传统兼容配置（替代 qt_standard_project_setup()）
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(QT_MIN_VERSION "6.4.2")

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_CURRENT_SOURCE_DIR}/cmake/kde-modules ${CMAKE_MODULE_PATH})

# 查找 Qt6 核心组件（包含私有组件）
find_package(Qt6 COMPONENTS Gui REQUIRED)
get_target_property(LIB_PATH Qt6::Gui IMPORTED_LOCATION)
get_filename_component(PARENT_PATH "${LIB_PATH}" PATH)
get_filename_component(PARENT_PATH "${PARENT_PATH}" PATH)
message("qt6-style-ukui PARENT_PATH: ${PARENT_PATH}")

find_package(Qt6 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS
    DBus
    Widgets
    Gui
    GuiPrivate  # 显式查找私有组件
    WidgetsPrivate
)

# ========== 自动识别 Qt6 私有头文件路径 ==========
get_target_property(QT6_GUI_LIB_PATH Qt6::Gui IMPORTED_LOCATION)
get_filename_component(QT6_LIB_DIR "${QT6_GUI_LIB_PATH}" DIRECTORY)
get_filename_component(QT6_PREFIX_DIR "${QT6_LIB_DIR}" DIRECTORY)
set(QT6_GUI_PRIVATE_BASE "${QT6_PREFIX_DIR}/include/x86_64-linux-gnu/qt6/QtGui")
file(GLOB_RECURSE QT6_GUI_PRIVATE_HEADER 
    "${QT6_GUI_PRIVATE_BASE}/*/QtGui/private/qtextengine_p.h"
)
if(QT6_GUI_PRIVATE_HEADER)
    get_filename_component(QT6_GUI_PRIVATE_DIR "${QT6_GUI_PRIVATE_HEADER}" DIRECTORY)
    include_directories(${QT6_GUI_PRIVATE_DIR})
    message(STATUS "✅ Auto found Qt6 Gui private dir: ${QT6_GUI_PRIVATE_DIR}")
else()
    set(QT6_GUI_PRIVATE_FALLBACK "${QT6_GUI_PRIVATE_BASE}/private")
    include_directories(${QT6_GUI_PRIVATE_FALLBACK})
    message(WARNING "⚠️ Auto find failed, use fallback dir: ${QT6_GUI_PRIVATE_FALLBACK}")
endif()
# ========== 自动识别结束 ==========

include_directories(${Qt6Widgets_PRIVATE_INCLUDE_DIRS})
include_directories(${Qt6Gui_PRIVATE_INCLUDE_DIRS})

# 查找 gsettings-qt
find_package(PkgConfig REQUIRED)
pkg_check_modules(GSETTINGS_QT REQUIRED gsettings-qt6)

# 头文件目录
include_directories(
    ../libqt6-ukui-style
    ${GSETTINGS_QT_INCLUDE_DIRS}
)

# 条件依赖处理
if (KYSDKCONF2_FOUND)
    include_directories(${KYSDKCONF2_INCLUDE_DIRS})
    link_directories(${KYSDKCONF2_LIBRARY_DIRS})
endif()
if (UKUIWINDOWHELPER_FOUND)
    include_directories(${UKUIWINDOWHELPER_INCLUDE_DIRS})
    link_directories(${UKUIWINDOWHELPER_LIBRARY_DIRS})
endif()

# 源文件列表（保持不变）
set(Header
    platformthemedebug.h
    readconfig.h
    ukui-style-plugin.h
    qt5-config-style-ukui/config-style-helper.h
    qt5-config-style-ukui/ukui-config-style.h
    qt5-config-style-ukui/ukui-config-style-parameters.h
    qt5-config-style-ukui/animations/config-box-animation-helper.h
    qt5-config-style-ukui/animations/config-box-animator.h
    qt5-config-style-ukui/animations/config-button-animation-helper.h
    qt5-config-style-ukui/animations/config-button-animator.h
    qt5-config-style-ukui/animations/config-checkbox-animation-helper.h
    qt5-config-style-ukui/animations/config-checkbox-animator.h
    qt5-config-style-ukui/animations/config-progressbar-animation.h
    qt5-config-style-ukui/animations/config-progressbar-animation-helper.h
    qt5-config-style-ukui/animations/config-radiobutton-animation-helper.h
    qt5-config-style-ukui/animations/config-radiobutton-animator.h
    qt5-config-style-ukui/animations/config-scrollbar-animation-helper.h
    qt5-config-style-ukui/animations/config-slider-animation-helper.h
    qt5-config-style-ukui/animations/config-slider-animator.h
    qt5-config-style-ukui/animations/config-tab-widget-animation-helper.h
    qt5-config-style-ukui/animations/config-tree-animation-helper.h
    qt5-config-style-ukui/animations/config-tree-animator.h
    qt5-config-style-ukui/widget-parameters/config-checkbox-parameters.h
    qt5-config-style-ukui/widget-parameters/config-combobox-parameters.h
    qt5-config-style-ukui/widget-parameters/config-lineedit-parameters.h
    qt5-config-style-ukui/widget-parameters/config-list-parameters.h
    qt5-config-style-ukui/widget-parameters/config-menu-parameters.h
    qt5-config-style-ukui/widget-parameters/config-progressbar-parameters.h
    qt5-config-style-ukui/widget-parameters/config-pushbutton-parameters.h
    qt5-config-style-ukui/widget-parameters/config-radiobutton-parameters.h
    qt5-config-style-ukui/widget-parameters/config-scrollbar-parameters.h
    qt5-config-style-ukui/widget-parameters/config-slider-parameters.h
    qt5-config-style-ukui/widget-parameters/config-spinbox-parameters.h
    qt5-config-style-ukui/widget-parameters/config-table-parameters.h
    qt5-config-style-ukui/widget-parameters/config-tabwidget-parameters.h
    qt5-config-style-ukui/widget-parameters/config-toolbutton-parameters.h
    qt5-config-style-ukui/widget-parameters/config-tooltip-parameters.h
    qt5-config-style-ukui/widget-parameters/config-tree-parameters.h
    qt5-config-style-ukui/widget-parameters/control-parameters.h
)

set(Src
    readconfig.cpp
    ukui-style-plugin.cpp
    qt5-config-style-ukui/config-view-helper.cpp
    qt5-config-style-ukui/config-style-helper.cpp
    qt5-config-style-ukui/ukui-config-style.cpp
    qt5-config-style-ukui/ukui-config-style-parameters.cpp
    qt5-config-style-ukui/animations/config-box-animation-helper.cpp
    qt5-config-style-ukui/animations/config-box-animator.cpp
    qt5-config-style-ukui/animations/config-button-animation-helper.cpp
    qt5-config-style-ukui/animations/config-button-animator.cpp
    qt5-config-style-ukui/animations/config-checkbox-animation-helper.cpp
    qt5-config-style-ukui/animations/config-checkbox-animator.cpp
    qt5-config-style-ukui/animations/config-progressbar-animation.cpp
    qt5-config-style-ukui/animations/config-progressbar-animation-helper.cpp
    qt5-config-style-ukui/animations/config-radiobutton-animation-helper.cpp
    qt5-config-style-ukui/animations/config-radiobutton-animator.cpp
    qt5-config-style-ukui/animations/config-scrollbar-animation-helper.cpp
    qt5-config-style-ukui/animations/config-slider-animation-helper.cpp
    qt5-config-style-ukui/animations/config-slider-animator.cpp
    qt5-config-style-ukui/animations/config-tab-widget-animation-helper.cpp
    qt5-config-style-ukui/animations/config-tree-animation-helper.cpp
    qt5-config-style-ukui/animations/config-tree-animator.cpp
    qt5-config-style-ukui/widget-parameters/config-checkbox-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-combobox-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-lineedit-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-list-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-menu-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-progressbar-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-pushbutton-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-radiobutton-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-scrollbar-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-slider-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-spinbox-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-table-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-tabwidget-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-toolbutton-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-tooltip-parameters.cpp
    qt5-config-style-ukui/widget-parameters/config-tree-parameters.cpp
    qt5-config-style-ukui/widget-parameters/control-parameters.cpp
)

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${Header} ${Src})

add_library(${PROJECT_NAME} MODULE ${Header} ${Src})
target_include_directories(${PROJECT_NAME} PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/qt5-config-style-ukui/animations
    ${CMAKE_CURRENT_SOURCE_DIR}/qt5-config-style-ukui/widget-parameters
    ${GSETTINGS_QT_INCLUDE_DIRS}
    ${Qt6Widgets_PRIVATE_INCLUDE_DIRS}
    ${Qt6Gui_PRIVATE_INCLUDE_DIRS}
    ${QT6_GUI_PRIVATE_DIR}  # 自动识别的私有路径
)

target_link_libraries(${PROJECT_NAME} PRIVATE
    Qt6::Widgets
    Qt6::DBus
    Qt6::Gui
    Qt6::GuiPrivate
    Qt6::WidgetsPrivate
    ${GSETTINGS_QT_LIBRARIES}
    qt6-ukui-style
)

target_compile_definitions(${PROJECT_NAME} PRIVATE
    -DQT5UKUIPLATFORMTHEME_LIBRARY
    -DQT_DEPRECATED_WARNINGS
    -DQT_MESSAGELOGCONTEXT
    -DQT_DISABLE_DEPRECATED_BEFORE=0x060000
)

if(UNIX)
    set(TARGET_PATH "/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}/qt6/plugins/styles")
    MESSAGE("libukui-styles TARGET_PATH: ${TARGET_PATH}")
    install(TARGETS ${PROJECT_NAME} DESTINATION ${TARGET_PATH})
endif()

