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, QGraphicsItem *parent)
38 : QGraphicsItem(parent),
39 _row(row), // needs to be set before the items
40 _timestampItem(model, this),
41 _senderItem(model, this),
42 _contentsItem(model, this),
48 QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
49 setHighlighted(model->data(index, MessageModel::FlagsRole).toInt() & Message::Highlight);
52 QRectF ChatLine::boundingRect () const {
53 //return childrenBoundingRect();
54 return QRectF(0, 0, _width, _height);
57 ChatItem &ChatLine::item(ChatLineModel::ColumnType column) {
59 case ChatLineModel::TimestampColumn:
60 return _timestampItem;
61 case ChatLineModel::SenderColumn:
63 case ChatLineModel::ContentsColumn:
66 return *(ChatItem *)0; // provoke an error
70 qreal ChatLine::setGeometry(qreal width) {
72 prepareGeometryChange();
74 ColumnHandleItem *firstColumnHandle = chatScene()->firstColumnHandle();
75 ColumnHandleItem *secondColumnHandle = chatScene()->secondColumnHandle();
76 _height = _contentsItem.setGeometry(width - secondColumnHandle->sceneRight());
77 _timestampItem.setGeometry(firstColumnHandle->sceneLeft(), _height);
78 _senderItem.setGeometry(secondColumnHandle->sceneLeft() - firstColumnHandle->sceneRight(), _height);
80 _senderItem.setPos(firstColumnHandle->sceneRight(), 0);
81 _contentsItem.setPos(secondColumnHandle->sceneRight(), 0);
87 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
89 quint8 sel = (_selection & 0x80) | 0x40 | minColumn;
90 if(sel != _selection) {
92 for(int i = 0; i < minColumn; i++)
93 item((ChatLineModel::ColumnType)i).clearSelection();
94 for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
95 item((ChatLineModel::ColumnType)i).setFullSelection();
99 quint8 sel = _selection & 0x80;
100 if(sel != _selection) {
102 for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
103 item((ChatLineModel::ColumnType)i).clearSelection();
109 void ChatLine::setHighlighted(bool highlighted) {
110 if(highlighted) _selection |= 0x80;
111 else _selection &= 0x7f;
115 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
118 if(_selection & Highlighted) {
119 painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
121 if(_selection & Selected) {
122 qreal left = item((ChatLineModel::ColumnType)(_selection & 0x3f)).x();
123 QRectF selectRect(left, 0, width() - left, height());
124 painter->fillRect(selectRect, QApplication::palette().brush(QPalette::Highlight));
128 const QAbstractItemModel *model_ = model();
129 if(model_ && row() > 0) {
130 QModelIndex prevRowIdx = model_->index(row() - 1, 0);
131 MsgId msgId = model_->data(prevRowIdx, MessageModel::MsgIdRole).value<MsgId>();
132 Message::Flags flags = (Message::Flags)model_->data(model_->index(row(), 0), MessageModel::FlagsRole).toInt();
133 // don't show the marker if we wrote that new line
134 if(!(flags & Message::Self)) {
135 BufferId bufferId = model_->data(prevRowIdx, MessageModel::BufferIdRole).value<BufferId>();
136 if(msgId == Client::networkModel()->lastSeenMsgId(bufferId) && chatScene()->isSingleBufferScene()) {
137 QtUiStyleSettings s("Colors");
138 QLinearGradient gradient(0, 0, 0, height());
139 gradient.setColorAt(0, s.value("newMsgMarkerFG", QColor(Qt::red)).value<QColor>());
140 gradient.setColorAt(0.1, Qt::transparent);
141 painter->fillRect(boundingRect(), gradient);