1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 ***************************************************************************/
24 #include <QGraphicsSceneMouseEvent>
28 #include "bufferinfo.h"
29 #include "buffersyncer.h"
33 #include "columnhandleitem.h"
34 #include "messagemodel.h"
35 #include "networkmodel.h"
37 #include "qtuisettings.h"
38 #include "qtuistyle.h"
40 ChatLine::ChatLine(int row,
41 QAbstractItemModel* model,
43 const qreal& timestampWidth,
44 const qreal& senderWidth,
45 const qreal& contentsWidth,
46 const QPointF& senderPos,
47 const QPointF& contentsPos,
48 QGraphicsItem* parent)
49 : QGraphicsItem(parent)
51 , // needs to be set before the items
53 , _contentsItem(contentsPos, contentsWidth, this)
54 , _senderItem(QRectF(senderPos, QSizeF(senderWidth, _contentsItem.height())), this)
55 , _timestampItem(QRectF(0, 0, timestampWidth, _contentsItem.height()), this)
57 , _height(_contentsItem.height())
59 , _mouseGrabberItem(nullptr)
63 QModelIndex index = model->index(row, ChatLineModel::ContentsColumn);
65 setAcceptHoverEvents(true);
66 setHighlighted(index.data(MessageModel::FlagsRole).toInt() & Message::Highlight);
72 chatView()->setHasCache(this, false);
75 ChatItem* ChatLine::item(ChatLineModel::ColumnType column)
78 case ChatLineModel::TimestampColumn:
79 return &_timestampItem;
80 case ChatLineModel::SenderColumn:
82 case ChatLineModel::ContentsColumn:
83 return &_contentsItem;
89 ChatItem* ChatLine::itemAt(const QPointF& pos)
91 if (_contentsItem.boundingRect().contains(pos))
92 return &_contentsItem;
93 if (_senderItem.boundingRect().contains(pos))
95 if (_timestampItem.boundingRect().contains(pos))
96 return &_timestampItem;
100 void ChatLine::clearCache()
102 _timestampItem.clearCache();
103 _senderItem.clearCache();
104 _contentsItem.clearCache();
107 void ChatLine::setMouseGrabberItem(ChatItem* item)
109 _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(nullptr);
123 return QGraphicsItem::sceneEvent(event);
126 void ChatLine::setFirstColumn(const qreal& timestampWidth, const qreal& senderWidth, const QPointF& senderPos)
128 _timestampItem.setGeometry(timestampWidth, _height);
129 _senderItem.setGeometry(senderWidth, _height);
130 _senderItem.setPos(senderPos);
133 void ChatLine::setSecondColumn(const qreal& senderWidth, const qreal& contentsWidth, const QPointF& contentsPos, qreal& linePos)
135 // linepos is the *bottom* position for the line
136 qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
138 bool needGeometryChange = (height != _height);
140 _timestampItem.setHeight(height);
141 _senderItem.setGeometry(senderWidth, height);
142 _contentsItem.setPos(contentsPos);
144 if (needGeometryChange)
145 prepareGeometryChange();
152 void ChatLine::setGeometryByWidth(const qreal& width, const qreal& contentsWidth, qreal& linePos)
154 // linepos is the *bottom* position for the line
155 qreal height = _contentsItem.setGeometryByWidth(contentsWidth);
157 bool needGeometryChange = (height != _height || width != _width);
159 if (height != _height) {
160 _timestampItem.setHeight(height);
161 _senderItem.setHeight(height);
164 if (needGeometryChange) {
165 prepareGeometryChange();
170 setPos(0, linePos); // set pos is _very_ cheap if nothing changes.
173 void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn)
176 quint8 sel = (_selection & Highlighted) | Selected | minColumn;
177 if (sel != _selection) {
179 for (int i = 0; i < minColumn; i++)
180 item((ChatLineModel::ColumnType)i)->clearSelection();
181 for (int i = minColumn; i <= ChatLineModel::ContentsColumn; i++)
182 item((ChatLineModel::ColumnType)i)->setFullSelection();
187 quint8 sel = _selection & Highlighted;
188 if (sel != _selection) {
190 for (int i = 0; i <= ChatLineModel::ContentsColumn; i++)
191 item((ChatLineModel::ColumnType)i)->clearSelection();
197 void ChatLine::setHighlighted(bool highlighted)
200 _selection |= Highlighted;
202 _selection &= ~Highlighted;
206 void ChatLine::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
211 const QAbstractItemModel* model_ = model();
212 QModelIndex myIdx = model_->index(row(), 0);
213 Message::Type type = (Message::Type)myIdx.data(MessageModel::TypeRole).toInt();
214 auto label = myIdx.data(ChatLineModel::MsgLabelRole).value<UiStyle::MessageLabel>();
216 QTextCharFormat msgFmt = QtUi::style()->format({UiStyle::formatType(type), {}, {}}, label);
217 if (msgFmt.hasProperty(QTextFormat::BackgroundBrush)) {
218 painter->fillRect(boundingRect(), msgFmt.background());
221 if (_selection & Selected) {
222 QTextCharFormat selFmt = QtUi::style()->format({UiStyle::formatType(type), {}, {}}, label | UiStyle::MessageLabel::Selected);
223 if (selFmt.hasProperty(QTextFormat::BackgroundBrush)) {
224 qreal left = item((ChatLineModel::ColumnType)(_selection & ItemMask))->pos().x();
225 QRectF selectRect(left, 0, width() - left, height());
226 painter->fillRect(selectRect, selFmt.background());
231 // the items draw themselves at the correct position
232 timestampItem()->paint(painter, option, widget);
233 senderItem()->paint(painter, option, widget);
234 contentsItem()->paint(painter, option, widget);
237 // We need to dispatch all mouse-related events to the appropriate (mouse grabbing) ChatItem
239 ChatItem* ChatLine::mouseEventTargetItem(const QPointF& pos)
241 if (mouseGrabberItem())
242 return mouseGrabberItem();
246 void ChatLine::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
248 ChatItem* item = mouseEventTargetItem(event->pos());
250 item->mouseMoveEvent(event);
253 void ChatLine::mousePressEvent(QGraphicsSceneMouseEvent* event)
255 ChatItem* item = mouseEventTargetItem(event->pos());
257 item->mousePressEvent(event);
260 void ChatLine::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
262 ChatItem* item = mouseEventTargetItem(event->pos());
264 item->mouseReleaseEvent(event);
267 void ChatLine::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
269 ChatItem* item = mouseEventTargetItem(event->pos());
270 if (item && !_hoverItem) {
272 item->hoverEnterEvent(event);
276 void ChatLine::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
279 _hoverItem->hoverLeaveEvent(event);
280 _hoverItem = nullptr;
284 void ChatLine::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
286 ChatItem* item = mouseEventTargetItem(event->pos());
288 item->hoverMoveEvent(event);