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 _hideJoinAction.setCheckable(true);
71 _hidePartAction.setCheckable(true);
72 _hideKillAction.setCheckable(true);
73 _hideQuitAction.setCheckable(true);
74 _hideModeAction.setCheckable(true);
75 _hideJoinAction.setEnabled(false);
76 _hidePartAction.setEnabled(false);
77 _ignoreListAction.setEnabled(false);
78 _hideKillAction.setEnabled(false);
79 _hideQuitAction.setEnabled(false);
80 _hideModeAction.setEnabled(false);
82 connect(this, SIGNAL(collapsed(const QModelIndex &)), this, SLOT(on_collapse(const QModelIndex &)));
83 connect(this, SIGNAL(expanded(const QModelIndex &)), this, SLOT(on_expand(const QModelIndex &)));
85 setSelectionMode(QAbstractItemView::ExtendedSelection);
88 void BufferView::init() {
90 header()->setContextMenuPolicy(Qt::ActionsContextMenu);
97 #ifndef QT_NO_DRAGANDDROP
100 setDropIndicatorShown(true);
103 setSortingEnabled(true);
104 sortByColumn(0, Qt::AscendingOrder);
106 // this is a workaround to not join channels automatically... we need a saner way to navigate for qtopia anyway though,
107 // such as mark first, activate at second click...
108 connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
110 connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(joinChannel(QModelIndex))); // Qtopia uses single click for activation
114 void BufferView::setModel(QAbstractItemModel *model) {
115 delete selectionModel();
117 QTreeView::setModel(model);
119 // remove old Actions
120 QList<QAction *> oldactions = header()->actions();
121 foreach(QAction *action, oldactions) {
122 header()->removeAction(action);
123 action->deleteLater();
130 QAction *showSection;
131 for(int i = 1; i < model->columnCount(); i++) {
132 sectionName = (model->headerData(i, Qt::Horizontal, Qt::DisplayRole)).toString();
133 showSection = new QAction(sectionName, header());
134 showSection->setCheckable(true);
135 showSection->setChecked(!isColumnHidden(i));
136 showSection->setProperty("column", i);
137 connect(showSection, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
138 header()->addAction(showSection);
143 void BufferView::setFilteredModel(QAbstractItemModel *model_, BufferViewConfig *config) {
144 BufferViewFilter *filter = qobject_cast<BufferViewFilter *>(model());
146 filter->setConfig(config);
152 disconnect(this, 0, model(), 0);
153 disconnect(model(), 0, this, 0);
159 BufferViewFilter *filter = new BufferViewFilter(model_, config);
161 connect(filter, SIGNAL(configChanged()), this, SLOT(on_configChanged()));
166 void BufferView::setSelectionModel(QItemSelectionModel *selectionModel) {
167 if(QTreeView::selectionModel())
168 disconnect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
169 model(), SIGNAL(checkPreviousCurrentForRemoval(QModelIndex, QModelIndex)));
171 QTreeView::setSelectionModel(selectionModel);
172 BufferViewFilter *filter = qobject_cast<BufferViewFilter *>(model());
174 connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
175 filter, SLOT(checkPreviousCurrentForRemoval(QModelIndex, QModelIndex)));
179 void BufferView::setConfig(BufferViewConfig *config) {
180 if(_config == config)
184 disconnect(_config, 0, this, 0);
189 connect(config, SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(setRootIndexForNetworkId(const NetworkId &)));
190 setRootIndexForNetworkId(config->networkId());
192 setRootIndex(QModelIndex());
196 void BufferView::setRootIndexForNetworkId(const NetworkId &networkId) {
197 if(!networkId.isValid() || !model()) {
198 setRootIndex(QModelIndex());
200 int networkCount = model()->rowCount();
202 for(int i = 0; i < networkCount; i++) {
203 child = model()->index(i, 0);
204 if(networkId == model()->data(child, NetworkModel::NetworkIdRole).value<NetworkId>())
210 void BufferView::joinChannel(const QModelIndex &index) {
211 BufferInfo::Type bufferType = (BufferInfo::Type)index.data(NetworkModel::BufferTypeRole).value<int>();
213 if(bufferType != BufferInfo::ChannelBuffer)
216 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
218 Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
221 void BufferView::keyPressEvent(QKeyEvent *event) {
222 if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
224 removeSelectedBuffers();
226 QTreeView::keyPressEvent(event);
229 void BufferView::removeSelectedBuffers(bool permanently) {
234 QSet<BufferId> removedRows;
235 foreach(QModelIndex index, selectionModel()->selectedIndexes()) {
236 if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::BufferItemType)
239 bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
240 if(removedRows.contains(bufferId))
243 removedRows << bufferId;
246 config()->requestRemoveBufferPermanently(bufferId);
248 config()->requestRemoveBuffer(bufferId);
252 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
253 QTreeView::rowsInserted(parent, start, end);
255 // ensure that newly inserted network nodes are expanded per default
256 if(parent.data(NetworkModel::ItemTypeRole) != NetworkModel::NetworkItemType)
259 if(model()->rowCount(parent) == 1 && parent.data(NetworkModel::ItemActiveRole) == true) {
260 // without updating the parent the expand will have no effect... Qt Bug?
266 void BufferView::on_configChanged() {
269 // expand all active networks... collapse inactive ones... unless manually changed
270 QModelIndex networkIdx;
272 for(int row = 0; row < model()->rowCount(); row++) {
273 networkIdx = model()->index(row, 0);
274 if(model()->rowCount(networkIdx) == 0)
277 networkId = model()->data(networkIdx, NetworkModel::NetworkIdRole).value<NetworkId>();
278 if(!networkId.isValid())
283 bool expandNetwork = false;
284 if(_expandedState.contains(networkId))
285 expandNetwork = _expandedState[networkId];
287 expandNetwork = model()->data(networkIdx, NetworkModel::ItemActiveRole).toBool();
292 collapse(networkIdx);
295 // update selection to current one
296 MappedSelectionModel *mappedSelectionModel = qobject_cast<MappedSelectionModel *>(selectionModel());
297 if(!config() || !mappedSelectionModel)
300 mappedSelectionModel->mappedSetCurrentIndex(Client::bufferModel()->standardSelectionModel()->currentIndex(), QItemSelectionModel::Current);
301 mappedSelectionModel->mappedSelect(Client::bufferModel()->standardSelectionModel()->selection(), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
304 void BufferView::on_collapse(const QModelIndex &index) {
305 storeExpandedState(index.data(NetworkModel::NetworkIdRole).value<NetworkId>(), false);
308 void BufferView::on_expand(const QModelIndex &index) {
309 storeExpandedState(index.data(NetworkModel::NetworkIdRole).value<NetworkId>(), true);
312 void BufferView::storeExpandedState(NetworkId networkId, bool expanded) {
313 _expandedState[networkId] = expanded;
316 void BufferView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
317 QTreeView::dataChanged(topLeft, bottomRight);
319 // determine how many items have been changed and if any of them is a networkitem
320 // which just swichted from active to inactive or vice versa
321 if(topLeft.data(NetworkModel::ItemTypeRole) != NetworkModel::NetworkItemType)
324 for(int i = topLeft.row(); i <= bottomRight.row(); i++) {
325 QModelIndex networkIdx = topLeft.sibling(topLeft.row(), 0);
326 if(model()->rowCount(networkIdx) == 0)
329 bool isActive = networkIdx.data(NetworkModel::ItemActiveRole).toBool();
330 if(isExpanded(networkIdx) != isActive) setExpanded(networkIdx, isActive);
334 void BufferView::toggleHeader(bool checked) {
335 QAction *action = qobject_cast<QAction *>(sender());
336 header()->setSectionHidden((action->property("column")).toInt(), !checked);
339 bool BufferView::checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState) {
343 ItemActiveStates isActive = index.data(NetworkModel::ItemActiveRole).toBool()
347 if(!(isActive & requiredActiveState))
353 void BufferView::addItemToMenu(QAction &action, QMenu &menu, const QModelIndex &index, ItemActiveStates requiredActiveState) {
354 if(checkRequirements(index, requiredActiveState)) {
355 menu.addAction(&action);
356 action.setVisible(true);
358 action.setVisible(false);
362 void BufferView::addItemToMenu(QAction &action, QMenu &menu, bool condition) {
364 menu.addAction(&action);
365 action.setVisible(true);
367 action.setVisible(false);
372 void BufferView::addItemToMenu(QMenu &subMenu, QMenu &menu, const QModelIndex &index, ItemActiveStates requiredActiveState) {
373 if(checkRequirements(index, requiredActiveState)) {
374 menu.addMenu(&subMenu);
375 subMenu.setVisible(true);
377 subMenu.setVisible(false);
381 void BufferView::addSeparatorToMenu(QMenu &menu, const QModelIndex &index, ItemActiveStates requiredActiveState) {
382 if(checkRequirements(index, requiredActiveState)) {
387 QMenu *BufferView::createHideEventsSubMenu(QMenu &menu) {
388 // QMenu *hideEventsMenu = new QMenu(tr("Hide Events"), &menu);
389 QMenu *hideEventsMenu = menu.addMenu(tr("Hide Events"));
390 hideEventsMenu->addAction(&_hideJoinAction);
391 hideEventsMenu->addAction(&_hidePartAction);
392 hideEventsMenu->addAction(&_hideKillAction);
393 hideEventsMenu->addAction(&_hideQuitAction);
394 hideEventsMenu->addAction(&_hideModeAction);
395 return hideEventsMenu;
398 void BufferView::contextMenuEvent(QContextMenuEvent *event) {
399 QModelIndex index = indexAt(event->pos());
403 const Network *network = Client::network(index.data(NetworkModel::NetworkIdRole).value<NetworkId>());
404 Q_CHECK_PTR(network);
406 QIcon connectionStateIcon;
408 if(network->connectionState() == Network::Initialized) {
409 connectionStateIcon = QIcon(":/22x22/actions/network-connect");
410 } else if(network->connectionState() == Network::Disconnected) {
411 connectionStateIcon = QIcon(":/22x22/actions/network-disconnect");
413 connectionStateIcon = QIcon(":/22x22/actions/gear");
417 QMenu contextMenu(this);
418 NetworkModel::itemType itemType = static_cast<NetworkModel::itemType>(index.data(NetworkModel::ItemTypeRole).toInt());
421 case NetworkModel::NetworkItemType:
422 showChannelList.setData(index.data(NetworkModel::NetworkIdRole));
423 _disconnectNetAction.setIcon(connectionStateIcon);
424 _connectNetAction.setIcon(connectionStateIcon);
425 addItemToMenu(showChannelList, contextMenu, index, ActiveState);
426 addItemToMenu(_disconnectNetAction, contextMenu, index, ActiveState);
427 addItemToMenu(_connectNetAction, contextMenu, index, InactiveState);
428 addSeparatorToMenu(contextMenu, index, ActiveState);
429 addItemToMenu(_joinChannelAction, contextMenu, index, ActiveState);
431 case NetworkModel::BufferItemType:
433 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
434 switch(bufferInfo.type()) {
435 case BufferInfo::ChannelBuffer:
436 addItemToMenu(_joinBufferAction, contextMenu, index, InactiveState);
437 addItemToMenu(_partBufferAction, contextMenu, index, ActiveState);
438 addItemToMenu(_hideBufferTemporarilyAction, contextMenu, (bool)config());
439 addItemToMenu(_hideBufferPermanentlyAction, contextMenu, (bool)config());
440 addItemToMenu(_removeBufferAction, contextMenu, index, InactiveState);
441 createHideEventsSubMenu(contextMenu);
442 addItemToMenu(_ignoreListAction, contextMenu);
444 case BufferInfo::QueryBuffer:
445 addItemToMenu(_hideBufferTemporarilyAction, contextMenu, (bool)config());
446 addItemToMenu(_hideBufferPermanentlyAction, contextMenu, (bool)config());
447 addItemToMenu(_removeBufferAction, contextMenu);
448 createHideEventsSubMenu(contextMenu);
451 addItemToMenu(_hideBufferTemporarilyAction, contextMenu, (bool)config());
452 addItemToMenu(_hideBufferPermanentlyAction, contextMenu, (bool)config());
461 if(contextMenu.actions().isEmpty())
463 QAction *result = contextMenu.exec(QCursor::pos());
466 if(network && result == &_connectNetAction) {
467 network->requestConnect();
471 if(network && result == &_disconnectNetAction) {
472 network->requestDisconnect();
476 if(result == &_joinChannelAction) {
477 // FIXME no QInputDialog in Qtopia
480 QString channelName = QInputDialog::getText(this, tr("Join Channel"), tr("Input channel name:"), QLineEdit::Normal, QString(), &ok);
481 if(ok && !channelName.isEmpty()) {
482 Client::instance()->userInput(BufferInfo::fakeStatusBuffer(index.data(NetworkModel::NetworkIdRole).value<NetworkId>()), QString("/J %1").arg(channelName));
488 if(result == &_joinBufferAction) {
489 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
490 Client::instance()->userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
494 if(result == &_partBufferAction) {
495 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
496 Client::instance()->userInput(bufferInfo, QString("/PART"));
500 if(result == &_hideBufferTemporarilyAction) {
501 removeSelectedBuffers();
505 if(result == &_hideBufferPermanentlyAction) {
506 removeSelectedBuffers(true);
510 if(result == &_removeBufferAction) {
511 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
512 int res = QMessageBox::question(this, tr("Remove buffer permanently?"),
513 tr("Do you want to delete the buffer \"%1\" permanently? This will delete all related data, including all backlog "
514 "data, from the core's database!").arg(bufferInfo.bufferName()),
515 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
516 if(res == QMessageBox::Yes) {
517 Client::removeBuffer(bufferInfo.bufferId());
524 void BufferView::wheelEvent(QWheelEvent* event) {
525 if(UiSettings().value("MouseWheelChangesBuffers", QVariant(true)).toBool() == (bool)(event->modifiers() & Qt::AltModifier))
526 return QTreeView::wheelEvent(event);
528 int rowDelta = ( event->delta() > 0 ) ? -1 : 1;
529 QModelIndex currentIndex = selectionModel()->currentIndex();
530 QModelIndex resultingIndex;
531 if( model()->hasIndex( currentIndex.row() + rowDelta, currentIndex.column(), currentIndex.parent() ) )
533 resultingIndex = currentIndex.sibling( currentIndex.row() + rowDelta, currentIndex.column() );
535 else //if we scroll into a the parent node...
537 QModelIndex parent = currentIndex.parent();
538 QModelIndex aunt = parent.sibling( parent.row() + rowDelta, parent.column() );
540 resultingIndex = aunt.child( model()->rowCount( aunt ) - 1, 0 );
542 resultingIndex = aunt.child( 0, 0 );
543 if( !resultingIndex.isValid() )
546 selectionModel()->setCurrentIndex( resultingIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
547 selectionModel()->select( resultingIndex, QItemSelectionModel::ClearAndSelect );
552 QSize BufferView::sizeHint() const {
553 return QTreeView::sizeHint();
556 return QTreeView::sizeHint();
558 if(model()->rowCount() == 0)
559 return QSize(120, 50);
562 for(int i = 0; i < model()->columnCount(); i++) {
563 if(!isColumnHidden(i))
564 columnSize += sizeHintForColumn(i);
566 return QSize(columnSize, 50);
569 // ==============================
571 // ==============================
572 BufferViewDock::BufferViewDock(BufferViewConfig *config, QWidget *parent)
573 : QDockWidget(config->bufferViewName(), parent)
575 setObjectName("BufferViewDock-" + QString::number(config->bufferViewId()));
576 toggleViewAction()->setData(config->bufferViewId());
577 setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea);
578 connect(config, SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(bufferViewRenamed(const QString &)));
581 BufferViewDock::BufferViewDock(QWidget *parent)
582 : QDockWidget(tr("All Buffers"), parent)
584 setObjectName("BufferViewDock--1");
585 toggleViewAction()->setData((int)-1);
586 setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea);
589 void BufferViewDock::bufferViewRenamed(const QString &newName) {
590 setWindowTitle(newName);
591 toggleViewAction()->setText(newName);