Another Speed boost for the new ChatView.
[quassel.git] / src / qtui / chatline.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QDateTime>
22 #include <QString>
23 #include <QtGui>
24
25 #include "bufferinfo.h"
26 #include "buffersyncer.h"
27 #include "client.h"
28 #include "chatitem.h"
29 #include "chatline.h"
30 #include "columnhandleitem.h"
31 #include "messagemodel.h"
32 #include "networkmodel.h"
33 #include "qtui.h"
34 #include "qtuisettings.h"
35 #include "qtuistyle.h"
36
37 // ChatLine::ChatLine(int row, QAbstractItemModel *model, QGraphicsItem *parent)
38 //   : QGraphicsItem(parent),
39 //     _row(row), // needs to be set before the items
40 //     _model(model),
41 //     _contentsItem(this),
42 //     _senderItem(this),
43 //     _timestampItem(this),
44 //     _width(0),
45 //     _height(0),
46 //     _selection(0)
47 // {
48 //   Q_ASSERT(model);
49 //   QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
50 //   setHighlighted(model->data(index, MessageModel::FlagsRole).toInt() & Message::Highlight);
51 // }
52
53 ChatLine::ChatLine(int row, QAbstractItemModel *model,
54                    const qreal &width,
55                    const qreal &timestampWidth, 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
60     _model(model),
61     _contentsItem(contentsWidth, contentsPos, this),
62     _senderItem(senderWidth, _contentsItem.height(), senderPos, this),
63     _timestampItem(timestampWidth, _contentsItem.height(), this),
64     _width(width),
65     _height(_contentsItem.height()),
66     _selection(0)
67 {
68   Q_ASSERT(model);
69   QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
70   setHighlighted(model->data(index, MessageModel::FlagsRole).toInt() & Message::Highlight);
71 }
72
73 QRectF ChatLine::boundingRect () const {
74   //return childrenBoundingRect();
75   return QRectF(0, 0, _width, _height);
76 }
77
78 ChatItem &ChatLine::item(ChatLineModel::ColumnType column) {
79   switch(column) {
80     case ChatLineModel::TimestampColumn:
81       return _timestampItem;
82     case ChatLineModel::SenderColumn:
83       return _senderItem;
84     case ChatLineModel::ContentsColumn:
85       return _contentsItem;
86   default:
87     return *(ChatItem *)0; // provoke an error
88   }
89 }
90
91 // WARNING: setColumns should not be used without either:
92 //  a) calling prepareGeometryChange() immediately before setColumns()
93 //  b) calling Chatline::setPos() immediately afterwards
94 //
95 // NOTE: senderPos and contentsPos are in ChatLines coordinate system!
96 qreal ChatLine::setColumns(const qreal &timestampWidth, 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);
101
102   _senderItem.setPos(senderPos);
103   _contentsItem.setPos(contentsPos);
104
105   _contentsItem.clearLayout();
106   _senderItem.clearLayout();
107   _timestampItem.clearLayout();
108
109   return _height;
110 }
111
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) {
116   _width = width;
117   _height = _contentsItem.setGeometryByWidth(contentsWidth);
118   _timestampItem.setHeight(_height);
119   _senderItem.setHeight(_height);
120   _contentsItem.clearLayout();
121   return _height;
122 }
123
124 qreal ChatLine::setGeometryByWidth(qreal width) {
125   if(width != _width)
126     prepareGeometryChange();
127
128   ColumnHandleItem *firstColumnHandle = chatScene()->firstColumnHandle();
129   ColumnHandleItem *secondColumnHandle = chatScene()->secondColumnHandle();
130
131   _height = _contentsItem.setGeometryByWidth(width - secondColumnHandle->sceneRight());
132   _timestampItem.setGeometry(firstColumnHandle->sceneLeft(), _height);
133   _senderItem.setGeometry(secondColumnHandle->sceneLeft() - firstColumnHandle->sceneRight(), _height);
134
135   _senderItem.setPos(firstColumnHandle->sceneRight(), 0);
136   _contentsItem.setPos(secondColumnHandle->sceneRight(), 0);
137
138   _width = width;
139   return _height;
140 }
141
142 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
143   if(selected) {
144     quint8 sel = (_selection & 0x80) | 0x40 | minColumn;
145     if(sel != _selection) {
146       _selection = sel;
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();
151       update();
152     }
153   } else {
154     quint8 sel = _selection & 0x80;
155     if(sel != _selection) {
156       _selection = sel;
157       for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
158         item((ChatLineModel::ColumnType)i).clearSelection();
159       update();
160     }
161   }
162 }
163
164 void ChatLine::setHighlighted(bool highlighted) {
165   if(highlighted) _selection |= 0x80;
166   else _selection &= 0x7f;
167   update();
168 }
169
170 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
171   Q_UNUSED(option);
172   Q_UNUSED(widget);
173   if(_selection & Highlighted) {
174     painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
175   }
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));
180   }
181
182   // new line marker
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);
197       }
198     }
199   }
200 }