1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 #include <QGraphicsItem>
28 #include "chatlinemodel.h"
29 #include "chatscene.h"
30 #include "clickable.h"
34 #include <QTextLayout>
36 class ChatItem : public QGraphicsItem {
38 ChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent);
41 inline const QAbstractItemModel *model() const;
42 inline int row() const;
43 virtual ChatLineModel::ColumnType column() const = 0;
44 inline ChatScene *chatScene() const { return qobject_cast<ChatScene *>(scene()); }
46 inline QRectF boundingRect() const { return _boundingRect; }
47 inline qreal width() const { return _boundingRect.width(); }
48 inline qreal height() const { return _boundingRect.height(); }
50 void initLayoutHelper(QTextLayout *layout, QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft) const;
51 virtual inline void initLayout(QTextLayout *layout) const {
52 initLayoutHelper(layout, QTextOption::NoWrap);
55 virtual void doLayout(QTextLayout *) const;
56 virtual UiStyle::FormatList formatList() const;
58 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
59 enum { Type = ChatScene::ChatItemType };
60 virtual inline int type() const { return Type; }
62 QVariant data(int role) const;
64 // selection stuff, to be called by the scene
65 QString selection() const;
66 void clearSelection();
67 void setFullSelection();
68 void continueSelecting(const QPointF &pos);
69 bool hasSelection() const;
70 bool isPosOverSelection(const QPointF &pos) const;
72 QList<QRectF> findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive);
74 virtual void addActionsToMenu(QMenu *menu, const QPointF &itemPos);
75 virtual void handleClick(const QPointF &pos, ChatScene::ClickMode);
84 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
85 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
86 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
88 void paintBackground(QPainter *);
89 QVector<QTextLayout::FormatRange> selectionFormats() const;
90 virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
91 void overlayFormat(UiStyle::FormatList &fmtList, int start, int end, quint32 overlayFmt) const;
93 inline qint16 selectionStart() const { return _selectionStart; }
94 inline void setSelectionStart(qint16 start) { _selectionStart = start; }
95 inline qint16 selectionEnd() const { return _selectionEnd; }
96 inline void setSelectionEnd(qint16 end) { _selectionEnd = end; }
97 inline SelectionMode selectionMode() const { return _selectionMode; }
98 inline void setSelectionMode(SelectionMode mode) { _selectionMode = mode; }
99 void setSelection(SelectionMode mode, qint16 selectionStart, qint16 selectionEnd);
101 qint16 posToCursor(const QPointF &pos) const;
103 // WARNING: setGeometry and setHeight should not be used without either:
104 // a) calling prepareGeometryChange() immediately before setColumns()
105 // b) calling Chatline::setPos() immediately afterwards
106 inline void setGeometry(qreal width, qreal height) {
107 _boundingRect.setWidth(width);
108 _boundingRect.setHeight(height);
110 inline void setHeight(const qreal &height) {
111 _boundingRect.setHeight(height);
113 inline void setWidth(const qreal &width) {
114 _boundingRect.setWidth(width);
118 // internal selection stuff
119 void setSelection(int start, int length);
121 QRectF _boundingRect;
123 SelectionMode _selectionMode;
124 qint16 _selectionStart, _selectionEnd;
126 friend class ChatLine;
129 // ************************************************************
131 // ************************************************************
133 //! A ChatItem for the timestamp column
134 class TimestampChatItem : public ChatItem {
136 TimestampChatItem(const qreal &width, const qreal &height, QGraphicsItem *parent) : ChatItem(width, height, QPointF(0, 0), parent) {}
137 enum { Type = ChatScene::TimestampChatItemType };
138 virtual inline int type() const { return Type; }
139 virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
142 // ************************************************************
144 // ************************************************************
145 //! A ChatItem for the sender column
146 class SenderChatItem : public ChatItem {
148 SenderChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent) : ChatItem(width, height, pos, parent) {}
149 virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
152 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
153 enum { Type = ChatScene::SenderChatItemType };
154 virtual inline int type() const { return Type; }
155 virtual inline void initLayout(QTextLayout *layout) const {
156 initLayoutHelper(layout, QTextOption::ManualWrap, Qt::AlignRight);
161 // ************************************************************
163 // ************************************************************
164 struct ContentsChatItemPrivate;
166 //! A ChatItem for the contents column
167 class ContentsChatItem : public ChatItem {
168 Q_DECLARE_TR_FUNCTIONS(ContentsChatItem)
171 ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent);
174 enum { Type = ChatScene::ContentsChatItemType };
175 virtual inline int type() const { return Type; }
177 inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
178 QFontMetricsF *fontMetrics() const;
181 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
182 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
183 virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
184 virtual void handleClick(const QPointF &pos, ChatScene::ClickMode clickMode);
186 virtual void addActionsToMenu(QMenu *menu, const QPointF &itemPos);
187 virtual void copyLinkToClipboard();
189 virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
191 virtual inline void initLayout(QTextLayout *layout) const {
192 initLayoutHelper(layout, QTextOption::WrapAtWordBoundaryOrAnywhere);
195 virtual void doLayout(QTextLayout *layout) const;
196 virtual UiStyle::FormatList formatList() const;
200 class WrapColumnFinder;
202 ContentsChatItemPrivate *_data;
203 ContentsChatItemPrivate *privateData() const;
205 Clickable clickableAt(const QPointF &pos) const;
208 void showWebPreview(const Clickable &click);
209 void clearWebPreview();
211 qreal setGeometryByWidth(qreal w);
212 friend class ChatLine;
213 friend struct ContentsChatItemPrivate;
215 QFontMetricsF *_fontMetrics;
217 // we need a receiver for Action signals
218 static ActionProxy _actionProxy;
221 struct ContentsChatItemPrivate {
222 ContentsChatItem *contentsItem;
223 ClickableList clickables;
224 Clickable currentClickable;
225 Clickable activeClickable;
227 ContentsChatItemPrivate(const ClickableList &c, ContentsChatItem *parent) : contentsItem(parent), clickables(c) {}
230 class ContentsChatItem::WrapColumnFinder {
232 WrapColumnFinder(const ChatItem *parent);
235 qint16 nextWrapColumn(qreal width);
238 const ChatItem *item;
241 ChatLineModel::WrapList wrapList;
244 qreal choppedTrailing;
247 //! Acts as a proxy for Action signals targetted at a ContentsChatItem
248 /** Since a ChatItem is not a QObject, hence cannot receive signals, we use a static ActionProxy
249 * as a receiver instead. This avoids having to handle ChatItem actions (e.g. context menu entries)
250 * outside the ChatItem.
252 class ContentsChatItem::ActionProxy : public QObject {
256 inline void copyLinkToClipboard() { item()->copyLinkToClipboard(); }
259 /// Returns the ContentsChatItem that should receive the action event.
260 /** For efficiency reasons, values are not checked for validity. You gotta make sure that you set the data() member
261 * in the Action correctly.
262 * @return The ChatItem from which the sending Action originated
264 inline ContentsChatItem *item() const {
265 return static_cast<ContentsChatItem *>(qobject_cast<QAction *>(sender())->data().value<void *>());
269 /*************************************************************************************************/
271 // Avoid circular include deps
272 #include "chatline.h"
273 const QAbstractItemModel *ChatItem::model() const { return static_cast<ChatLine *>(parentItem())->model(); }
274 int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }