caa1fb382f4f363130927f6fa8bb2e776b82fa93
[quassel.git] / src / qtgui / bufferviewfilter.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 "bufferviewfilter.h"
22
23 /*****************************************
24 * The Filter for the Tree View
25 *****************************************/
26 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, Modes filtermode, QStringList nets, QObject *parent) : QSortFilterProxyModel(parent) {
27   setSourceModel(model);
28   mode = filtermode;
29   networks = nets;
30   
31   connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidateMe()));
32   connect(model, SIGNAL(updateSelection(const QModelIndex &, QItemSelectionModel::SelectionFlags)), this, SLOT(select(const QModelIndex &, QItemSelectionModel::SelectionFlags)));
33     
34   connect(this, SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), model, SLOT(changeCurrent(const QModelIndex &, const QModelIndex &)));
35   connect(this, SIGNAL(doubleClicked(const QModelIndex &)), model, SLOT(doubleClickReceived(const QModelIndex &)));
36 }
37
38 void BufferViewFilter::invalidateMe() {
39   invalidateFilter();
40 }
41
42 void BufferViewFilter::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
43   emit updateSelection(mapFromSource(index), command);
44 }
45
46 void BufferViewFilter::changeCurrent(const QModelIndex &current, const QModelIndex &previous) {
47   emit currentChanged(mapToSource(current), mapToSource(previous));
48 }
49
50 void BufferViewFilter::doubleClickReceived(const QModelIndex &clicked) {
51   emit doubleClicked(mapToSource(clicked));
52 }
53
54 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
55   QModelIndex child = source_parent.child(source_row, 0);
56   if(!child.isValid())
57     return true; // can't imagine this case but true sounds good :)
58
59   Buffer::Type bufferType = (Buffer::Type) child.data(BufferTreeModel::BufferTypeRole).toInt();
60   if((mode & NoChannels) && bufferType == Buffer::ChannelBuffer) return false;
61   if((mode & NoQueries) && bufferType == Buffer::QueryBuffer) return false;
62   if((mode & NoServers) && bufferType == Buffer::ServerBuffer) return false;
63
64   bool isActive = child.data(BufferTreeModel::BufferActiveRole).toBool();
65   if((mode & NoActive) && isActive) return false;
66   if((mode & NoInactive) && !isActive) return false;
67
68   QString net = child.data(Qt::DisplayRole).toString();
69   if((mode & SomeNets) && !networks.contains(net)) return false;
70     
71   return true;
72 }