1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel IRC Development Team *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 //!\brief Construct a ChatLine object from a message.
25 * \param m The message to be layouted and rendered
26 * \param net The network name
27 * \param buf The buffer name
29 ChatLine::ChatLine(Message m) {
31 //networkName = m.buffer.network();
32 //bufferName = m.buffer.buffer();
38 ChatLine::~ChatLine() {
42 void ChatLine::formatMsg(Message msg) {
43 QString user = userFromMask(msg.sender);
44 QString host = hostFromMask(msg.sender);
45 QString nick = nickFromMask(msg.sender);
46 QString text = Style::mircToInternal(msg.text);
47 QString networkName = msg.buffer.network();
48 QString bufferName = msg.buffer.buffer();
50 QString c = tr("%DT[%1]").arg(msg.timeStamp.toLocalTime().toString("hh:mm:ss"));
54 s = tr("%DS<%1>").arg(nick); t = tr("%D0%1").arg(text); break;
56 s = tr("%Ds*"); t = tr("%Ds%1").arg(text); break;
58 s = tr("%De*"); t = tr("%De%1").arg(text); break;
60 s = tr("%Dj-->"); t = tr("%Dj%DN%DU%1%DU%DN %DH(%2@%3)%DH has joined %DC%DU%4%DU%DC").arg(nick, user, host, bufferName); break;
62 s = tr("%Dp<--"); t = tr("%Dp%DN%DU%1%DU%DN %DH(%2@%3)%DH has left %DC%DU%4%DU%DC").arg(nick, user, host, bufferName);
63 if(!text.isEmpty()) t = QString("%1 (%2)").arg(t).arg(text);
66 s = tr("%Dq<--"); t = tr("%Dq%DN%DU%1%DU%DN %DH(%2@%3)%DH has quit").arg(nick, user, host);
67 if(!text.isEmpty()) t = QString("%1 (%2)").arg(t).arg(text);
71 QString victim = text.section(" ", 0, 0);
72 //if(victim == ui.ownNick->currentText()) victim = tr("you");
73 QString kickmsg = text.section(" ", 1);
74 t = tr("%Dk%DN%DU%1%DU%DN has kicked %DN%DU%2%DU%DN from %DC%DU%3%DU%DC").arg(nick).arg(victim).arg(bufferName);
75 if(!kickmsg.isEmpty()) t = QString("%1 (%2)").arg(t).arg(kickmsg);
80 if(nick == msg.text) t = tr("%DrYou are now known as %DN%1%DN").arg(msg.text);
81 else t = tr("%Dr%DN%1%DN is now known as %DN%DU%2%DU%DN").arg(nick, msg.text);
85 if(nick.isEmpty()) t = tr("%DmUser mode: %DM%1%DM").arg(msg.text);
86 else t = tr("%DmMode %DM%1%DM by %DN%DU%2%DU%DN").arg(msg.text, nick);
90 t = tr("%Da%DN%DU%1%DU%DN %2").arg(nick).arg(msg.text);
93 s = tr("%De%1").arg(msg.sender);
94 t = tr("%De[%1]").arg(msg.text);
96 QTextOption tsOption, senderOption, textOption;
97 tsFormatted = Style::internalToFormatted(c);
98 senderFormatted = Style::internalToFormatted(s);
99 textFormatted = Style::internalToFormatted(t);
103 QList<ChatLine::FormatRange> ChatLine::calcFormatRanges(const Style::FormattedString &fs, QTextLayout::FormatRange additional) {
104 QList<FormatRange> ranges;
105 QList<QTextLayout::FormatRange> formats = fs.formats;
106 formats.append(additional);
108 FormatRange range, lastrange;
109 for(int i = 0; i < fs.text.length(); i++) {
110 QTextCharFormat format;
111 foreach(QTextLayout::FormatRange f, formats) {
112 if(i >= f.start && i < f.start + f.length) format.merge(f.format);
115 range.start = 0; range.length = 1; range.format= format;
118 if(format == range.format) range.length++;
120 QFontMetrics metrics(range.format.font());
121 range.height = metrics.lineSpacing();
122 ranges.append(range);
123 range.start = i; range.length = 1; range.format = format;
129 QFontMetrics metrics(range.format.font());
130 range.height = metrics.lineSpacing();
131 ranges.append(range);
136 void ChatLine::setSelection(SelectionMode mode, int start, int end) {
137 selectionMode = mode;
138 //tsFormat.clear(); senderFormat.clear(); textFormat.clear();
139 QPalette pal = QApplication::palette();
140 QTextLayout::FormatRange tsSel, senderSel, textSel;
143 tsFormat = calcFormatRanges(tsFormatted);
144 senderFormat = calcFormatRanges(senderFormatted);
145 textFormat = calcFormatRanges(textFormatted);
148 selectionStart = qMin(start, end); selectionEnd = qMax(start, end);
149 textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
150 textSel.format.setBackground(pal.brush(QPalette::Highlight));
151 textSel.start = selectionStart;
152 textSel.length = selectionEnd - selectionStart;
153 //textFormat.append(textSel);
154 textFormat = calcFormatRanges(textFormatted, textSel);
155 foreach(FormatRange fr, textFormat);
158 tsSel.format.setForeground(pal.brush(QPalette::HighlightedText));
159 tsSel.format.setBackground(pal.brush(QPalette::Highlight));
160 tsSel.start = 0; tsSel.length = tsFormatted.text.length();
161 tsFormat = calcFormatRanges(tsFormatted, tsSel);
162 senderSel.format.setForeground(pal.brush(QPalette::HighlightedText));
163 senderSel.format.setBackground(pal.brush(QPalette::Highlight));
164 senderSel.start = 0; senderSel.length = senderFormatted.text.length();
165 senderFormat = calcFormatRanges(senderFormatted, senderSel);
166 textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
167 textSel.format.setBackground(pal.brush(QPalette::Highlight));
168 textSel.start = 0; textSel.length = textFormatted.text.length();
169 textFormat = calcFormatRanges(textFormatted, textSel);
174 uint ChatLine::msgId() const {
175 return msg.buffer.uid();
178 BufferId ChatLine::bufferId() const {
182 QDateTime ChatLine::timeStamp() const {
183 return msg.timeStamp;
186 QString ChatLine::sender() const {
187 return senderFormatted.text;
190 QString ChatLine::text() const {
191 return textFormatted.text;
194 bool ChatLine::isUrl(int c) const {
195 if(c < 0 || c >= charUrlIdx.count()) return false;;
196 return charUrlIdx[c] >= 0;
199 QUrl ChatLine::getUrl(int c) const {
200 if(c < 0 || c >= charUrlIdx.count()) return QUrl();
201 int i = charUrlIdx[c];
202 if(i >= 0) return textFormatted.urls[i].url;
206 //!\brief Return the cursor position for the given coordinate pos.
208 * \param pos The position relative to the ChatLine
209 * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
211 int ChatLine::posToCursor(QPointF pos) {
212 if(pos.x() < tsWidth + (int)Style::sepTsSender()/2) return -2;
213 qreal textStart = tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText();
214 if(pos.x() < textStart) return -1;
215 int x = (int)(pos.x() - textStart);
216 for(int l = lineLayouts.count() - 1; l >=0; l--) {
217 LineLayout line = lineLayouts[l];
218 if(pos.y() >= line.y) {
219 int offset = charPos[line.start]; x += offset;
220 for(int i = line.start + line.length - 1; i >= line.start; i--) {
221 if((charPos[i] + charPos[i+1])/2 <= x) return i+1; // FIXME: Optimize this!
226 qWarning() << "Should we ever reach this point?";
230 void ChatLine::precomputeLine() {
231 tsFormat = calcFormatRanges(tsFormatted);
232 senderFormat = calcFormatRanges(senderFormatted);
233 textFormat = calcFormatRanges(textFormatted);
236 foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
237 foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
240 charPos.resize(textFormatted.text.length() + 1);
241 charHeights.resize(textFormatted.text.length());
242 charUrlIdx.fill(-1, textFormatted.text.length());
243 for(int i = 0; i < textFormatted.urls.count(); i++) {
244 Style::UrlInfo url = textFormatted.urls[i];
245 for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i;
247 if(!textFormat.count()) return;
248 int idx = 0; int cnt = 0; int w = 0;
249 QFontMetrics metrics(textFormat[0].format.font());
251 wr.start = -1; wr.trailing = -1;
252 for(int i = 0; i < textFormatted.text.length(); ) {
253 charPos[i] = w; charHeights[i] = textFormat[idx].height;
254 w += metrics.charWidth(textFormatted.text, i);
255 if(!textFormatted.text[i].isSpace()) {
256 if(wr.trailing >= 0) {
257 // new word after space
262 wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
264 wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
268 wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
273 if(++i < textFormatted.text.length() && ++cnt >= textFormat[idx].length) {
275 Q_ASSERT(idx < textFormat.count());
276 metrics = QFontMetrics(textFormat[idx].format.font());
279 charPos[textFormatted.text.length()] = w;
280 if(wr.start >= 0) words.append(wr);
283 qreal ChatLine::layout(qreal tsw, qreal senderw, qreal textw) {
284 tsWidth = tsw; senderWidth = senderw; textWidth = textw;
285 if(textw <= 0) return minHeight;
286 lineLayouts.clear(); LineLayout line;
288 int offset = 0; int numWords = 0;
291 line.height = minHeight; // first line needs room for ts and sender
292 for(int i = 0; i < words.count(); i++) {
293 int lastpos = charPos[words[i].start + words[i].length]; // We use charPos[lastchar + 1], 'coz last char needs to fit
294 if(lastpos - offset <= textw) {
295 line.height = qMax(line.height, words[i].height);
296 line.length = words[i].start + words[i].length - line.start;
301 // ok, we had some words before, so store the layout and start a new line
303 line.length = words[i-1].start + words[i-1].length - line.start;
304 lineLayouts.append(line);
305 line.y += line.height;
306 line.start = words[i].start;
307 line.height = words[i].height;
308 offset = charPos[words[i].start];
311 // check if the word fits into the current line
312 if(lastpos - offset <= textw) {
313 line.length = words[i].length;
315 // we need to break a word in the middle
316 int border = (int)textw + offset; // save some additions
317 line.start = words[i].start;
319 line.height = charHeights[line.start];
320 int j = line.start + 1;
321 for(int l = 1; l < words[i].length; j++, l++) {
322 if(charPos[j+1] < border) {
324 line.height = qMax(line.height, charHeights[j]);
328 lineLayouts.append(line);
329 line.y += line.height;
331 line.height = charHeights[j];
334 border = (int)textw + offset;
342 lineLayouts.append(line);
348 //!\brief Draw ChatLine on the given QPainter at the given position.
349 void ChatLine::draw(QPainter *p, const QPointF &pos) {
350 QPalette pal = QApplication::palette();
352 if(selectionMode == Full) {
353 p->setPen(Qt::NoPen);
354 p->setBrush(pal.brush(QPalette::Highlight));
355 p->drawRect(QRectF(pos, QSizeF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText() + textWidth, height())));
356 } else if(selectionMode == Partial) {
359 p->setClipRect(QRectF(pos, QSizeF(tsWidth, height())));
360 tsLayout.draw(p, pos, tsFormat);
361 p->setClipRect(QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, height())));
362 senderLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender(), 0), senderFormat);
363 p->setClipping(false);
364 textLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0), textFormat);
366 //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
367 //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
368 p->setBackgroundMode(Qt::OpaqueMode);
370 QRectF rect(pos, QSizeF(tsWidth, minHeight));
372 foreach(FormatRange fr, tsFormat) {
373 p->setFont(fr.format.font());
374 p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
375 p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, tsFormatted.text.mid(fr.start, fr.length), &brect);
376 rect.setLeft(brect.right());
378 rect = QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, minHeight));
379 for(int i = senderFormat.count() - 1; i >= 0; i--) {
380 FormatRange fr = senderFormat[i];
381 p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
382 p->drawText(rect, Qt::AlignRight|Qt::TextSingleLine, senderFormatted.text.mid(fr.start, fr.length), &brect);
383 rect.setRight(brect.left());
385 QPointF tpos = pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0);
386 qreal h = 0; int l = 0;
387 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
389 foreach(FormatRange fr, textFormat) {
390 if(l >= lineLayouts.count()) break;
391 p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
392 int start, end, frend, llend;
394 frend = fr.start + fr.length;
395 if(frend <= lineLayouts[l].start) break;
396 llend = lineLayouts[l].start + lineLayouts[l].length;
397 start = qMax(fr.start, lineLayouts[l].start); end = qMin(frend, llend);
398 rect.setLeft(tpos.x() + charPos[start] - offset);
399 p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, textFormatted.text.mid(start, end - start), &brect);
401 h += lineLayouts[l].height;
403 if(l < lineLayouts.count()) {
404 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
405 offset = charPos[lineLayouts[l].start];
408 } while(end < frend && l < lineLayouts.count());