some minor tweaks
[quassel.git] / src / qtgui / 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   connect(this, SIGNAL(doubleClicked(const QModelIndex &)),
42           model, SLOT(doubleClickReceived(const QModelIndex &)));
43 }
44
45 void BufferViewFilter::invalidateMe() {
46   invalidateFilter();
47 }
48
49 void BufferViewFilter::select(const QModelIndex &index) {
50   emit selectionChanged(mapFromSource(index));
51 }
52
53 void BufferViewFilter::changeCurrent(const QModelIndex &current, const QModelIndex &previous) {
54   emit currentChanged(mapToSource(current), mapToSource(previous));
55 }
56
57 void BufferViewFilter::doubleClickReceived(const QModelIndex &clicked) {
58   emit doubleClicked(mapToSource(clicked));
59 }
60
61 /*
62 void BufferViewFilter::enterDrag() {
63   connect(sourceModel(), SIGNAL(addBuffer(const uint &, const QString &)),
64           this, SLOT(addBuffer(const uint &, const QString &)));
65 }
66
67 void BufferViewFilter::leaveDrag() {
68   disconnect(sourceModel(), SIGNAL(addBuffer(const uint &, const QString &)),
69              this, SLOT(addBuffer(const uint &, const QString &)));
70 }
71 */
72
73 void BufferViewFilter::dropEvent(QDropEvent *event) {
74   const QMimeData *data = event->mimeData();
75   if(!(data->hasFormat("application/Quassel/BufferItem/row")
76        && data->hasFormat("application/Quassel/BufferItem/network")
77        && data->hasFormat("application/Quassel/BufferItem/bufferId")))
78     return; // whatever the drop is... it's not a buffer...
79   
80   event->accept();
81   uint bufferid = data->data("application/Quassel/BufferItem/bufferId").toUInt();
82   QString network = QString::fromUtf8(data->data("application/Quassel/BufferItem/network"));
83   addBuffer(bufferid, network);
84 }
85
86
87 void BufferViewFilter::addBuffer(const uint &bufferuid, const QString &network) {
88   if(!networks.contains(network)) {
89     networks << network;
90   }
91   
92   if(!customBuffers.contains(bufferuid)) {
93     customBuffers << bufferuid;
94     invalidateFilter();
95   }
96   
97 }
98
99 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
100   Buffer::Type bufferType = (Buffer::Type) source_bufferIndex.data(BufferTreeModel::BufferTypeRole).toInt();
101   if((mode & NoChannels) && bufferType == Buffer::ChannelBuffer) return false;
102   if((mode & NoQueries) && bufferType == Buffer::QueryBuffer) return false;
103   if((mode & NoServers) && bufferType == Buffer::ServerBuffer) return false;
104
105   bool isActive = source_bufferIndex.data(BufferTreeModel::BufferActiveRole).toBool();
106   if((mode & NoActive) && isActive) return false;
107   if((mode & NoInactive) && !isActive) return false;
108
109   if((mode & FullCustom)) {
110     uint bufferuid = source_bufferIndex.data(BufferTreeModel::BufferIdRole).toUInt();
111     if(!customBuffers.contains(bufferuid))
112       return false;
113   }
114     
115   return true;
116 }
117
118 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
119   QString net = source_index.data(Qt::DisplayRole).toString();
120   if((mode & SomeNets) && !networks.contains(net))
121     return false;
122   else
123     return true;
124 }
125
126 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
127   QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
128   
129   if(!child.isValid()) {
130     qDebug() << "filterAcceptsRow has been called with an invalid Child";
131     return false;
132   }
133
134   if(source_parent == QModelIndex())
135     return filterAcceptNetwork(child);
136   else
137     return filterAcceptBuffer(child);
138 }
139
140 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) {
141   return QSortFilterProxyModel::lessThan(left, right);
142 }
143