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 <QHeaderView>
28 #include "buffermodel.h"
30 #include "nickviewfilter.h"
31 #include "networkmodel.h"
32 #include "quasselui.h"
35 class ExpandAllEvent : public QEvent {
37 ExpandAllEvent() : QEvent(QEvent::User) {}
40 NickView::NickView(QWidget *parent)
43 QAbstractItemDelegate *oldDelegate = itemDelegate();
44 NickViewDelegate *newDelegate = new NickViewDelegate(this);
45 setItemDelegate(newDelegate);
52 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
53 setSortingEnabled(true);
54 sortByColumn(0, Qt::AscendingOrder);
56 setContextMenuPolicy(Qt::CustomContextMenu);
57 setSelectionMode(QAbstractItemView::ExtendedSelection);
59 connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&)));
61 #if defined Q_WS_QWS || defined Q_WS_X11
62 connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
64 // afaik this is better on Mac and Windows
65 connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
69 void NickView::init() {
73 for(int i = 1; i < model()->columnCount(); i++)
74 setColumnHidden(i, true);
77 void NickView::setModel(QAbstractItemModel *model) {
78 QTreeView::setModel(model);
82 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
83 QTreeView::rowsInserted(parent, start, end);
84 if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
85 QCoreApplication::postEvent(this, new ExpandAllEvent);
89 void NickView::setRootIndex(const QModelIndex &index) {
90 QAbstractItemView::setRootIndex(index);
92 QCoreApplication::postEvent(this, new ExpandAllEvent);
95 void NickView::showContextMenu(const QPoint & pos ) {
96 QModelIndex index = indexAt(pos);
97 if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
100 QModelIndexList indexList = selectedIndexes();
101 // make sure the item we clicked on is first
102 indexList.removeAll(index);
103 indexList.prepend(index);
105 QMenu contextMenu(this);
106 Client::mainUi()->actionProvider()->addActions(&contextMenu, indexList);
107 contextMenu.exec(QCursor::pos());
110 void NickView::startQuery(const QModelIndex &index) {
111 if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
114 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
115 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
116 if(!ircUser || !networkId.isValid())
119 BufferId bufId = Client::networkModel()->bufferId(networkId, ircUser->nick());
121 Client::bufferModel()->switchToBuffer(bufId);
123 Client::userInput(index.data(NetworkModel::BufferInfoRole).value<BufferInfo>(), QString("/QUERY %1").arg(ircUser->nick()));
126 void NickView::customEvent(QEvent *event) {
127 // THIS IS A REPLACEMENT FOR expandAll()
128 /* WARNING: do not call expandAll()!
129 * it fucks up big time in combination with sorting and changing the rootIndex
130 * the following sequence of commands leads to unexpected behavior when inserting new items
131 * setSortingEnabled(true);
136 if(event->type() != QEvent::User)
139 QModelIndex topLevelIdx;
140 for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
141 topLevelIdx = model()->index(i, 0, rootIndex());
142 if(isExpanded(topLevelIdx))
146 if(i < model()->rowCount(rootIndex()) - 1)
147 QCoreApplication::postEvent(this, new ExpandAllEvent);
155 // ****************************************
157 // ****************************************
158 NickViewDelegate::NickViewDelegate(QObject *parent)
159 : QStyledItemDelegate(parent)
163 void NickViewDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const {
164 QStyledItemDelegate::initStyleOption(option, index);
166 if(!index.data(NetworkModel::ItemActiveRole).toBool())
167 option->palette.setColor(QPalette::Text, option->palette.color(QPalette::Dark));