1aefaf70f775b7ce31b3eab5e6bdcd0c1854f602
[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 "buffersyncer.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   setContextMenuPolicy(Qt::CustomContextMenu);
33
34   connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
35           this, SLOT(showContextMenu(const QPoint &)));
36 }
37
38 void BufferView::init() {
39   setIndentation(10);
40   header()->setContextMenuPolicy(Qt::ActionsContextMenu);
41   hideColumn(1);
42   hideColumn(2);
43   expandAll();
44
45   setAnimated(true);
46
47 #ifndef QT_NO_DRAGANDDROP
48   setDragEnabled(true);
49   setAcceptDrops(true);
50   setDropIndicatorShown(true);
51 #endif
52
53   setSortingEnabled(true);
54   sortByColumn(0, Qt::AscendingOrder);
55   connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
56 }
57
58 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QList<NetworkId> nets) {
59   BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
60   setModel(filter);
61   connect(this, SIGNAL(removeBuffer(const QModelIndex &)), filter, SLOT(removeBuffer(const QModelIndex &)));
62 }
63
64 void BufferView::setModel(QAbstractItemModel *model) {
65   delete selectionModel();
66   QTreeView::setModel(model);
67   init();
68
69   // remove old Actions
70   QList<QAction *> oldactions = header()->actions();
71   foreach(QAction *action, oldactions) {
72     header()->removeAction(action);
73     action->deleteLater();
74   }
75
76   QString sectionName;
77   QAction *showSection;
78   for(int i = 0; i < model->columnCount(); i++) {
79     sectionName = (model->headerData(i, Qt::Horizontal, Qt::DisplayRole)).toString();
80     showSection = new QAction(sectionName, header());
81     showSection->setCheckable(true);
82     showSection->setChecked(!isColumnHidden(i));
83     showSection->setProperty("column", i);
84     connect(showSection, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
85     header()->addAction(showSection);
86   }
87   
88 }
89
90 void BufferView::joinChannel(const QModelIndex &index) {
91   BufferInfo::Type bufferType = (BufferInfo::Type)index.data(NetworkModel::BufferTypeRole).value<int>();
92
93   if(bufferType != BufferInfo::ChannelBuffer)
94     return;
95
96   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
97   
98   Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
99 }
100
101 void BufferView::keyPressEvent(QKeyEvent *event) {
102   if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
103     event->accept();
104     QModelIndex index = selectionModel()->selectedIndexes().first();
105     if(index.isValid()) {
106       emit removeBuffer(index);
107     }
108   }
109   QTreeView::keyPressEvent(event);
110 }
111
112 // ensure that newly inserted network nodes are expanded per default
113 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
114   QTreeView::rowsInserted(parent, start, end);
115   if(model()->rowCount(parent) == 1 && parent != QModelIndex()) {
116     // without updating the parent the expand will have no effect... Qt Bug?
117     update(parent); 
118     expand(parent);
119   }
120 }
121
122 void BufferView::toggleHeader(bool checked) {
123   QAction *action = qobject_cast<QAction *>(sender());
124   header()->setSectionHidden((action->property("column")).toInt(), !checked);
125 }
126
127 void BufferView::showContextMenu(const QPoint &pos) {
128   QModelIndex index = indexAt(pos);
129   if(!index.isValid() || index.data(NetworkModel::ItemTypeRole) != NetworkModel::BufferItemType) return;
130   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
131   if(bufferInfo.type() != BufferInfo::ChannelBuffer && bufferInfo.type() != BufferInfo::QueryBuffer) return;
132   QMenu contextMenu(this);
133   QAction *removeBufferAction = contextMenu.addAction(tr("Delete buffer"));
134   if(bufferInfo.type() == BufferInfo::ChannelBuffer && index.data(NetworkModel::ItemActiveRole).toBool())
135      removeBufferAction->setEnabled(false);
136
137   QAction *result = contextMenu.exec(QCursor::pos());
138   if(result == removeBufferAction) {
139     int res = QMessageBox::question(this, tr("Remove buffer permanently?"),
140                                     tr("Do you want to delete the buffer \"%1\" permanently? This will delete all related data, including all backlog "
141                                        "data, from the core's database!").arg(bufferInfo.bufferName()),
142                                         QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
143     if(res == QMessageBox::Yes) {
144       Client::bufferSyncer()->requestRemoveBuffer(bufferInfo.bufferId());
145     }
146   }
147 }
148