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, ChatLineModel::ColumnType column, 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 QFontMetricsF *fontMetrics() const { return _fontMetrics; }
47 inline QRectF boundingRect() const { return _boundingRect; }
48 inline qreal width() const { return _boundingRect.width(); }
49 inline qreal height() const { return _boundingRect.height(); }
51 inline bool hasLayout() const { return (bool)_data; }
52 QTextLayout *createLayout(QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft);
53 virtual inline QTextLayout *createLayout() { return createLayout(QTextOption::WrapAnywhere); }
56 virtual void updateLayout();
57 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
59 QVariant data(int role) const;
61 // selection stuff, to be called by the scene
62 void clearSelection();
63 void setFullSelection();
64 void continueSelecting(const QPointF &pos);
66 QList<QRectF> findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive);
69 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
70 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
71 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
73 inline QTextLayout *layout() const;
75 virtual inline QVector<QTextLayout::FormatRange> additionalFormats() const { return QVector<QTextLayout::FormatRange>(); }
76 qint16 posToCursor(const QPointF &pos);
78 inline void ChatItem::setPrivateData(ChatItemPrivate *data) { Q_ASSERT(!_data); _data = data; }
79 inline ChatItemPrivate *ChatItem::privateData() const;
81 // WARNING: setGeometry and setHeight should not be used without either:
82 // a) calling prepareGeometryChange() immediately before setColumns()
83 // b) calling Chatline::setPos() immediately afterwards
84 inline void setGeometry(qreal width, qreal height) {
85 _boundingRect.setWidth(width);
86 _boundingRect.setHeight(height);
88 inline void setHeight(const qreal &height) { _boundingRect.setHeight(height); }
89 inline void setWidth(const qreal &width) { _boundingRect.setWidth(width); }
92 // internal selection stuff
93 void setSelection(int start, int length);
95 ChatItemPrivate *_data;
97 QFontMetricsF *_fontMetrics;
99 enum SelectionMode { NoSelection, PartialSelection, FullSelection };
100 SelectionMode _selectionMode;
101 qint16 _selectionStart, _selectionEnd;
103 friend class ChatLine;
106 struct ChatItemPrivate {
108 ChatItemPrivate(QTextLayout *l) : layout(l) {}
114 // inlines of ChatItem
115 QTextLayout *ChatItem::layout() const { return privateData()->layout; }
116 ChatItemPrivate *ChatItem::privateData() const { return _data; }
118 // ************************************************************
120 // ************************************************************
122 //! A ChatItem for the timestamp column
123 class TimestampChatItem : public ChatItem {
125 TimestampChatItem(const qreal &width, const qreal &height, QGraphicsItem *parent) : ChatItem(width, height, QPointF(0, 0), column(), parent) {}
126 virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
129 // ************************************************************
131 // ************************************************************
132 //! A ChatItem for the sender column
133 class SenderChatItem : public ChatItem {
135 SenderChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent) : ChatItem(width, height, pos, column(), parent) {}
136 virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
137 virtual inline QTextLayout *createLayout() { return ChatItem::createLayout(QTextOption::WrapAnywhere, Qt::AlignRight); }
140 // ************************************************************
142 // ************************************************************
143 struct ContentsChatItemPrivate;
145 //! A ChatItem for the contents column
146 class ContentsChatItem : public ChatItem {
148 ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent);
150 inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
153 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
154 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
155 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
156 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
157 virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
159 virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
161 virtual void updateLayout();
165 class WrapColumnFinder;
167 inline ContentsChatItemPrivate *privateData() const;
169 QList<Clickable> findClickables();
172 // WARNING: setGeometry and setHeight should not be used without either:
173 // a) calling prepareGeometryChange() immediately before setColumns()
174 // b) calling Chatline::setPos() immediately afterwards
175 qreal setGeometryByWidth(qreal w);
176 friend class ChatLine;
177 friend struct ContentsChatItemPrivate;
180 struct ContentsChatItem::Clickable {
181 // Don't change these enums without also changing the regexps in analyze()!
193 inline Clickable() : type(Invalid) {};
194 inline Clickable(Type type_, quint16 start_, quint16 length_) : type(type_), start(start_), length(length_) {};
195 inline bool isValid() const { return type != Invalid; }
198 struct ContentsChatItemPrivate : ChatItemPrivate {
199 QList<ContentsChatItem::Clickable> clickables;
200 ContentsChatItem::Clickable currentClickable;
203 ContentsChatItemPrivate(QTextLayout *l, const QList<ContentsChatItem::Clickable> &c) : ChatItemPrivate(l), clickables(c), hasDragged(false) {}
206 class ContentsChatItem::WrapColumnFinder {
208 WrapColumnFinder(ChatItem *parent);
211 qint16 nextWrapColumn();
217 ChatLineModel::WrapList wrapList;
220 qreal choppedTrailing;
226 /*************************************************************************************************/
228 // Avoid circular include deps
229 #include "chatline.h"
230 const QAbstractItemModel *ChatItem::model() const { return static_cast<ChatLine *>(parentItem())->model(); }
231 int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }
233 ContentsChatItemPrivate *ContentsChatItem::privateData() const { return (ContentsChatItemPrivate *)privateData(); }