1 /***************************************************************************
2 * Copyright (C) 2005-2014 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
22 #include <QGraphicsSceneMouseEvent>
26 #include "bufferinfo.h"
27 #include "buffersyncer.h"
32 #include "columnhandleitem.h"
33 #include "messagemodel.h"
34 #include "networkmodel.h"
36 #include "qtuisettings.h"
37 #include "qtuistyle.h"
39 ChatLine::ChatLine(int row, QAbstractItemModel *model,
41 const qreal ×tampWidth, 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
47 _contentsItem(contentsPos, contentsWidth, this),
48 _senderItem(QRectF(senderPos, QSizeF(senderWidth, _contentsItem.height())), this),
49 _timestampItem(QRectF(0, 0, timestampWidth, _contentsItem.height()), this),
51 _height(_contentsItem.height()),
57 QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
59 setAcceptHoverEvents(true);
60 setHighlighted(index.data(MessageModel::FlagsRole).toInt() & Message::Highlight);
67 chatView()->setHasCache(this, false);
71 ChatItem *ChatLine::item(ChatLineModel::ColumnType column)
74 case ChatLineModel::TimestampColumn:
75 return &_timestampItem;
76 case ChatLineModel::SenderColumn:
78 case ChatLineModel::ContentsColumn:
79 return &_contentsItem;
86 ChatItem *ChatLine::itemAt(const QPointF &pos)
88 if (_contentsItem.boundingRect().contains(pos))
89 return &_contentsItem;
90 if (_senderItem.boundingRect().contains(pos))
92 if (_timestampItem.boundingRect().contains(pos))
93 return &_timestampItem;
98 void ChatLine::clearCache()
100 _timestampItem.clearCache();
101 _senderItem.clearCache();
102 _contentsItem.clearCache();
106 void ChatLine::setMouseGrabberItem(ChatItem *item)
108 _mouseGrabberItem = item;
112 bool ChatLine::sceneEvent(QEvent *event)
114 if (event->type() == QEvent::GrabMouse) {
115 // get mouse cursor pos relative to us
116 ChatView *view = chatScene()->chatView();
117 QPointF linePos = mapFromScene(view->mapToScene(view->mapFromGlobal(QCursor::pos())));
118 setMouseGrabberItem(itemAt(linePos));
120 else if (event->type() == QEvent::UngrabMouse) {
121 setMouseGrabberItem(0);
123 return QGraphicsItem::sceneEvent(event);
127 void ChatLine::setFirstColumn(const qreal ×tampWidth, const qreal &senderWidth, const QPointF &senderPos)
129 _timestampItem.setGeometry(timestampWidth, _height);
130 _senderItem.setGeometry(senderWidth, _height);
131 _senderItem.setPos(senderPos);
135 void ChatLine::setSecondColumn(const qreal &senderWidth, const qreal &contentsWidth, const QPointF &contentsPos, qreal &linePos)
137 // linepos is the *bottom* position for the line
138 qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
140 bool needGeometryChange = (height != _height);
142 _timestampItem.setHeight(height);
143 _senderItem.setGeometry(senderWidth, height);
144 _contentsItem.setPos(contentsPos);
146 if (needGeometryChange)
147 prepareGeometryChange();
155 void ChatLine::setGeometryByWidth(const qreal &width, const qreal &contentsWidth, qreal &linePos)
157 // linepos is the *bottom* position for the line
158 qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
160 bool needGeometryChange = (height != _height || width != _width);
162 if (height != _height) {
163 _timestampItem.setHeight(height);
164 _senderItem.setHeight(height);
167 if (needGeometryChange) {
168 prepareGeometryChange();
173 setPos(0, linePos); // set pos is _very_ cheap if nothing changes.
177 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn)
180 quint8 sel = (_selection & Highlighted) | Selected | minColumn;
181 if (sel != _selection) {
183 for (int i = 0; i < minColumn; i++)
184 item((ChatLineModel::ColumnType)i)->clearSelection();
185 for (int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
186 item((ChatLineModel::ColumnType)i)->setFullSelection();
191 quint8 sel = _selection & Highlighted;
192 if (sel != _selection) {
194 for (int i = 0; i <= ChatLineModel::ContentsColumn; i++)
195 item((ChatLineModel::ColumnType)i)->clearSelection();
202 void ChatLine::setHighlighted(bool highlighted)
204 if (highlighted) _selection |= Highlighted;
205 else _selection &= ~Highlighted;
210 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
215 const QAbstractItemModel *model_ = model();
216 QModelIndex myIdx = model_->index(row(), 0);
217 Message::Type type = (Message::Type)myIdx.data(MessageModel::TypeRole).toInt();
218 UiStyle::MessageLabel label = (UiStyle::MessageLabel)myIdx.data(ChatLineModel::MsgLabelRole).toInt();
220 QTextCharFormat msgFmt = QtUi::style()->format(UiStyle::formatType(type), label);
221 if (msgFmt.hasProperty(QTextFormat::BackgroundBrush)) {
222 painter->fillRect(boundingRect(), msgFmt.background());
225 if (_selection & Selected) {
226 QTextCharFormat selFmt = QtUi::style()->format(UiStyle::formatType(type), label | UiStyle::Selected);
227 if (selFmt.hasProperty(QTextFormat::BackgroundBrush)) {
228 qreal left = item((ChatLineModel::ColumnType)(_selection & ItemMask))->pos().x();
229 QRectF selectRect(left, 0, width() - left, height());
230 painter->fillRect(selectRect, selFmt.background());
235 // the items draw themselves at the correct position
236 timestampItem()->paint(painter, option, widget);
237 senderItem()->paint(painter, option, widget);
238 contentsItem()->paint(painter, option, widget);
242 // We need to dispatch all mouse-related events to the appropriate (mouse grabbing) ChatItem
244 ChatItem *ChatLine::mouseEventTargetItem(const QPointF &pos)
246 if (mouseGrabberItem())
247 return mouseGrabberItem();
252 void ChatLine::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
254 ChatItem *item = mouseEventTargetItem(event->pos());
256 item->mouseMoveEvent(event);
260 void ChatLine::mousePressEvent(QGraphicsSceneMouseEvent *event)
262 ChatItem *item = mouseEventTargetItem(event->pos());
264 item->mousePressEvent(event);
268 void ChatLine::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
270 ChatItem *item = mouseEventTargetItem(event->pos());
272 item->mouseReleaseEvent(event);
276 void ChatLine::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
278 ChatItem *item = mouseEventTargetItem(event->pos());
279 if (item && !_hoverItem) {
281 item->hoverEnterEvent(event);
286 void ChatLine::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
289 _hoverItem->hoverLeaveEvent(event);
295 void ChatLine::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
297 ChatItem *item = mouseEventTargetItem(event->pos());
299 item->hoverMoveEvent(event);