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