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 const Network *net = Client::network(msg.bufferInfo().networkId());
47 QRegExp nickRegExp("^(.*\\W)?"+net->myNick()+"(\\W.*)?$");
48 if((msg.type() == Message::Plain || msg.type() == Message::Notice || msg.type() == Message::Action) && nickRegExp.exactMatch(msg.text()))
51 QTextOption tsOption, senderOption, textOption;
52 styledTimeStamp = QtUi::style()->styleString(msg.formattedTimestamp());
53 styledSender = QtUi::style()->styleString(msg.formattedSender());
54 styledText = QtUi::style()->styleString(msg.formattedText());
58 // This function is almost obsolete, since with the new style engine, we already get a list of formats...
59 // We don't know yet if we keep this implementation of ChatLine, so I won't bother making this actually nice.
60 // Also, the additional format is ignored for now, which means that you won't see a selection...
62 QList<ChatLine::FormatRange> ChatLine::calcFormatRanges(const UiStyle::StyledText &fs, QTextLayout::FormatRange additional) {
63 QList<FormatRange> ranges;
65 foreach(QTextLayout::FormatRange f, fs.formats) {
67 range.start = f.start;
68 range.length = f.length;
69 range.format = f.format;
70 QFontMetrics metrics(range.format.font());
71 range.height = metrics.lineSpacing();
75 QList<QTextLayout::FormatRange> formats = fs.formats;
76 formats.append(additional);
78 FormatRange range, lastrange;
79 for(int i = 0; i < fs.text.length(); i++) {
80 QTextCharFormat format;
81 foreach(QTextLayout::FormatRange f, formats) {
82 if(i >= f.start && i < f.start + f.length) format.merge(f.format);
85 range.start = 0; range.length = 1; range.format= format;
88 if(format == range.format) range.length++;
90 QFontMetrics metrics(range.format.font());
91 range.height = metrics.lineSpacing();
93 range.start = i; range.length = 1; range.format = format;
99 QFontMetrics metrics(range.format.font());
100 range.height = metrics.lineSpacing();
101 ranges.append(range);
107 void ChatLine::setSelection(SelectionMode mode, int start, int end) {
108 selectionMode = mode;
109 //tsFormat.clear(); senderFormat.clear(); textFormat.clear();
110 QPalette pal = QApplication::palette();
111 QTextLayout::FormatRange tsSel, senderSel, textSel;
114 tsFormat = calcFormatRanges(styledTimeStamp);
115 senderFormat = calcFormatRanges(styledSender);
116 textFormat = calcFormatRanges(styledText);
119 selectionStart = qMin(start, end); selectionEnd = qMax(start, end);
120 textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
121 textSel.format.setBackground(pal.brush(QPalette::Highlight));
122 textSel.start = selectionStart;
123 textSel.length = selectionEnd - selectionStart;
124 //textFormat.append(textSel);
125 textFormat = calcFormatRanges(styledText, textSel);
126 foreach(FormatRange fr, textFormat);
129 tsSel.format.setForeground(pal.brush(QPalette::HighlightedText));
130 tsSel.format.setBackground(pal.brush(QPalette::Highlight));
131 tsSel.start = 0; tsSel.length = styledTimeStamp.text.length();
132 tsFormat = calcFormatRanges(styledTimeStamp, tsSel);
133 senderSel.format.setForeground(pal.brush(QPalette::HighlightedText));
134 senderSel.format.setBackground(pal.brush(QPalette::Highlight));
135 senderSel.start = 0; senderSel.length = styledSender.text.length();
136 senderFormat = calcFormatRanges(styledSender, senderSel);
137 textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
138 textSel.format.setBackground(pal.brush(QPalette::Highlight));
139 textSel.start = 0; textSel.length = styledText.text.length();
140 textFormat = calcFormatRanges(styledText, textSel);
145 MsgId ChatLine::msgId() const {
149 BufferInfo ChatLine::bufferInfo() const {
150 return msg.bufferInfo();
153 QDateTime ChatLine::timestamp() const {
154 return msg.timestamp();
157 QString ChatLine::sender() const {
158 return styledSender.text;
161 QString ChatLine::text() const {
162 return styledText.text;
165 bool ChatLine::isUrl(int c) const {
166 if(c < 0 || c >= charUrlIdx.count()) return false;;
167 return charUrlIdx[c] >= 0;
170 QUrl ChatLine::getUrl(int c) const {
171 if(c < 0 || c >= charUrlIdx.count()) return QUrl();
172 int i = charUrlIdx[c];
173 if(i >= 0) return styledText.urls[i].url;
177 //!\brief Return the cursor position for the given coordinate pos.
179 * \param pos The position relative to the ChatLine
180 * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
182 int ChatLine::posToCursor(QPointF pos) {
183 if(pos.x() < tsWidth + (int)QtUi::style()->sepTsSender()/2) return -2;
184 qreal textStart = tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText();
185 if(pos.x() < textStart) return -1;
186 int x = (int)(pos.x() - textStart);
187 for(int l = lineLayouts.count() - 1; l >=0; l--) {
188 LineLayout line = lineLayouts[l];
189 if(pos.y() >= line.y) {
190 int offset = charPos[line.start]; x += offset;
191 for(int i = line.start + line.length - 1; i >= line.start; i--) {
192 if((charPos[i] + charPos[i+1])/2 <= x) return i+1; // FIXME: Optimize this!
200 void ChatLine::precomputeLine() {
201 tsFormat = calcFormatRanges(styledTimeStamp);
202 senderFormat = calcFormatRanges(styledSender);
203 textFormat = calcFormatRanges(styledText);
206 foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
207 foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
210 charPos.resize(styledText.text.length() + 1);
211 charHeights.resize(styledText.text.length());
212 charUrlIdx.fill(-1, styledText.text.length());
213 for(int i = 0; i < styledText.urls.count(); i++) {
214 QtUiStyle::UrlInfo url = styledText.urls[i];
215 for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i;
217 if(!textFormat.count()) return;
218 int idx = 0; int cnt = 0; int w = 0;
219 QFontMetrics metrics(textFormat[0].format.font());
221 wr.start = -1; wr.trailing = -1;
222 for(int i = 0; i < styledText.text.length(); ) {
223 charPos[i] = w; charHeights[i] = textFormat[idx].height;
224 w += metrics.charWidth(styledText.text, i);
225 if(!styledText.text[i].isSpace()) {
226 if(wr.trailing >= 0) {
227 // new word after space
232 wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
234 wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
238 wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
243 if(++i < styledText.text.length() && ++cnt >= textFormat[idx].length) {
245 Q_ASSERT(idx < textFormat.count());
246 metrics = QFontMetrics(textFormat[idx].format.font());
249 charPos[styledText.text.length()] = w;
250 if(wr.start >= 0) words.append(wr);
253 qreal ChatLine::layout(qreal tsw, qreal senderw, qreal textw) {
254 tsWidth = tsw; senderWidth = senderw; textWidth = textw;
255 if(textw <= 0) return minHeight;
256 lineLayouts.clear(); LineLayout line;
258 int offset = 0; int numWords = 0;
261 line.height = minHeight; // first line needs room for ts and sender
262 for(uint i = 0; i < (uint)words.count(); i++) {
263 int lastpos = charPos[words[i].start + words[i].length]; // We use charPos[lastchar + 1], 'coz last char needs to fit
264 if(lastpos - offset <= textw) {
265 line.height = qMax(line.height, words[i].height);
266 line.length = words[i].start + words[i].length - line.start;
271 // ok, we had some words before, so store the layout and start a new line
273 line.length = words[i-1].start + words[i-1].length - line.start;
274 lineLayouts.append(line);
275 line.y += line.height;
276 line.start = words[i].start;
277 line.height = words[i].height;
278 offset = charPos[words[i].start];
281 // check if the word fits into the current line
282 if(lastpos - offset <= textw) {
283 line.length = words[i].length;
285 // we need to break a word in the middle
286 int border = (int)textw + offset; // save some additions
287 line.start = words[i].start;
289 line.height = charHeights[line.start];
290 int j = line.start + 1;
291 for(int l = 1; l < words[i].length; j++, l++) {
292 if(charPos[j+1] < border) {
294 line.height = qMax(line.height, charHeights[j]);
298 lineLayouts.append(line);
299 line.y += line.height;
301 line.height = charHeights[j];
304 border = (int)textw + offset;
312 lineLayouts.append(line);
318 //!\brief Draw ChatLine on the given QPainter at the given position.
319 void ChatLine::draw(QPainter *p, const QPointF &pos) {
320 QPalette pal = QApplication::palette();
322 if(selectionMode == Full) {
323 p->setPen(Qt::NoPen);
324 p->setBrush(pal.brush(QPalette::Highlight));
325 p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
328 p->setPen(Qt::NoPen);
329 p->setBrush(pal.brush(QPalette::AlternateBase));
330 p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
332 if(selectionMode == Partial) {
336 p->setClipRect(QRectF(pos, QSizeF(tsWidth, height())));
337 tsLayout.draw(p, pos, tsFormat);
338 p->setClipRect(QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, height())));
339 senderLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender(), 0), senderFormat);
340 p->setClipping(false);
341 textLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0), textFormat);
343 //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
344 //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
345 p->setBackgroundMode(Qt::OpaqueMode);
347 QRectF rect(pos, QSizeF(tsWidth, minHeight));
349 foreach(FormatRange fr, tsFormat) {
350 p->setFont(fr.format.font());
351 p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
352 p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledTimeStamp.text.mid(fr.start, fr.length), &brect);
353 rect.setLeft(brect.right());
355 rect = QRectF(pos + QPointF(tsWidth + QtUi::style()->sepTsSender(), 0), QSizeF(senderWidth, minHeight));
356 for(int i = senderFormat.count() - 1; i >= 0; i--) {
357 FormatRange fr = senderFormat[i];
358 p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
359 p->drawText(rect, Qt::AlignRight|Qt::TextSingleLine, styledSender.text.mid(fr.start, fr.length), &brect);
360 rect.setRight(brect.left());
362 QPointF tpos = pos + QPointF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText(), 0);
363 qreal h = 0; int l = 0;
364 if(lineLayouts.count() == 0) return; // how can this happen?
365 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
367 foreach(FormatRange fr, textFormat) {
368 if(l >= lineLayouts.count()) break;
369 p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
370 int start, end, frend, llend;
372 frend = fr.start + fr.length;
373 if(frend <= lineLayouts[l].start) break;
374 llend = lineLayouts[l].start + lineLayouts[l].length;
375 start = qMax(fr.start, lineLayouts[l].start); end = qMin(frend, llend);
376 rect.setLeft(tpos.x() + charPos[start] - offset);
377 p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledText.text.mid(start, end - start), &brect);
379 h += lineLayouts[l].height;
381 if(l < lineLayouts.count()) {
382 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
383 offset = charPos[lineLayouts[l].start];
386 } while(end < frend && l < lineLayouts.count());