-void ChatItem::layout() {
- if(!_layout.additionalFormats().count()) return; // no text set
- if(_width <= 0) return;
- prepareGeometryChange();
- QFontMetrics metrics(_layout.additionalFormats()[0].format.font());
- int leading = metrics.leading();
- int height = 0;
- _layout.setTextOption(textOption());
- _layout.beginLayout();
- while(1) {
- QTextLine line = _layout.createLine();
- if(!line.isValid()) break;
- line.setLineWidth(_width);
- if(textOption().wrapMode() != QTextOption::NoWrap && line.naturalTextWidth() > _width) {
- // word did not fit, we need to wrap it in the middle
- // this is a workaround for Qt failing to handle WrapAtWordBoundaryOrAnywhere correctly
- QTextOption::WrapMode mode = textOption().wrapMode();
- textOption().setWrapMode(QTextOption::WrapAnywhere);
- _layout.setTextOption(textOption());
- line.setLineWidth(_width);
- textOption().setWrapMode(mode);
- _layout.setTextOption(textOption());
+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, fontMetrics()->leading()));
+ }
+ _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
+ int wordidx = 0;
+ ChatLineModel::Word word = wrapList.at(0);
+
+ 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());
+
+ h += fontMetrics()->leading();
+ line.setPosition(QPointF(0, h));
+ h += line.height();
+ }
+ _layout->endLayout();