Switch some dirty hacking to using real infrastructure. A Chatline now contains three...
[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 #include "abstractbuffercontainer.h"
27 #include "types.h"
28
29 class ChatLineOld;
30 class AbstractUiMsg;
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, public AbstractChatView {
44   Q_OBJECT
45
46   public:
47     ChatWidget(BufferId, QWidget *parent = 0);
48     ~ChatWidget();
49     void init(BufferId id);
50
51     virtual QSize minimumSizeHint() const;
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(ChatLineOld *);
61     void appendChatLine(ChatLineOld *);
62     void prependChatLines(QList<ChatLineOld *>);
63     void appendChatLines(QList<ChatLineOld *>);
64     void setContents(const QList<AbstractUiMsg *> &);
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     void viewportChanged(int newPos);
81
82   private:
83     BufferId bufferId;
84     enum SelectionMode { NoSelection, TextSelected, LinesSelected };
85     enum MouseMode { Normal, Pressed, DragTsSep, DragTextSep, MarkText, MarkLines };
86     enum MousePos { None, OverTsSep, OverTextSep, OverUrl };
87     MouseMode mouseMode;
88     MousePos mousePos;
89     QPoint dragStartPos;
90     MouseMode dragStartMode;
91     int dragStartLine;
92     int dragStartCursor;
93     int curCursor;
94     int curLine;
95     SelectionMode selectionMode;
96     int selectionStart, selectionEnd, selectionLine;
97
98     int bottomLine, bottomLineOffset;
99
100     QList<ChatLineOld *> lines;
101     QList<qreal> ycoords;
102     QTimer *scrollTimer;
103     QPoint pointerPosition;
104
105     int senderX;
106     int textX;
107     int tsWidth;
108     int senderWidth;
109     int textWidth;
110     int tsGrabPos;     ///< X-Position for changing the timestamp width
111     int senderGrabPos;
112     void computePositions();
113
114     int width;
115     qreal height;
116     qreal y;
117
118     void adjustScrollBar();
119
120     int yToLineIdx(qreal y);
121     void clearSelection();
122     QString selectionToString();
123     void handleMouseMoveEvent(const QPoint &pos);
124
125     MsgId lastBacklogOffset;
126     int lastBacklogSize;
127 };
128
129 #endif