- if(w == width()) {
- //qDebug() << Q_FUNC_INFO << "Geometry change requested with identical width!";
- }
- // We use this for reloading layout info as well
- //if(w != width()) {
- prepareGeometryChange();
- setWidth(w);
- QTextLayout layout;
- initLayout(&layout);
- setHeight(layout.boundingRect().height());
- delete _data;
- _data = 0;
- //}
- return height();
-}
-
-// NOTE: This method is not threadsafe and not reentrant!
-// (RegExps are not constant while matching, and they are static here for efficiency)
-QList<ContentsChatItem::Clickable> ContentsChatItem::findClickables() const {
- // For matching URLs
- static QString urlEnd("(?:>|[,.;:\"]*\\s|\\b|$)");
- static QString urlChars("(?:[,.;:]*[\\w\\-~@/?&=+$()!%#*|{}\\[\\]'^])");
-
- static QRegExp regExp[] = {
- // URL
- // QRegExp(QString("((?:https?://|s?ftp://|irc://|mailto:|www\\.)%1+|%1+\\.[a-z]{2,4}(?:?=/%1+|\\b))%2").arg(urlChars, urlEnd)),
- QRegExp(QString("((?:(?:mailto:|\\w+://)|www\\.)%1+)%2").arg(urlChars, urlEnd), Qt::CaseInsensitive),
-
- // Channel name
- // We don't match for channel names starting with + or &, because that gives us a lot of false positives.
- QRegExp("((?:#|![A-Z0-9]{5})[^,:\\s]+(?::[^,:\\s]+)?)\\b", Qt::CaseInsensitive)
-
- // TODO: Nicks, we'll need a filtering for only matching known nicknames further down if we do this
- };
-
- static const int regExpCount = 2; // number of regexps in the array above
-
- qint16 matches[] = { 0, 0, 0 };
- qint16 matchEnd[] = { 0, 0, 0 };
-
- QString str = data(ChatLineModel::DisplayRole).toString();
-
- QList<Clickable> result;
- qint16 idx = 0;
- qint16 minidx;
- int type = -1;
-
- do {
- type = -1;
- minidx = str.length();
- for(int i = 0; i < regExpCount; i++) {
- if(matches[i] < 0 || matchEnd[i] > str.length()) continue;
- if(idx >= matchEnd[i]) {
- matches[i] = regExp[i].indexIn(str, qMax(matchEnd[i], idx));
- if(matches[i] >= 0) matchEnd[i] = matches[i] + regExp[i].cap(1).length();
- }
- if(matches[i] >= 0 && matches[i] < minidx) {
- minidx = matches[i];
- type = i;
- }
+ // We use this for reloading layout info as well, so we can't bail out if the width doesn't change
+
+ // compute height
+ int lines = 1;
+ WrapColumnFinder finder(this);
+ while(finder.nextWrapColumn(w) > 0)
+ lines++;
+ qreal spacing = qMax(fontMetrics()->lineSpacing(), fontMetrics()->height()); // cope with negative leading()
+ qreal h = lines * spacing;
+ delete _data;
+ _data = 0;
+
+ if(w != width() || h != height())
+ setGeometry(w, h);
+
+ return h;
+}
+
+void ContentsChatItem::initLayout(QTextLayout *layout) const {
+ initLayoutHelper(layout, QTextOption::WrapAtWordBoundaryOrAnywhere);
+ doLayout(layout);
+}
+
+void ContentsChatItem::doLayout(QTextLayout *layout) const {
+ ChatLineModel::WrapList wrapList = data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
+ if(!wrapList.count()) return; // empty chatitem
+
+ qreal h = 0;
+ qreal spacing = qMax(fontMetrics()->lineSpacing(), fontMetrics()->height()); // cope with negative leading()
+ WrapColumnFinder finder(this);
+ layout->beginLayout();
+ forever {
+ QTextLine line = layout->createLine();
+ if(!line.isValid())
+ break;
+
+ int col = finder.nextWrapColumn(width());
+ if(col < 0)
+ col = layout->text().length();
+ int num = col - line.textStart();
+
+ line.setNumColumns(num);
+
+ // Sometimes, setNumColumns will create a line that's too long (cf. Qt bug 238249)
+ // We verify this and try setting the width again, making it shorter each time until the lengths match.
+ // Dead fugly, but seems to work…
+ for(int i = line.textLength()-1; i >= 0 && line.textLength() > num; i--) {
+ line.setNumColumns(i);