Merging branches/0.2/@r859 with trunk
[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 <QCoreApplication>
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->isInitialized()) {
75     configInitialized();
76   } else {
77     connect(config, SIGNAL(initDone()), this, SLOT(configInitialized()));
78     invalidate();
79   }
80 }
81
82 void BufferViewFilter::configInitialized() {
83   if(!config())
84     return;
85     
86   connect(config(), SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(invalidate()));
87   connect(config(), SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(invalidate()));
88   connect(config(), SIGNAL(addNewBuffersAutomaticallySet(bool)), this, SLOT(invalidate()));
89   connect(config(), SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(invalidate()));
90   connect(config(), SIGNAL(hideInactiveBuffersSet(bool)), this, SLOT(invalidate()));
91   connect(config(), SIGNAL(allowedBufferTypesSet(int)), this, SLOT(invalidate()));
92   connect(config(), SIGNAL(minimumActivitySet(int)), this, SLOT(invalidate()));
93   connect(config(), SIGNAL(bufferListSet()), this, SLOT(invalidate()));
94   connect(config(), SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(invalidate()));
95   connect(config(), SIGNAL(bufferMoved(const BufferId &, int)), this, SLOT(invalidate()));
96   connect(config(), SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(invalidate()));
97
98   disconnect(config(), SIGNAL(initDone()), this, SLOT(configInitialized()));
99
100   invalidate();
101 }
102
103 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
104   Qt::ItemFlags flags = mapToSource(index).flags();
105   if(_config && index == QModelIndex() || index.parent() == QModelIndex())
106     flags |= Qt::ItemIsDropEnabled;
107   return flags;
108 }
109
110 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
111   if(!config() || !NetworkModel::mimeContainsBufferList(data))
112     return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
113
114   NetworkId droppedNetworkId;
115   if(parent.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
116     droppedNetworkId = parent.data(NetworkModel::NetworkIdRole).value<NetworkId>();
117
118   QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
119   BufferId bufferId;
120   NetworkId networkId;
121   int pos;
122   for(int i = 0; i < bufferList.count(); i++) {
123     networkId = bufferList[i].first;
124     bufferId = bufferList[i].second;
125     if(droppedNetworkId == networkId) {
126       if(row < 0)
127         row = 0;
128       if(row < rowCount(parent)) {
129         BufferId beforeBufferId = parent.child(row, 0).data(NetworkModel::BufferIdRole).value<BufferId>();
130         pos = config()->bufferList().indexOf(beforeBufferId);
131       } else {
132         pos = config()->bufferList().count();
133       }
134
135       if(config()->bufferList().contains(bufferId)) {
136         if(config()->bufferList().indexOf(bufferId) < pos)
137           pos--;
138         config()->requestMoveBuffer(bufferId, pos);
139       } else {
140         config()->requestAddBuffer(bufferId, pos);
141       }
142
143     } else {
144       addBuffer(bufferId);
145     }
146   }
147   return true;
148 }
149
150 void BufferViewFilter::addBuffer(const BufferId &bufferId) const {
151   if(!config() || config()->bufferList().contains(bufferId))
152     return;
153   
154   int pos = config()->bufferList().count();
155   bool lt;
156   for(int i = 0; i < config()->bufferList().count(); i++) {
157     if(config() && config()->sortAlphabetically())
158       lt = bufferIdLessThan(bufferId, config()->bufferList()[i]);
159     else
160       lt = bufferId < config()->bufferList()[i];
161     
162     if(lt) {
163       pos = i;
164       break;
165     }
166   }
167   config()->requestAddBuffer(bufferId, pos);
168 }
169
170 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
171   BufferId bufferId = sourceModel()->data(source_bufferIndex, NetworkModel::BufferIdRole).value<BufferId>();
172   Q_ASSERT(bufferId.isValid());
173   if(!config())
174     return true;
175
176   int activityLevel = source_bufferIndex.data(NetworkModel::BufferActivityRole).toInt();
177   if(config()->networkId().isValid() && config()->networkId() != sourceModel()->data(source_bufferIndex, NetworkModel::NetworkIdRole).value<NetworkId>())
178     return false;
179
180   if(!(config()->allowedBufferTypes() & (BufferInfo::Type)source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt()))
181     return false;
182
183   if(config()->hideInactiveBuffers() && !source_bufferIndex.data(NetworkModel::ItemActiveRole).toBool())
184     return false;
185
186   if(config()->minimumActivity() > activityLevel) {
187     if(bufferId != Client::bufferModel()->standardSelectionModel()->currentIndex().data(NetworkModel::BufferIdRole).value<BufferId>())
188       return false;
189   }
190
191   if(config()->bufferList().contains(bufferId))
192     return true;
193
194   if(config()->isInitialized() && !config()->removedBuffers().contains(bufferId) && activityLevel > Buffer::OtherActivity)
195     addBuffer(bufferId);
196
197   return false;
198 }
199
200 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
201   if(!config())
202     return true;
203
204   if(!config()->networkId().isValid()) {
205     return true;
206   } else {
207     return config()->networkId() == sourceModel()->data(source_index, NetworkModel::NetworkIdRole).value<NetworkId>();
208   }
209 }
210
211 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
212   QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
213   
214   if(!child.isValid()) {
215     qWarning() << "filterAcceptsRow has been called with an invalid Child";
216     return false;
217   }
218
219   if(!source_parent.isValid())
220     return filterAcceptNetwork(child);
221   else
222     return filterAcceptBuffer(child);
223 }
224
225 bool BufferViewFilter::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
226   int itemType = sourceModel()->data(source_left, NetworkModel::ItemTypeRole).toInt();
227   switch(itemType) {
228   case NetworkModel::NetworkItemType:
229     return networkLessThan(source_left, source_right);
230   case NetworkModel::BufferItemType:
231     return bufferLessThan(source_left, source_right);
232   default:
233     return QSortFilterProxyModel::lessThan(source_left, source_right);    
234   }
235 }
236
237 bool BufferViewFilter::bufferLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
238   BufferId leftBufferId = sourceModel()->data(source_left, NetworkModel::BufferIdRole).value<BufferId>();
239   BufferId rightBufferId = sourceModel()->data(source_right, NetworkModel::BufferIdRole).value<BufferId>();
240   if(config()) {
241     return config()->bufferList().indexOf(leftBufferId) < config()->bufferList().indexOf(rightBufferId);
242   } else
243     return bufferIdLessThan(leftBufferId, rightBufferId);
244 }
245
246 bool BufferViewFilter::networkLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
247   NetworkId leftNetworkId = sourceModel()->data(source_left, NetworkModel::NetworkIdRole).value<NetworkId>();
248   NetworkId rightNetworkId = sourceModel()->data(source_right, NetworkModel::NetworkIdRole).value<NetworkId>();
249
250   if(config() && config()->sortAlphabetically())
251     return QSortFilterProxyModel::lessThan(source_left, source_right);
252   else
253     return leftNetworkId < rightNetworkId;
254 }
255
256 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
257   if(role == Qt::ForegroundRole)
258     return foreground(index);
259   else
260     return QSortFilterProxyModel::data(index, role);
261 }
262
263 QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
264   if(!index.data(NetworkModel::ItemActiveRole).toBool())
265     return _FgColorInactiveActivity;
266
267   Buffer::ActivityLevel activity = (Buffer::ActivityLevel)index.data(NetworkModel::BufferActivityRole).toInt();
268
269   if(activity & Buffer::Highlight)
270     return _FgColorHighlightActivity;
271   if(activity & Buffer::NewMessage)
272     return _FgColorNewMessageActivity;
273   if(activity & Buffer::OtherActivity)
274     return _FgColorOtherActivity;
275
276   return _FgColorNoActivity;
277 }
278
279 void BufferViewFilter::source_rowsInserted(const QModelIndex &parent, int start, int end) {
280   if(parent.data(NetworkModel::ItemTypeRole) != NetworkModel::BufferItemType)
281     return;
282
283   if(!config() || !config()->addNewBuffersAutomatically())
284     return;
285
286   QModelIndex child;
287   for(int row = start; row <= end; row++) {
288     child = sourceModel()->index(row, 0, parent);
289     addBuffer(sourceModel()->data(child, NetworkModel::BufferIdRole).value<BufferId>());
290   }
291 }
292
293 void BufferViewFilter::checkPreviousCurrentForRemoval(const QModelIndex &current, const QModelIndex &previous) {
294   Q_UNUSED(current);
295   if(previous.isValid())
296     QCoreApplication::postEvent(this, new CheckRemovalEvent(previous));
297 }
298
299 void BufferViewFilter::customEvent(QEvent *event) {
300   if(event->type() != QEvent::User)
301     return;
302   
303   CheckRemovalEvent *removalEvent = static_cast<CheckRemovalEvent *>(event);
304   checkItemForRemoval(removalEvent->index);
305   
306   event->accept();
307 }
308
309 void BufferViewFilter::checkItemsForRemoval(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
310   QModelIndex source_topLeft = mapToSource(topLeft);
311   QModelIndex source_bottomRight = mapToSource(bottomRight);
312   emit _dataChanged(source_topLeft, source_bottomRight);
313 }
314
315 // ******************************
316 //  Helper
317 // ******************************
318 bool bufferIdLessThan(const BufferId &left, const BufferId &right) {
319   Q_CHECK_PTR(Client::networkModel());
320   if(!Client::networkModel())
321     return true;
322   
323   QModelIndex leftIndex = Client::networkModel()->bufferIndex(left);
324   QModelIndex rightIndex = Client::networkModel()->bufferIndex(right);
325
326   int leftType = Client::networkModel()->data(leftIndex, NetworkModel::BufferTypeRole).toInt();
327   int rightType = Client::networkModel()->data(rightIndex, NetworkModel::BufferTypeRole).toInt();
328
329   if(leftType != rightType)
330     return leftType < rightType;
331   else
332     return QString::compare(Client::networkModel()->data(leftIndex, Qt::DisplayRole).toString(), Client::networkModel()->data(rightIndex, Qt::DisplayRole).toString(), Qt::CaseInsensitive) < 0;
333 }
334