From df741f7c762e98fc892971e56681504046c0b1bf Mon Sep 17 00:00:00 2001
From: Utkarsh Ayachit <utkarsh.ayachit@kitware.com>
Date: Tue, 2 Aug 2016 16:25:01 -0400
Subject: [PATCH] Use vtk_module_export_code_find_package() for Qt4.

Similar to what we're doing with Qt5, this change now adds called to
vtk_module_export_code_find_package() for Qt4. That way, when importing
a module using Qt4, we automatically find Qt4.

Also changed to use imported targets rather than library paths when
linking to Qt4 libraries.
---
 GUISupport/Qt/CMakeLists.txt       | 18 ++++++++++++------
 GUISupport/QtOpenGL/CMakeLists.txt | 11 ++++++++---
 GUISupport/QtSQL/CMakeLists.txt    | 11 ++++++++---
 GUISupport/QtWebkit/CMakeLists.txt |  2 ++
 Rendering/Qt/CMakeLists.txt        |  7 ++++++-
 Views/Qt/CMakeLists.txt            | 12 ++++++++----
 6 files changed, 44 insertions(+), 17 deletions(-)

diff --git a/GUISupport/Qt/CMakeLists.txt b/GUISupport/Qt/CMakeLists.txt
index dd97113dcc..8eea09dda3 100644
--- a/GUISupport/Qt/CMakeLists.txt
+++ b/GUISupport/Qt/CMakeLists.txt
@@ -84,13 +84,18 @@ if(VTK_QT_VERSION VERSION_GREATER "4")
   vtk_module_export_code_find_package(Qt5 COMPONENTS Widgets)
 else()
   # import Qt4 build settings
-  set(QT_USE_QTNETWORK 1)
-  find_package(Qt4 REQUIRED QUIET)
+  find_package(Qt4 COMPONENTS QtCore QtGui REQUIRED QUIET)
   mark_as_advanced(QT_QMAKE_EXECUTABLE)
-  include(${QT_USE_FILE})
+
+  include_directories(${QT_INCLUDES})
+  add_definitions(${QT_DEFINITIONS})
 
   qt4_wrap_cpp(QVTKLibMocSrcs ${QVTKMocHeaders})
+  set(QT_LIBRARIES Qt4::QtCore Qt4::QtGui)
   set(QT_LIBRARIES_PRIVATE)
+
+  # When this module is loaded by an app, load Qt too.
+  vtk_module_export_code_find_package(Qt4 COMPONENTS QtCore QtGui)
 endif()
 
 foreach(opt
@@ -140,10 +145,11 @@ if(BUILD_SHARED_LIBS AND VTK_BUILD_QT_DESIGNER_PLUGIN)
     qt5_wrap_cpp(PluginMocSrcs ${PluginMocHeaders} TARGET QVTKWidgetPlugin)
     set(QT_LIBRARIES Qt5::${qt_component})
   else()
-    add_definitions(-DQT_PLUGIN)
-    include_directories(${QT_QTDESIGNER_INCLUDE_DIR})
+    find_package(Qt4 COMPONENTS QtCore QtGui QtDesignerComponents REQUIRED QUIET)
+    include_directories(${QT_INCLUDES})
+    add_definitions(${QT_DEFINITIONS})
     qt4_wrap_cpp(PluginMocSrcs ${PluginMocHeaders})
-    set(QT_LIBRARIES ${QT_QTGUI_LIBRARY} ${QT_QTCORE_LIBRARY})
+    set(QT_LIBRARIES Qt4::QtCore Qt4::QtGui Qt4::QtDesignerComponents)
   endif()
 
   # add QVTK plugin from sources
diff --git a/GUISupport/QtOpenGL/CMakeLists.txt b/GUISupport/QtOpenGL/CMakeLists.txt
index c876e10e67..c529d7f524 100644
--- a/GUISupport/QtOpenGL/CMakeLists.txt
+++ b/GUISupport/QtOpenGL/CMakeLists.txt
@@ -25,11 +25,16 @@ if(VTK_QT_VERSION VERSION_GREATER "4")
   vtk_module_export_code_find_package(Qt5 COMPONENTS OpenGL)
 else()
   # import Qt4 build settings
-  set(QT_USE_QTOPENGL 1)
-  find_package(Qt4 REQUIRED QUIET)
-  include(${QT_USE_FILE})
+  find_package(Qt4 COMPONENTS QtCore QtGui QtOpenGL REQUIRED QUIET)
+  include_directories(${QT_INCLUDES})
+  add_definitions(${QT_DEFINITIONS})
+
+  set(QT_LIBRARIES Qt4::QtCore Qt4::QtGui Qt4::QtOpenGL)
 
   qt4_wrap_cpp(LibMocSrcs ${MocHeaders})
+
+  # When this module is loaded by an app, load Qt too.
+  vtk_module_export_code_find_package(Qt4 COMPONENTS QtCore QtGui QtOpenGL)
 endif()
 
 set(${vtk-module}_NO_HeaderTest 1)
diff --git a/GUISupport/QtSQL/CMakeLists.txt b/GUISupport/QtSQL/CMakeLists.txt
index 493c1ebde6..56516804a5 100644
--- a/GUISupport/QtSQL/CMakeLists.txt
+++ b/GUISupport/QtSQL/CMakeLists.txt
@@ -27,9 +27,14 @@ if(VTK_QT_VERSION VERSION_GREATER "4")
   vtk_module_export_code_find_package(Qt5 COMPONENTS Sql Widgets)
 else()
   # import Qt4 build settings
-  set(QT_USE_QTSQL 1)
-  find_package(Qt4 REQUIRED QUIET)
-  include(${QT_USE_FILE})
+  find_package(Qt4 REQUIRED QtCore QtSql QUIET)
+  include_directories(${QT_INCLUDES})
+  add_definitions(${QT_DEFINITIONS})
+
+  set(QT_LIBRARIES Qt4::QtCore Qt4::QtSql)
+
+  # When this module is loaded by an app, load Qt too.
+  vtk_module_export_code_find_package(Qt4 COMPONENTS QtCore QtSql)
 endif()
 
 set(${vtk-module}_NO_HeaderTest 1)
diff --git a/GUISupport/QtWebkit/CMakeLists.txt b/GUISupport/QtWebkit/CMakeLists.txt
index 8492a7aeb9..2b3f8472de 100644
--- a/GUISupport/QtWebkit/CMakeLists.txt
+++ b/GUISupport/QtWebkit/CMakeLists.txt
@@ -37,6 +37,8 @@ else()
   qt4_wrap_ui(UI_FILES vtkQtRichTextView.ui)
   qt4_wrap_cpp(LibMocSrcs ${MocHeaders})
   if (QT_WEBKIT_FOUND)
+    # When this module is loaded by an app, load Qt too.
+    vtk_module_export_code_find_package(Qt4 COMPONENTS QtCore QtGui QtWebKit)
     set(_FOUND 1)
   endif()
 endif()
diff --git a/Rendering/Qt/CMakeLists.txt b/Rendering/Qt/CMakeLists.txt
index eb66fe7cb4..f68f39e46f 100644
--- a/Rendering/Qt/CMakeLists.txt
+++ b/Rendering/Qt/CMakeLists.txt
@@ -19,7 +19,12 @@ if(VTK_QT_VERSION VERSION_GREATER "4")
 else()
   # import Qt4 build settings
   find_package(Qt4 REQUIRED QtCore QtGui QUIET)
-  include(${QT_USE_FILE})
+  include_directories(${QT_INCLUDES})
+  add_definitions(${QT_DEFINITIONS})
+  set(QT_LIBRARIES Qt4::QtCore Qt4::QtGui)
+
+  # When this module is loaded by an app, load Qt too.
+  vtk_module_export_code_find_package(Qt4 COMPONENTS QtCore QtGui)
 endif()
 
 vtk_module_library(${vtk-module} ${LibSrcs})
diff --git a/Views/Qt/CMakeLists.txt b/Views/Qt/CMakeLists.txt
index eb6996eaec..9420c1bfef 100644
--- a/Views/Qt/CMakeLists.txt
+++ b/Views/Qt/CMakeLists.txt
@@ -32,14 +32,18 @@ if(VTK_QT_VERSION VERSION_GREATER "4")
   vtk_module_export_code_find_package(Qt5 COMPONENTS Widgets)
 else()
   # import Qt4 build settings
-  set(QT_USE_QTNETWORK 1)
-  find_package(Qt4 REQUIRED QUIET)
-  include(${QT_USE_FILE})
+  find_package(Qt4 REQUIRED QtCore QtGui QUIET)
+  include_directories(${QT_INCLUDES})
+  add_definitions(${QT_DEFINITIONS})
 
   qt4_wrap_cpp(LibMocSrcs ${MocHeaders})
+
+  set(QT_LIBRARIES Qt4::QtCore Qt4::QtGui)
+
+  # When this module is loaded by an app, load Qt too.
+  vtk_module_export_code_find_package(Qt4 COMPONENTS QtCore QtGui)
 endif()
 
 set(${vtk-module}_NO_HeaderTest 1)
 vtk_module_library(${vtk-module} ${LibSrcs} ${LibMocSrcs})
-
 target_link_libraries(${vtk-module} LINK_PRIVATE ${QT_LIBRARIES})
-- 
GitLab