Prevent coreinfodlg from being too tall
[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 struct ChatItemPrivate;
34
35 class ChatItem : public QGraphicsItem {
36 protected:
37   ChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent);
38   virtual ~ChatItem();
39
40 public:
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()); }
45
46   inline QRectF boundingRect() const { return _boundingRect; }
47   inline qreal width() const { return _boundingRect.width(); }
48   inline qreal height() const { return _boundingRect.height(); }
49
50   QTextLayout *createLayout(QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft) const;
51   virtual void doLayout();
52   void clearLayout();
53
54   virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
55   enum { Type = ChatScene::ChatItemType };
56   virtual inline int type() const { return Type; }
57
58   QVariant data(int role) const;
59
60   // selection stuff, to be called by the scene
61   void clearSelection();
62   void setFullSelection();
63   void continueSelecting(const QPointF &pos);
64
65   QList<QRectF> findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive);
66
67 protected:
68   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
69   virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
70   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
71
72   inline QTextLayout *layout() const;
73
74   virtual QTextLayout::FormatRange selectionFormat() const;
75   virtual inline QVector<QTextLayout::FormatRange> additionalFormats() const { return QVector<QTextLayout::FormatRange>(); }
76
77   qint16 posToCursor(const QPointF &pos);
78
79   inline bool hasPrivateData() const { return (bool)_data; }
80   ChatItemPrivate *privateData() const;
81   virtual inline ChatItemPrivate *newPrivateData();
82
83   // WARNING: setGeometry and setHeight should not be used without either:
84   //  a) calling prepareGeometryChange() immediately before setColumns()
85   //  b) calling Chatline::setPos() immediately afterwards
86   inline void setGeometry(qreal width, qreal height) {
87     _boundingRect.setWidth(width);
88     _boundingRect.setHeight(height);
89   }
90   inline void setHeight(const qreal &height) {
91     _boundingRect.setHeight(height);
92   }
93   inline void setWidth(const qreal &width) {
94     _boundingRect.setWidth(width);
95   }
96
97 private:
98   // internal selection stuff
99   void setSelection(int start, int length);
100
101   ChatItemPrivate *_data;
102   QRectF _boundingRect;
103
104   enum SelectionMode { NoSelection, PartialSelection, FullSelection };
105   SelectionMode _selectionMode;
106   qint16 _selectionStart, _selectionEnd;
107
108   friend class ChatLine;
109 };
110
111 struct ChatItemPrivate {
112   QTextLayout *layout;
113   ChatItemPrivate(QTextLayout *l) : layout(l) {}
114   ~ChatItemPrivate() {
115     delete layout;
116   }
117 };
118
119 // inlines of ChatItem
120 QTextLayout *ChatItem::layout() const { return privateData()->layout; }
121 ChatItemPrivate *ChatItem::newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::WrapAnywhere)); }
122
123 // ************************************************************
124 // TimestampChatItem
125 // ************************************************************
126
127 //! A ChatItem for the timestamp column
128 class TimestampChatItem : public ChatItem {
129 public:
130   TimestampChatItem(const qreal &width, const qreal &height, QGraphicsItem *parent) : ChatItem(width, height, QPointF(0, 0), parent) {}
131   enum { Type = ChatScene::TimestampChatItemType };
132   virtual inline int type() const { return Type; }
133   virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
134 };
135
136 // ************************************************************
137 // SenderChatItem
138 // ************************************************************
139 //! A ChatItem for the sender column
140 class SenderChatItem : public ChatItem {
141 public:
142   SenderChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent) : ChatItem(width, height, pos, parent) {}
143   virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
144
145 protected:
146   virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
147   enum { Type = ChatScene::SenderChatItemType };
148   virtual inline int type() const { return Type; }
149   virtual inline ChatItemPrivate *newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::ManualWrap, Qt::AlignRight)); }
150 };
151
152 // ************************************************************
153 // ContentsChatItem
154 // ************************************************************
155 struct ContentsChatItemPrivate;
156
157 //! A ChatItem for the contents column
158 class ContentsChatItem : public ChatItem {
159 public:
160   ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent);
161
162   enum { Type = ChatScene::ContentsChatItemType };
163   virtual inline int type() const { return Type; }
164
165   inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
166   inline QFontMetricsF *fontMetrics() const { return _fontMetrics; }
167
168 protected:
169   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
170   virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
171   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
172   virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
173   virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
174   virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
175
176
177
178   virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
179
180   virtual void doLayout();
181   virtual inline ChatItemPrivate *newPrivateData();
182
183 private:
184   struct Clickable;
185   class WrapColumnFinder;
186
187   inline ContentsChatItemPrivate *privateData() const;
188
189   QList<Clickable> findClickables() const;
190   void endHoverMode();
191   void showWebPreview(const Clickable &click);
192   void clearWebPreview();
193
194   qreal setGeometryByWidth(qreal w);
195   friend class ChatLine;
196   friend struct ContentsChatItemPrivate;
197
198   QFontMetricsF *_fontMetrics;
199 };
200
201 struct ContentsChatItem::Clickable {
202   // Don't change these enums without also changing the regexps in analyze()!
203   enum Type {
204     Invalid = -1,
205     Url = 0,
206     Channel = 1,
207     Nick = 2
208   };
209
210   Type type;
211   quint16 start;
212   quint16 length;
213
214   inline Clickable() : type(Invalid) {};
215   inline Clickable(Type type_, quint16 start_, quint16 length_) : type(type_), start(start_), length(length_) {};
216   inline bool isValid() const { return type != Invalid; }
217 };
218
219 struct ContentsChatItemPrivate : ChatItemPrivate {
220   ContentsChatItem *contentsItem;
221   QList<ContentsChatItem::Clickable> clickables;
222   ContentsChatItem::Clickable currentClickable;
223   bool hasDragged;
224
225   ContentsChatItemPrivate(QTextLayout *l, const QList<ContentsChatItem::Clickable> &c, ContentsChatItem *parent)
226   : ChatItemPrivate(l), contentsItem(parent), clickables(c), hasDragged(false) {}
227 };
228
229 //inlines regarding ContentsChatItemPrivate
230 ChatItemPrivate *ContentsChatItem::newPrivateData() { return new ContentsChatItemPrivate(createLayout(QTextOption::WrapAnywhere), findClickables(), this); }
231 ContentsChatItemPrivate *ContentsChatItem::privateData() const { return (ContentsChatItemPrivate *)ChatItem::privateData(); }
232
233 class ContentsChatItem::WrapColumnFinder {
234 public:
235   WrapColumnFinder(ChatItem *parent);
236   ~WrapColumnFinder();
237
238   qint16 nextWrapColumn();
239
240 private:
241   ChatItem *item;
242   QTextLayout *layout;
243   QTextLine line;
244   ChatLineModel::WrapList wrapList;
245   qint16 wordidx;
246   qint16 lineCount;
247   qreal choppedTrailing;
248 };
249
250 /*************************************************************************************************/
251
252 // Avoid circular include deps
253 #include "chatline.h"
254 const QAbstractItemModel *ChatItem::model() const { return static_cast<ChatLine *>(parentItem())->model(); }
255 int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }
256
257 #endif