Replace deprecated additionalFormats with formats
authorJanne Koschinski <janne@kuschku.de>
Mon, 5 Aug 2019 10:34:46 +0000 (12:34 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Wed, 28 Aug 2019 19:45:23 +0000 (21:45 +0200)
src/qtui/chatitem.cpp
src/qtui/chatlinemodelitem.cpp
src/uisupport/styledlabel.cpp
src/uisupport/uistyle.cpp
src/uisupport/uistyle.h

index 1f4897a..913c81e 100644 (file)
@@ -142,11 +142,12 @@ void ChatItem::initLayoutHelper(QTextLayout* layout, QTextOption::WrapMode wrapM
     option.setAlignment(alignment);
     layout->setTextOption(option);
 
-    QList<QTextLayout::FormatRange> formatRanges = QtUi::style()
-                                                       ->toTextLayoutList(formatList(),
-                                                                          layout->text().length(),
-                                                                          data(ChatLineModel::MsgLabelRole).value<UiStyle::MessageLabel>());
-    layout->setAdditionalFormats(formatRanges);
+    UiStyle::FormatContainer formatRanges = QtUi::style()->toTextLayoutList(
+        formatList(),
+        layout->text().length(),
+        data(ChatLineModel::MsgLabelRole).value<UiStyle::MessageLabel>()
+    );
+    UiStyle::setTextLayoutFormats(*layout, formatRanges);
 }
 
 void ChatItem::initLayout(QTextLayout* layout) const
@@ -347,16 +348,16 @@ QVector<QTextLayout::FormatRange> ChatItem::additionalFormats() const
     }
 
     // Add all formats that have an extra label to the additionalFormats list
-    QList<QTextLayout::FormatRange> additionalFormats;
+    UiStyle::FormatContainer additionalFormats;
     for (size_t i = 0; i < labelFmtList.size() - 1; ++i) {
         if (labelFmtList[i].label != itemLabel) {
             additionalFormats << QtUi::style()->toTextLayoutList({std::make_pair(labelFmtList[i].offset, labelFmtList[i].format)},
-                                                                 labelFmtList[i + 1].offset,
-                                                                 labelFmtList[i].label);
+                                                                   labelFmtList[i + 1].offset,
+                                                                   labelFmtList[i].label);
         }
     }
 
-    return additionalFormats.toVector();
+    return UiStyle::containerToVector(additionalFormats);
 }
 
 bool ChatItem::hasSelection() const
index a8e1ae0..9ebc126 100644 (file)
@@ -200,7 +200,7 @@ void ChatLineModelItem::computeWrapList() const
     option.setWrapMode(QTextOption::NoWrap);
     layout.setTextOption(option);
 
-    layout.setAdditionalFormats(QtUi::style()->toTextLayoutList(_styledMsg.contentsFormatList(), length, messageLabel()));
+    UiStyle::setTextLayoutFormats(layout, QtUi::style()->toTextLayoutList(_styledMsg.contentsFormatList(), length, messageLabel()));
     layout.beginLayout();
     QTextLine line = layout.createLine();
     line.setNumColumns(length);
index 78973fc..f54bea8 100644 (file)
@@ -110,10 +110,8 @@ void StyledLabel::updateSizeHint()
 
 void StyledLabel::setText(const QString& text)
 {
-    UiStyle* style = GraphicalUi::uiStyle();
-
-    UiStyle::StyledString sstr = style->styleString(style->mircToInternal(text), UiStyle::FormatType::PlainMsg);
-    QList<QTextLayout::FormatRange> layoutList = style->toTextLayoutList(sstr.formatList, sstr.plainText.length(), UiStyle::MessageLabel::None);
+    UiStyle::StyledString sstr = UiStyle::styleString(UiStyle::mircToInternal(text), UiStyle::FormatType::PlainMsg);
+    UiStyle::FormatContainer layoutList = GraphicalUi::uiStyle()->toTextLayoutList(sstr.formatList, sstr.plainText.length(), UiStyle::MessageLabel::None);
 
     // Use default font rather than the style's
     QTextLayout::FormatRange fmtRange;
@@ -135,7 +133,7 @@ void StyledLabel::setText(const QString& text)
     }
 
     _layout.setText(sstr.plainText);
-    _layout.setAdditionalFormats(layoutList);
+    UiStyle::setTextLayoutFormats(_layout, layoutList);
 
     layout();
 
index 6e0291c..19f6e16 100644 (file)
@@ -583,9 +583,9 @@ QString UiStyle::formatCode(FormatType ftype)
     return _formatCodes.key(ftype);
 }
 
-QList<QTextLayout::FormatRange> UiStyle::toTextLayoutList(const FormatList& formatList, int textLength, MessageLabel messageLabel) const
+UiStyle::FormatContainer UiStyle::toTextLayoutList(const FormatList& formatList, int textLength, MessageLabel messageLabel) const
 {
-    QList<QTextLayout::FormatRange> formatRanges;
+    UiStyle::FormatContainer formatRanges;
     QTextLayout::FormatRange range;
     size_t i = 0;
     for (i = 0; i < formatList.size(); i++) {
index d1308fd..888abcd 100644 (file)
@@ -49,6 +49,28 @@ public:
     UiStyle(QObject* parent = nullptr);
     ~UiStyle() override;
 
+// For backwards compatibility with Qt 5.5, the setFormats method was introduced
+// in Qt 5.6, but the old setAdditionalFormats was deprecated in 5.6 as well.
+//
+// So we use the old one on Qt 5.5, and the new one everywhere else.
+#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
+    using FormatContainer = QVector<QTextLayout::FormatRange>;
+    static inline void setTextLayoutFormats(QTextLayout& layout, const FormatContainer& formats) {
+        layout.setFormats(formats);
+    }
+    static inline QVector<QTextLayout::FormatRange> containerToVector(const FormatContainer& container) {
+        return container;
+    }
+#else
+    using FormatContainer = QList<QTextLayout::FormatRange>;
+    static inline void setTextLayoutFormats(QTextLayout& layout, const FormatContainer& formats) {
+        layout.setAdditionalFormats(formats);
+    }
+    static inline QVector<QTextLayout::FormatRange> containerToVector(const FormatContainer& container) {
+        return container.toVector();
+    }
+#endif
+
     //! This enumerates the possible formats a text element may have. */
     /** These formats are ordered on increasing importance, in cases where a given property is specified
      *  by multiple active formats.
@@ -275,7 +297,7 @@ public:
     QTextCharFormat format(const Format& format, MessageLabel messageLabel) const;
     QFontMetricsF* fontMetrics(FormatType formatType, MessageLabel messageLabel) const;
 
-    QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList&, int textLength, MessageLabel messageLabel) const;
+    FormatContainer toTextLayoutList(const FormatList&, int textLength, MessageLabel messageLabel) const;
 
     inline const QBrush& brush(ColorRole role) const { return _uiStylePalette.at((int)role); }
     inline void setBrush(ColorRole role, const QBrush& brush) { _uiStylePalette[(int)role] = brush; }