1 /***************************************************************************
2 * Copyright (C) 2005/06 by The Quassel Team *
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) any later version. *
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 ***************************************************************************/
21 #ifndef _CHATWIDGET_H_
22 #define _CHATWIDGET_H_
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.
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.
43 class ChatWidget : public QAbstractScrollArea {
47 ChatWidget(QWidget *parent = 0);
49 void init(QString net, QString buf);
51 virtual QSize sizeHint() const;
55 void prependChatLines(QList<ChatLine *>);
56 void appendMsg(Message);
57 void appendMsgList(QList<Message> *);
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);
69 void scrollBarAction(int);
70 void scrollBarValChanged(int);
71 void ensureVisible(int line);
72 void handleScrollTimer();
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 };
82 MouseMode dragStartMode;
87 SelectionMode selectionMode;
88 int selectionStart, selectionEnd, selectionLine;
90 int bottomLine, bottomLineOffset;
92 QList<ChatLine *> lines;
95 QPoint pointerPosition;
102 int tsGrabPos; ///< X-Position for changing the timestamp width
104 void computePositions();
110 void adjustScrollBar();
112 int yToLineIdx(qreal y);
113 void clearSelection();
114 QString selectionToString();
115 void handleMouseMoveEvent(const QPoint &pos);
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.
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
132 class ChatLine : public QObject {
136 ChatLine(Message message, QString networkName, QString bufferName);
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);
144 enum SelectionMode { None, Partial, Full };
145 void setSelection(SelectionMode, int start = 0, int end = 0);
146 QDateTime getTimeStamp();
151 QUrl getUrl(int pos);
158 QString networkName, bufferName;
159 qreal tsWidth, senderWidth, textWidth;
160 Style::FormattedString tsFormatted, senderFormatted, textFormatted;
166 QTextCharFormat format;
180 QVector<int> charPos;
181 QVector<int> charWidths;
182 QVector<int> charHeights;
183 QVector<int> charUrlIdx;
184 QList<FormatRange> tsFormat, senderFormat, textFormat;
186 QList<LineLayout> lineLayouts;
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());
197 QList<Message> messages;
200 QList<ChatLine *> lines;
203 Q_DECLARE_METATYPE(LayoutTask);
205 class LayoutThread : public QThread {
210 virtual ~LayoutThread();
214 void processTask(LayoutTask task);
217 void taskProcessed(LayoutTask task);
220 QList<LayoutTask> queue;
222 QWaitCondition condition;