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) {
157 if(_selection & Highlighted) {
158 painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
160 if(_selection & Selected) {
161 qreal left = item((ChatLineModel::ColumnType)(_selection & ItemMask)).x();
162 QRectF selectRect(left, 0, width() - left, height());
163 painter->fillRect(selectRect, QApplication::palette().brush(QPalette::Highlight));
167 const QAbstractItemModel *model_ = model();
168 if(model_ && row() > 0 && chatScene()->isSingleBufferScene()) {
169 QModelIndex prevRowIdx = model_->index(row() - 1, 0);
170 MsgId prevMsgId = model_->data(prevRowIdx, MessageModel::MsgIdRole).value<MsgId>();
171 QModelIndex myIdx = model_->index(row(), 0);
172 MsgId myMsgId = model_->data(myIdx, MessageModel::MsgIdRole).value<MsgId>();
173 Message::Flags flags = (Message::Flags)model_->data(myIdx, MessageModel::FlagsRole).toInt();
174 // don't show the marker if we wrote that new line
175 if(!(flags & Message::Self)) {
176 BufferId bufferId = BufferId(chatScene()->idString().toInt());
177 MsgId lastSeenMsgId = Client::networkModel()->lastSeenMarkerMsgId(bufferId);
178 if(lastSeenMsgId < myMsgId && lastSeenMsgId >= prevMsgId) {
179 QtUiStyleSettings s("Colors");
180 QLinearGradient gradient(0, 0, 0, contentsItem().fontMetrics()->lineSpacing());
181 gradient.setColorAt(0, s.value("newMsgMarkerFG", QColor(Qt::red)).value<QColor>());
182 gradient.setColorAt(0.1, Qt::transparent);
183 painter->fillRect(boundingRect(), gradient);