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