1 /***************************************************************************
2 * Copyright (C) 2005-08 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 ***************************************************************************/
24 #include <QGraphicsItem>
27 #include "chatlinemodel.h"
28 #include "chatscene.h"
33 struct ChatItemPrivate;
35 class ChatItem : public QGraphicsItem {
37 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 QTextLayout *createLayout(QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft) const;
51 virtual void doLayout();
54 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
55 enum { Type = ChatScene::ChatItemType };
56 virtual inline int type() const { return Type; }
58 QVariant data(int role) const;
60 // selection stuff, to be called by the scene
61 QString selection() const;
62 void clearSelection();
63 void setFullSelection();
64 void continueSelecting(const QPointF &pos);
65 bool hasSelection() const;
66 bool isPosOverSelection(const QPointF &pos) const;
68 QList<QRectF> findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive);
70 virtual void handleClick(const QPointF &pos, ChatScene::ClickMode);
79 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
80 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
81 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
83 //virtual bool handleClick(ClickMode mode);
85 inline QTextLayout *layout() const;
87 virtual QTextLayout::FormatRange selectionFormat() const;
88 virtual inline QVector<QTextLayout::FormatRange> additionalFormats() const { return QVector<QTextLayout::FormatRange>(); }
90 inline qint16 selectionStart() const { return _selectionStart; }
91 inline void setSelectionStart(qint16 start) { _selectionStart = start; }
92 inline qint16 selectionEnd() const { return _selectionEnd; }
93 inline void setSelectionEnd(qint16 end) { _selectionEnd = end; }
94 inline SelectionMode selectionMode() const { return _selectionMode; }
95 inline void setSelectionMode(SelectionMode mode) { _selectionMode = mode; }
96 void setSelection(SelectionMode mode, qint16 selectionStart, qint16 selectionEnd);
98 qint16 posToCursor(const QPointF &pos) const;
100 inline bool hasPrivateData() const { return (bool)_data; }
101 ChatItemPrivate *privateData() const;
102 virtual inline ChatItemPrivate *newPrivateData();
104 // WARNING: setGeometry and setHeight should not be used without either:
105 // a) calling prepareGeometryChange() immediately before setColumns()
106 // b) calling Chatline::setPos() immediately afterwards
107 inline void setGeometry(qreal width, qreal height) {
108 _boundingRect.setWidth(width);
109 _boundingRect.setHeight(height);
111 inline void setHeight(const qreal &height) {
112 _boundingRect.setHeight(height);
114 inline void setWidth(const qreal &width) {
115 _boundingRect.setWidth(width);
119 // internal selection stuff
120 void setSelection(int start, int length);
122 ChatItemPrivate *_data;
123 QRectF _boundingRect;
125 SelectionMode _selectionMode;
126 qint16 _selectionStart, _selectionEnd;
128 friend class ChatLine;
131 struct ChatItemPrivate {
133 ChatItemPrivate(QTextLayout *l) : layout(l) {}
139 // inlines of ChatItem
140 QTextLayout *ChatItem::layout() const { return privateData()->layout; }
141 ChatItemPrivate *ChatItem::newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::WrapAnywhere)); }
143 // ************************************************************
145 // ************************************************************
147 //! A ChatItem for the timestamp column
148 class TimestampChatItem : public ChatItem {
150 TimestampChatItem(const qreal &width, const qreal &height, QGraphicsItem *parent) : ChatItem(width, height, QPointF(0, 0), parent) {}
151 enum { Type = ChatScene::TimestampChatItemType };
152 virtual inline int type() const { return Type; }
153 virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
156 // ************************************************************
158 // ************************************************************
159 //! A ChatItem for the sender column
160 class SenderChatItem : public ChatItem {
162 SenderChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent) : ChatItem(width, height, pos, parent) {}
163 virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
166 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
167 enum { Type = ChatScene::SenderChatItemType };
168 virtual inline int type() const { return Type; }
169 virtual inline ChatItemPrivate *newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::ManualWrap, Qt::AlignRight)); }
172 // ************************************************************
174 // ************************************************************
175 struct ContentsChatItemPrivate;
177 //! A ChatItem for the contents column
178 class ContentsChatItem : public ChatItem {
180 ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent);
182 enum { Type = ChatScene::ContentsChatItemType };
183 virtual inline int type() const { return Type; }
185 inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
186 inline QFontMetricsF *fontMetrics() const { return _fontMetrics; }
189 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
190 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
191 virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
192 virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
194 virtual void handleClick(const QPointF &pos, ChatScene::ClickMode clickMode);
196 virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
198 virtual void doLayout();
199 virtual inline ChatItemPrivate *newPrivateData();
203 class WrapColumnFinder;
205 inline ContentsChatItemPrivate *privateData() const;
207 QList<Clickable> findClickables() const;
209 void showWebPreview(const Clickable &click);
210 void clearWebPreview();
212 qreal setGeometryByWidth(qreal w);
213 friend class ChatLine;
214 friend struct ContentsChatItemPrivate;
216 QFontMetricsF *_fontMetrics;
219 struct ContentsChatItem::Clickable {
220 // Don't change these enums without also changing the regexps in analyze()!
232 inline Clickable() : type(Invalid) {};
233 inline Clickable(Type type_, quint16 start_, quint16 length_) : type(type_), start(start_), length(length_) {};
234 inline bool isValid() const { return type != Invalid; }
237 struct ContentsChatItemPrivate : ChatItemPrivate {
238 ContentsChatItem *contentsItem;
239 QList<ContentsChatItem::Clickable> clickables;
240 ContentsChatItem::Clickable currentClickable;
242 ContentsChatItemPrivate(QTextLayout *l, const QList<ContentsChatItem::Clickable> &c, ContentsChatItem *parent)
243 : ChatItemPrivate(l), contentsItem(parent), clickables(c) {}
246 //inlines regarding ContentsChatItemPrivate
247 ChatItemPrivate *ContentsChatItem::newPrivateData() {
248 return new ContentsChatItemPrivate(createLayout(QTextOption::WrapAnywhere), findClickables(), this);
250 ContentsChatItemPrivate *ContentsChatItem::privateData() const { return (ContentsChatItemPrivate *)ChatItem::privateData(); }
252 class ContentsChatItem::WrapColumnFinder {
254 WrapColumnFinder(ChatItem *parent);
257 qint16 nextWrapColumn();
263 ChatLineModel::WrapList wrapList;
266 qreal choppedTrailing;
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(); }