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 #include "qtuisettings.h"
28 //! Construct a ChatLineOld object from a message.
30 * \param m The message to be layouted and rendered
32 ChatLineOld::ChatLineOld(Message m) {
41 ChatLineOld::~ChatLineOld() {
45 void ChatLineOld::formatMsg(Message msg) {
46 isHighlight = msg.flags() & Message::Highlight;
47 QTextOption tsOption, senderOption, textOption;
48 styledTimeStamp = QtUi::style()->styleString(msg.formattedTimestamp());
49 styledSender = QtUi::style()->styleString(msg.formattedSender());
50 styledText = QtUi::style()->styleString(msg.formattedText());
54 QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(const UiStyle::StyledText &fs) {
55 QTextLayout::FormatRange additional;
56 additional.start = additional.length = 0;
57 return calcFormatRanges(fs, additional);
60 // This function is almost obsolete, since with the new style engine, we already get a list of formats...
61 // We don't know yet if we keep this implementation of ChatLineOld, so I won't bother making this actually nice.
62 QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(const UiStyle::StyledText &_fs,
63 const QTextLayout::FormatRange &additional) {
64 UiStyle::StyledText fs = _fs;
65 QList<FormatRange> ranges;
67 if(additional.length > 0) {
68 for(int i = 0; i < fs.formats.count(); i++) {
69 int oldend = fs.formats[i].start + fs.formats[i].length - 1;
70 int addend = additional.start + additional.length - 1;
71 if(oldend < additional.start) continue;
72 fs.formats[i].length = additional.start - fs.formats[i].start;
73 QTextLayout::FormatRange addfmtrng = fs.formats[i];
74 addfmtrng.format.merge(additional.format);
75 addfmtrng.start = additional.start;
76 addfmtrng.length = qMin(oldend, addend) - additional.start + 1;
77 fs.formats.insert(++i, addfmtrng);
78 if(addend == oldend) break;
80 QTextLayout::FormatRange restfmtrng = fs.formats[i-1];
81 restfmtrng.start = addend + 1;
82 restfmtrng.length = oldend - addend;
83 fs.formats.insert(++i, restfmtrng);
89 foreach(QTextLayout::FormatRange f, fs.formats) {
90 if(f.length <= 0) continue;
92 range.start = f.start;
93 range.length = f.length;
94 range.format = f.format;
95 QFontMetrics metrics(range.format.font());
96 range.height = metrics.lineSpacing();
102 void ChatLineOld::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 = calcFormatRanges(styledText, textSel);
122 tsSel.format.setForeground(pal.brush(QPalette::HighlightedText));
123 tsSel.format.setBackground(pal.brush(QPalette::Highlight));
124 tsSel.start = 0; tsSel.length = styledTimeStamp.text.length();
125 tsFormat = calcFormatRanges(styledTimeStamp, tsSel);
126 senderSel.format.setForeground(pal.brush(QPalette::HighlightedText));
127 senderSel.format.setBackground(pal.brush(QPalette::Highlight));
128 senderSel.start = 0; senderSel.length = styledSender.text.length();
129 senderFormat = calcFormatRanges(styledSender, senderSel);
130 textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
131 textSel.format.setBackground(pal.brush(QPalette::Highlight));
132 textSel.start = 0; textSel.length = styledText.text.length();
133 textFormat = calcFormatRanges(styledText, textSel);
138 MsgId ChatLineOld::msgId() const {
142 BufferInfo ChatLineOld::bufferInfo() const {
143 return msg.bufferInfo();
146 QDateTime ChatLineOld::timestamp() const {
147 return msg.timestamp();
150 QString ChatLineOld::sender() const {
151 return styledSender.text;
154 QString ChatLineOld::text() const {
155 return styledText.text;
158 bool ChatLineOld::isUrl(int c) const {
159 if(c < 0 || c >= charUrlIdx.count()) return false;;
160 return charUrlIdx[c] >= 0;
163 QUrl ChatLineOld::getUrl(int c) const {
164 if(c < 0 || c >= charUrlIdx.count()) return QUrl();
165 int i = charUrlIdx[c];
166 if(i >= 0) return styledText.urls[i].url;
170 //!\brief Return the cursor position for the given coordinate pos.
172 * \param pos The position relative to the ChatLineOld
173 * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
175 int ChatLineOld::posToCursor(QPointF pos) {
176 if(pos.x() < tsWidth + (int)QtUi::style()->sepTsSender()/2) return -2;
177 qreal textStart = tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText();
178 if(pos.x() < textStart) return -1;
179 int x = (int)(pos.x() - textStart);
180 for(int l = lineLayouts.count() - 1; l >=0; l--) {
181 LineLayout line = lineLayouts[l];
182 if(pos.y() >= line.y) {
183 int offset = charPos[line.start]; x += offset;
184 for(int i = line.start + line.length - 1; i >= line.start; i--) {
185 if((charPos[i] + charPos[i+1])/2 <= x) return i+1; // FIXME: Optimize this!
193 void ChatLineOld::precomputeLine() {
194 tsFormat = calcFormatRanges(styledTimeStamp);
195 senderFormat = calcFormatRanges(styledSender);
196 textFormat = calcFormatRanges(styledText);
199 foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
200 foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
203 charPos.resize(styledText.text.length() + 1);
204 charHeights.resize(styledText.text.length());
205 charUrlIdx.fill(-1, styledText.text.length());
206 for(int i = 0; i < styledText.urls.count(); i++) {
207 QtUiStyle::UrlInfo url = styledText.urls[i];
208 for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i;
210 if(!textFormat.count()) return;
211 int idx = 0; int cnt = 0; int w = 0;
212 QFontMetrics metrics(textFormat[0].format.font());
214 wr.start = -1; wr.trailing = -1;
215 for(int i = 0; i < styledText.text.length(); ) {
216 charPos[i] = w; charHeights[i] = textFormat[idx].height;
217 w += metrics.charWidth(styledText.text, i);
218 if(!styledText.text[i].isSpace()) {
219 if(wr.trailing >= 0) {
220 // new word after space
225 wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
227 wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
231 wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
236 if(++i < styledText.text.length() && ++cnt >= textFormat[idx].length) {
238 Q_ASSERT(idx < textFormat.count());
239 metrics = QFontMetrics(textFormat[idx].format.font());
242 charPos[styledText.text.length()] = w;
243 if(wr.start >= 0) words.append(wr);
246 qreal ChatLineOld::layout(qreal tsw, qreal senderw, qreal textw) {
247 tsWidth = tsw; senderWidth = senderw; textWidth = textw;
248 if(textw <= 0) return minHeight;
249 lineLayouts.clear(); LineLayout line;
251 int offset = 0; int numWords = 0;
254 line.height = minHeight; // first line needs room for ts and sender
255 for(uint i = 0; i < (uint)words.count(); i++) {
256 int lastpos = charPos[words[i].start + words[i].length]; // We use charPos[lastchar + 1], 'coz last char needs to fit
257 if(lastpos - offset <= textw) {
258 line.height = qMax(line.height, words[i].height);
259 line.length = words[i].start + words[i].length - line.start;
264 // ok, we had some words before, so store the layout and start a new line
266 line.length = words[i-1].start + words[i-1].length - line.start;
267 lineLayouts.append(line);
268 line.y += line.height;
269 line.start = words[i].start;
270 line.height = words[i].height;
271 offset = charPos[words[i].start];
274 // check if the word fits into the current line
275 if(lastpos - offset <= textw) {
276 line.length = words[i].length;
278 // we need to break a word in the middle
279 int border = (int)textw + offset; // save some additions
280 line.start = words[i].start;
282 line.height = charHeights[line.start];
283 int j = line.start + 1;
284 for(int l = 1; l < words[i].length; j++, l++) {
285 if(charPos[j+1] < border) {
287 line.height = qMax(line.height, charHeights[j]);
291 lineLayouts.append(line);
292 line.y += line.height;
294 line.height = charHeights[j];
297 border = (int)textw + offset;
305 lineLayouts.append(line);
311 //!\brief Draw ChatLineOld on the given QPainter at the given position.
312 void ChatLineOld::draw(QPainter *p, const QPointF &pos) {
313 QPalette pal = QApplication::palette();
315 if(selectionMode == Full) {
316 p->setPen(Qt::NoPen);
317 p->setBrush(pal.brush(QPalette::Highlight));
318 p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
321 QtUiSettings s("QtUi/Colors");
322 QColor highlightColor = s.value("highlightColor", QVariant(QColor("lightcoral"))).value<QColor>();
323 p->setPen(Qt::NoPen);
324 p->setBrush(highlightColor /*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());