c1cd7d2ff970d805aca446b8592f27a1722e4460
[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 #include "qtuisettings.h"
27
28 //! Construct a ChatLineOld object from a message.
29 /**
30  * \param m   The message to be layouted and rendered
31  */
32 ChatLineOld::ChatLineOld(Message m) {
33   hght = 0;
34
35   msg = m;
36   selectionMode = None;
37   isHighlight = false;
38   formatMsg(msg);
39 }
40
41 ChatLineOld::~ChatLineOld() {
42
43 }
44
45 void ChatLineOld::formatMsg(Message msg) {
46   isHighlight = msg.flags() & Message::Highlight;
47   QTextOption tsOption, senderOption, textOption;
48   styledTimeStamp = QtUi::style()->styleString(msg.formattedTimestamp());
49   styledSender = QtUi::style()->styleString(msg.formattedSender());
50   styledText = QtUi::style()->styleString(msg.formattedText());
51   precomputeLine();
52 }
53
54 QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(const UiStyle::StyledText &fs) {
55   QTextLayout::FormatRange additional;
56   additional.start = additional.length = 0;
57   return calcFormatRanges(fs, additional);
58 }
59
60 // This function is almost obsolete, since with the new style engine, we already get a list of formats...
61 // We don't know yet if we keep this implementation of ChatLineOld, so I won't bother making this actually nice.
62 QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(const UiStyle::StyledText &_fs,
63      const QTextLayout::FormatRange &additional) {
64   UiStyle::StyledText fs = _fs;
65   QList<FormatRange> ranges;
66
67   if(additional.length > 0) {
68     for(int i = 0; i < fs.formats.count(); i++) {
69       int oldend = fs.formats[i].start + fs.formats[i].length - 1;
70       int addend = additional.start + additional.length - 1;
71       if(oldend < additional.start) continue;
72       fs.formats[i].length = additional.start - fs.formats[i].start;
73       QTextLayout::FormatRange addfmtrng = fs.formats[i];
74       addfmtrng.format.merge(additional.format);
75       addfmtrng.start = additional.start;
76       addfmtrng.length = qMin(oldend, addend) - additional.start + 1;
77       fs.formats.insert(++i, addfmtrng);
78       if(addend == oldend) break;
79       if(addend < oldend) {
80         QTextLayout::FormatRange restfmtrng = fs.formats[i-1];
81         restfmtrng.start = addend + 1;
82         restfmtrng.length = oldend - addend;
83         fs.formats.insert(++i, restfmtrng);
84         break;
85       }
86     }
87   }
88
89   foreach(QTextLayout::FormatRange f, fs.formats) {
90     if(f.length <= 0) continue;
91     FormatRange range;
92     range.start = f.start;
93     range.length = f.length;
94     range.format = f.format;
95     QFontMetrics metrics(range.format.font());
96     range.height = metrics.lineSpacing();
97     ranges.append(range);
98   }
99   return ranges;
100 }
101
102 void ChatLineOld::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 = calcFormatRanges(styledText, textSel);
120       break;
121     case Full:
122       tsSel.format.setForeground(pal.brush(QPalette::HighlightedText));
123       tsSel.format.setBackground(pal.brush(QPalette::Highlight));
124       tsSel.start = 0; tsSel.length = styledTimeStamp.text.length();
125       tsFormat = calcFormatRanges(styledTimeStamp, tsSel);
126       senderSel.format.setForeground(pal.brush(QPalette::HighlightedText));
127       senderSel.format.setBackground(pal.brush(QPalette::Highlight));
128       senderSel.start = 0; senderSel.length = styledSender.text.length();
129       senderFormat = calcFormatRanges(styledSender, senderSel);
130       textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
131       textSel.format.setBackground(pal.brush(QPalette::Highlight));
132       textSel.start = 0; textSel.length = styledText.text.length();
133       textFormat = calcFormatRanges(styledText, textSel);
134       break;
135   }
136 }
137
138 MsgId ChatLineOld::msgId() const {
139   return msg.msgId();
140 }
141
142 BufferInfo ChatLineOld::bufferInfo() const {
143   return msg.bufferInfo();
144 }
145
146 QDateTime ChatLineOld::timestamp() const {
147   return msg.timestamp();
148 }
149
150 QString ChatLineOld::sender() const {
151   return styledSender.text;
152 }
153
154 QString ChatLineOld::text() const {
155   return styledText.text;
156 }
157
158 bool ChatLineOld::isUrl(int c) const {
159   if(c < 0 || c >= charUrlIdx.count()) return false;;
160   return charUrlIdx[c] >= 0;
161 }
162
163 QUrl ChatLineOld::getUrl(int c) const {
164   if(c < 0 || c >= charUrlIdx.count()) return QUrl();
165   int i = charUrlIdx[c];
166   if(i >= 0) return styledText.urls[i].url;
167   else return QUrl();
168 }
169
170 //!\brief Return the cursor position for the given coordinate pos.
171 /**
172  * \param pos The position relative to the ChatLineOld
173  * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
174  */
175 int ChatLineOld::posToCursor(QPointF pos) {
176   if(pos.x() < tsWidth + (int)QtUi::style()->sepTsSender()/2) return -2;
177   qreal textStart = tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText();
178   if(pos.x() < textStart) return -1;
179   int x = (int)(pos.x() - textStart);
180   for(int l = lineLayouts.count() - 1; l >=0; l--) {
181     LineLayout line = lineLayouts[l];
182     if(pos.y() >= line.y) {
183       int offset = charPos[line.start]; x += offset;
184       for(int i = line.start + line.length - 1; i >= line.start; i--) {
185         if((charPos[i] + charPos[i+1])/2 <= x) return i+1; // FIXME: Optimize this!
186       }
187       return line.start;
188     }
189   }
190   return 0;
191 }
192
193 void ChatLineOld::precomputeLine() {
194   tsFormat = calcFormatRanges(styledTimeStamp);
195   senderFormat = calcFormatRanges(styledSender);
196   textFormat = calcFormatRanges(styledText);
197
198   minHeight = 0;
199   foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
200   foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
201
202   words.clear();
203   charPos.resize(styledText.text.length() + 1);
204   charHeights.resize(styledText.text.length());
205   charUrlIdx.fill(-1, styledText.text.length());
206   for(int i = 0; i < styledText.urls.count(); i++) {
207     QtUiStyle::UrlInfo url = styledText.urls[i];
208     for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i;
209   }
210   if(!textFormat.count()) return;
211   int idx = 0; int cnt = 0; int w = 0;
212   QFontMetrics metrics(textFormat[0].format.font());
213   Word wr;
214   wr.start = -1; wr.trailing = -1;
215   for(int i = 0; i < styledText.text.length(); ) {
216     charPos[i] = w; charHeights[i] = textFormat[idx].height;
217     w += metrics.charWidth(styledText.text, i);
218     if(!styledText.text[i].isSpace()) {
219       if(wr.trailing >= 0) {
220         // new word after space
221         words.append(wr);
222         wr.start = -1;
223       }
224       if(wr.start < 0) {
225         wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
226       } else {
227         wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
228       }
229     } else {
230       if(wr.start < 0) {
231         wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
232       } else {
233         wr.trailing++;
234       }
235     }
236     if(++i < styledText.text.length() && ++cnt >= textFormat[idx].length) {
237       cnt = 0; idx++;
238       Q_ASSERT(idx < textFormat.count());
239       metrics = QFontMetrics(textFormat[idx].format.font());
240     }
241   }
242   charPos[styledText.text.length()] = w;
243   if(wr.start >= 0) words.append(wr);
244 }
245
246 qreal ChatLineOld::layout(qreal tsw, qreal senderw, qreal textw) {
247   tsWidth = tsw; senderWidth = senderw; textWidth = textw;
248   if(textw <= 0) return minHeight;
249   lineLayouts.clear(); LineLayout line;
250   int h = 0;
251   int offset = 0; int numWords = 0;
252   line.y = 0;
253   line.start = 0;
254   line.height = minHeight;  // first line needs room for ts and sender
255   for(uint i = 0; i < (uint)words.count(); i++) {
256     int lastpos = charPos[words[i].start + words[i].length]; // We use charPos[lastchar + 1], 'coz last char needs to fit
257     if(lastpos - offset <= textw) {
258       line.height = qMax(line.height, words[i].height);
259       line.length = words[i].start + words[i].length - line.start;
260       numWords++;
261     } else {
262       // we need to wrap!
263       if(numWords > 0) {
264         // ok, we had some words before, so store the layout and start a new line
265         h += line.height;
266         line.length = words[i-1].start + words[i-1].length - line.start;
267         lineLayouts.append(line);
268         line.y += line.height;
269         line.start = words[i].start;
270         line.height = words[i].height;
271         offset = charPos[words[i].start];
272       }
273       numWords = 1;
274       // check if the word fits into the current line
275       if(lastpos - offset <= textw) {
276         line.length = words[i].length;
277       } else {
278         // we need to break a word in the middle
279         int border = (int)textw + offset; // save some additions
280         line.start = words[i].start;
281         line.length = 1;
282         line.height = charHeights[line.start];
283         int j = line.start + 1;
284         for(int l = 1; l < words[i].length; j++, l++) {
285           if(charPos[j+1] < border) {
286             line.length++;
287             line.height = qMax(line.height, charHeights[j]);
288             continue;
289           } else {
290             h += line.height;
291             lineLayouts.append(line);
292             line.y += line.height;
293             line.start = j;
294             line.height = charHeights[j];
295             line.length = 1;
296             offset = charPos[j];
297             border = (int)textw + offset;
298           }
299         }
300       }
301     }
302   }
303   h += line.height;
304   if(numWords > 0) {
305     lineLayouts.append(line);
306   }
307   hght = h;
308   return hght;
309 }
310
311 //!\brief Draw ChatLineOld on the given QPainter at the given position.
312 void ChatLineOld::draw(QPainter *p, const QPointF &pos) {
313   QPalette pal = QApplication::palette();
314
315   if(selectionMode == Full) {
316     p->setPen(Qt::NoPen);
317     p->setBrush(pal.brush(QPalette::Highlight));
318     p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
319   } else {
320     if(isHighlight) {
321       QtUiSettings s("QtUi/Colors");
322       QColor highlightColor = s.value("highlightColor", QVariant(QColor("lightcoral"))).value<QColor>();
323       p->setPen(Qt::NoPen);
324       p->setBrush(highlightColor /*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 }