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