Fixing a client crash that could be triggered under certain preconditions if a ircUse...
[quassel.git] / src / uisupport / nickviewfilter.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 "nickviewfilter.h"
22 #include "networkmodel.h"
23 #include "uisettings.h"
24
25 /******************************************************************************************
26  * NickViewFilter
27  ******************************************************************************************/
28 NickViewFilter::NickViewFilter(const BufferId &bufferId, NetworkModel *parent)
29   : QSortFilterProxyModel(parent),
30     _bufferId(bufferId)
31 {
32   setSourceModel(parent);
33   setDynamicSortFilter(true);
34   setSortCaseSensitivity(Qt::CaseInsensitive);
35   setSortRole(TreeModel::SortRole);
36   loadColors();
37 }
38
39 void NickViewFilter::loadColors() {
40   UiSettings s("QtUi/Colors");
41   _FgOnlineStatus = s.value("onlineStatusFG", QVariant(QColor(Qt::black))).value<QColor>();
42   _FgAwayStatus = s.value("awayStatusFG", QVariant(QColor(Qt::gray))).value<QColor>();
43   // FIXME: use the style interface instead of qsettings
44 }
45
46 QVariant NickViewFilter::data(const QModelIndex &index, int role) const {
47   if(role == Qt::ForegroundRole)
48     return foreground(index);
49   else
50     return QSortFilterProxyModel::data(index, role);
51 }
52
53 QVariant NickViewFilter::foreground(const QModelIndex &index) const {
54   if(!index.data(NetworkModel::ItemActiveRole).toBool())
55     return _FgAwayStatus;
56   return _FgOnlineStatus;
57 }
58
59
60 bool NickViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
61   // root node, networkindexes, the bufferindex of the buffer this filter is active for and it's childs are accepted
62   if(!source_parent.isValid())
63     return true;
64
65   QModelIndex source_child = source_parent.child(source_row, 0);
66   return (sourceModel()->data(source_child, NetworkModel::BufferIdRole).value<BufferId>() == _bufferId);
67 }