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