topic changes are now handled properly
[quassel.git] / src / qtui / chatline-old.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "chatline-old.h"
22 #include "client.h"
23 #include "network.h"
24 #include "qtui.h"
25
26 //! Construct a ChatLineOld object from a message.
27 /**
28  * \param m   The message to be layouted and rendered
29  */
30 ChatLineOld::ChatLineOld(Message m) {
31   hght = 0;
32   //networkName = m.buffer.network();
33   //bufferName = m.buffer.buffer();
34   msg = m;
35   selectionMode = None;
36   isHighlight = false;
37   formatMsg(msg);
38 }
39
40 ChatLineOld::~ChatLineOld() {
41
42 }
43
44 void ChatLineOld::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());
50   precomputeLine();
51 }
52
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 ChatLineOld, so I won't bother making this actually nice.
55 QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(UiStyle::StyledText fs, QTextLayout::FormatRange additional) {
56   QList<FormatRange> ranges;
57
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;
70       if(addend < oldend) {
71         QTextLayout::FormatRange restfmtrng = fs.formats[i-1];
72         restfmtrng.start = addend + 1;
73         restfmtrng.length = oldend - addend;
74         fs.formats.insert(++i, restfmtrng);
75         break;
76       }
77     }
78   }
79   foreach(QTextLayout::FormatRange f, fs.formats) {
80     if(f.length <= 0) continue;
81     FormatRange range;
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();
87     ranges.append(range);
88   }
89   return ranges;
90 }
91
92 void ChatLineOld::setSelection(SelectionMode mode, int start, int end) {
93   selectionMode = mode;
94   //tsFormat.clear(); senderFormat.clear(); textFormat.clear();
95   QPalette pal = QApplication::palette();
96   QTextLayout::FormatRange tsSel, senderSel, textSel;
97   switch (mode) {
98     case None:
99       tsFormat = calcFormatRanges(styledTimeStamp);
100       senderFormat = calcFormatRanges(styledSender);
101       textFormat = calcFormatRanges(styledText);
102       break;
103     case Partial:
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);
110       break;
111     case Full:
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);
124       break;
125   }
126 }
127
128 MsgId ChatLineOld::msgId() const {
129   return msg.msgId();
130 }
131
132 BufferInfo ChatLineOld::bufferInfo() const {
133   return msg.bufferInfo();
134 }
135
136 QDateTime ChatLineOld::timestamp() const {
137   return msg.timestamp();
138 }
139
140 QString ChatLineOld::sender() const {
141   return styledSender.text;
142 }
143
144 QString ChatLineOld::text() const {
145   return styledText.text;
146 }
147
148 bool ChatLineOld::isUrl(int c) const {
149   if(c < 0 || c >= charUrlIdx.count()) return false;;
150   return charUrlIdx[c] >= 0;
151 }
152
153 QUrl ChatLineOld::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;
157   else return QUrl();
158 }
159
160 //!\brief Return the cursor position for the given coordinate pos.
161 /**
162  * \param pos The position relative to the ChatLineOld
163  * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
164  */
165 int ChatLineOld::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!
176       }
177       return line.start;
178     }
179   }
180   return 0;
181 }
182
183 void ChatLineOld::precomputeLine() {
184   tsFormat = calcFormatRanges(styledTimeStamp);
185   senderFormat = calcFormatRanges(styledSender);
186   textFormat = calcFormatRanges(styledText);
187
188   minHeight = 0;
189   foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
190   foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
191
192   words.clear();
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;
199   }
200   if(!textFormat.count()) return;
201   int idx = 0; int cnt = 0; int w = 0;
202   QFontMetrics metrics(textFormat[0].format.font());
203   Word wr;
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
211         words.append(wr);
212         wr.start = -1;
213       }
214       if(wr.start < 0) {
215         wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
216       } else {
217         wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
218       }
219     } else {
220       if(wr.start < 0) {
221         wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
222       } else {
223         wr.trailing++;
224       }
225     }
226     if(++i < styledText.text.length() && ++cnt >= textFormat[idx].length) {
227       cnt = 0; idx++;
228       Q_ASSERT(idx < textFormat.count());
229       metrics = QFontMetrics(textFormat[idx].format.font());
230     }
231   }
232   charPos[styledText.text.length()] = w;
233   if(wr.start >= 0) words.append(wr);
234 }
235
236 qreal ChatLineOld::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;
240   int h = 0;
241   int offset = 0; int numWords = 0;
242   line.y = 0;
243   line.start = 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;
250       numWords++;
251     } else {
252       // we need to wrap!
253       if(numWords > 0) {
254         // ok, we had some words before, so store the layout and start a new line
255         h += line.height;
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];
262       }
263       numWords = 1;
264       // check if the word fits into the current line
265       if(lastpos - offset <= textw) {
266         line.length = words[i].length;
267       } else {
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;
271         line.length = 1;
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) {
276             line.length++;
277             line.height = qMax(line.height, charHeights[j]);
278             continue;
279           } else {
280             h += line.height;
281             lineLayouts.append(line);
282             line.y += line.height;
283             line.start = j;
284             line.height = charHeights[j];
285             line.length = 1;
286             offset = charPos[j];
287             border = (int)textw + offset;
288           }
289         }
290       }
291     }
292   }
293   h += line.height;
294   if(numWords > 0) {
295     lineLayouts.append(line);
296   }
297   hght = h;
298   return hght;
299 }
300
301 //!\brief Draw ChatLineOld on the given QPainter at the given position.
302 void ChatLineOld::draw(QPainter *p, const QPointF &pos) {
303   QPalette pal = QApplication::palette();
304
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())));
309   } else {
310     if(isHighlight) {
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())));
314     }
315     if(selectionMode == Partial) {
316
317     }
318   } /*
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);
325     */
326   //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
327   //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
328   p->setBackgroundMode(Qt::OpaqueMode);
329   QPointF tp = pos;
330   QRectF rect(pos, QSizeF(tsWidth, minHeight));
331   QRectF brect;
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());
337   }
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());
344   }
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));
349   int offset = 0;
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;
354     do {
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);
361       if(llend <= end) {
362         h += lineLayouts[l].height;
363         l++;
364         if(l < lineLayouts.count()) {
365           rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
366           offset = charPos[lineLayouts[l].start];
367         }
368       }
369     } while(end < frend && l < lineLayouts.count());
370   }
371 }