16418242889136c501896ac3528c55514da5a03c
[quassel.git] / gui / chatwidget.h
1 /***************************************************************************
2  *   Copyright (C) 2005/06 by The Quassel Team                             *
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) any later version.                                   *
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 _CHATWIDGET_H_
22 #define _CHATWIDGET_H_
23
24 #include "style.h"
25 #include "message.h"
26 #include "buffer.h"
27 #include <QtCore>
28 #include <QtGui>
29
30 class ChatLine;
31
32 //!\brief Scroll area showing part of the chat messages for a given buffer.
33 /** The contents of the scroll area, i.e. a widget of type ChatWidgetContents,
34  * needs to be provided by calling init(). We don't create this widget ourselves, because
35  * while a ChatWidget will be destroyed and recreated quite often (for example when switching
36  * buffers), there ususally is no need to re-render its content every time (which can be time-consuming).
37  * Before a ChatWidget is destroyed, it gives up its ownership of its contents, referring responsibility
38  * back to where it came from.
39  *
40  * Because we use this as a custom widget in Qt Designer, we cannot use a constructor that takes custom
41  * parameters. Instead, it is mandatory to call init() before using this widget.
42  */
43 class ChatWidget : public QAbstractScrollArea {
44   Q_OBJECT
45
46   public:
47     ChatWidget(QWidget *parent = 0);
48     ~ChatWidget();
49     void init(QString net, QString buf);
50
51     virtual QSize sizeHint() const;
52
53   public slots:
54     void clear();
55     void prependChatLines(QList<ChatLine *>);
56     void appendMsg(Message);
57     void appendMsgList(QList<Message> *);
58
59   protected:
60     virtual void resizeEvent(QResizeEvent *event);
61     virtual void paintEvent(QPaintEvent * event);
62     virtual void mousePressEvent(QMouseEvent *event);
63     virtual void mouseReleaseEvent(QMouseEvent *event);
64     virtual void mouseMoveEvent(QMouseEvent *event);
65     virtual void mouseDoubleClickEvent(QMouseEvent *event);
66
67   private slots:
68     void layout();
69     void scrollBarAction(int);
70     void scrollBarValChanged(int);
71     void ensureVisible(int line);
72     void handleScrollTimer();
73
74   private:
75     QString networkName, bufferName;
76     enum SelectionMode { NoSelection, TextSelected, LinesSelected };
77     enum MouseMode { Normal, Pressed, DragTsSep, DragTextSep, MarkText, MarkLines };
78     enum MousePos { None, OverTsSep, OverTextSep, OverUrl };
79     MouseMode mouseMode;
80     MousePos mousePos;
81     QPoint dragStartPos;
82     MouseMode dragStartMode;
83     int dragStartLine;
84     int dragStartCursor;
85     int curCursor;
86     int curLine;
87     SelectionMode selectionMode;
88     int selectionStart, selectionEnd, selectionLine;
89
90     int bottomLine, bottomLineOffset;
91
92     QList<ChatLine *> lines;
93     QList<qreal> ycoords;
94     QTimer *scrollTimer;
95     QPoint pointerPosition;
96
97     int senderX;
98     int textX;
99     int tsWidth;
100     int senderWidth;
101     int textWidth;
102     int tsGrabPos;     ///< X-Position for changing the timestamp width
103     int senderGrabPos;
104     void computePositions();
105
106     int width;
107     qreal height;
108     qreal y;
109
110     void adjustScrollBar();
111
112     int yToLineIdx(qreal y);
113     void clearSelection();
114     QString selectionToString();
115     void handleMouseMoveEvent(const QPoint &pos);
116
117 };
118
119 //FIXME: chatline doku
120 //!\brief Containing the layout and providing the rendering of a single message.
121 /** A ChatLine takes a Message object,
122  * formats it (by turning the various message types into a human-readable form and afterwards pumping it through
123  * our Style engine), and stores it as a number of QTextLayouts representing the three fields of a chat line
124  * (timestamp, sender and text). These layouts already include any rendering information such as font,
125  * color, or selected characters. By calling layout(), they can be quickly layouted to fit a given set of field widths.
126  * Afterwards, they can quickly be painted whenever necessary.
127  *
128  * By separating the complex and slow task of interpreting and formatting Message objects (which happens exactly once
129  * per message) from the actual layouting and painting, we gain a lot of speed compared to the standard Qt rendering
130  * functions.
131  */
132 class ChatLine : public QObject {
133   Q_OBJECT
134
135   public:
136     ChatLine(Message message, QString networkName, QString bufferName);
137     ~ChatLine();
138
139     qreal layout(qreal tsWidth, qreal nickWidth, qreal textWidth);
140     qreal height() { return hght; }
141     int posToCursor(QPointF pos);
142     void draw(QPainter *p, const QPointF &pos);
143
144     enum SelectionMode { None, Partial, Full };
145     void setSelection(SelectionMode, int start = 0, int end = 0);
146     QDateTime getTimeStamp();
147     QString getSender();
148     QString getText();
149
150     bool isUrl(int pos);
151     QUrl getUrl(int pos);
152
153   public slots:
154
155   private:
156     qreal hght;
157     Message msg;
158     QString networkName, bufferName;
159     qreal tsWidth, senderWidth, textWidth;
160     Style::FormattedString tsFormatted, senderFormatted, textFormatted;
161
162     struct FormatRange {
163       int start;
164       int length;
165       int height;
166       QTextCharFormat format;
167     };
168     struct Word {
169       int start;
170       int length;
171       int trailing;
172       int height;
173     };
174     struct LineLayout {
175       int y;
176       int height;
177       int start;
178       int length;
179     };
180     QVector<int> charPos;
181     QVector<int> charWidths;
182     QVector<int> charHeights;
183     QVector<int> charUrlIdx;
184     QList<FormatRange> tsFormat, senderFormat, textFormat;
185     QList<Word> words;
186     QList<LineLayout> lineLayouts;
187     int minHeight;
188
189     SelectionMode selectionMode;
190     int selectionStart, selectionEnd;
191     void formatMsg(Message);
192     void precomputeLine();
193     QList<FormatRange> calcFormatRanges(const Style::FormattedString &, QTextLayout::FormatRange additional = QTextLayout::FormatRange());
194 };
195
196 struct LayoutTask {
197   QList<Message> messages;
198   Buffer *buffer;
199   QString net, buf;
200   QList<ChatLine *> lines;
201 };
202
203 Q_DECLARE_METATYPE(LayoutTask);
204
205 class LayoutThread : public QThread {
206   Q_OBJECT
207
208   public:
209     LayoutThread();
210     virtual ~LayoutThread();
211     virtual void run();
212
213   public:
214     void processTask(LayoutTask task);
215
216   signals:
217     void taskProcessed(LayoutTask task);
218
219   private:
220     QList<LayoutTask> queue;
221     QMutex mutex;
222     QWaitCondition condition;
223     bool abort;
224
225 };
226
227 #endif