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