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