+void ChatItem::layout() {
+ if(_layout) return;
+ _layout = new QTextLayout(data(MessageModel::DisplayRole).toString());
+
+ // Convert format information into a FormatRange
+ QList<QTextLayout::FormatRange> formatRanges;
+ UiStyle::FormatList formatList = data(MessageModel::FormatRole).value<UiStyle::FormatList>();
+ QTextLayout::FormatRange range;
+ int i = 0;
+ for(i = 0; i < formatList.count(); i++) {
+ range.format = QtUi::style()->mergedFormat(formatList.at(i).second);
+ range.start = formatList.at(i).first;
+ if(i > 0) formatRanges.last().length = range.start - formatRanges.last().start;
+ formatRanges.append(range);
+ }
+ if(i > 0) formatRanges.last().length = _layout->text().length() - formatRanges.last().start;
+ _layout->setAdditionalFormats(formatRanges);
+
+ // Now layout
+ qreal h = 0;
+ _layout->beginLayout();
+ forever {
+ QTextLine line = _layout->createLine();
+ if (!line.isValid())
+ break;
+
+ line.setLineWidth(width());
+ h += _lineLeading;
+ line.setPosition(QPointF(0, h));
+ h += line.height();
+ }
+ _layout->endLayout();