Semi-yearly copyright bump
[quassel.git] / src / uisupport / bufferview.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #ifndef BUFFERVIEW_H_
22 #define BUFFERVIEW_H_
23
24 #include <QAction>
25 #include <QMenu>
26 #include <QDockWidget>
27 #include <QModelIndex>
28 #include <QStyledItemDelegate>
29 #include <QPointer>
30
31 #include "actioncollection.h"
32 #include "bufferviewconfig.h"
33 #include "networkmodel.h"
34 #include "treeviewtouch.h"
35 #include "types.h"
36
37 /*****************************************
38  * The TreeView showing the Buffers
39  *****************************************/
40 class BufferView : public TreeViewTouch
41 {
42     Q_OBJECT
43
44 public:
45     enum Direction {
46         Forward = 1,
47         Backward = -1
48     };
49
50     BufferView(QWidget *parent = 0);
51     void init();
52
53     void setModel(QAbstractItemModel *model);
54     void setFilteredModel(QAbstractItemModel *model, BufferViewConfig *config);
55
56     void setConfig(BufferViewConfig *config);
57     inline BufferViewConfig *config() { return _config; }
58
59     void addActionsToMenu(QMenu *menu, const QModelIndex &index);
60     void addFilterActions(QMenu *contextMenu, const QModelIndex &index);
61
62 public slots:
63     void setRootIndexForNetworkId(const NetworkId &networkId);
64     void removeSelectedBuffers(bool permanently = false);
65     void menuActionTriggered(QAction *);
66     void nextBuffer();
67     void previousBuffer();
68     void hideCurrentBuffer();
69
70 signals:
71     void removeBuffer(const QModelIndex &);
72     void removeBufferPermanently(const QModelIndex &);
73
74 protected:
75     virtual void keyPressEvent(QKeyEvent *);
76     virtual void dropEvent(QDropEvent *event);
77     virtual void rowsInserted(const QModelIndex &parent, int start, int end);
78     virtual void wheelEvent(QWheelEvent *);
79     virtual QSize sizeHint() const;
80     virtual void focusInEvent(QFocusEvent *event) { QAbstractScrollArea::focusInEvent(event); }
81     virtual void contextMenuEvent(QContextMenuEvent *event);
82
83 #if QT_VERSION < 0x050000
84     virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
85 #else
86     virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
87 #endif
88
89 private slots:
90     void joinChannel(const QModelIndex &index);
91     void toggleHeader(bool checked);
92
93     /**
94      * Expand all active networks and collapse inactive ones unless manually changed
95      *
96      * Applies to all networks.  Shouldn't need called except during initialization.
97      */
98     void setExpandedState();
99
100     /**
101      * Save the current display state of the given network
102      *
103      * Tracks expanded or collapsed and active or inactive.
104      *
105      * @see setExpandedState()
106      * @param[in] networkIdx QModelIndex of the root network to store
107      */
108     void storeExpandedState(const QModelIndex &networkIdx);
109
110     /**
111      * Set the display state of the given network according to network status and manual changes
112      *
113      * Expands if active or previously expanded, collapses if inactive or previously collapsed.
114      *
115      * @see storeExpandedState()
116      * @param[in] networkIdx QModelIndex of the root network to update
117      */
118     void setExpandedState(const QModelIndex &networkIdx);
119
120     void on_configChanged();
121     void on_layoutChanged();
122
123     void changeBuffer(Direction direction);
124
125 private:
126     QPointer<BufferViewConfig> _config;
127
128     enum ExpandedState {
129         WasExpanded = 0x01,
130         WasActive = 0x02
131     };
132     QHash<NetworkId, short> _expandedState;
133 };
134
135
136 // ******************************
137 //  BufferViewDelgate
138 // ******************************
139
140 class BufferViewDelegate : public QStyledItemDelegate
141 {
142     Q_OBJECT
143
144 public:
145     BufferViewDelegate(QObject *parent = 0);
146     bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
147
148 protected:
149     virtual void customEvent(QEvent *event);
150 };
151
152
153 // ==============================
154 //  BufferView Dock
155 // ==============================
156 class BufferViewDock : public QDockWidget
157 {
158     Q_OBJECT
159     Q_PROPERTY(bool active READ isActive WRITE setActive STORED true)
160
161 public :
162         BufferViewDock(BufferViewConfig *config, QWidget *parent);
163
164     int bufferViewId() const;
165     BufferViewConfig *config() const;
166     inline BufferView *bufferView() const { return qobject_cast<BufferView *>(widget()); }
167     inline bool isActive() const { return _active; }
168
169 public slots:
170     void setActive(bool active = true);
171
172 private slots:
173     void bufferViewRenamed(const QString &newName);
174     void updateTitle();
175
176 private:
177
178     bool _active;
179     QString _title;
180 };
181
182
183 #endif