+QUrl ChatLine::getUrl(int c) {
+ if(c < 0 || c >= charUrlIdx.count()) return QUrl();
+ int i = charUrlIdx[c];
+ if(i >= 0) return textFormatted.urls[i].url;
+ else return QUrl();
+}
+
+//!\brief Return the cursor position for the given coordinate pos.
+/**
+ * \param pos The position relative to the ChatLine
+ * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
+ */
+int ChatLine::posToCursor(QPointF pos) {
+ if(pos.x() < tsWidth + (int)Style::sepTsSender()/2) return -2;
+ qreal textStart = tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText();
+ if(pos.x() < textStart) return -1;
+ int x = (int)(pos.x() - textStart);
+ for(int l = lineLayouts.count() - 1; l >=0; l--) {
+ LineLayout line = lineLayouts[l];
+ if(pos.y() >= line.y) {
+ int offset = charPos[line.start]; x += offset;
+ for(int i = line.start + line.length - 1; i >= line.start; i--) {
+ if((charPos[i] + charPos[i+1])/2 <= x) return i+1; // FIXME: Optimize this!
+ }
+ return line.start;
+ }
+ }
+}
+
+void ChatLine::precomputeLine() {
+ tsFormat = calcFormatRanges(tsFormatted);
+ senderFormat = calcFormatRanges(senderFormatted);
+ textFormat = calcFormatRanges(textFormatted);
+
+ minHeight = 0;
+ foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
+ foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
+
+ words.clear();
+ charPos.resize(textFormatted.text.length() + 1);
+ charHeights.resize(textFormatted.text.length());
+ charUrlIdx.fill(-1, textFormatted.text.length());
+ for(int i = 0; i < textFormatted.urls.count(); i++) {
+ Style::UrlInfo url = textFormatted.urls[i];
+ for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i;
+ }
+ if(!textFormat.count()) return;
+ int idx = 0; int cnt = 0; int w = 0; int h = 0;
+ QFontMetrics metrics(textFormat[0].format.font());
+ Word wr;
+ wr.start = -1; wr.trailing = -1;
+ for(int i = 0; i < textFormatted.text.length(); ) {
+ charPos[i] = w; charHeights[i] = textFormat[idx].height;
+ w += metrics.charWidth(textFormatted.text, i);
+ if(!textFormatted.text[i].isSpace()) {
+ if(wr.trailing >= 0) {
+ // new word after space
+ words.append(wr);
+ wr.start = -1;
+ }
+ if(wr.start < 0) {
+ wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
+ } else {
+ wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
+ }
+ } else {
+ if(wr.start < 0) {
+ wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
+ } else {
+ wr.trailing++;
+ }
+ }
+ if(++i < textFormatted.text.length() && ++cnt >= textFormat[idx].length) {
+ cnt = 0; idx++;
+ Q_ASSERT(idx < textFormat.count());
+ metrics = QFontMetrics(textFormat[idx].format.font());
+ }
+ }
+ charPos[textFormatted.text.length()] = w;
+ if(wr.start >= 0) words.append(wr);