Fix expanding networks in Chat Monitor settings
[quassel.git] / src / uisupport / bufferview.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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     void selectFirstBuffer();
63
64 public slots:
65     void setRootIndexForNetworkId(const NetworkId &networkId);
66     void removeSelectedBuffers(bool permanently = false);
67     void menuActionTriggered(QAction *);
68     void nextBuffer();
69     void previousBuffer();
70     void hideCurrentBuffer();
71     void filterTextChanged(QString filterString);
72
73 signals:
74     void removeBuffer(const QModelIndex &);
75     void removeBufferPermanently(const QModelIndex &);
76
77 protected:
78     virtual void keyPressEvent(QKeyEvent *);
79     virtual void dropEvent(QDropEvent *event);
80     virtual void rowsInserted(const QModelIndex &parent, int start, int end);
81     virtual void wheelEvent(QWheelEvent *);
82     virtual QSize sizeHint() const;
83     virtual void focusInEvent(QFocusEvent *event) { QAbstractScrollArea::focusInEvent(event); }
84     virtual void contextMenuEvent(QContextMenuEvent *event);
85
86 #if QT_VERSION < 0x050000
87     virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
88 #else
89     virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
90 #endif
91
92 private slots:
93     void joinChannel(const QModelIndex &index);
94     void toggleHeader(bool checked);
95
96     /**
97      * Expand all active networks and collapse inactive ones unless manually changed
98      *
99      * Applies to all networks.  Shouldn't need called except during initialization.
100      */
101     void setExpandedState();
102
103     /**
104      * Save the current display state of the given network
105      *
106      * Tracks expanded or collapsed and active or inactive.
107      *
108      * @see setExpandedState()
109      * @param[in] networkIdx QModelIndex of the root network to store
110      */
111     void storeExpandedState(const QModelIndex &networkIdx);
112
113     /**
114      * Set the display state of the given network according to network status and manual changes
115      *
116      * Expands if active or previously expanded, collapses if inactive or previously collapsed.
117      *
118      * @see storeExpandedState()
119      * @param[in] networkIdx QModelIndex of the root network to update
120      */
121     void setExpandedState(const QModelIndex &networkIdx);
122
123     void on_configChanged();
124     void on_layoutChanged();
125
126     void changeBuffer(Direction direction);
127
128 private:
129     QPointer<BufferViewConfig> _config;
130
131     enum ExpandedState {
132         WasExpanded = 0x01,
133         WasActive = 0x02
134     };
135     QHash<NetworkId, short> _expandedState;
136 };
137
138
139 // ******************************
140 //  BufferViewDelgate
141 // ******************************
142
143 class BufferViewDelegate : public QStyledItemDelegate
144 {
145     Q_OBJECT
146
147 public:
148     BufferViewDelegate(QObject *parent = 0);
149     bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
150
151 protected:
152     virtual void customEvent(QEvent *event);
153 };
154
155
156 // ==============================
157 //  BufferView Dock
158 // ==============================
159 class BufferViewDock : public QDockWidget
160 {
161     Q_OBJECT
162     Q_PROPERTY(bool active READ isActive WRITE setActive STORED true)
163
164 public :
165         BufferViewDock(BufferViewConfig *config, QWidget *parent);
166
167     int bufferViewId() const;
168     BufferViewConfig *config() const;
169     inline BufferView *bufferView() const { return qobject_cast<BufferView *>(widget()); }
170     inline bool isActive() const { return _active; }
171     void setWidget(QWidget *newWidget);
172     QWidget *widget() const { return _childWidget; }
173
174     void activateFilter();
175
176 public slots:
177     void setActive(bool active = true);
178
179 protected slots:
180     virtual bool eventFilter(QObject *object, QEvent *event);
181     virtual void focusInEvent(QFocusEvent*event) { qDebug() << event; }
182
183 private slots:
184     void bufferViewRenamed(const QString &newName);
185     void updateTitle();
186     void configChanged();
187     void onFilterReturnPressed();
188
189 private:
190     QWidget *_childWidget;
191     QWidget *_widget;
192     QPointer<QWidget> _oldFocusItem; // QPointer in case the old item gets deleted
193     QLineEdit *_filterEdit;
194     bool _active;
195     QString _title;
196 };
197
198
199 #endif