1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
22 #include "buffersyncer.h"
23 #include "bufferview.h"
24 #include "networkmodel.h"
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);
34 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
35 this, SLOT(showContextMenu(const QPoint &)));
38 void BufferView::init() {
40 header()->setContextMenuPolicy(Qt::ActionsContextMenu);
47 #ifndef QT_NO_DRAGANDDROP
50 setDropIndicatorShown(true);
53 setSortingEnabled(true);
54 sortByColumn(0, Qt::AscendingOrder);
55 connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
58 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QList<NetworkId> nets) {
59 BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
61 connect(this, SIGNAL(removeBuffer(const QModelIndex &)), filter, SLOT(removeBuffer(const QModelIndex &)));
64 void BufferView::setModel(QAbstractItemModel *model) {
65 delete selectionModel();
66 QTreeView::setModel(model);
70 QList<QAction *> oldactions = header()->actions();
71 foreach(QAction *action, oldactions) {
72 header()->removeAction(action);
73 action->deleteLater();
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);
90 void BufferView::joinChannel(const QModelIndex &index) {
91 BufferInfo::Type bufferType = (BufferInfo::Type)index.data(NetworkModel::BufferTypeRole).value<int>();
93 if(bufferType != BufferInfo::ChannelBuffer)
96 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
98 Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
101 void BufferView::keyPressEvent(QKeyEvent *event) {
102 if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
104 QModelIndex index = selectionModel()->selectedIndexes().first();
105 if(index.isValid()) {
106 emit removeBuffer(index);
109 QTreeView::keyPressEvent(event);
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?
122 void BufferView::toggleHeader(bool checked) {
123 QAction *action = qobject_cast<QAction *>(sender());
124 header()->setSectionHidden((action->property("column")).toInt(), !checked);
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);
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());