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 QColor ChatLineOld::_highlightColor;
33 ChatLineOld::ChatLineOld(const Message &m) {
40 if(!_highlightColor.isValid()) {
41 QtUiSettings s("QtUi/Colors");
42 _highlightColor = s.value("highlightColor", QVariant(QColor("lightcoral"))).value<QColor>();
46 ChatLineOld::~ChatLineOld() {
49 void ChatLineOld::formatMsg(Message msg) {
50 isHighlight = msg.flags() & Message::Highlight;
51 QTextOption tsOption, senderOption, textOption;
52 styledTimeStamp = QtUi::style()->styleString(msg.formattedTimestamp());
53 styledSender = QtUi::style()->styleString(msg.formattedSender());
54 styledContents = QtUi::style()->styleString(msg.formattedText());
58 QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(const UiStyle::StyledText &fs) {
59 QTextLayout::FormatRange additional;
60 additional.start = additional.length = 0;
61 return calcFormatRanges(fs, additional);
64 // This function is almost obsolete, since with the new style engine, we already get a list of formats...
65 // We don't know yet if we keep this implementation of ChatLineOld, so I won't bother making this actually nice.
66 QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(const UiStyle::StyledText &_fs,
67 const QTextLayout::FormatRange &additional) {
68 UiStyle::StyledText fs = _fs;
69 QList<FormatRange> ranges;
71 if(additional.length > 0) {
72 for(int i = 0; i < fs.formatList.count(); i++) {
73 int oldend = fs.formatList[i].start + fs.formatList[i].length - 1;
74 int addend = additional.start + additional.length - 1;
75 if(oldend < additional.start) continue;
76 fs.formatList[i].length = additional.start - fs.formatList[i].start;
77 QTextLayout::FormatRange addfmtrng = fs.formatList[i];
78 addfmtrng.format.merge(additional.format);
79 addfmtrng.start = additional.start;
80 addfmtrng.length = qMin(oldend, addend) - additional.start + 1;
81 fs.formatList.insert(++i, addfmtrng);
82 if(addend == oldend) break;
84 QTextLayout::FormatRange restfmtrng = fs.formatList[i-1];
85 restfmtrng.start = addend + 1;
86 restfmtrng.length = oldend - addend;
87 fs.formatList.insert(++i, restfmtrng);
93 foreach(QTextLayout::FormatRange f, fs.formatList) {
94 if(f.length <= 0) continue;
96 range.start = f.start;
97 range.length = f.length;
98 range.format = f.format;
99 QFontMetrics metrics(range.format.font());
100 range.height = metrics.lineSpacing();
101 ranges.append(range);
106 void ChatLineOld::setSelection(SelectionMode mode, int start, int end) {
107 selectionMode = mode;
108 //tsFormat.clear(); senderFormat.clear(); textFormat.clear();
109 QPalette pal = QApplication::palette();
110 QTextLayout::FormatRange tsSel, senderSel, textSel;
113 tsFormat = calcFormatRanges(styledTimeStamp);
114 senderFormat = calcFormatRanges(styledSender);
115 textFormat = calcFormatRanges(styledContents);
118 selectionStart = qMin(start, end); selectionEnd = qMax(start, end);
119 textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
120 textSel.format.setBackground(pal.brush(QPalette::Highlight));
121 textSel.start = selectionStart;
122 textSel.length = selectionEnd - selectionStart;
123 textFormat = calcFormatRanges(styledContents, textSel);
126 tsSel.format.setForeground(pal.brush(QPalette::HighlightedText));
127 tsSel.format.setBackground(pal.brush(QPalette::Highlight));
128 tsSel.start = 0; tsSel.length = styledTimeStamp.plainText.length();
129 tsFormat = calcFormatRanges(styledTimeStamp, tsSel);
130 senderSel.format.setForeground(pal.brush(QPalette::HighlightedText));
131 senderSel.format.setBackground(pal.brush(QPalette::Highlight));
132 senderSel.start = 0; senderSel.length = styledSender.plainText.length();
133 senderFormat = calcFormatRanges(styledSender, senderSel);
134 textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
135 textSel.format.setBackground(pal.brush(QPalette::Highlight));
136 textSel.start = 0; textSel.length = styledContents.plainText.length();
137 textFormat = calcFormatRanges(styledContents, textSel);
142 MsgId ChatLineOld::msgId() const {
146 BufferInfo ChatLineOld::bufferInfo() const {
147 return msg.bufferInfo();
150 QDateTime ChatLineOld::timestamp() const {
151 return msg.timestamp();
154 QString ChatLineOld::sender() const {
155 return styledSender.plainText;
158 QString ChatLineOld::text() const {
159 return styledContents.plainText;
162 bool ChatLineOld::isUrl(int c) const {
163 if(c < 0 || c >= charUrlIdx.count()) return false;;
164 return charUrlIdx[c] >= 0;
167 QUrl ChatLineOld::getUrl(int c) const {
168 if(c < 0 || c >= charUrlIdx.count()) return QUrl();
169 int i = charUrlIdx[c];
170 if(i >= 0) return styledContents.urls[i].url;
174 //!\brief Return the cursor position for the given coordinate pos.
176 * \param pos The position relative to the ChatLineOld
177 * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
179 int ChatLineOld::posToCursor(QPointF pos) {
180 if(pos.x() < tsWidth + (int)QtUi::style()->sepTsSender()/2) return -2;
181 qreal textStart = tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText();
182 if(pos.x() < textStart) return -1;
183 int x = (int)(pos.x() - textStart);
184 for(int l = lineLayouts.count() - 1; l >=0; l--) {
185 LineLayout line = lineLayouts[l];
186 if(pos.y() >= line.y) {
187 int offset = charPos[line.start]; x += offset;
188 for(int i = line.start + line.length - 1; i >= line.start; i--) {
189 if((charPos[i] + charPos[i+1])/2 <= x) return i+1; // FIXME: Optimize this!
197 void ChatLineOld::precomputeLine() {
198 tsFormat = calcFormatRanges(styledTimeStamp);
199 senderFormat = calcFormatRanges(styledSender);
200 textFormat = calcFormatRanges(styledContents);
203 foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
204 foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
207 charPos.resize(styledContents.plainText.length() + 1);
208 charHeights.resize(styledContents.plainText.length());
209 charUrlIdx.fill(-1, styledContents.plainText.length());
210 for(int i = 0; i < styledContents.urls.count(); i++) {
211 QtUiStyle::UrlInfo url = styledContents.urls[i];
212 for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i;
214 if(!textFormat.count()) return;
215 int idx = 0; int cnt = 0; int w = 0;
216 QFontMetrics metrics(textFormat[0].format.font());
218 wr.start = -1; wr.trailing = -1;
219 for(int i = 0; i < styledContents.plainText.length(); ) {
220 charPos[i] = w; charHeights[i] = textFormat[idx].height;
221 w += metrics.charWidth(styledContents.plainText, i);
222 if(!styledContents.plainText[i].isSpace()) {
223 if(wr.trailing >= 0) {
224 // new word after space
229 wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
231 wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
235 wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
240 if(++i < styledContents.plainText.length() && ++cnt >= textFormat[idx].length) {
242 Q_ASSERT(idx < textFormat.count());
243 metrics = QFontMetrics(textFormat[idx].format.font());
246 charPos[styledContents.plainText.length()] = w;
247 if(wr.start >= 0) words.append(wr);
250 qreal ChatLineOld::layout(qreal tsw, qreal senderw, qreal textw) {
251 tsWidth = tsw; senderWidth = senderw; textWidth = textw;
252 if(textw <= 0) return minHeight;
253 lineLayouts.clear(); LineLayout line;
255 int offset = 0; int numWords = 0;
258 line.height = minHeight; // first line needs room for ts and sender
259 for(uint i = 0; i < (uint)words.count(); i++) {
260 int lastpos = charPos[words[i].start + words[i].length]; // We use charPos[lastchar + 1], 'coz last char needs to fit
261 if(lastpos - offset <= textw) {
262 line.height = qMax(line.height, words[i].height);
263 line.length = words[i].start + words[i].length - line.start;
268 // ok, we had some words before, so store the layout and start a new line
270 line.length = words[i-1].start + words[i-1].length - line.start;
271 lineLayouts.append(line);
272 line.y += line.height;
273 line.start = words[i].start;
274 line.height = words[i].height;
275 offset = charPos[words[i].start];
278 // check if the word fits into the current line
279 if(lastpos - offset <= textw) {
280 line.length = words[i].length;
282 // we need to break a word in the middle
283 int border = (int)textw + offset; // save some additions
284 line.start = words[i].start;
286 line.height = charHeights[line.start];
287 int j = line.start + 1;
288 for(int l = 1; l < words[i].length; j++, l++) {
289 if(charPos[j+1] < border) {
291 line.height = qMax(line.height, charHeights[j]);
295 lineLayouts.append(line);
296 line.y += line.height;
298 line.height = charHeights[j];
301 border = (int)textw + offset;
309 lineLayouts.append(line);
315 //!\brief Draw ChatLineOld on the given QPainter at the given position.
316 void ChatLineOld::draw(QPainter *p, const QPointF &pos) {
317 QPalette pal = QApplication::palette();
319 if(selectionMode == Full) {
320 p->setPen(Qt::NoPen);
321 p->setBrush(pal.brush(QPalette::Highlight));
322 p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
325 p->setPen(Qt::NoPen);
326 p->setBrush(_highlightColor /*pal.brush(QPalette::AlternateBase) */);
327 p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
329 if(selectionMode == Partial) {
333 p->setClipRect(QRectF(pos, QSizeF(tsWidth, height())));
334 tsLayout.draw(p, pos, tsFormat);
335 p->setClipRect(QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, height())));
336 senderLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender(), 0), senderFormat);
337 p->setClipping(false);
338 textLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0), textFormat);
340 //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
341 //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
342 p->setBackgroundMode(Qt::OpaqueMode);
344 QRectF rect(pos, QSizeF(tsWidth, minHeight));
346 foreach(FormatRange fr, tsFormat) {
347 p->setFont(fr.format.font());
348 p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
349 p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledTimeStamp.plainText.mid(fr.start, fr.length), &brect);
350 rect.setLeft(brect.right());
352 rect = QRectF(pos + QPointF(tsWidth + QtUi::style()->sepTsSender(), 0), QSizeF(senderWidth, minHeight));
353 for(int i = senderFormat.count() - 1; i >= 0; i--) {
354 FormatRange fr = senderFormat[i];
355 p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
356 p->drawText(rect, Qt::AlignRight|Qt::TextSingleLine, styledSender.plainText.mid(fr.start, fr.length), &brect);
357 rect.setRight(brect.left());
359 QPointF tpos = pos + QPointF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText(), 0);
360 qreal h = 0; int l = 0;
361 if(lineLayouts.count() == 0) return; // how can this happen?
362 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
364 foreach(FormatRange fr, textFormat) {
365 if(l >= lineLayouts.count()) break;
366 p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
367 int start, end, frend, llend;
369 frend = fr.start + fr.length;
370 if(frend <= lineLayouts[l].start) break;
371 llend = lineLayouts[l].start + lineLayouts[l].length;
372 start = qMax(fr.start, lineLayouts[l].start); end = qMin(frend, llend);
373 rect.setLeft(tpos.x() + charPos[start] - offset);
374 p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledContents.plainText.mid(start, end - start), &brect);
376 h += lineLayouts[l].height;
378 if(l < lineLayouts.count()) {
379 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
380 offset = charPos[lineLayouts[l].start];
383 } while(end < frend && l < lineLayouts.count());