- QVariantList wrapList = data(ChatLineModel::WrapListRole).toList();
- int lines = 1;
- int offset = 0;
- for(int i = 0; i < wrapList.count(); i+=2) {
- if(wrapList.at(i+1).toUInt() - offset < width) continue;
- lines++;
- if(i > 0) {
- if(offset < wrapList.at(i-1).toUInt()) offset = wrapList.at(i-1).toUInt();
- else offset += width;
- } else {
- offset += width;
+ _lines = 1;
+ WrapColumnFinder finder(this);
+ while(finder.nextWrapColumn() > 0) _lines++;
+ return _lines * fontMetrics()->lineSpacing();
+}
+
+ChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) : item(_item) {
+ wrapList = item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
+ wordidx = 0;
+ layout = 0;
+ lastwrapcol = 0;
+ lastwrappos = 0;
+ w = 0;
+}
+
+ChatItem::WrapColumnFinder::~WrapColumnFinder() {
+ delete layout;
+}
+
+int ChatItem::WrapColumnFinder::nextWrapColumn() {
+ while(wordidx < wrapList.count()) {
+ w += wrapList.at(wordidx).width;
+ if(w >= item->width()) {
+ if(lastwrapcol >= wrapList.at(wordidx).start) {
+ // first word, and it doesn't fit
+ if(!line.isValid()) {
+ layout = item->createLayout(QTextOption::NoWrap);
+ layout->beginLayout();
+ line = layout->createLine();
+ line.setLineWidth(item->width());
+ layout->endLayout();
+ }
+ int idx = line.xToCursor(lastwrappos + item->width(), QTextLine::CursorOnCharacter);
+ qreal x = line.cursorToX(idx, QTextLine::Trailing);
+ w = w - wrapList.at(wordidx).width - (x - lastwrappos);
+ lastwrappos = x;
+ lastwrapcol = idx;
+ return idx;
+ }
+ // not the first word, so just wrap before this
+ lastwrapcol = wrapList.at(wordidx).start;
+ lastwrappos = lastwrappos + w - wrapList.at(wordidx).width;
+ w = 0;
+ return lastwrapcol;