1 /***************************************************************************
2 * Copyright (C) 2005-09 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 ***************************************************************************/
25 #include "bufferinfo.h"
26 #include "buffersyncer.h"
30 #include "columnhandleitem.h"
31 #include "messagemodel.h"
32 #include "networkmodel.h"
34 #include "qtuisettings.h"
35 #include "qtuistyle.h"
37 ChatLine::ChatLine(int row, QAbstractItemModel *model,
39 const qreal ×tampWidth, const qreal &senderWidth, const qreal &contentsWidth,
40 const QPointF &senderPos, const QPointF &contentsPos,
41 QGraphicsItem *parent)
42 : QGraphicsItem(parent),
43 _row(row), // needs to be set before the items
45 _contentsItem(contentsWidth, contentsPos, this),
46 _senderItem(senderWidth, _contentsItem.height(), senderPos, this),
47 _timestampItem(timestampWidth, _contentsItem.height(), this),
49 _height(_contentsItem.height()),
53 QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
55 setHighlighted(model->data(index, MessageModel::FlagsRole).toInt() & Message::Highlight);
58 ChatItem &ChatLine::item(ChatLineModel::ColumnType column) {
60 case ChatLineModel::TimestampColumn:
61 return _timestampItem;
62 case ChatLineModel::SenderColumn:
64 case ChatLineModel::ContentsColumn:
67 return *(ChatItem *)0; // provoke an error
71 // NOTE: senderPos is in ChatLines coordinate system!
72 void ChatLine::setFirstColumn(const qreal ×tampWidth, const qreal &senderWidth, const QPointF &senderPos) {
73 _timestampItem.prepareGeometryChange();
74 _timestampItem.setGeometry(timestampWidth, _height);
75 // senderItem doesn't need a geom change as it's Pos is changed (ensured by void ChatScene::firstHandlePositionChanged(qreal xpos))
76 _senderItem.setGeometry(senderWidth, _height);
77 _senderItem.setPos(senderPos);
80 // NOTE: contentsPos is in ChatLines coordinate system!
81 void ChatLine::setSecondColumn(const qreal &senderWidth, const qreal &contentsWidth,
82 const QPointF &contentsPos, qreal &linePos) {
83 // contentsItem doesn't need a geom change as it's Pos is changed (ensured by void ChatScene::firstHandlePositionChanged(qreal xpos))
84 qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
86 bool needGeometryChange = linePos == pos().y();
88 if(needGeometryChange) {
89 _timestampItem.prepareGeometryChange();
90 _senderItem.prepareGeometryChange();
92 _timestampItem.setHeight(height);
93 _senderItem.setGeometry(senderWidth, height);
95 _contentsItem.setPos(contentsPos);
97 if(needGeometryChange)
98 prepareGeometryChange();
105 void ChatLine::setGeometryByWidth(const qreal &width, const qreal &contentsWidth, qreal &linePos) {
106 qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
108 bool needGeometryChange = linePos == pos().y();
110 if(needGeometryChange) {
111 _timestampItem.prepareGeometryChange();
112 _senderItem.prepareGeometryChange();
114 _timestampItem.setHeight(height);
115 _senderItem.setHeight(height);
117 if(needGeometryChange)
118 prepareGeometryChange();
123 setPos(0, linePos); // set pos is _very_ cheap if nothing changes.
126 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
128 quint8 sel = (_selection & Highlighted) | Selected | minColumn;
129 if(sel != _selection) {
131 for(int i = 0; i < minColumn; i++)
132 item((ChatLineModel::ColumnType)i).clearSelection();
133 for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
134 item((ChatLineModel::ColumnType)i).setFullSelection();
138 quint8 sel = _selection & Highlighted;
139 if(sel != _selection) {
141 for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
142 item((ChatLineModel::ColumnType)i).clearSelection();
148 void ChatLine::setHighlighted(bool highlighted) {
149 if(highlighted) _selection |= Highlighted;
150 else _selection &= ~Highlighted;
154 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
158 const QAbstractItemModel *model_ = model();
159 QModelIndex myIdx = model_->index(row(), 0);
160 Message::Type type = (Message::Type)myIdx.data(MessageModel::TypeRole).toInt();
161 UiStyle::MessageLabel label = (UiStyle::MessageLabel)myIdx.data(ChatLineModel::MsgLabelRole).toInt();
163 QTextCharFormat msgFmt = QtUi::style()->format(UiStyle::formatType(type), label);
164 if(msgFmt.hasProperty(QTextFormat::BackgroundBrush)) {
165 painter->fillRect(boundingRect(), msgFmt.background());
168 if(_selection & Selected) {
169 QTextCharFormat selFmt = QtUi::style()->format(UiStyle::formatType(type), label | UiStyle::Selected);
170 if(selFmt.hasProperty(QTextFormat::BackgroundBrush)) {
171 qreal left = item((ChatLineModel::ColumnType)(_selection & ItemMask)).x();
172 QRectF selectRect(left, 0, width() - left, height());
173 painter->fillRect(selectRect, selFmt.background());
178 if(model_ && row() > 0 && chatScene()->isSingleBufferScene()) {
179 QModelIndex prevRowIdx = model_->index(row() - 1, 0);
180 MsgId prevMsgId = prevRowIdx.data(MessageModel::MsgIdRole).value<MsgId>();
181 MsgId myMsgId = myIdx.data(MessageModel::MsgIdRole).value<MsgId>();
182 Message::Flags flags = (Message::Flags)myIdx.data(MessageModel::FlagsRole).toInt();
184 // don't show the marker if we wrote that new line
185 if(!(flags & Message::Self)) {
186 BufferId bufferId = BufferId(chatScene()->idString().toInt());
187 MsgId lastSeenMsgId = Client::networkModel()->lastSeenMarkerMsgId(bufferId);
188 if(lastSeenMsgId < myMsgId && lastSeenMsgId >= prevMsgId) {
189 QtUiStyleSettings s("Colors");
190 QLinearGradient gradient(0, 0, 0, contentsItem().fontMetrics()->lineSpacing());
191 gradient.setColorAt(0, s.value("newMsgMarkerFG", QColor(Qt::red)).value<QColor>());
192 gradient.setColorAt(0.1, Qt::transparent);
193 painter->fillRect(boundingRect(), gradient);