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