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"
34 #include "uisettings.h"
36 class ExpandAllEvent : public QEvent {
38 ExpandAllEvent() : QEvent(QEvent::User) {}
41 NickView::NickView(QWidget *parent)
44 QAbstractItemDelegate *oldDelegate = itemDelegate();
45 NickViewDelegate *newDelegate = new NickViewDelegate(this);
46 setItemDelegate(newDelegate);
53 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
54 setSortingEnabled(true);
55 sortByColumn(0, Qt::AscendingOrder);
57 setContextMenuPolicy(Qt::CustomContextMenu);
58 setSelectionMode(QAbstractItemView::ExtendedSelection);
60 connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&)));
62 #if defined Q_WS_QWS || defined Q_WS_X11
63 connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(startQuery(QModelIndex)));
65 // afaik this is better on Mac and Windows
66 connect(this, SIGNAL(activated(QModelIndex)), SLOT(startQuery(QModelIndex)));
70 void NickView::init() {
74 for(int i = 1; i < model()->columnCount(); i++)
75 setColumnHidden(i, true);
78 void NickView::setModel(QAbstractItemModel *model) {
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 QCoreApplication::postEvent(this, new ExpandAllEvent);
90 void NickView::setRootIndex(const QModelIndex &index) {
91 QAbstractItemView::setRootIndex(index);
93 QCoreApplication::postEvent(this, new ExpandAllEvent);
96 void NickView::showContextMenu(const QPoint & pos ) {
97 QModelIndex index = indexAt(pos);
98 if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
101 QModelIndexList indexList = selectedIndexes();
102 // make sure the item we clicked on is first
103 indexList.removeAll(index);
104 indexList.prepend(index);
106 QMenu contextMenu(this);
107 Client::mainUi()->actionProvider()->addActions(&contextMenu, indexList);
108 contextMenu.exec(QCursor::pos());
111 void NickView::startQuery(const QModelIndex &index) {
112 if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
115 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
116 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
117 if(!ircUser || !networkId.isValid())
120 BufferId bufId = Client::networkModel()->bufferId(networkId, ircUser->nick());
122 Client::bufferModel()->switchToBuffer(bufId);
124 Client::userInput(index.data(NetworkModel::BufferInfoRole).value<BufferInfo>(), QString("/QUERY %1").arg(ircUser->nick()));
127 void NickView::customEvent(QEvent *event) {
128 // THIS IS A REPLACEMENT FOR expandAll()
129 /* WARNING: do not call expandAll()!
130 * it fucks up big time in combination with sorting and changing the rootIndex
131 * the following sequence of commands leads to unexpected behavior when inserting new items
132 * setSortingEnabled(true);
137 if(event->type() != QEvent::User)
140 QModelIndex topLevelIdx;
141 for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
142 topLevelIdx = model()->index(i, 0, rootIndex());
143 if(isExpanded(topLevelIdx))
147 if(i < model()->rowCount(rootIndex()) - 1)
148 QCoreApplication::postEvent(this, new ExpandAllEvent);
156 // ****************************************
158 // ****************************************
159 NickViewDelegate::NickViewDelegate(QObject *parent)
160 : QStyledItemDelegate(parent)
162 UiSettings s("QtUiStyle/Colors");
163 _FgOnlineStatus = s.value("onlineStatusFG", QVariant(QColor(Qt::black))).value<QColor>();
164 _FgAwayStatus = s.value("awayStatusFG", QVariant(QColor(Qt::gray))).value<QColor>();
167 void NickViewDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const {
168 QStyledItemDelegate::initStyleOption(option, index);
173 QColor fgColor = _FgOnlineStatus;
174 if(!index.data(NetworkModel::ItemActiveRole).toBool())
175 fgColor = _FgAwayStatus;
177 option->palette.setColor(QPalette::Text, fgColor);