0a3cc20038ea526529f4bcbe5c7e3331e2697362
[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
33   msg = m;
34   selectionMode = None;
35   isHighlight = false;
36   formatMsg(msg);
37 }
38
39 ChatLineOld::~ChatLineOld() {
40
41 }
42
43 void ChatLineOld::formatMsg(Message msg) {
44   isHighlight = msg.flags() & Message::Highlight;
45   QTextOption tsOption, senderOption, textOption;
46   styledTimeStamp = QtUi::style()->styleString(msg.formattedTimestamp());
47   styledSender = QtUi::style()->styleString(msg.formattedSender());
48   styledText = QtUi::style()->styleString(msg.formattedText());
49   precomputeLine();
50 }
51
52 QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(const UiStyle::StyledText &fs) {
53   QTextLayout::FormatRange additional;
54   additional.start = additional.length = 0;
55   return calcFormatRanges(fs, additional);
56 }
57
58 // This function is almost obsolete, since with the new style engine, we already get a list of formats...
59 // We don't know yet if we keep this implementation of ChatLineOld, so I won't bother making this actually nice.
60 QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(const UiStyle::StyledText &_fs,
61      const QTextLayout::FormatRange &additional) {
62   UiStyle::StyledText fs = _fs;
63   QList<FormatRange> ranges;
64
65   if(additional.length > 0) {
66     for(int i = 0; i < fs.formats.count(); i++) {
67       int oldend = fs.formats[i].start + fs.formats[i].length - 1;
68       int addend = additional.start + additional.length - 1;
69       if(oldend < additional.start) continue;
70       fs.formats[i].length = additional.start - fs.formats[i].start;
71       QTextLayout::FormatRange addfmtrng = fs.formats[i];
72       addfmtrng.format.merge(additional.format);
73       addfmtrng.start = additional.start;
74       addfmtrng.length = qMin(oldend, addend) - additional.start + 1;
75       fs.formats.insert(++i, addfmtrng);
76       if(addend == oldend) break;
77       if(addend < oldend) {
78         QTextLayout::FormatRange restfmtrng = fs.formats[i-1];
79         restfmtrng.start = addend + 1;
80         restfmtrng.length = oldend - addend;
81         fs.formats.insert(++i, restfmtrng);
82         break;
83       }
84     }
85   }
86
87   foreach(QTextLayout::FormatRange f, fs.formats) {
88     if(f.length <= 0) continue;
89     FormatRange range;
90     range.start = f.start;
91     range.length = f.length;
92     range.format = f.format;
93     QFontMetrics metrics(range.format.font());
94     range.height = metrics.lineSpacing();
95     ranges.append(range);
96   }
97   return ranges;
98 }
99
100 void ChatLineOld::setSelection(SelectionMode mode, int start, int end) {
101   selectionMode = mode;
102   //tsFormat.clear(); senderFormat.clear(); textFormat.clear();
103   QPalette pal = QApplication::palette();
104   QTextLayout::FormatRange tsSel, senderSel, textSel;
105   switch (mode) {
106     case None:
107       tsFormat = calcFormatRanges(styledTimeStamp);
108       senderFormat = calcFormatRanges(styledSender);
109       textFormat = calcFormatRanges(styledText);
110       break;
111     case Partial:
112       selectionStart = qMin(start, end); selectionEnd = qMax(start, end);
113       textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
114       textSel.format.setBackground(pal.brush(QPalette::Highlight));
115       textSel.start = selectionStart;
116       textSel.length = selectionEnd - selectionStart;
117       textFormat = calcFormatRanges(styledText, textSel);
118       break;
119     case Full:
120       tsSel.format.setForeground(pal.brush(QPalette::HighlightedText));
121       tsSel.format.setBackground(pal.brush(QPalette::Highlight));
122       tsSel.start = 0; tsSel.length = styledTimeStamp.text.length();
123       tsFormat = calcFormatRanges(styledTimeStamp, tsSel);
124       senderSel.format.setForeground(pal.brush(QPalette::HighlightedText));
125       senderSel.format.setBackground(pal.brush(QPalette::Highlight));
126       senderSel.start = 0; senderSel.length = styledSender.text.length();
127       senderFormat = calcFormatRanges(styledSender, senderSel);
128       textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
129       textSel.format.setBackground(pal.brush(QPalette::Highlight));
130       textSel.start = 0; textSel.length = styledText.text.length();
131       textFormat = calcFormatRanges(styledText, textSel);
132       break;
133   }
134 }
135
136 MsgId ChatLineOld::msgId() const {
137   return msg.msgId();
138 }
139
140 BufferInfo ChatLineOld::bufferInfo() const {
141   return msg.bufferInfo();
142 }
143
144 QDateTime ChatLineOld::timestamp() const {
145   return msg.timestamp();
146 }
147
148 QString ChatLineOld::sender() const {
149   return styledSender.text;
150 }
151
152 QString ChatLineOld::text() const {
153   return styledText.text;
154 }
155
156 bool ChatLineOld::isUrl(int c) const {
157   if(c < 0 || c >= charUrlIdx.count()) return false;;
158   return charUrlIdx[c] >= 0;
159 }
160
161 QUrl ChatLineOld::getUrl(int c) const {
162   if(c < 0 || c >= charUrlIdx.count()) return QUrl();
163   int i = charUrlIdx[c];
164   if(i >= 0) return styledText.urls[i].url;
165   else return QUrl();
166 }
167
168 //!\brief Return the cursor position for the given coordinate pos.
169 /**
170  * \param pos The position relative to the ChatLineOld
171  * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
172  */
173 int ChatLineOld::posToCursor(QPointF pos) {
174   if(pos.x() < tsWidth + (int)QtUi::style()->sepTsSender()/2) return -2;
175   qreal textStart = tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText();
176   if(pos.x() < textStart) return -1;
177   int x = (int)(pos.x() - textStart);
178   for(int l = lineLayouts.count() - 1; l >=0; l--) {
179     LineLayout line = lineLayouts[l];
180     if(pos.y() >= line.y) {
181       int offset = charPos[line.start]; x += offset;
182       for(int i = line.start + line.length - 1; i >= line.start; i--) {
183         if((charPos[i] + charPos[i+1])/2 <= x) return i+1; // FIXME: Optimize this!
184       }
185       return line.start;
186     }
187   }
188   return 0;
189 }
190
191 void ChatLineOld::precomputeLine() {
192   tsFormat = calcFormatRanges(styledTimeStamp);
193   senderFormat = calcFormatRanges(styledSender);
194   textFormat = calcFormatRanges(styledText);
195
196   minHeight = 0;
197   foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
198   foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
199
200   words.clear();
201   charPos.resize(styledText.text.length() + 1);
202   charHeights.resize(styledText.text.length());
203   charUrlIdx.fill(-1, styledText.text.length());
204   for(int i = 0; i < styledText.urls.count(); i++) {
205     QtUiStyle::UrlInfo url = styledText.urls[i];
206     for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i;
207   }
208   if(!textFormat.count()) return;
209   int idx = 0; int cnt = 0; int w = 0;
210   QFontMetrics metrics(textFormat[0].format.font());
211   Word wr;
212   wr.start = -1; wr.trailing = -1;
213   for(int i = 0; i < styledText.text.length(); ) {
214     charPos[i] = w; charHeights[i] = textFormat[idx].height;
215     w += metrics.charWidth(styledText.text, i);
216     if(!styledText.text[i].isSpace()) {
217       if(wr.trailing >= 0) {
218         // new word after space
219         words.append(wr);
220         wr.start = -1;
221       }
222       if(wr.start < 0) {
223         wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
224       } else {
225         wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
226       }
227     } else {
228       if(wr.start < 0) {
229         wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
230       } else {
231         wr.trailing++;
232       }
233     }
234     if(++i < styledText.text.length() && ++cnt >= textFormat[idx].length) {
235       cnt = 0; idx++;
236       Q_ASSERT(idx < textFormat.count());
237       metrics = QFontMetrics(textFormat[idx].format.font());
238     }
239   }
240   charPos[styledText.text.length()] = w;
241   if(wr.start >= 0) words.append(wr);
242 }
243
244 qreal ChatLineOld::layout(qreal tsw, qreal senderw, qreal textw) {
245   tsWidth = tsw; senderWidth = senderw; textWidth = textw;
246   if(textw <= 0) return minHeight;
247   lineLayouts.clear(); LineLayout line;
248   int h = 0;
249   int offset = 0; int numWords = 0;
250   line.y = 0;
251   line.start = 0;
252   line.height = minHeight;  // first line needs room for ts and sender
253   for(uint i = 0; i < (uint)words.count(); i++) {
254     int lastpos = charPos[words[i].start + words[i].length]; // We use charPos[lastchar + 1], 'coz last char needs to fit
255     if(lastpos - offset <= textw) {
256       line.height = qMax(line.height, words[i].height);
257       line.length = words[i].start + words[i].length - line.start;
258       numWords++;
259     } else {
260       // we need to wrap!
261       if(numWords > 0) {
262         // ok, we had some words before, so store the layout and start a new line
263         h += line.height;
264         line.length = words[i-1].start + words[i-1].length - line.start;
265         lineLayouts.append(line);
266         line.y += line.height;
267         line.start = words[i].start;
268         line.height = words[i].height;
269         offset = charPos[words[i].start];
270       }
271       numWords = 1;
272       // check if the word fits into the current line
273       if(lastpos - offset <= textw) {
274         line.length = words[i].length;
275       } else {
276         // we need to break a word in the middle
277         int border = (int)textw + offset; // save some additions
278         line.start = words[i].start;
279         line.length = 1;
280         line.height = charHeights[line.start];
281         int j = line.start + 1;
282         for(int l = 1; l < words[i].length; j++, l++) {
283           if(charPos[j+1] < border) {
284             line.length++;
285             line.height = qMax(line.height, charHeights[j]);
286             continue;
287           } else {
288             h += line.height;
289             lineLayouts.append(line);
290             line.y += line.height;
291             line.start = j;
292             line.height = charHeights[j];
293             line.length = 1;
294             offset = charPos[j];
295             border = (int)textw + offset;
296           }
297         }
298       }
299     }
300   }
301   h += line.height;
302   if(numWords > 0) {
303     lineLayouts.append(line);
304   }
305   hght = h;
306   return hght;
307 }
308
309 //!\brief Draw ChatLineOld on the given QPainter at the given position.
310 void ChatLineOld::draw(QPainter *p, const QPointF &pos) {
311   QPalette pal = QApplication::palette();
312
313   if(selectionMode == Full) {
314     p->setPen(Qt::NoPen);
315     p->setBrush(pal.brush(QPalette::Highlight));
316     p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
317   } else {
318     if(isHighlight) {
319       p->setPen(Qt::NoPen);
320       p->setBrush(QColor("lightcoral") /*pal.brush(QPalette::AlternateBase) */);
321       p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
322     }
323     if(selectionMode == Partial) {
324
325     }
326   } /*
327   p->setClipRect(QRectF(pos, QSizeF(tsWidth, height())));
328   tsLayout.draw(p, pos, tsFormat);
329   p->setClipRect(QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, height())));
330   senderLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender(), 0), senderFormat);
331   p->setClipping(false);
332   textLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0), textFormat);
333     */
334   //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
335   //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
336   p->setBackgroundMode(Qt::OpaqueMode);
337   QPointF tp = pos;
338   QRectF rect(pos, QSizeF(tsWidth, minHeight));
339   QRectF brect;
340   foreach(FormatRange fr, tsFormat) {
341     p->setFont(fr.format.font());
342     p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
343     p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledTimeStamp.text.mid(fr.start, fr.length), &brect);
344     rect.setLeft(brect.right());
345   }
346   rect = QRectF(pos + QPointF(tsWidth + QtUi::style()->sepTsSender(), 0), QSizeF(senderWidth, minHeight));
347   for(int i = senderFormat.count() - 1; i >= 0; i--) {
348     FormatRange fr = senderFormat[i];
349     p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
350     p->drawText(rect, Qt::AlignRight|Qt::TextSingleLine, styledSender.text.mid(fr.start, fr.length), &brect);
351     rect.setRight(brect.left());
352   }
353   QPointF tpos = pos + QPointF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText(), 0);
354   qreal h = 0; int l = 0;
355   if(lineLayouts.count() == 0) return; // how can this happen?
356   rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
357   int offset = 0;
358   foreach(FormatRange fr, textFormat) {
359     if(l >= lineLayouts.count()) break;
360     p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
361     int start, end, frend, llend;
362     do {
363       frend = fr.start + fr.length;
364       if(frend <= lineLayouts[l].start) break;
365       llend = lineLayouts[l].start + lineLayouts[l].length;
366       start = qMax(fr.start, lineLayouts[l].start); end = qMin(frend, llend);
367       rect.setLeft(tpos.x() + charPos[start] - offset);
368       p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledText.text.mid(start, end - start), &brect);
369       if(llend <= end) {
370         h += lineLayouts[l].height;
371         l++;
372         if(l < lineLayouts.count()) {
373           rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
374           offset = charPos[lineLayouts[l].start];
375         }
376       }
377     } while(end < frend && l < lineLayouts.count());
378   }
379 }