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