7e2cf2175fd723dc1dda53b644f002e38004eb2a
[quassel.git] / gui / bufferview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
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) any later version.                                   *
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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "bufferview.h"
22 #include "bufferviewwidget.h"
23
24 /*****************************************
25 * The TreeView showing the Buffers
26 *****************************************/
27 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, Modes filtermode, QStringList nets, QObject *parent) : QSortFilterProxyModel(parent) {
28   setSourceModel(model);
29   mode = filtermode;
30   networks = nets;
31   
32   connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidateMe()));
33   connect(model, SIGNAL(updateSelection(const QModelIndex &, QItemSelectionModel::SelectionFlags)), this, SLOT(select(const QModelIndex &, QItemSelectionModel::SelectionFlags)));
34     
35   connect(this, SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), model, SLOT(changeCurrent(const QModelIndex &, const QModelIndex &)));
36   connect(this, SIGNAL(doubleClicked(const QModelIndex &)), model, SLOT(doubleClickReceived(const QModelIndex &)));
37 }
38
39 void BufferViewFilter::invalidateMe() {
40   invalidateFilter();
41 }
42
43 void BufferViewFilter::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
44   emit updateSelection(mapFromSource(index), command);
45 }
46
47 void BufferViewFilter::changeCurrent(const QModelIndex &current, const QModelIndex &previous) {
48   emit currentChanged(mapToSource(current), mapToSource(previous));
49 }
50
51 void BufferViewFilter::doubleClickReceived(const QModelIndex &clicked) {
52   emit doubleClicked(mapToSource(clicked));
53 }
54
55 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
56   QModelIndex child = source_parent.child(source_row, 0);
57   if(!child.isValid())
58     return true; // can't imagine this case but true sounds good :)
59   
60   Buffer::Type bufferType = (Buffer::Type) child.data(BufferTreeModel::BufferTypeRole).toInt();
61   if((mode & NoChannels) && bufferType == Buffer::ChannelBuffer) return false;
62   if((mode & NoQueries) && bufferType == Buffer::QueryBuffer) return false;
63   if((mode & NoServers) && bufferType == Buffer::ServerBuffer) return false;
64
65   bool isActive = child.data(BufferTreeModel::BufferActiveRole).toBool();
66   if((mode & NoActive) && isActive) return false;
67   if((mode & NoInactive) && !isActive) return false;
68
69   QString net = child.data(Qt::DisplayRole).toString();
70   if((mode & SomeNets) && !networks.contains(net)) return false;
71     
72   return true;
73 }
74
75 /*****************************************
76 * The TreeView showing the Buffers
77 *****************************************/
78 BufferView::BufferView(QWidget *parent) : QTreeView(parent) {
79 }
80
81 void BufferView::init() {
82   setIndentation(10);
83   header()->hide();
84   header()->hideSection(1);
85   
86   setDragEnabled(true);
87   setAcceptDrops(true);
88   setDropIndicatorShown(true);
89   
90   connect(selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), model(), SLOT(changeCurrent(const QModelIndex &, const QModelIndex &)));
91   connect(this, SIGNAL(doubleClicked(const QModelIndex &)), model(), SLOT(doubleClickReceived(const QModelIndex &)));
92   connect(model(), SIGNAL(updateSelection(const QModelIndex &, QItemSelectionModel::SelectionFlags)), selectionModel(), SLOT(select(const QModelIndex &, QItemSelectionModel::SelectionFlags)));
93 }
94
95 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QStringList nets) {
96   BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
97   setModel(filter);
98 }
99
100 void BufferView::setModel(QAbstractItemModel *model) {
101   QTreeView::setModel(model);
102   init();
103 }
104
105 void BufferView::dragEnterEvent(QDragEnterEvent *event) {
106   // not yet needed... this will be usefull to keep track of the active view when customizing them with drag and drop
107   QTreeView::dragEnterEvent(event);
108 }
109
110 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
111   setExpanded(parent, true);
112 }