2b2f00ddc3f7eac52bc2b3a6b8fa2e4b31e1c3e4
[quassel.git] / src / uisupport / bufferview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC 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) 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   // dirty fast hack:
33   header()->setContextMenuPolicy(Qt::ActionsContextMenu);
34
35   QAction *showBufferAct = new QAction(tr("Buffer"), header());
36   showBufferAct->setCheckable(true);
37   showBufferAct->setChecked(true);
38   showBufferAct->setProperty("column", 0);
39   connect(showBufferAct, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
40   header()->addAction(showBufferAct);
41   
42   QAction *showTopicAct = new QAction(tr("Topic"), header());
43   showTopicAct->setCheckable(true);
44   showTopicAct->setChecked(true);
45   showTopicAct->setProperty("column", 1);
46   connect(showTopicAct, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
47   header()->addAction(showTopicAct);
48     
49   QAction *showNickAct = new QAction(tr("Nick Count"), header());
50   showNickAct->setCheckable(true);
51   showNickAct->setChecked(true);
52   showNickAct->setProperty("column", 2);
53   connect(showNickAct, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
54   header()->addAction(showNickAct);
55   
56 }
57
58 void BufferView::init() {
59   setIndentation(10);
60   // header()->hide();
61   // header()->hideSection(1);
62   expandAll();
63
64   setAnimated(true);
65
66 #ifndef QT_NO_DRAGANDDROP
67   setDragEnabled(true);
68   setAcceptDrops(true);
69   setDropIndicatorShown(true);
70 #endif
71
72   setSortingEnabled(true);
73   sortByColumn(0, Qt::AscendingOrder);
74   connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
75 }
76
77 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QList<uint> nets) {
78   BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
79   setModel(filter);
80   connect(this, SIGNAL(removeBuffer(const QModelIndex &)), filter, SLOT(removeBuffer(const QModelIndex &)));
81 }
82
83 void BufferView::setModel(QAbstractItemModel *model) {
84   delete selectionModel();
85   QTreeView::setModel(model);
86   init();
87   
88 }
89
90 void BufferView::joinChannel(const QModelIndex &index) {
91   Buffer::Type bufferType = (Buffer::Type)index.data(NetworkModel::BufferTypeRole).toInt();
92
93   if(bufferType != Buffer::ChannelType)
94     return;
95   
96   Client::fakeInput(index.data(NetworkModel::BufferUidRole).toUInt(), QString("/JOIN %1").arg(index.sibling(index.row(), 0).data().toString()));
97 }
98
99 void BufferView::keyPressEvent(QKeyEvent *event) {
100   if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
101     event->accept();
102     QModelIndex index = selectionModel()->selectedIndexes().first();
103     if(index.isValid()) {
104       emit removeBuffer(index);
105     }
106   }
107   QTreeView::keyPressEvent(event);
108 }
109
110 // ensure that newly inserted network nodes are expanded per default
111 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
112   QTreeView::rowsInserted(parent, start, end);
113   if(model()->rowCount(parent) == 1 && parent != QModelIndex()) {
114     // without updating the parent the expand will have no effect... Qt Bug?
115     update(parent); 
116     expand(parent);
117   }
118 }
119
120 void BufferView::toggleHeader(bool checked) {
121   QAction *action = qobject_cast<QAction *>(sender());
122   header()->setSectionHidden((action->property("column")).toInt(), not checked);
123 }