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