1 /***************************************************************************
2 * Copyright (C) 2005-2016 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>
26 #include "chatlinemodel.h"
28 #include "chatscene.h"
30 class ChatLine : public QGraphicsItem
33 ChatLine(int row, QAbstractItemModel *model,
35 const qreal ×tampWidth, const qreal &senderWidth, const qreal &contentsWidth,
36 const QPointF &senderPos, const QPointF &contentsPos,
37 QGraphicsItem *parent = 0);
41 virtual inline QRectF boundingRect() const { return QRectF(0, 0, _width, _height); }
43 inline QModelIndex index() const { return model()->index(row(), 0); }
44 inline MsgId msgId() const { return index().data(MessageModel::MsgIdRole).value<MsgId>(); }
45 inline Message::Type msgType() const { return (Message::Type)index().data(MessageModel::TypeRole).toInt(); }
47 inline int row() const { return _row; }
48 inline void setRow(int row) { _row = row; }
50 inline const QAbstractItemModel *model() const { return _model; }
51 inline ChatScene *chatScene() const { return qobject_cast<ChatScene *>(scene()); }
52 inline ChatView *chatView() const { return chatScene() ? chatScene()->chatView() : 0; }
54 inline qreal width() const { return _width; }
55 inline qreal height() const { return _height; }
57 ChatItem *item(ChatLineModel::ColumnType);
58 ChatItem *itemAt(const QPointF &pos);
59 inline ChatItem *timestampItem() { return &_timestampItem; }
60 inline ChatItem *senderItem() { return &_senderItem; }
61 inline ContentsChatItem *contentsItem() { return &_contentsItem; }
63 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
64 enum { Type = ChatScene::ChatLineType };
65 virtual inline int type() const { return Type; }
67 // pos is relative to the parent ChatLine
68 void setFirstColumn(const qreal ×tampWidth, const qreal &senderWidth, const QPointF &senderPos);
69 // setSecondColumn and setGeometryByWidth both also relocate the chatline.
70 // the _bottom_ position is passed via linePos. linePos is updated to the top of the chatLine.
71 void setSecondColumn(const qreal &senderWidth, const qreal &contentsWidth, const QPointF &contentsPos, qreal &linePos);
72 void setGeometryByWidth(const qreal &width, const qreal &contentsWidth, qreal &linePos);
74 void setSelected(bool selected, ChatLineModel::ColumnType minColumn = ChatLineModel::ContentsColumn);
75 void setHighlighted(bool highlighted);
80 virtual bool sceneEvent(QEvent *event);
82 // These need to be relayed to the appropriate ChatItem
83 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
84 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
85 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
86 virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
87 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
88 virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
90 ChatItem *mouseEventTargetItem(const QPointF &pos);
92 inline ChatItem *mouseGrabberItem() const { return _mouseGrabberItem; }
93 void setMouseGrabberItem(ChatItem *item);
97 QAbstractItemModel *_model;
98 ContentsChatItem _contentsItem;
99 SenderChatItem _senderItem;
100 TimestampChatItem _timestampItem;
101 qreal _width, _height;
103 enum { ItemMask = 0x3f,
105 Highlighted = 0x80 };
106 // _selection[1..0] ... Min Selected Column (See MessageModel::ColumnType)
107 // _selection[5..2] ... reserved for new column types
108 // _selection[6] ...... Selected
109 // _selection[7] ...... Highlighted
110 quint8 _selection; // save space, so we put both the col and the flags into one byte
112 ChatItem *_mouseGrabberItem;
113 ChatItem *_hoverItem;