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 ***************************************************************************/
21 #include "bufferview.h"
23 #include "buffermodel.h"
24 #include "bufferviewfilter.h"
25 #include "buffersyncer.h"
27 #include "mappedselectionmodel.h"
29 #include "networkmodel.h"
31 #include "uisettings.h"
37 #include <QHeaderView>
38 #include <QInputDialog>
41 #include <QMessageBox>
44 /*****************************************
45 * The TreeView showing the Buffers
46 *****************************************/
47 // Please be carefull when reimplementing methods which are used to inform the view about changes to the data
48 // to be on the safe side: call QTreeView's method aswell
49 BufferView::BufferView(QWidget *parent)
51 showChannelList(tr("Show Channel List"), this),
52 _connectNetAction(tr("Connect"), this),
53 _disconnectNetAction(tr("Disconnect"), this),
54 _joinChannelAction(tr("Join Channel"), this),
56 _joinBufferAction(tr("Join"), this),
57 _partBufferAction(tr("Part"), this),
58 _hideBufferTemporarilyAction(tr("Hide buffers"), this),
59 _hideBufferPermanentlyAction(tr("Hide buffers permanently"), this),
60 _removeBufferAction(tr("Delete buffer"), this),
61 _ignoreListAction(tr("Ignore list"), this),
63 _hideJoinAction(tr("Join Events"), this),
64 _hidePartAction(tr("Part Events"), this),
65 _hideKillAction(tr("Kill Events"), this),
66 _hideQuitAction(tr("Quit Events"), this),
67 _hideModeAction(tr("Mode Events"), this)
70 // currently no events can be hidden -> disable actions
71 _hideJoinAction.setCheckable(true);
72 _hidePartAction.setCheckable(true);
73 _hideKillAction.setCheckable(true);
74 _hideQuitAction.setCheckable(true);
75 _hideModeAction.setCheckable(true);
76 _hideJoinAction.setEnabled(false);
77 _hidePartAction.setEnabled(false);
78 _ignoreListAction.setEnabled(false);
79 _hideKillAction.setEnabled(false);
80 _hideQuitAction.setEnabled(false);
81 _hideModeAction.setEnabled(false);
83 setSelectionMode(QAbstractItemView::ExtendedSelection);
86 void BufferView::init() {
88 header()->setContextMenuPolicy(Qt::ActionsContextMenu);
95 #ifndef QT_NO_DRAGANDDROP
98 setDropIndicatorShown(true);
101 setSortingEnabled(true);
102 sortByColumn(0, Qt::AscendingOrder);
104 // this is a workaround to not join channels automatically... we need a saner way to navigate for qtopia anyway though,
105 // such as mark first, activate at second click...
106 connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
108 connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(joinChannel(QModelIndex))); // Qtopia uses single click for activation
112 void BufferView::setModel(QAbstractItemModel *model) {
113 delete selectionModel();
114 if(QTreeView::model()) {
115 disconnect(QTreeView::model(), SIGNAL(layoutChanged()), this, SLOT(layoutChanged()));
118 QTreeView::setModel(model);
120 // remove old Actions
121 QList<QAction *> oldactions = header()->actions();
122 foreach(QAction *action, oldactions) {
123 header()->removeAction(action);
124 action->deleteLater();
130 connect(model, SIGNAL(layoutChanged()), this, SLOT(layoutChanged()));
133 QAction *showSection;
134 for(int i = 1; i < model->columnCount(); i++) {
135 sectionName = (model->headerData(i, Qt::Horizontal, Qt::DisplayRole)).toString();
136 showSection = new QAction(sectionName, header());
137 showSection->setCheckable(true);
138 showSection->setChecked(!isColumnHidden(i));
139 showSection->setProperty("column", i);
140 connect(showSection, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
141 header()->addAction(showSection);
146 void BufferView::setFilteredModel(QAbstractItemModel *model_, BufferViewConfig *config) {
147 BufferViewFilter *filter = qobject_cast<BufferViewFilter *>(model());
149 filter->setConfig(config);
155 disconnect(this, 0, model(), 0);
161 BufferViewFilter *filter = new BufferViewFilter(model_, config);
167 void BufferView::setSelectionModel(QItemSelectionModel *selectionModel) {
168 if(QTreeView::selectionModel())
169 disconnect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
170 model(), SIGNAL(checkPreviousCurrentForRemoval(QModelIndex, QModelIndex)));
172 QTreeView::setSelectionModel(selectionModel);
173 BufferViewFilter *filter = qobject_cast<BufferViewFilter *>(model());
175 connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
176 filter, SLOT(checkPreviousCurrentForRemoval(QModelIndex, QModelIndex)));
180 void BufferView::setConfig(BufferViewConfig *config) {
181 if(_config == config)
185 disconnect(_config, 0, this, 0);
190 connect(config, SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(setRootIndexForNetworkId(const NetworkId &)));
191 setRootIndexForNetworkId(config->networkId());
193 setRootIndex(QModelIndex());
197 void BufferView::setRootIndexForNetworkId(const NetworkId &networkId) {
198 if(!networkId.isValid() || !model()) {
199 setRootIndex(QModelIndex());
201 int networkCount = model()->rowCount();
203 for(int i = 0; i < networkCount; i++) {
204 child = model()->index(i, 0);
205 if(networkId == model()->data(child, NetworkModel::NetworkIdRole).value<NetworkId>())
211 void BufferView::joinChannel(const QModelIndex &index) {
212 BufferInfo::Type bufferType = (BufferInfo::Type)index.data(NetworkModel::BufferTypeRole).value<int>();
214 if(bufferType != BufferInfo::ChannelBuffer)
217 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
219 Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
222 void BufferView::keyPressEvent(QKeyEvent *event) {
223 if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
225 removeSelectedBuffers();
227 QTreeView::keyPressEvent(event);
230 void BufferView::removeSelectedBuffers(bool permanently) {
235 QSet<BufferId> removedRows;
236 foreach(QModelIndex index, selectionModel()->selectedIndexes()) {
237 if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::BufferItemType)
240 bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
241 if(removedRows.contains(bufferId))
244 removedRows << bufferId;
247 config()->requestRemoveBufferPermanently(bufferId);
249 config()->requestRemoveBuffer(bufferId);
253 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
254 QTreeView::rowsInserted(parent, start, end);
256 // ensure that newly inserted network nodes are expanded per default
257 if(parent.data(NetworkModel::ItemTypeRole) != NetworkModel::NetworkItemType)
260 if(model()->rowCount(parent) == 1 && parent.data(NetworkModel::ItemActiveRole) == true) {
261 // without updating the parent the expand will have no effect... Qt Bug?
267 void BufferView::layoutChanged() {
270 // expand all active networks
271 QModelIndex networkIdx;
272 for(int row = 0; row < model()->rowCount(); row++) {
273 networkIdx = model()->index(row, 0);
275 if(model()->rowCount(networkIdx) > 0 && model()->data(networkIdx, NetworkModel::ItemActiveRole) == true) {
278 collapse(networkIdx);
282 // update selection to current one
283 MappedSelectionModel *mappedSelectionModel = qobject_cast<MappedSelectionModel *>(selectionModel());
284 if(!config() || !mappedSelectionModel)
287 mappedSelectionModel->mappedSetCurrentIndex(Client::bufferModel()->standardSelectionModel()->currentIndex(), QItemSelectionModel::Current);
288 mappedSelectionModel->mappedSelect(Client::bufferModel()->standardSelectionModel()->selection(), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
291 void BufferView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
292 QTreeView::dataChanged(topLeft, bottomRight);
294 // determine how many items have been changed and if any of them is a networkitem
295 // which just swichted from active to inactive or vice versa
296 if(topLeft.data(NetworkModel::ItemTypeRole) != NetworkModel::NetworkItemType)
299 for(int i = topLeft.row(); i <= bottomRight.row(); i++) {
300 QModelIndex networkIdx = topLeft.sibling(topLeft.row(), 0);
301 if(model()->rowCount(networkIdx) == 0)
304 bool isActive = networkIdx.data(NetworkModel::ItemActiveRole).toBool();
306 if(isExpanded(networkIdx) != isActive) setExpanded(networkIdx, true);
308 if(isExpanded(networkIdx) != isActive) setExpanded(networkIdx, isActive);
314 void BufferView::toggleHeader(bool checked) {
315 QAction *action = qobject_cast<QAction *>(sender());
316 header()->setSectionHidden((action->property("column")).toInt(), !checked);
319 bool BufferView::checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState) {
323 // NetworkModel::itemTypes itemType = static_cast<NetworkModel::itemTypes>(index.data(NetworkModel::ItemTypeRole).toInt());
324 // if(!(itemType & validItemTypes))
327 ItemActiveStates isActive = index.data(NetworkModel::ItemActiveRole).toBool()
331 if(!(isActive & requiredActiveState))
337 void BufferView::addItemToMenu(QAction &action, QMenu &menu, const QModelIndex &index, ItemActiveStates requiredActiveState) {
338 if(checkRequirements(index, requiredActiveState)) {
339 menu.addAction(&action);
340 action.setVisible(true);
342 action.setVisible(false);
346 void BufferView::addItemToMenu(QAction &action, QMenu &menu, bool condition) {
348 menu.addAction(&action);
349 action.setVisible(true);
351 action.setVisible(false);
356 void BufferView::addItemToMenu(QMenu &subMenu, QMenu &menu, const QModelIndex &index, ItemActiveStates requiredActiveState) {
357 if(checkRequirements(index, requiredActiveState)) {
358 menu.addMenu(&subMenu);
359 subMenu.setVisible(true);
361 subMenu.setVisible(false);
365 void BufferView::addSeparatorToMenu(QMenu &menu, const QModelIndex &index, ItemActiveStates requiredActiveState) {
366 if(checkRequirements(index, requiredActiveState)) {
371 QMenu *BufferView::createHideEventsSubMenu(QMenu &menu) {
372 // QMenu *hideEventsMenu = new QMenu(tr("Hide Events"), &menu);
373 QMenu *hideEventsMenu = menu.addMenu(tr("Hide Events"));
374 hideEventsMenu->addAction(&_hideJoinAction);
375 hideEventsMenu->addAction(&_hidePartAction);
376 hideEventsMenu->addAction(&_hideKillAction);
377 hideEventsMenu->addAction(&_hideQuitAction);
378 hideEventsMenu->addAction(&_hideModeAction);
379 return hideEventsMenu;
382 //void BufferView::showContextMenu(const QPoint &pos) {
383 void BufferView::contextMenuEvent(QContextMenuEvent *event) {
384 QModelIndex index = indexAt(event->pos());
388 const Network *network = Client::network(index.data(NetworkModel::NetworkIdRole).value<NetworkId>());
389 Q_CHECK_PTR(network);
391 QIcon connectionStateIcon;
393 if(network->connectionState() == Network::Initialized) {
394 connectionStateIcon = QIcon(":/22x22/actions/network-connect");
395 } else if(network->connectionState() == Network::Disconnected) {
396 connectionStateIcon = QIcon(":/22x22/actions/network-disconnect");
398 connectionStateIcon = QIcon(":/22x22/actions/gear");
402 QMenu contextMenu(this);
403 NetworkModel::itemType itemType = static_cast<NetworkModel::itemType>(index.data(NetworkModel::ItemTypeRole).toInt());
406 case NetworkModel::NetworkItemType:
407 showChannelList.setData(index.data(NetworkModel::NetworkIdRole));
408 _disconnectNetAction.setIcon(connectionStateIcon);
409 _connectNetAction.setIcon(connectionStateIcon);
410 addItemToMenu(showChannelList, contextMenu, index, ActiveState);
411 addItemToMenu(_disconnectNetAction, contextMenu, index, ActiveState);
412 addItemToMenu(_connectNetAction, contextMenu, index, InactiveState);
413 addSeparatorToMenu(contextMenu, index, ActiveState);
414 addItemToMenu(_joinChannelAction, contextMenu, index, ActiveState);
416 case NetworkModel::BufferItemType:
418 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
419 switch(bufferInfo.type()) {
420 case BufferInfo::ChannelBuffer:
421 addItemToMenu(_joinBufferAction, contextMenu, index, InactiveState);
422 addItemToMenu(_partBufferAction, contextMenu, index, ActiveState);
423 addItemToMenu(_hideBufferTemporarilyAction, contextMenu, (bool)config());
424 addItemToMenu(_hideBufferPermanentlyAction, contextMenu, (bool)config());
425 addItemToMenu(_removeBufferAction, contextMenu, index, InactiveState);
426 createHideEventsSubMenu(contextMenu);
427 addItemToMenu(_ignoreListAction, contextMenu);
429 case BufferInfo::QueryBuffer:
430 addItemToMenu(_hideBufferTemporarilyAction, contextMenu, (bool)config());
431 addItemToMenu(_hideBufferPermanentlyAction, contextMenu, (bool)config());
432 addItemToMenu(_removeBufferAction, contextMenu);
433 createHideEventsSubMenu(contextMenu);
436 addItemToMenu(_hideBufferTemporarilyAction, contextMenu, (bool)config());
437 addItemToMenu(_hideBufferPermanentlyAction, contextMenu, (bool)config());
446 if(contextMenu.actions().isEmpty())
448 QAction *result = contextMenu.exec(QCursor::pos());
451 if(network && result == &_connectNetAction) {
452 network->requestConnect();
456 if(network && result == &_disconnectNetAction) {
457 network->requestDisconnect();
461 if(result == &_joinChannelAction) {
462 // FIXME no QInputDialog in Qtopia
465 QString channelName = QInputDialog::getText(this, tr("Join Channel"), tr("Input channel name:"), QLineEdit::Normal, QString(), &ok);
466 if(ok && !channelName.isEmpty()) {
467 Client::instance()->userInput(BufferInfo::fakeStatusBuffer(index.data(NetworkModel::NetworkIdRole).value<NetworkId>()), QString("/J %1").arg(channelName));
468 // BufferInfo bufferInfo = index.child(0,0).data(NetworkModel::BufferInfoRole).value<BufferInfo>();
469 // if(bufferInfo.isValid()) {
470 // Client::instance()->userInput(bufferInfo, QString("/J %1").arg(channelName));
477 if(result == &_joinBufferAction) {
478 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
479 Client::instance()->userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
483 if(result == &_partBufferAction) {
484 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
485 Client::instance()->userInput(bufferInfo, QString("/PART"));
489 if(result == &_hideBufferTemporarilyAction) {
490 removeSelectedBuffers();
494 if(result == &_hideBufferPermanentlyAction) {
495 removeSelectedBuffers(true);
499 if(result == &_removeBufferAction) {
500 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
501 int res = QMessageBox::question(this, tr("Remove buffer permanently?"),
502 tr("Do you want to delete the buffer \"%1\" permanently? This will delete all related data, including all backlog "
503 "data, from the core's database!").arg(bufferInfo.bufferName()),
504 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
505 if(res == QMessageBox::Yes) {
506 Client::removeBuffer(bufferInfo.bufferId());
513 void BufferView::wheelEvent(QWheelEvent* event) {
514 if(UiSettings().value("MouseWheelChangesBuffers", QVariant(true)).toBool() == (bool)(event->modifiers() & Qt::AltModifier))
515 return QTreeView::wheelEvent(event);
517 int rowDelta = ( event->delta() > 0 ) ? -1 : 1;
518 QModelIndex currentIndex = selectionModel()->currentIndex();
519 QModelIndex resultingIndex;
520 if( model()->hasIndex( currentIndex.row() + rowDelta, currentIndex.column(), currentIndex.parent() ) )
522 resultingIndex = currentIndex.sibling( currentIndex.row() + rowDelta, currentIndex.column() );
524 else //if we scroll into a the parent node...
526 QModelIndex parent = currentIndex.parent();
527 QModelIndex aunt = parent.sibling( parent.row() + rowDelta, parent.column() );
529 resultingIndex = aunt.child( model()->rowCount( aunt ) - 1, 0 );
531 resultingIndex = aunt.child( 0, 0 );
532 if( !resultingIndex.isValid() )
535 selectionModel()->setCurrentIndex( resultingIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
536 selectionModel()->select( resultingIndex, QItemSelectionModel::ClearAndSelect );
541 QSize BufferView::sizeHint() const {
542 return QTreeView::sizeHint();
545 return QTreeView::sizeHint();
547 if(model()->rowCount() == 0)
548 return QSize(120, 50);
551 for(int i = 0; i < model()->columnCount(); i++) {
552 if(!isColumnHidden(i))
553 columnSize += sizeHintForColumn(i);
555 return QSize(columnSize, 50);
558 // ==============================
560 // ==============================
561 BufferViewDock::BufferViewDock(BufferViewConfig *config, QWidget *parent)
562 : QDockWidget(config->bufferViewName(), parent)
564 setObjectName("BufferViewDock-" + QString::number(config->bufferViewId()));
565 toggleViewAction()->setData(config->bufferViewId());
566 setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea);
567 connect(config, SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(bufferViewRenamed(const QString &)));
570 BufferViewDock::BufferViewDock(QWidget *parent)
571 : QDockWidget(tr("All Buffers"), parent)
573 setObjectName("BufferViewDock--1");
574 toggleViewAction()->setData((int)-1);
575 setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea);
578 void BufferViewDock::bufferViewRenamed(const QString &newName) {
579 setWindowTitle(newName);
580 toggleViewAction()->setText(newName);