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 <QHeaderView>
26 #include "buffermodel.h"
29 #include "nickviewfilter.h"
30 #include "networkmodel.h"
31 #include "quasselui.h"
34 class ExpandAllEvent : public QEvent {
36 ExpandAllEvent() : QEvent(QEvent::User) {}
39 NickView::NickView(QWidget *parent)
45 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
46 setSortingEnabled(true);
47 sortByColumn(0, Qt::AscendingOrder);
49 setContextMenuPolicy(Qt::CustomContextMenu);
51 connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&)));
53 #if defined Q_WS_QWS || defined Q_WS_X11
54 connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
56 // afaik this is better on Mac and Windows
57 connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
61 void NickView::init() {
65 for(int i = 1; i < model()->columnCount(); i++)
66 setColumnHidden(i, true);
69 void NickView::setModel(QAbstractItemModel *model) {
70 QTreeView::setModel(model);
74 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
75 QTreeView::rowsInserted(parent, start, end);
76 if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
77 QCoreApplication::postEvent(this, new ExpandAllEvent);
81 void NickView::setRootIndex(const QModelIndex &index) {
82 QAbstractItemView::setRootIndex(index);
84 QCoreApplication::postEvent(this, new ExpandAllEvent);
87 void NickView::showContextMenu(const QPoint & pos ) {
88 QModelIndex index = indexAt(pos);
89 if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
92 QMenu contextMenu(this);
93 Client::mainUi()->actionProvider()->addActions(&contextMenu, index);
94 contextMenu.exec(QCursor::pos());
97 void NickView::startQuery(const QModelIndex &index) {
98 if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
101 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
102 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
103 if(!ircUser || !networkId.isValid())
106 BufferId bufId = Client::networkModel()->bufferId(networkId, ircUser->nick());
108 Client::bufferModel()->switchToBuffer(bufId);
110 Client::userInput(index.data(NetworkModel::BufferInfoRole).value<BufferInfo>(), QString("/QUERY %1").arg(ircUser->nick()));
113 void NickView::customEvent(QEvent *event) {
114 // THIS IS A REPLACEMENT FOR expandAll()
115 /* WARNING: do not call expandAll()!
116 * it fucks up big time in combination with sorting and changing the rootIndex
117 * the following sequence of commands leads to unexpected behavior when inserting new items
118 * setSortingEnabled(true);
123 if(event->type() != QEvent::User)
126 QModelIndex topLevelIdx;
127 for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
128 topLevelIdx = model()->index(i, 0, rootIndex());
129 if(isExpanded(topLevelIdx))
133 if(i < model()->rowCount(rootIndex()) - 1)
134 QCoreApplication::postEvent(this, new ExpandAllEvent);