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