# Copyright 2010-2011,2013,2018 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

########################################################################
# Setup dependencies
########################################################################
include(GrPython)

# Note: gr-qtgui requires Qt6.

find_package(Qt6 QUIET
    COMPONENTS Core Gui Widgets
    OPTIONAL_COMPONENTS OpenGL OpenGLWidgets
)
set(QT_FOUND ${Qt6Widgets_FOUND})

gr_python_check_module(
    DESC "PyQt6"
    MODULE PyQt6
    VAR PYQT_FOUND
)
if(CONAN_TOOLCHAIN_DETECTED)
    gr_find_package(qwt GLOBAL)
else()
    gr_find_package(Qwt GLOBAL)
endif()
message(STATUS "QWT_FOUND, qwt_FOUND=${qwt_FOUND}, Qwt_FOUND=${Qwt_FOUND}")
if(qwt_FOUND OR Qwt_FOUND)
    set(QWT_FOUND TRUE)
endif()

########################################################################
# Register component
########################################################################
include(GrComponent)
if(NOT CMAKE_CROSSCOMPILING)
    set(qt_gui_python_deps PYQT_FOUND)
endif(NOT CMAKE_CROSSCOMPILING)

gr_register_component(
    NAME "gr-qtgui"
    VAR ENABLE_GR_QTGUI
    REQUIRED_TPLS
        QT_FOUND
        QWT_FOUND
        PYTHONLIBS_FOUND
        ${qt_gui_python_deps}
    REQUIRED_COMPONENTS
        gnuradio-runtime
        gr-fft
        gr-filter
)

set(GR_PKG_QTGUI_EXAMPLES_DIR ${GR_PKG_DATA_DIR}/examples/qt-gui)

########################################################################
# Begin conditional configuration
########################################################################
if(ENABLE_GR_QTGUI)
    if(Qt6OpenGL_FOUND AND Qt6OpenGLWidgets_FOUND)
          set(ENABLE_GR_QTGUI_OPENGL TRUE)
          message(STATUS "gr-qtgui: Qt6 OpenGL/OpenGLWidgets found, fosphor/rfnoc_f15 display enabled")
      else()
          set(ENABLE_GR_QTGUI_OPENGL FALSE)
          message(STATUS "gr-qtgui: Qt6 OpenGL/OpenGLWidgets not found, fosphor/rfnoc_f15 display disabled")
      endif()
    set(OpenGL_GL_PREFERENCE GLVND)
    ########################################################################
    # Add subdirectories
    ########################################################################
    add_subdirectory(include/gnuradio/qtgui)
    add_subdirectory(lib)
    add_subdirectory(docs)
    if(ENABLE_GR_ANALOG AND ENABLE_EXAMPLES)
        add_subdirectory(examples/c++)
    endif(ENABLE_GR_ANALOG AND ENABLE_EXAMPLES)
    if(ENABLE_PYTHON)
        add_subdirectory(python/qtgui)
        if(ENABLE_EXAMPLES)
            add_subdirectory(examples)
        endif(ENABLE_EXAMPLES)
        add_subdirectory(apps)
    endif(ENABLE_PYTHON)
    if(ENABLE_GRC)
        add_subdirectory(grc)
    endif(ENABLE_GRC)

    ########################################################################
    # Create Pkg Config File
    ########################################################################
    set(PC_LIBS "${QWT_LIBRARIES} ${QT_LIBRARIES}")
    string(REPLACE ";" " " PC_LIBS_STR "${PC_LIBS}")
    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/gnuradio-qtgui.pc.in
                   ${CMAKE_CURRENT_BINARY_DIR}/gnuradio-qtgui.pc @ONLY)

    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/gnuradio-qtgui.pc
            DESTINATION ${GR_LIBRARY_DIR}/pkgconfig)

    ########################################################################
    # Install the conf file
    ########################################################################
    install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/gr-qtgui.conf DESTINATION ${GR_PREFSDIR_relative_to_prefix})

    ########################################################################
    # Install the themes (QSS) files
    ########################################################################
    install(FILES
            themes/alt.qss
            themes/dark.qss
            themes/nsf.qss
            themes/plain.qss
            themes/projector.qss
            DESTINATION ${GR_THEMES_DIR})

    if(GR_BUILD_INSTALLER)
        if(WIN32)
            install(DIRECTORY ${qt_Qt6_QWindowsIntegrationPlugin_LIB_DIRS_RELEASE}
                    DESTINATION ${REL_PYTHON_INSTALL_PATH})
        elseif(APPLE)
            install(DIRECTORY ${Qt6_DIR}/plugins/platforms DESTINATION ${REL_PYTHON_INSTALL_PATH}/python.framework/)
        endif()
    endif(GR_BUILD_INSTALLER)

endif(ENABLE_GR_QTGUI)
