Refactoring the GUI. Work in progress.
[quassel.git] / gui / bufferview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
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) any later version.                                   *
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 "bufferview.h"
22 #include "bufferviewwidget.h"
23
24 /*****************************************
25 * The TreeView showing the Buffers
26 *****************************************/
27 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, Modes filtermode, QStringList nets, QObject *parent) : QSortFilterProxyModel(parent) {
28   setSourceModel(model);
29   mode = filtermode;
30   networks = nets;
31   
32   connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidateMe()));
33   connect(model, SIGNAL(updateSelection(const QModelIndex &, QItemSelectionModel::SelectionFlags)), this, SLOT(select(const QModelIndex &, QItemSelectionModel::SelectionFlags)));
34     
35   connect(this, SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), model, SLOT(changeCurrent(const QModelIndex &, const QModelIndex &)));
36   connect(this, SIGNAL(doubleClicked(const QModelIndex &)), model, SLOT(doubleClickReceived(const QModelIndex &)));
37 }
38
39 void BufferViewFilter::invalidateMe() {
40   invalidateFilter();
41 }
42
43 void BufferViewFilter::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
44   emit updateSelection(mapFromSource(index), command);
45 }
46
47 void BufferViewFilter::changeCurrent(const QModelIndex &current, const QModelIndex &previous) {
48   emit currentChanged(mapToSource(current), mapToSource(previous));
49 }
50
51 void BufferViewFilter::doubleClickReceived(const QModelIndex &clicked) {
52   emit doubleClicked(mapToSource(clicked));
53 }
54
55 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
56   QModelIndex child = source_parent.child(source_row, 0);
57   if(!child.isValid())
58     return true; // can't imagine this case but true sounds good :)
59   
60   Buffer::Type bufferType = (Buffer::Type) child.data(BufferTreeModel::BufferTypeRole).toInt();
61   if((mode & NoChannels) && bufferType == Buffer::ChannelBuffer) return false;
62   if((mode & NoQueries) && bufferType == Buffer::QueryBuffer) return false;
63   if((mode & NoServers) && bufferType == Buffer::ServerBuffer) return false;
64
65   bool isActive = child.data(BufferTreeModel::BufferActiveRole).toBool();
66   if((mode & NoActive) && isActive) return false;
67   if((mode & NoInactive) && !isActive) return false;
68
69   QString net = child.data(Qt::DisplayRole).toString();
70   if((mode & SomeNets) && !networks.contains(net)) return false;
71     
72   return true;
73 }
74
75 /*****************************************
76 * The TreeView showing the Buffers
77 *****************************************/
78 // Please be carefull when reimplementing methods which are used to inform the view about changes to the data
79 // to be on the safe side: call QTreeView's method aswell
80 BufferView::BufferView(QWidget *parent) : QTreeView(parent) {
81 }
82
83 void BufferView::init() {
84   setIndentation(10);
85   header()->hide();
86   header()->hideSection(1);
87   
88   setDragEnabled(true);
89   setAcceptDrops(true);
90   setDropIndicatorShown(true);
91   
92   connect(selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), model(), SLOT(changeCurrent(const QModelIndex &, const QModelIndex &)));
93   connect(this, SIGNAL(doubleClicked(const QModelIndex &)), model(), SLOT(doubleClickReceived(const QModelIndex &)));
94   connect(model(), SIGNAL(updateSelection(const QModelIndex &, QItemSelectionModel::SelectionFlags)), selectionModel(), SLOT(select(const QModelIndex &, QItemSelectionModel::SelectionFlags)));
95 }
96
97 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QStringList nets) {
98   BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
99   setModel(filter);
100 }
101
102 void BufferView::setModel(QAbstractItemModel *model) {
103   QTreeView::setModel(model);
104   init();
105 }
106
107 void BufferView::dragEnterEvent(QDragEnterEvent *event) {
108   // not yet needed... this will be usefull to keep track of the active view when customizing them with drag and drop
109   QTreeView::dragEnterEvent(event);
110 }
111
112 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
113   if(parent.parent() == QModelIndex()) setExpanded(parent, true); qDebug() << "expanded";
114   QTreeView::rowsInserted(parent, start, end);
115 }