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"
34 class ChatItem : public QGraphicsItem {
37 ChatItem(ChatLineModel::ColumnType column, QAbstractItemModel *, QGraphicsItem *parent);
41 inline const QAbstractItemModel *model() const { return chatScene() ? chatScene()->model() : 0; }
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 virtual inline bool haveLayout() const { return layout() != 0; }
52 virtual void clearLayout();
53 virtual QTextLayout *createLayout(QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft);
54 virtual void updateLayout();
55 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
57 virtual QVariant data(int role) const;
60 qreal setGeometry(qreal width, qreal height = -1);
62 // selection stuff, to be called by the scene
63 void clearSelection();
64 void setFullSelection();
65 void continueSelecting(const QPointF &pos);
67 QList<QRectF> findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive);
70 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
71 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
72 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
74 virtual inline QTextLayout *layout() const { return _layout; }
75 virtual inline void setLayout(QTextLayout *l) { _layout = l; }
76 qint16 posToCursor(const QPointF &pos);
78 virtual qreal computeHeight();
83 // internal selection stuff
84 void setSelection(int start, int length);
86 QFontMetricsF *_fontMetrics;
88 enum SelectionMode { NoSelection, PartialSelection, FullSelection };
89 SelectionMode _selectionMode;
90 qint16 _selectionStart, _selectionEnd;
95 /*************************************************************************************************/
97 //! A ChatItem for the timestamp column
98 class TimestampChatItem : public ChatItem {
101 TimestampChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent) {}
102 inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
106 /*************************************************************************************************/
108 //! A ChatItem for the sender column
109 class SenderChatItem : public ChatItem {
112 SenderChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent) {}
113 inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
115 virtual void updateLayout();
118 /*************************************************************************************************/
120 //! A ChatItem for the contents column
121 class ContentsChatItem : public ChatItem {
124 ContentsChatItem(QAbstractItemModel *model, QGraphicsItem *parent);
125 virtual ~ContentsChatItem();
127 inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
129 virtual void clearLayout();
130 virtual void updateLayout();
131 virtual inline bool haveLayout() const { return _layoutData != 0 && layout() != 0; }
134 virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
135 virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
136 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
137 virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
139 virtual inline QTextLayout *layout() const;
140 virtual void setLayout(QTextLayout *l);
144 class WrapColumnFinder;
146 qreal computeHeight();
148 LayoutData *_layoutData;
151 struct ContentsChatItem::LayoutData {
154 LayoutData() { layout = 0; }
155 ~LayoutData() { delete layout; }
158 class ContentsChatItem::WrapColumnFinder {
160 WrapColumnFinder(ChatItem *parent);
163 qint16 nextWrapColumn();
169 ChatLineModel::WrapList wrapList;
176 /*************************************************************************************************/
178 // Avoid circular include deps
179 #include "chatline.h"
180 int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }
181 QTextLayout *ContentsChatItem::layout() const { return _layoutData->layout; }