6bcfb6adbb9cec7af815d593a53f3e9651a89b12
[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 QStringList &nets) : QSortFilterProxyModel(model) {
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 }
36
37 void BufferViewFilter::invalidateMe() {
38   invalidateFilter();
39 }
40
41 void BufferViewFilter::dropEvent(QDropEvent *event) {
42   const QMimeData *data = event->mimeData();
43   if(!(mode & FullCustom))
44     return; // only custom buffers can be customized... obviously... :)
45   
46   if(!(data->hasFormat("application/Quassel/BufferItem/row")
47        && data->hasFormat("application/Quassel/BufferItem/network")
48        && data->hasFormat("application/Quassel/BufferItem/bufferInfo")))
49     return; // whatever the drop is... it's not a buffer...
50   
51   event->accept();
52   uint bufferuid = data->data("application/Quassel/BufferItem/bufferInfo").toUInt();
53   QString networkname = QString::fromUtf8("application/Quassel/BufferItem/network");
54   
55   for(int rowid = 0; rowid < rowCount(); rowid++) {
56     QModelIndex networkindex = index(rowid, 0);
57     if(networkindex.data(Qt::DisplayRole) == networkname) {
58       addBuffer(bufferuid);
59       return;
60     }
61   }
62   beginInsertRows(QModelIndex(), rowCount(), rowCount());
63   addBuffer(bufferuid);
64   endInsertRows();
65 }
66
67
68 void BufferViewFilter::addBuffer(const uint &bufferuid) {
69   if(!customBuffers.contains(bufferuid)) {
70     customBuffers << bufferuid;
71     invalidateFilter();
72   }
73 }
74
75 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
76   if(!(mode & FullCustom))
77     return; // only custom buffers can be customized... obviously... :)
78   
79   if(index.parent() == QModelIndex())
80     return; // only child elements can be deleted
81   
82   uint bufferuid = index.data(BufferTreeModel::BufferUidRole).toUInt();
83   if(customBuffers.contains(bufferuid)) {
84     beginRemoveRows(index.parent(), index.row(), index.row());
85     customBuffers.removeAt(customBuffers.indexOf(bufferuid));
86     endRemoveRows();
87     invalidateFilter();
88   }
89   
90 }
91
92
93 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
94   Buffer::Type bufferType = (Buffer::Type) source_bufferIndex.data(BufferTreeModel::BufferTypeRole).toInt();
95   if((mode & NoChannels) && bufferType == Buffer::ChannelType) return false;
96   if((mode & NoQueries) && bufferType == Buffer::QueryType) return false;
97   if((mode & NoServers) && bufferType == Buffer::StatusType) return false;
98
99   bool isActive = source_bufferIndex.data(BufferTreeModel::BufferActiveRole).toBool();
100   if((mode & NoActive) && isActive) return false;
101   if((mode & NoInactive) && !isActive) return false;
102
103   if((mode & FullCustom)) {
104     uint bufferuid = source_bufferIndex.data(BufferTreeModel::BufferUidRole).toUInt();
105     if(!customBuffers.contains(bufferuid))
106       return false;
107   }
108     
109   return true;
110 }
111
112 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
113   QString net = source_index.data(Qt::DisplayRole).toString();
114   if((mode & SomeNets) && !networks.contains(net)) {
115     return false;
116   } else if(mode & FullCustom) {
117     // let's check if we got a child that want's to show off
118     int childcount = sourceModel()->rowCount(source_index);
119     for(int rowid = 0; rowid < childcount; rowid++) {
120       QModelIndex child = sourceModel()->index(rowid, 0, source_index);
121       uint bufferuid = child.data(BufferTreeModel::BufferUidRole).toUInt();
122       if(customBuffers.contains(bufferuid))
123         return true;
124     }
125     return false;
126   } else {
127     return true;
128   }
129 }
130
131 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
132   QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
133   
134   if(!child.isValid()) {
135     qDebug() << "filterAcceptsRow has been called with an invalid Child";
136     return false;
137   }
138
139   if(source_parent == QModelIndex())
140     return filterAcceptNetwork(child);
141   else
142     return filterAcceptBuffer(child);
143 }
144
145 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) {
146   // pretty interesting stuff here, eh?
147   return QSortFilterProxyModel::lessThan(left, right);
148 }
149