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);
54 setHighlighted(model->data(index, MessageModel::FlagsRole).toInt() & Message::Highlight);
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 // WARNING: setColumns should not be used without either:
71 // a) calling prepareGeometryChange() immediately before setColumns()
72 // b) calling Chatline::setPos() immediately afterwards
74 // NOTE: senderPos and contentsPos are in ChatLines coordinate system!
75 qreal ChatLine::setColumns(const qreal ×tampWidth, const qreal &senderWidth, const qreal &contentsWidth,
76 const QPointF &senderPos, const QPointF &contentsPos) {
77 _height = _contentsItem.setGeometryByWidth(contentsWidth);
78 _senderItem.setGeometry(senderWidth, _height);
79 _timestampItem.setGeometry(timestampWidth, _height);
81 _senderItem.setPos(senderPos);
82 _contentsItem.setPos(contentsPos);
84 _contentsItem.clearLayout();
85 _senderItem.clearLayout();
86 _timestampItem.clearLayout();
91 // WARNING: setGeometryByWidth should not be used without either:
92 // a) calling prepareGeometryChange() immediately before setColumns()
93 // b) calling Chatline::setPos() immediately afterwards
94 qreal ChatLine::setGeometryByWidth(const qreal &width, const qreal &contentsWidth) {
96 _height = _contentsItem.setGeometryByWidth(contentsWidth);
97 _timestampItem.setHeight(_height);
98 _senderItem.setHeight(_height);
99 _contentsItem.clearLayout();
103 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
105 quint8 sel = (_selection & Highlighted) | Selected | minColumn;
106 if(sel != _selection) {
108 for(int i = 0; i < minColumn; i++)
109 item((ChatLineModel::ColumnType)i).clearSelection();
110 for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
111 item((ChatLineModel::ColumnType)i).setFullSelection();
115 quint8 sel = _selection & Highlighted;
116 if(sel != _selection) {
118 for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
119 item((ChatLineModel::ColumnType)i).clearSelection();
125 void ChatLine::setHighlighted(bool highlighted) {
126 if(highlighted) _selection |= Highlighted;
127 else _selection &= ~Highlighted;
131 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
134 if(_selection & Highlighted) {
135 painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
137 if(_selection & Selected) {
138 qreal left = item((ChatLineModel::ColumnType)(_selection & ItemMask)).x();
139 QRectF selectRect(left, 0, width() - left, height());
140 painter->fillRect(selectRect, QApplication::palette().brush(QPalette::Highlight));
144 const QAbstractItemModel *model_ = model();
145 if(model_ && row() > 0) {
146 QModelIndex prevRowIdx = model_->index(row() - 1, 0);
147 MsgId msgId = model_->data(prevRowIdx, MessageModel::MsgIdRole).value<MsgId>();
148 Message::Flags flags = (Message::Flags)model_->data(model_->index(row(), 0), MessageModel::FlagsRole).toInt();
149 // don't show the marker if we wrote that new line
150 if(!(flags & Message::Self)) {
151 BufferId bufferId = model_->data(prevRowIdx, MessageModel::BufferIdRole).value<BufferId>();
152 if(msgId == Client::networkModel()->lastSeenMsgId(bufferId) && chatScene()->isSingleBufferScene()) {
153 QtUiStyleSettings s("Colors");
154 QLinearGradient gradient(0, 0, 0, height());
155 gradient.setColorAt(0, s.value("newMsgMarkerFG", QColor(Qt::red)).value<QColor>());
156 gradient.setColorAt(0.1, Qt::transparent);
157 painter->fillRect(boundingRect(), gradient);