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) : QTreeView(parent) {
50 setContextMenuPolicy(Qt::CustomContextMenu);
52 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
53 this, SLOT(showContextMenu(const QPoint &)));
55 setSelectionMode(QAbstractItemView::ExtendedSelection);
58 void BufferView::init() {
60 header()->setContextMenuPolicy(Qt::ActionsContextMenu);
67 #ifndef QT_NO_DRAGANDDROP
70 setDropIndicatorShown(true);
73 setSortingEnabled(true);
74 sortByColumn(0, Qt::AscendingOrder);
76 // this is a workaround to not join channels automatically... we need a saner way to navigate for qtopia anyway though,
77 // such as mark first, activate at second click...
78 connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
80 connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(joinChannel(QModelIndex))); // Qtopia uses single click for activation
84 void BufferView::setModel(QAbstractItemModel *model) {
85 delete selectionModel();
86 if(QTreeView::model()) {
87 disconnect(QTreeView::model(), SIGNAL(layoutChanged()), this, SLOT(layoutChanged()));
90 QTreeView::setModel(model);
93 QList<QAction *> oldactions = header()->actions();
94 foreach(QAction *action, oldactions) {
95 header()->removeAction(action);
96 action->deleteLater();
102 connect(model, SIGNAL(layoutChanged()), this, SLOT(layoutChanged()));
105 QAction *showSection;
106 for(int i = 1; i < model->columnCount(); i++) {
107 sectionName = (model->headerData(i, Qt::Horizontal, Qt::DisplayRole)).toString();
108 showSection = new QAction(sectionName, header());
109 showSection->setCheckable(true);
110 showSection->setChecked(!isColumnHidden(i));
111 showSection->setProperty("column", i);
112 connect(showSection, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
113 header()->addAction(showSection);
118 void BufferView::setFilteredModel(QAbstractItemModel *model_, BufferViewConfig *config) {
119 BufferViewFilter *filter = qobject_cast<BufferViewFilter *>(model());
121 filter->setConfig(config);
127 disconnect(this, 0, model(), 0);
133 BufferViewFilter *filter = new BufferViewFilter(model_, config);
135 connect(this, SIGNAL(removeBuffer(const QModelIndex &)),
136 filter, SLOT(removeBuffer(const QModelIndex &)));
141 void BufferView::setSelectionModel(QItemSelectionModel *selectionModel) {
142 if(QTreeView::selectionModel())
143 disconnect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
144 model(), SIGNAL(checkPreviousCurrentForRemoval(QModelIndex, QModelIndex)));
146 QTreeView::setSelectionModel(selectionModel);
147 BufferViewFilter *filter = qobject_cast<BufferViewFilter *>(model());
149 connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
150 filter, SLOT(checkPreviousCurrentForRemoval(QModelIndex, QModelIndex)));
154 void BufferView::setConfig(BufferViewConfig *config) {
155 if(_config == config)
159 disconnect(_config, 0, this, 0);
164 connect(config, SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(setRootIndexForNetworkId(const NetworkId &)));
165 setRootIndexForNetworkId(config->networkId());
167 setRootIndex(QModelIndex());
171 void BufferView::setRootIndexForNetworkId(const NetworkId &networkId) {
172 if(!networkId.isValid() || !model()) {
173 setRootIndex(QModelIndex());
175 int networkCount = model()->rowCount();
177 for(int i = 0; i < networkCount; i++) {
178 child = model()->index(i, 0);
179 if(networkId == model()->data(child, NetworkModel::NetworkIdRole).value<NetworkId>())
185 void BufferView::joinChannel(const QModelIndex &index) {
186 BufferInfo::Type bufferType = (BufferInfo::Type)index.data(NetworkModel::BufferTypeRole).value<int>();
188 if(bufferType != BufferInfo::ChannelBuffer)
191 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
193 Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
196 void BufferView::keyPressEvent(QKeyEvent *event) {
197 if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
199 removeSelectedBuffers();
201 QTreeView::keyPressEvent(event);
204 void BufferView::removeSelectedBuffers() {
205 QSet<int> removedRows;
206 foreach(QModelIndex index, selectionModel()->selectedIndexes()) {
207 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType && !removedRows.contains(index.row())) {
208 removedRows << index.row();
209 emit removeBuffer(index);
214 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
215 QTreeView::rowsInserted(parent, start, end);
217 // ensure that newly inserted network nodes are expanded per default
218 if(parent.data(NetworkModel::ItemTypeRole) != NetworkModel::NetworkItemType)
221 if(model()->rowCount(parent) == 1 && parent.data(NetworkModel::ItemActiveRole) == true) {
222 // without updating the parent the expand will have no effect... Qt Bug?
228 void BufferView::layoutChanged() {
231 // expand all active networks
232 QModelIndex networkIdx;
233 for(int row = 0; row < model()->rowCount(); row++) {
234 networkIdx = model()->index(row, 0);
236 if(model()->rowCount(networkIdx) > 0 && model()->data(networkIdx, NetworkModel::ItemActiveRole) == true) {
239 collapse(networkIdx);
243 // update selection to current one
244 MappedSelectionModel *mappedSelectionModel = qobject_cast<MappedSelectionModel *>(selectionModel());
245 if(!config() || !mappedSelectionModel)
248 mappedSelectionModel->mappedSetCurrentIndex(Client::bufferModel()->standardSelectionModel()->currentIndex(), QItemSelectionModel::Current);
249 mappedSelectionModel->mappedSelect(Client::bufferModel()->standardSelectionModel()->selection(), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
252 void BufferView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
253 QTreeView::dataChanged(topLeft, bottomRight);
255 // determine how many items have been changed and if any of them is a networkitem
256 // which just swichted from active to inactive or vice versa
257 if(topLeft.data(NetworkModel::ItemTypeRole) != NetworkModel::NetworkItemType)
260 for(int i = topLeft.row(); i <= bottomRight.row(); i++) {
261 QModelIndex networkIdx = topLeft.sibling(topLeft.row(), 0);
262 if(model()->rowCount(networkIdx) == 0)
265 bool isActive = networkIdx.data(NetworkModel::ItemActiveRole).toBool();
266 if(isExpanded(networkIdx) != isActive) setExpanded(networkIdx, isActive);
271 void BufferView::toggleHeader(bool checked) {
272 QAction *action = qobject_cast<QAction *>(sender());
273 header()->setSectionHidden((action->property("column")).toInt(), !checked);
276 void BufferView::showContextMenu(const QPoint &pos) {
277 QModelIndex index = indexAt(pos);
278 if(!index.isValid()) return;
279 QMenu contextMenu(this);
280 QAction *connectNetAction = contextMenu.addAction(tr("Connect"));
281 QAction *disconnectNetAction = contextMenu.addAction(tr("Disconnect"));
282 QAction *joinChannelAction = contextMenu.addAction(tr("Join Channel"));
284 QAction *joinBufferAction = contextMenu.addAction(tr("Join"));
285 QAction *partBufferAction = contextMenu.addAction(tr("Part"));
286 QAction *hideBufferAction = contextMenu.addAction(tr("Remove buffers"));
287 hideBufferAction->setToolTip(tr("Removes the selected buffers from a custom view but leaves the buffer itself untouched"));
288 QAction *removeBufferAction = contextMenu.addAction(tr("Delete buffer"));
290 QMenu *hideEventsMenu = contextMenu.addMenu(tr("Hide Events"));
291 QAction *hideJoinAction = hideEventsMenu->addAction(tr("Join Events"));
292 QAction *hidePartAction = hideEventsMenu->addAction(tr("Part Events"));
293 QAction *hideKillAction = hideEventsMenu->addAction(tr("Kill Events"));
294 QAction *hideQuitAction = hideEventsMenu->addAction(tr("Quit Events"));
295 QAction *hideModeAction = hideEventsMenu->addAction(tr("Mode Events"));
296 hideJoinAction->setCheckable(true);
297 hidePartAction->setCheckable(true);
298 hideKillAction->setCheckable(true);
299 hideQuitAction->setCheckable(true);
300 hideModeAction->setCheckable(true);
301 hideJoinAction->setEnabled(false);
302 hidePartAction->setEnabled(false);
303 hideKillAction->setEnabled(false);
304 hideQuitAction->setEnabled(false);
305 hideModeAction->setEnabled(false);
307 QAction *ignoreListAction = new QAction(tr("Ignore list"), this);
308 ignoreListAction->setEnabled(false);
309 QAction *whoBufferAction = new QAction(tr("WHO"), this);
311 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
312 if(index.data(NetworkModel::ItemActiveRole).toBool()) {
313 contextMenu.addAction(disconnectNetAction);
314 contextMenu.addSeparator();
315 contextMenu.addAction(joinChannelAction);
317 contextMenu.addAction(connectNetAction);
321 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
322 QString channelname = index.sibling(index.row(), 0).data().toString();
324 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
325 if(bufferInfo.type() != BufferInfo::ChannelBuffer && bufferInfo.type() != BufferInfo::QueryBuffer) return;
326 contextMenu.addAction(joinBufferAction);
327 contextMenu.addAction(partBufferAction);
329 contextMenu.addAction(hideBufferAction);
330 contextMenu.addAction(removeBufferAction);
331 contextMenu.addMenu(hideEventsMenu);
332 contextMenu.addAction(ignoreListAction);
333 contextMenu.addAction(whoBufferAction);
335 if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
336 if(index.data(NetworkModel::ItemActiveRole).toBool()) {
337 removeBufferAction->setEnabled(false);
338 removeBufferAction->setToolTip("To delete the buffer, part the channel first.");
339 joinBufferAction->setVisible(false);
340 whoBufferAction->setVisible(false);
342 partBufferAction->setVisible(false);
345 joinBufferAction->setVisible(false);
346 partBufferAction->setVisible(false);
350 QAction *result = contextMenu.exec(QCursor::pos());
351 if(result == connectNetAction || result == disconnectNetAction) {
352 const Network *network = Client::network(index.data(NetworkModel::NetworkIdRole).value<NetworkId>());
354 if(network->connectionState() == Network::Disconnected)
355 network->requestConnect();
357 network->requestDisconnect();
359 if(result == joinChannelAction) {
360 // FIXME no QInputDialog in Qtopia
363 QString channelName = QInputDialog::getText(this, tr("Join Channel"),
364 tr("Input channel name:"),QLineEdit::Normal,
365 QDir::home().dirName(), &ok);
367 if (ok && !channelName.isEmpty()) {
368 BufferInfo bufferInfo = index.child(0,0).data(NetworkModel::BufferInfoRole).value<BufferInfo>();
369 if(bufferInfo.isValid()) {
370 Client::instance()->userInput(bufferInfo, QString("/J %1").arg(channelName));
374 } else if(result == joinBufferAction) {
375 Client::instance()->userInput(bufferInfo, QString("/JOIN %1").arg(channelname));
376 } else if(result == partBufferAction) {
377 Client::instance()->userInput(bufferInfo, QString("/PART %1").arg(channelname));
378 } else if(result == hideBufferAction) {
379 removeSelectedBuffers();
380 } else if(result == removeBufferAction) {
381 int res = QMessageBox::question(this, tr("Remove buffer permanently?"),
382 tr("Do you want to delete the buffer \"%1\" permanently? This will delete all related data, including all backlog "
383 "data, from the core's database!").arg(bufferInfo.bufferName()),
384 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
385 if(res == QMessageBox::Yes) {
386 Client::removeBuffer(bufferInfo.bufferId());
389 if(result == whoBufferAction) {
390 Client::instance()->userInput(bufferInfo, QString("/WHO %1").arg(channelname));
394 void BufferView::wheelEvent(QWheelEvent* event) {
395 if(UiSettings().value("MouseWheelChangesBuffers", QVariant(true)).toBool() == (bool)(event->modifiers() & Qt::AltModifier))
396 return QTreeView::wheelEvent(event);
398 int rowDelta = ( event->delta() > 0 ) ? -1 : 1;
399 QModelIndex currentIndex = selectionModel()->currentIndex();
400 QModelIndex resultingIndex;
401 if( model()->hasIndex( currentIndex.row() + rowDelta, currentIndex.column(), currentIndex.parent() ) )
403 resultingIndex = currentIndex.sibling( currentIndex.row() + rowDelta, currentIndex.column() );
405 else //if we scroll into a the parent node...
407 QModelIndex parent = currentIndex.parent();
408 QModelIndex aunt = parent.sibling( parent.row() + rowDelta, parent.column() );
410 resultingIndex = aunt.child( model()->rowCount( aunt ) - 1, 0 );
412 resultingIndex = aunt.child( 0, 0 );
413 if( !resultingIndex.isValid() )
416 selectionModel()->setCurrentIndex( resultingIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
417 selectionModel()->select( resultingIndex, QItemSelectionModel::ClearAndSelect );
422 QSize BufferView::sizeHint() const {
423 return QTreeView::sizeHint();
426 return QTreeView::sizeHint();
428 if(model()->rowCount() == 0)
429 return QSize(120, 50);
432 for(int i = 0; i < model()->columnCount(); i++) {
433 if(!isColumnHidden(i))
434 columnSize += sizeHintForColumn(i);
436 return QSize(columnSize, 50);
439 // ==============================
441 // ==============================
442 BufferViewDock::BufferViewDock(BufferViewConfig *config, QWidget *parent)
443 : QDockWidget(config->bufferViewName(), parent)
445 setObjectName("BufferViewDock-" + QString::number(config->bufferViewId()));
446 toggleViewAction()->setData(config->bufferViewId());
447 setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea);
448 connect(config, SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(bufferViewRenamed(const QString &)));
451 BufferViewDock::BufferViewDock(QWidget *parent)
452 : QDockWidget(tr("All Buffers"), parent)
454 setObjectName("BufferViewDock--1");
455 toggleViewAction()->setData((int)-1);
456 setAllowedAreas(Qt::RightDockWidgetArea|Qt::LeftDockWidgetArea);
459 void BufferViewDock::bufferViewRenamed(const QString &newName) {
460 setWindowTitle(newName);
461 toggleViewAction()->setText(newName);