1 /***************************************************************************
2 * Copyright (C) 2005-2019 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 <QGraphicsItem>
27 #include "chatlinemodel.h"
28 #include "chatscene.h"
30 class ChatLine : public QGraphicsItem
34 QAbstractItemModel* model,
36 const qreal& timestampWidth,
37 const qreal& senderWidth,
38 const qreal& contentsWidth,
39 const QPointF& senderPos,
40 const QPointF& contentsPos,
41 QGraphicsItem* parent = nullptr);
45 inline QRectF boundingRect() const override { return {0, 0, _width, _height}; }
47 inline QModelIndex index() const { return model()->index(row(), 0); }
48 inline MsgId msgId() const { return index().data(MessageModel::MsgIdRole).value<MsgId>(); }
49 inline Message::Type msgType() const { return (Message::Type)index().data(MessageModel::TypeRole).toInt(); }
51 inline int row() const { return _row; }
52 inline void setRow(int row) { _row = row; }
54 inline const QAbstractItemModel* model() const { return _model; }
55 inline ChatScene* chatScene() const { return qobject_cast<ChatScene*>(scene()); }
56 inline ChatView* chatView() const { return chatScene() ? chatScene()->chatView() : nullptr; }
58 inline qreal width() const { return _width; }
59 inline qreal height() const { return _height; }
61 ChatItem* item(ChatLineModel::ColumnType);
62 ChatItem* itemAt(const QPointF& pos);
63 inline ChatItem* timestampItem() { return &_timestampItem; }
64 inline ChatItem* senderItem() { return &_senderItem; }
65 inline ContentsChatItem* contentsItem() { return &_contentsItem; }
67 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
70 Type = ChatScene::ChatLineType
72 inline int type() const override { return Type; }
74 // pos is relative to the parent ChatLine
75 void setFirstColumn(const qreal& timestampWidth, const qreal& senderWidth, const QPointF& senderPos);
76 // setSecondColumn and setGeometryByWidth both also relocate the chatline.
77 // the _bottom_ position is passed via linePos. linePos is updated to the top of the chatLine.
78 void setSecondColumn(const qreal& senderWidth, const qreal& contentsWidth, const QPointF& contentsPos, qreal& linePos);
79 void setGeometryByWidth(const qreal& width, const qreal& contentsWidth, qreal& linePos);
81 void setSelected(bool selected, ChatLineModel::ColumnType minColumn = ChatLineModel::ContentsColumn);
82 void setHighlighted(bool highlighted);
87 bool sceneEvent(QEvent* event) override;
89 // These need to be relayed to the appropriate ChatItem
90 void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
91 void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
92 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
93 void hoverEnterEvent(QGraphicsSceneHoverEvent* event) override;
94 void hoverLeaveEvent(QGraphicsSceneHoverEvent* event) override;
95 void hoverMoveEvent(QGraphicsSceneHoverEvent* event) override;
97 ChatItem* mouseEventTargetItem(const QPointF& pos);
99 inline ChatItem* mouseGrabberItem() const { return _mouseGrabberItem; }
100 void setMouseGrabberItem(ChatItem* item);
104 QAbstractItemModel* _model;
105 ContentsChatItem _contentsItem;
106 SenderChatItem _senderItem;
107 TimestampChatItem _timestampItem;
108 qreal _width, _height;
116 // _selection[1..0] ... Min Selected Column (See MessageModel::ColumnType)
117 // _selection[5..2] ... reserved for new column types
118 // _selection[6] ...... Selected
119 // _selection[7] ...... Highlighted
120 quint8 _selection; // save space, so we put both the col and the flags into one byte
122 ChatItem* _mouseGrabberItem;
123 ChatItem* _hoverItem;