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