1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
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) version 3. *
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 ***************************************************************************/
21 #include "chatline-old.h"
26 //! Construct a ChatLine object from a message.
28 * \param m The message to be layouted and rendered
30 ChatLine::ChatLine(Message m) {
32 //networkName = m.buffer.network();
33 //bufferName = m.buffer.buffer();
40 ChatLine::~ChatLine() {
44 void ChatLine::formatMsg(Message msg) {
45 isHighlight = msg.flags() & Message::Highlight;
46 QTextOption tsOption, senderOption, textOption;
47 styledTimeStamp = QtUi::style()->styleString(msg.formattedTimestamp());
48 styledSender = QtUi::style()->styleString(msg.formattedSender());
49 styledText = QtUi::style()->styleString(msg.formattedText());
53 // This function is almost obsolete, since with the new style engine, we already get a list of formats...
54 // We don't know yet if we keep this implementation of ChatLine, so I won't bother making this actually nice.
55 QList<ChatLine::FormatRange> ChatLine::calcFormatRanges(UiStyle::StyledText fs, QTextLayout::FormatRange additional) {
56 QList<FormatRange> ranges;
58 if(additional.length > 0) {
59 for(int i = 0; i < fs.formats.count(); i++) {
60 int oldend = fs.formats[i].start + fs.formats[i].length - 1;
61 int addend = additional.start + additional.length - 1;
62 if(oldend < additional.start) continue;
63 fs.formats[i].length = additional.start - fs.formats[i].start;
64 QTextLayout::FormatRange addfmtrng = fs.formats[i];
65 addfmtrng.format.merge(additional.format);
66 addfmtrng.start = additional.start;
67 addfmtrng.length = qMin(oldend, addend) - additional.start + 1;
68 fs.formats.insert(++i, addfmtrng);
69 if(addend == oldend) break;
71 QTextLayout::FormatRange restfmtrng = fs.formats[i-1];
72 restfmtrng.start = addend + 1;
73 restfmtrng.length = oldend - addend;
74 fs.formats.insert(++i, restfmtrng);
79 foreach(QTextLayout::FormatRange f, fs.formats) {
80 if(f.length <= 0) continue;
82 range.start = f.start;
83 range.length = f.length;
84 range.format = f.format;
85 QFontMetrics metrics(range.format.font());
86 range.height = metrics.lineSpacing();
92 void ChatLine::setSelection(SelectionMode mode, int start, int end) {
94 //tsFormat.clear(); senderFormat.clear(); textFormat.clear();
95 QPalette pal = QApplication::palette();
96 QTextLayout::FormatRange tsSel, senderSel, textSel;
99 tsFormat = calcFormatRanges(styledTimeStamp);
100 senderFormat = calcFormatRanges(styledSender);
101 textFormat = calcFormatRanges(styledText);
104 selectionStart = qMin(start, end); selectionEnd = qMax(start, end);
105 textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
106 textSel.format.setBackground(pal.brush(QPalette::Highlight));
107 textSel.start = selectionStart;
108 textSel.length = selectionEnd - selectionStart;
109 textFormat = calcFormatRanges(styledText, textSel);
112 tsSel.format.setForeground(pal.brush(QPalette::HighlightedText));
113 tsSel.format.setBackground(pal.brush(QPalette::Highlight));
114 tsSel.start = 0; tsSel.length = styledTimeStamp.text.length();
115 tsFormat = calcFormatRanges(styledTimeStamp, tsSel);
116 senderSel.format.setForeground(pal.brush(QPalette::HighlightedText));
117 senderSel.format.setBackground(pal.brush(QPalette::Highlight));
118 senderSel.start = 0; senderSel.length = styledSender.text.length();
119 senderFormat = calcFormatRanges(styledSender, senderSel);
120 textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
121 textSel.format.setBackground(pal.brush(QPalette::Highlight));
122 textSel.start = 0; textSel.length = styledText.text.length();
123 textFormat = calcFormatRanges(styledText, textSel);
128 MsgId ChatLine::msgId() const {
132 BufferInfo ChatLine::bufferInfo() const {
133 return msg.bufferInfo();
136 QDateTime ChatLine::timestamp() const {
137 return msg.timestamp();
140 QString ChatLine::sender() const {
141 return styledSender.text;
144 QString ChatLine::text() const {
145 return styledText.text;
148 bool ChatLine::isUrl(int c) const {
149 if(c < 0 || c >= charUrlIdx.count()) return false;;
150 return charUrlIdx[c] >= 0;
153 QUrl ChatLine::getUrl(int c) const {
154 if(c < 0 || c >= charUrlIdx.count()) return QUrl();
155 int i = charUrlIdx[c];
156 if(i >= 0) return styledText.urls[i].url;
160 //!\brief Return the cursor position for the given coordinate pos.
162 * \param pos The position relative to the ChatLine
163 * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
165 int ChatLine::posToCursor(QPointF pos) {
166 if(pos.x() < tsWidth + (int)QtUi::style()->sepTsSender()/2) return -2;
167 qreal textStart = tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText();
168 if(pos.x() < textStart) return -1;
169 int x = (int)(pos.x() - textStart);
170 for(int l = lineLayouts.count() - 1; l >=0; l--) {
171 LineLayout line = lineLayouts[l];
172 if(pos.y() >= line.y) {
173 int offset = charPos[line.start]; x += offset;
174 for(int i = line.start + line.length - 1; i >= line.start; i--) {
175 if((charPos[i] + charPos[i+1])/2 <= x) return i+1; // FIXME: Optimize this!
183 void ChatLine::precomputeLine() {
184 tsFormat = calcFormatRanges(styledTimeStamp);
185 senderFormat = calcFormatRanges(styledSender);
186 textFormat = calcFormatRanges(styledText);
189 foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
190 foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
193 charPos.resize(styledText.text.length() + 1);
194 charHeights.resize(styledText.text.length());
195 charUrlIdx.fill(-1, styledText.text.length());
196 for(int i = 0; i < styledText.urls.count(); i++) {
197 QtUiStyle::UrlInfo url = styledText.urls[i];
198 for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i;
200 if(!textFormat.count()) return;
201 int idx = 0; int cnt = 0; int w = 0;
202 QFontMetrics metrics(textFormat[0].format.font());
204 wr.start = -1; wr.trailing = -1;
205 for(int i = 0; i < styledText.text.length(); ) {
206 charPos[i] = w; charHeights[i] = textFormat[idx].height;
207 w += metrics.charWidth(styledText.text, i);
208 if(!styledText.text[i].isSpace()) {
209 if(wr.trailing >= 0) {
210 // new word after space
215 wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
217 wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
221 wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
226 if(++i < styledText.text.length() && ++cnt >= textFormat[idx].length) {
228 Q_ASSERT(idx < textFormat.count());
229 metrics = QFontMetrics(textFormat[idx].format.font());
232 charPos[styledText.text.length()] = w;
233 if(wr.start >= 0) words.append(wr);
236 qreal ChatLine::layout(qreal tsw, qreal senderw, qreal textw) {
237 tsWidth = tsw; senderWidth = senderw; textWidth = textw;
238 if(textw <= 0) return minHeight;
239 lineLayouts.clear(); LineLayout line;
241 int offset = 0; int numWords = 0;
244 line.height = minHeight; // first line needs room for ts and sender
245 for(uint i = 0; i < (uint)words.count(); i++) {
246 int lastpos = charPos[words[i].start + words[i].length]; // We use charPos[lastchar + 1], 'coz last char needs to fit
247 if(lastpos - offset <= textw) {
248 line.height = qMax(line.height, words[i].height);
249 line.length = words[i].start + words[i].length - line.start;
254 // ok, we had some words before, so store the layout and start a new line
256 line.length = words[i-1].start + words[i-1].length - line.start;
257 lineLayouts.append(line);
258 line.y += line.height;
259 line.start = words[i].start;
260 line.height = words[i].height;
261 offset = charPos[words[i].start];
264 // check if the word fits into the current line
265 if(lastpos - offset <= textw) {
266 line.length = words[i].length;
268 // we need to break a word in the middle
269 int border = (int)textw + offset; // save some additions
270 line.start = words[i].start;
272 line.height = charHeights[line.start];
273 int j = line.start + 1;
274 for(int l = 1; l < words[i].length; j++, l++) {
275 if(charPos[j+1] < border) {
277 line.height = qMax(line.height, charHeights[j]);
281 lineLayouts.append(line);
282 line.y += line.height;
284 line.height = charHeights[j];
287 border = (int)textw + offset;
295 lineLayouts.append(line);
301 //!\brief Draw ChatLine on the given QPainter at the given position.
302 void ChatLine::draw(QPainter *p, const QPointF &pos) {
303 QPalette pal = QApplication::palette();
305 if(selectionMode == Full) {
306 p->setPen(Qt::NoPen);
307 p->setBrush(pal.brush(QPalette::Highlight));
308 p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
311 p->setPen(Qt::NoPen);
312 p->setBrush(QColor("lightcoral") /*pal.brush(QPalette::AlternateBase) */);
313 p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
315 if(selectionMode == Partial) {
319 p->setClipRect(QRectF(pos, QSizeF(tsWidth, height())));
320 tsLayout.draw(p, pos, tsFormat);
321 p->setClipRect(QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, height())));
322 senderLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender(), 0), senderFormat);
323 p->setClipping(false);
324 textLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0), textFormat);
326 //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
327 //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
328 p->setBackgroundMode(Qt::OpaqueMode);
330 QRectF rect(pos, QSizeF(tsWidth, minHeight));
332 foreach(FormatRange fr, tsFormat) {
333 p->setFont(fr.format.font());
334 p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
335 p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledTimeStamp.text.mid(fr.start, fr.length), &brect);
336 rect.setLeft(brect.right());
338 rect = QRectF(pos + QPointF(tsWidth + QtUi::style()->sepTsSender(), 0), QSizeF(senderWidth, minHeight));
339 for(int i = senderFormat.count() - 1; i >= 0; i--) {
340 FormatRange fr = senderFormat[i];
341 p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
342 p->drawText(rect, Qt::AlignRight|Qt::TextSingleLine, styledSender.text.mid(fr.start, fr.length), &brect);
343 rect.setRight(brect.left());
345 QPointF tpos = pos + QPointF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText(), 0);
346 qreal h = 0; int l = 0;
347 if(lineLayouts.count() == 0) return; // how can this happen?
348 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
350 foreach(FormatRange fr, textFormat) {
351 if(l >= lineLayouts.count()) break;
352 p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
353 int start, end, frend, llend;
355 frend = fr.start + fr.length;
356 if(frend <= lineLayouts[l].start) break;
357 llend = lineLayouts[l].start + lineLayouts[l].length;
358 start = qMax(fr.start, lineLayouts[l].start); end = qMin(frend, llend);
359 rect.setLeft(tpos.x() + charPos[start] - offset);
360 p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledText.text.mid(start, end - start), &brect);
362 h += lineLayouts[l].height;
364 if(l < lineLayouts.count()) {
365 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
366 offset = charPos[lineLayouts[l].start];
369 } while(end < frend && l < lineLayouts.count());