1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "bufferviewfilter.h"
25 #include "networkmodel.h"
27 #include "uisettings.h"
29 /*****************************************
30 * The Filter for the Tree View
31 *****************************************/
32 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, const Modes &filtermode, const QList<NetworkId> &nets)
33 : QSortFilterProxyModel(model),
35 networks(QSet<NetworkId>::fromList(nets))
37 setSourceModel(model);
38 setSortCaseSensitivity(Qt::CaseInsensitive);
41 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
42 Qt::ItemFlags flags = mapToSource(index).flags();
43 if(mode & FullCustom) {
44 if(index == QModelIndex() || index.parent() == QModelIndex())
45 flags |= Qt::ItemIsDropEnabled;
50 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
51 // drops have to occur in the open field
52 if(parent != QModelIndex())
53 return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
55 if(!NetworkModel::mimeContainsBufferList(data))
58 QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
62 for(int i = 0; i < bufferList.count(); i++) {
63 netId = bufferList[i].first;
64 bufferId = bufferList[i].second;
65 if(!networks.contains(netId)) {
73 void BufferViewFilter::addBuffer(const BufferId &bufferuid) {
74 if(!buffers.contains(bufferuid)) {
80 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
81 if(!(mode & FullCustom))
82 return; // only custom buffers can be customized... obviously... :)
84 if(index.parent() == QModelIndex())
85 return; // only child elements can be deleted
87 bool lastBuffer = (rowCount(index.parent()) == 1);
88 NetworkId netId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
89 BufferId bufferuid = index.data(NetworkModel::BufferIdRole).value<BufferId>();
91 if(buffers.contains(bufferuid)) {
92 buffers.remove(bufferuid);
95 networks.remove(netId);
96 Q_ASSERT(!networks.contains(netId));
105 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
106 BufferInfo::Type bufferType = (BufferInfo::Type) source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt();
108 if((mode & NoChannels) && bufferType == BufferInfo::ChannelBuffer)
110 if((mode & NoQueries) && bufferType == BufferInfo::QueryBuffer)
112 if((mode & NoServers) && bufferType == BufferInfo::StatusBuffer)
115 // bool isActive = source_bufferIndex.data(NetworkModel::BufferActiveRole).toBool();
116 // if((mode & NoActive) && isActive)
118 // if((mode & NoInactive) && !isActive)
121 if((mode & FullCustom)) {
122 BufferId bufferuid = source_bufferIndex.data(NetworkModel::BufferIdRole).value<BufferId>();
123 return buffers.contains(bufferuid);
129 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
130 NetworkId net = source_index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
131 return !((mode & (SomeNets | FullCustom)) && !networks.contains(net));
134 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
135 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
137 if(!child.isValid()) {
138 qDebug() << "filterAcceptsRow has been called with an invalid Child";
142 if(source_parent == QModelIndex())
143 return filterAcceptNetwork(child);
145 return filterAcceptBuffer(child);
148 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) const {
149 int lefttype = left.data(NetworkModel::BufferTypeRole).toInt();
150 int righttype = right.data(NetworkModel::BufferTypeRole).toInt();
152 if(lefttype != righttype)
153 return lefttype < righttype;
155 return QSortFilterProxyModel::lessThan(left, right);
158 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
159 if(role == Qt::ForegroundRole)
160 return foreground(index);
162 return QSortFilterProxyModel::data(index, role);
165 QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
166 UiSettings s("QtUi/Colors");
167 QVariant inactiveActivity = s.value("inactiveActivityFG", QVariant(QColor(Qt::gray)));
168 QVariant noActivity = s.value("noActivityFG", QVariant(QColor(Qt::black)));
169 QVariant highlightActivity = s.value("highlightActivityFG", QVariant(QColor(Qt::magenta)));
170 QVariant newMessageActivity = s.value("newMessageActivityFG", QVariant(QColor(Qt::green)));
171 QVariant otherActivity = s.value("otherActivityFG", QVariant(QColor(Qt::darkGreen)));
173 if(!index.data(NetworkModel::ItemActiveRole).toBool())
174 return inactiveActivity.value<QColor>();
176 Buffer::ActivityLevel activity = (Buffer::ActivityLevel)index.data(NetworkModel::BufferActivityRole).toInt();
178 if(activity & Buffer::Highlight)
179 return highlightActivity.value<QColor>();
180 if(activity & Buffer::NewMessage)
181 return newMessageActivity.value<QColor>();
182 if(activity & Buffer::OtherActivity)
183 return otherActivity.value<QColor>();
185 return noActivity.value<QColor>();
187 // FIXME:: make colors configurable;