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"
27 /*****************************************
28 * The TreeView showing the Buffers
29 *****************************************/
30 // Please be carefull when reimplementing methods which are used to inform the view about changes to the data
31 // to be on the safe side: call QTreeView's method aswell
32 BufferView::BufferView(QWidget *parent) : QTreeView(parent) {
33 setContextMenuPolicy(Qt::CustomContextMenu);
35 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
36 this, SLOT(showContextMenu(const QPoint &)));
39 void BufferView::init() {
41 header()->setContextMenuPolicy(Qt::ActionsContextMenu);
48 #ifndef QT_NO_DRAGANDDROP
51 setDropIndicatorShown(true);
54 setSortingEnabled(true);
55 sortByColumn(0, Qt::AscendingOrder);
56 connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
59 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QList<NetworkId> nets) {
60 BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
62 connect(this, SIGNAL(removeBuffer(const QModelIndex &)), filter, SLOT(removeBuffer(const QModelIndex &)));
65 void BufferView::setModel(QAbstractItemModel *model) {
66 delete selectionModel();
67 QTreeView::setModel(model);
71 QList<QAction *> oldactions = header()->actions();
72 foreach(QAction *action, oldactions) {
73 header()->removeAction(action);
74 action->deleteLater();
79 for(int i = 0; i < model->columnCount(); i++) {
80 sectionName = (model->headerData(i, Qt::Horizontal, Qt::DisplayRole)).toString();
81 showSection = new QAction(sectionName, header());
82 showSection->setCheckable(true);
83 showSection->setChecked(!isColumnHidden(i));
84 showSection->setProperty("column", i);
85 connect(showSection, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
86 header()->addAction(showSection);
91 void BufferView::joinChannel(const QModelIndex &index) {
92 BufferInfo::Type bufferType = (BufferInfo::Type)index.data(NetworkModel::BufferTypeRole).value<int>();
94 if(bufferType != BufferInfo::ChannelBuffer)
97 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
99 Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
102 void BufferView::keyPressEvent(QKeyEvent *event) {
103 if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
105 QModelIndex index = selectionModel()->selectedIndexes().first();
106 if(index.isValid()) {
107 emit removeBuffer(index);
110 QTreeView::keyPressEvent(event);
113 // ensure that newly inserted network nodes are expanded per default
114 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
115 QTreeView::rowsInserted(parent, start, end);
116 if(model()->rowCount(parent) == 1 && parent != QModelIndex()) {
117 // without updating the parent the expand will have no effect... Qt Bug?
123 // select newly inserted buffers
124 if(parent.data(NetworkModel::ItemTypeRole) != NetworkModel::NetworkItemType)
127 QModelIndex newCurrent = parent.child(end, 0);
128 selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
129 selectionModel()->select(newCurrent, QItemSelectionModel::ClearAndSelect);
132 void BufferView::toggleHeader(bool checked) {
133 QAction *action = qobject_cast<QAction *>(sender());
134 header()->setSectionHidden((action->property("column")).toInt(), !checked);
137 void BufferView::showContextMenu(const QPoint &pos) {
138 QModelIndex index = indexAt(pos);
139 if(!index.isValid()) return;
140 QMenu contextMenu(this);
141 QAction *connectNetAction = new QAction(tr("Connect"), this);
142 QAction *disconnectNetAction = new QAction(tr("Disconnect"), this);
144 QAction *joinBufferAction = new QAction(tr("Join"), this);
145 QAction *partBufferAction = new QAction(tr("Part"), this);
146 QAction *removeBufferAction = new QAction(tr("Delete buffer"), this);
148 QMenu *hideEventsMenu = new QMenu(tr("Hide Events"), this);
149 QAction *hideJoinAction = hideEventsMenu->addAction(tr("Join Events"));
150 QAction *hidePartAction = hideEventsMenu->addAction(tr("Part Events"));
151 QAction *hideKillAction = hideEventsMenu->addAction(tr("Kill Events"));
152 QAction *hideQuitAction = hideEventsMenu->addAction(tr("Quit Events"));
153 QAction *hideModeAction = hideEventsMenu->addAction(tr("Mode Events"));
154 hideJoinAction->setCheckable(true);
155 hidePartAction->setCheckable(true);
156 hideKillAction->setCheckable(true);
157 hideQuitAction->setCheckable(true);
158 hideModeAction->setCheckable(true);
159 hideJoinAction->setEnabled(false);
160 hidePartAction->setEnabled(false);
161 hideKillAction->setEnabled(false);
162 hideQuitAction->setEnabled(false);
163 hideModeAction->setEnabled(false);
165 QAction *ignoreListAction = new QAction(tr("Ignore list"), this);
166 ignoreListAction->setEnabled(false);
167 QAction *whoBufferAction = new QAction(tr("WHO"), this);
169 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
170 if(index.data(NetworkModel::ItemActiveRole).toBool()) {
171 contextMenu.addAction(disconnectNetAction);
173 contextMenu.addAction(connectNetAction);
177 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
178 QString channelname = index.sibling(index.row(), 0).data().toString();
180 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
181 if(bufferInfo.type() != BufferInfo::ChannelBuffer && bufferInfo.type() != BufferInfo::QueryBuffer) return;
182 contextMenu.addAction(joinBufferAction);
183 contextMenu.addAction(partBufferAction);
184 contextMenu.addAction(removeBufferAction);
185 contextMenu.addMenu(hideEventsMenu);
186 contextMenu.addAction(ignoreListAction);
187 contextMenu.addAction(whoBufferAction);
189 if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
190 if(index.data(NetworkModel::ItemActiveRole).toBool()) {
191 removeBufferAction->setEnabled(false);
192 removeBufferAction->setToolTip("To delete the buffer, part the channel first.");
193 joinBufferAction->setVisible(false);
195 partBufferAction->setVisible(false);
198 joinBufferAction->setVisible(false);
199 partBufferAction->setVisible(false);
203 QAction *result = contextMenu.exec(QCursor::pos());
204 if(result == connectNetAction || result == disconnectNetAction) {
205 const Network *network = Client::network(index.data(NetworkModel::NetworkIdRole).value<NetworkId>());
207 if(network->connectionState() == Network::Disconnected)
208 network->requestConnect();
210 network->requestDisconnect();
212 if(result == joinBufferAction) {
213 Client::instance()->userInput(bufferInfo, QString("/JOIN %1").arg(channelname));
215 if(result == partBufferAction) {
216 Client::instance()->userInput(bufferInfo, QString("/PART %1").arg(channelname));
218 if(result == removeBufferAction) {
219 int res = QMessageBox::question(this, tr("Remove buffer permanently?"),
220 tr("Do you want to delete the buffer \"%1\" permanently? This will delete all related data, including all backlog "
221 "data, from the core's database!").arg(bufferInfo.bufferName()),
222 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
223 if(res == QMessageBox::Yes) {
224 Client::removeBuffer(bufferInfo.bufferId());
227 if(result == whoBufferAction) {
228 Client::instance()->userInput(bufferInfo, QString("/WHO %1").arg(channelname));