+int ChatItem::heightForWidth(int width) {
+ if(data(ChatLineModel::ColumnTypeRole).toUInt() != ChatLineModel::ContentsColumn)
+ return _lineHeight; // only contents can be multi-line
+
+ ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
+ int lines = 1;
+ qreal w = 0;
+ for(int i = 0; i < wrapList.count(); i++) {
+ w += wrapList.at(i).width;
+ if(w <= width) {
+ w += wrapList.at(i).trailing;
+ continue;
+ }
+ lines++;
+ w = wrapList.at(i).width;
+ while(w >= width) {
+ lines++;
+ w -= width;
+ }
+ }
+ return lines * _lineHeight;
+}
+
+void ChatItem::layout() {
+ if(haveLayout()) 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);
+ updateLayout();