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