#------------------------------------------------------------------------------
# Declare source files for modelbuilder

include(${PARAVIEW_USE_FILE})

# Headers that are not Qt subclasses
set(modelbuilder_HDRS
  mbMenuBuilder.h
)

# Headers that are Qt subclasses (and thus need moc run on them)
set(modelbuilder_MOC_HDRS
  mbMainWindow.h
  mbTestEventPlayer.h
  mbTestEventTranslator.h
)

# Implementation files.
set(modelbuilder_SRCS
  mbMainWindow.cxx
  mbMenuBuilder.cxx
  mbTestEventPlayer.cxx
  mbTestEventTranslator.cxx
)

# A feature that exposes ParaView's main window events (show, close, drag, etc.)
# was introduced just after the tagging of 5.6.0. These signals allow us to
# prompt the user to save modified resources prior to closing the application.
# To make this feature available while not requiring the use an as-yet untagged
# ParaView, we set a local compiler definition to enable this feature if the
# detected ParaView contains the features we need.
#
# TODO: In the future, ParaView will have a means of differentiating between the
# latest tagged branch and master. When that happens, we can replace this logic
# with a ParaView version check to see if our ParaView is greater than 5.6.0.
# Until then, we simply look for the header file associated with the feature we
# want.
find_file(main_window_event_manager_hdr
  NAMES pqMainWindowEventManager.h
  PATHS ${PARAVIEW_INCLUDE_DIRS}
  NO_DEFAULT_PATH)
if (main_window_event_manager_hdr)
  set_source_files_properties(mbMainWindow.cxx
    PROPERTIES COMPILE_DEFINITIONS "HAS_MAIN_WINDOW_EVENT_MANAGER")
endif()


# Qt UI files
set(modelbuilder_UI_SRCS
  ui/mbMainWindow.ui
  ui/mbFileMenu.ui
)

# Qt resource files
set(modelbuilder_RSRCS
  resource/mbResource.qrc
)

#------------------------------------------------------------------------------
# Add extra library containing custom code for the client.
include(ParaViewQt)
pv_find_package_qt(qt_targets REQUIRED QUIET QT5_COMPONENTS Widgets)
pv_qt_wrap_cpp(MOC_BUILT_SOURCES ${modelbuilder_MOC_HDRS})
pv_qt_wrap_ui(UI_BUILT_SOURCES ${modelbuilder_UI_SRCS})
pv_qt_add_resources(QT_BUILT_RESOURCES ${modelbuilder_RSRCS})
include_directories(
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${CMAKE_CURRENT_BINARY_DIR}
)
# ${CMAKE_CURRENT_SOURCE_DIR}/Documentation)

configure_file(
  mbTestData.h.in
  ${CMAKE_CURRENT_BINARY_DIR}/mbTestData.h
)

set(modelbuilder_SOURCE_FILES
  ${modelbuilder_SRCS}
  ${modelbuilder_HDRS}
  ${modelbuilder_MOC_HDRS}
  ${MOC_BUILT_SOURCES}
  ${UI_BUILT_SOURCES}
  ${QT_BUILT_RESOURCES}
)

#------------------------------------------------------------------------------
# ParaView applications provides a mechanism to add gui xmls from modules.
# This is done by defining variables named ${vtk-module}_PARAVIEW_GUI_XMLS in
# the module.cmake file for the modules pointing to the paths for the
# GUI-xmls.  We process those here.
set (application_gui_xmls
# "${CMAKE_CURRENT_SOURCE_DIR}/ParaViewSources.xml"
# "${CMAKE_CURRENT_SOURCE_DIR}/ParaViewFilters.xml"
)
foreach (module IN LISTS VTK_MODULES_ENABLED)
  get_property(gui_xml GLOBAL PROPERTY ${module}_PARAVIEW_GUI_XMLS)
  if (gui_xml)
    foreach(xml IN LISTS gui_xml)
      list(APPEND application_gui_xmls ${xml})
    endforeach()
  endif()
endforeach()

#if (PARAVIEW_ENABLE_EMBEDDED_DOCUMENTATION)
#  #------------------------------------------------------------------------------
#  # Build Online-Help (aka Embedded Help) for the modelbuilder application.
#  # This is done after the above piece of code that sets the application_gui_xmls
#  # variable. Documentation/CMakeLists.txt depends on it.
#  add_subdirectory(Documentation)
#endif()

set(title "modelbuilder ${cmb_version} ${cmb_build_architecture}-bit")
#------------------------------------------------------------------------------
# Build the client
build_paraview_client(modelbuilder
  APPLICATION_NAME "modelbuilder"
  TITLE "${title}"
  ORGANIZATION  "modelbuilder"
  VERSION_MAJOR ${cmb_version_major}
  VERSION_MINOR ${cmb_version_minor}
  VERSION_PATCH ${cmb_version_patch}
  PVMAIN_WINDOW mbMainWindow
  PVMAIN_WINDOW_INCLUDE mbMainWindow.h
  BUNDLE_ICON   "${CMAKE_CURRENT_SOURCE_DIR}/modelbuilder.icns"
  APPLICATION_ICON  "${CMAKE_CURRENT_SOURCE_DIR}/modelbuilder.ico"
  # GUI_CONFIGURATION_XMLS ${application_gui_xmls}
  SOURCES ${modelbuilder_SOURCE_FILES}
  INSTALL_RUNTIME_DIR "${VTK_INSTALL_RUNTIME_DIR}"
  INSTALL_LIBRARY_DIR "${VTK_INSTALL_LIBRARY_DIR}"
  INSTALL_ARCHIVE_DIR "${VTK_INSTALL_ARCHIVE_DIR}"
  # Disable this until we have a preference to prevent it,
  # otherwise it interferes with debugging:
  # SPLASH_IMAGE "${CMAKE_CURRENT_SOURCE_DIR}/resource/splash.png"
)

set(cumulus_libs)
if (ENABLE_Cumulus)
  set(cumulus_libs  smtkCumulusExt smtkPQCumulusJobsPlugin)
endif()

target_link_libraries(modelbuilder
  LINK_PRIVATE
    ${qt_targets}
    vtkPVServerManagerDefault
    # TODO: we should not be linking against smtk directly.
    smtkCore
    smtkDefaultPlugins
    ${cumulus_libs}
    )
target_include_directories(modelbuilder
  PUBLIC
    $<BUILD_INTERFACE:${cmb_SOURCE_DIR}>
    $<BUILD_INTERFACE:${cmb_BINARY_DIR}>
)

#if (PARAVIEW_ENABLE_EMBEDDED_DOCUMENTATION)
#  # Link against the documentation module.
#  target_link_libraries(modelbuilder
#    LINK_PRIVATE vtkParaViewDocumentation)
#endif()

# link enabled plugins if not building in shared library mode and
# add dependecies to linked python modules These are non-empty only when
# building statically.
if (PARAVIEW_ENABLE_PYTHON)
  target_link_libraries(modelbuilder
    LINK_PRIVATE
      vtkUtilitiesPythonInitializer
  )
endif()

if(NOT BUILD_SHARED_LIBS)
  target_link_libraries(modelbuilder
    LINK_PRIVATE
      vtkPVStaticPluginsInit
  )
endif()

if (cmb_enable_testing)
  add_subdirectory(testing)
endif ()

#------------------------------------------------------------------------------
if (APPLE AND NOT PARAVIEW_DO_UNIX_STYLE_INSTALLS)

  # Use a custom plist file, since the default one provided by CMake does not
  # enable high resolution graphics. In the future, we can augment this plist
  # file to include default file extensions that can be opened by modelbuilder.
  # See ParaView's info.plist as an example.
  configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/resource/MacOSXBundleInfo.plist.in.in"
    "${CMAKE_CURRENT_BINARY_DIR}/MacOSXBundleInfo.plist.in"
    @ONLY
    )
  set_target_properties(modelbuilder
    PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_BINARY_DIR}/MacOSXBundleInfo.plist.in)

  # For Macs, we add install rule to package everything that's built into a single
  # App. Look at the explanation of MACOSX_APP_INSTALL_PREFIX in the top-level
  # CMakeLists.txt file for details.

  # add install rules to generate the App bundle.
  install(CODE "
   include(\"${ParaView_CMAKE_DIR}/ParaViewBrandingInstallApp.cmake\")

   #fillup bundle with all the libraries and plugins.
   cleanup_bundle(
     \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_RUNTIME_DIR}/modelbuilder.app/Contents/MacOS/modelbuilder
     \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_RUNTIME_DIR}/modelbuilder.app
     \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_LIBRARY_DIR}
     \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${PARAVIEW_INSTALL_PLUGINS_DIR}
     \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_DATA_DIR})

   # Place the App at the requested location.
   file(INSTALL DESTINATION \"${MACOSX_APP_INSTALL_PREFIX}\"
        TYPE DIRECTORY FILES
          \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_RUNTIME_DIR}/modelbuilder.app\"
        USE_SOURCE_PERMISSIONS)
   "
   COMPONENT Runtime)
elseif (APPLE AND PARAVIEW_DO_UNIX_STYLE_INSTALLS)
  # This is a unix style install on OsX. Purge the bundle.
  install(CODE
    "
      include(\"${ParaView_CMAKE_DIR}/ParaViewBrandingInstallApp.cmake\")
      convert_bundle_to_executable(
          \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_RUNTIME_DIR}/modelbuilder.app/Contents/MacOS/modelbuilder
          \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_RUNTIME_DIR}/modelbuilder.app
          \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${VTK_INSTALL_RUNTIME_DIR})
    "
    COMPONENT Runtime)
elseif (UNIX)
  configure_file(
    modelbuilder.desktop.in
    "${CMAKE_CURRENT_BINARY_DIR}/modelbuilder.desktop"
    @ONLY)
  install(
    FILES       "${CMAKE_CURRENT_BINARY_DIR}/modelbuilder.desktop"
    DESTINATION share/applications
    COMPONENT   runtime)
  foreach (iconsize 22x22 32x32 96x96)
    install(
      FILES       "resource/modelbuilder-${iconsize}.png"
      DESTINATION "share/icons/hicolor/${iconsize}/apps"
      RENAME      modelbuilder.png
      COMPONENT   runtime)
  endforeach ()
  install(
    FILES       resource/modelbuilder.appdata.xml
    DESTINATION share/appdata
    COMPONENT   runtime)
endif()
