fixed a bug preventing quasel from crashing on exit
[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   setSortRole(BufferTreeModel::BufferNameRole);
29   setSortCaseSensitivity(Qt::CaseInsensitive);
30     
31   mode = filtermode;
32   networks = nets;
33   
34   connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidateMe()));
35   connect(model, SIGNAL(updateSelection(const QModelIndex &, QItemSelectionModel::SelectionFlags)), this, SLOT(select(const QModelIndex &, QItemSelectionModel::SelectionFlags)));
36     
37   connect(this, SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), model, SLOT(changeCurrent(const QModelIndex &, const QModelIndex &)));
38   connect(this, SIGNAL(doubleClicked(const QModelIndex &)), model, SLOT(doubleClickReceived(const QModelIndex &)));
39 }
40
41 void BufferViewFilter::invalidateMe() {
42   invalidateFilter();
43 }
44
45 void BufferViewFilter::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
46   emit updateSelection(mapFromSource(index), command);
47 }
48
49 void BufferViewFilter::changeCurrent(const QModelIndex &current, const QModelIndex &previous) {
50   emit currentChanged(mapToSource(current), mapToSource(previous));
51 }
52
53 void BufferViewFilter::doubleClickReceived(const QModelIndex &clicked) {
54   emit doubleClicked(mapToSource(clicked));
55 }
56
57 void BufferViewFilter::enterDrag() {
58   connect(sourceModel(), SIGNAL(addBuffer(const uint &, const QString &)),
59           this, SLOT(addBuffer(const uint &, const QString &)));
60 }
61
62 void BufferViewFilter::leaveDrag() {
63   disconnect(sourceModel(), SIGNAL(addBuffer(const uint &, const QString &)),
64              this, SLOT(addBuffer(const uint &, const QString &)));
65 }
66
67 void BufferViewFilter::addBuffer(const uint &bufferuid, const QString &network) {
68   if(!networks.contains(network)) {
69     networks << network;
70   }
71   
72   if(!customBuffers.contains(bufferuid)) {
73     customBuffers << bufferuid;
74     invalidateFilter();
75   }
76   
77 }
78
79 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
80   Buffer::Type bufferType = (Buffer::Type) source_bufferIndex.data(BufferTreeModel::BufferTypeRole).toInt();
81   if((mode & NoChannels) && bufferType == Buffer::ChannelBuffer) return false;
82   if((mode & NoQueries) && bufferType == Buffer::QueryBuffer) return false;
83   if((mode & NoServers) && bufferType == Buffer::ServerBuffer) return false;
84
85   bool isActive = source_bufferIndex.data(BufferTreeModel::BufferActiveRole).toBool();
86   if((mode & NoActive) && isActive) return false;
87   if((mode & NoInactive) && !isActive) return false;
88
89   if((mode & FullCustom)) {
90     uint bufferuid = source_bufferIndex.data(BufferTreeModel::BufferIdRole).toUInt();
91     if(!customBuffers.contains(bufferuid))
92       return false;
93   }
94     
95   return true;
96 }
97
98 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
99   QString net = source_index.data(Qt::DisplayRole).toString();
100   if((mode & SomeNets) && !networks.contains(net))
101     return false;
102   else
103     return true;
104 }
105
106 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
107   QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
108   
109   if(!child.isValid()) {
110     qDebug() << "filterAcceptsRow has been called with an invalid Child";
111     return false;
112   }
113
114   if(source_parent == QModelIndex())
115     return filterAcceptNetwork(child);
116   else
117     return filterAcceptBuffer(child);
118 }
119
120 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) {
121   return QSortFilterProxyModel::lessThan(left, right);
122 }
123