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