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