X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fqtui%2Fchatline.cpp;h=24e4a5d104a94e435682381d00f14545c13de75c;hp=335e15b74f96bc40299c8cd2fdefc03b1d9e6ad8;hb=62192fb6cd9cc211b5b9fe844c9b4c2f98f923cc;hpb=9fd4619e9aca7d53d7c5df156a0b25956a1bf682 diff --git a/src/qtui/chatline.cpp b/src/qtui/chatline.cpp index 335e15b7..24e4a5d1 100644 --- a/src/qtui/chatline.cpp +++ b/src/qtui/chatline.cpp @@ -1,11 +1,11 @@ /*************************************************************************** - * Copyright (C) 2005-07 by The Quassel IRC Development Team * + * Copyright (C) 2005-08 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * + * (at your option) version 3. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * @@ -18,339 +18,127 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#include "chatline.h" - -//!\brief Construct a ChatLine object from a message. -/** - * \param m The message to be layouted and rendered - * \param net The network name - * \param buf The buffer name - */ -ChatLine::ChatLine(Message m) { - hght = 0; - //networkName = m.buffer.network(); - //bufferName = m.buffer.buffer(); - msg = m; - selectionMode = None; - formatMsg(msg); -} - -ChatLine::~ChatLine() { - -} +#include +#include +#include -void ChatLine::formatMsg(Message msg) { - QTextOption tsOption, senderOption, textOption; - styledTimeStamp = Style::formattedToStyled(msg.formattedTimeStamp()); - styledSender = Style::formattedToStyled(msg.formattedSender()); - styledText = Style::formattedToStyled(msg.formattedText()); - precomputeLine(); -} - -QList ChatLine::calcFormatRanges(const Style::StyledString &fs, QTextLayout::FormatRange additional) { - QList ranges; - QList formats = fs.formats; - formats.append(additional); - int cur = -1; - FormatRange range, lastrange; - for(int i = 0; i < fs.text.length(); i++) { - QTextCharFormat format; - foreach(QTextLayout::FormatRange f, formats) { - if(i >= f.start && i < f.start + f.length) format.merge(f.format); - } - if(cur < 0) { - range.start = 0; range.length = 1; range.format= format; - cur = 0; - } else { - if(format == range.format) range.length++; - else { - QFontMetrics metrics(range.format.font()); - range.height = metrics.lineSpacing(); - ranges.append(range); - range.start = i; range.length = 1; range.format = format; - cur++; - } - } - } - if(cur >= 0) { - QFontMetrics metrics(range.format.font()); - range.height = metrics.lineSpacing(); - ranges.append(range); - } - return ranges; -} - -void ChatLine::setSelection(SelectionMode mode, int start, int end) { - selectionMode = mode; - //tsFormat.clear(); senderFormat.clear(); textFormat.clear(); - QPalette pal = QApplication::palette(); - QTextLayout::FormatRange tsSel, senderSel, textSel; - switch (mode) { - case None: - tsFormat = calcFormatRanges(styledTimeStamp); - senderFormat = calcFormatRanges(styledSender); - textFormat = calcFormatRanges(styledText); - break; - case Partial: - selectionStart = qMin(start, end); selectionEnd = qMax(start, end); - textSel.format.setForeground(pal.brush(QPalette::HighlightedText)); - textSel.format.setBackground(pal.brush(QPalette::Highlight)); - textSel.start = selectionStart; - textSel.length = selectionEnd - selectionStart; - //textFormat.append(textSel); - textFormat = calcFormatRanges(styledText, textSel); - foreach(FormatRange fr, textFormat); - break; - case Full: - tsSel.format.setForeground(pal.brush(QPalette::HighlightedText)); - tsSel.format.setBackground(pal.brush(QPalette::Highlight)); - tsSel.start = 0; tsSel.length = styledTimeStamp.text.length(); - tsFormat = calcFormatRanges(styledTimeStamp, tsSel); - senderSel.format.setForeground(pal.brush(QPalette::HighlightedText)); - senderSel.format.setBackground(pal.brush(QPalette::Highlight)); - senderSel.start = 0; senderSel.length = styledSender.text.length(); - senderFormat = calcFormatRanges(styledSender, senderSel); - textSel.format.setForeground(pal.brush(QPalette::HighlightedText)); - textSel.format.setBackground(pal.brush(QPalette::Highlight)); - textSel.start = 0; textSel.length = styledText.text.length(); - textFormat = calcFormatRanges(styledText, textSel); - break; +#include "bufferinfo.h" +#include "buffersyncer.h" +#include "client.h" +#include "chatitem.h" +#include "chatline.h" +#include "columnhandleitem.h" +#include "messagemodel.h" +#include "networkmodel.h" +#include "qtui.h" +#include "qtuisettings.h" + +ChatLine::ChatLine(int row, QAbstractItemModel *model, QGraphicsItem *parent) + : QGraphicsItem(parent), + _row(row), // needs to be set before the items + _timestampItem(model, this), + _senderItem(model, this), + _contentsItem(model, this), + _width(0), + _height(0), + _selection(0) +{ + Q_ASSERT(model); + QModelIndex index = model->index(row, ChatLineModel::ContentsColumn); + setHighlighted(model->data(index, MessageModel::FlagsRole).toInt() & Message::Highlight); +} + +QRectF ChatLine::boundingRect () const { + //return childrenBoundingRect(); + return QRectF(0, 0, _width, _height); +} + +ChatItem &ChatLine::item(ChatLineModel::ColumnType column) { + switch(column) { + case ChatLineModel::TimestampColumn: + return _timestampItem; + case ChatLineModel::SenderColumn: + return _senderItem; + case ChatLineModel::ContentsColumn: + return _contentsItem; + default: + return *(ChatItem *)0; // provoke an error } } -uint ChatLine::msgId() const { - return msg.buffer().uid(); -} +qreal ChatLine::setGeometry(qreal width) { + if(width != _width) + prepareGeometryChange(); -BufferInfo ChatLine::bufferInfo() const { - return msg.buffer(); -} + ColumnHandleItem *firstColumnHandle = chatScene()->firstColumnHandle(); + ColumnHandleItem *secondColumnHandle = chatScene()->secondColumnHandle(); + _height = _contentsItem.setGeometry(width - secondColumnHandle->sceneRight()); + _timestampItem.setGeometry(firstColumnHandle->sceneLeft(), _height); + _senderItem.setGeometry(secondColumnHandle->sceneLeft() - firstColumnHandle->sceneRight(), _height); -QDateTime ChatLine::timeStamp() const { - return msg.timeStamp(); -} - -QString ChatLine::sender() const { - return styledSender.text; -} - -QString ChatLine::text() const { - return styledText.text; -} + _senderItem.setPos(firstColumnHandle->sceneRight(), 0); + _contentsItem.setPos(secondColumnHandle->sceneRight(), 0); -bool ChatLine::isUrl(int c) const { - if(c < 0 || c >= charUrlIdx.count()) return false;; - return charUrlIdx[c] >= 0; + _width = width; + return _height; } -QUrl ChatLine::getUrl(int c) const { - if(c < 0 || c >= charUrlIdx.count()) return QUrl(); - int i = charUrlIdx[c]; - if(i >= 0) return styledText.urls[i].url; - else return QUrl(); -} - -//!\brief Return the cursor position for the given coordinate pos. -/** - * \param pos The position relative to the ChatLine - * \return The cursor position, [or -3 for invalid,] or -2 for timestamp, or -1 for sender - */ -int ChatLine::posToCursor(QPointF pos) { - if(pos.x() < tsWidth + (int)Style::sepTsSender()/2) return -2; - qreal textStart = tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(); - if(pos.x() < textStart) return -1; - int x = (int)(pos.x() - textStart); - for(int l = lineLayouts.count() - 1; l >=0; l--) { - LineLayout line = lineLayouts[l]; - if(pos.y() >= line.y) { - int offset = charPos[line.start]; x += offset; - for(int i = line.start + line.length - 1; i >= line.start; i--) { - if((charPos[i] + charPos[i+1])/2 <= x) return i+1; // FIXME: Optimize this! - } - return line.start; - } - } - return 0; -} - -void ChatLine::precomputeLine() { - tsFormat = calcFormatRanges(styledTimeStamp); - senderFormat = calcFormatRanges(styledSender); - textFormat = calcFormatRanges(styledText); - - minHeight = 0; - foreach(FormatRange fr, tsFormat) minHeight = qMax(minHeight, fr.height); - foreach(FormatRange fr, senderFormat) minHeight = qMax(minHeight, fr.height); - - words.clear(); - charPos.resize(styledText.text.length() + 1); - charHeights.resize(styledText.text.length()); - charUrlIdx.fill(-1, styledText.text.length()); - for(int i = 0; i < styledText.urls.count(); i++) { - Style::UrlInfo url = styledText.urls[i]; - for(int j = url.start; j < url.end; j++) charUrlIdx[j] = i; - } - if(!textFormat.count()) return; - int idx = 0; int cnt = 0; int w = 0; - QFontMetrics metrics(textFormat[0].format.font()); - Word wr; - wr.start = -1; wr.trailing = -1; - for(int i = 0; i < styledText.text.length(); ) { - charPos[i] = w; charHeights[i] = textFormat[idx].height; - w += metrics.charWidth(styledText.text, i); - if(!styledText.text[i].isSpace()) { - if(wr.trailing >= 0) { - // new word after space - words.append(wr); - wr.start = -1; - } - if(wr.start < 0) { - wr.start = i; wr.length = 1; wr.trailing = -1; wr.height = textFormat[idx].height; - } else { - wr.length++; wr.height = qMax(wr.height, textFormat[idx].height); - } - } else { - if(wr.start < 0) { - wr.start = i; wr.length = 0; wr.trailing = 1; wr.height = 0; - } else { - wr.trailing++; - } +void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) { + if(selected) { + quint8 sel = (_selection & 0x80) | 0x40 | minColumn; + if(sel != _selection) { + _selection = sel; + for(int i = 0; i < minColumn; i++) + item((ChatLineModel::ColumnType)i).clearSelection(); + for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++) + item((ChatLineModel::ColumnType)i).setFullSelection(); + update(); } - if(++i < styledText.text.length() && ++cnt >= textFormat[idx].length) { - cnt = 0; idx++; - Q_ASSERT(idx < textFormat.count()); - metrics = QFontMetrics(textFormat[idx].format.font()); + } else { + quint8 sel = _selection & 0x80; + if(sel != _selection) { + _selection = sel; + for(int i = 0; i <= ChatLineModel::ContentsColumn; i++) + item((ChatLineModel::ColumnType)i).clearSelection(); + update(); } } - charPos[styledText.text.length()] = w; - if(wr.start >= 0) words.append(wr); } -qreal ChatLine::layout(qreal tsw, qreal senderw, qreal textw) { - tsWidth = tsw; senderWidth = senderw; textWidth = textw; - if(textw <= 0) return minHeight; - lineLayouts.clear(); LineLayout line; - int h = 0; - int offset = 0; int numWords = 0; - line.y = 0; - line.start = 0; - line.height = minHeight; // first line needs room for ts and sender - for(uint i = 0; i < (uint)words.count(); i++) { - int lastpos = charPos[words[i].start + words[i].length]; // We use charPos[lastchar + 1], 'coz last char needs to fit - if(lastpos - offset <= textw) { - line.height = qMax(line.height, words[i].height); - line.length = words[i].start + words[i].length - line.start; - numWords++; - } else { - // we need to wrap! - if(numWords > 0) { - // ok, we had some words before, so store the layout and start a new line - h += line.height; - line.length = words[i-1].start + words[i-1].length - line.start; - lineLayouts.append(line); - line.y += line.height; - line.start = words[i].start; - line.height = words[i].height; - offset = charPos[words[i].start]; - } - numWords = 1; - // check if the word fits into the current line - if(lastpos - offset <= textw) { - line.length = words[i].length; - } else { - // we need to break a word in the middle - int border = (int)textw + offset; // save some additions - line.start = words[i].start; - line.length = 1; - line.height = charHeights[line.start]; - int j = line.start + 1; - for(int l = 1; l < words[i].length; j++, l++) { - if(charPos[j+1] < border) { - line.length++; - line.height = qMax(line.height, charHeights[j]); - continue; - } else { - h += line.height; - lineLayouts.append(line); - line.y += line.height; - line.start = j; - line.height = charHeights[j]; - line.length = 1; - offset = charPos[j]; - border = (int)textw + offset; - } - } - } - } - } - h += line.height; - if(numWords > 0) { - lineLayouts.append(line); - } - hght = h; - return hght; +void ChatLine::setHighlighted(bool highlighted) { + if(highlighted) _selection |= 0x80; + else _selection &= 0x7f; + update(); } -//!\brief Draw ChatLine on the given QPainter at the given position. -void ChatLine::draw(QPainter *p, const QPointF &pos) { - QPalette pal = QApplication::palette(); - - if(selectionMode == Full) { - p->setPen(Qt::NoPen); - p->setBrush(pal.brush(QPalette::Highlight)); - p->drawRect(QRectF(pos, QSizeF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText() + textWidth, height()))); - } else if(selectionMode == Partial) { - - } /* - p->setClipRect(QRectF(pos, QSizeF(tsWidth, height()))); - tsLayout.draw(p, pos, tsFormat); - p->setClipRect(QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, height()))); - senderLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender(), 0), senderFormat); - p->setClipping(false); - textLayout.draw(p, pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0), textFormat); - */ - //p->setClipRect(QRectF(pos, QSizeF(tsWidth, 15))); - //p->drawRect(QRectF(pos, QSizeF(tsWidth, minHeight))); - p->setBackgroundMode(Qt::OpaqueMode); - QPointF tp = pos; - QRectF rect(pos, QSizeF(tsWidth, minHeight)); - QRectF brect; - foreach(FormatRange fr, tsFormat) { - p->setFont(fr.format.font()); - p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background()); - p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledTimeStamp.text.mid(fr.start, fr.length), &brect); - rect.setLeft(brect.right()); +void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { + Q_UNUSED(option); + Q_UNUSED(widget); + if(_selection & Highlighted) { + painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor())); } - rect = QRectF(pos + QPointF(tsWidth + Style::sepTsSender(), 0), QSizeF(senderWidth, minHeight)); - for(int i = senderFormat.count() - 1; i >= 0; i--) { - FormatRange fr = senderFormat[i]; - p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background()); - p->drawText(rect, Qt::AlignRight|Qt::TextSingleLine, styledSender.text.mid(fr.start, fr.length), &brect); - rect.setRight(brect.left()); + if(_selection & Selected) { + qreal left = item((ChatLineModel::ColumnType)(_selection & 0x3f)).x(); + QRectF selectRect(left, 0, width() - left, height()); + painter->fillRect(selectRect, QApplication::palette().brush(QPalette::Highlight)); } - QPointF tpos = pos + QPointF(tsWidth + Style::sepTsSender() + senderWidth + Style::sepSenderText(), 0); - qreal h = 0; int l = 0; - rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height)); - int offset = 0; - foreach(FormatRange fr, textFormat) { - if(l >= lineLayouts.count()) break; - p->setFont(fr.format.font()); p->setPen(QPen(fr.format.foreground(), 0)); p->setBackground(fr.format.background()); - int start, end, frend, llend; - do { - frend = fr.start + fr.length; - if(frend <= lineLayouts[l].start) break; - llend = lineLayouts[l].start + lineLayouts[l].length; - start = qMax(fr.start, lineLayouts[l].start); end = qMin(frend, llend); - rect.setLeft(tpos.x() + charPos[start] - offset); - p->drawText(rect, Qt::AlignLeft|Qt::TextSingleLine, styledText.text.mid(start, end - start), &brect); - if(llend <= end) { - h += lineLayouts[l].height; - l++; - if(l < lineLayouts.count()) { - rect = QRectF(tpos + QPointF(0, h), QSizeF(textWidth, lineLayouts[l].height)); - offset = charPos[lineLayouts[l].start]; - } + + // new line marker + const QAbstractItemModel *model_ = model(); + if(model_ && row() > 0) { + QModelIndex prevRowIdx = model_->index(row() - 1, 0); + MsgId msgId = model_->data(prevRowIdx, MessageModel::MsgIdRole).value(); + Message::Flags flags = (Message::Flags)model_->data(model_->index(row(), 0), MessageModel::FlagsRole).toInt(); + // don't show the marker if we wrote that new line + if(!(flags & Message::Self)) { + BufferId bufferId = model_->data(prevRowIdx, MessageModel::BufferIdRole).value(); + if(msgId == Client::networkModel()->lastSeenMsgId(bufferId) && chatScene()->isSingleBufferScene()) { + QtUiStyleSettings s("Colors"); + QLinearGradient gradient(0, 0, 0, height()); + gradient.setColorAt(0, s.value("newMsgMarkerFG", QColor(Qt::red)).value()); + gradient.setColorAt(0.1, Qt::transparent); + painter->fillRect(boundingRect(), gradient); } - } while(end < frend && l < lineLayouts.count()); + } } }