Search the web with selected text.
[quassel.git] / src / uisupport / bufferview.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2014 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 <QTreeView>
30 #include <QPointer>
31
32 #include "actioncollection.h"
33 #include "bufferviewconfig.h"
34 #include "networkmodel.h"
35 #include "types.h"
36
37 /*****************************************
38  * The TreeView showing the Buffers
39  *****************************************/
40 class BufferView : public QTreeView
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     virtual void setSelectionModel(QItemSelectionModel *selectionModel);
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 public slots:
64     void setRootIndexForNetworkId(const NetworkId &networkId);
65     void removeSelectedBuffers(bool permanently = false);
66     void menuActionTriggered(QAction *);
67     void nextBuffer();
68     void previousBuffer();
69     void hideCurrentBuffer();
70
71 signals:
72     void removeBuffer(const QModelIndex &);
73     void removeBufferPermanently(const QModelIndex &);
74
75 protected:
76     virtual void keyPressEvent(QKeyEvent *);
77     virtual void dropEvent(QDropEvent *event);
78     virtual void rowsInserted(const QModelIndex &parent, int start, int end);
79     virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
80     virtual void wheelEvent(QWheelEvent *);
81     virtual QSize sizeHint() const;
82     virtual void focusInEvent(QFocusEvent *event) { QAbstractScrollArea::focusInEvent(event); }
83     virtual void contextMenuEvent(QContextMenuEvent *event);
84
85 private slots:
86     void joinChannel(const QModelIndex &index);
87     void toggleHeader(bool checked);
88
89     void storeExpandedState(const QModelIndex &networkIdx);
90     void setExpandedState(const QModelIndex &networkIdx);
91
92     void on_configChanged();
93     void on_layoutChanged();
94
95     void changeBuffer(Direction direction);
96
97 private:
98     QPointer<BufferViewConfig> _config;
99
100     enum ExpandedState {
101         WasExpanded = 0x01,
102         WasActive = 0x02
103     };
104     QHash<NetworkId, short> _expandedState;
105 };
106
107
108 // ******************************
109 //  BufferViewDelgate
110 // ******************************
111
112 class BufferViewDelegate : public QStyledItemDelegate
113 {
114     Q_OBJECT
115
116 public:
117     BufferViewDelegate(QObject *parent = 0);
118     bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
119
120 protected:
121     virtual void customEvent(QEvent *event);
122 };
123
124
125 // ==============================
126 //  BufferView Dock
127 // ==============================
128 class BufferViewDock : public QDockWidget
129 {
130     Q_OBJECT
131     Q_PROPERTY(bool active READ isActive WRITE setActive STORED true)
132
133 public :
134         BufferViewDock(BufferViewConfig *config, QWidget *parent);
135
136     int bufferViewId() const;
137     BufferViewConfig *config() const;
138     inline BufferView *bufferView() const { return qobject_cast<BufferView *>(widget()); }
139     inline bool isActive() const { return _active; }
140
141 public slots:
142     void setActive(bool active = true);
143
144 private slots:
145     void bufferViewRenamed(const QString &newName);
146     void updateTitle();
147
148 private:
149
150     bool _active;
151     QString _title;
152 };
153
154
155 #endif