9f0d4a1a4c8080c9f0ad8887af5f10ec0ddbbf34
[quassel.git] / src / uisupport / bufferviewfilter.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC 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) 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  *   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, const Modes &filtermode, const QList<uint> &nets)
27   : QSortFilterProxyModel(model),
28     mode(filtermode),
29     networks(QSet<uint>::fromList(nets))
30 {
31   setSourceModel(model);
32   setSortCaseSensitivity(Qt::CaseInsensitive);
33
34   // FIXME
35   // ok the following basically sucks. therfore it's commented out. Justice served.
36   // a better solution would use dataChanged()
37   
38   // I have this feeling that this resulted in a fuckup once... no clue though right now and invalidateFilter isn't a slot -.-
39   //connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidate()));
40   // connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidateFilter_()));
41 }
42
43 void BufferViewFilter::invalidateFilter_() {
44   QSortFilterProxyModel::invalidateFilter();
45 }
46
47 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
48   Qt::ItemFlags flags = mapToSource(index).flags();
49   if(mode & FullCustom) {
50     if(index == QModelIndex() || index.parent() == QModelIndex())
51       flags |= Qt::ItemIsDropEnabled;
52   }
53   return flags;
54 }
55
56 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
57   // drops have to occur in the open field
58   if(parent != QModelIndex())
59     return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
60
61   if(!NetworkModel::mimeContainsBufferList(data))
62     return false;
63
64   QList< QPair<uint, uint> > bufferList = NetworkModel::mimeDataToBufferList(data);
65
66   uint netId, bufferId;
67   for(int i = 0; i < bufferList.count(); i++) {
68     netId = bufferList[i].first;
69     bufferId = bufferList[i].second;
70     if(!networks.contains(netId)) {
71       networks << netId;
72     }
73     addBuffer(bufferId);
74   }
75   return true;
76 }
77
78 void BufferViewFilter::addBuffer(const uint &bufferuid) {
79   if(!buffers.contains(bufferuid)) {
80     buffers << bufferuid;
81     invalidateFilter();
82   }
83 }
84
85 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
86   if(!(mode & FullCustom))
87     return; // only custom buffers can be customized... obviously... :)
88   
89   if(index.parent() == QModelIndex())
90     return; // only child elements can be deleted
91
92   bool lastBuffer = (rowCount(index.parent()) == 1);
93   uint netId = index.data(NetworkModel::NetworkIdRole).toUInt();
94   uint bufferuid = index.data(NetworkModel::BufferUidRole).toUInt();
95
96   if(buffers.contains(bufferuid)) {
97     buffers.remove(bufferuid);
98     
99     if(lastBuffer) {
100       networks.remove(netId);
101       Q_ASSERT(!networks.contains(netId));
102     }
103
104     invalidateFilter();
105   }
106   
107 }
108
109
110 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
111   Buffer::Type bufferType = (Buffer::Type) source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt();
112   
113   if((mode & NoChannels) && bufferType == Buffer::ChannelType)
114     return false;
115   if((mode & NoQueries) && bufferType == Buffer::QueryType)
116     return false;
117   if((mode & NoServers) && bufferType == Buffer::StatusType)
118     return false;
119
120 //   bool isActive = source_bufferIndex.data(NetworkModel::BufferActiveRole).toBool();
121 //   if((mode & NoActive) && isActive)
122 //     return false;
123 //   if((mode & NoInactive) && !isActive)
124 //     return false;
125
126   if((mode & FullCustom)) {
127     uint bufferuid = source_bufferIndex.data(NetworkModel::BufferUidRole).toUInt();
128     return buffers.contains(bufferuid);
129   }
130     
131   return true;
132 }
133
134 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
135   uint net = source_index.data(NetworkModel::NetworkIdRole).toUInt();
136   return !((mode & (SomeNets | FullCustom)) && !networks.contains(net));
137 }
138
139 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
140   QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
141   
142   if(!child.isValid()) {
143     qDebug() << "filterAcceptsRow has been called with an invalid Child";
144     return false;
145   }
146
147   if(source_parent == QModelIndex())
148     return filterAcceptNetwork(child);
149   else
150     return filterAcceptBuffer(child);
151 }
152
153 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) const {
154   int lefttype = left.data(NetworkModel::BufferTypeRole).toInt();
155   int righttype = right.data(NetworkModel::BufferTypeRole).toInt();
156
157   if(lefttype != righttype)
158     return lefttype < righttype;
159   else
160     return QSortFilterProxyModel::lessThan(left, right);
161 }
162