-bool ChatLine::isUrl(int c) const {
- if(c < 0 || c >= charUrlIdx.count()) return false;;
- return charUrlIdx[c] >= 0;
-}
-
-QUrl ChatLine::getUrl(int c) const {
- if(c < 0 || c >= charUrlIdx.count()) return QUrl();
- int i = charUrlIdx[c];
- if(i >= 0) return styledText.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)QtUi::style()->sepTsSender()/2) return -2;
- qreal textStart = tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::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;
- }
- }
- return 0;
-}
-
-void ChatLine::precomputeLine() {
- tsFormat = calcFormatRanges(styledTimeStamp);
- senderFormat = calcFormatRanges(styledSender);
- textFormat = calcFormatRanges(styledText);
-
- minHeight = 0;
- foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
- foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
-
- words.clear();
- charPos.resize(styledText.text.length() + 1);
- charHeights.resize(styledText.text.length());
- charUrlIdx.fill(-1, styledText.text.length());
- for(int i = 0; i < styledText.urls.count(); i++) {
- QtUiStyle::UrlInfo url = styledText.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;
- QFontMetrics metrics(textFormat[0].format.font());
- Word wr;
- wr.start = -1; wr.trailing = -1;
- for(int i = 0; i < styledText.text.length(); ) {
- charPos[i] = w; charHeights[i] = textFormat[idx].height;
- w += metrics.charWidth(styledText.text, i);
- if(!styledText.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++;
- }