1 /***************************************************************************
2 * Copyright (C) 2005-09 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 ***************************************************************************/
23 #include <QApplication>
24 #include <QHeaderView>
29 #include "buffermodel.h"
31 #include "contextmenuactionprovider.h"
32 #include "graphicalui.h"
34 #include "nickviewfilter.h"
35 #include "networkmodel.h"
38 NickView::NickView(QWidget *parent)
43 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 setSortingEnabled(true);
45 sortByColumn(0, Qt::AscendingOrder);
47 setContextMenuPolicy(Qt::CustomContextMenu);
48 setSelectionMode(QAbstractItemView::ExtendedSelection);
50 // // breaks with Qt 4.8
51 // if(QString("4.8.0") > qVersion()) // FIXME breaks with Qt versions >= 4.10!
54 connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&)));
56 #if defined Q_WS_QWS || defined Q_WS_X11
57 connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
59 // afaik this is better on Mac and Windows
60 connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
64 void NickView::init() {
68 for(int i = 1; i < model()->columnCount(); i++)
69 setColumnHidden(i, true);
71 connect(selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), SIGNAL(selectionUpdated()));
72 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SIGNAL(selectionUpdated()));
75 void NickView::setModel(QAbstractItemModel *model_) {
77 disconnect(model(), 0, this, 0);
79 QTreeView::setModel(model_);
83 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
84 QTreeView::rowsInserted(parent, start, end);
85 if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
86 unanimatedExpandAll();
90 void NickView::setRootIndex(const QModelIndex &index) {
91 QAbstractItemView::setRootIndex(index);
93 unanimatedExpandAll();
96 QModelIndexList NickView::selectedIndexes() const {
97 QModelIndexList indexList = QTreeView::selectedIndexes();
99 // make sure the item we clicked on is first
100 if(indexList.contains(currentIndex())) {
101 indexList.removeAll(currentIndex());
102 indexList.prepend(currentIndex());
108 void NickView::unanimatedExpandAll() {
109 // since of Qt Version 4.8.0 the default expandAll will not properly work if
110 // animations are enabled. Therefore we perform an unanimated expand when a
111 // model is set or a toplevel node is inserted.
112 bool wasAnimated = isAnimated();
115 setAnimated(wasAnimated);
119 void NickView::showContextMenu(const QPoint &pos ) {
122 QMenu contextMenu(this);
123 GraphicalUi::contextMenuActionProvider()->addActions(&contextMenu, selectedIndexes());
124 contextMenu.exec(QCursor::pos());
127 void NickView::startQuery(const QModelIndex &index) {
128 if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
131 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
132 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
133 if(!ircUser || !networkId.isValid())
136 Client::bufferModel()->switchToOrStartQuery(networkId, ircUser->nick());