- Improved the speed of IrcServerHandler (and other BasicHandler
[quassel.git] / src / uisupport / bufferview.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 "client.h"
22 //#include "buffer.h"
23 #include "bufferview.h"
24 #include "networkmodel.h"
25
26 /*****************************************
27 * The TreeView showing the Buffers
28 *****************************************/
29 // Please be carefull when reimplementing methods which are used to inform the view about changes to the data
30 // to be on the safe side: call QTreeView's method aswell
31 BufferView::BufferView(QWidget *parent) : QTreeView(parent) {
32 }
33
34 void BufferView::init() {
35   setIndentation(10);
36   header()->setContextMenuPolicy(Qt::ActionsContextMenu);
37   expandAll();
38
39   setAnimated(true);
40
41 #ifndef QT_NO_DRAGANDDROP
42   setDragEnabled(true);
43   setAcceptDrops(true);
44   setDropIndicatorShown(true);
45 #endif
46
47   setSortingEnabled(true);
48   sortByColumn(0, Qt::AscendingOrder);
49   connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
50 }
51
52 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QList<uint> nets) {
53   BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
54   setModel(filter);
55   connect(this, SIGNAL(removeBuffer(const QModelIndex &)), filter, SLOT(removeBuffer(const QModelIndex &)));
56 }
57
58 void BufferView::setModel(QAbstractItemModel *model) {
59   delete selectionModel();
60   QTreeView::setModel(model);
61   init();
62
63   // remove old Actions
64   QList<QAction *> oldactions = header()->actions();
65   foreach(QAction *action, oldactions) {
66     header()->removeAction(action);
67     action->deleteLater();
68   }
69
70   QString sectionName;
71   QAction *showSection;
72   for(int i = 0; i < model->columnCount(); i++) {
73     sectionName = (model->headerData(i, Qt::Horizontal, Qt::DisplayRole)).toString();
74     showSection = new QAction(sectionName, header());
75     showSection->setCheckable(true);
76     showSection->setChecked(true);
77     showSection->setProperty("column", i);
78     connect(showSection, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
79     header()->addAction(showSection);
80   }
81   
82 }
83
84 void BufferView::joinChannel(const QModelIndex &index) {
85   BufferItem::Type bufferType = (BufferItem::Type)index.data(NetworkModel::BufferTypeRole).toInt();
86
87   if(bufferType != BufferItem::ChannelType)
88     return;
89   
90   Client::fakeInput(index.data(NetworkModel::BufferIdRole).toUInt(), QString("/JOIN %1").arg(index.sibling(index.row(), 0).data().toString()));
91 }
92
93 void BufferView::keyPressEvent(QKeyEvent *event) {
94   if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
95     event->accept();
96     QModelIndex index = selectionModel()->selectedIndexes().first();
97     if(index.isValid()) {
98       emit removeBuffer(index);
99     }
100   }
101   QTreeView::keyPressEvent(event);
102 }
103
104 // ensure that newly inserted network nodes are expanded per default
105 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
106   QTreeView::rowsInserted(parent, start, end);
107   if(model()->rowCount(parent) == 1 && parent != QModelIndex()) {
108     // without updating the parent the expand will have no effect... Qt Bug?
109     update(parent); 
110     expand(parent);
111   }
112 }
113
114 void BufferView::toggleHeader(bool checked) {
115   QAction *action = qobject_cast<QAction *>(sender());
116   header()->setSectionHidden((action->property("column")).toInt(), not checked);
117 }