if (NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12)
endif ()

PROJECT(QtTesting)

IF(NOT DEFINED QtTesting_QT_VERSION)
  SET(QtTesting_QT_VERSION "4" CACHE STRING "Expected Qt version")
  MARK_AS_ADVANCED(QtTesting_QT_VERSION)
  SET_PROPERTY(CACHE QtTesting_QT_VERSION PROPERTY STRINGS 4 5)
ENDIF()
IF(NOT (QtTesting_QT_VERSION VERSION_EQUAL "4" OR
  QtTesting_QT_VERSION VERSION_EQUAL "5"))
  message(FATAL_ERROR "Expected value for QtTesting_QT_VERSION is either '4' or '5'")
ENDIF()

set(qt_imported_targets)
IF(QtTesting_QT_VERSION VERSION_GREATER "4")
  FIND_PACKAGE(Qt5 REQUIRED COMPONENTS Core Widgets)
  SET(qt_imported_targets Qt5::Core Qt5::Widgets)
ELSE()
  FIND_PACKAGE(Qt4 REQUIRED COMPONENTS QtGui)
  SET(qt_imported_targets Qt4::QtCore Qt4::QtGui)
ENDIF()

IF(NOT DEFINED QT_TESTING_WITH_PYTHON)
  OPTION(QT_TESTING_WITH_PYTHON "Enable Qt Testing with Python" OFF)
ENDIF()

IF(NOT DEFINED QtTesting_INSTALL_BIN_DIR)
  SET(QtTesting_INSTALL_BIN_DIR bin)
ENDIF()

IF(NOT DEFINED QtTesting_INSTALL_INCLUDE_DIR)
  SET(QtTesting_INSTALL_INCLUDE_DIR include/QtTesting)
ENDIF()

IF(NOT DEFINED QtTesting_INSTALL_LIB_DIR)
  SET(QtTesting_INSTALL_LIB_DIR lib)
ENDIF()

IF(NOT DEFINED QtTesting_INSTALL_CMAKE_DIR)
  SET(QtTesting_INSTALL_CMAKE_DIR lib/cmake/qttesting)
ENDIF()

IF(NOT DEFINED QT_TESTING_EVENT_PLAYBACK_DELAY)
  SET(QT_TESTING_EVENT_PLAYBACK_DELAY "100" CACHE STRING "Delay between invocation of each testing event." FORCE)
  MARK_AS_ADVANCED(QT_TESTING_EVENT_PLAYBACK_DELAY)
ENDIF()

IF(NOT DEFINED QT_TESTING_INSTALL_EXPORT_NAME)
  SET(QT_TESTING_INSTALL_EXPORT_NAME QtTestingTargets)
ENDIF()

# One can define QT_TESTING_CUSTOM_LIBRARY_PREFIX and/or
# QT_TESTING_CUSTOM_LIBRARY_SUFFIX to add prefix/suffix to libraries
# generated by thus project. Default is empty.
if(NOT DEFINED QT_TESTING_CUSTOM_LIBRARY_SUFFIX)
  set(QT_TESTING_CUSTOM_LIBRARY_SUFFIX)
endif()
if(NOT DEFINED QT_TESTING_CUSTOM_LIBRARY_PREFIX)
  set(QT_TESTING_CUSTOM_LIBRARY_PREFIX)
endif()

IF(QT_TESTING_WITH_PYTHON)

  IF(NOT PythonLibs_FOUND)
    FIND_PACKAGE(PythonLibs REQUIRED)
  ENDIF()

  IF(UNIX)
    FIND_LIBRARY(PYTHON_UTIL_LIBRARY
      NAMES util
      PATHS /usr/lib
      DOC "Utility library needed for vtkpython"
      )
    MARK_AS_ADVANCED(PYTHON_UTIL_LIBRARY)
    IF(PYTHON_UTIL_LIBRARY)
      SET(PYTHON_UTIL_LIBRARY_LIB ${PYTHON_UTIL_LIBRARY})
    ENDIF()
  ENDIF()

  INCLUDE_DIRECTORIES(
    ${PYTHON_INCLUDE_PATH}
  )
  SET(PYTHON_SRCS
    pqPythonEventObserver.cxx
    pqPythonEventObserver.h
    pqPythonEventSource.cxx
    pqPythonEventSource.h
  )
ENDIF(QT_TESTING_WITH_PYTHON)

set(ui_files
  pqPlayBackEventsDialog.ui
  pqRecordEventsDialog.ui)
set(rc_files
  Resources/QtTesting.qrc)

set(CMAKE_AUTOMOC 1)
set(CMAKE_AUTOUIC 1)
set(CMAKE_AUTORCC 1)

SET(QtTesting_SOURCES
  pq3DViewEventPlayer.cxx
  pq3DViewEventTranslator.cxx
  pqAbstractActivateEventPlayer.cxx
  pqAbstractBooleanEventPlayer.cxx
  pqAbstractButtonEventTranslator.cxx
  pqAbstractDoubleEventPlayer.cxx
  pqAbstractIntEventPlayer.cxx
  pqAbstractItemViewEventPlayer.cxx
  pqAbstractItemViewEventPlayerBase.cxx
  pqAbstractItemViewEventTranslator.cxx
  pqAbstractItemViewEventTranslatorBase.cxx
  pqAbstractMiscellaneousEventPlayer.cxx
  pqAbstractSliderEventTranslator.cxx
  pqAbstractStringEventPlayer.cxx
  pqBasicWidgetEventPlayer.cxx
  pqBasicWidgetEventTranslator.cxx
  pqCheckEventOverlay.cxx
  pqComboBoxEventTranslator.cxx
  pqComboBoxEventPlayer.cxx
  pqCommentEventPlayer.cxx
  pqDoubleSpinBoxEventTranslator.cxx
  pqEventComment.cxx
  pqEventDispatcher.cxx
  pqEventObserver.cxx
  pqEventPlayer.cxx
  pqEventRecorder.cxx
  pqEventTranslator.cxx
  pqLineEditEventTranslator.cxx
  pqListViewEventPlayer.cxx
  pqListViewEventTranslator.cxx
  pqMenuEventTranslator.cxx
  pqNativeFileDialogEventPlayer.cxx
  pqNativeFileDialogEventTranslator.cxx
  pqObjectNaming.cxx
  pqPlayBackEventsDialog.cxx
  pqRecordEventsDialog.cxx
  pqSpinBoxEventTranslator.cxx
  pqStdoutEventObserver.cxx
  pqTabBarEventPlayer.cxx
  pqTabBarEventTranslator.cxx
  pqTableViewEventPlayer.cxx
  pqTableViewEventTranslator.cxx
  pqTestUtility.cxx
  pqThreadedEventSource.cxx
  pqTimer.cxx
  pqTreeViewEventPlayer.cxx
  pqTreeViewEventTranslator.cxx
  pqWidgetEventPlayer.cxx
  pqWidgetEventTranslator.cxx
)

SET(QtTesting_DEVEL_HEADERS
  QtTestingExport.h
  pq3DViewEventPlayer.h
  pq3DViewEventTranslator.h
  pqAbstractActivateEventPlayer.h
  pqAbstractBooleanEventPlayer.h
  pqAbstractButtonEventTranslator.h
  pqAbstractDoubleEventPlayer.h
  pqAbstractIntEventPlayer.h
  pqAbstractItemViewEventPlayer.h
  pqAbstractItemViewEventPlayerBase.h
  pqAbstractItemViewEventTranslator.h
  pqAbstractItemViewEventTranslatorBase.h
  pqAbstractMiscellaneousEventPlayer.h
  pqAbstractSliderEventTranslator.h
  pqAbstractStringEventPlayer.h
  pqBasicWidgetEventPlayer.h
  pqBasicWidgetEventTranslator.h
  pqCheckEventOverlay.h
  pqComboBoxEventTranslator.h
  pqComboBoxEventPlayer.h
  pqCommentEventPlayer.h
  pqDoubleSpinBoxEventTranslator.h
  pqEventComment.h
  pqEventDispatcher.h
  pqEventObserver.h
  pqEventPlayer.h
  pqEventRecorder.h
  pqEventSource.h
  pqEventTranslator.h
  pqEventTypes.h
  pqLineEditEventTranslator.h
  pqListViewEventPlayer.h
  pqListViewEventTranslator.h
  pqMenuEventTranslator.h
  pqNativeFileDialogEventPlayer.h
  pqNativeFileDialogEventTranslator.h
  pqObjectNaming.h
  pqPlayBackEventsDialog.h
  pqRecordEventsDialog.h
  pqSpinBoxEventTranslator.h
  pqStdoutEventObserver.h
  pqTabBarEventPlayer.h
  pqTabBarEventTranslator.h
  pqTableViewEventPlayer.h
  pqTableViewEventTranslator.h
  pqTestUtility.h
  pqThreadedEventSource.h
  pqTimer.h
  pqTreeViewEventPlayer.h
  pqTreeViewEventTranslator.h
  pqWidgetEventPlayer.h
  pqWidgetEventTranslator.h
  ${QtTesting_BINARY_DIR}/QtTestingConfigure.h
)

vtk_module_add_module(ParaView::qttesting
  SOURCES
    ${QtTesting_SOURCES}
    ${ui_files}
    ${rc_files}
    ${PYTHON_SRCS}
  HEADERS
    ${QtTesting_DEVEL_HEADERS}
  HEADERS_SUBDIR
    "vtkqttesting"
)
set_target_properties(qttesting
  PROPERTIES
    DEFINE_SYMBOL QtTesting_EXPORTS)
add_library(ParaView::qttesting ALIAS qttesting)
target_include_directories(qttesting
  PUBLIC
    "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
    "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
    "$<INSTALL_INTERFACE:${_vtk_build_HEADERS_DESTINATION}/vtkqttesting>")

# Set library name to include custom prefixes/suffixes.
set_property(TARGET qttesting
  PROPERTY OUTPUT_NAME ${QT_TESTING_CUSTOM_LIBRARY_PREFIX}QtTesting${QT_TESTING_CUSTOM_LIBRARY_SUFFIX})

TARGET_LINK_LIBRARIES(qttesting
  ${qt_imported_targets}
)

IF(QT_TESTING_WITH_PYTHON)
  TARGET_LINK_LIBRARIES(qttesting
    VTK::Python
  )
ENDIF()

SET(QTTESTING_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
CONFIGURE_FILE(${QtTesting_SOURCE_DIR}/QtTestingConfigure.h.in
               ${QtTesting_BINARY_DIR}/QtTestingConfigure.h)

if (FALSE)
INSTALL(TARGETS qttesting
  EXPORT ${QT_TESTING_INSTALL_EXPORT_NAME}
  RUNTIME DESTINATION ${QtTesting_INSTALL_BIN_DIR} COMPONENT Runtime
  LIBRARY DESTINATION ${QtTesting_INSTALL_LIB_DIR} COMPONENT Runtime
  ARCHIVE DESTINATION ${QtTesting_INSTALL_LIB_DIR} COMPONENT Development)
endif ()


if (NOT DEFINED BUILD_EXAMPLES)
  option(BUILD_EXAMPLES "Build examples" OFF)
endif ()
if (BUILD_EXAMPLES)
  add_subdirectory(Examples)
endif ()

include(CTest)
IF(BUILD_TESTING)
  add_subdirectory(Testing)
ENDIF()

export(TARGETS qttesting FILE ${QtTesting_BINARY_DIR}/QtTestingExports.cmake)

# Set up the build export configuration
set(QtTesting_EXPORT_FILE "${QtTesting_BINARY_DIR}/QtTestingConfig.cmake")
configure_file(
  "${QtTesting_SOURCE_DIR}/QtTestingConfig.cmake.in"
  "${QtTesting_EXPORT_FILE}"
  @ONLY
)

# Set up the install export
IF(IS_ABSOLUTE QtTesting_INSTALL_INCLUDE_DIR)
  set(QtTesting_INSTALL_INCLUDE_FULL_DIR "${QtTesting_INSTALL_INCLUDE_DIR}")
ELSE()
  set(QtTesting_INSTALL_INCLUDE_FULL_DIR "${CMAKE_INSTALL_PREFIX}/${QtTesting_INSTALL_INCLUDE_DIR}")
  get_filename_component(QtTesting_INSTALL_INCLUDE_FULL_DIR "${QtTesting_INSTALL_INCLUDE_FULL_DIR}" ABSOLUTE)
ENDIF()

IF(IS_ABSOLUTE QtTesting_INSTALL_LIB_DIR)
  set(QtTesting_INSTALL_LIB_FULL_DIR "${QtTesting_INSTALL_LIB_DIR}")
ELSE()
  set(QtTesting_INSTALL_LIB_FULL_DIR "${CMAKE_INSTALL_PREFIX}/${QtTesting_INSTALL_LIB_DIR}")
  get_filename_component(QtTesting_INSTALL_LIB_FULL_DIR "${QtTesting_INSTALL_LIB_FULL_DIR}" ABSOLUTE)
ENDIF()

set(QtTesting_EXPORT_INSTALL_FILE "${QtTesting_BINARY_DIR}/CMakeFiles/QtTestingConfig.cmake")
configure_file(
  "${QtTesting_SOURCE_DIR}/QtTestingConfig-install.cmake.in"
  "${QtTesting_EXPORT_INSTALL_FILE}"
  @ONLY
)

IF(NOT QtTesting_INSTALL_NO_DEVELOPMENT)
  install( FILES ${QtTesting_DEVEL_HEADERS}
    DESTINATION ${QtTesting_INSTALL_INCLUDE_DIR}
  )

  # Configure the CMake EXPORT file during installation
  install( FILES ${QtTesting_BINARY_DIR}/CMakeFiles/QtTestingConfig.cmake
    DESTINATION ${QtTesting_INSTALL_CMAKE_DIR}
  )
  install( EXPORT ${QT_TESTING_INSTALL_EXPORT_NAME}
    DESTINATION ${QtTesting_INSTALL_CMAKE_DIR}
  )
ENDIF()
