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