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