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
41 // _contentsItem(this),
43 // _timestampItem(this),
49 // QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
50 // setHighlighted(model->data(index, MessageModel::FlagsRole).toInt() & Message::Highlight);
53 ChatLine::ChatLine(int row, QAbstractItemModel *model,
55 const qreal ×tampWidth, const qreal &senderWidth, const qreal &contentsWidth,
56 const QPointF &senderPos, const QPointF &contentsPos,
57 QGraphicsItem *parent)
58 : QGraphicsItem(parent),
59 _row(row), // needs to be set before the items
61 _contentsItem(contentsWidth, contentsPos, this),
62 _senderItem(senderWidth, _contentsItem.height(), senderPos, this),
63 _timestampItem(timestampWidth, _contentsItem.height(), this),
65 _height(_contentsItem.height()),
69 QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
70 setHighlighted(model->data(index, MessageModel::FlagsRole).toInt() & Message::Highlight);
73 QRectF ChatLine::boundingRect () const {
74 //return childrenBoundingRect();
75 return QRectF(0, 0, _width, _height);
78 ChatItem &ChatLine::item(ChatLineModel::ColumnType column) {
80 case ChatLineModel::TimestampColumn:
81 return _timestampItem;
82 case ChatLineModel::SenderColumn:
84 case ChatLineModel::ContentsColumn:
87 return *(ChatItem *)0; // provoke an error
91 // WARNING: setColumns should not be used without either:
92 // a) calling prepareGeometryChange() immediately before setColumns()
93 // b) calling Chatline::setPos() immediately afterwards
95 // NOTE: senderPos and contentsPos are in ChatLines coordinate system!
96 qreal ChatLine::setColumns(const qreal ×tampWidth, const qreal &senderWidth, const qreal &contentsWidth,
97 const QPointF &senderPos, const QPointF &contentsPos) {
98 _height = _contentsItem.setGeometryByWidth(contentsWidth);
99 _senderItem.setGeometry(senderWidth, _height);
100 _timestampItem.setGeometry(timestampWidth, _height);
102 _senderItem.setPos(senderPos);
103 _contentsItem.setPos(contentsPos);
105 _contentsItem.clearLayout();
106 _senderItem.clearLayout();
107 _timestampItem.clearLayout();
112 // WARNING: setGeometryByWidth should not be used without either:
113 // a) calling prepareGeometryChange() immediately before setColumns()
114 // b) calling Chatline::setPos() immediately afterwards
115 qreal ChatLine::setGeometryByWidth(const qreal &width, const qreal &contentsWidth) {
117 _height = _contentsItem.setGeometryByWidth(contentsWidth);
118 _timestampItem.setHeight(_height);
119 _senderItem.setHeight(_height);
120 _contentsItem.clearLayout();
124 qreal ChatLine::setGeometryByWidth(qreal width) {
126 prepareGeometryChange();
128 ColumnHandleItem *firstColumnHandle = chatScene()->firstColumnHandle();
129 ColumnHandleItem *secondColumnHandle = chatScene()->secondColumnHandle();
131 _height = _contentsItem.setGeometryByWidth(width - secondColumnHandle->sceneRight());
132 _timestampItem.setGeometry(firstColumnHandle->sceneLeft(), _height);
133 _senderItem.setGeometry(secondColumnHandle->sceneLeft() - firstColumnHandle->sceneRight(), _height);
135 _senderItem.setPos(firstColumnHandle->sceneRight(), 0);
136 _contentsItem.setPos(secondColumnHandle->sceneRight(), 0);
142 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
144 quint8 sel = (_selection & 0x80) | 0x40 | minColumn;
145 if(sel != _selection) {
147 for(int i = 0; i < minColumn; i++)
148 item((ChatLineModel::ColumnType)i).clearSelection();
149 for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
150 item((ChatLineModel::ColumnType)i).setFullSelection();
154 quint8 sel = _selection & 0x80;
155 if(sel != _selection) {
157 for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
158 item((ChatLineModel::ColumnType)i).clearSelection();
164 void ChatLine::setHighlighted(bool highlighted) {
165 if(highlighted) _selection |= 0x80;
166 else _selection &= 0x7f;
170 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
173 if(_selection & Highlighted) {
174 painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
176 if(_selection & Selected) {
177 qreal left = item((ChatLineModel::ColumnType)(_selection & 0x3f)).x();
178 QRectF selectRect(left, 0, width() - left, height());
179 painter->fillRect(selectRect, QApplication::palette().brush(QPalette::Highlight));
183 const QAbstractItemModel *model_ = model();
184 if(model_ && row() > 0) {
185 QModelIndex prevRowIdx = model_->index(row() - 1, 0);
186 MsgId msgId = model_->data(prevRowIdx, MessageModel::MsgIdRole).value<MsgId>();
187 Message::Flags flags = (Message::Flags)model_->data(model_->index(row(), 0), MessageModel::FlagsRole).toInt();
188 // don't show the marker if we wrote that new line
189 if(!(flags & Message::Self)) {
190 BufferId bufferId = model_->data(prevRowIdx, MessageModel::BufferIdRole).value<BufferId>();
191 if(msgId == Client::networkModel()->lastSeenMsgId(bufferId) && chatScene()->isSingleBufferScene()) {
192 QtUiStyleSettings s("Colors");
193 QLinearGradient gradient(0, 0, 0, height());
194 gradient.setColorAt(0, s.value("newMsgMarkerFG", QColor(Qt::red)).value<QColor>());
195 gradient.setColorAt(0.1, Qt::transparent);
196 painter->fillRect(boundingRect(), gradient);