f15be23f1cf23fd85388c20ff73d60b0a26f23c3
[quassel.git] / src / qtgui / 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 class AbstractUiMsg;
32
33 //!\brief Scroll area showing part of the chat messages for a given buffer.
34 /** The contents of the scroll area, i.e. a widget of type ChatWidgetContents,
35  * needs to be provided by calling init(). We don't create this widget ourselves, because
36  * while a ChatWidget will be destroyed and recreated quite often (for example when switching
37  * buffers), there ususally is no need to re-render its content every time (which can be time-consuming).
38  * Before a ChatWidget is destroyed, it gives up its ownership of its contents, referring responsibility
39  * back to where it came from.
40  *
41  * Because we use this as a custom widget in Qt Designer, we cannot use a constructor that takes custom
42  * parameters. Instead, it is mandatory to call init() before using this widget.
43  */
44 class ChatWidget : public QAbstractScrollArea {
45   Q_OBJECT
46
47   public:
48     ChatWidget(QWidget *parent = 0);
49     ~ChatWidget();
50     void init(QString net, QString buf);
51
52     virtual QSize sizeHint() const;
53
54   public slots:
55     void clear();
56
57     void prependMsg(AbstractUiMsg *);
58     void appendMsg(AbstractUiMsg *);
59
60     void prependChatLine(ChatLine *);
61     void appendChatLine(ChatLine *);
62     void prependChatLines(QList<ChatLine *>);
63     void appendChatLines(QList<ChatLine *>);
64     void setContents(QList<ChatLine *>);
65
66   protected:
67     virtual void resizeEvent(QResizeEvent *event);
68     virtual void paintEvent(QPaintEvent * event);
69     virtual void mousePressEvent(QMouseEvent *event);
70     virtual void mouseReleaseEvent(QMouseEvent *event);
71     virtual void mouseMoveEvent(QMouseEvent *event);
72     virtual void mouseDoubleClickEvent(QMouseEvent *event);
73
74   private slots:
75     void layout();
76     void scrollBarAction(int);
77     void scrollBarValChanged(int);
78     void ensureVisible(int line);
79     void handleScrollTimer();
80
81   private:
82     QString networkName, bufferName;
83     enum SelectionMode { NoSelection, TextSelected, LinesSelected };
84     enum MouseMode { Normal, Pressed, DragTsSep, DragTextSep, MarkText, MarkLines };
85     enum MousePos { None, OverTsSep, OverTextSep, OverUrl };
86     MouseMode mouseMode;
87     MousePos mousePos;
88     QPoint dragStartPos;
89     MouseMode dragStartMode;
90     int dragStartLine;
91     int dragStartCursor;
92     int curCursor;
93     int curLine;
94     SelectionMode selectionMode;
95     int selectionStart, selectionEnd, selectionLine;
96
97     int bottomLine, bottomLineOffset;
98
99     QList<ChatLine *> lines;
100     QList<qreal> ycoords;
101     QTimer *scrollTimer;
102     QPoint pointerPosition;
103
104     int senderX;
105     int textX;
106     int tsWidth;
107     int senderWidth;
108     int textWidth;
109     int tsGrabPos;     ///< X-Position for changing the timestamp width
110     int senderGrabPos;
111     void computePositions();
112
113     int width;
114     qreal height;
115     qreal y;
116
117     void adjustScrollBar();
118
119     int yToLineIdx(qreal y);
120     void clearSelection();
121     QString selectionToString();
122     void handleMouseMoveEvent(const QPoint &pos);
123
124 };
125
126 #endif