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 ChatLineOld object from a message.
28 * \param m The message to be layouted and rendered
30 ChatLineOld::ChatLineOld(Message m) {
39 ChatLineOld::~ChatLineOld() {
43 void ChatLineOld::formatMsg(Message msg) {
44 isHighlight = msg.flags() & Message::Highlight;
45 QTextOption tsOption, senderOption, textOption;
46 styledTimeStamp = QtUi::style()->styleString(msg.formattedTimestamp());
47 styledSender = QtUi::style()->styleString(msg.formattedSender());
48 styledText = QtUi::style()->styleString(msg.formattedText());
52 QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(const UiStyle::StyledText &fs) {
53 QTextLayout::FormatRange additional;
54 additional.start = additional.length = 0;
55 return calcFormatRanges(fs, additional);
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 ChatLineOld, so I won't bother making this actually nice.
60 QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(const UiStyle::StyledText &_fs,
61 const QTextLayout::FormatRange &additional) {
62 UiStyle::StyledText fs = _fs;
63 QList<FormatRange> ranges;
65 if(additional.length > 0) {
66 for(int i = 0; i < fs.formats.count(); i++) {
67 int oldend = fs.formats[i].start + fs.formats[i].length - 1;
68 int addend = additional.start + additional.length - 1;
69 if(oldend < additional.start) continue;
70 fs.formats[i].length = additional.start - fs.formats[i].start;
71 QTextLayout::FormatRange addfmtrng = fs.formats[i];
72 addfmtrng.format.merge(additional.format);
73 addfmtrng.start = additional.start;
74 addfmtrng.length = qMin(oldend, addend) - additional.start + 1;
75 fs.formats.insert(++i, addfmtrng);
76 if(addend == oldend) break;
78 QTextLayout::FormatRange restfmtrng = fs.formats[i-1];
79 restfmtrng.start = addend + 1;
80 restfmtrng.length = oldend - addend;
81 fs.formats.insert(++i, restfmtrng);
87 foreach(QTextLayout::FormatRange f, fs.formats) {
88 if(f.length <= 0) continue;
90 range.start = f.start;
91 range.length = f.length;
92 range.format = f.format;
93 QFontMetrics metrics(range.format.font());
94 range.height = metrics.lineSpacing();
100 void ChatLineOld::setSelection(SelectionMode mode, int start, int end) {
101 selectionMode = mode;
102 //tsFormat.clear(); senderFormat.clear(); textFormat.clear();
103 QPalette pal = QApplication::palette();
104 QTextLayout::FormatRange tsSel, senderSel, textSel;
107 tsFormat = calcFormatRanges(styledTimeStamp);
108 senderFormat = calcFormatRanges(styledSender);
109 textFormat = calcFormatRanges(styledText);
112 selectionStart = qMin(start, end); selectionEnd = qMax(start, end);
113 textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
114 textSel.format.setBackground(pal.brush(QPalette::Highlight));
115 textSel.start = selectionStart;
116 textSel.length = selectionEnd - selectionStart;
117 textFormat = calcFormatRanges(styledText, textSel);
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 ChatLineOld::msgId() const {
140 BufferInfo ChatLineOld::bufferInfo() const {
141 return msg.bufferInfo();
144 QDateTime ChatLineOld::timestamp() const {
145 return msg.timestamp();
148 QString ChatLineOld::sender() const {
149 return styledSender.text;
152 QString ChatLineOld::text() const {
153 return styledText.text;
156 bool ChatLineOld::isUrl(int c) const {
157 if(c < 0 || c >= charUrlIdx.count()) return false;;
158 return charUrlIdx[c] >= 0;
161 QUrl ChatLineOld::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 ChatLineOld
171 * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
173 int ChatLineOld::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 ChatLineOld::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 ChatLineOld::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 ChatLineOld on the given QPainter at the given position.
310 void ChatLineOld::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())));
319 p->setPen(Qt::NoPen);
320 p->setBrush(QColor("lightcoral") /*pal.brush(QPalette::AlternateBase) */);
321 p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
323 if(selectionMode == Partial) {
327 p->setClipRect(QRectF(pos, QSizeF(tsWidth, height())));
328 tsLayout.draw(p, pos, tsFormat);
329 p->setClipRect(QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, height())));
330 senderLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender(), 0), senderFormat);
331 p->setClipping(false);
332 textLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0), textFormat);
334 //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
335 //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
336 p->setBackgroundMode(Qt::OpaqueMode);
338 QRectF rect(pos, QSizeF(tsWidth, minHeight));
340 foreach(FormatRange fr, tsFormat) {
341 p->setFont(fr.format.font());
342 p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
343 p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledTimeStamp.text.mid(fr.start, fr.length), &brect);
344 rect.setLeft(brect.right());
346 rect = QRectF(pos + QPointF(tsWidth + QtUi::style()->sepTsSender(), 0), QSizeF(senderWidth, minHeight));
347 for(int i = senderFormat.count() - 1; i >= 0; i--) {
348 FormatRange fr = senderFormat[i];
349 p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
350 p->drawText(rect, Qt::AlignRight|Qt::TextSingleLine, styledSender.text.mid(fr.start, fr.length), &brect);
351 rect.setRight(brect.left());
353 QPointF tpos = pos + QPointF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText(), 0);
354 qreal h = 0; int l = 0;
355 if(lineLayouts.count() == 0) return; // how can this happen?
356 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
358 foreach(FormatRange fr, textFormat) {
359 if(l >= lineLayouts.count()) break;
360 p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
361 int start, end, frend, llend;
363 frend = fr.start + fr.length;
364 if(frend <= lineLayouts[l].start) break;
365 llend = lineLayouts[l].start + lineLayouts[l].length;
366 start = qMax(fr.start, lineLayouts[l].start); end = qMin(frend, llend);
367 rect.setLeft(tpos.x() + charPos[start] - offset);
368 p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledText.text.mid(start, end - start), &brect);
370 h += lineLayouts[l].height;
372 if(l < lineLayouts.count()) {
373 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
374 offset = charPos[lineLayouts[l].start];
377 } while(end < frend && l < lineLayouts.count());