- MessageTypes are now binary exclusive which allows easy checks with multimple condi...
[quassel.git] / src / uisupport / bufferviewfilter.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
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) version 3.                                           *
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 #include <QColor>
24
25 #include "networkmodel.h"
26
27 /*****************************************
28 * The Filter for the Tree View
29 *****************************************/
30 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, const Modes &filtermode, const QList<NetworkId> &nets)
31   : QSortFilterProxyModel(model),
32     mode(filtermode),
33     networks(QSet<NetworkId>::fromList(nets))
34 {
35   setSourceModel(model);
36   setSortCaseSensitivity(Qt::CaseInsensitive);
37 }
38
39 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
40   Qt::ItemFlags flags = mapToSource(index).flags();
41   if(mode & FullCustom) {
42     if(index == QModelIndex() || index.parent() == QModelIndex())
43       flags |= Qt::ItemIsDropEnabled;
44   }
45   return flags;
46 }
47
48 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
49   // drops have to occur in the open field
50   if(parent != QModelIndex())
51     return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
52
53   if(!NetworkModel::mimeContainsBufferList(data))
54     return false;
55
56   QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
57
58   NetworkId netId;
59   BufferId bufferId;
60   for(int i = 0; i < bufferList.count(); i++) {
61     netId = bufferList[i].first;
62     bufferId = bufferList[i].second;
63     if(!networks.contains(netId)) {
64       networks << netId;
65     }
66     addBuffer(bufferId);
67   }
68   return true;
69 }
70
71 void BufferViewFilter::addBuffer(const BufferId &bufferuid) {
72   if(!buffers.contains(bufferuid)) {
73     buffers << bufferuid;
74     invalidateFilter();
75   }
76 }
77
78 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
79   if(!(mode & FullCustom))
80     return; // only custom buffers can be customized... obviously... :)
81   
82   if(index.parent() == QModelIndex())
83     return; // only child elements can be deleted
84
85   bool lastBuffer = (rowCount(index.parent()) == 1);
86   NetworkId netId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
87   BufferId bufferuid = index.data(NetworkModel::BufferIdRole).value<BufferId>();
88
89   if(buffers.contains(bufferuid)) {
90     buffers.remove(bufferuid);
91     
92     if(lastBuffer) {
93       networks.remove(netId);
94       Q_ASSERT(!networks.contains(netId));
95     }
96
97     invalidateFilter();
98   }
99   
100 }
101
102
103 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
104   BufferInfo::Type bufferType = (BufferInfo::Type) source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt();
105   
106   if((mode & NoChannels) && bufferType == BufferInfo::ChannelBuffer)
107     return false;
108   if((mode & NoQueries) && bufferType == BufferInfo::QueryBuffer)
109     return false;
110   if((mode & NoServers) && bufferType == BufferInfo::StatusBuffer)
111     return false;
112
113 //   bool isActive = source_bufferIndex.data(NetworkModel::BufferActiveRole).toBool();
114 //   if((mode & NoActive) && isActive)
115 //     return false;
116 //   if((mode & NoInactive) && !isActive)
117 //     return false;
118
119   if((mode & FullCustom)) {
120     BufferId bufferuid = source_bufferIndex.data(NetworkModel::BufferIdRole).value<BufferId>();
121     return buffers.contains(bufferuid);
122   }
123     
124   return true;
125 }
126
127 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
128   NetworkId net = source_index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
129   return !((mode & (SomeNets | FullCustom)) && !networks.contains(net));
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) const {
147   int lefttype = left.data(NetworkModel::BufferTypeRole).toInt();
148   int righttype = right.data(NetworkModel::BufferTypeRole).toInt();
149
150   if(lefttype != righttype)
151     return lefttype < righttype;
152   else
153     return QSortFilterProxyModel::lessThan(left, right);
154 }
155
156 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
157   if(role == Qt::ForegroundRole)
158     return foreground(index);
159   else
160     return QSortFilterProxyModel::data(index, role);
161 }
162
163 QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
164   if(!index.data(NetworkModel::ItemActiveRole).toBool())
165     return QColor(Qt::gray);
166
167   BufferItem::ActivityLevel activity = (BufferItem::ActivityLevel)index.data(NetworkModel::BufferActivityRole).toInt();
168
169   if(activity & BufferItem::Highlight)
170     return QColor(Qt::magenta);
171   if(activity & BufferItem::NewMessage)
172     return QColor(Qt::green);
173   if(activity & BufferItem::OtherActivity)
174     return QColor(Qt::darkGreen);
175   
176   return QColor(Qt::black);
177   
178   // FIXME:: make colors configurable;
179
180 }