fixed renaming issue for queries
[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 "networkmodel.h"
26
27 #include "uisettings.h"
28
29 /*****************************************
30 * The Filter for the Tree View
31 *****************************************/
32 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, const Modes &filtermode, const QList<NetworkId> &nets)
33   : QSortFilterProxyModel(model),
34     mode(filtermode),
35     networks(QSet<NetworkId>::fromList(nets))
36 {
37   setSourceModel(model);
38   setSortCaseSensitivity(Qt::CaseInsensitive);
39 }
40
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;
46   }
47   return flags;
48 }
49
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);
54
55   if(!NetworkModel::mimeContainsBufferList(data))
56     return false;
57
58   QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
59
60   NetworkId netId;
61   BufferId bufferId;
62   for(int i = 0; i < bufferList.count(); i++) {
63     netId = bufferList[i].first;
64     bufferId = bufferList[i].second;
65     if(!networks.contains(netId)) {
66       networks << netId;
67     }
68     addBuffer(bufferId);
69   }
70   return true;
71 }
72
73 void BufferViewFilter::addBuffer(const BufferId &bufferuid) {
74   if(!buffers.contains(bufferuid)) {
75     buffers << bufferuid;
76     invalidateFilter();
77   }
78 }
79
80 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
81   if(!(mode & FullCustom))
82     return; // only custom buffers can be customized... obviously... :)
83   
84   if(index.parent() == QModelIndex())
85     return; // only child elements can be deleted
86
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>();
90
91   if(buffers.contains(bufferuid)) {
92     buffers.remove(bufferuid);
93     
94     if(lastBuffer) {
95       networks.remove(netId);
96       Q_ASSERT(!networks.contains(netId));
97     }
98
99     invalidateFilter();
100   }
101   
102 }
103
104
105 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
106   BufferInfo::Type bufferType = (BufferInfo::Type) source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt();
107   
108   if((mode & NoChannels) && bufferType == BufferInfo::ChannelBuffer)
109     return false;
110   if((mode & NoQueries) && bufferType == BufferInfo::QueryBuffer)
111     return false;
112   if((mode & NoServers) && bufferType == BufferInfo::StatusBuffer)
113     return false;
114
115 //   bool isActive = source_bufferIndex.data(NetworkModel::BufferActiveRole).toBool();
116 //   if((mode & NoActive) && isActive)
117 //     return false;
118 //   if((mode & NoInactive) && !isActive)
119 //     return false;
120
121   if((mode & FullCustom)) {
122     BufferId bufferuid = source_bufferIndex.data(NetworkModel::BufferIdRole).value<BufferId>();
123     return buffers.contains(bufferuid);
124   }
125     
126   return true;
127 }
128
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));
132 }
133
134 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
135   QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
136   
137   if(!child.isValid()) {
138     qDebug() << "filterAcceptsRow has been called with an invalid Child";
139     return false;
140   }
141
142   if(source_parent == QModelIndex())
143     return filterAcceptNetwork(child);
144   else
145     return filterAcceptBuffer(child);
146 }
147
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();
151
152   if(lefttype != righttype)
153     return lefttype < righttype;
154   else
155     return QSortFilterProxyModel::lessThan(left, right);
156 }
157
158 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
159   if(role == Qt::ForegroundRole)
160     return foreground(index);
161   else
162     return QSortFilterProxyModel::data(index, role);
163 }
164
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)));
172
173   if(!index.data(NetworkModel::ItemActiveRole).toBool())
174     return inactiveActivity.value<QColor>();
175
176   Buffer::ActivityLevel activity = (Buffer::ActivityLevel)index.data(NetworkModel::BufferActivityRole).toInt();
177
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>();
184   
185   return noActivity.value<QColor>();
186   
187   // FIXME:: make colors configurable;
188
189 }