1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "chatline-old.h"
24 //! Construct a ChatLine object from a message.
26 * \param m The message to be layouted and rendered
28 ChatLine::ChatLine(Message m) {
30 //networkName = m.buffer.network();
31 //bufferName = m.buffer.buffer();
37 ChatLine::~ChatLine() {
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());
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...
53 QList<ChatLine::FormatRange> ChatLine::calcFormatRanges(const UiStyle::StyledText &fs, QTextLayout::FormatRange additional) {
54 QList<FormatRange> ranges;
56 foreach(QTextLayout::FormatRange f, fs.formats) {
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();
66 QList<QTextLayout::FormatRange> formats = fs.formats;
67 formats.append(additional);
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);
76 range.start = 0; range.length = 1; range.format= format;
79 if(format == range.format) range.length++;
81 QFontMetrics metrics(range.format.font());
82 range.height = metrics.lineSpacing();
84 range.start = i; range.length = 1; range.format = format;
90 QFontMetrics metrics(range.format.font());
91 range.height = metrics.lineSpacing();
98 void ChatLine::setSelection(SelectionMode mode, int start, int end) {
100 //tsFormat.clear(); senderFormat.clear(); textFormat.clear();
101 QPalette pal = QApplication::palette();
102 QTextLayout::FormatRange tsSel, senderSel, textSel;
105 tsFormat = calcFormatRanges(styledTimeStamp);
106 senderFormat = calcFormatRanges(styledSender);
107 textFormat = calcFormatRanges(styledText);
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);
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);
136 MsgId ChatLine::msgId() const {
140 BufferInfo ChatLine::bufferInfo() const {
141 return msg.bufferInfo();
144 QDateTime ChatLine::timestamp() const {
145 return msg.timestamp();
148 QString ChatLine::sender() const {
149 return styledSender.text;
152 QString ChatLine::text() const {
153 return styledText.text;
156 bool ChatLine::isUrl(int c) const {
157 if(c < 0 || c >= charUrlIdx.count()) return false;;
158 return charUrlIdx[c] >= 0;
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;
168 //!\brief Return the cursor position for the given coordinate pos.
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
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!
191 void ChatLine::precomputeLine() {
192 tsFormat = calcFormatRanges(styledTimeStamp);
193 senderFormat = calcFormatRanges(styledSender);
194 textFormat = calcFormatRanges(styledText);
197 foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
198 foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
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;
208 if(!textFormat.count()) return;
209 int idx = 0; int cnt = 0; int w = 0;
210 QFontMetrics metrics(textFormat[0].format.font());
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
223 wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
225 wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
229 wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
234 if(++i < styledText.text.length() && ++cnt >= textFormat[idx].length) {
236 Q_ASSERT(idx < textFormat.count());
237 metrics = QFontMetrics(textFormat[idx].format.font());
240 charPos[styledText.text.length()] = w;
241 if(wr.start >= 0) words.append(wr);
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;
249 int offset = 0; int numWords = 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;
262 // ok, we had some words before, so store the layout and start a new line
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];
272 // check if the word fits into the current line
273 if(lastpos - offset <= textw) {
274 line.length = words[i].length;
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;
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) {
285 line.height = qMax(line.height, charHeights[j]);
289 lineLayouts.append(line);
290 line.y += line.height;
292 line.height = charHeights[j];
295 border = (int)textw + offset;
303 lineLayouts.append(line);
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();
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) {
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);
327 //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
328 //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
329 p->setBackgroundMode(Qt::OpaqueMode);
331 QRectF rect(pos, QSizeF(tsWidth, minHeight));
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());
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());
346 QPointF tpos = pos + QPointF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText(), 0);
347 qreal h = 0; int l = 0;
348 if(lineLayouts.count() == 0) return; // how can this happen?
349 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
351 foreach(FormatRange fr, textFormat) {
352 if(l >= lineLayouts.count()) break;
353 p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
354 int start, end, frend, llend;
356 frend = fr.start + fr.length;
357 if(frend <= lineLayouts[l].start) break;
358 llend = lineLayouts[l].start + lineLayouts[l].length;
359 start = qMax(fr.start, lineLayouts[l].start); end = qMin(frend, llend);
360 rect.setLeft(tpos.x() + charPos[start] - offset);
361 p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledText.text.mid(start, end - start), &brect);
363 h += lineLayouts[l].height;
365 if(l < lineLayouts.count()) {
366 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
367 offset = charPos[lineLayouts[l].start];
370 } while(end < frend && l < lineLayouts.count());