From 3dd76d9373fb46b7be3f7f963b3d3a38ded63ae5 Mon Sep 17 00:00:00 2001 From: Janne Koschinski Date: Mon, 5 Aug 2019 12:34:46 +0200 Subject: [PATCH] Replace deprecated additionalFormats with formats --- src/qtui/chatitem.cpp | 19 ++++++++++--------- src/qtui/chatlinemodelitem.cpp | 2 +- src/uisupport/styledlabel.cpp | 8 +++----- src/uisupport/uistyle.cpp | 4 ++-- src/uisupport/uistyle.h | 24 +++++++++++++++++++++++- 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/qtui/chatitem.cpp b/src/qtui/chatitem.cpp index 1f4897a7..913c81e7 100644 --- a/src/qtui/chatitem.cpp +++ b/src/qtui/chatitem.cpp @@ -142,11 +142,12 @@ void ChatItem::initLayoutHelper(QTextLayout* layout, QTextOption::WrapMode wrapM option.setAlignment(alignment); layout->setTextOption(option); - QList formatRanges = QtUi::style() - ->toTextLayoutList(formatList(), - layout->text().length(), - data(ChatLineModel::MsgLabelRole).value()); - layout->setAdditionalFormats(formatRanges); + UiStyle::FormatContainer formatRanges = QtUi::style()->toTextLayoutList( + formatList(), + layout->text().length(), + data(ChatLineModel::MsgLabelRole).value() + ); + UiStyle::setTextLayoutFormats(*layout, formatRanges); } void ChatItem::initLayout(QTextLayout* layout) const @@ -347,16 +348,16 @@ QVector ChatItem::additionalFormats() const } // Add all formats that have an extra label to the additionalFormats list - QList 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 diff --git a/src/qtui/chatlinemodelitem.cpp b/src/qtui/chatlinemodelitem.cpp index a8e1ae03..9ebc1264 100644 --- a/src/qtui/chatlinemodelitem.cpp +++ b/src/qtui/chatlinemodelitem.cpp @@ -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); diff --git a/src/uisupport/styledlabel.cpp b/src/uisupport/styledlabel.cpp index 78973fc5..f54bea81 100644 --- a/src/uisupport/styledlabel.cpp +++ b/src/uisupport/styledlabel.cpp @@ -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 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(); diff --git a/src/uisupport/uistyle.cpp b/src/uisupport/uistyle.cpp index 6e0291c1..19f6e165 100644 --- a/src/uisupport/uistyle.cpp +++ b/src/uisupport/uistyle.cpp @@ -583,9 +583,9 @@ QString UiStyle::formatCode(FormatType ftype) return _formatCodes.key(ftype); } -QList UiStyle::toTextLayoutList(const FormatList& formatList, int textLength, MessageLabel messageLabel) const +UiStyle::FormatContainer UiStyle::toTextLayoutList(const FormatList& formatList, int textLength, MessageLabel messageLabel) const { - QList formatRanges; + UiStyle::FormatContainer formatRanges; QTextLayout::FormatRange range; size_t i = 0; for (i = 0; i < formatList.size(); i++) { diff --git a/src/uisupport/uistyle.h b/src/uisupport/uistyle.h index d1308fd4..888abcde 100644 --- a/src/uisupport/uistyle.h +++ b/src/uisupport/uistyle.h @@ -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; + static inline void setTextLayoutFormats(QTextLayout& layout, const FormatContainer& formats) { + layout.setFormats(formats); + } + static inline QVector containerToVector(const FormatContainer& container) { + return container; + } +#else + using FormatContainer = QList; + static inline void setTextLayoutFormats(QTextLayout& layout, const FormatContainer& formats) { + layout.setAdditionalFormats(formats); + } + static inline QVector 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 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; } -- 2.20.1