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