From e458b88e481e8af4ef2d97c9af79fdf40e40ed81 Mon Sep 17 00:00:00 2001
From: Ethan A Merritt <merritt@u.washington.edu>
Date: Sun, 30 Jul 2023 11:37:14 -0700
Subject: [PATCH] configure:   Check for Qt6, fall back to Qt5

Note: requires C++  -std=c++17

Note: requires Qt6 compatibility module Core5Compat
	for class QTextCodec
		codecForLocale() codecForMib() fromUnicode() toUnicode()
	in order to support non-utf8 encodings (gnuplot's "set encoding").

	This is not entirely satisfactory, since there is apparently not
	a guarantee from Qt that the compatibility modules will continue
	to be available in the future.

./configure --with-qt=qt4  is still accepted but I no longer have
the Qt4 libraries installed to confirm that it works.


Adapted by MXE Octave for newer versions of Qt6 which seem to have
a different dependency between modules.
---
 configure.ac                     | 53 ++++++++++++++++++++++----------
 src/qtterminal/qt_conversion.cpp |  9 ++++++
 2 files changed, 45 insertions(+), 17 deletions(-)

diff --git a/configure.ac b/configure.ac
index 3f98c46d0..ca09c31d2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1065,29 +1065,48 @@ if test "${enable_qt}" = yes ; then
       enable_qt_ok=no
   fi
 
-  dnl First check for Qt5
-  if test "x${with_qt}" = "xqt5"; then
-    try_qt4=no
-  else
+  dnl Qt4 only by special request
+  if test "x${with_qt}" = "xqt4"; then
     try_qt4=yes
+  else
+    try_qt4=no
   fi
+
   if test "x${with_qt}" != "xqt4"; then
-    PKG_CHECK_MODULES_NOFAIL(QT, [Qt5Core Qt5Gui Qt5Network Qt5Svg Qt5PrintSupport])
+    AC_MSG_CHECKING([Checking for Qt6 support libraries])
+    PKG_CHECK_MODULES_NOFAIL(QT, [Qt6Core Qt6Gui Qt6Network Qt6Svg Qt6Widgets Qt6PrintSupport Qt6Core5Compat])
     if test $pkg_failed = no; then
-      try_qt4=no
-      QT5LOC=`$PKG_CONFIG --variable=host_bins Qt5Core`
-      if test "x${QT5LOC}" != "x"; then
-        UIC=${QT5LOC}/uic
-        MOC=${QT5LOC}/moc
-        RCC=${QT5LOC}/rcc
-        LRELEASE=${QT5LOC}/lrelease
+      QT6LOC=`$PKG_CONFIG --variable=host_bins Qt6Core`
+      if test "x${QT6LOC}" != "x"; then
+        UIC=${QT6LOC}/uic
+        MOC=${QT6LOC}/moc
+        RCC=${QT6LOC}/rcc
+        LRELEASE=${QT6LOC}/lrelease
       fi
       CXXFLAGS="$CXXFLAGS -fPIC"
+      AC_MSG_RESULT([The Qt terminal will use Qt6.])
+      QTVER="6"
+    else
+      AC_MSG_CHECKING([Checking for Qt5 support libraries])
+      PKG_CHECK_MODULES_NOFAIL(QT, [Qt5Core Qt5Gui Qt5Network Qt5Svg Qt5PrintSupport])
+      if test $pkg_failed = no; then
+        QT5LOC=`$PKG_CONFIG --variable=host_bins Qt5Core`
+        if test "x${QT5LOC}" != "x"; then
+          UIC=${QT5LOC}/uic
+          MOC=${QT5LOC}/moc
+          RCC=${QT5LOC}/rcc
+          LRELEASE=${QT5LOC}/lrelease
+        fi
+        CXXFLAGS="$CXXFLAGS -fPIC"
+        AC_MSG_RESULT([The Qt terminal will use Qt5.])
+        QTVER="5"
+      fi
     fi 
   fi
 
-  dnl No Qt5, check for Qt4.5 or greater
-  if test ${try_qt4} != no; then
+  dnl Explicit request for qt4 
+  if test ${try_qt4} = yes; then
+    AC_MSG_CHECKING([Checking for Qt4 support])
     PKG_CHECK_MODULES_NOFAIL(QT, [QtCore >= 4.5 QtGui >= 4.5 QtNetwork >= 4.5 QtSvg >= 4.5])
     if test $pkg_failed != no; then
       enable_qt_ok=no
@@ -1170,9 +1189,6 @@ if test "${enable_qt}" = yes ; then
       AC_MSG_WARN([The Qt terminal will use Qt4.])
       QTVER="4"
     fi
-  else
-    AC_MSG_WARN([The Qt terminal will use Qt5.])
-    QTVER="5"
   fi
 fi
 
@@ -1366,6 +1382,9 @@ if test "$enable_qt_ok" = yes; then
   if test "$QTVER" = 5; then
       AC_MSG_RESULT([  Qt                   : yes (qt5)])
   fi
+  if test "$QTVER" = 6; then
+      AC_MSG_RESULT([  Qt                   : yes (qt6)])
+  fi
 else
   AC_MSG_RESULT([  Qt                   : no (use --with-qt or --with-qt=qt4 to enable])
 fi
diff --git a/src/qtterminal/qt_conversion.cpp b/src/qtterminal/qt_conversion.cpp
index cf8a20930..a50d357b8 100644
--- a/src/qtterminal/qt_conversion.cpp
+++ b/src/qtterminal/qt_conversion.cpp
@@ -41,6 +41,15 @@
  * under either the GPL or the gnuplot license.
 ]*/
 
+/*
+ * Qt6 no longer supports other 8-bit encodings natively, relegating the
+ * QTextCodec functions to a compatibility module Core5Compat.
+ * FIXME: roll our own?
+ */
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+    #include <QTextCodec>
+#endif
+
 static QColor qt_colorList[12] =
 {
 	Qt::white,
-- 
2.38.0.windows.1

