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 // WARNING: setColumns should not be used without either:
72 // a) calling prepareGeometryChange() immediately before setColumns()
73 // b) calling Chatline::setPos() immediately afterwards
75 // NOTE: senderPos and contentsPos are in ChatLines coordinate system!
76 qreal ChatLine::setColumns(const qreal ×tampWidth, const qreal &senderWidth, const qreal &contentsWidth,
77 const QPointF &senderPos, const QPointF &contentsPos) {
78 prepareGeometryChange();
79 qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
80 _senderItem.setGeometry(senderWidth, height);
81 _timestampItem.setGeometry(timestampWidth, height);
83 _senderItem.setPos(senderPos);
84 _contentsItem.setPos(contentsPos);
86 _contentsItem.clearLayout();
87 _senderItem.clearLayout();
88 _timestampItem.clearLayout();
96 // WARNING: setGeometryByWidth should not be used without either:
97 // a) calling prepareGeometryChange() immediately before setColumns()
98 // b) calling Chatline::setPos() immediately afterwards
99 qreal ChatLine::setGeometryByWidth(const qreal &width, const qreal &contentsWidth) {
100 prepareGeometryChange();
101 qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
102 _timestampItem.setHeight(height);
103 _senderItem.setHeight(height);
104 _contentsItem.clearLayout();
110 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
112 quint8 sel = (_selection & Highlighted) | Selected | minColumn;
113 if(sel != _selection) {
115 for(int i = 0; i < minColumn; i++)
116 item((ChatLineModel::ColumnType)i).clearSelection();
117 for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
118 item((ChatLineModel::ColumnType)i).setFullSelection();
122 quint8 sel = _selection & Highlighted;
123 if(sel != _selection) {
125 for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
126 item((ChatLineModel::ColumnType)i).clearSelection();
132 void ChatLine::setHighlighted(bool highlighted) {
133 if(highlighted) _selection |= Highlighted;
134 else _selection &= ~Highlighted;
138 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
141 if(_selection & Highlighted) {
142 painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
144 if(_selection & Selected) {
145 qreal left = item((ChatLineModel::ColumnType)(_selection & ItemMask)).x();
146 QRectF selectRect(left, 0, width() - left, height());
147 painter->fillRect(selectRect, QApplication::palette().brush(QPalette::Highlight));
151 const QAbstractItemModel *model_ = model();
152 if(model_ && row() > 0) {
153 QModelIndex prevRowIdx = model_->index(row() - 1, 0);
154 MsgId msgId = model_->data(prevRowIdx, MessageModel::MsgIdRole).value<MsgId>();
155 Message::Flags flags = (Message::Flags)model_->data(model_->index(row(), 0), MessageModel::FlagsRole).toInt();
156 // don't show the marker if we wrote that new line
157 if(!(flags & Message::Self)) {
158 BufferId bufferId = model_->data(prevRowIdx, MessageModel::BufferIdRole).value<BufferId>();
159 if(msgId == Client::networkModel()->lastSeenMsgId(bufferId) && chatScene()->isSingleBufferScene()) {
160 QtUiStyleSettings s("Colors");
161 QLinearGradient gradient(0, 0, 0, contentsItem().fontMetrics()->lineSpacing());
162 gradient.setColorAt(0, s.value("newMsgMarkerFG", QColor(Qt::red)).value<QColor>());
163 gradient.setColorAt(0.1, Qt::transparent);
164 painter->fillRect(boundingRect(), gradient);