+void ChatItem::updateLayout() {
+ switch(data(ChatLineModel::ColumnTypeRole).toUInt()) {
+ case ChatLineModel::TimestampColumn:
+ if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignLeft);
+ // fallthrough
+ case ChatLineModel::SenderColumn:
+ if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere, Qt::AlignRight);
+ _layout->beginLayout();
+ {
+ QTextLine line = _layout->createLine();
+ if(line.isValid()) {
+ line.setLineWidth(width());
+ line.setPosition(QPointF(0,0));
+ }
+ _layout->endLayout();
+ }
+ break;
+ case ChatLineModel::ContentsColumn: {
+ if(!haveLayout()) _layout = createLayout(QTextOption::WrapAnywhere);
+
+ // Now layout
+ ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
+ if(!wrapList.count()) return; // empty chatitem
+
+ qreal h = 0;
+ WrapColumnFinder finder(this);
+ _layout->beginLayout();
+ forever {
+ QTextLine line = _layout->createLine();
+ if(!line.isValid())
+ break;
+
+ int col = finder.nextWrapColumn();
+ line.setNumColumns(col >= 0 ? col - line.textStart() : _layout->text().length());
+ line.setPosition(QPointF(0, h));
+ h += line.height() + fontMetrics()->leading();
+ }
+ _layout->endLayout();
+ }
+ break;
+ }