code cleanup
[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 "buffermodel.h"
26 #include "client.h"
27 #include "networkmodel.h"
28
29 #include "uisettings.h"
30
31 /*****************************************
32 * The Filter for the Tree View
33 *****************************************/
34 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, BufferViewConfig *config)
35   : QSortFilterProxyModel(model),
36     _config(0)
37 {
38   setConfig(config);
39   setSourceModel(model);
40   connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(source_rowsInserted(const QModelIndex &, int, int)));
41                         
42   setDynamicSortFilter(true);
43 }
44
45 void BufferViewFilter::setConfig(BufferViewConfig *config) {
46   if(_config == config)
47     return;
48   
49   if(_config) {
50     disconnect(_config, 0, this, 0);
51   }
52
53   _config = config;
54   if(config) {
55     connect(config, SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(invalidate()));
56     connect(config, SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(invalidate()));
57     connect(config, SIGNAL(addNewBuffersAutomaticallySet(bool)), this, SLOT(invalidate()));
58     connect(config, SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(invalidate()));
59     connect(config, SIGNAL(hideInactiveBuffersSet(bool)), this, SLOT(invalidate()));
60     connect(config, SIGNAL(allowedBufferTypesSet(int)), this, SLOT(invalidate()));
61     connect(config, SIGNAL(minimumActivitySet(int)), this, SLOT(invalidate()));
62     connect(config, SIGNAL(bufferListSet()), this, SLOT(invalidate()));
63     connect(config, SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(invalidate()));
64     connect(config, SIGNAL(bufferMoved(const BufferId &, int)), this, SLOT(invalidate()));
65     connect(config, SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(invalidate()));
66   }
67   invalidate();
68 }
69
70 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
71   Qt::ItemFlags flags = mapToSource(index).flags();
72   if(_config && index == QModelIndex() || index.parent() == QModelIndex())
73     flags |= Qt::ItemIsDropEnabled;
74   return flags;
75 }
76
77 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
78   if(!config() || !NetworkModel::mimeContainsBufferList(data))
79     return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
80
81   NetworkId droppedNetworkId;
82   if(parent.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
83     droppedNetworkId = parent.data(NetworkModel::NetworkIdRole).value<NetworkId>();
84
85   QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
86   BufferId bufferId;
87   NetworkId networkId;
88   int pos;
89   for(int i = 0; i < bufferList.count(); i++) {
90     networkId = bufferList[i].first;
91     bufferId = bufferList[i].second;
92     if(droppedNetworkId == networkId) {
93       if(row < 0)
94         row = 0;
95       if(row < rowCount(parent)) {
96         BufferId beforeBufferId = parent.child(row, 0).data(NetworkModel::BufferIdRole).value<BufferId>();
97         pos = config()->bufferList().indexOf(beforeBufferId);
98       } else {
99         pos = config()->bufferList().count();
100       }
101
102       if(config()->bufferList().contains(bufferId)) {
103         if(config()->bufferList().indexOf(bufferId) < pos)
104           pos--;
105         config()->requestMoveBuffer(bufferId, pos);
106       } else {
107         config()->requestAddBuffer(bufferId, pos);
108       }
109
110     } else {
111       addBuffer(bufferId);
112     }
113   }
114   return true;
115 }
116
117 void BufferViewFilter::addBuffer(const BufferId &bufferId) {
118   if(!config() || config()->bufferList().contains(bufferId))
119     return;
120   
121   int pos = config()->bufferList().count();
122   bool lt;
123   for(int i = 0; i < config()->bufferList().count(); i++) {
124     if(config() && config()->sortAlphabetically())
125       lt = bufferIdLessThan(bufferId, config()->bufferList()[i]);
126     else
127       lt = bufferId < config()->bufferList()[i];
128     
129     if(lt) {
130       pos = i;
131       break;
132     }
133   }
134   config()->requestAddBuffer(bufferId, pos);
135 }
136
137 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
138   if(!config())
139     return;
140   
141   BufferId bufferId = data(index, NetworkModel::BufferIdRole).value<BufferId>();
142   config()->requestRemoveBuffer(bufferId);
143 }
144
145
146 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
147   if(!_config)
148     return true;
149
150   if(config()->networkId().isValid() && config()->networkId() != sourceModel()->data(source_bufferIndex, NetworkModel::NetworkIdRole).value<NetworkId>())
151     return false;
152
153   if(!(_config->allowedBufferTypes() & (BufferInfo::Type)source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt()))
154     return false;
155
156   if(_config->hideInactiveBuffers() && !source_bufferIndex.data(NetworkModel::ItemActiveRole).toBool())
157     return false;
158
159   if(_config->minimumActivity() > source_bufferIndex.data(NetworkModel::BufferActivityRole).toInt()) {
160     if(!Client::bufferModel()->standardSelectionModel()->isSelected(source_bufferIndex))
161       return false;
162   }
163
164   BufferId bufferId = sourceModel()->data(source_bufferIndex, NetworkModel::BufferIdRole).value<BufferId>();
165   return _config->bufferList().contains(bufferId);
166 }
167
168 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
169   if(!config())
170     return true;
171
172   if(!config()->networkId().isValid()) {
173     return true;
174   } else {
175     return config()->networkId() == sourceModel()->data(source_index, NetworkModel::NetworkIdRole).value<NetworkId>();
176   }
177 }
178
179 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
180   QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
181   
182   if(!child.isValid()) {
183     qWarning() << "filterAcceptsRow has been called with an invalid Child";
184     return false;
185   }
186
187   if(source_parent == QModelIndex())
188     return filterAcceptNetwork(child);
189   else
190     return filterAcceptBuffer(child);
191 }
192
193 bool BufferViewFilter::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
194   int itemType = sourceModel()->data(source_left, NetworkModel::ItemTypeRole).toInt();
195   switch(itemType) {
196   case NetworkModel::NetworkItemType:
197     return networkLessThan(source_left, source_right);
198   case NetworkModel::BufferItemType:
199     return bufferLessThan(source_left, source_right);
200   default:
201     return QSortFilterProxyModel::lessThan(source_left, source_right);    
202   }
203 }
204
205 bool BufferViewFilter::bufferLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
206   BufferId leftBufferId = sourceModel()->data(source_left, NetworkModel::BufferIdRole).value<BufferId>();
207   BufferId rightBufferId = sourceModel()->data(source_right, NetworkModel::BufferIdRole).value<BufferId>();
208   if(config()) {
209     return config()->bufferList().indexOf(leftBufferId) < config()->bufferList().indexOf(rightBufferId);
210   } else
211     return bufferIdLessThan(leftBufferId, rightBufferId);
212 }
213
214 bool BufferViewFilter::networkLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
215   NetworkId leftNetworkId = sourceModel()->data(source_left, NetworkModel::NetworkIdRole).value<NetworkId>();
216   NetworkId rightNetworkId = sourceModel()->data(source_right, NetworkModel::NetworkIdRole).value<NetworkId>();
217
218   if(config() && config()->sortAlphabetically())
219     return QSortFilterProxyModel::lessThan(source_left, source_right);
220   else
221     return leftNetworkId < rightNetworkId;
222 }
223
224 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
225   if(role == Qt::ForegroundRole)
226     return foreground(index);
227   else
228     return QSortFilterProxyModel::data(index, role);
229 }
230
231 QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
232   UiSettings s("QtUi/Colors");
233   QVariant inactiveActivity = s.value("inactiveActivityFG", QVariant(QColor(Qt::gray)));
234   QVariant noActivity = s.value("noActivityFG", QVariant(QColor(Qt::black)));
235   QVariant highlightActivity = s.value("highlightActivityFG", QVariant(QColor(Qt::magenta)));
236   QVariant newMessageActivity = s.value("newMessageActivityFG", QVariant(QColor(Qt::green)));
237   QVariant otherActivity = s.value("otherActivityFG", QVariant(QColor(Qt::darkGreen)));
238
239   if(!index.data(NetworkModel::ItemActiveRole).toBool())
240     return inactiveActivity;
241
242   Buffer::ActivityLevel activity = (Buffer::ActivityLevel)index.data(NetworkModel::BufferActivityRole).toInt();
243
244   if(activity & Buffer::Highlight)
245     return highlightActivity;
246   if(activity & Buffer::NewMessage)
247     return newMessageActivity;
248   if(activity & Buffer::OtherActivity)
249     return otherActivity;
250
251   return noActivity;
252
253 }
254
255 void BufferViewFilter::source_rowsInserted(const QModelIndex &parent, int start, int end) {
256   if(parent.data(NetworkModel::ItemTypeRole) != NetworkModel::BufferItemType)
257     return;
258
259   if(!config() || !config()->addNewBuffersAutomatically())
260     return;
261
262   QModelIndex child;
263   for(int row = start; row <= end; row++) {
264     child = sourceModel()->index(row, 0, parent);
265     addBuffer(sourceModel()->data(child, NetworkModel::BufferIdRole).value<BufferId>());
266   }
267 }
268
269 // ******************************
270 //  Helper
271 // ******************************
272 bool bufferIdLessThan(const BufferId &left, const BufferId &right) {
273   Q_CHECK_PTR(Client::networkModel());
274   if(!Client::networkModel())
275     return true;
276   
277   QModelIndex leftIndex = Client::networkModel()->bufferIndex(left);
278   QModelIndex rightIndex = Client::networkModel()->bufferIndex(right);
279
280   int leftType = Client::networkModel()->data(leftIndex, NetworkModel::BufferTypeRole).toInt();
281   int rightType = Client::networkModel()->data(rightIndex, NetworkModel::BufferTypeRole).toInt();
282
283   if(leftType != rightType)
284     return leftType < rightType;
285   else
286     return QString::compare(Client::networkModel()->data(leftIndex, Qt::DisplayRole).toString(), Client::networkModel()->data(rightIndex, Qt::DisplayRole).toString(), Qt::CaseInsensitive) < 0;
287 }
288