af77b12ca5a29ddf51983b534daefb0dd9efe764
[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 "messagemodel.h"
31 #include "networkmodel.h"
32 #include "qtui.h"
33
34 ChatLine::ChatLine(int row, QAbstractItemModel *model, QGraphicsItem *parent)
35   : QGraphicsItem(parent),
36     _row(row), // needs to be set before the items
37     _timestampItem(ChatLineModel::TimestampColumn, model, this),
38     _senderItem(ChatLineModel::SenderColumn, model, this),
39     _contentsItem(ChatLineModel::ContentsColumn, model, this),
40     _width(0),
41     _height(0),
42     _selection(0)
43 {
44   Q_ASSERT(model);
45   QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
46   setHighlighted(model->data(index, MessageModel::FlagsRole).toInt() & Message::Highlight);
47 }
48
49 QRectF ChatLine::boundingRect () const {
50   //return childrenBoundingRect();
51   return QRectF(0, 0, _width, _height);
52 }
53
54 ChatItem &ChatLine::item(ChatLineModel::ColumnType column) {
55   switch(column) {
56     case ChatLineModel::TimestampColumn:
57       return _timestampItem;
58     case ChatLineModel::SenderColumn:
59       return _senderItem;
60     case ChatLineModel::ContentsColumn:
61       return _contentsItem;
62   default:
63     return *(ChatItem *)0; // provoke an error
64   }
65 }
66
67 qreal ChatLine::setGeometry(qreal width, qreal firstHandlePos, qreal secondHandlePos) {
68   if(width != _width)
69     prepareGeometryChange();
70   qreal firstsep = QtUi::style()->firstColumnSeparator()/2;
71   qreal secondsep = QtUi::style()->secondColumnSeparator()/2;
72
73   _timestampItem.setWidth(firstHandlePos - firstsep);
74   _senderItem.setWidth(secondHandlePos - firstHandlePos - (firstsep+secondsep));
75   _height = _contentsItem.setWidth(width - secondHandlePos - secondsep);
76
77   _senderItem.setPos(firstHandlePos + firstsep, 0);
78   _contentsItem.setPos(secondHandlePos + secondsep, 0);
79
80   _width = width;
81   return _height;
82 }
83
84 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
85   if(selected) {
86     quint8 sel = (_selection & 0x80) | 0x40 | minColumn;
87     if(sel != _selection) {
88       _selection = sel;
89       for(int i = 0; i < minColumn; i++)
90         item((ChatLineModel::ColumnType)i).clearSelection();
91       for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
92         item((ChatLineModel::ColumnType)i).setFullSelection();
93       update();
94     }
95   } else {
96     quint8 sel = _selection & 0x80;
97     if(sel != _selection) {
98       _selection = sel;
99       for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
100         item((ChatLineModel::ColumnType)i).clearSelection();
101       update();
102     }
103   }
104 }
105
106 void ChatLine::setHighlighted(bool highlighted) {
107   if(highlighted) _selection |= 0x80;
108   else _selection &= 0x7f;
109   update();
110 }
111
112 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
113   if(_selection & Highlighted) {
114     painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
115   }
116   if(_selection & Selected) {
117     qreal left = item((ChatLineModel::ColumnType)(_selection & 0x3f)).x();
118     QRectF selectRect(left, 0, width() - left, height());
119     painter->fillRect(selectRect, QApplication::palette().brush(QPalette::Highlight));
120   }
121
122   const QAbstractItemModel *model_ = model();
123   if(model_ && row() > 0) {
124     MsgId msgId = model_->data(model_->index(row() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
125     BufferId bufferId = model_->data(model_->index(row() - 1, 0), MessageModel::BufferIdRole).value<BufferId>();
126     if(msgId == Client::networkModel()->lastSeenMsgId(bufferId) && chatScene()->isSingleBufferScene()) {
127       QLinearGradient gradient(0, 0, 0, height());
128       gradient.setColorAt(0, Qt::transparent);
129       gradient.setColorAt(1, Qt::red);
130       painter->fillRect(boundingRect(), gradient);
131     }
132   }
133
134
135 }