# bindings/qt_gui/CMakeLists.txt
### Process this file with cmake to produce Makefile
###
# Copyright (C) 2006-2015 Alan W. Irwin
#
# This file is part of PLplot.
#
# PLplot is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; version 2 of the License.
#
# PLplot 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with PLplot; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

if(ENABLE_qt)
  # Set up Qt4-based build environment.
  set(qt_SOURCE plqt.cpp)
  include_directories(
    ${CMAKE_SOURCE_DIR}/include
    ${CMAKE_SOURCE_DIR}/lib/qsastime
    ${CMAKE_SOURCE_DIR}/lib/nistcd
    ${CMAKE_SOURCE_DIR}/drivers
    ${CMAKE_BINARY_DIR}
    ${CMAKE_BINARY_DIR}/include
    )

  if(BUILD_SHARED_LIBS)
    set_source_files_properties(
      ${qt_SOURCE}
      PROPERTIES COMPILE_FLAGS "-DUSINGDLL"
      )
  endif(BUILD_SHARED_LIBS)

  if(ANY_QT_DEVICE)
    add_library(plplotqt ${qt_SOURCE})
    add_dependencies(plplotqt moc_outfile_generated)
  else(ANY_QT_DEVICE)
    message(FATAL_ERROR "Internal build system inconsistency between ENABLE_qt true and ANY_QT_DEVICE false.")
  endif(ANY_QT_DEVICE)

  if(PLPLOT_USE_QT5)
    # According to advice from Steve Kelly on the Cmake list, the Qt5
    # Gui component is a dependency of the Qt5 Svg component so is not
    # needed here, but I will leave it in since it is informative.
    qt5_use_modules(plplotqt Svg Gui PrintSupport)

    target_link_libraries(
      plplotqt
      plplot
      ${MATH_LIB}
      )
  else(PLPLOT_USE_QT5)
    if(NOT QT_LIBRARIES)
      message(FATAL_ERROR "PLplot build-system inconsistency in bindings/qt_gui subdirectory.")
    endif(NOT QT_LIBRARIES)
    target_link_libraries(
      plplotqt
      plplot
      ${MATH_LIB}
      ${QT_LIBRARIES}
      )
    # Update the target COMPILE_DEFINITIONS and INCLUDE_DIRECTORIES
    set_qt_target_properties(plplotqt)
  endif(PLPLOT_USE_QT5)

  if(NON_TRANSITIVE)
    # Note that there are implicit references to classes in QtCore, QtGui and QtSvg 
    # so we need to add all three to the list of libraries to link to plplotqtd.
    # This is just equivalent to adding QT_LIBRARIES.
    # 
    # empty list ==> non-transitive linking for everything that links to
    # libplplotqtd in the shared libraries case.
    target_link_libraries(plplotqt LINK_INTERFACE_LIBRARIES ${QT_LIBRARIES})
  endif(NON_TRANSITIVE)
  # qt_RPATH set in cmake/modules/qt.cmake. This is only used for the 
  # install-tree rpath since cmake handles the build-tree rpath
  # automatically (so long as full pathnames to libraries are used).
  #message("qt_RPATH = ${qt_RPATH}")
  if(USE_RPATH)
    # Order is important here because of /usr/lib concerns.
    set(LIB_INSTALL_RPATH 
      ${LIB_DIR}
      ${qt_RPATH}
      ${libplplot_RPATH}
      )
    #message("LIB_INSTALL_RPATH = ${LIB_INSTALL_RPATH}")
    set_target_properties(plplotqt
      PROPERTIES
      SOVERSION ${plplotqt_SOVERSION}
      VERSION ${plplotqt_VERSION}
      INSTALL_RPATH "${LIB_INSTALL_RPATH}"
      INSTALL_NAME_DIR "${LIB_DIR}"
      )
  else(USE_RPATH)
    set_target_properties(plplotqt
      PROPERTIES
      SOVERSION ${plplotqt_SOVERSION}
      VERSION ${plplotqt_VERSION}
      INSTALL_NAME_DIR "${LIB_DIR}"
      )
  endif(USE_RPATH)
  install(TARGETS plplotqt
    EXPORT export_plplot
    ARCHIVE DESTINATION ${LIB_DIR}
    LIBRARY DESTINATION ${LIB_DIR}
    RUNTIME DESTINATION ${BIN_DIR}
    )

  if(PLPLOT_USE_QT5)
    # For this special case QT_LIBRARIES is not populated so must
    # use this alternative method to add Qt(5) libraries to the pkg-config file
    # for the plplotqt library.
    set(
      libplplotqt_LINK_FLAGS
      "${Qt5_library_LINK_FLAGS}"
      )
  else(PLPLOT_USE_QT5)
    set(
      libplplotqt_LINK_FLAGS
      "${QT_LIBRARIES}"
      )
  endif(PLPLOT_USE_QT5)

  # Configure pkg-config *.pc file corresponding to the compile and link
  # flags needed by a Qt GUI executable using extqt to gain access to PLplot.
  pkg_config_file("qt" "Qt" " Qt binding" "plplotqt" "${pc_qt_COMPILE_FLAGS}" "${libplplotqt_LINK_FLAGS}")

  add_subdirectory(pyqt4)
  add_subdirectory(smoke)
endif(ENABLE_qt)
