added context menu to systray
[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   hideColumn(1);
38   hideColumn(2);
39   expandAll();
40
41   setAnimated(true);
42
43 #ifndef QT_NO_DRAGANDDROP
44   setDragEnabled(true);
45   setAcceptDrops(true);
46   setDropIndicatorShown(true);
47 #endif
48
49   setSortingEnabled(true);
50   sortByColumn(0, Qt::AscendingOrder);
51   connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
52 }
53
54 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QList<NetworkId> nets) {
55   BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
56   setModel(filter);
57   connect(this, SIGNAL(removeBuffer(const QModelIndex &)), filter, SLOT(removeBuffer(const QModelIndex &)));
58 }
59
60 void BufferView::setModel(QAbstractItemModel *model) {
61   delete selectionModel();
62   QTreeView::setModel(model);
63   init();
64
65   // remove old Actions
66   QList<QAction *> oldactions = header()->actions();
67   foreach(QAction *action, oldactions) {
68     header()->removeAction(action);
69     action->deleteLater();
70   }
71
72   QString sectionName;
73   QAction *showSection;
74   for(int i = 0; i < model->columnCount(); i++) {
75     sectionName = (model->headerData(i, Qt::Horizontal, Qt::DisplayRole)).toString();
76     showSection = new QAction(sectionName, header());
77     showSection->setCheckable(true);
78     showSection->setChecked(!isColumnHidden(i));
79     showSection->setProperty("column", i);
80     connect(showSection, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
81     header()->addAction(showSection);
82   }
83   
84 }
85
86 void BufferView::joinChannel(const QModelIndex &index) {
87   BufferItem::Type bufferType = (BufferItem::Type)index.data(NetworkModel::BufferTypeRole).toInt();
88
89   if(bufferType != BufferItem::ChannelType)
90     return;
91   
92   Client::fakeInput(index.data(NetworkModel::BufferIdRole).value<BufferId>(), QString("/JOIN %1").arg(index.sibling(index.row(), 0).data().toString()));
93 }
94
95 void BufferView::keyPressEvent(QKeyEvent *event) {
96   if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
97     event->accept();
98     QModelIndex index = selectionModel()->selectedIndexes().first();
99     if(index.isValid()) {
100       emit removeBuffer(index);
101     }
102   }
103   QTreeView::keyPressEvent(event);
104 }
105
106 // ensure that newly inserted network nodes are expanded per default
107 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
108   QTreeView::rowsInserted(parent, start, end);
109   if(model()->rowCount(parent) == 1 && parent != QModelIndex()) {
110     // without updating the parent the expand will have no effect... Qt Bug?
111     update(parent); 
112     expand(parent);
113   }
114 }
115
116 void BufferView::toggleHeader(bool checked) {
117   QAction *action = qobject_cast<QAction *>(sender());
118   header()->setSectionHidden((action->property("column")).toInt(), !checked);
119 }