Fixed formatting code %O (which should switch off all format codes, but accidentally
[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 prependChatLine(ChatLine *);
56     void appendChatLine(ChatLine *);
57     void prependChatLines(QList<ChatLine *>);
58     void appendChatLines(QList<ChatLine *>);
59     void setContents(QList<ChatLine *>);
60
61   protected:
62     virtual void resizeEvent(QResizeEvent *event);
63     virtual void paintEvent(QPaintEvent * event);
64     virtual void mousePressEvent(QMouseEvent *event);
65     virtual void mouseReleaseEvent(QMouseEvent *event);
66     virtual void mouseMoveEvent(QMouseEvent *event);
67     virtual void mouseDoubleClickEvent(QMouseEvent *event);
68
69   private slots:
70     void layout();
71     void scrollBarAction(int);
72     void scrollBarValChanged(int);
73     void ensureVisible(int line);
74     void handleScrollTimer();
75
76   private:
77     QString networkName, bufferName;
78     enum SelectionMode { NoSelection, TextSelected, LinesSelected };
79     enum MouseMode { Normal, Pressed, DragTsSep, DragTextSep, MarkText, MarkLines };
80     enum MousePos { None, OverTsSep, OverTextSep, OverUrl };
81     MouseMode mouseMode;
82     MousePos mousePos;
83     QPoint dragStartPos;
84     MouseMode dragStartMode;
85     int dragStartLine;
86     int dragStartCursor;
87     int curCursor;
88     int curLine;
89     SelectionMode selectionMode;
90     int selectionStart, selectionEnd, selectionLine;
91
92     int bottomLine, bottomLineOffset;
93
94     QList<ChatLine *> lines;
95     QList<qreal> ycoords;
96     QTimer *scrollTimer;
97     QPoint pointerPosition;
98
99     int senderX;
100     int textX;
101     int tsWidth;
102     int senderWidth;
103     int textWidth;
104     int tsGrabPos;     ///< X-Position for changing the timestamp width
105     int senderGrabPos;
106     void computePositions();
107
108     int width;
109     qreal height;
110     qreal y;
111
112     void adjustScrollBar();
113
114     int yToLineIdx(qreal y);
115     void clearSelection();
116     QString selectionToString();
117     void handleMouseMoveEvent(const QPoint &pos);
118
119 };
120
121 //FIXME: chatline doku
122 //!\brief Containing the layout and providing the rendering of a single message.
123 /** A ChatLine takes a Message object,
124  * formats it (by turning the various message types into a human-readable form and afterwards pumping it through
125  * our Style engine), and stores it as a number of QTextLayouts representing the three fields of a chat line
126  * (timestamp, sender and text). These layouts already include any rendering information such as font,
127  * color, or selected characters. By calling layout(), they can be quickly layouted to fit a given set of field widths.
128  * Afterwards, they can quickly be painted whenever necessary.
129  *
130  * By separating the complex and slow task of interpreting and formatting Message objects (which happens exactly once
131  * per message) from the actual layouting and painting, we gain a lot of speed compared to the standard Qt rendering
132  * functions.
133  */
134 class ChatLine : public QObject {
135   Q_OBJECT
136
137   public:
138     ChatLine(Message message);
139     ~ChatLine();
140
141     qreal layout(qreal tsWidth, qreal nickWidth, qreal textWidth);
142     qreal height() { return hght; }
143     int posToCursor(QPointF pos);
144     void draw(QPainter *p, const QPointF &pos);
145
146     enum SelectionMode { None, Partial, Full };
147     void setSelection(SelectionMode, int start = 0, int end = 0);
148     QDateTime timeStamp();
149     QString sender();
150     QString text();
151     uint msgId();
152     BufferId bufferId();
153
154     bool isUrl(int pos);
155     QUrl getUrl(int pos);
156
157   public slots:
158
159   private:
160     qreal hght;
161     Message msg;
162     qreal tsWidth, senderWidth, textWidth;
163     Style::FormattedString tsFormatted, senderFormatted, textFormatted;
164
165     struct FormatRange {
166       int start;
167       int length;
168       int height;
169       QTextCharFormat format;
170     };
171     struct Word {
172       int start;
173       int length;
174       int trailing;
175       int height;
176     };
177     struct LineLayout {
178       int y;
179       int height;
180       int start;
181       int length;
182     };
183     QVector<int> charPos;
184     QVector<int> charWidths;
185     QVector<int> charHeights;
186     QVector<int> charUrlIdx;
187     QList<FormatRange> tsFormat, senderFormat, textFormat;
188     QList<Word> words;
189     QList<LineLayout> lineLayouts;
190     int minHeight;
191
192     SelectionMode selectionMode;
193     int selectionStart, selectionEnd;
194     void formatMsg(Message);
195     void precomputeLine();
196     QList<FormatRange> calcFormatRanges(const Style::FormattedString &, QTextLayout::FormatRange additional = QTextLayout::FormatRange());
197 };
198
199 /*
200 struct LayoutTask {
201   QList<Message> messages;
202   Buffer *buffer;
203   QString net, buf;
204   QList<ChatLine *> lines;
205 };
206
207 Q_DECLARE_METATYPE(LayoutTask);
208
209 class LayoutThread : public QThread {
210   Q_OBJECT
211
212   public:
213     LayoutThread();
214     virtual ~LayoutThread();
215     virtual void run();
216
217   public:
218     void processTask(LayoutTask task);
219
220   signals:
221     void taskProcessed(LayoutTask task);
222
223   private:
224     QList<LayoutTask> queue;
225     QMutex mutex;
226     QWaitCondition condition;
227     bool abort;
228
229 };
230 */
231
232 #endif