
if(OCTOVIS_QT5)
  find_package(Qt5Core REQUIRED)
  find_package(Qt5Gui REQUIRED)
  find_package(Qt5OpenGL REQUIRED)
  find_package(Qt5Widgets REQUIRED)
  find_package(Qt5Xml REQUIRED)
  set(QT_LIBRARIES Qt5::Core Qt5::Gui Qt5::OpenGL Qt5::Widgets Qt5::Xml "${OPENGL_gl_LIBRARY}" "${OPENGL_glu_LIBRARY}")
  include_directories(
    "${Qt5Core_INCLUDE_DIRS}"
    "${Qt5Gui_INCLUDE_DIRS}"
    "${Qt5OpenGL_INCLUDE_DIRS}"
    "${Qt5Widgets_INCLUDE_DIRS}"
    "${Qt5Xml_INCLUDE_DIRS}"
    )
else(OCTOVIS_QT5)
  # Qt4-support (more info: http://qtnode.net/wiki?title=Qt_with_cmake)
  find_package(Qt4 REQUIRED)
  set(QT_USE_QTOPENGL TRUE)
  set(QT_USE_QTXML TRUE)
  # include the files enabled above
  include(${QT_USE_FILE})
endif(OCTOVIS_QT5)

# Mac OS X seems to require special linker flags:
IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  SET (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework OpenGL")
  SET (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -framework OpenGL")
  SET (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -framework OpenGL")
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

# sources for octovis library
set(viewerlib_SRCS
	src/SceneObject.cpp
	src/PointcloudDrawer.cpp
	src/OcTreeDrawer.cpp
	src/SelectionBox.cpp
	src/TrajectoryDrawer.cpp
	src/ColorOcTreeDrawer.cpp
)

# sources for viewer binary
set(viewer_SRCS
	src/main.cpp	
	src/ViewerGui.cpp
	src/ViewerWidget.cpp
	src/ViewerSettings.cpp
	src/ViewerSettingsPanel.cpp
	src/ViewerSettingsPanelCamera.cpp
	src/CameraFollowMode.cpp
)	

# Resource files (icons, ...)
if(OCTOVIS_QT5)
  QT5_ADD_RESOURCES(viewer_RES src/icons.qrc)
else(OCTOVIS_QT5)
  QT4_ADD_RESOURCES(viewer_RES src/icons.qrc)
endif(OCTOVIS_QT5)

#found QGLViewer lib dir
link_directories(${QGLViewer_LIBRARY_DIR})

INCLUDE_DIRECTORIES(
  ${QGLViewer_INCLUDE_DIR}
)

SET(viewer_MOC_HDRS
  ${PROJECT_SOURCE_DIR}/include/octovis/ViewerGui.h
  ${PROJECT_SOURCE_DIR}/include/octovis/ViewerWidget.h
  ${PROJECT_SOURCE_DIR}/include/octovis/ViewerSettings.h
  ${PROJECT_SOURCE_DIR}/include/octovis/ViewerSettingsPanel.h
  ${PROJECT_SOURCE_DIR}/include/octovis/ViewerSettingsPanelCamera.h
  ${PROJECT_SOURCE_DIR}/include/octovis/CameraFollowMode.h
)

# generate list of MOC srcs:
if(OCTOVIS_QT5)
  QT5_WRAP_CPP(viewer_MOC_SRCS ${viewer_MOC_HDRS})
else(OCTOVIS_QT5)
  QT4_WRAP_CPP(viewer_MOC_SRCS ${viewer_MOC_HDRS})
endif(OCTOVIS_QT5)

# let cmake generate ui*.h files from .ui files (Qt Designer):
SET(viewer_UIS
    ${PROJECT_SOURCE_DIR}/include/octovis/ViewerGui.ui
    ${PROJECT_SOURCE_DIR}/include/octovis/ViewerSettings.ui
    ${PROJECT_SOURCE_DIR}/include/octovis/ViewerSettingsPanel.ui
    ${PROJECT_SOURCE_DIR}/include/octovis/ViewerSettingsPanelCamera.ui
)
if(OCTOVIS_QT5)
  QT5_WRAP_UI(viewer_UIS_H ${viewer_UIS})
else(OCTOVIS_QT5)
  QT4_WRAP_UI(viewer_UIS_H ${viewer_UIS})
endif(OCTOVIS_QT5)

# Don't forget to include output directory, otherwise
# the UI file won't be wrapped!
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_BINARY_DIR})


# Library target 
add_library(octovis-static STATIC ${viewerlib_SRCS})
target_link_libraries(octovis-static
  ${OPENGL_gl_LIBRARY} 
  ${OPENGL_glu_LIBRARY} 
  ${OCTOMAP_LIBRARIES}
  ${QGLViewer_LIBRARIES}
)
set_target_properties(octovis-static PROPERTIES OUTPUT_NAME octovis)

add_library(octovis-shared SHARED ${viewerlib_SRCS})
target_link_libraries(octovis-shared 
  ${OPENGL_gl_LIBRARY} 
  ${OPENGL_glu_LIBRARY} 
  ${OCTOMAP_LIBRARIES}
  ${QGLViewer_LIBRARIES}
)
set_target_properties(octovis-shared PROPERTIES
  OUTPUT_NAME octovis
  VERSION ${OCTOVIS_VERSION}
  SOVERSION ${OCTOVIS_SOVERSION}
)

# directly depend on the octomap library target when building the
# complete distribution, so it is recompiled as needed
if (CMAKE_PROJECT_NAME STREQUAL "octomap-distribution")
  ADD_DEPENDENCIES(octovis-static octomap-static)
  ADD_DEPENDENCIES(octovis-shared octomap)
endif()


# Now add these generated files to the ADD_EXECUTABLE step
# If this is NOT done, then the ui_*.h files will not be generated
add_executable(octovis ${viewer_SRCS} ${viewer_UIS_H} ${viewer_MOC_SRCS} ${viewer_RES})

target_link_libraries(octovis 
#  ${QGLViewer_LIBRARIES}
  ${QT_LIBRARIES}
  ${OCTOMAP_LIBRARIES}
  octovis-shared
)

# special handling of MacOS X:
IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  add_custom_command(TARGET octovis POST_BUILD
    COMMAND install_name_tool -change libQGLViewer.2.dylib /opt/local/lib/libQGLViewer.2.dylib ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/octovis
  )
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

if(NOT EXISTS "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/octovis")
  file(MAKE_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/octovis")
endif()

export(TARGETS octovis octovis-static octovis-shared
  FILE "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/octovis/octovis-targets.cmake")

install(TARGETS octovis octovis-static octovis-shared
  EXPORT octovis-targets
  INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
  ${INSTALL_TARGETS_DEFAULT_ARGS}
)
install(EXPORT octovis-targets DESTINATION "${CMAKE_INSTALL_DATADIR}/octovis")

file(GLOB octovis_HDRS ${PROJECT_SOURCE_DIR}/include/octovis/*.h)
# filter generated headers for GUI:
list(REMOVE_ITEM octovis_HDRS ${viewer_MOC_HDRS} ${viewer_UIS_H})
install(FILES ${octovis_HDRS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/octovis")

