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"
26 //! Construct a ChatLine object from a message.
28 * \param m The message to be layouted and rendered
30 ChatLine::ChatLine(Message m) {
32 //networkName = m.buffer.network();
33 //bufferName = m.buffer.buffer();
40 ChatLine::~ChatLine() {
44 void ChatLine::formatMsg(Message msg) {
45 isHighlight = msg.flags() & Message::Highlight;
46 QTextOption tsOption, senderOption, textOption;
47 styledTimeStamp = QtUi::style()->styleString(msg.formattedTimestamp());
48 styledSender = QtUi::style()->styleString(msg.formattedSender());
49 styledText = QtUi::style()->styleString(msg.formattedText());
53 // This function is almost obsolete, since with the new style engine, we already get a list of formats...
54 // We don't know yet if we keep this implementation of ChatLine, so I won't bother making this actually nice.
55 // Also, the additional format is ignored for now, which means that you won't see a selection...
57 QList<ChatLine::FormatRange> ChatLine::calcFormatRanges(const UiStyle::StyledText &fs, QTextLayout::FormatRange additional) {
58 QList<FormatRange> ranges;
60 foreach(QTextLayout::FormatRange f, fs.formats) {
62 range.start = f.start;
63 range.length = f.length;
64 range.format = f.format;
65 QFontMetrics metrics(range.format.font());
66 range.height = metrics.lineSpacing();
70 QList<QTextLayout::FormatRange> formats = fs.formats;
71 formats.append(additional);
73 FormatRange range, lastrange;
74 for(int i = 0; i < fs.text.length(); i++) {
75 QTextCharFormat format;
76 foreach(QTextLayout::FormatRange f, formats) {
77 if(i >= f.start && i < f.start + f.length) format.merge(f.format);
80 range.start = 0; range.length = 1; range.format= format;
83 if(format == range.format) range.length++;
85 QFontMetrics metrics(range.format.font());
86 range.height = metrics.lineSpacing();
88 range.start = i; range.length = 1; range.format = format;
94 QFontMetrics metrics(range.format.font());
95 range.height = metrics.lineSpacing();
102 void ChatLine::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;
109 tsFormat = calcFormatRanges(styledTimeStamp);
110 senderFormat = calcFormatRanges(styledSender);
111 textFormat = calcFormatRanges(styledText);
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.append(textSel);
120 textFormat = calcFormatRanges(styledText, textSel);
121 foreach(FormatRange fr, textFormat);
124 tsSel.format.setForeground(pal.brush(QPalette::HighlightedText));
125 tsSel.format.setBackground(pal.brush(QPalette::Highlight));
126 tsSel.start = 0; tsSel.length = styledTimeStamp.text.length();
127 tsFormat = calcFormatRanges(styledTimeStamp, tsSel);
128 senderSel.format.setForeground(pal.brush(QPalette::HighlightedText));
129 senderSel.format.setBackground(pal.brush(QPalette::Highlight));
130 senderSel.start = 0; senderSel.length = styledSender.text.length();
131 senderFormat = calcFormatRanges(styledSender, senderSel);
132 textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
133 textSel.format.setBackground(pal.brush(QPalette::Highlight));
134 textSel.start = 0; textSel.length = styledText.text.length();
135 textFormat = calcFormatRanges(styledText, textSel);
140 MsgId ChatLine::msgId() const {
144 BufferInfo ChatLine::bufferInfo() const {
145 return msg.bufferInfo();
148 QDateTime ChatLine::timestamp() const {
149 return msg.timestamp();
152 QString ChatLine::sender() const {
153 return styledSender.text;
156 QString ChatLine::text() const {
157 return styledText.text;
160 bool ChatLine::isUrl(int c) const {
161 if(c < 0 || c >= charUrlIdx.count()) return false;;
162 return charUrlIdx[c] >= 0;
165 QUrl ChatLine::getUrl(int c) const {
166 if(c < 0 || c >= charUrlIdx.count()) return QUrl();
167 int i = charUrlIdx[c];
168 if(i >= 0) return styledText.urls[i].url;
172 //!\brief Return the cursor position for the given coordinate pos.
174 * \param pos The position relative to the ChatLine
175 * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
177 int ChatLine::posToCursor(QPointF pos) {
178 if(pos.x() < tsWidth + (int)QtUi::style()->sepTsSender()/2) return -2;
179 qreal textStart = tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText();
180 if(pos.x() < textStart) return -1;
181 int x = (int)(pos.x() - textStart);
182 for(int l = lineLayouts.count() - 1; l >=0; l--) {
183 LineLayout line = lineLayouts[l];
184 if(pos.y() >= line.y) {
185 int offset = charPos[line.start]; x += offset;
186 for(int i = line.start + line.length - 1; i >= line.start; i--) {
187 if((charPos[i] + charPos[i+1])/2 <= x) return i+1; // FIXME: Optimize this!
195 void ChatLine::precomputeLine() {
196 tsFormat = calcFormatRanges(styledTimeStamp);
197 senderFormat = calcFormatRanges(styledSender);
198 textFormat = calcFormatRanges(styledText);
201 foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
202 foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
205 charPos.resize(styledText.text.length() + 1);
206 charHeights.resize(styledText.text.length());
207 charUrlIdx.fill(-1, styledText.text.length());
208 for(int i = 0; i < styledText.urls.count(); i++) {
209 QtUiStyle::UrlInfo url = styledText.urls[i];
210 for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i;
212 if(!textFormat.count()) return;
213 int idx = 0; int cnt = 0; int w = 0;
214 QFontMetrics metrics(textFormat[0].format.font());
216 wr.start = -1; wr.trailing = -1;
217 for(int i = 0; i < styledText.text.length(); ) {
218 charPos[i] = w; charHeights[i] = textFormat[idx].height;
219 w += metrics.charWidth(styledText.text, i);
220 if(!styledText.text[i].isSpace()) {
221 if(wr.trailing >= 0) {
222 // new word after space
227 wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
229 wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
233 wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
238 if(++i < styledText.text.length() && ++cnt >= textFormat[idx].length) {
240 Q_ASSERT(idx < textFormat.count());
241 metrics = QFontMetrics(textFormat[idx].format.font());
244 charPos[styledText.text.length()] = w;
245 if(wr.start >= 0) words.append(wr);
248 qreal ChatLine::layout(qreal tsw, qreal senderw, qreal textw) {
249 tsWidth = tsw; senderWidth = senderw; textWidth = textw;
250 if(textw <= 0) return minHeight;
251 lineLayouts.clear(); LineLayout line;
253 int offset = 0; int numWords = 0;
256 line.height = minHeight; // first line needs room for ts and sender
257 for(uint i = 0; i < (uint)words.count(); i++) {
258 int lastpos = charPos[words[i].start + words[i].length]; // We use charPos[lastchar + 1], 'coz last char needs to fit
259 if(lastpos - offset <= textw) {
260 line.height = qMax(line.height, words[i].height);
261 line.length = words[i].start + words[i].length - line.start;
266 // ok, we had some words before, so store the layout and start a new line
268 line.length = words[i-1].start + words[i-1].length - line.start;
269 lineLayouts.append(line);
270 line.y += line.height;
271 line.start = words[i].start;
272 line.height = words[i].height;
273 offset = charPos[words[i].start];
276 // check if the word fits into the current line
277 if(lastpos - offset <= textw) {
278 line.length = words[i].length;
280 // we need to break a word in the middle
281 int border = (int)textw + offset; // save some additions
282 line.start = words[i].start;
284 line.height = charHeights[line.start];
285 int j = line.start + 1;
286 for(int l = 1; l < words[i].length; j++, l++) {
287 if(charPos[j+1] < border) {
289 line.height = qMax(line.height, charHeights[j]);
293 lineLayouts.append(line);
294 line.y += line.height;
296 line.height = charHeights[j];
299 border = (int)textw + offset;
307 lineLayouts.append(line);
313 //!\brief Draw ChatLine on the given QPainter at the given position.
314 void ChatLine::draw(QPainter *p, const QPointF &pos) {
315 QPalette pal = QApplication::palette();
317 if(selectionMode == Full) {
318 p->setPen(Qt::NoPen);
319 p->setBrush(pal.brush(QPalette::Highlight));
320 p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
323 p->setPen(Qt::NoPen);
324 p->setBrush(pal.brush(QPalette::AlternateBase));
325 p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
327 if(selectionMode == Partial) {
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);
338 //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
339 //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
340 p->setBackgroundMode(Qt::OpaqueMode);
342 QRectF rect(pos, QSizeF(tsWidth, minHeight));
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());
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());
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));
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;
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);
374 h += lineLayouts[l].height;
376 if(l < lineLayouts.count()) {
377 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
378 offset = charPos[lineLayouts[l].start];
381 } while(end < frend && l < lineLayouts.count());