changeset 28571:dea121672378

avoid deprecated Qt QFontMetrics::width function * acinclude.m4 (OCTAVE_CHECK_FUNC_QFONTMETRICS_HORIZONTAL_ADVANCE): New macro. (OCTAVE_CHECK_QT_VERSION): Use it. * qt-utils.h (qt_fontmetrics_horizontal_advance): New wrapper functions. * TerminalView.cpp, variable-editor.cc: Use qt_fontmetrics_horizontal_advance instead of QFontMetrics::width.
author John W. Eaton <jwe@octave.org>
date Tue, 14 Jul 2020 00:45:39 -0400
parents 5322aed1304c
children 7f233261e268
files libgui/qterminal/libqterminal/unix/TerminalView.cpp libgui/src/qt-utils.h libgui/src/variable-editor.cc m4/acinclude.m4
diffstat 4 files changed, 72 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/qterminal/libqterminal/unix/TerminalView.cpp	Mon Jul 13 15:54:38 2020 -0400
+++ b/libgui/qterminal/libqterminal/unix/TerminalView.cpp	Tue Jul 14 00:45:39 2020 -0400
@@ -23,6 +23,12 @@
     02110-1301  USA.
 */
 
+#if defined (HAVE_CONFIG_H)
+#  include "config.h"
+#endif
+
+#include "qt-utils.h"
+
 // Own
 #include "unix/TerminalView.h"
 
@@ -162,14 +168,15 @@
   // "Base character width on widest ASCII character. This prevents too wide
   //  characters in the presence of double wide (e.g. Japanese) characters."
   // Get the width from representative normal width characters
-  _fontWidth = (double)fm.width(REPCHAR)/(double)strlen(REPCHAR);
+  _fontWidth = ((double)octave::qt_fontmetrics_horizontal_advance(fm, REPCHAR)
+                / (double)strlen(REPCHAR));
 
   _fixedFont = true;
 
-  int fw = fm.width(REPCHAR[0]);
+  int fw = octave::qt_fontmetrics_horizontal_advance(fm, REPCHAR[0]);
   for(unsigned int i=1; i< strlen(REPCHAR); i++)
     {
-      if (fw != fm.width(REPCHAR[i]))
+      if (fw != octave::qt_fontmetrics_horizontal_advance(fm, REPCHAR[i]))
         {
           _fixedFont = false;
           break;
@@ -909,7 +916,7 @@
       if (!_resizeWidget)
         {
           _resizeWidget = new QLabel(("Size: XXX x XXX"), this);
-          _resizeWidget->setMinimumWidth(_resizeWidget->fontMetrics().width(("Size: XXX x XXX")));
+          _resizeWidget->setMinimumWidth(octave::qt_fontmetrics_horizontal_advance(_resizeWidget->fontMetrics(), "Size: XXX x XXX"));
           _resizeWidget->setMinimumHeight(_resizeWidget->sizeHint().height());
           _resizeWidget->setAlignment(Qt::AlignCenter);
 
--- a/libgui/src/qt-utils.h	Mon Jul 13 15:54:38 2020 -0400
+++ b/libgui/src/qt-utils.h	Tue Jul 14 00:45:39 2020 -0400
@@ -28,12 +28,13 @@
 
 #include <list>
 
+#include <QFontMetrics>
 #include <QList>
 
 namespace octave
 {
   template <typename T>
-  QList<T>
+  inline QList<T>
   std_list_to_qt_list (const std::list<T>& lst)
   {
 #if defined (HAVE_QLIST_ITERATOR_CONSTRUCTOR)
@@ -42,6 +43,27 @@
     return QList<T>::fromStdList (lst);
 #endif
   }
+
+  inline int
+  qt_fontmetrics_horizontal_advance (const QFontMetrics& fm, QChar ch)
+  {
+#if defined (HAVE_QFONTMETRICS_HORIZONTAL_ADVANCE)
+    return fm.horizontalAdvance (ch);
+#else
+    return fm.width (ch);
+#endif
+  }
+
+  inline int
+  qt_fontmetrics_horizontal_advance (const QFontMetrics& fm,
+                                     const QString& text, int len = -1)
+  {
+#if defined (HAVE_QFONTMETRICS_HORIZONTAL_ADVANCE)
+    return fm.horizontalAdvance (text, len);
+#else
+    return fm.width (text, len);
+#endif
+  }
 }
 
 #endif
--- a/libgui/src/variable-editor.cc	Mon Jul 13 15:54:38 2020 -0400
+++ b/libgui/src/variable-editor.cc	Tue Jul 14 00:45:39 2020 -0400
@@ -53,6 +53,7 @@
 #include "gui-preferences-global.h"
 #include "gui-preferences-ve.h"
 #include "octave-qobject.h"
+#include "qt-utils.h"
 #include "shortcut-manager.h"
 #include "variable-editor-model.h"
 #include "variable-editor.h"
@@ -501,7 +502,8 @@
         // font, so any character will do.  If not, you lose!
 
         QFontMetrics fm (font ());
-        int w = m_var_model->column_width () * fm.width ('0');
+        int w = (m_var_model->column_width ()
+                 * qt_fontmetrics_horizontal_advance (fm, '0'));
         horizontalHeader ()->setDefaultSectionSize (w);
       }
   }
--- a/m4/acinclude.m4	Mon Jul 13 15:54:38 2020 -0400
+++ b/m4/acinclude.m4	Tue Jul 14 00:45:39 2020 -0400
@@ -490,6 +490,39 @@
   fi
 ])
 dnl
+dnl Check whether the Qt class QList has a constructor that accepts
+dnl a pair of iterators.  This constructor was introduced in Qt 5.14.
+dnl
+AC_DEFUN([OCTAVE_CHECK_FUNC_QFONTMETRICS_HORIZONTAL_ADVANCE], [
+  AC_CACHE_CHECK([for QFontMetrics::horizontalAdvance function],
+    [octave_cv_func_qfontmetrics_horizontal_advance],
+    [AC_LANG_PUSH(C++)
+    ac_octave_save_CPPFLAGS="$CPPFLAGS"
+    ac_octave_save_CXXFLAGS="$CXXFLAGS"
+    CPPFLAGS="$QT_CPPFLAGS $CXXPICFLAG $CPPFLAGS"
+    CXXFLAGS="$CXXPICFLAG $CXXFLAGS"
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
+        #include <QFont>
+        #include <QFontMetrics>
+        #include <QString>
+        ]], [[
+        QFont font;
+        QFontMetrics fm (font);
+        fm.horizontalAdvance ('x');
+        fm.horizontalAdvance (QString ("string"));
+        ]])],
+      octave_cv_func_qfontmetrics_horizontal_advance=yes,
+      octave_cv_func_qfontmetrics_horizontal_advance=no)
+    CPPFLAGS="$ac_octave_save_CPPFLAGS"
+    CXXFLAGS="$ac_octave_save_CXXFLAGS"
+    AC_LANG_POP(C++)
+  ])
+  if test $octave_cv_func_qfontmetrics_horizontal_advance = yes; then
+    AC_DEFINE(HAVE_QFONTMETRICS_HORIZONTAL_ADVANCE, 1,
+      [Define to 1 if you have the `QFontMetrics::horizontalAdvance' function.])
+  fi
+])
+dnl
 dnl Check whether HDF5 library has version 1.6 API functions.
 dnl
 AC_DEFUN([OCTAVE_CHECK_HDF5_HAS_VER_16_API], [
@@ -1789,10 +1822,11 @@
     ## tests if they fail because we have already decided that the Qt
     ## version that we are testing now will be the one used.
 
+    OCTAVE_CHECK_FUNC_QFONTMETRICS_HORIZONTAL_ADVANCE
     OCTAVE_CHECK_FUNC_QGUIAPPLICATION_SETDESKTOPFILENAME
     OCTAVE_CHECK_FUNC_QHELPSEARCHQUERYWIDGET_SEARCHINPUT
+    OCTAVE_CHECK_FUNC_QLIST_ITERATOR_CONSTRUCTOR
     OCTAVE_CHECK_FUNC_QSCREEN_DEVICEPIXELRATIO
-    OCTAVE_CHECK_FUNC_QLIST_ITERATOR_CONSTRUCTOR
 
     if test -n "$OPENGL_LIBS"; then
       OCTAVE_CHECK_QT_OPENGL_OK([build_qt_graphics=yes],