1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel Team *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "bufferviewfilter.h"
23 /*****************************************
24 * The Filter for the Tree View
25 *****************************************/
26 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, const Modes &filtermode, const QStringList &nets)
27 : QSortFilterProxyModel(model),
31 setSourceModel(model);
32 setSortRole(BufferTreeModel::BufferNameRole);
33 setSortCaseSensitivity(Qt::CaseInsensitive);
35 connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidateMe()));
38 void BufferViewFilter::invalidateMe() {
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... :)
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...
53 uint bufferuid = data->data("application/Quassel/BufferItem/bufferInfo").toUInt();
54 QString networkname = QString::fromUtf8("application/Quassel/BufferItem/network");
56 for(int rowid = 0; rowid < rowCount(); rowid++) {
57 QModelIndex networkindex = index(rowid, 0);
58 if(networkindex.data(Qt::DisplayRole) == networkname) {
63 beginInsertRows(QModelIndex(), rowCount(), rowCount());
69 void BufferViewFilter::addBuffer(const uint &bufferuid) {
70 if(!customBuffers.contains(bufferuid)) {
71 customBuffers << bufferuid;
76 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
77 if(!(mode & FullCustom))
78 return; // only custom buffers can be customized... obviously... :)
80 if(index.parent() == QModelIndex())
81 return; // only child elements can be deleted
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));
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;
100 bool isActive = source_bufferIndex.data(BufferTreeModel::BufferActiveRole).toBool();
101 if((mode & NoActive) && isActive) return false;
102 if((mode & NoInactive) && !isActive) return false;
104 if((mode & FullCustom)) {
105 uint bufferuid = source_bufferIndex.data(BufferTreeModel::BufferUidRole).toUInt();
106 if(!customBuffers.contains(bufferuid))
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)) {
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))
132 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
133 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
135 if(!child.isValid()) {
136 qDebug() << "filterAcceptsRow has been called with an invalid Child";
140 if(source_parent == QModelIndex())
141 return filterAcceptNetwork(child);
143 return filterAcceptBuffer(child);
146 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) {
147 // pretty interesting stuff here, eh?
148 return QSortFilterProxyModel::lessThan(left, right);