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) : QSortFilterProxyModel(model) {
27 setSourceModel(model);
28 setSortRole(BufferTreeModel::BufferNameRole);
29 setSortCaseSensitivity(Qt::CaseInsensitive);
34 connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidateMe()));
35 connect(model, SIGNAL(selectionChanged(const QModelIndex &)),
36 this, SLOT(select(const QModelIndex &)));
38 connect(this, SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
39 model, SLOT(changeCurrent(const QModelIndex &, const QModelIndex &)));
41 connect(this, SIGNAL(doubleClicked(const QModelIndex &)),
42 model, SLOT(doubleClickReceived(const QModelIndex &)));
45 void BufferViewFilter::invalidateMe() {
49 void BufferViewFilter::select(const QModelIndex &index) {
50 emit selectionChanged(mapFromSource(index));
53 void BufferViewFilter::changeCurrent(const QModelIndex ¤t, const QModelIndex &previous) {
54 emit currentChanged(mapToSource(current), mapToSource(previous));
57 void BufferViewFilter::doubleClickReceived(const QModelIndex &clicked) {
58 emit doubleClicked(mapToSource(clicked));
61 void BufferViewFilter::dropEvent(QDropEvent *event) {
62 const QMimeData *data = event->mimeData();
63 if(!(mode & FullCustom))
64 return; // only custom buffers can be customized... obviously... :)
66 if(!(data->hasFormat("application/Quassel/BufferItem/row")
67 && data->hasFormat("application/Quassel/BufferItem/network")
68 && data->hasFormat("application/Quassel/BufferItem/bufferId")))
69 return; // whatever the drop is... it's not a buffer...
72 uint bufferuid = data->data("application/Quassel/BufferItem/bufferId").toUInt();
73 QString networkname = QString::fromUtf8("application/Quassel/BufferItem/network");
75 for(int rowid = 0; rowid < rowCount(); rowid++) {
76 QModelIndex networkindex = index(rowid, 0);
77 if(networkindex.data(Qt::DisplayRole) == networkname) {
82 beginInsertRows(QModelIndex(), rowCount(), rowCount());
88 void BufferViewFilter::addBuffer(const uint &bufferuid) {
89 if(!customBuffers.contains(bufferuid)) {
90 customBuffers << bufferuid;
95 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
96 if(!(mode & FullCustom))
97 return; // only custom buffers can be customized... obviously... :)
99 if(index.parent() == QModelIndex())
100 return; // only child elements can be deleted
102 uint bufferuid = index.data(BufferTreeModel::BufferIdRole).toUInt();
103 if(customBuffers.contains(bufferuid)) {
104 beginRemoveRows(index.parent(), index.row(), index.row());
105 customBuffers.removeAt(customBuffers.indexOf(bufferuid));
113 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
114 Buffer::Type bufferType = (Buffer::Type) source_bufferIndex.data(BufferTreeModel::BufferTypeRole).toInt();
115 if((mode & NoChannels) && bufferType == Buffer::ChannelBuffer) return false;
116 if((mode & NoQueries) && bufferType == Buffer::QueryBuffer) return false;
117 if((mode & NoServers) && bufferType == Buffer::ServerBuffer) return false;
119 bool isActive = source_bufferIndex.data(BufferTreeModel::BufferActiveRole).toBool();
120 if((mode & NoActive) && isActive) return false;
121 if((mode & NoInactive) && !isActive) return false;
123 if((mode & FullCustom)) {
124 uint bufferuid = source_bufferIndex.data(BufferTreeModel::BufferIdRole).toUInt();
125 if(!customBuffers.contains(bufferuid))
132 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
133 QString net = source_index.data(Qt::DisplayRole).toString();
134 if((mode & SomeNets) && !networks.contains(net)) {
136 } else if(mode & FullCustom) {
137 // let's check if we got a child that want's to show off
138 int childcount = sourceModel()->rowCount(source_index);
139 for(int rowid = 0; rowid < childcount; rowid++) {
140 QModelIndex child = sourceModel()->index(rowid, 0, source_index);
141 uint bufferuid = child.data(BufferTreeModel::BufferIdRole).toUInt();
142 if(customBuffers.contains(bufferuid))
151 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
152 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
154 if(!child.isValid()) {
155 qDebug() << "filterAcceptsRow has been called with an invalid Child";
159 if(source_parent == QModelIndex())
160 return filterAcceptNetwork(child);
162 return filterAcceptBuffer(child);
165 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) {
166 // pretty interesting stuff here, eh?
167 return QSortFilterProxyModel::lessThan(left, right);