1From caacc9bc622238ca48674ea6f40d07466e4b97a5 Mon Sep 17 00:00:00 2001
2From: Valentin Ochs <a@0au.de>
3Date: Sat, 20 Jun 2020 16:01:27 +0200
4Subject: [PATCH] Replace obsolete/deprecated Qt methods
5
6[Thomas: Backport from upstream commit
7ae726b70a7ada9a4be5808e00f0c951318479684, one conflict manually
8resolved in pv/util.cpp.]
9Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
10[Fabrice: restore original patch in pv/util.cpp for 0.4.2]
11Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
12---
13 pv/util.cpp                     | 21 +++++++++++++++++++--
14 pv/util.hpp                     | 10 ++++++++++
15 pv/views/trace/decodetrace.cpp  |  3 ++-
16 pv/views/trace/ruler.cpp        |  2 +-
17 pv/widgets/timestampspinbox.cpp |  2 +-
18 5 files changed, 33 insertions(+), 5 deletions(-)
19
20diff --git a/pv/util.cpp b/pv/util.cpp
21index 49b9467c..2a63038d 100644
22--- a/pv/util.cpp
23+++ b/pv/util.cpp
24@@ -137,7 +137,7 @@ QString format_time_si(const Timestamp& v, SIPrefix prefix,
25 	QString s;
26 	QTextStream ts(&s);
27 	if (sign && !v.is_zero())
28-		ts << forcesign;
29+		ts.setNumberFlags(ts.numberFlags() | QTextStream::ForceSign);
30 	ts << qSetRealNumberPrecision(precision) << (v * multiplier);
31 	ts << ' ' << prefix << unit;
32
33@@ -171,7 +171,7 @@ QString format_value_si(double v, SIPrefix prefix, unsigned precision,
34 	QString s;
35 	QTextStream ts(&s);
36 	if (sign && (v != 0))
37-		ts << forcesign;
38+		ts.setNumberFlags(ts.numberFlags() | QTextStream::ForceSign);
39 	ts.setRealNumberNotation(QTextStream::FixedNotation);
40 	ts.setRealNumberPrecision(precision);
41 	ts << (v * multiplier) << ' ' << prefix << unit;
42@@ -281,5 +281,22 @@ vector<string> split_string(string text, string separator)
43 	return result;
44 }
45
46+/**
47+ * Return the width of a string in a given font.
48+ *
49+ * @param[in] metric metrics of the font
50+ * @param[in] string the string whose width should be determined
51+ *
52+ * @return width of the string in pixels
53+ */
54+std::streamsize text_width(const QFontMetrics &metric, const QString &string)
55+{
56+#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
57+	return metric.horizontalAdvance(string);
58+#else
59+	return metric.width(string);
60+#endif
61+}
62+
63 } // namespace util
64 } // namespace pv
65diff --git a/pv/util.hpp b/pv/util.hpp
66index dd7be222..ad904f85 100644
67--- a/pv/util.hpp
68+++ b/pv/util.hpp
69@@ -30,6 +30,7 @@
70
71 #include <QMetaType>
72 #include <QString>
73+#include <QFontMetrics>
74
75 using std::string;
76 using std::vector;
77@@ -137,6 +138,15 @@ QString format_time_minutes(const Timestamp& t, signed precision = 0,
78
79 vector<string> split_string(string text, string separator);
80
81+/**
82+ * Return the width of a string in a given font.
83+ * @param[in] metric metrics of the font
84+ * @param[in] string the string whose width should be determined
85+ *
86+ * @return width of the string in pixels
87+ */
88+std::streamsize text_width(const QFontMetrics &metric, const QString &string);
89+
90 } // namespace util
91 } // namespace pv
92
93diff --git a/pv/views/trace/decodetrace.cpp b/pv/views/trace/decodetrace.cpp
94index 9c7196bf..1ee7ae9f 100644
95--- a/pv/views/trace/decodetrace.cpp
96+++ b/pv/views/trace/decodetrace.cpp
97@@ -103,7 +103,8 @@ DecodeTrace::DecodeTrace(pv::Session &session,
98
99 	// Determine shortest string we want to see displayed in full
100 	QFontMetrics m(QApplication::font());
101-	min_useful_label_width_ = m.width("XX"); // e.g. two hex characters
102+	// e.g. two hex characters
103+	min_useful_label_width_ = util::text_width(m, "XX");
104
105 	// For the base color, we want to start at a very different color for
106 	// every decoder stack, so multiply the index with a number that is
107diff --git a/pv/views/trace/ruler.cpp b/pv/views/trace/ruler.cpp
108index acea8a36..68134966 100644
109--- a/pv/views/trace/ruler.cpp
110+++ b/pv/views/trace/ruler.cpp
111@@ -218,7 +218,7 @@ void Ruler::paintEvent(QPaintEvent*)
112 		const int rightedge = width();
113 		const int x_tick = tick.first;
114 		if ((x_tick > leftedge) && (x_tick < rightedge)) {
115-			const int x_left_bound = QFontMetrics(font()).width(tick.second) / 2;
116+			const int x_left_bound = util::text_width(QFontMetrics(font()), tick.second) / 2;
117 			const int x_right_bound = rightedge - x_left_bound;
118 			const int x_legend = min(max(x_tick, x_left_bound), x_right_bound);
119 			p.drawText(x_legend, ValueMargin, 0, text_height,
120diff --git a/pv/widgets/timestampspinbox.cpp b/pv/widgets/timestampspinbox.cpp
121index 21b3d0d7..383aed1f 100644
122--- a/pv/widgets/timestampspinbox.cpp
123+++ b/pv/widgets/timestampspinbox.cpp
124@@ -75,7 +75,7 @@ QSize TimestampSpinBox::minimumSizeHint() const
125 {
126 	const QFontMetrics fm(fontMetrics());
127 	const int l = round(value_).str().size() + precision_ + 10;
128-	const int w = fm.width(QString(l, '0'));
129+	const int w = util::text_width(fm, QString(l, '0'));
130 	const int h = lineEdit()->minimumSizeHint().height();
131 	return QSize(w, h);
132 }
133--
1342.26.2
135
136