745f2d32b122dc8471ce6633195e9529097b9a33
[quassel.git] / src / qtui / chatitem.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #ifndef CHATITEM_H_
22 #define CHATITEM_H_
23
24 #include <QGraphicsItem>
25 #include <QObject>
26
27 #include "chatlinemodel.h"
28 #include "chatscene.h"
29 #include "uistyle.h"
30 #include "qtui.h"
31
32 class QTextLayout;
33
34 class ChatItem : public QGraphicsItem {
35
36 protected:
37   ChatItem(ChatLineModel::ColumnType column, QAbstractItemModel *, QGraphicsItem *parent);
38   virtual ~ChatItem();
39
40 public:
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()); }
45
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(); }
50
51   inline bool haveLayout() const { return _layoutData != 0 && layout() != 0; }
52   void clearLayoutData();
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);
56
57   virtual QVariant data(int role) const;
58
59   // returns height
60   qreal setGeometry(qreal width, qreal height = -1);
61
62   // selection stuff, to be called by the scene
63   void clearSelection();
64   void setFullSelection();
65   void continueSelecting(const QPointF &pos);
66
67   QList<QRectF> findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive);
68
69 protected:
70   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
71   virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
72   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
73   virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
74
75   virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
76   virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
77   virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
78
79   struct LayoutData;
80   inline QTextLayout *layout() const;
81   void setLayout(QTextLayout *);
82   qint16 posToCursor(const QPointF &pos);
83
84   virtual qreal computeHeight();
85
86   QRectF _boundingRect;
87
88 private:
89   // internal selection stuff
90   void setSelection(int start, int length);
91
92   QFontMetricsF *_fontMetrics;
93
94   enum SelectionMode { NoSelection, PartialSelection, FullSelection };
95   SelectionMode _selectionMode;
96   qint16 _selectionStart, _selectionEnd;
97
98   LayoutData *_layoutData;
99 };
100
101 struct ChatItem::LayoutData {
102   QTextLayout *layout;
103
104   LayoutData() { layout = 0; }
105   ~LayoutData() { delete layout; }
106 };
107
108 class TimestampChatItem : public ChatItem {
109
110 public:
111   TimestampChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent) {}
112   inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
113
114 };
115
116 class SenderChatItem : public ChatItem {
117
118 public:
119   SenderChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent) {}
120   inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
121
122   void updateLayout();
123 };
124
125 class ContentsChatItem : public ChatItem {
126
127 public:
128   ContentsChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent) {}
129   inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
130
131   void updateLayout();
132
133 private:
134   qreal computeHeight();
135
136   class WrapColumnFinder;
137 };
138
139
140
141 class ContentsChatItem::WrapColumnFinder {
142 public:
143   WrapColumnFinder(ChatItem *parent);
144   ~WrapColumnFinder();
145
146   qint16 nextWrapColumn();
147
148 private:
149   ChatItem *item;
150   QTextLayout *layout;
151   QTextLine line;
152   ChatLineModel::WrapList wrapList;
153   qint16 wordidx;
154   qint16 lastwrapcol;
155   qreal lastwrappos;
156   qreal w;
157 };
158
159 #include "chatline.h"
160 inline int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }
161 inline QTextLayout *ChatItem::layout() const { return _layoutData->layout; }
162
163 #endif