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 ***************************************************************************/
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);
79 _timestampItem.clearLayout();
80 _senderItem.clearLayout();
83 // NOTE: contentsPos is in ChatLines coordinate system!
84 void ChatLine::setSecondColumn(const qreal &senderWidth, const qreal &contentsWidth,
85 const QPointF &contentsPos, qreal &linePos) {
86 // contentsItem doesn't need a geom change as it's Pos is changed (ensured by void ChatScene::firstHandlePositionChanged(qreal xpos))
87 qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
89 bool needGeometryChange = linePos == pos().y();
91 if(needGeometryChange) {
92 _timestampItem.prepareGeometryChange();
93 _senderItem.prepareGeometryChange();
95 _timestampItem.setHeight(height);
96 _senderItem.setGeometry(senderWidth, height);
98 _contentsItem.setPos(contentsPos);
100 _timestampItem.clearLayout();
101 _senderItem.clearLayout();
103 if(needGeometryChange)
104 prepareGeometryChange();
111 void ChatLine::setGeometryByWidth(const qreal &width, const qreal &contentsWidth, qreal &linePos) {
112 qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
114 bool needGeometryChange = linePos == pos().y();
116 if(needGeometryChange) {
117 _timestampItem.prepareGeometryChange();
118 _senderItem.prepareGeometryChange();
120 _timestampItem.setHeight(height);
121 _senderItem.setHeight(height);
122 _contentsItem.clearLayout();
124 if(needGeometryChange)
125 prepareGeometryChange();
130 setPos(0, linePos); // set pos is _very_ cheap if nothing changes.
133 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
135 quint8 sel = (_selection & Highlighted) | Selected | minColumn;
136 if(sel != _selection) {
138 for(int i = 0; i < minColumn; i++)
139 item((ChatLineModel::ColumnType)i).clearSelection();
140 for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
141 item((ChatLineModel::ColumnType)i).setFullSelection();
145 quint8 sel = _selection & Highlighted;
146 if(sel != _selection) {
148 for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
149 item((ChatLineModel::ColumnType)i).clearSelection();
155 void ChatLine::setHighlighted(bool highlighted) {
156 if(highlighted) _selection |= Highlighted;
157 else _selection &= ~Highlighted;
161 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
164 if(_selection & Highlighted) {
165 painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
167 if(_selection & Selected) {
168 qreal left = item((ChatLineModel::ColumnType)(_selection & ItemMask)).x();
169 QRectF selectRect(left, 0, width() - left, height());
170 painter->fillRect(selectRect, QApplication::palette().brush(QPalette::Highlight));
174 const QAbstractItemModel *model_ = model();
175 if(model_ && row() > 0 && chatScene()->isSingleBufferScene()) {
176 QModelIndex prevRowIdx = model_->index(row() - 1, 0);
177 MsgId prevMsgId = model_->data(prevRowIdx, MessageModel::MsgIdRole).value<MsgId>();
178 QModelIndex myIdx = model_->index(row(), 0);
179 MsgId myMsgId = model_->data(myIdx, MessageModel::MsgIdRole).value<MsgId>();
180 Message::Flags flags = (Message::Flags)model_->data(myIdx, MessageModel::FlagsRole).toInt();
181 // don't show the marker if we wrote that new line
182 if(!(flags & Message::Self)) {
183 BufferId bufferId = BufferId(chatScene()->idString().toInt());
184 MsgId lastSeenMsgId = Client::networkModel()->lastSeenMsgId(bufferId);
185 if(lastSeenMsgId < myMsgId && lastSeenMsgId >= prevMsgId) {
186 QtUiStyleSettings s("Colors");
187 QLinearGradient gradient(0, 0, 0, contentsItem().fontMetrics()->lineSpacing());
188 gradient.setColorAt(0, s.value("newMsgMarkerFG", QColor(Qt::red)).value<QColor>());
189 gradient.setColorAt(0.1, Qt::transparent);
190 painter->fillRect(boundingRect(), gradient);