d2f55be962f1df5641cf9bdd04adf744a788fbac
[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 "qtui.h"
23
24 //! Construct a ChatLine object from a message.
25 /**
26  * \param m   The message to be layouted and rendered
27  */
28 ChatLine::ChatLine(Message m) {
29   hght = 0;
30   //networkName = m.buffer.network();
31   //bufferName = m.buffer.buffer();
32   msg = m;
33   selectionMode = None;
34   formatMsg(msg);
35 }
36
37 ChatLine::~ChatLine() {
38
39 }
40
41 void ChatLine::formatMsg(Message msg) {
42   QTextOption tsOption, senderOption, textOption;
43   styledTimeStamp = QtUi::style()->styleString(msg.formattedTimestamp());
44   styledSender = QtUi::style()->styleString(msg.formattedSender());
45   styledText = QtUi::style()->styleString(msg.formattedText());
46   precomputeLine();
47 }
48
49 // This function is almost obsolete, since with the new style engine, we already get a list of formats...
50 // We don't know yet if we keep this implementation of ChatLine, so I won't bother making this actually nice.
51 // Also, the additional format is ignored for now, which means that you won't see a selection...
52 // FIXME TODO
53 QList<ChatLine::FormatRange> ChatLine::calcFormatRanges(const UiStyle::StyledText &fs, QTextLayout::FormatRange additional) {
54   QList<FormatRange> ranges;
55
56   foreach(QTextLayout::FormatRange f, fs.formats) {
57     FormatRange range;
58     range.start = f.start;
59     range.length = f.length;
60     range.format = f.format;
61     QFontMetrics metrics(range.format.font());
62     range.height = metrics.lineSpacing();
63     ranges.append(range);
64   }
65   /*
66   QList<QTextLayout::FormatRange> formats = fs.formats;
67   formats.append(additional);
68   int cur = -1;
69   FormatRange range, lastrange;
70   for(int i = 0; i < fs.text.length(); i++) {
71     QTextCharFormat format;
72     foreach(QTextLayout::FormatRange f, formats) {
73       if(i >= f.start && i < f.start + f.length) format.merge(f.format);
74     }
75     if(cur < 0) {
76       range.start = 0; range.length = 1; range.format= format;
77       cur = 0;
78     } else {
79       if(format == range.format) range.length++;
80       else {
81         QFontMetrics metrics(range.format.font());
82         range.height = metrics.lineSpacing();
83         ranges.append(range);
84         range.start = i; range.length = 1; range.format = format;
85         cur++;
86       }
87     }
88   }
89   if(cur >= 0) {
90     QFontMetrics metrics(range.format.font());
91     range.height = metrics.lineSpacing();
92     ranges.append(range);
93   }
94   */
95   return ranges;
96 }
97
98 void ChatLine::setSelection(SelectionMode mode, int start, int end) {
99   selectionMode = mode;
100   //tsFormat.clear(); senderFormat.clear(); textFormat.clear();
101   QPalette pal = QApplication::palette();
102   QTextLayout::FormatRange tsSel, senderSel, textSel;
103   switch (mode) {
104     case None:
105       tsFormat = calcFormatRanges(styledTimeStamp);
106       senderFormat = calcFormatRanges(styledSender);
107       textFormat = calcFormatRanges(styledText);
108       break;
109     case Partial:
110       selectionStart = qMin(start, end); selectionEnd = qMax(start, end);
111       textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
112       textSel.format.setBackground(pal.brush(QPalette::Highlight));
113       textSel.start = selectionStart;
114       textSel.length = selectionEnd - selectionStart;
115       //textFormat.append(textSel);
116       textFormat = calcFormatRanges(styledText, textSel);
117       foreach(FormatRange fr, textFormat);
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 ChatLine::msgId() const {
137   return msg.msgId();
138 }
139
140 BufferInfo ChatLine::bufferInfo() const {
141   return msg.buffer();
142 }
143
144 QDateTime ChatLine::timestamp() const {
145   return msg.timestamp();
146 }
147
148 QString ChatLine::sender() const {
149   return styledSender.text;
150 }
151
152 QString ChatLine::text() const {
153   return styledText.text;
154 }
155
156 bool ChatLine::isUrl(int c) const {
157   if(c < 0 || c >= charUrlIdx.count()) return false;;
158   return charUrlIdx[c] >= 0;
159 }
160
161 QUrl ChatLine::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 ChatLine
171  * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
172  */
173 int ChatLine::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 ChatLine::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 ChatLine::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 ChatLine on the given QPainter at the given position.
310 void ChatLine::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 if(selectionMode == Partial) {
318
319   } /*
320   p->setClipRect(QRectF(pos, QSizeF(tsWidth, height())));
321   tsLayout.draw(p, pos, tsFormat);
322   p->setClipRect(QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, height())));
323   senderLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender(), 0), senderFormat);
324   p->setClipping(false);
325   textLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0), textFormat);
326     */
327   //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
328   //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
329   p->setBackgroundMode(Qt::OpaqueMode);
330   QPointF tp = pos;
331   QRectF rect(pos, QSizeF(tsWidth, minHeight));
332   QRectF brect;
333   foreach(FormatRange fr, tsFormat) {
334     p->setFont(fr.format.font());
335     p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
336     p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledTimeStamp.text.mid(fr.start, fr.length), &brect);
337     rect.setLeft(brect.right());
338   }
339   rect = QRectF(pos + QPointF(tsWidth + QtUi::style()->sepTsSender(), 0), QSizeF(senderWidth, minHeight));
340   for(int i = senderFormat.count() - 1; i >= 0; i--) {
341     FormatRange fr = senderFormat[i];
342     p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
343     p->drawText(rect, Qt::AlignRight|Qt::TextSingleLine, styledSender.text.mid(fr.start, fr.length), &brect);
344     rect.setRight(brect.left());
345   }
346   QPointF tpos = pos + QPointF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText(), 0);
347   qreal h = 0; int l = 0;
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 }