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) {
40 QtUiSettings s("QtUi/Colors");
41 _highlightColor = s.value("highlightColor", QVariant(QColor("lightcoral"))).value<QColor>();
44 ChatLineOld::~ChatLineOld() {
48 void ChatLineOld::formatMsg(Message msg) {
49 isHighlight = msg.flags() & Message::Highlight;
50 QTextOption tsOption, senderOption, textOption;
51 styledTimeStamp = QtUi::style()->styleString(msg.formattedTimestamp());
52 styledSender = QtUi::style()->styleString(msg.formattedSender());
53 styledText = QtUi::style()->styleString(msg.formattedText());
57 QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(const UiStyle::StyledText &fs) {
58 QTextLayout::FormatRange additional;
59 additional.start = additional.length = 0;
60 return calcFormatRanges(fs, additional);
63 // This function is almost obsolete, since with the new style engine, we already get a list of formats...
64 // We don't know yet if we keep this implementation of ChatLineOld, so I won't bother making this actually nice.
65 QList<ChatLineOld::FormatRange> ChatLineOld::calcFormatRanges(const UiStyle::StyledText &_fs,
66 const QTextLayout::FormatRange &additional) {
67 UiStyle::StyledText fs = _fs;
68 QList<FormatRange> ranges;
70 if(additional.length > 0) {
71 for(int i = 0; i < fs.formats.count(); i++) {
72 int oldend = fs.formats[i].start + fs.formats[i].length - 1;
73 int addend = additional.start + additional.length - 1;
74 if(oldend < additional.start) continue;
75 fs.formats[i].length = additional.start - fs.formats[i].start;
76 QTextLayout::FormatRange addfmtrng = fs.formats[i];
77 addfmtrng.format.merge(additional.format);
78 addfmtrng.start = additional.start;
79 addfmtrng.length = qMin(oldend, addend) - additional.start + 1;
80 fs.formats.insert(++i, addfmtrng);
81 if(addend == oldend) break;
83 QTextLayout::FormatRange restfmtrng = fs.formats[i-1];
84 restfmtrng.start = addend + 1;
85 restfmtrng.length = oldend - addend;
86 fs.formats.insert(++i, restfmtrng);
92 foreach(QTextLayout::FormatRange f, fs.formats) {
93 if(f.length <= 0) continue;
95 range.start = f.start;
96 range.length = f.length;
97 range.format = f.format;
98 QFontMetrics metrics(range.format.font());
99 range.height = metrics.lineSpacing();
100 ranges.append(range);
105 void ChatLineOld::setSelection(SelectionMode mode, int start, int end) {
106 selectionMode = mode;
107 //tsFormat.clear(); senderFormat.clear(); textFormat.clear();
108 QPalette pal = QApplication::palette();
109 QTextLayout::FormatRange tsSel, senderSel, textSel;
112 tsFormat = calcFormatRanges(styledTimeStamp);
113 senderFormat = calcFormatRanges(styledSender);
114 textFormat = calcFormatRanges(styledText);
117 selectionStart = qMin(start, end); selectionEnd = qMax(start, end);
118 textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
119 textSel.format.setBackground(pal.brush(QPalette::Highlight));
120 textSel.start = selectionStart;
121 textSel.length = selectionEnd - selectionStart;
122 textFormat = calcFormatRanges(styledText, textSel);
125 tsSel.format.setForeground(pal.brush(QPalette::HighlightedText));
126 tsSel.format.setBackground(pal.brush(QPalette::Highlight));
127 tsSel.start = 0; tsSel.length = styledTimeStamp.text.length();
128 tsFormat = calcFormatRanges(styledTimeStamp, tsSel);
129 senderSel.format.setForeground(pal.brush(QPalette::HighlightedText));
130 senderSel.format.setBackground(pal.brush(QPalette::Highlight));
131 senderSel.start = 0; senderSel.length = styledSender.text.length();
132 senderFormat = calcFormatRanges(styledSender, senderSel);
133 textSel.format.setForeground(pal.brush(QPalette::HighlightedText));
134 textSel.format.setBackground(pal.brush(QPalette::Highlight));
135 textSel.start = 0; textSel.length = styledText.text.length();
136 textFormat = calcFormatRanges(styledText, textSel);
141 MsgId ChatLineOld::msgId() const {
145 BufferInfo ChatLineOld::bufferInfo() const {
146 return msg.bufferInfo();
149 QDateTime ChatLineOld::timestamp() const {
150 return msg.timestamp();
153 QString ChatLineOld::sender() const {
154 return styledSender.text;
157 QString ChatLineOld::text() const {
158 return styledText.text;
161 bool ChatLineOld::isUrl(int c) const {
162 if(c < 0 || c >= charUrlIdx.count()) return false;;
163 return charUrlIdx[c] >= 0;
166 QUrl ChatLineOld::getUrl(int c) const {
167 if(c < 0 || c >= charUrlIdx.count()) return QUrl();
168 int i = charUrlIdx[c];
169 if(i >= 0) return styledText.urls[i].url;
173 //!\brief Return the cursor position for the given coordinate pos.
175 * \param pos The position relative to the ChatLineOld
176 * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender
178 int ChatLineOld::posToCursor(QPointF pos) {
179 if(pos.x() < tsWidth + (int)QtUi::style()->sepTsSender()/2) return -2;
180 qreal textStart = tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText();
181 if(pos.x() < textStart) return -1;
182 int x = (int)(pos.x() - textStart);
183 for(int l = lineLayouts.count() - 1; l >=0; l--) {
184 LineLayout line = lineLayouts[l];
185 if(pos.y() >= line.y) {
186 int offset = charPos[line.start]; x += offset;
187 for(int i = line.start + line.length - 1; i >= line.start; i--) {
188 if((charPos[i] + charPos[i+1])/2 <= x) return i+1; // FIXME: Optimize this!
196 void ChatLineOld::precomputeLine() {
197 tsFormat = calcFormatRanges(styledTimeStamp);
198 senderFormat = calcFormatRanges(styledSender);
199 textFormat = calcFormatRanges(styledText);
202 foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height);
203 foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height);
206 charPos.resize(styledText.text.length() + 1);
207 charHeights.resize(styledText.text.length());
208 charUrlIdx.fill(-1, styledText.text.length());
209 for(int i = 0; i < styledText.urls.count(); i++) {
210 QtUiStyle::UrlInfo url = styledText.urls[i];
211 for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i;
213 if(!textFormat.count()) return;
214 int idx = 0; int cnt = 0; int w = 0;
215 QFontMetrics metrics(textFormat[0].format.font());
217 wr.start = -1; wr.trailing = -1;
218 for(int i = 0; i < styledText.text.length(); ) {
219 charPos[i] = w; charHeights[i] = textFormat[idx].height;
220 w += metrics.charWidth(styledText.text, i);
221 if(!styledText.text[i].isSpace()) {
222 if(wr.trailing >= 0) {
223 // new word after space
228 wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height;
230 wr.length++; wr.height = qMax(wr.height, textFormat[idx].height);
234 wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0;
239 if(++i < styledText.text.length() && ++cnt >= textFormat[idx].length) {
241 Q_ASSERT(idx < textFormat.count());
242 metrics = QFontMetrics(textFormat[idx].format.font());
245 charPos[styledText.text.length()] = w;
246 if(wr.start >= 0) words.append(wr);
249 qreal ChatLineOld::layout(qreal tsw, qreal senderw, qreal textw) {
250 tsWidth = tsw; senderWidth = senderw; textWidth = textw;
251 if(textw <= 0) return minHeight;
252 lineLayouts.clear(); LineLayout line;
254 int offset = 0; int numWords = 0;
257 line.height = minHeight; // first line needs room for ts and sender
258 for(uint i = 0; i < (uint)words.count(); i++) {
259 int lastpos = charPos[words[i].start + words[i].length]; // We use charPos[lastchar + 1], 'coz last char needs to fit
260 if(lastpos - offset <= textw) {
261 line.height = qMax(line.height, words[i].height);
262 line.length = words[i].start + words[i].length - line.start;
267 // ok, we had some words before, so store the layout and start a new line
269 line.length = words[i-1].start + words[i-1].length - line.start;
270 lineLayouts.append(line);
271 line.y += line.height;
272 line.start = words[i].start;
273 line.height = words[i].height;
274 offset = charPos[words[i].start];
277 // check if the word fits into the current line
278 if(lastpos - offset <= textw) {
279 line.length = words[i].length;
281 // we need to break a word in the middle
282 int border = (int)textw + offset; // save some additions
283 line.start = words[i].start;
285 line.height = charHeights[line.start];
286 int j = line.start + 1;
287 for(int l = 1; l < words[i].length; j++, l++) {
288 if(charPos[j+1] < border) {
290 line.height = qMax(line.height, charHeights[j]);
294 lineLayouts.append(line);
295 line.y += line.height;
297 line.height = charHeights[j];
300 border = (int)textw + offset;
308 lineLayouts.append(line);
314 //!\brief Draw ChatLineOld on the given QPainter at the given position.
315 void ChatLineOld::draw(QPainter *p, const QPointF &pos) {
316 QPalette pal = QApplication::palette();
318 if(selectionMode == Full) {
319 p->setPen(Qt::NoPen);
320 p->setBrush(pal.brush(QPalette::Highlight));
321 p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
324 p->setPen(Qt::NoPen);
325 p->setBrush(_highlightColor /*pal.brush(QPalette::AlternateBase) */);
326 p->drawRect(QRectF(pos, QSizeF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText() + textWidth, height())));
328 if(selectionMode == Partial) {
332 p->setClipRect(QRectF(pos, QSizeF(tsWidth, height())));
333 tsLayout.draw(p, pos, tsFormat);
334 p->setClipRect(QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, height())));
335 senderLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender(), 0), senderFormat);
336 p->setClipping(false);
337 textLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0), textFormat);
339 //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15)));
340 //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight)));
341 p->setBackgroundMode(Qt::OpaqueMode);
343 QRectF rect(pos, QSizeF(tsWidth, minHeight));
345 foreach(FormatRange fr, tsFormat) {
346 p->setFont(fr.format.font());
347 p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
348 p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledTimeStamp.text.mid(fr.start, fr.length), &brect);
349 rect.setLeft(brect.right());
351 rect = QRectF(pos + QPointF(tsWidth + QtUi::style()->sepTsSender(), 0), QSizeF(senderWidth, minHeight));
352 for(int i = senderFormat.count() - 1; i >= 0; i--) {
353 FormatRange fr = senderFormat[i];
354 p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
355 p->drawText(rect, Qt::AlignRight|Qt::TextSingleLine, styledSender.text.mid(fr.start, fr.length), &brect);
356 rect.setRight(brect.left());
358 QPointF tpos = pos + QPointF(tsWidth + QtUi::style()->sepTsSender() + senderWidth + QtUi::style()->sepSenderText(), 0);
359 qreal h = 0; int l = 0;
360 if(lineLayouts.count() == 0) return; // how can this happen?
361 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
363 foreach(FormatRange fr, textFormat) {
364 if(l >= lineLayouts.count()) break;
365 p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background());
366 int start, end, frend, llend;
368 frend = fr.start + fr.length;
369 if(frend <= lineLayouts[l].start) break;
370 llend = lineLayouts[l].start + lineLayouts[l].length;
371 start = qMax(fr.start, lineLayouts[l].start); end = qMin(frend, llend);
372 rect.setLeft(tpos.x() + charPos[start] - offset);
373 p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledText.text.mid(start, end - start), &brect);
375 h += lineLayouts[l].height;
377 if(l < lineLayouts.count()) {
378 rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height));
379 offset = charPos[lineLayouts[l].start];
382 } while(end < frend && l < lineLayouts.count());