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
}
// 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
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);
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;
}
_layout.setText(sstr.plainText);
- _layout.setAdditionalFormats(layoutList);
+ UiStyle::setTextLayoutFormats(_layout, layoutList);
layout();
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++) {
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.
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; }