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);
56 QVariant data(int role) const;
58 // selection stuff, to be called by the scene
59 void clearSelection();
60 void setFullSelection();
61 void continueSelecting(const QPointF &pos);
63 QList<QRectF> findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive);
66 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
67 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
68 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
70 inline QTextLayout *layout() const;
72 virtual inline QVector<QTextLayout::FormatRange> additionalFormats() const { return QVector<QTextLayout::FormatRange>(); }
73 qint16 posToCursor(const QPointF &pos);
75 inline bool hasPrivateData() const { return (bool)_data; }
76 ChatItemPrivate *privateData() const;
77 virtual inline ChatItemPrivate *newPrivateData();
79 // WARNING: setGeometry and setHeight should not be used without either:
80 // a) calling prepareGeometryChange() immediately before setColumns()
81 // b) calling Chatline::setPos() immediately afterwards
82 inline void setGeometry(qreal width, qreal height) {
83 _boundingRect.setWidth(width);
84 _boundingRect.setHeight(height);
86 inline void setHeight(const qreal &height) {
87 _boundingRect.setHeight(height);
89 inline void setWidth(const qreal &width) {
90 _boundingRect.setWidth(width);
94 // internal selection stuff
95 void setSelection(int start, int length);
97 ChatItemPrivate *_data;
100 enum SelectionMode { NoSelection, PartialSelection, FullSelection };
101 SelectionMode _selectionMode;
102 qint16 _selectionStart, _selectionEnd;
104 friend class ChatLine;
107 struct ChatItemPrivate {
109 ChatItemPrivate(QTextLayout *l) : layout(l) {}
115 // inlines of ChatItem
116 QTextLayout *ChatItem::layout() const { return privateData()->layout; }
117 ChatItemPrivate *ChatItem::newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::WrapAnywhere)); }
119 // ************************************************************
121 // ************************************************************
123 //! A ChatItem for the timestamp column
124 class TimestampChatItem : public ChatItem {
126 TimestampChatItem(const qreal &width, const qreal &height, QGraphicsItem *parent) : ChatItem(width, height, QPointF(0, 0), parent) {}
127 virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
130 // ************************************************************
132 // ************************************************************
133 //! A ChatItem for the sender column
134 class SenderChatItem : public ChatItem {
136 SenderChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent) : ChatItem(width, height, pos, parent) {}
137 virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
140 virtual inline ChatItemPrivate *newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::WrapAnywhere, Qt::AlignRight)); }
143 // ************************************************************
145 // ************************************************************
146 struct ContentsChatItemPrivate;
148 //! A ChatItem for the contents column
149 class ContentsChatItem : public ChatItem {
151 ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent);
153 inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
154 inline QFontMetricsF *fontMetrics() const { return _fontMetrics; }
157 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
158 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
159 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
160 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
161 virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
163 virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
165 virtual void doLayout();
166 virtual inline ChatItemPrivate *newPrivateData();
170 class WrapColumnFinder;
172 inline ContentsChatItemPrivate *privateData() const;
174 QList<Clickable> findClickables() const;
176 void showWebPreview(const Clickable &click);
177 void clearWebPreview();
179 qreal setGeometryByWidth(qreal w);
180 friend class ChatLine;
181 friend struct ContentsChatItemPrivate;
183 QFontMetricsF *_fontMetrics;
186 struct ContentsChatItem::Clickable {
187 // Don't change these enums without also changing the regexps in analyze()!
199 inline Clickable() : type(Invalid) {};
200 inline Clickable(Type type_, quint16 start_, quint16 length_) : type(type_), start(start_), length(length_) {};
201 inline bool isValid() const { return type != Invalid; }
204 struct ContentsChatItemPrivate : ChatItemPrivate {
205 ContentsChatItem *contentsItem;
206 QList<ContentsChatItem::Clickable> clickables;
207 ContentsChatItem::Clickable currentClickable;
210 ContentsChatItemPrivate(QTextLayout *l, const QList<ContentsChatItem::Clickable> &c, ContentsChatItem *parent) : ChatItemPrivate(l), contentsItem(parent), clickables(c), hasDragged(false) {}
213 //inlines regarding ContentsChatItemPrivate
214 ChatItemPrivate *ContentsChatItem::newPrivateData() { return new ContentsChatItemPrivate(createLayout(QTextOption::WrapAnywhere), findClickables(), this); }
215 ContentsChatItemPrivate *ContentsChatItem::privateData() const { return (ContentsChatItemPrivate *)ChatItem::privateData(); }
217 class ContentsChatItem::WrapColumnFinder {
219 WrapColumnFinder(ChatItem *parent);
222 qint16 nextWrapColumn();
228 ChatLineModel::WrapList wrapList;
231 qreal choppedTrailing;
234 /*************************************************************************************************/
236 // Avoid circular include deps
237 #include "chatline.h"
238 const QAbstractItemModel *ChatItem::model() const { return static_cast<ChatLine *>(parentItem())->model(); }
239 int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }