52d4ffa23cc46f0713ffe9d8766c941f30c2c257
[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 #pragma once
22
23 #include "uisupport-export.h"
24
25 #include <QAction>
26 #include <QMenu>
27 #include <QDockWidget>
28 #include <QModelIndex>
29 #include <QStyledItemDelegate>
30 #include <QPointer>
31
32 #include "actioncollection.h"
33 #include "bufferviewconfig.h"
34 #include "networkmodel.h"
35 #include "treeviewtouch.h"
36 #include "types.h"
37
38 /*****************************************
39  * The TreeView showing the Buffers
40  *****************************************/
41 class UISUPPORT_EXPORT BufferView : public TreeViewTouch
42 {
43     Q_OBJECT
44
45 public:
46     enum Direction {
47         Forward = 1,
48         Backward = -1
49     };
50
51     BufferView(QWidget *parent = nullptr);
52     void init();
53
54     void setModel(QAbstractItemModel *model);
55     void setFilteredModel(QAbstractItemModel *model, BufferViewConfig *config);
56
57     void setConfig(BufferViewConfig *config);
58     inline BufferViewConfig *config() { return _config; }
59
60     void addActionsToMenu(QMenu *menu, const QModelIndex &index);
61     void addFilterActions(QMenu *contextMenu, const QModelIndex &index);
62
63     void selectFirstBuffer();
64
65 public slots:
66     void setRootIndexForNetworkId(const NetworkId &networkId);
67     void removeSelectedBuffers(bool permanently = false);
68     void menuActionTriggered(QAction *);
69     void nextBuffer();
70     void previousBuffer();
71     void hideCurrentBuffer();
72     void filterTextChanged(const QString& filterString);
73     void changeHighlight(Direction direction);
74     void selectHighlighted();
75     void clearHighlight();
76
77 signals:
78     void removeBuffer(const QModelIndex &);
79     void removeBufferPermanently(const QModelIndex &);
80
81 protected:
82     virtual void dropEvent(QDropEvent *event);
83     virtual void rowsInserted(const QModelIndex &parent, int start, int end);
84     virtual void wheelEvent(QWheelEvent *);
85     virtual QSize sizeHint() const;
86     virtual void focusInEvent(QFocusEvent *event) { QAbstractScrollArea::focusInEvent(event); }
87     virtual void contextMenuEvent(QContextMenuEvent *event);
88
89     virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
90
91 private slots:
92     void joinChannel(const QModelIndex &index);
93     void toggleHeader(bool checked);
94
95     /**
96      * Expand all active networks and collapse inactive ones unless manually changed
97      *
98      * Applies to all networks.  Shouldn't need called except during initialization.
99      */
100     void setExpandedState();
101
102     /**
103      * Save the current display state of the given network
104      *
105      * Tracks expanded or collapsed and active or inactive.
106      *
107      * @see setExpandedState()
108      * @param[in] networkIdx QModelIndex of the root network to store
109      */
110     void storeExpandedState(const QModelIndex &networkIdx);
111
112     /**
113      * Set the display state of the given network according to network status and manual changes
114      *
115      * Expands if active or previously expanded, collapses if inactive or previously collapsed.
116      *
117      * @see storeExpandedState()
118      * @param[in] networkIdx QModelIndex of the root network to update
119      */
120     void setExpandedState(const QModelIndex &networkIdx);
121
122     void on_configChanged();
123     void on_layoutChanged();
124
125     void changeBuffer(Direction direction);
126
127 private:
128     QPointer<BufferViewConfig> _config;
129
130     enum ExpandedState {
131         WasExpanded = 0x01,
132         WasActive = 0x02
133     };
134     QHash<NetworkId, short> _expandedState;
135     QModelIndex _currentHighlight;
136 };
137
138
139 // ******************************
140 //  BufferViewDelegate
141 // ******************************
142
143 class BufferViewDelegate : public QStyledItemDelegate
144 {
145     Q_OBJECT
146
147 public:
148     BufferViewDelegate(QObject *parent = nullptr);
149     bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override;
150
151     void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
152
153 public:
154     QModelIndex currentHighlight;
155
156 protected:
157     void customEvent(QEvent *event) override;
158 };
159
160
161 // ==============================
162 //  BufferView Dock
163 // ==============================
164 class UISUPPORT_EXPORT BufferViewDock : public QDockWidget
165 {
166     Q_OBJECT
167     Q_PROPERTY(bool active READ isActive WRITE setActive STORED true)
168
169 public :
170         BufferViewDock(BufferViewConfig *config, QWidget *parent);
171
172     int bufferViewId() const;
173     BufferViewConfig *config() const;
174     inline BufferView *bufferView() const { return qobject_cast<BufferView *>(widget()); }
175     inline bool isActive() const { return _active; }
176     void setWidget(QWidget *newWidget);
177     void setLocked(bool locked);
178     QWidget *widget() const { return _childWidget; }
179
180     void activateFilter();
181
182 public slots:
183     void setActive(bool active = true);
184
185 protected slots:
186     virtual bool eventFilter(QObject *object, QEvent *event);
187     virtual void focusInEvent(QFocusEvent*event) { qDebug() << event; }
188
189 private slots:
190     void bufferViewRenamed(const QString &newName);
191     void updateTitle();
192     void configChanged();
193     void onFilterReturnPressed();
194
195 private:
196     QWidget *_childWidget;
197     QWidget *_widget;
198     QPointer<QWidget> _oldFocusItem; // QPointer in case the old item gets deleted
199     QLineEdit *_filterEdit;
200     bool _active;
201     QString _title;
202 };