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