1 /***************************************************************************
2 * Copyright (C) 2005-2020 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 ***************************************************************************/
23 #include <QGraphicsItem>
24 #include <QModelIndex>
28 #include "chatlinemodel.h"
29 #include "chatscene.h"
31 class QAbstractItemModel;
33 class QGraphicsSceneMouseEvent;
34 class QGraphicsSceneHoverEvent;
36 class QStyleOptionGraphicsItem;
38 class ChatLine : public QGraphicsItem
42 QAbstractItemModel* model,
44 const qreal& timestampWidth,
45 const qreal& senderWidth,
46 const qreal& contentsWidth,
47 const QPointF& senderPos,
48 const QPointF& contentsPos,
49 QGraphicsItem* parent = nullptr);
53 inline QRectF boundingRect() const override { return {0, 0, _width, _height}; }
55 inline QModelIndex index() const { return model()->index(row(), 0); }
56 inline MsgId msgId() const { return index().data(MessageModel::MsgIdRole).value<MsgId>(); }
57 inline Message::Type msgType() const { return (Message::Type)index().data(MessageModel::TypeRole).toInt(); }
59 inline int row() const { return _row; }
60 inline void setRow(int row) { _row = row; }
62 inline const QAbstractItemModel* model() const { return _model; }
63 inline ChatScene* chatScene() const { return qobject_cast<ChatScene*>(scene()); }
64 inline ChatView* chatView() const { return chatScene() ? chatScene()->chatView() : nullptr; }
66 inline qreal width() const { return _width; }
67 inline qreal height() const { return _height; }
69 ChatItem* item(ChatLineModel::ColumnType);
70 ChatItem* itemAt(const QPointF& pos);
71 inline ChatItem* timestampItem() { return &_timestampItem; }
72 inline ChatItem* senderItem() { return &_senderItem; }
73 inline ContentsChatItem* contentsItem() { return &_contentsItem; }
75 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
78 Type = ChatScene::ChatLineType
80 inline int type() const override { return Type; }
82 // pos is relative to the parent ChatLine
83 void setFirstColumn(const qreal& timestampWidth, const qreal& senderWidth, const QPointF& senderPos);
84 // setSecondColumn and setGeometryByWidth both also relocate the chatline.
85 // the _bottom_ position is passed via linePos. linePos is updated to the top of the chatLine.
86 void setSecondColumn(const qreal& senderWidth, const qreal& contentsWidth, const QPointF& contentsPos, qreal& linePos);
87 void setGeometryByWidth(const qreal& width, const qreal& contentsWidth, qreal& linePos);
89 void setSelected(bool selected, ChatLineModel::ColumnType minColumn = ChatLineModel::ContentsColumn);
90 void setHighlighted(bool highlighted);
95 bool sceneEvent(QEvent* event) override;
97 // These need to be relayed to the appropriate ChatItem
98 void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
99 void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
100 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
101 void hoverEnterEvent(QGraphicsSceneHoverEvent* event) override;
102 void hoverLeaveEvent(QGraphicsSceneHoverEvent* event) override;
103 void hoverMoveEvent(QGraphicsSceneHoverEvent* event) override;
105 ChatItem* mouseEventTargetItem(const QPointF& pos);
107 inline ChatItem* mouseGrabberItem() const { return _mouseGrabberItem; }
108 void setMouseGrabberItem(ChatItem* item);
112 QAbstractItemModel* _model;
113 ContentsChatItem _contentsItem;
114 SenderChatItem _senderItem;
115 TimestampChatItem _timestampItem;
116 qreal _width, _height;
124 // _selection[1..0] ... Min Selected Column (See MessageModel::ColumnType)
125 // _selection[5..2] ... reserved for new column types
126 // _selection[6] ...... Selected
127 // _selection[7] ...... Highlighted
128 quint8 _selection; // save space, so we put both the col and the flags into one byte
130 ChatItem* _mouseGrabberItem;
131 ChatItem* _hoverItem;