IF(IS_64_BIT)
	SET(Qt5Widgets_Loc_1 "C:/Qt/Qt5.2.1/5.2.1/msvc2012_64_opengl/lib/cmake/Qt5Widgets")
	SET(Qt5Widgets_Loc_2 "C:/Qt/Qt5.2.1/5.2.1/msvc2012_64/lib/cmake/Qt5Widgets")
ELSE()
	SET(Qt5Widgets_Loc_1 "C:/Qt/Qt5.3.1/5.3/msvc2012_opengl/lib/cmake/Qt5Widgets")
	SET(Qt5Widgets_Loc_2 "C:/Qt/Qt5.4.0/5.4/msvc2012_opengl/lib/cmake/Qt5Widgets")
ENDIF()

SET(HAS_OPENGL TRUE)

IF(EXISTS "${Qt5Widgets_Loc_1}")
	SET(Qt5Widgets_DIR "${Qt5Widgets_Loc_1}" CACHE STRING "Default search path for QtWidgets cmake file")
ELSEIF(EXISTS "${Qt5Widgets_Loc_2}")
	IF(IS_64_BIT)
		SET(HAS_OPENGL FALSE)
	ENDIF()
	SET(Qt5Widgets_DIR "${Qt5Widgets_Loc_2}" CACHE STRING "Default search path for QtWidgets cmake file")
ELSE()
	SET(Qt5Widgets_DIR "" CACHE STRING "Default search path for QtWidgets cmake file")
ENDIF()

MESSAGE(STATUS "qt Installation has OpenGL = ${HAS_OPENGL}")
set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}; ${QTDIR}")

find_package(Qt5Widgets QUIET)

IF(Qt5Widgets_FOUND)
	OPTION(USE_QT "Build qt-based GUI controls." TRUE)
ELSE()
	OPTION(USE_QT "Build qt-based GUI controls." FALSE)
ENDIF()

IF(USE_QT)
	IF(IS_WINDOWS)
		find_package(Qt5Widgets REQUIRED)
	ELSE()
		find_package(Qt5 COMPONENTS Widgets REQUIRED)
	ENDIF()
ENDIF()

IF(USE_QT AND Qt5Widgets_FOUND)
	SET(qt_FOUND TRUE)
	ADD_DEFINITIONS( -D_USE_QT_ )
	string(REPLACE "." ";" Qt5Widgets_VERSION_SEP ${Qt5Widgets_VERSION})
	list(GET Qt5Widgets_VERSION_SEP 0 QT_VERSION_MAJOR)
	list(GET Qt5Widgets_VERSION_SEP 1 QT_VERSION_MINOR)
	list(GET Qt5Widgets_VERSION_SEP 2 QT_VERSION_PATCH)
	
	IF("${QT_VERSION_PATCH}" STREQUAL "")
		SET(QT_VERSION_PATCH "0")
	ENDIF()
	
	if(POLICY CMP0020)
		cmake_policy(SET CMP0020 NEW)
	endif()
	
	foreach(SUBLIB_NAME ${SUBLIBRARY_NAMES})
		string(TOUPPER "${SUBLIB_NAME}" UPPERCASE_NAME)
		IF(IS_WINDOWS)
			IF( ("${QT_VERSION_MAJOR}" STREQUAL "5") AND ("${QT_VERSION_MINOR}" STREQUAL "2") )
				SET(Qt_${UPPERCASE_NAME}_BUILD_DIR "build-${SUBLIB_NAME}-Desktop_Qt_${QT_VERSION_MAJOR}_${QT_VERSION_MINOR}_${QT_VERSION_PATCH}_${MSVC_ABBREVIATION_CAPS}_openGL_${ARCH_BITS}bit-Debug")
			ELSEIF( ("${QT_VERSION_MAJOR}" STREQUAL "5") AND ("${QT_VERSION_MINOR}" STREQUAL "4") )
				SET(Qt_${UPPERCASE_NAME}_BUILD_DIR "build-${SUBLIB_NAME}-Desktop_Qt_${QT_VERSION_MAJOR}_${QT_VERSION_MINOR}_0_${MSVC_ABBREVIATION_CAPS}_openGL_${ARCH_BITS}bit-Debug")
			ELSE()
				SET(Qt_${UPPERCASE_NAME}_BUILD_DIR "build-UR10_${SUBLIB_NAME}-Desktop_Qt_${QT_VERSION_MAJOR}_${QT_VERSION_MINOR}_${MSVC_ABBREVIATION_CAPS}_openGL_${ARCH_BITS}bit-Debug")
			ENDIF()
		ELSE()
			SET(Qt_${UPPERCASE_NAME}_BUILD_DIR "build-${SUBLIB_NAME}-Desktop-Debug")
		ENDIF()
		SET(QT${UPPERCASE_NAME}_BUILD_PATH "${CMAKE_CURRENT_SOURCE_DIR}/qt/${Qt_${UPPERCASE_NAME}_BUILD_DIR}" CACHE STRING "Build directory for Qt ${SUBLIB_NAME} app")
	endforeach(SUBLIB_NAME) 

	LIST(APPEND ADDITIONAL_LIBRARIES Qt5::Widgets)
	
	SET(APOSITION "0")
	STRING( FIND ${Qt5Widgets_DIR} "5.3" APOSITION )
	
	IF("QT_VERSION_MINOR" STREQUAL "2")
		SET(QT_ICU_VER_NUM "51")
	ELSEIF("${APOSITION}" STREQUAL "-1")
		SET(QT_ICU_VER_NUM "53")
	ELSE()
		SET(QT_ICU_VER_NUM "52")
	ENDIF()
	
ELSEIF(USE_QT)
	SET(qt_FOUND FALSE)
	MESSAGE(FATAL_ERROR "Qt5 was not found. Please check that the <Qt5Widgets_DIR> variable points to the location of <Qt5WidgetsConfig.cmake>. If you want to build without the GUI, please deselect the option <BUILD_GUI>")
ENDIF()
