Fix includes
[quassel.git] / src / qtui / chatline.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2012 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 <QGraphicsSceneMouseEvent>
23 #include <QString>
24 #include <QtGui>
25
26 #include "bufferinfo.h"
27 #include "buffersyncer.h"
28 #include "client.h"
29 #include "chatitem.h"
30 #include "chatline.h"
31 #include "chatview.h"
32 #include "columnhandleitem.h"
33 #include "messagemodel.h"
34 #include "networkmodel.h"
35 #include "qtui.h"
36 #include "qtuisettings.h"
37 #include "qtuistyle.h"
38
39 ChatLine::ChatLine(int row, QAbstractItemModel *model,
40                    const qreal &width,
41                    const qreal &timestampWidth, const qreal &senderWidth, const qreal &contentsWidth,
42                    const QPointF &senderPos, const QPointF &contentsPos,
43                    QGraphicsItem *parent)
44   : QGraphicsItem(parent),
45     _row(row), // needs to be set before the items
46     _model(model),
47     _contentsItem(contentsPos, contentsWidth, this),
48     _senderItem(QRectF(senderPos, QSizeF(senderWidth, _contentsItem.height())), this),
49     _timestampItem(QRectF(0, 0, timestampWidth, _contentsItem.height()), this),
50     _width(width),
51     _height(_contentsItem.height()),
52     _selection(0),
53     _mouseGrabberItem(0),
54     _hoverItem(0)
55 {
56   Q_ASSERT(model);
57   QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
58   setZValue(0);
59   setAcceptHoverEvents(true);
60   setHighlighted(index.data(MessageModel::FlagsRole).toInt() & Message::Highlight);
61 }
62
63 ChatLine::~ChatLine() {
64   if(chatView())
65     chatView()->setHasCache(this, false);
66 }
67
68 ChatItem *ChatLine::item(ChatLineModel::ColumnType column) {
69   switch(column) {
70     case ChatLineModel::TimestampColumn:
71       return &_timestampItem;
72     case ChatLineModel::SenderColumn:
73       return &_senderItem;
74     case ChatLineModel::ContentsColumn:
75       return &_contentsItem;
76   default:
77     return 0;
78   }
79 }
80
81 ChatItem *ChatLine::itemAt(const QPointF &pos) {
82   if(_contentsItem.boundingRect().contains(pos))
83     return &_contentsItem;
84   if(_senderItem.boundingRect().contains(pos))
85     return &_senderItem;
86   if(_timestampItem.boundingRect().contains(pos))
87     return &_timestampItem;
88   return 0;
89 }
90
91 void ChatLine::clearCache() {
92   _timestampItem.clearCache();
93   _senderItem.clearCache();
94   _contentsItem.clearCache();
95 }
96
97 void ChatLine::setMouseGrabberItem(ChatItem *item) {
98   _mouseGrabberItem = item;
99 }
100
101 bool ChatLine::sceneEvent(QEvent *event) {
102   if(event->type() == QEvent::GrabMouse) {
103     // get mouse cursor pos relative to us
104     ChatView *view = chatScene()->chatView();
105     QPointF linePos = mapFromScene(view->mapToScene(view->mapFromGlobal(QCursor::pos())));
106     setMouseGrabberItem(itemAt(linePos));
107   } else if(event->type() == QEvent::UngrabMouse) {
108     setMouseGrabberItem(0);
109   }
110   return QGraphicsItem::sceneEvent(event);
111 }
112
113 void ChatLine::setFirstColumn(const qreal &timestampWidth, const qreal &senderWidth, const QPointF &senderPos) {
114   _timestampItem.setGeometry(timestampWidth, _height);
115   _senderItem.setGeometry(senderWidth, _height);
116   _senderItem.setPos(senderPos);
117 }
118
119 void ChatLine::setSecondColumn(const qreal &senderWidth, const qreal &contentsWidth, const QPointF &contentsPos, qreal &linePos) {
120   // linepos is the *bottom* position for the line
121   qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
122   linePos -= height;
123   bool needGeometryChange = (height != _height);
124
125   _timestampItem.setHeight(height);
126   _senderItem.setGeometry(senderWidth, height);
127   _contentsItem.setPos(contentsPos);
128
129   if(needGeometryChange)
130     prepareGeometryChange();
131
132   _height = height;
133
134   setPos(0, linePos);
135 }
136
137 void ChatLine::setGeometryByWidth(const qreal &width, const qreal &contentsWidth, qreal &linePos) {
138   // linepos is the *bottom* position for the line
139   qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
140   linePos -= height;
141   bool needGeometryChange = (height != _height || width != _width);
142
143   if(height != _height) {
144     _timestampItem.setHeight(height);
145     _senderItem.setHeight(height);
146   }
147
148   if(needGeometryChange) {
149     prepareGeometryChange();
150     _height = height;
151     _width = width;
152   }
153
154   setPos(0, linePos); // set pos is _very_ cheap if nothing changes.
155 }
156
157 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
158   if(selected) {
159     quint8 sel = (_selection & Highlighted) | Selected | minColumn;
160     if(sel != _selection) {
161       _selection = sel;
162       for(int i = 0; i < minColumn; i++)
163         item((ChatLineModel::ColumnType)i)->clearSelection();
164       for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
165         item((ChatLineModel::ColumnType)i)->setFullSelection();
166       update();
167     }
168   } else {
169     quint8 sel = _selection & Highlighted;
170     if(sel != _selection) {
171       _selection = sel;
172       for(int i = 0; i <= ChatLineModel::ContentsColumn; i++)
173         item((ChatLineModel::ColumnType)i)->clearSelection();
174       update();
175     }
176   }
177 }
178
179 void ChatLine::setHighlighted(bool highlighted) {
180   if(highlighted) _selection |= Highlighted;
181   else _selection &= ~Highlighted;
182   update();
183 }
184
185 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
186   Q_UNUSED(option);
187   Q_UNUSED(widget);
188
189   const QAbstractItemModel *model_ = model();
190   QModelIndex myIdx = model_->index(row(), 0);
191   Message::Type type = (Message::Type)myIdx.data(MessageModel::TypeRole).toInt();
192   UiStyle::MessageLabel label = (UiStyle::MessageLabel)myIdx.data(ChatLineModel::MsgLabelRole).toInt();
193
194   QTextCharFormat msgFmt = QtUi::style()->format(UiStyle::formatType(type), label);
195   if(msgFmt.hasProperty(QTextFormat::BackgroundBrush)) {
196     painter->fillRect(boundingRect(), msgFmt.background());
197   }
198
199   if(_selection & Selected) {
200     QTextCharFormat selFmt = QtUi::style()->format(UiStyle::formatType(type), label | UiStyle::Selected);
201     if(selFmt.hasProperty(QTextFormat::BackgroundBrush)) {
202       qreal left = item((ChatLineModel::ColumnType)(_selection & ItemMask))->pos().x();
203       QRectF selectRect(left, 0, width() - left, height());
204       painter->fillRect(selectRect, selFmt.background());
205     }
206   }
207
208   // draw chatitems
209   // the items draw themselves at the correct position
210   timestampItem()->paint(painter, option, widget);
211   senderItem()->paint(painter, option, widget);
212   contentsItem()->paint(painter, option, widget);
213 }
214
215 // We need to dispatch all mouse-related events to the appropriate (mouse grabbing) ChatItem
216
217 ChatItem *ChatLine::mouseEventTargetItem(const QPointF &pos) {
218   if(mouseGrabberItem())
219     return mouseGrabberItem();
220   return itemAt(pos);
221 }
222
223 void ChatLine::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
224   ChatItem *item = mouseEventTargetItem(event->pos());
225   if(item)
226     item->mouseMoveEvent(event);
227 }
228
229 void ChatLine::mousePressEvent(QGraphicsSceneMouseEvent *event) {
230   ChatItem *item = mouseEventTargetItem(event->pos());
231   if(item)
232     item->mousePressEvent(event);
233 }
234
235 void ChatLine::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
236   ChatItem *item = mouseEventTargetItem(event->pos());
237   if(item)
238     item->mouseReleaseEvent(event);
239 }
240
241 void ChatLine::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
242   ChatItem *item = mouseEventTargetItem(event->pos());
243   if(item && !_hoverItem) {
244     _hoverItem = item;
245     item->hoverEnterEvent(event);
246   }
247 }
248
249 void ChatLine::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
250   if(_hoverItem) {
251     _hoverItem->hoverLeaveEvent(event);
252     _hoverItem = 0;
253   }
254 }
255
256 void ChatLine::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
257   ChatItem *item = mouseEventTargetItem(event->pos());
258   if(item)
259     item->hoverMoveEvent(event);
260 }