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 ***************************************************************************/
27 #include "chatlinemodel.h"
28 #include "chatscene.h"
29 #include "clickable.h"
33 #include <QTextLayout>
38 /* All external positions are relative to the parent ChatLine */
39 /* Yes, that's also true for the boundingRect() and related things */
44 // boundingRect is relative to the parent ChatLine
45 ChatItem(const QRectF &boundingRect, ChatLine *parent);
49 const QAbstractItemModel *model() const;
50 ChatLine *chatLine() const;
51 ChatScene *chatScene() const;
52 ChatView *chatView() const;
54 virtual ChatLineModel::ColumnType column() const = 0;
56 // The boundingRect() is relative to the parent ChatLine
57 inline QRectF boundingRect() const;
58 inline qreal width() const;
59 inline qreal height() const;
60 inline QPointF pos() const;
61 inline qreal x() const;
62 inline qreal y() const;
64 QPointF mapToLine(const QPointF &) const;
65 QPointF mapFromLine(const QPointF &) const;
66 QPointF mapToScene(const QPointF &) const;
67 QPointF mapFromScene(const QPointF &) const;
69 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
70 virtual inline int type() const { return ChatScene::ChatItemType; }
72 QVariant data(int role) const;
74 // selection stuff, to be called by the scene
75 QString selection() const;
76 void clearSelection();
77 void setFullSelection();
78 void continueSelecting(const QPointF &pos);
79 bool hasSelection() const;
80 bool isPosOverSelection(const QPointF &pos) const;
82 QList<QRectF> findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive);
84 virtual void addActionsToMenu(QMenu *menu, const QPointF &itemPos);
85 virtual void handleClick(const QPointF &pos, ChatScene::ClickMode);
87 void initLayoutHelper(QTextLayout *layout, QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft) const;
89 //! Remove internally cached data
90 /** This removes e.g. the cached QTextLayout to avoid wasting space for nonvisible ChatLines
92 virtual void clearCache();
101 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
102 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
103 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
104 virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *) {}
105 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *) {}
106 virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *) {}
108 QTextLayout *layout() const;
110 virtual void initLayout(QTextLayout *layout) const;
111 virtual void doLayout(QTextLayout *) const;
112 virtual UiStyle::FormatList formatList() const;
114 void paintBackground(QPainter *);
115 virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
116 void overlayFormat(UiStyle::FormatList &fmtList, quint16 start, quint16 end, UiStyle::FormatType overlayFmt) const;
118 inline qint16 selectionStart() const { return _selectionStart; }
119 inline void setSelectionStart(qint16 start) { _selectionStart = start; }
120 inline qint16 selectionEnd() const { return _selectionEnd; }
121 inline void setSelectionEnd(qint16 end) { _selectionEnd = end; }
122 inline SelectionMode selectionMode() const { return _selectionMode; }
123 inline void setSelectionMode(SelectionMode mode) { _selectionMode = mode; }
124 void setSelection(SelectionMode mode, qint16 selectionStart, qint16 selectionEnd);
126 virtual bool hasActiveClickable() const;
127 virtual std::pair<quint16, quint16> activeClickableRange() const;
129 qint16 posToCursor(const QPointF &pos) const;
131 inline void setGeometry(qreal width, qreal height) { clearCache(); _boundingRect.setSize(QSizeF(width, height)); }
132 inline void setHeight(const qreal &height) { clearCache(); _boundingRect.setHeight(height); }
133 inline void setWidth(const qreal &width) { clearCache(); _boundingRect.setWidth(width); }
134 inline void setPos(const QPointF &pos) { _boundingRect.moveTopLeft(pos); }
138 QRectF _boundingRect;
140 SelectionMode _selectionMode;
141 qint16 _selectionStart, _selectionEnd;
143 mutable QTextLayout *_cachedLayout;
145 // internal selection stuff
146 void setSelection(int start, int length);
148 friend class ChatLine;
152 // ************************************************************
154 // ************************************************************
156 //! A ChatItem for the timestamp column
157 class TimestampChatItem : public ChatItem
160 TimestampChatItem(const QRectF &boundingRect, ChatLine *parent) : ChatItem(boundingRect, parent) {}
161 virtual inline int type() const { return ChatScene::TimestampChatItemType; }
162 virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
166 // ************************************************************
168 // ************************************************************
169 //! A ChatItem for the sender column
170 class SenderChatItem : public ChatItem
173 SenderChatItem(const QRectF &boundingRect, ChatLine *parent) : ChatItem(boundingRect, parent) {}
174 virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
175 virtual void handleClick(const QPointF &pos, ChatScene::ClickMode clickMode);
178 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
179 virtual inline int type() const { return ChatScene::SenderChatItemType; }
180 virtual void initLayout(QTextLayout *layout) const;
184 // ************************************************************
186 // ************************************************************
187 struct ContentsChatItemPrivate;
189 //! A ChatItem for the contents column
190 class ContentsChatItem : public ChatItem
192 Q_DECLARE_TR_FUNCTIONS(ContentsChatItem)
195 ContentsChatItem(const QPointF &pos, const qreal &width, ChatLine *parent);
198 virtual inline int type() const { return ChatScene::ContentsChatItemType; }
200 inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
201 QFontMetricsF *fontMetrics() const;
203 virtual void clearCache();
206 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
207 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
208 virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
209 virtual void handleClick(const QPointF &pos, ChatScene::ClickMode clickMode);
211 virtual bool hasActiveClickable() const;
212 virtual std::pair<quint16, quint16> activeClickableRange() const;
214 virtual void addActionsToMenu(QMenu *menu, const QPointF &itemPos);
215 virtual void copyLinkToClipboard();
217 virtual void initLayout(QTextLayout *layout) const;
218 virtual void doLayout(QTextLayout *layout) const;
219 virtual UiStyle::FormatList formatList() const;
223 class WrapColumnFinder;
225 mutable ContentsChatItemPrivate *_data;
226 ContentsChatItemPrivate *privateData() const;
228 Clickable clickableAt(const QPointF &pos) const;
231 void showWebPreview(const Clickable &click);
232 void clearWebPreview();
234 qreal setGeometryByWidth(qreal w);
236 QFontMetricsF *_fontMetrics;
238 // we need a receiver for Action signals
239 static ActionProxy _actionProxy;
241 friend class ChatLine;
242 friend struct ContentsChatItemPrivate;
246 struct ContentsChatItemPrivate {
247 ContentsChatItem *contentsItem;
248 ClickableList clickables;
249 Clickable currentClickable;
250 Clickable activeClickable;
252 ContentsChatItemPrivate(const ClickableList &c, ContentsChatItem *parent) : contentsItem(parent), clickables(c) {}
255 class ContentsChatItem::WrapColumnFinder
258 WrapColumnFinder(const ChatItem *parent);
261 qint16 nextWrapColumn(qreal width);
264 const ChatItem *item;
267 ChatLineModel::WrapList wrapList;
270 qreal choppedTrailing;
274 //! Acts as a proxy for Action signals targetted at a ContentsChatItem
275 /** Since a ChatItem is not a QObject, hence cannot receive signals, we use a static ActionProxy
276 * as a receiver instead. This avoids having to handle ChatItem actions (e.g. context menu entries)
277 * outside the ChatItem.
279 class ContentsChatItem::ActionProxy : public QObject
284 inline void copyLinkToClipboard() { item()->copyLinkToClipboard(); }
287 /// Returns the ContentsChatItem that should receive the action event.
288 /** For efficiency reasons, values are not checked for validity. You gotta make sure that you set the data() member
289 * in the Action correctly.
290 * @return The ChatItem from which the sending Action originated
292 inline ContentsChatItem *item() const
294 return static_cast<ContentsChatItem *>(qobject_cast<QAction *>(sender())->data().value<void *>());
299 /*************************************************************************************************/
303 QRectF ChatItem::boundingRect() const { return _boundingRect; }
304 qreal ChatItem::width() const { return _boundingRect.width(); }
305 qreal ChatItem::height() const { return _boundingRect.height(); }
306 QPointF ChatItem::pos() const { return _boundingRect.topLeft(); }
307 qreal ChatItem::x() const { return pos().x(); }
308 qreal ChatItem::y() const { return pos().y(); }