cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / qtui / chatline.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2022 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include <QGraphicsItem>
24 #include <QModelIndex>
25 #include <QRectF>
26
27 #include "chatitem.h"
28 #include "chatlinemodel.h"
29 #include "chatscene.h"
30
31 class QAbstractItemModel;
32 class QEvent;
33 class QGraphicsSceneMouseEvent;
34 class QGraphicsSceneHoverEvent;
35 class QPainter;
36 class QStyleOptionGraphicsItem;
37
38 class ChatLine : public QGraphicsItem
39 {
40 public:
41     ChatLine(int row,
42              QAbstractItemModel* model,
43              const qreal& width,
44              const qreal& timestampWidth,
45              const qreal& senderWidth,
46              const qreal& contentsWidth,
47              const QPointF& senderPos,
48              const QPointF& contentsPos,
49              QGraphicsItem* parent = nullptr);
50
51     ~ChatLine() override;
52
53     inline QRectF boundingRect() const override { return {0, 0, _width, _height}; }
54
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(); }
58
59     inline int row() const { return _row; }
60     inline void setRow(int row) { _row = row; }
61
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; }
65
66     inline qreal width() const { return _width; }
67     inline qreal height() const { return _height; }
68
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; }
74
75     void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
76     enum
77     {
78         Type = ChatScene::ChatLineType
79     };
80     inline int type() const override { return Type; }
81
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);
88
89     void setSelected(bool selected, ChatLineModel::ColumnType minColumn = ChatLineModel::ContentsColumn);
90     void setHighlighted(bool highlighted);
91
92     void clearCache();
93
94 protected:
95     bool sceneEvent(QEvent* event) override;
96
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;
104
105     ChatItem* mouseEventTargetItem(const QPointF& pos);
106
107     inline ChatItem* mouseGrabberItem() const { return _mouseGrabberItem; }
108     void setMouseGrabberItem(ChatItem* item);
109
110 private:
111     int _row;
112     QAbstractItemModel* _model;
113     ContentsChatItem _contentsItem;
114     SenderChatItem _senderItem;
115     TimestampChatItem _timestampItem;
116     qreal _width, _height;
117
118     enum
119     {
120         ItemMask = 0x3f,
121         Selected = 0x40,
122         Highlighted = 0x80
123     };
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
129
130     ChatItem* _mouseGrabberItem;
131     ChatItem* _hoverItem;
132 };