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 inline bool hasLayout() const { return (bool)_data; }
51 QTextLayout *createLayout(QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft);
52 virtual inline QTextLayout *createLayout() { return createLayout(QTextOption::WrapAnywhere); }
53 virtual void updateLayout();
56 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
58 QVariant data(int role) const;
60 // selection stuff, to be called by the scene
61 void clearSelection();
62 void setFullSelection();
63 void continueSelecting(const QPointF &pos);
65 QList<QRectF> findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive);
68 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
69 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
70 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
72 inline QTextLayout *layout() const;
74 virtual inline QVector<QTextLayout::FormatRange> additionalFormats() const { return QVector<QTextLayout::FormatRange>(); }
75 qint16 posToCursor(const QPointF &pos);
77 inline void setPrivateData(ChatItemPrivate *data) { Q_ASSERT(!_data); _data = data; }
78 inline ChatItemPrivate *privateData() const;
80 // WARNING: setGeometry and setHeight should not be used without either:
81 // a) calling prepareGeometryChange() immediately before setColumns()
82 // b) calling Chatline::setPos() immediately afterwards
83 inline void setGeometry(qreal width, qreal height) {
84 _boundingRect.setWidth(width);
85 _boundingRect.setHeight(height);
87 inline void setHeight(const qreal &height) { _boundingRect.setHeight(height); }
88 inline void setWidth(const qreal &width) { _boundingRect.setWidth(width); }
91 // internal selection stuff
92 void setSelection(int start, int length);
94 ChatItemPrivate *_data;
97 enum SelectionMode { NoSelection, PartialSelection, FullSelection };
98 SelectionMode _selectionMode;
99 qint16 _selectionStart, _selectionEnd;
101 friend class ChatLine;
104 struct ChatItemPrivate {
106 ChatItemPrivate(QTextLayout *l) : layout(l) {}
112 // inlines of ChatItem
113 QTextLayout *ChatItem::layout() const { return privateData()->layout; }
114 ChatItemPrivate *ChatItem::privateData() const { return _data; }
116 // ************************************************************
118 // ************************************************************
120 //! A ChatItem for the timestamp column
121 class TimestampChatItem : public ChatItem {
123 TimestampChatItem(const qreal &width, const qreal &height, QGraphicsItem *parent) : ChatItem(width, height, QPointF(0, 0), parent) {}
124 virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
127 // ************************************************************
129 // ************************************************************
130 //! A ChatItem for the sender column
131 class SenderChatItem : public ChatItem {
133 SenderChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent) : ChatItem(width, height, pos, parent) {}
134 virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
135 virtual inline QTextLayout *createLayout() { return ChatItem::createLayout(QTextOption::WrapAnywhere, Qt::AlignRight); }
138 // ************************************************************
140 // ************************************************************
141 struct ContentsChatItemPrivate;
143 //! A ChatItem for the contents column
144 class ContentsChatItem : public ChatItem {
146 ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent);
148 inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
151 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
152 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
153 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
154 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
155 virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
157 virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
159 virtual void updateLayout();
163 class WrapColumnFinder;
165 inline ContentsChatItemPrivate *privateData() const;
167 QList<Clickable> findClickables();
170 // WARNING: setGeometry and setHeight should not be used without either:
171 // a) calling prepareGeometryChange() immediately before setColumns()
172 // b) calling Chatline::setPos() immediately afterwards
173 qreal setGeometryByWidth(qreal w);
174 friend class ChatLine;
175 friend struct ContentsChatItemPrivate;
177 inline QFontMetricsF *fontMetrics() const { return _fontMetrics; }
178 QFontMetricsF *_fontMetrics;
181 struct ContentsChatItem::Clickable {
182 // Don't change these enums without also changing the regexps in analyze()!
194 inline Clickable() : type(Invalid) {};
195 inline Clickable(Type type_, quint16 start_, quint16 length_) : type(type_), start(start_), length(length_) {};
196 inline bool isValid() const { return type != Invalid; }
199 struct ContentsChatItemPrivate : ChatItemPrivate {
200 QList<ContentsChatItem::Clickable> clickables;
201 ContentsChatItem::Clickable currentClickable;
204 ContentsChatItemPrivate(QTextLayout *l, const QList<ContentsChatItem::Clickable> &c) : ChatItemPrivate(l), clickables(c), hasDragged(false) {}
207 //inlines regarding ContentsChatItemPrivate
208 ContentsChatItemPrivate *ContentsChatItem::privateData() const { return (ContentsChatItemPrivate *)ChatItem::privateData(); }
210 class ContentsChatItem::WrapColumnFinder {
212 WrapColumnFinder(ChatItem *parent);
215 qint16 nextWrapColumn();
221 ChatLineModel::WrapList wrapList;
224 qreal choppedTrailing;
227 /*************************************************************************************************/
229 // Avoid circular include deps
230 #include "chatline.h"
231 const QAbstractItemModel *ChatItem::model() const { return static_cast<ChatLine *>(parentItem())->model(); }
232 int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }