+void ChatItem::updateLayout() {
+ if(!haveLayout()) layout();
+
+ // Now layout
+ ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
+ if(!wrapList.count()) return; // empty chatitem
+ int wordidx = 0;
+ ChatLineModel::Word word = wrapList.at(0);
+
+ qreal h = 0;
+ _layout->beginLayout();
+ forever {
+ QTextLine line = _layout->createLine();
+ if (!line.isValid())
+ break;
+
+ if(word.width >= width()) {
+ line.setLineWidth(width());
+ word.width -= line.naturalTextWidth();
+ word.start = line.textStart() + line.textLength();
+ } else {
+ int w = 0;
+ while((w += word.width) <= width() && wordidx < wrapList.count()) {
+ w += word.trailing;
+ if(++wordidx < wrapList.count()) word = wrapList.at(wordidx);
+ else {
+ // last word (and it fits), but if we expected an extra line, wrap anyway here
+ // yeah, this is cheating, but much cheaper than computing widths ourself
+ if(_layout->lineCount() < _lines) {
+ wordidx--;
+ break;
+ }
+ }
+ }
+ int lastcol = wordidx < wrapList.count() ? word.start : _layout->text().length();
+ line.setNumColumns(lastcol - line.textStart());// qDebug() << "setting cols:" << lastcol - line.textStart();
+ }