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